PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/components/com_content/models/articles.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 372 lines | 236 code | 43 blank | 93 comment | 26 complexity | f581d07f65dec5d1d980be11d55cc832 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_content
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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 article records.
  12. *
  13. * @package Joomla.Administrator
  14. * @subpackage com_content
  15. */
  16. class ContentModelArticles extends JModelList
  17. {
  18. /**
  19. * Constructor.
  20. *
  21. * @param array $config An optional associative array of configuration settings.
  22. *
  23. * @since 1.6
  24. * @see JController
  25. */
  26. public function __construct($config = array())
  27. {
  28. if (empty($config['filter_fields']))
  29. {
  30. $config['filter_fields'] = array(
  31. 'id', 'a.id',
  32. 'title', 'a.title',
  33. 'alias', 'a.alias',
  34. 'checked_out', 'a.checked_out',
  35. 'checked_out_time', 'a.checked_out_time',
  36. 'catid', 'a.catid', 'category_title',
  37. 'state', 'a.state',
  38. 'access', 'a.access', 'access_level',
  39. 'created', 'a.created',
  40. 'created_by', 'a.created_by',
  41. 'created_by_alias', 'a.created_by_alias',
  42. 'ordering', 'a.ordering',
  43. 'featured', 'a.featured',
  44. 'language', 'a.language',
  45. 'hits', 'a.hits',
  46. 'publish_up', 'a.publish_up',
  47. 'publish_down', 'a.publish_down',
  48. );
  49. $app = JFactory::getApplication();
  50. $assoc = isset($app->item_associations) ? $app->item_associations : 0;
  51. if ($assoc)
  52. {
  53. $config['filter_fields'][] = 'association';
  54. }
  55. }
  56. parent::__construct($config);
  57. }
  58. /**
  59. * Method to auto-populate the model state.
  60. *
  61. * Note. Calling getState in this method will result in recursion.
  62. *
  63. * @param string $ordering An optional ordering field.
  64. * @param string $direction An optional direction (asc|desc).
  65. *
  66. * @return void
  67. *
  68. * @since 1.6
  69. */
  70. protected function populateState($ordering = null, $direction = null)
  71. {
  72. $app = JFactory::getApplication();
  73. // Adjust the context to support modal layouts.
  74. if ($layout = $app->input->get('layout'))
  75. {
  76. $this->context .= '.' . $layout;
  77. }
  78. $search = $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search');
  79. $this->setState('filter.search', $search);
  80. $access = $this->getUserStateFromRequest($this->context . '.filter.access', 'filter_access', 0, 'int');
  81. $this->setState('filter.access', $access);
  82. $authorId = $app->getUserStateFromRequest($this->context . '.filter.author_id', 'filter_author_id');
  83. $this->setState('filter.author_id', $authorId);
  84. $published = $this->getUserStateFromRequest($this->context . '.filter.published', 'filter_published', '');
  85. $this->setState('filter.published', $published);
  86. $categoryId = $this->getUserStateFromRequest($this->context . '.filter.category_id', 'filter_category_id');
  87. $this->setState('filter.category_id', $categoryId);
  88. $level = $this->getUserStateFromRequest($this->context . '.filter.level', 'filter_level', 0, 'int');
  89. $this->setState('filter.level', $level);
  90. $language = $this->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '');
  91. $this->setState('filter.language', $language);
  92. // force a language
  93. $forcedLanguage = $app->input->get('forcedLanguage');
  94. if (!empty($forcedLanguage))
  95. {
  96. $this->setState('filter.language', $forcedLanguage);
  97. $this->setState('filter.forcedLanguage', $forcedLanguage);
  98. }
  99. $tag = $this->getUserStateFromRequest($this->context . '.filter.tag', 'filter_tag', '');
  100. $this->setState('filter.tag', $tag);
  101. // List state information.
  102. parent::populateState('a.title', 'asc');
  103. }
  104. /**
  105. * Method to get a store id based on model configuration state.
  106. *
  107. * This is necessary because the model is used by the component and
  108. * different modules that might need different sets of data or different
  109. * ordering requirements.
  110. *
  111. * @param string $id A prefix for the store id.
  112. *
  113. * @return string A store id.
  114. * @since 1.6
  115. */
  116. protected function getStoreId($id = '')
  117. {
  118. // Compile the store id.
  119. $id .= ':' . $this->getState('filter.search');
  120. $id .= ':' . $this->getState('filter.access');
  121. $id .= ':' . $this->getState('filter.published');
  122. $id .= ':' . $this->getState('filter.category_id');
  123. $id .= ':' . $this->getState('filter.author_id');
  124. $id .= ':' . $this->getState('filter.language');
  125. return parent::getStoreId($id);
  126. }
  127. /**
  128. * Build an SQL query to load the list data.
  129. *
  130. * @return JDatabaseQuery
  131. * @since 1.6
  132. */
  133. protected function getListQuery()
  134. {
  135. // Create a new query object.
  136. $db = $this->getDbo();
  137. $query = $db->getQuery(true);
  138. $user = JFactory::getUser();
  139. $app = JFactory::getApplication();
  140. // Select the required fields from the table.
  141. $query->select(
  142. $this->getState(
  143. 'list.select',
  144. 'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' .
  145. ', a.state, a.access, a.created, a.created_by, a.created_by_alias, a.ordering, a.featured, a.language, a.hits' .
  146. ', a.publish_up, a.publish_down'
  147. )
  148. );
  149. $query->from('#__content AS a');
  150. // Join over the language
  151. $query->select('l.title AS language_title')
  152. ->join('LEFT', $db->quoteName('#__languages') . ' AS l ON l.lang_code = a.language');
  153. // Join over the users for the checked out user.
  154. $query->select('uc.name AS editor')
  155. ->join('LEFT', '#__users AS uc ON uc.id=a.checked_out');
  156. // Join over the asset groups.
  157. $query->select('ag.title AS access_level')
  158. ->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
  159. // Join over the categories.
  160. $query->select('c.title AS category_title')
  161. ->join('LEFT', '#__categories AS c ON c.id = a.catid');
  162. // Join over the users for the author.
  163. $query->select('ua.name AS author_name')
  164. ->join('LEFT', '#__users AS ua ON ua.id = a.created_by');
  165. // Join over the associations.
  166. $assoc = isset($app->item_associations) ? $app->item_associations : 0;
  167. if ($assoc)
  168. {
  169. $query->select('COUNT(asso2.id)>1 as association')
  170. ->join('LEFT', '#__associations AS asso ON asso.id = a.id AND asso.context=' . $db->quote('com_content.item'))
  171. ->join('LEFT', '#__associations AS asso2 ON asso2.key = asso.key')
  172. ->group('a.id');
  173. }
  174. // Filter by access level.
  175. if ($access = $this->getState('filter.access'))
  176. {
  177. $query->where('a.access = ' . (int) $access);
  178. }
  179. // Implement View Level Access
  180. if (!$user->authorise('core.admin'))
  181. {
  182. $groups = implode(',', $user->getAuthorisedViewLevels());
  183. $query->where('a.access IN (' . $groups . ')');
  184. }
  185. // Filter by published state
  186. $published = $this->getState('filter.published');
  187. if (is_numeric($published))
  188. {
  189. $query->where('a.state = ' . (int) $published);
  190. }
  191. elseif ($published === '')
  192. {
  193. $query->where('(a.state = 0 OR a.state = 1)');
  194. }
  195. // Filter by a single or group of categories.
  196. $baselevel = 1;
  197. $categoryId = $this->getState('filter.category_id');
  198. if (is_numeric($categoryId))
  199. {
  200. $cat_tbl = JTable::getInstance('Category', 'JTable');
  201. $cat_tbl->load($categoryId);
  202. $rgt = $cat_tbl->rgt;
  203. $lft = $cat_tbl->lft;
  204. $baselevel = (int) $cat_tbl->level;
  205. $query->where('c.lft >= ' . (int) $lft)
  206. ->where('c.rgt <= ' . (int) $rgt);
  207. }
  208. elseif (is_array($categoryId))
  209. {
  210. JArrayHelper::toInteger($categoryId);
  211. $categoryId = implode(',', $categoryId);
  212. $query->where('a.catid IN (' . $categoryId . ')');
  213. }
  214. // Filter on the level.
  215. if ($level = $this->getState('filter.level'))
  216. {
  217. $query->where('c.level <= ' . ((int) $level + (int) $baselevel - 1));
  218. }
  219. // Filter by author
  220. $authorId = $this->getState('filter.author_id');
  221. if (is_numeric($authorId))
  222. {
  223. $type = $this->getState('filter.author_id.include', true) ? '= ' : '<>';
  224. $query->where('a.created_by ' . $type . (int) $authorId);
  225. }
  226. // Filter by search in title.
  227. $search = $this->getState('filter.search');
  228. if (!empty($search))
  229. {
  230. if (stripos($search, 'id:') === 0)
  231. {
  232. $query->where('a.id = ' . (int) substr($search, 3));
  233. }
  234. elseif (stripos($search, 'author:') === 0)
  235. {
  236. $search = $db->quote('%' . $db->escape(substr($search, 7), true) . '%');
  237. $query->where('(ua.name LIKE ' . $search . ' OR ua.username LIKE ' . $search . ')');
  238. }
  239. else
  240. {
  241. $search = $db->quote('%' . $db->escape($search, true) . '%');
  242. $query->where('(a.title LIKE ' . $search . ' OR a.alias LIKE ' . $search . ')');
  243. }
  244. }
  245. // Filter on the language.
  246. if ($language = $this->getState('filter.language'))
  247. {
  248. $query->where('a.language = ' . $db->quote($language));
  249. }
  250. // Filter by a single tag.
  251. $tagId = $this->getState('filter.tag');
  252. if (is_numeric($tagId))
  253. {
  254. $query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId)
  255. ->join(
  256. 'LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
  257. . ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
  258. . ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article')
  259. );
  260. }
  261. // Add the list ordering clause.
  262. $orderCol = $this->state->get('list.ordering', 'a.title');
  263. $orderDirn = $this->state->get('list.direction', 'asc');
  264. if ($orderCol == 'a.ordering' || $orderCol == 'category_title')
  265. {
  266. $orderCol = 'c.title ' . $orderDirn . ', a.ordering';
  267. }
  268. //sqlsrv change
  269. if ($orderCol == 'language')
  270. {
  271. $orderCol = 'l.title';
  272. }
  273. if ($orderCol == 'access_level')
  274. {
  275. $orderCol = 'ag.title';
  276. }
  277. $query->order($db->escape($orderCol . ' ' . $orderDirn));
  278. // echo nl2br(str_replace('#__','jos_',$query));
  279. return $query;
  280. }
  281. /**
  282. * Build a list of authors
  283. *
  284. * @return JDatabaseQuery
  285. * @since 1.6
  286. */
  287. public function getAuthors()
  288. {
  289. // Create a new query object.
  290. $db = $this->getDbo();
  291. $query = $db->getQuery(true);
  292. // Construct the query
  293. $query->select('u.id AS value, u.name AS text')
  294. ->from('#__users AS u')
  295. ->join('INNER', '#__content AS c ON c.created_by = u.id')
  296. ->group('u.id, u.name')
  297. ->order('u.name');
  298. // Setup the query
  299. $db->setQuery($query);
  300. // Return the result
  301. return $db->loadObjectList();
  302. }
  303. /**
  304. * Method to get a list of articles.
  305. * Overridden to add a check for access levels.
  306. *
  307. * @return mixed An array of data items on success, false on failure.
  308. * @since 1.6.1
  309. */
  310. public function getItems()
  311. {
  312. $items = parent::getItems();
  313. $app = JFactory::getApplication();
  314. if ($app->isSite())
  315. {
  316. $user = JFactory::getUser();
  317. $groups = $user->getAuthorisedViewLevels();
  318. for ($x = 0, $count = count($items); $x < $count; $x++)
  319. {
  320. //Check the access level. Remove articles the user shouldn't see
  321. if (!in_array($items[$x]->access, $groups))
  322. {
  323. unset($items[$x]);
  324. }
  325. }
  326. }
  327. return $items;
  328. }
  329. }