PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/modules/mod_articles_category/helper.php

https://github.com/reechalee/joomla1.6
PHP | 457 lines | 322 code | 83 blank | 52 comment | 59 complexity | 59bc0809901c98e2fa878ee2ddb3d221 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. * @version $Id: helper.php 20807 2011-02-21 19:51:41Z dextercowley $
  4. * @package Joomla.Site
  5. * @subpackage mod_articles_category
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.model');
  12. $com_path = JPATH_SITE.'/components/com_content/';
  13. require_once $com_path.'router.php';
  14. require_once $com_path.'helpers/route.php';
  15. jimport('joomla.application.component.model');
  16. JModel::addIncludePath($com_path.DS.'models', 'ContentModel');
  17. abstract class modArticlesCategoryHelper
  18. {
  19. public static function getList(&$params)
  20. {
  21. // Get an instance of the generic articles model
  22. $articles = JModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
  23. // Set application parameters in model
  24. $app = JFactory::getApplication();
  25. $appParams = $app->getParams();
  26. $articles->setState('params', $appParams);
  27. // Set the filters based on the module params
  28. $articles->setState('list.start', 0);
  29. $articles->setState('list.limit', (int) $params->get('count', 0));
  30. $articles->setState('filter.published', 1);
  31. // Access filter
  32. $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
  33. $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
  34. $articles->setState('filter.access', $access);
  35. // Prep for Normal or Dynamic Modes
  36. $mode = $params->get('mode', 'normal');
  37. switch ($mode)
  38. {
  39. case 'dynamic':
  40. $option = JRequest::getCmd('option');
  41. $view = JRequest::getCmd('view');
  42. if ($option === 'com_content') {
  43. switch($view)
  44. {
  45. case 'category':
  46. $catids = array(JRequest::getInt('id'));
  47. break;
  48. case 'categories':
  49. $catids = array(JRequest::getInt('id'));
  50. break;
  51. case 'article':
  52. if ($params->get('show_on_article_page', 1)) {
  53. $article_id = JRequest::getInt('id');
  54. $catid = JRequest::getInt('catid');
  55. if (!$catid) {
  56. // Get an instance of the generic article model
  57. $article = JModel::getInstance('Article', 'ContentModel', array('ignore_request' => true));
  58. $article->setState('params', $appParams);
  59. $article->setState('filter.published', 1);
  60. $article->setState('article.id', (int) $article_id);
  61. $item = $article->getItem();
  62. $catids = array($item->catid);
  63. }
  64. else {
  65. $catids = array($catid);
  66. }
  67. }
  68. else {
  69. // Return right away if show_on_article_page option is off
  70. return;
  71. }
  72. break;
  73. case 'featured':
  74. default:
  75. // Return right away if not on the category or article views
  76. return;
  77. }
  78. }
  79. else {
  80. // Return right away if not on a com_content page
  81. return;
  82. }
  83. break;
  84. case 'normal':
  85. default:
  86. $catids = $params->get('catid');
  87. $articles->setState('filter.category_id.include', (bool) $params->get('category_filtering_type', 1));
  88. break;
  89. }
  90. // Category filter
  91. if ($catids) {
  92. if ($params->get('show_child_category_articles', 0) && (int) $params->get('levels', 0) > 0) {
  93. // Get an instance of the generic categories model
  94. $categories = JModel::getInstance('Categories', 'ContentModel', array('ignore_request' => true));
  95. $categories->setState('params', $appParams);
  96. $levels = $params->get('levels', 1) ? $params->get('levels', 1) : 9999;
  97. $categories->setState('filter.get_children', $levels);
  98. $categories->setState('filter.published', 1);
  99. $categories->setState('filter.access', $access);
  100. $additional_catids = array();
  101. foreach($catids as $catid)
  102. {
  103. $categories->setState('filter.parentId', $catid);
  104. $recursive = true;
  105. $items = $categories->getItems($recursive);
  106. if ($items)
  107. {
  108. foreach($items as $category)
  109. {
  110. $condition = (($category->level - $categories->getParent()->level) <= $levels);
  111. if ($condition) {
  112. $additional_catids[] = $category->id;
  113. }
  114. }
  115. }
  116. }
  117. $catids = array_unique(array_merge($catids, $additional_catids));
  118. }
  119. $articles->setState('filter.category_id', $catids);
  120. }
  121. // Ordering
  122. $articles->setState('list.ordering', $params->get('article_ordering', 'a.ordering'));
  123. $articles->setState('list.direction', $params->get('article_ordering_direction', 'ASC'));
  124. // New Parameters
  125. $articles->setState('filter.featured', $params->get('show_front', 'show'));
  126. $articles->setState('filter.author_id', $params->get('created_by', ""));
  127. $articles->setState('filter.author_id.include', $params->get('author_filtering_type', 1));
  128. $articles->setState('filter.author_alias', $params->get('created_by_alias', ""));
  129. $articles->setState('filter.author_alias.include', $params->get('author_alias_filtering_type', 1));
  130. $excluded_articles = $params->get('excluded_articles', '');
  131. if ($excluded_articles) {
  132. $excluded_articles = explode("\r\n", $excluded_articles);
  133. $articles->setState('filter.article_id', $excluded_articles);
  134. $articles->setState('filter.article_id.include', false); // Exclude
  135. }
  136. $date_filtering = $params->get('date_filtering', 'off');
  137. if ($date_filtering !== 'off') {
  138. $articles->setState('filter.date_filtering', $date_filtering);
  139. $articles->setState('filter.date_field', $params->get('date_field', 'a.created'));
  140. $articles->setState('filter.start_date_range', $params->get('start_date_range', '1000-01-01 00:00:00'));
  141. $articles->setState('filter.end_date_range', $params->get('end_date_range', '9999-12-31 23:59:59'));
  142. $articles->setState('filter.relative_date', $params->get('relative_date', 30));
  143. }
  144. // Filter by language
  145. $articles->setState('filter.language',$app->getLanguageFilter());
  146. $items = $articles->getItems();
  147. // Display options
  148. $show_date = $params->get('show_date', 0);
  149. $show_date_field = $params->get('show_date_field', 'created');
  150. $show_date_format = $params->get('show_date_format', 'Y-m-d H:i:s');
  151. $show_category = $params->get('show_category', 0);
  152. $show_hits = $params->get('show_hits', 0);
  153. $show_author = $params->get('show_author', 0);
  154. $show_introtext = $params->get('show_introtext', 0);
  155. $introtext_limit = $params->get('introtext_limit', 100);
  156. // Find current Article ID if on an article page
  157. $option = JRequest::getCmd('option');
  158. $view = JRequest::getCmd('view');
  159. if ($option === 'com_content' && $view === 'article') {
  160. $active_article_id = JRequest::getInt('id');
  161. }
  162. else {
  163. $active_article_id = 0;
  164. }
  165. // Prepare data for display using display options
  166. foreach ($items as &$item)
  167. {
  168. $item->slug = $item->id.':'.$item->alias;
  169. $item->catslug = $item->catid ? $item->catid .':'.$item->category_alias : $item->catid;
  170. if ($access || in_array($item->access, $authorised)) {
  171. // We know that user has the privilege to view the article
  172. $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug));
  173. }
  174. else {
  175. // Angie Fixed Routing
  176. $app = JFactory::getApplication();
  177. $menu = $app->getMenu();
  178. $menuitems = $menu->getItems('link', 'index.php?option=com_users&view=login');
  179. if(isset($menuitems[0])) {
  180. $Itemid = $menuitems[0]->id;
  181. } else if (JRequest::getInt('Itemid') > 0) { //use Itemid from requesting page only if there is no existing menu
  182. $Itemid = JRequest::getInt('Itemid');
  183. }
  184. $item->link = JRoute::_('index.php?option=com_users&view=login&Itemid='.$Itemid);
  185. }
  186. // Used for styling the active article
  187. $item->active = $item->id == $active_article_id ? 'active' : '';
  188. $item->displayDate = '';
  189. if ($show_date) {
  190. $date = new JDate($item->$show_date_field);
  191. $item->displayDate= $date->format($show_date_format);
  192. }
  193. if ($item->catid) {
  194. $item->displayCategoryLink = JRoute::_(ContentHelperRoute::getCategoryRoute($item->catid));
  195. $item->displayCategoryTitle = $show_category ? '<a href="'.$item->displayCategoryLink.'">'.$item->category_title.'</a>' : '';
  196. }
  197. else {
  198. $item->displayCategoryTitle = $show_category ? $item->category_title : '';
  199. }
  200. $item->displayHits = $show_hits ? $item->hits : '';
  201. $item->displayAuthorName = $show_author ? $item->author : '';
  202. $item->introtext = JHtml::_('content.prepare', $item->introtext);
  203. $item->introtext = self::_cleanIntrotext($item->introtext);
  204. $item->displayIntrotext = $show_introtext ? self::truncate($item->introtext, $introtext_limit) : '';
  205. // added Angie show_unauthorizid
  206. $item->displayReadmore = $item->alternative_readmore;
  207. }
  208. return $items;
  209. }
  210. public static function _cleanIntrotext($introtext)
  211. {
  212. $introtext = str_replace('<p>', ' ', $introtext);
  213. $introtext = str_replace('</p>', ' ', $introtext);
  214. $introtext = strip_tags($introtext, '<a><em><strong>');
  215. $introtext = trim($introtext);
  216. return $introtext;
  217. }
  218. /**
  219. * This is a better truncate implementation than what we
  220. * currently have available in the library. In particular,
  221. * on index.php/Banners/Banners/site-map.html JHtml's truncate
  222. * method would only return "Article...". This implementation
  223. * was taken directly from the Stack Overflow thread referenced
  224. * below. It was then modified to return a string rather than
  225. * print out the output and made to use the relevant JString
  226. * methods.
  227. *
  228. * @link http://stackoverflow.com/questions/1193500/php-truncate-html-ignoring-tags
  229. * @param mixed $html
  230. * @param mixed $maxLength
  231. */
  232. public static function truncate($html, $maxLength = 0)
  233. {
  234. $printedLength = 0;
  235. $position = 0;
  236. $tags = array();
  237. $output = '';
  238. if (empty($html)) {
  239. return $output;
  240. }
  241. while ($printedLength < $maxLength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position))
  242. {
  243. list($tag, $tagPosition) = $match[0];
  244. // Print text leading up to the tag.
  245. $str = JString::substr($html, $position, $tagPosition - $position);
  246. if ($printedLength + JString::strlen($str) > $maxLength) {
  247. $output .= JString::substr($str, 0, $maxLength - $printedLength);
  248. $printedLength = $maxLength;
  249. break;
  250. }
  251. $output .= $str;
  252. $lastCharacterIsOpenBracket = (JString::substr($output, -1, 1) === '<');
  253. if ($lastCharacterIsOpenBracket) {
  254. $output = JString::substr($output, 0, JString::strlen($output) - 1);
  255. }
  256. $printedLength += JString::strlen($str);
  257. if ($tag[0] == '&') {
  258. // Handle the entity.
  259. $output .= $tag;
  260. $printedLength++;
  261. }
  262. else {
  263. // Handle the tag.
  264. $tagName = $match[1][0];
  265. if ($tag[1] == '/') {
  266. // This is a closing tag.
  267. $openingTag = array_pop($tags);
  268. $output .= $tag;
  269. }
  270. else if ($tag[JString::strlen($tag) - 2] == '/') {
  271. // Self-closing tag.
  272. $output .= $tag;
  273. }
  274. else {
  275. // Opening tag.
  276. $output .= $tag;
  277. $tags[] = $tagName;
  278. }
  279. }
  280. // Continue after the tag.
  281. if ($lastCharacterIsOpenBracket) {
  282. $position = ($tagPosition - 1) + JString::strlen($tag);
  283. }
  284. else {
  285. $position = $tagPosition + JString::strlen($tag);
  286. }
  287. }
  288. // Print any remaining text.
  289. if ($printedLength < $maxLength && $position < JString::strlen($html)) {
  290. $output .= JString::substr($html, $position, $maxLength - $printedLength);
  291. }
  292. // Close any open tags.
  293. while (!empty($tags))
  294. {
  295. $output .= sprintf('</%s>', array_pop($tags));
  296. }
  297. $length = JString::strlen($output);
  298. $lastChar = JString::substr($output, ($length - 1), 1);
  299. $characterNumber = ord($lastChar);
  300. if ($characterNumber === 194) {
  301. $output = JString::substr($output, 0, JString::strlen($output) - 1);
  302. }
  303. $output = JString::rtrim($output);
  304. return $output.'&hellip;';
  305. }
  306. public static function groupBy($list, $fieldName, $article_grouping_direction, $fieldNameToKeep = null)
  307. {
  308. $grouped = array();
  309. if (!is_array($list)) {
  310. if ($list == '') {
  311. return $grouped;
  312. }
  313. $list = array($list);
  314. }
  315. foreach($list as $key => $item)
  316. {
  317. if (!isset($grouped[$item->$fieldName])) {
  318. $grouped[$item->$fieldName] = array();
  319. }
  320. if (is_null($fieldNameToKeep)) {
  321. $grouped[$item->$fieldName][$key] = $item;
  322. }
  323. else {
  324. $grouped[$item->$fieldName][$key] = $item->$fieldNameToKeep;
  325. }
  326. unset($list[$key]);
  327. }
  328. $article_grouping_direction($grouped);
  329. return $grouped;
  330. }
  331. public static function groupByDate($list, $type = 'year', $article_grouping_direction, $month_year_format = 'F Y')
  332. {
  333. $grouped = array();
  334. if (!is_array($list)) {
  335. if ($list == '') {
  336. return $grouped;
  337. }
  338. $list = array($list);
  339. }
  340. foreach($list as $key => $item)
  341. {
  342. switch($type)
  343. {
  344. case 'month_year':
  345. $month_year = JString::substr($item->created, 0, 7);
  346. if (!isset($grouped[$month_year])) {
  347. $grouped[$month_year] = array();
  348. }
  349. $grouped[$month_year][$key] = $item;
  350. break;
  351. case 'year':
  352. default:
  353. $year = JString::substr($item->created, 0, 4);
  354. if (!isset($grouped[$year])) {
  355. $grouped[$year] = array();
  356. }
  357. $grouped[$year][$key] = $item;
  358. break;
  359. }
  360. unset($list[$key]);
  361. }
  362. $article_grouping_direction($grouped);
  363. if ($type === 'month_year') {
  364. foreach($grouped as $group => $items)
  365. {
  366. $date = new JDate($group);
  367. $formatted_group = $date->format($month_year_format);
  368. $grouped[$formatted_group] = $items;
  369. unset($grouped[$group]);
  370. }
  371. }
  372. return $grouped;
  373. }
  374. }