PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/joomla/administrator/components/com_newsfeeds/models/newsfeeds.php

https://gitlab.com/ricardosanchez/prueba
PHP | 267 lines | 160 code | 40 blank | 67 comment | 16 complexity | 80a16f782517bb263a3cd79e35623ee5 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_newsfeeds
  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. /**
  11. * Methods supporting a list of newsfeed records.
  12. *
  13. * @since 1.6
  14. */
  15. class NewsfeedsModelNewsfeeds extends JModelList
  16. {
  17. /**
  18. * Constructor.
  19. *
  20. * @param array $config An optional associative array of configuration settings.
  21. *
  22. * @since 1.6
  23. */
  24. public function __construct($config = array())
  25. {
  26. if (empty($config['filter_fields']))
  27. {
  28. $config['filter_fields'] = array(
  29. 'id', 'a.id',
  30. 'name', 'a.name',
  31. 'alias', 'a.alias',
  32. 'checked_out', 'a.checked_out',
  33. 'checked_out_time', 'a.checked_out_time',
  34. 'catid', 'a.catid', 'category_title',
  35. 'published', 'a.published',
  36. 'access', 'a.access', 'access_level',
  37. 'created', 'a.created',
  38. 'created_by', 'a.created_by',
  39. 'ordering', 'a.ordering',
  40. 'language', 'a.language',
  41. 'publish_up', 'a.publish_up',
  42. 'publish_down', 'a.publish_down',
  43. 'cache_time', 'a.cache_time',
  44. 'numarticles',
  45. );
  46. $app = JFactory::getApplication();
  47. $assoc = JLanguageAssociations::isEnabled();
  48. if ($assoc)
  49. {
  50. $config['filter_fields'][] = 'association';
  51. }
  52. }
  53. parent::__construct($config);
  54. }
  55. /**
  56. * Method to auto-populate the model state.
  57. *
  58. * Note. Calling getState in this method will result in recursion.
  59. *
  60. * @param string $ordering An optional ordering field.
  61. * @param string $direction An optional direction (asc|desc).
  62. *
  63. * @return void
  64. *
  65. * @since 1.6
  66. */
  67. protected function populateState($ordering = null, $direction = null)
  68. {
  69. $app = JFactory::getApplication('administrator');
  70. // Load the filter state.
  71. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  72. $this->setState('filter.search', $search);
  73. $accessId = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', null, 'int');
  74. $this->setState('filter.access', $accessId);
  75. $state = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '', 'string');
  76. $this->setState('filter.published', $state);
  77. $categoryId = $this->getUserStateFromRequest($this->context . '.filter.category_id', 'filter_category_id', null);
  78. $this->setState('filter.category_id', $categoryId);
  79. $language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
  80. $this->setState('filter.language', $language);
  81. // Force a language
  82. $forcedLanguage = $app->input->get('forcedLanguage');
  83. if (!empty($forcedLanguage))
  84. {
  85. $this->setState('filter.language', $forcedLanguage);
  86. $this->setState('filter.forcedLanguage', $forcedLanguage);
  87. }
  88. $tag = $this->getUserStateFromRequest($this->context . '.filter.tag', 'filter_tag', '');
  89. $this->setState('filter.tag', $tag);
  90. // Load the parameters.
  91. $params = JComponentHelper::getParams('com_newsfeeds');
  92. $this->setState('params', $params);
  93. // List state information.
  94. parent::populateState('a.name', 'asc');
  95. }
  96. /**
  97. * Method to get a store id based on model configuration state.
  98. *
  99. * This is necessary because the model is used by the component and
  100. * different modules that might need different sets of data or different
  101. * ordering requirements.
  102. *
  103. * @param string $id A prefix for the store id.
  104. *
  105. * @return string A store id.
  106. */
  107. protected function getStoreId($id = '')
  108. {
  109. // Compile the store id.
  110. $id .= ':' . $this->getState('filter.search');
  111. $id .= ':' . $this->getState('filter.access');
  112. $id .= ':' . $this->getState('filter.published');
  113. $id .= ':' . $this->getState('filter.category_id');
  114. $id .= ':' . $this->getState('filter.language');
  115. return parent::getStoreId($id);
  116. }
  117. /**
  118. * Build an SQL query to load the list data.
  119. *
  120. * @return JDatabaseQuery
  121. */
  122. protected function getListQuery()
  123. {
  124. // Create a new query object.
  125. $db = $this->getDbo();
  126. $query = $db->getQuery(true);
  127. $user = JFactory::getUser();
  128. $app = JFactory::getApplication();
  129. // Select the required fields from the table.
  130. $query->select(
  131. $this->getState(
  132. 'list.select',
  133. 'a.id, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid,' .
  134. ' a.numarticles, a.cache_time,' .
  135. ' a.published, a.access, a.ordering, a.language, a.publish_up, a.publish_down'
  136. )
  137. );
  138. $query->from($db->quoteName('#__newsfeeds') . ' AS a');
  139. // Join over the language
  140. $query->select('l.title AS language_title')
  141. ->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
  142. // Join over the users for the checked out user.
  143. $query->select('uc.name AS editor')
  144. ->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
  145. // Join over the asset groups.
  146. $query->select('ag.title AS access_level')
  147. ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  148. // Join over the categories.
  149. $query->select('c.title AS category_title')
  150. ->join('LEFT', '#__categories AS c ON c.id = a.catid');
  151. // Join over the associations.
  152. $assoc = JLanguageAssociations::isEnabled();
  153. if ($assoc)
  154. {
  155. $query->select('COUNT(asso2.id)>1 as association')
  156. ->join('LEFT', '#__associations AS asso ON asso.id = a.id AND asso.context=' . $db->quote('com_newsfeeds.item'))
  157. ->join('LEFT', '#__associations AS asso2 ON asso2.key = asso.key')
  158. ->group('a.id, l.title, uc.name, ag.title, c.title');
  159. }
  160. // Filter by access level.
  161. if ($access = $this->getState('filter.access'))
  162. {
  163. $query->where('a.access = ' . (int) $access);
  164. }
  165. // Implement View Level Access
  166. if (!$user->authorise('core.admin'))
  167. {
  168. $groups = implode(',', $user->getAuthorisedViewLevels());
  169. $query->where('a.access IN (' . $groups . ')');
  170. }
  171. // Filter by published state.
  172. $published = $this->getState('filter.published');
  173. if (is_numeric($published))
  174. {
  175. $query->where('a.published = ' . (int) $published);
  176. }
  177. elseif ($published === '')
  178. {
  179. $query->where('(a.published IN (0, 1))');
  180. }
  181. // Filter by category.
  182. $categoryId = $this->getState('filter.category_id');
  183. if (is_numeric($categoryId))
  184. {
  185. $query->where('a.catid = ' . (int) $categoryId);
  186. }
  187. // Filter by search in title
  188. $search = $this->getState('filter.search');
  189. if (!empty($search))
  190. {
  191. if (stripos($search, 'id:') === 0)
  192. {
  193. $query->where('a.id = ' . (int) substr($search, 3));
  194. }
  195. else
  196. {
  197. $search = $db->quote('%' . str_replace(' ', '%', $db->escape(trim($search), true) . '%'));
  198. $query->where('(a.name LIKE ' . $search . ' OR a.alias LIKE ' . $search . ')');
  199. }
  200. }
  201. // Filter on the language.
  202. if ($language = $this->getState('filter.language'))
  203. {
  204. $query->where('a.language = ' . $db->quote($language));
  205. }
  206. // Filter by a single tag.
  207. $tagId = $this->getState('filter.tag');
  208. if (is_numeric($tagId))
  209. {
  210. $query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId)
  211. ->join(
  212. 'LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
  213. . ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
  214. . ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed')
  215. );
  216. }
  217. // Add the list ordering clause.
  218. $orderCol = $this->state->get('list.ordering');
  219. $orderDirn = $this->state->get('list.direction');
  220. if ($orderCol == 'a.ordering' || $orderCol == 'category_title')
  221. {
  222. $orderCol = 'c.title ' . $orderDirn . ', a.ordering';
  223. }
  224. $query->order($db->escape($orderCol . ' ' . $orderDirn));
  225. return $query;
  226. }
  227. }