PageRenderTime 288ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/code/trunk/modules/mod_artof_articles/helper.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 229 lines | 136 code | 27 blank | 66 comment | 16 complexity | 271a8667f50b2df3677fb86e6a06602c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id:combolayout.php 2547 2007-11-10 04:37:15Z masterchief $
  4. * @package Artof.Modules
  5. * @subpackage mod_artof_articles
  6. * @copyright Copyright 2005 - 2010 New Life in IT Pty Ltd. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @author Andrew Eddie <andrew.eddie@newlifeinit.com>
  9. * @link http://www.theartofjoomla.com/extensions/mod-artof-articles.html
  10. */
  11. // No direct access.
  12. defined('JPATH_BASE') or die();
  13. require_once JPATH_SITE.'/components/com_content/helpers/route.php';
  14. /**
  15. * @package Artof.Modules
  16. * @subpackage mod_artof_articles
  17. * @since 1.0.0
  18. */
  19. class modArtofArticlesHelper
  20. {
  21. /**
  22. * Get a list of the articles for the given filter parameters.
  23. *
  24. * @param JObject $params The filter parameters.
  25. *
  26. * @return array A list of articles.
  27. * @since 1.0.0
  28. */
  29. public static function getList(&$params)
  30. {
  31. $db = JFactory::getDBO();
  32. $user = JFactory::getUser();
  33. $userId = (int) $user->get('id');
  34. $count = (int) $params->get('count', 5);
  35. $catId = trim($params->get('catid'));
  36. $aid = $user->get('aid', 0);
  37. $config = JComponentHelper::getParams('com_content');
  38. $access = !$config->get('show_noauth');
  39. $nullDate = $db->getNullDate();
  40. $date = JFactory::getDate();
  41. $now = $date->toMySQL();
  42. $where = 'a.state = 1'
  43. . ' AND (a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).')'
  44. . ' AND (a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).')';
  45. // Ordering
  46. switch ($params->get('ordering'))
  47. {
  48. default:
  49. case 'o_asc':
  50. $ordering = 'a.ordering ASC';
  51. break;
  52. case 'o_dsc':
  53. $ordering = 'a.ordering DESC';
  54. break;
  55. case 'm_dsc':
  56. $ordering = 'a.modified DESC, a.created DESC';
  57. break;
  58. case 'c_dsc':
  59. $ordering = 'a.created DESC';
  60. break;
  61. }
  62. if ($catId) {
  63. $ids = explode(',', $catId);
  64. JArrayHelper::toInteger($ids);
  65. $catCondition = ' AND (cc.id=' . implode(' OR cc.id=', $ids) . ')';
  66. }
  67. // Content Items only
  68. $db->setQuery(
  69. 'SELECT a.id, a.title, a.alias, a.created_by_alias, a.access, a.sectionid, ' .
  70. ($params->get('show_intro') ? 'a.introtext, ' : '').
  71. ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
  72. ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
  73. ' CASE WHEN CHAR_LENGTH(a.created_by_alias) THEN a.created_by_alias ELSE u.name END as author'.
  74. ' FROM #__content AS a' .
  75. ' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
  76. ' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
  77. ' INNER JOIN #__users AS u ON u.id = a.created_by' .
  78. ' WHERE '. $where .' AND s.id > 0' .
  79. ($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
  80. ($catId ? $catCondition : '').
  81. ' AND s.published = 1' .
  82. ' AND cc.published = 1' .
  83. ' ORDER BY '. $ordering,
  84. 0, $count
  85. );
  86. $rows = $db->loadObjectList();
  87. if ($error = $db->getErrorMsg()) {
  88. JError::raiseWarning(500, $error);
  89. return;
  90. }
  91. foreach ($rows as &$row)
  92. {
  93. if($row->access <= $aid) {
  94. $row->link = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));
  95. }
  96. else {
  97. $row->link = JRoute::_('index.php?option=com_user&view=login');
  98. }
  99. if ($params->get('show_intro')) {
  100. $row->text = JHtml::_('content.prepare', $row->introtext);
  101. }
  102. else {
  103. $row->text = '';
  104. }
  105. }
  106. return $rows;
  107. }
  108. /**
  109. * Get the route for the Viewall link.
  110. *
  111. * @param JObject The module parameters
  112. *
  113. * @return string The route for the link.
  114. * @since 1.0.0
  115. */
  116. public static function getViewallRoute(&$params)
  117. {
  118. $categoryId = (int) $params->get('catid');
  119. $db = JFactory::getDbo();
  120. $db->setQuery(
  121. 'SELECT section' .
  122. ' FROM #__categories' .
  123. ' WHERE id = '.$categoryId
  124. );
  125. $sectionId = (int) $db->loadResult();
  126. if ($error = $db->getErrorMsg()) {
  127. JError::raiseError(500, $error);
  128. return false;
  129. }
  130. else {
  131. return ContentHelperRoute::getCategoryRoute($categoryId, $sectionId);
  132. }
  133. }
  134. /**
  135. * Gets a thumbnail from the first image in the article.
  136. *
  137. * @param string $text The source text.
  138. * @param boolean $autoThumb True to create a sized thumbnail, false to use the native image.
  139. * @param int $width The width of the thumbnail.
  140. * @param int $height The height of the thumbnail.
  141. * @param string $alt The alt text for the image.
  142. * @param string $align The align.
  143. * @param string $title The title for the image.
  144. *
  145. * @return string
  146. */
  147. public static function getThumb(&$text, $autoThumb = false, $width = null, $height = null, $alt = null, $align = null, $title = null)
  148. {
  149. $result = '';
  150. // lets get the first image out of the article and use it for the main image
  151. preg_match('@<img[^>]*src=\"([^\"]*)\"[^>]*>@Usi', $text, $matches);
  152. $images = (count($matches)) ? $matches : array ();
  153. if (count($images)) {
  154. // did we find an image?
  155. if ($image = $images[1]) {
  156. $attribs = ($align) ? ' align="'.$align.'"' : '';
  157. if ($autoThumb) {
  158. require_once dirname(__FILE__).'/image.php';
  159. //and ($thumb = $this->makeThumb($image, $width, $height))) {
  160. // $image = '<img src="'.$thumb.'" alt="'.$row->title.'"'.$align.' />';
  161. $src = JXHTMLMedia::thumb($image, $width, $height);
  162. }
  163. else {
  164. $src = $image;
  165. }
  166. $attribs .= ($width) ? ' width="'.(int) $width.'"' : '';
  167. $attribs .= ($height) ? ' height="'.(int) $height.'"' : '';
  168. $result = '<img src="'.$image.'" alt="'.$alt.'"'.$attribs.' />';
  169. // Remove the image from the text.
  170. $text = str_replace($images[0], '', $text);
  171. }
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Truncates text blocks over the specified character limit. The
  177. * behavior will not truncate an individual word, it will find the first
  178. * space that is within the limit and truncate at that point. This
  179. * method is UTF-8 safe.
  180. *
  181. * @param string $text The text to truncate.
  182. * @param int $length The maximum length of the text.
  183. *
  184. * @return string The truncated text.
  185. * @since 1.0.0
  186. */
  187. public static function truncate($text, $length = 0, $append = '...')
  188. {
  189. // Truncate the item text if it is too long.
  190. if ($length > 0 && JString::strlen($text) > $length) {
  191. // Find the first space within the allowed length.
  192. $tmp = JString::substr($text, 0, $length);
  193. $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
  194. // If we don't have 3 characters of room, go to the second space within the limit.
  195. if (JString::strlen($tmp) >= $length - 3) {
  196. $tmp = JString::substr($tmp, 0, JString::strrpos($tmp, ' '));
  197. }
  198. $text = $tmp.$append;
  199. }
  200. return $text;
  201. }
  202. }