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

/components/com_content/models/category.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 511 lines | 302 code | 63 blank | 146 comment | 32 complexity | 112d8a5326db9873c265faf6d24cffb7 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: category.php 10094 2008-03-02 04:35:10Z instance $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.application.component.model');
  17. /**
  18. * Content Component Category Model
  19. *
  20. * @author Louis Landry <louis.landry@joomla.org>
  21. * @package Joomla
  22. * @subpackage Content
  23. * @since 1.5
  24. */
  25. class ContentModelCategory extends JModel
  26. {
  27. /**
  28. * Category id
  29. *
  30. * @var int
  31. */
  32. var $_id = null;
  33. /**
  34. * Category items data
  35. *
  36. * @var array
  37. */
  38. var $_data = null;
  39. /**
  40. * Category number items
  41. *
  42. * @var integer
  43. */
  44. var $_total = null;
  45. /**
  46. * Category data
  47. *
  48. * @var object
  49. */
  50. var $_category = null;
  51. /**
  52. * Category data
  53. *
  54. * @var array
  55. */
  56. var $_siblings = null;
  57. /**
  58. * Constructor
  59. *
  60. * @since 1.5
  61. */
  62. function __construct()
  63. {
  64. parent::__construct();
  65. global $mainframe;
  66. $id = JRequest::getVar('id', 0, '', 'int');
  67. $this->setId((int)$id);
  68. // here we initialize defaults for category model
  69. $params = &$mainframe->getParams();
  70. $params->def('filter', 1);
  71. $params->def('filter_type', 'title');
  72. }
  73. /**
  74. * Method to set the category id
  75. *
  76. * @access public
  77. * @param int Category ID number
  78. */
  79. function setId($id)
  80. {
  81. // Set category ID and wipe data
  82. $this->_id = $id;
  83. $this->_category = null;
  84. $this->_siblings = null;
  85. $this->_data = array();
  86. $this->_total = null;
  87. }
  88. /**
  89. * Method to get content item data for the current category
  90. *
  91. * @param int $state The content state to pull from for the current
  92. * category
  93. * @since 1.5
  94. */
  95. function getData($state = 1)
  96. {
  97. // Load the Category data
  98. if ($this->_loadCategory() && $this->_loadData($state))
  99. {
  100. // Initialize some variables
  101. $user =& JFactory::getUser();
  102. // Make sure the category is published
  103. if (!$this->_category->published)
  104. {
  105. JError::raiseError(404, JText::_("Resource Not Found"));
  106. return false;
  107. }
  108. // check whether category access level allows access
  109. if ($this->_category->access > $user->get('aid', 0))
  110. {
  111. JError::raiseError(403, JText::_("ALERTNOTAUTH"));
  112. return false;
  113. }
  114. }
  115. return $this->_data[$state];
  116. }
  117. /**
  118. * Method to get the total number of content items for the frontpage
  119. *
  120. * @access public
  121. * @return integer
  122. */
  123. function getTotal($state = 1)
  124. {
  125. // Lets load the content if it doesn't already exist
  126. if (empty($this->_total))
  127. {
  128. $query = $this->_buildQuery($state);
  129. $this->_total[$state] = $this->_getListCount($query);
  130. }
  131. return $this->_total[$state];
  132. }
  133. /**
  134. * Method to get category data for the current category
  135. *
  136. * @since 1.5
  137. */
  138. function getCategory()
  139. {
  140. // Load the Category data
  141. if ($this->_loadCategory())
  142. {
  143. // Initialize some variables
  144. $user = &JFactory::getUser();
  145. // Make sure the category is published
  146. if (!$this->_category->published) {
  147. JError::raiseError(404, JText::_("Resource Not Found"));
  148. return false;
  149. }
  150. // check whether category access level allows access
  151. if ($this->_category->access > $user->get('aid', 0)) {
  152. JError::raiseError(403, JText::_("ALERTNOTAUTH"));
  153. return false;
  154. }
  155. }
  156. return $this->_category;
  157. }
  158. /**
  159. * Method to get sibling category data for the current category
  160. *
  161. * @since 1.5
  162. */
  163. function getSiblings()
  164. {
  165. // Initialize some variables
  166. $user =& JFactory::getUser();
  167. // Load the Category data
  168. if ($this->_loadCategory() && $this->_loadSiblings())
  169. {
  170. // Make sure the category is published
  171. if (!$this->_category->published)
  172. {
  173. JError::raiseError(404, JText::_("Resource Not Found"));
  174. return false;
  175. }
  176. // check whether category access level allows access
  177. if ($this->_category->access > $user->get('aid', 0))
  178. {
  179. JError::raiseError(403, JText::_("ALERTNOTAUTH"));
  180. return false;
  181. }
  182. }
  183. return $this->_siblings;
  184. }
  185. /**
  186. * Method to get archived article data for the current category
  187. *
  188. * @param int $state The content state to pull from for the current section
  189. * @since 1.5
  190. */
  191. function getArchives($state = -1)
  192. {
  193. return $this->getContent(-1);
  194. }
  195. /**
  196. * Method to load category data if it doesn't exist.
  197. *
  198. * @access private
  199. * @return boolean True on success
  200. */
  201. function _loadCategory()
  202. {
  203. if (empty($this->_category))
  204. {
  205. // Lets get the information for the current category
  206. $query = 'SELECT c.*, s.id as sectionid, s.title as sectiontitle,' .
  207. ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(":", c.id, c.alias) ELSE c.id END as slug'.
  208. ' FROM #__categories AS c' .
  209. ' INNER JOIN #__sections AS s ON s.id = c.section' .
  210. ' WHERE c.id = '. (int) $this->_id;
  211. $this->_db->setQuery($query, 0, 1);
  212. $this->_category = $this->_db->loadObject();
  213. }
  214. return true;
  215. }
  216. /**
  217. * Method to load sibling category data if it doesn't exist.
  218. *
  219. * @access private
  220. * @return boolean True on success
  221. */
  222. function _loadSiblings()
  223. {
  224. global $mainframe;
  225. if (empty($this->_category))
  226. {
  227. return false; // TODO: set error -- can't get siblings when we don't know the category
  228. }
  229. // Lets load the siblings if they don't already exist
  230. if (empty($this->_siblings))
  231. {
  232. $user =& JFactory::getUser();
  233. // Get the page/component configuration
  234. $params = &$mainframe->getParams();
  235. $noauth = !$params->get('show_noauth');
  236. $gid = (int) $user->get('aid', 0);
  237. $now = $mainframe->get('requestTime');
  238. $nullDate = $this->_db->getNullDate();
  239. $section = $this->_category->section;
  240. // Get the parameters of the active menu item
  241. $menu =& JSite::getMenu();
  242. $item = $menu->getActive();
  243. $params =& $menu->getParams($item->id);
  244. if ($user->authorize('com_content', 'edit', 'content', 'all'))
  245. {
  246. $xwhere = '';
  247. $xwhere2 = ' AND b.state >= 0';
  248. }
  249. else
  250. {
  251. $xwhere = ' AND c.published = 1';
  252. $xwhere2 = ' AND b.state = 1' .
  253. ' AND ( publish_up = '.$this->_db->Quote($nullDate).' OR publish_up <= '.$this->_db->Quote($now).' )' .
  254. ' AND ( publish_down = '.$this->_db->Quote($nullDate).' OR publish_down >= '.$this->_db->Quote($now).' )';
  255. }
  256. // show/hide empty categories
  257. $empty = null;
  258. if (!$params->get('empty_cat'))
  259. {
  260. $empty = ' HAVING COUNT( b.id ) > 0';
  261. }
  262. // Get the list of sibling categories [categories with the same parent]
  263. $query = 'SELECT c.*, COUNT( b.id ) AS numitems' .
  264. ' FROM #__categories AS c' .
  265. ' LEFT JOIN #__content AS b ON b.catid = c.id '.
  266. $xwhere2.
  267. ($noauth ? ' AND b.access <= '. (int) $gid : '') .
  268. ' WHERE c.section = '. $this->_db->Quote($section).
  269. $xwhere.
  270. ($noauth ? ' AND c.access <= '. (int) $gid : '').
  271. ' GROUP BY c.id'.$empty.
  272. ' ORDER BY c.ordering';
  273. $this->_db->setQuery($query);
  274. $this->_siblings = $this->_db->loadObjectList();
  275. }
  276. return true;
  277. }
  278. /**
  279. * Method to load content item data for items in the category if they don't
  280. * exist.
  281. *
  282. * @access private
  283. * @return boolean True on success
  284. */
  285. function _loadData($state = 1)
  286. {
  287. if (empty($this->_category)) {
  288. return false; // TODO: set error -- can't get siblings when we don't know the category
  289. }
  290. // Lets load the siblings if they don't already exist
  291. if (empty($this->_content[$state]))
  292. {
  293. // Get the pagination request variables
  294. $limit = JRequest::getVar('limit', 0, '', 'int');
  295. $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
  296. $query = $this->_buildQuery();
  297. $Arows = $this->_getList($query, $limitstart, $limit);
  298. // special handling required as Uncategorized content does not have a section / category id linkage
  299. $i = $limitstart;
  300. $rows = array();
  301. foreach ($Arows as $row)
  302. {
  303. // check to determine if section or category has proper access rights
  304. $rows[$i] = $row;
  305. $i ++;
  306. }
  307. $this->_data[$state] = $rows;
  308. }
  309. return true;
  310. }
  311. function _buildQuery($state = 1)
  312. {
  313. global $mainframe;
  314. // Get the page/component configuration
  315. $params = &$mainframe->getParams();
  316. // If voting is turned on, get voting data as well for the content items
  317. $voting = ContentHelperQuery::buildVotingQuery($params);
  318. // Get the WHERE and ORDER BY clauses for the query
  319. $where = $this->_buildContentWhere($state);
  320. $orderby = $this->_buildContentOrderBy($state);
  321. $query = 'SELECT cc.title AS category, a.id, a.title, a.title_alias, a.introtext, a.fulltext, a.sectionid, a.state, a.catid, a.created, a.created_by, a.created_by_alias, a.modified, a.modified_by,' .
  322. ' a.checked_out, a.checked_out_time, a.publish_up, a.publish_down, a.attribs, a.hits, a.images, a.urls, a.ordering, a.metakey, a.metadesc, a.access,' .
  323. ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
  324. ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug,'.
  325. ' CHAR_LENGTH( a.`fulltext` ) AS readmore, u.name AS author, u.usertype, g.name AS groups'.$voting['select'] .
  326. ' FROM #__content AS a' .
  327. ' LEFT JOIN #__categories AS cc ON a.catid = cc.id' .
  328. ' LEFT JOIN #__users AS u ON u.id = a.created_by' .
  329. ' LEFT JOIN #__groups AS g ON a.access = g.id'.
  330. $voting['join'].
  331. $where.
  332. $orderby;
  333. return $query;
  334. }
  335. function _buildContentOrderBy($state = 1)
  336. {
  337. global $mainframe;
  338. // Get the page/component configuration
  339. $params = &$mainframe->getParams();
  340. $filter_order = JRequest::getCmd('filter_order');
  341. $filter_order_Dir = JRequest::getWord('filter_order_Dir');
  342. $orderby = ' ORDER BY ';
  343. if ($filter_order && $filter_order_Dir)
  344. {
  345. $orderby .= $filter_order .' '. $filter_order_Dir.', ';
  346. }
  347. if ($filter_order == 'author')
  348. {
  349. $orderby .= 'created_by_alias '. $filter_order_Dir.', ';
  350. }
  351. switch ($state)
  352. {
  353. case -1:
  354. // Special ordering for archive articles
  355. $orderby_sec = $params->def('orderby', 'rdate');
  356. $secondary = ContentHelperQuery::orderbySecondary($orderby_sec).', ';
  357. $primary = '';
  358. break;
  359. case 1:
  360. default:
  361. $orderby_sec = $params->def('orderby_sec', 'rdate');
  362. $orderby_sec = ($orderby_sec == 'front') ? '' : $orderby_sec;
  363. $orderby_pri = $params->def('orderby_pri', '');
  364. $secondary = ContentHelperQuery::orderbySecondary($orderby_sec).', ';
  365. $primary = ContentHelperQuery::orderbyPrimary($orderby_pri);
  366. break;
  367. }
  368. $orderby .= $primary .' '. $secondary .' a.created DESC';
  369. return $orderby;
  370. }
  371. function _buildContentWhere($state = 1)
  372. {
  373. global $mainframe;
  374. $user =& JFactory::getUser();
  375. $gid = $user->get('aid', 0);
  376. $jnow =& JFactory::getDate();
  377. $now = $jnow->toMySQL();
  378. // Get the page/component configuration
  379. $params = &$mainframe->getParams();
  380. $noauth = !$params->get('show_noauth');
  381. $nullDate = $this->_db->getNullDate();
  382. $where = ' WHERE 1';
  383. // Does the user have access to view the items?
  384. if ($noauth) {
  385. $where .= ' AND a.access <= '.(int) $gid;
  386. }
  387. // First thing we need to do is assert that the articles are in the current category
  388. if ($this->_id)
  389. {
  390. $where .= ' AND a.catid = '.(int) $this->_id;
  391. }
  392. // Regular Published Content
  393. switch ($state)
  394. {
  395. case 1:
  396. if ($user->authorize('com_content', 'edit', 'content', 'all'))
  397. {
  398. $where .= ' AND a.state >= 0';
  399. }
  400. else
  401. {
  402. $where .= ' AND a.state = 1' .
  403. ' AND ( publish_up = '.$this->_db->Quote($nullDate).' OR publish_up <= '.$this->_db->Quote($now).' )' .
  404. ' AND ( publish_down = '.$this->_db->Quote($nullDate).' OR publish_down >= '.$this->_db->Quote($now).' )';
  405. }
  406. break;
  407. // Archive Content
  408. case -1:
  409. // Get some request vars specific to this state
  410. $year = JRequest::getInt( 'year', date('Y') );
  411. $month = JRequest::getInt( 'month', date('m') );
  412. $where .= ' AND a.state = -1';
  413. $where .= ' AND YEAR( a.created ) = '.(int) $year;
  414. $where .= ' AND MONTH( a.created ) = '.(int) $month;
  415. break;
  416. default:
  417. $where .= ' AND a.state = '.(int) $state;
  418. break;
  419. }
  420. /*
  421. * If we have a filter, and this is enabled... lets tack the AND clause
  422. * for the filter onto the WHERE clause of the content item query.
  423. */
  424. if ($params->get('filter'))
  425. {
  426. $filter = JRequest::getString('filter', '', 'request');
  427. if ($filter)
  428. {
  429. // clean filter variable
  430. $filter = JString::strtolower($filter);
  431. $filter = $this->_db->Quote( '%'.$this->_db->getEscaped( $filter, true ).'%', false );
  432. switch ($params->get('filter_type'))
  433. {
  434. case 'title' :
  435. $where .= ' AND LOWER( a.title ) LIKE '.$filter;
  436. break;
  437. case 'author' :
  438. $where .= ' AND ( ( LOWER( u.name ) LIKE '.$filter.' ) OR ( LOWER( a.created_by_alias ) LIKE '.$filter.' ) )';
  439. break;
  440. case 'hits' :
  441. $where .= ' AND a.hits LIKE '.$filter;
  442. break;
  443. }
  444. }
  445. }
  446. return $where;
  447. }
  448. }