PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/components/com_flexicontent/models/flexicontent.php

http://flexicontent.googlecode.com/
PHP | 504 lines | 288 code | 66 blank | 150 comment | 30 complexity | 3220e0f83ca0ba5d30a7ea5f8e01571d MD5 | raw file
Possible License(s): MIT, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * @version 1.5 stable $Id: flexicontent.php 1876 2014-03-24 03:24:41Z ggppdk $
  4. * @package Joomla
  5. * @subpackage FLEXIcontent
  6. * @copyright (C) 2009 Emmanuel Danan - www.vistamedia.fr
  7. * @license GNU/GPL v2
  8. *
  9. * FLEXIcontent is a derivative work of the excellent QuickFAQ component
  10. * @copyright (C) 2008 Christoph Lukes
  11. * see www.schlu.net for more information
  12. *
  13. * FLEXIcontent is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. // no direct access
  19. defined( '_JEXEC' ) or die( 'Restricted access' );
  20. jimport('joomla.application.component.model');
  21. /**
  22. * FLEXIcontent Component Model
  23. *
  24. * @package Joomla
  25. * @subpackage Flexicontent
  26. * @since 1.0
  27. */
  28. class FlexicontentModelFlexicontent extends JModelLegacy
  29. {
  30. /**
  31. * Root category from this directory
  32. *
  33. * @var int
  34. */
  35. var $_rootcat = null;
  36. /**
  37. * data
  38. *
  39. * @var object
  40. */
  41. var $_data = null;
  42. /**
  43. * total
  44. *
  45. * @var int
  46. */
  47. var $_total = null;
  48. /**
  49. * Pagination object
  50. *
  51. * @var object
  52. */
  53. var $_pagination = null;
  54. /**
  55. * parameters
  56. *
  57. * @var object
  58. */
  59. var $_params = null;
  60. /**
  61. * Constructor
  62. *
  63. * @since 1.0
  64. */
  65. function __construct()
  66. {
  67. parent::__construct();
  68. // Set id and load parameters
  69. $id = 0; // no id used by this view
  70. $this->setId((int)$id);
  71. $params = & $this->_params;
  72. //get the root category of the directory
  73. $this->_rootcat = (int) JRequest::getInt('rootcat', 0);
  74. if ( !$this->_rootcat)
  75. // compatibility of old saved menu items, the value is inside params instead of being URL query variable
  76. $this->_rootcat = $params->get('rootcat', FLEXI_J16GE ? 1:0);
  77. else
  78. $params->set('rootcat', $this->_rootcat);
  79. //set limits
  80. $limit = $params->def('catlimit', 5);
  81. $limitstart = JRequest::getInt('limitstart');
  82. $this->setState('limit', $limit);
  83. $this->setState('limitstart', $limitstart);
  84. }
  85. /**
  86. * Method to set initialize data, setting an element id for the view
  87. *
  88. * @access public
  89. * @param int
  90. */
  91. function setId($id)
  92. {
  93. //$this->_id = $id; // not used by current view
  94. $this->_rootcat = null;
  95. $this->_data = null;
  96. $this->_total = null;
  97. $this->_pagination = null;
  98. $this->_params = null;
  99. $this->_loadParams();
  100. }
  101. /**
  102. * Method to get Data
  103. *
  104. * @access public
  105. * @return object
  106. */
  107. function getData()
  108. {
  109. // Lets load the categories if it doesn't already exist
  110. if (empty($this->_data))
  111. {
  112. //get data
  113. $this->_data = $this->_getList( $this->_buildQuery(), $this->getState('limitstart'), $this->getState('limit') );
  114. //add childs of each category
  115. $k = 0;
  116. $count = count($this->_data);
  117. for($i = 0; $i < $count; $i++)
  118. {
  119. $category =& $this->_data[$i];
  120. $category->subcats = $this->_getsubs( $category->id );
  121. $k = 1 - $k;
  122. }
  123. }
  124. return $this->_data;
  125. }
  126. /**
  127. * Build the order clause
  128. *
  129. * @access private
  130. * @return string
  131. */
  132. function _buildCatOrderBy($prefix)
  133. {
  134. $request_var = '';
  135. $config_param = $prefix.'orderby';
  136. $default_order = $this->getState('filter_order', 'c.title');
  137. $default_order_dir = $this->getState('filter_order_dir', 'ASC');
  138. // Precedence: $request_var ==> $order ==> $config_param ==> $default_order
  139. return flexicontent_db::buildCatOrderBy(
  140. $this->_params,
  141. $order='', $request_var, $config_param,
  142. $cat_tbl_alias = 'c', $user_tbl_alias = 'u',
  143. $default_order, $default_order_dir
  144. );
  145. }
  146. /**
  147. * Method to build the Categories query
  148. * This method creates the first level categories and their assigned item count
  149. *
  150. * @access private
  151. * @return string
  152. */
  153. function _buildQuery($type='')
  154. {
  155. $params = $this->_params;
  156. $user = JFactory::getUser();
  157. $db = JFactory::getDBO();
  158. $orderby = $this->_buildCatOrderBy('cat_');
  159. // Get a 2 character language tag
  160. $lang = flexicontent_html::getUserCurrentLang();
  161. // Do we filter the categories
  162. $filtercat = $params->get('filtercat', 0);
  163. // show unauthorized items
  164. $show_noauth = $params->get('show_noauth', 0);
  165. // Build where clause
  166. $where = ' WHERE cc.published = 1';
  167. $where .= ' AND c.id = cc.id';
  168. // Filter the category view with the active active language
  169. if ((FLEXI_FISH || FLEXI_J16GE) && $filtercat) {
  170. $lta = FLEXI_J16GE ? 'i': 'ie';
  171. $where .= ' AND ( '.$lta.'.language LIKE ' . $db->Quote( $lang .'%' ) . (FLEXI_J16GE ? ' OR '.$lta.'.language="*" ' : '') . ' ) ';
  172. }
  173. $states = ((int)$user->get('gid') > 19) ? '1, -5, 0, -3, -4' : '1, -5';
  174. $where .= ' AND i.state IN ('.$states.')';
  175. // Select only items that user has view access, if listing of unauthorized content is not enabled
  176. $subjoin = $suband = $join = $and = '';
  177. if (!$show_noauth) {
  178. if (FLEXI_J16GE) {
  179. $aid_arr = $user->getAuthorisedViewLevels();
  180. $aid_list = implode(",", $aid_arr);
  181. $suband .= ' AND ty.access IN (0,'.$aid_list.')';
  182. $suband .= ' AND cc.access IN (0,'.$aid_list.')';
  183. $suband .= ' AND i.access IN (0,'.$aid_list.')';
  184. $and .= ' AND c.access IN (0,'.$aid_list.')';
  185. } else {
  186. $aid = (int) $user->get('aid');
  187. if (FLEXI_ACCESS) {
  188. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgt ON ty.id = sgt.axo AND sgt.aco = "read" AND sgt.axosection = "type"';
  189. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgc ON cc.id = sgc.axo AND sgc.aco = "read" AND sgc.axosection = "category"';
  190. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgi ON i.id = sgi.axo AND sgi.aco = "read" AND sgi.axosection = "item"';
  191. $suband .= ' AND (sgt.aro IN ( '.$user->gmid.' ) OR ty.access <= '. $aid . ')';
  192. $suband .= ' AND (sgc.aro IN ( '.$user->gmid.' ) OR cc.access <= '. $aid . ')';
  193. $suband .= ' AND (sgi.aro IN ( '.$user->gmid.' ) OR i.access <= '. $aid . ')';
  194. $join .= ' LEFT JOIN #__flexiaccess_acl AS gc ON c.id = gc.axo AND gc.aco = "read" AND gc.axosection = "category"';
  195. $and .= ' AND (gc.aro IN ( '.$user->gmid.' ) OR c.access <= '. $aid . ')';
  196. } else {
  197. $suband .= ' AND ty.access <= '.$aid;
  198. $suband .= ' AND cc.access <= '.$aid;
  199. $suband .= ' AND i.access <= '.$aid;
  200. $and .= ' AND c.access <= '.$aid;
  201. }
  202. }
  203. }
  204. $join .= (FLEXI_J16GE ? ' LEFT JOIN #__users AS u ON u.id = c.created_user_id' : '');
  205. if ($type=='feed') {
  206. }
  207. $query = 'SELECT c.*,'
  208. . (FLEXI_J16GE ? ' u.name as author,' : '')
  209. . ' CASE WHEN CHAR_LENGTH( c.alias ) THEN CONCAT_WS( \':\', c.id, c.alias ) ELSE c.id END AS slug,'
  210. . ' ('
  211. . ' SELECT COUNT( DISTINCT i.id )'
  212. . ' FROM #__content AS i'
  213. . ' JOIN #__flexicontent_cats_item_relations AS rel ON rel.itemid = i.id'
  214. . ' JOIN #__flexicontent_items_ext AS ie ON ie.item_id = i.id'
  215. . ' JOIN #__flexicontent_types AS ty ON ie.type_id = ty.id'
  216. . ' JOIN #__categories AS cc ON cc.id = rel.catid'
  217. . $subjoin
  218. . $where
  219. . $suband
  220. . ') AS assigneditems'
  221. ;
  222. $query .= ' FROM #__categories AS c'
  223. . $join
  224. . ' WHERE c.published = 1'
  225. . (!FLEXI_J16GE ? ' AND c.section = '.FLEXI_SECTION : ' AND c.extension="'.FLEXI_CAT_EXTENSION.'" ' )
  226. . (!$this->_rootcat ? ' AND c.parent_id = '.(FLEXI_J16GE ? 1 : 0) : ' AND c.parent_id = '. (int)$this->_rootcat)
  227. . $and
  228. . $orderby
  229. ;
  230. return $query;
  231. }
  232. /**
  233. * Method to build the Categories query without subselect
  234. * That's enough to get the total value.
  235. *
  236. * @access private
  237. * @return string
  238. */
  239. function _buildQueryTotal()
  240. {
  241. $params = $this->_params;
  242. $user = JFactory::getUser();
  243. // show unauthorized items
  244. $show_noauth = $params->get('show_noauth', 0);
  245. // Select only items user has access to if he is not allowed to show unauthorized items
  246. $join = $and = '';
  247. if (!$show_noauth) {
  248. if (FLEXI_J16GE) {
  249. $aid_arr = $user->getAuthorisedViewLevels();
  250. $aid_list = implode(",", $aid_arr);
  251. $and = ' AND c.access IN (0,'.$aid_list.')';
  252. } else {
  253. $aid = (int) $user->get('aid');
  254. if (FLEXI_ACCESS) {
  255. $join = ' LEFT JOIN #__flexiaccess_acl AS gc ON c.id = gc.axo AND gc.aco = "read" AND gc.axosection = "category"';
  256. $and = ' AND (gc.aro IN ( '.$user->gmid.' ) OR c.access <= '. $aid . ')';
  257. } else {
  258. $and = ' AND c.access <= '.$aid;
  259. }
  260. }
  261. }
  262. $query = 'SELECT c.id'
  263. . ' FROM #__categories AS c'
  264. . $join
  265. . ' WHERE c.published = 1'
  266. . (!FLEXI_J16GE ? ' AND c.section = '.FLEXI_SECTION : ' AND c.extension="'.FLEXI_CAT_EXTENSION.'" ' )
  267. . (!$this->_rootcat ? ' AND c.parent_id = '.(FLEXI_J16GE ? 1 : 0) : ' AND c.parent_id = '. (int)$this->_rootcat)
  268. . $and
  269. ;
  270. return $query;
  271. }
  272. /**
  273. * Total nr of Categories
  274. *
  275. * @access public
  276. * @return integer
  277. */
  278. function getTotal()
  279. {
  280. // Lets load the total nr if it doesn't already exist
  281. if (empty($this->_total))
  282. {
  283. $query = $this->_buildQueryTotal();
  284. $this->_total = $this->_getListCount($query);
  285. }
  286. return $this->_total;
  287. }
  288. /**
  289. * Method to get a pagination object
  290. *
  291. * @access public
  292. * @return integer
  293. */
  294. public function getPagination() {
  295. // Load the content if it doesn't already exist
  296. if (empty($this->_pagination)) {
  297. //jimport('joomla.html.pagination');
  298. require_once (JPATH_COMPONENT.DS.'helpers'.DS.'pagination.php');
  299. $this->_pagination = new FCPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
  300. }
  301. return $this->_pagination;
  302. }
  303. /**
  304. * Method to fetch the subcategories
  305. *
  306. * @access private
  307. * @return object
  308. */
  309. function _getsubs($id)
  310. {
  311. $params = $this->_params;
  312. $user = JFactory::getUser();
  313. $db = JFactory::getDBO();
  314. $orderby = $this->_buildCatOrderBy('subcat_');
  315. // Get a 2 character language tag
  316. $lang = flexicontent_html::getUserCurrentLang();
  317. // Do we filter the categories
  318. $filtercat = $params->get('filtercat', 0);
  319. // show unauthorized items
  320. $show_noauth = $params->get('show_noauth', 0);
  321. // Build where clause
  322. $where = ' WHERE cc.published = 1';
  323. $where .= ' AND c.id = cc.id';
  324. // Filter the category view with the active active language
  325. if ((FLEXI_FISH || FLEXI_J16GE) && $filtercat) {
  326. $lta = FLEXI_J16GE ? 'i': 'ie';
  327. $where .= ' AND ( '.$lta.'.language LIKE ' . $db->Quote( $lang .'%' ) . (FLEXI_J16GE ? ' OR '.$lta.'.language="*" ' : '') . ' ) ';
  328. }
  329. $states = ((int)$user->get('gid') > 19) ? '1, -5, 0, -3, -4' : '1, -5';
  330. $where .= ' AND i.state IN ('.$states.')';
  331. // Select only items that user has view access, if listing of unauthorized content is not enabled
  332. $subjoin = $suband = $join = $and = '';
  333. if (!$show_noauth) {
  334. if (FLEXI_J16GE) {
  335. $aid_arr = $user->getAuthorisedViewLevels();
  336. $aid_list = implode(",", $aid_arr);
  337. $suband .= ' AND ty.access IN (0,'.$aid_list.')';
  338. $suband .= ' AND cc.access IN (0,'.$aid_list.')';
  339. $suband .= ' AND i.access IN (0,'.$aid_list.')';
  340. $and .= ' AND c.access IN (0,'.$aid_list.')';
  341. } else {
  342. $aid = (int) $user->get('aid');
  343. if (FLEXI_ACCESS) {
  344. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgt ON ty.id = sgt.axo AND sgt.aco = "read" AND sgt.axosection = "type"';
  345. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgc ON cc.id = sgc.axo AND sgc.aco = "read" AND sgc.axosection = "category"';
  346. $subjoin .= ' LEFT JOIN #__flexiaccess_acl AS sgi ON i.id = sgi.axo AND sgi.aco = "read" AND sgi.axosection = "item"';
  347. $suband .= ' AND (sgt.aro IN ( '.$user->gmid.' ) OR ty.access <= '. $aid . ')';
  348. $suband .= ' AND (sgc.aro IN ( '.$user->gmid.' ) OR cc.access <= '. $aid . ')';
  349. $suband .= ' AND (sgi.aro IN ( '.$user->gmid.' ) OR i.access <= '. $aid . ')';
  350. $join .= ' LEFT JOIN #__flexiaccess_acl AS gc ON c.id = gc.axo AND gc.aco = "read" AND gc.axosection = "category"';
  351. $and .= ' AND (gc.aro IN ( '.$user->gmid.' ) OR c.access <= '. $aid . ')';
  352. } else {
  353. $suband .= ' AND ty.access <= '.$aid;
  354. $suband .= ' AND cc.access <= '.$aid;
  355. $suband .= ' AND i.access <= '.$aid;
  356. $and .= ' AND c.access <= '.$aid;
  357. }
  358. }
  359. }
  360. $query = 'SELECT c.*,'
  361. . ' CASE WHEN CHAR_LENGTH( c.alias ) THEN CONCAT_WS( \':\', c.id, c.alias ) ELSE c.id END AS slug,'
  362. . ' ('
  363. . ' SELECT COUNT( DISTINCT i.id )'
  364. . ' FROM #__content AS i'
  365. . ' JOIN #__flexicontent_cats_item_relations AS rel ON rel.itemid = i.id'
  366. . ' JOIN #__flexicontent_items_ext AS ie ON ie.item_id = i.id'
  367. . ' JOIN #__flexicontent_types AS ty ON ie.type_id = ty.id'
  368. . ' JOIN #__categories AS cc ON cc.id = rel.catid'
  369. . $subjoin
  370. . $where
  371. . $suband
  372. . ' ) AS assignedsubitems,'
  373. . ' ('
  374. . ' SELECT COUNT( sc.id )'
  375. . ' FROM #__categories AS sc'
  376. . ' WHERE c.id = sc.parent_id'
  377. . ' AND sc.published = 1'
  378. . ' ) AS assignedcats'
  379. . ' FROM #__categories AS c'
  380. . $join
  381. . ' WHERE c.published = 1'
  382. . (!FLEXI_J16GE ? ' AND c.section = '.FLEXI_SECTION : ' AND c.extension="'.FLEXI_CAT_EXTENSION.'" ' )
  383. . ' AND c.parent_id = '.(int)$id
  384. . $and
  385. . $orderby
  386. ;
  387. $this->_db->setQuery($query);
  388. $this->_subs = $this->_db->loadObjectList();
  389. return $this->_subs;
  390. }
  391. /**
  392. * Get the feed data
  393. *
  394. * @access public
  395. * @return object
  396. */
  397. function getFeed()
  398. {
  399. $feed = $this->_getList( $this->_buildQuery('feed'), $this->getState('limitstart'), $this->getState('limit') );
  400. if ($this->_db->getErrorNum()) {
  401. echo __FUNCTION__.'(): SQL QUERY ERROR:<br/>'.nl2br($this->_db->getErrorMsg()),'error';
  402. exit;
  403. }
  404. return $feed;
  405. }
  406. /**
  407. * Method to load parameters
  408. *
  409. * @access private
  410. * @return void
  411. * @since 1.5
  412. */
  413. function _loadParams()
  414. {
  415. if ( $this->_params !== NULL ) return;
  416. $app = JFactory::getApplication();
  417. $menu = $app->getMenu()->getActive(); // Retrieve active menu
  418. // Get the COMPONENT only parameters, then merge the menu parameters
  419. $comp_params = JComponentHelper::getComponent('com_flexicontent')->params;
  420. $params = FLEXI_J16GE ? clone ($comp_params) : new JParameter( $comp_params ); // clone( JComponentHelper::getParams('com_flexicontent') );
  421. if ($menu) {
  422. $menu_params = FLEXI_J16GE ? $menu->params : new JParameter($menu->params);
  423. $params->merge($menu_params);
  424. }
  425. $this->_params = $params;
  426. }
  427. /**
  428. * Method to get view's parameters
  429. *
  430. * @access public
  431. * @return object
  432. */
  433. function &getParams()
  434. {
  435. return $this->_params;
  436. }
  437. }
  438. ?>