PageRenderTime 25ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/modules/mod_articles_category/helper.php

https://gitlab.com/ricardosanchez/prueba
PHP | 481 lines | 308 code | 76 blank | 97 comment | 38 complexity | 367e26da858797755f68c1bb6aceab1c MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage mod_articles_category
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. $com_path = JPATH_SITE . '/components/com_content/';
  11. require_once $com_path . 'helpers/route.php';
  12. JModelLegacy::addIncludePath($com_path . '/models', 'ContentModel');
  13. /**
  14. * Helper for mod_articles_category
  15. *
  16. * @package Joomla.Site
  17. * @subpackage mod_articles_category
  18. *
  19. * @since 1.6
  20. */
  21. abstract class ModArticlesCategoryHelper
  22. {
  23. /**
  24. * Get a list of articles from a specific category
  25. *
  26. * @param \Joomla\Registry\Registry &$params object holding the models parameters
  27. *
  28. * @return mixed
  29. *
  30. * @since 1.6
  31. */
  32. public static function getList(&$params)
  33. {
  34. // Get an instance of the generic articles model
  35. $articles = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
  36. // Set application parameters in model
  37. $app = JFactory::getApplication();
  38. $appParams = $app->getParams();
  39. $articles->setState('params', $appParams);
  40. // Set the filters based on the module params
  41. $articles->setState('list.start', 0);
  42. $articles->setState('list.limit', (int) $params->get('count', 0));
  43. $articles->setState('filter.published', 1);
  44. // Access filter
  45. $access = !JComponentHelper::getParams('com_content')->get('show_noauth');
  46. $authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
  47. $articles->setState('filter.access', $access);
  48. // Prep for Normal or Dynamic Modes
  49. $mode = $params->get('mode', 'normal');
  50. switch ($mode)
  51. {
  52. case 'dynamic' :
  53. $option = $app->input->get('option');
  54. $view = $app->input->get('view');
  55. if ($option === 'com_content')
  56. {
  57. switch ($view)
  58. {
  59. case 'category' :
  60. $catids = array($app->input->getInt('id'));
  61. break;
  62. case 'categories' :
  63. $catids = array($app->input->getInt('id'));
  64. break;
  65. case 'article' :
  66. if ($params->get('show_on_article_page', 1))
  67. {
  68. $article_id = $app->input->getInt('id');
  69. $catid = $app->input->getInt('catid');
  70. if (!$catid)
  71. {
  72. // Get an instance of the generic article model
  73. $article = JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request' => true));
  74. $article->setState('params', $appParams);
  75. $article->setState('filter.published', 1);
  76. $article->setState('article.id', (int) $article_id);
  77. $item = $article->getItem();
  78. $catids = array($item->catid);
  79. }
  80. else
  81. {
  82. $catids = array($catid);
  83. }
  84. }
  85. else
  86. {
  87. // Return right away if show_on_article_page option is off
  88. return;
  89. }
  90. break;
  91. case 'featured' :
  92. default:
  93. // Return right away if not on the category or article views
  94. return;
  95. }
  96. }
  97. else
  98. {
  99. // Return right away if not on a com_content page
  100. return;
  101. }
  102. break;
  103. case 'normal' :
  104. default:
  105. $catids = $params->get('catid');
  106. $articles->setState('filter.category_id.include', (bool) $params->get('category_filtering_type', 1));
  107. break;
  108. }
  109. // Category filter
  110. if ($catids)
  111. {
  112. if ($params->get('show_child_category_articles', 0) && (int) $params->get('levels', 0) > 0)
  113. {
  114. // Get an instance of the generic categories model
  115. $categories = JModelLegacy::getInstance('Categories', 'ContentModel', array('ignore_request' => true));
  116. $categories->setState('params', $appParams);
  117. $levels = $params->get('levels', 1) ? $params->get('levels', 1) : 9999;
  118. $categories->setState('filter.get_children', $levels);
  119. $categories->setState('filter.published', 1);
  120. $categories->setState('filter.access', $access);
  121. $additional_catids = array();
  122. foreach ($catids as $catid)
  123. {
  124. $categories->setState('filter.parentId', $catid);
  125. $recursive = true;
  126. $items = $categories->getItems($recursive);
  127. if ($items)
  128. {
  129. foreach ($items as $category)
  130. {
  131. $condition = (($category->level - $categories->getParent()->level) <= $levels);
  132. if ($condition)
  133. {
  134. $additional_catids[] = $category->id;
  135. }
  136. }
  137. }
  138. }
  139. $catids = array_unique(array_merge($catids, $additional_catids));
  140. }
  141. $articles->setState('filter.category_id', $catids);
  142. }
  143. // Ordering
  144. $articles->setState('list.ordering', $params->get('article_ordering', 'a.ordering'));
  145. $articles->setState('list.direction', $params->get('article_ordering_direction', 'ASC'));
  146. // New Parameters
  147. $articles->setState('filter.featured', $params->get('show_front', 'show'));
  148. $articles->setState('filter.author_id', $params->get('created_by', ""));
  149. $articles->setState('filter.author_id.include', $params->get('author_filtering_type', 1));
  150. $articles->setState('filter.author_alias', $params->get('created_by_alias', ""));
  151. $articles->setState('filter.author_alias.include', $params->get('author_alias_filtering_type', 1));
  152. $excluded_articles = $params->get('excluded_articles', '');
  153. if ($excluded_articles)
  154. {
  155. $excluded_articles = explode("\r\n", $excluded_articles);
  156. $articles->setState('filter.article_id', $excluded_articles);
  157. // Exclude
  158. $articles->setState('filter.article_id.include', false);
  159. }
  160. $date_filtering = $params->get('date_filtering', 'off');
  161. if ($date_filtering !== 'off')
  162. {
  163. $articles->setState('filter.date_filtering', $date_filtering);
  164. $articles->setState('filter.date_field', $params->get('date_field', 'a.created'));
  165. $articles->setState('filter.start_date_range', $params->get('start_date_range', '1000-01-01 00:00:00'));
  166. $articles->setState('filter.end_date_range', $params->get('end_date_range', '9999-12-31 23:59:59'));
  167. $articles->setState('filter.relative_date', $params->get('relative_date', 30));
  168. }
  169. // Filter by language
  170. $articles->setState('filter.language', $app->getLanguageFilter());
  171. $items = $articles->getItems();
  172. // Display options
  173. $show_date = $params->get('show_date', 0);
  174. $show_date_field = $params->get('show_date_field', 'created');
  175. $show_date_format = $params->get('show_date_format', 'Y-m-d H:i:s');
  176. $show_category = $params->get('show_category', 0);
  177. $show_hits = $params->get('show_hits', 0);
  178. $show_author = $params->get('show_author', 0);
  179. $show_introtext = $params->get('show_introtext', 0);
  180. $introtext_limit = $params->get('introtext_limit', 100);
  181. // Find current Article ID if on an article page
  182. $option = $app->input->get('option');
  183. $view = $app->input->get('view');
  184. if ($option === 'com_content' && $view === 'article')
  185. {
  186. $active_article_id = $app->input->getInt('id');
  187. }
  188. else
  189. {
  190. $active_article_id = 0;
  191. }
  192. // Prepare data for display using display options
  193. foreach ($items as &$item)
  194. {
  195. $item->slug = $item->id . ':' . $item->alias;
  196. $item->catslug = $item->catid . ':' . $item->category_alias;
  197. if ($access || in_array($item->access, $authorised))
  198. {
  199. // We know that user has the privilege to view the article
  200. $item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
  201. }
  202. else
  203. {
  204. $app = JFactory::getApplication();
  205. $menu = $app->getMenu();
  206. $menuitems = $menu->getItems('link', 'index.php?option=com_users&view=login');
  207. if (isset($menuitems[0]))
  208. {
  209. $Itemid = $menuitems[0]->id;
  210. }
  211. elseif ($app->input->getInt('Itemid') > 0)
  212. {
  213. // Use Itemid from requesting page only if there is no existing menu
  214. $Itemid = $app->input->getInt('Itemid');
  215. }
  216. $item->link = JRoute::_('index.php?option=com_users&view=login&Itemid=' . $Itemid);
  217. }
  218. // Used for styling the active article
  219. $item->active = $item->id == $active_article_id ? 'active' : '';
  220. $item->displayDate = '';
  221. if ($show_date)
  222. {
  223. $item->displayDate = JHtml::_('date', $item->$show_date_field, $show_date_format);
  224. }
  225. if ($item->catid)
  226. {
  227. $item->displayCategoryLink = JRoute::_(ContentHelperRoute::getCategoryRoute($item->catid));
  228. $item->displayCategoryTitle = $show_category ? '<a href="' . $item->displayCategoryLink . '">' . $item->category_title . '</a>' : '';
  229. }
  230. else
  231. {
  232. $item->displayCategoryTitle = $show_category ? $item->category_title : '';
  233. }
  234. $item->displayHits = $show_hits ? $item->hits : '';
  235. $item->displayAuthorName = $show_author ? $item->author : '';
  236. if ($show_introtext)
  237. {
  238. $item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'mod_articles_category.content');
  239. $item->introtext = self::_cleanIntrotext($item->introtext);
  240. }
  241. $item->displayIntrotext = $show_introtext ? self::truncate($item->introtext, $introtext_limit) : '';
  242. $item->displayReadmore = $item->alternative_readmore;
  243. }
  244. return $items;
  245. }
  246. /**
  247. * Strips unnecessary tags from the introtext
  248. *
  249. * @param string $introtext introtext to sanitize
  250. *
  251. * @return mixed|string
  252. *
  253. * @since 1.6
  254. */
  255. public static function _cleanIntrotext($introtext)
  256. {
  257. $introtext = str_replace('<p>', ' ', $introtext);
  258. $introtext = str_replace('</p>', ' ', $introtext);
  259. $introtext = strip_tags($introtext, '<a><em><strong>');
  260. $introtext = trim($introtext);
  261. return $introtext;
  262. }
  263. /**
  264. * Method to truncate introtext
  265. *
  266. * The goal is to get the proper length plain text string with as much of
  267. * the html intact as possible with all tags properly closed.
  268. *
  269. * @param string $html The content of the introtext to be truncated
  270. * @param integer $maxLength The maximum number of charactes to render
  271. *
  272. * @return string The truncated string
  273. *
  274. * @since 1.6
  275. */
  276. public static function truncate($html, $maxLength = 0)
  277. {
  278. $baseLength = strlen($html);
  279. // First get the plain text string. This is the rendered text we want to end up with.
  280. $ptString = JHtml::_('string.truncate', $html, $maxLength, $noSplit = true, $allowHtml = false);
  281. for ($maxLength; $maxLength < $baseLength;)
  282. {
  283. // Now get the string if we allow html.
  284. $htmlString = JHtml::_('string.truncate', $html, $maxLength, $noSplit = true, $allowHtml = true);
  285. // Now get the plain text from the html string.
  286. $htmlStringToPtString = JHtml::_('string.truncate', $htmlString, $maxLength, $noSplit = true, $allowHtml = false);
  287. // If the new plain text string matches the original plain text string we are done.
  288. if ($ptString == $htmlStringToPtString)
  289. {
  290. return $htmlString;
  291. }
  292. // Get the number of html tag characters in the first $maxlength characters
  293. $diffLength = strlen($ptString) - strlen($htmlStringToPtString);
  294. // Set new $maxlength that adjusts for the html tags
  295. $maxLength += $diffLength;
  296. if ($baseLength <= $maxLength || $diffLength <= 0)
  297. {
  298. return $htmlString;
  299. }
  300. }
  301. return $html;
  302. }
  303. /**
  304. * Groups items by field
  305. *
  306. * @param array $list list of items
  307. * @param string $fieldName name of field that is used for grouping
  308. * @param string $article_grouping_direction ordering direction
  309. * @param null $fieldNameToKeep field name to keep
  310. *
  311. * @return array
  312. *
  313. * @since 1.6
  314. */
  315. public static function groupBy($list, $fieldName, $article_grouping_direction, $fieldNameToKeep = null)
  316. {
  317. $grouped = array();
  318. if (!is_array($list))
  319. {
  320. if ($list == '')
  321. {
  322. return $grouped;
  323. }
  324. $list = array($list);
  325. }
  326. foreach ($list as $key => $item)
  327. {
  328. if (!isset($grouped[$item->$fieldName]))
  329. {
  330. $grouped[$item->$fieldName] = array();
  331. }
  332. if (is_null($fieldNameToKeep))
  333. {
  334. $grouped[$item->$fieldName][$key] = $item;
  335. }
  336. else
  337. {
  338. $grouped[$item->$fieldName][$key] = $item->$fieldNameToKeep;
  339. }
  340. unset($list[$key]);
  341. }
  342. $article_grouping_direction($grouped);
  343. return $grouped;
  344. }
  345. /**
  346. * Groups items by date
  347. *
  348. * @param array $list list of items
  349. * @param string $type type of grouping
  350. * @param string $article_grouping_direction ordering direction
  351. * @param string $month_year_format date format to use
  352. *
  353. * @return array
  354. *
  355. * @since 1.6
  356. */
  357. public static function groupByDate($list, $type = 'year', $article_grouping_direction, $month_year_format = 'F Y')
  358. {
  359. $grouped = array();
  360. if (!is_array($list))
  361. {
  362. if ($list == '')
  363. {
  364. return $grouped;
  365. }
  366. $list = array($list);
  367. }
  368. foreach ($list as $key => $item)
  369. {
  370. switch ($type)
  371. {
  372. case 'month_year' :
  373. $month_year = JString::substr($item->created, 0, 7);
  374. if (!isset($grouped[$month_year]))
  375. {
  376. $grouped[$month_year] = array();
  377. }
  378. $grouped[$month_year][$key] = $item;
  379. break;
  380. case 'year' :
  381. default:
  382. $year = JString::substr($item->created, 0, 4);
  383. if (!isset($grouped[$year]))
  384. {
  385. $grouped[$year] = array();
  386. }
  387. $grouped[$year][$key] = $item;
  388. break;
  389. }
  390. unset($list[$key]);
  391. }
  392. $article_grouping_direction($grouped);
  393. if ($type === 'month_year')
  394. {
  395. foreach ($grouped as $group => $items)
  396. {
  397. $date = new JDate($group);
  398. $formatted_group = $date->format($month_year_format);
  399. $grouped[$formatted_group] = $items;
  400. unset($grouped[$group]);
  401. }
  402. }
  403. return $grouped;
  404. }
  405. }