PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_content/models/category.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 474 lines | 286 code | 66 blank | 122 comment | 39 complexity | 2aefc210ac405bcf47a914c3971afc5b MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Site
  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. * This models supports retrieving a category, the articles associated with the category,
  12. * sibling, child and parent categories.
  13. *
  14. * @package Joomla.Site
  15. * @subpackage com_content
  16. * @since 1.5
  17. */
  18. class ContentModelCategory extends JModelList
  19. {
  20. /**
  21. * Category items data
  22. *
  23. * @var array
  24. */
  25. protected $_item = null;
  26. protected $_articles = null;
  27. protected $_siblings = null;
  28. protected $_children = null;
  29. protected $_parent = null;
  30. /**
  31. * Model context string.
  32. *
  33. * @var string
  34. */
  35. protected $_context = 'com_content.category';
  36. /**
  37. * The category that applies.
  38. *
  39. * @access protected
  40. * @var object
  41. */
  42. protected $_category = null;
  43. /**
  44. * The list of other newfeed categories.
  45. *
  46. * @access protected
  47. * @var array
  48. */
  49. protected $_categories = null;
  50. /**
  51. * Constructor.
  52. *
  53. * @param array An optional associative array of configuration settings.
  54. * @see JController
  55. * @since 1.6
  56. */
  57. public function __construct($config = array())
  58. {
  59. if (empty($config['filter_fields']))
  60. {
  61. $config['filter_fields'] = array(
  62. 'id', 'a.id',
  63. 'title', 'a.title',
  64. 'alias', 'a.alias',
  65. 'checked_out', 'a.checked_out',
  66. 'checked_out_time', 'a.checked_out_time',
  67. 'catid', 'a.catid', 'category_title',
  68. 'state', 'a.state',
  69. 'access', 'a.access', 'access_level',
  70. 'created', 'a.created',
  71. 'created_by', 'a.created_by',
  72. 'modified', 'a.modified',
  73. 'ordering', 'a.ordering',
  74. 'featured', 'a.featured',
  75. 'language', 'a.language',
  76. 'hits', 'a.hits',
  77. 'publish_up', 'a.publish_up',
  78. 'publish_down', 'a.publish_down',
  79. 'author', 'a.author'
  80. );
  81. }
  82. parent::__construct($config);
  83. }
  84. /**
  85. * Method to auto-populate the model state.
  86. *
  87. * Note. Calling getState in this method will result in recursion.
  88. *
  89. * return void
  90. * @since 1.6
  91. */
  92. protected function populateState($ordering = null, $direction = null)
  93. {
  94. $app = JFactory::getApplication('site');
  95. $pk = $app->input->getInt('id');
  96. $this->setState('category.id', $pk);
  97. // Load the parameters. Merge Global and Menu Item params into new object
  98. $params = $app->getParams();
  99. $menuParams = new JRegistry;
  100. if ($menu = $app->getMenu()->getActive())
  101. {
  102. $menuParams->loadString($menu->params);
  103. }
  104. $mergedParams = clone $menuParams;
  105. $mergedParams->merge($params);
  106. $this->setState('params', $mergedParams);
  107. $user = JFactory::getUser();
  108. // Create a new query object.
  109. $db = $this->getDbo();
  110. $query = $db->getQuery(true);
  111. $groups = implode(',', $user->getAuthorisedViewLevels());
  112. if ((!$user->authorise('core.edit.state', 'com_content')) && (!$user->authorise('core.edit', 'com_content'))){
  113. // limit to published for people who can't edit or edit.state.
  114. $this->setState('filter.published', 1);
  115. // Filter by start and end dates.
  116. $nullDate = $db->quote($db->getNullDate());
  117. $nowDate = $db->quote(JFactory::getDate()->toSQL());
  118. $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')')
  119. ->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
  120. }
  121. else
  122. {
  123. $this->setState('filter.published', array(0, 1, 2));
  124. }
  125. // process show_noauth parameter
  126. if (!$params->get('show_noauth'))
  127. {
  128. $this->setState('filter.access', true);
  129. }
  130. else
  131. {
  132. $this->setState('filter.access', false);
  133. }
  134. // Optional filter text
  135. $this->setState('list.filter', $app->input->getString('filter-search'));
  136. // filter.order
  137. $itemid = $app->input->get('id', 0, 'int') . ':' . $app->input->get('Itemid', 0, 'int');
  138. $orderCol = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
  139. if (!in_array($orderCol, $this->filter_fields))
  140. {
  141. $orderCol = 'a.ordering';
  142. }
  143. $this->setState('list.ordering', $orderCol);
  144. $listOrder = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir',
  145. 'filter_order_Dir', '', 'cmd');
  146. if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', '')))
  147. {
  148. $listOrder = 'ASC';
  149. }
  150. $this->setState('list.direction', $listOrder);
  151. $this->setState('list.start', $app->input->get('limitstart', 0, 'uint'));
  152. // set limit for query. If list, use parameter. If blog, add blog parameters for limit.
  153. if (($app->input->get('layout') == 'blog') || $params->get('layout_type') == 'blog')
  154. {
  155. $limit = $params->get('num_leading_articles') + $params->get('num_intro_articles') + $params->get('num_links');
  156. $this->setState('list.links', $params->get('num_links'));
  157. }
  158. else
  159. {
  160. $limit = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.limit', 'limit', $params->get('display_num'), 'uint');
  161. }
  162. $this->setState('list.limit', $limit);
  163. // set the depth of the category query based on parameter
  164. $showSubcategories = $params->get('show_subcategory_content', '0');
  165. if ($showSubcategories)
  166. {
  167. $this->setState('filter.max_category_levels', $params->get('show_subcategory_content', '1'));
  168. $this->setState('filter.subcategories', true);
  169. }
  170. $this->setState('filter.language', JLanguageMultilang::isEnabled());
  171. $this->setState('layout', $app->input->get('layout'));
  172. }
  173. /**
  174. * Get the articles in the category
  175. *
  176. * @return mixed An array of articles or false if an error occurs.
  177. * @since 1.5
  178. */
  179. function getItems()
  180. {
  181. $params = $this->getState()->get('params');
  182. $limit = $this->getState('list.limit');
  183. if ($this->_articles === null && $category = $this->getCategory())
  184. {
  185. $model = JModelLegacy::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
  186. $model->setState('params', JFactory::getApplication()->getParams());
  187. $model->setState('filter.category_id', $category->id);
  188. $model->setState('filter.published', $this->getState('filter.published'));
  189. $model->setState('filter.access', $this->getState('filter.access'));
  190. $model->setState('filter.language', $this->getState('filter.language'));
  191. $model->setState('list.ordering', $this->_buildContentOrderBy());
  192. $model->setState('list.start', $this->getState('list.start'));
  193. $model->setState('list.limit', $limit);
  194. $model->setState('list.direction', $this->getState('list.direction'));
  195. $model->setState('list.filter', $this->getState('list.filter'));
  196. // filter.subcategories indicates whether to include articles from subcategories in the list or blog
  197. $model->setState('filter.subcategories', $this->getState('filter.subcategories'));
  198. $model->setState('filter.max_category_levels', $this->setState('filter.max_category_levels'));
  199. $model->setState('list.links', $this->getState('list.links'));
  200. if ($limit >= 0)
  201. {
  202. $this->_articles = $model->getItems();
  203. if ($this->_articles === false)
  204. {
  205. $this->setError($model->getError());
  206. }
  207. }
  208. else
  209. {
  210. $this->_articles = array();
  211. }
  212. $this->_pagination = $model->getPagination();
  213. }
  214. return $this->_articles;
  215. }
  216. /**
  217. * Build the orderby for the query
  218. *
  219. * @return string $orderby portion of query
  220. * @since 1.5
  221. */
  222. protected function _buildContentOrderBy()
  223. {
  224. $app = JFactory::getApplication('site');
  225. $db = $this->getDbo();
  226. $params = $this->state->params;
  227. $itemid = $app->input->get('id', 0, 'int') . ':' . $app->input->get('Itemid', 0, 'int');
  228. $orderCol = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order', 'filter_order', '', 'string');
  229. $orderDirn = $app->getUserStateFromRequest('com_content.category.list.' . $itemid . '.filter_order_Dir', 'filter_order_Dir', '', 'cmd');
  230. $orderby = ' ';
  231. if (!in_array($orderCol, $this->filter_fields))
  232. {
  233. $orderCol = null;
  234. }
  235. if (!in_array(strtoupper($orderDirn), array('ASC', 'DESC', '')))
  236. {
  237. $orderDirn = 'ASC';
  238. }
  239. if ($orderCol && $orderDirn)
  240. {
  241. $orderby .= $db->escape($orderCol) . ' ' . $db->escape($orderDirn) . ', ';
  242. }
  243. $articleOrderby = $params->get('orderby_sec', 'rdate');
  244. $articleOrderDate = $params->get('order_date');
  245. $categoryOrderby = $params->def('orderby_pri', '');
  246. $secondary = ContentHelperQuery::orderbySecondary($articleOrderby, $articleOrderDate) . ', ';
  247. $primary = ContentHelperQuery::orderbyPrimary($categoryOrderby);
  248. $orderby .= $primary . ' ' . $secondary . ' a.created ';
  249. return $orderby;
  250. }
  251. public function getPagination()
  252. {
  253. if (empty($this->_pagination))
  254. {
  255. return null;
  256. }
  257. return $this->_pagination;
  258. }
  259. /**
  260. * Method to get category data for the current category
  261. *
  262. * @param integer An optional ID
  263. *
  264. * @return object
  265. * @since 1.5
  266. */
  267. public function getCategory()
  268. {
  269. if (!is_object($this->_item))
  270. {
  271. if ( isset( $this->state->params ) )
  272. {
  273. $params = $this->state->params;
  274. $options = array();
  275. $options['countItems'] = $params->get('show_cat_num_articles', 1) || !$params->get('show_empty_categories_cat', 0);
  276. }
  277. else {
  278. $options['countItems'] = 0;
  279. }
  280. $categories = JCategories::getInstance('Content', $options);
  281. $this->_item = $categories->get($this->getState('category.id', 'root'));
  282. // Compute selected asset permissions.
  283. if (is_object($this->_item))
  284. {
  285. $user = JFactory::getUser();
  286. $userId = $user->get('id');
  287. $asset = 'com_content.category.'.$this->_item->id;
  288. // Check general create permission.
  289. if ($user->authorise('core.create', $asset))
  290. {
  291. $this->_item->getParams()->set('access-create', true);
  292. }
  293. // TODO: Why aren't we lazy loading the children and siblings?
  294. $this->_children = $this->_item->getChildren();
  295. $this->_parent = false;
  296. if ($this->_item->getParent())
  297. {
  298. $this->_parent = $this->_item->getParent();
  299. }
  300. $this->_rightsibling = $this->_item->getSibling();
  301. $this->_leftsibling = $this->_item->getSibling(false);
  302. }
  303. else {
  304. $this->_children = false;
  305. $this->_parent = false;
  306. }
  307. }
  308. return $this->_item;
  309. }
  310. /**
  311. * Get the parent category.
  312. *
  313. * @param integer An optional category id. If not supplied, the model state 'category.id' will be used.
  314. *
  315. * @return mixed An array of categories or false if an error occurs.
  316. * @since 1.6
  317. */
  318. public function getParent()
  319. {
  320. if (!is_object($this->_item))
  321. {
  322. $this->getCategory();
  323. }
  324. return $this->_parent;
  325. }
  326. /**
  327. * Get the left sibling (adjacent) categories.
  328. *
  329. * @return mixed An array of categories or false if an error occurs.
  330. * @since 1.6
  331. */
  332. function &getLeftSibling()
  333. {
  334. if (!is_object($this->_item))
  335. {
  336. $this->getCategory();
  337. }
  338. return $this->_leftsibling;
  339. }
  340. /**
  341. * Get the right sibling (adjacent) categories.
  342. *
  343. * @return mixed An array of categories or false if an error occurs.
  344. * @since 1.6
  345. */
  346. function &getRightSibling()
  347. {
  348. if (!is_object($this->_item))
  349. {
  350. $this->getCategory();
  351. }
  352. return $this->_rightsibling;
  353. }
  354. /**
  355. * Get the child categories.
  356. *
  357. * @param integer An optional category id. If not supplied, the model state 'category.id' will be used.
  358. *
  359. * @return mixed An array of categories or false if an error occurs.
  360. * @since 1.6
  361. */
  362. function &getChildren()
  363. {
  364. if (!is_object($this->_item))
  365. {
  366. $this->getCategory();
  367. }
  368. // Order subcategories
  369. if (count($this->_children))
  370. {
  371. $params = $this->getState()->get('params');
  372. if ($params->get('orderby_pri') == 'alpha' || $params->get('orderby_pri') == 'ralpha')
  373. {
  374. jimport('joomla.utilities.arrayhelper');
  375. JArrayHelper::sortObjects($this->_children, 'title', ($params->get('orderby_pri') == 'alpha') ? 1 : -1);
  376. }
  377. }
  378. return $this->_children;
  379. }
  380. /**
  381. * Increment the hit counter for the category.
  382. *
  383. * @param int $pk Optional primary key of the category to increment.
  384. *
  385. * @return boolean True if successful; false otherwise and internal error set.
  386. */
  387. public function hit($pk = 0)
  388. {
  389. // Initialise variables.
  390. $pk = (!empty($pk)) ? $pk : (int) $this->getState('category.id');
  391. $db = $this->getDbo();
  392. $query = $db->getQuery(true)
  393. ->update('#__categories')
  394. ->set('hits = hits + 1')
  395. ->where('id = ' . (int) $pk);
  396. $db->setQuery($query);
  397. try
  398. {
  399. $db->execute();
  400. }
  401. catch (RuntimeException $e)
  402. {
  403. $this->setError($e->getMessage());
  404. return false;
  405. }
  406. return true;
  407. }
  408. }