/components/com_weblinks/models/category.php
PHP | 312 lines | 169 code | 44 blank | 99 comment | 22 complexity | 494fcd47f3be30bd5703b813499291d5 MD5 | raw file
Possible License(s): LGPL-2.1
1<?php 2/** 3 * @package Joomla.Site 4 * @subpackage com_weblinks 5 * 6 * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10defined('_JEXEC') or die; 11 12/** 13 * Weblinks Component Weblink Model 14 * 15 * @package Joomla.Site 16 * @subpackage com_weblinks 17 * @since 1.5 18 */ 19class WeblinksModelCategory extends JModelList 20{ 21 /** 22 * Category items data 23 * 24 * @var array 25 */ 26 protected $_item = null; 27 28 protected $_articles = null; 29 30 protected $_siblings = null; 31 32 protected $_children = null; 33 34 protected $_parent = null; 35 36 /** 37 * Constructor. 38 * 39 * @param array An optional associative array of configuration settings. 40 * @see JController 41 * @since 1.6 42 */ 43 public function __construct($config = array()) 44 { 45 if (empty($config['filter_fields'])) { 46 $config['filter_fields'] = array( 47 'id', 'a.id', 48 'title', 'a.title', 49 'hits', 'a.hits', 50 'ordering', 'a.ordering', 51 ); 52 } 53 54 parent::__construct($config); 55 } 56 57 /** 58 * The category that applies. 59 * 60 * @access protected 61 * @var object 62 */ 63 protected $_category = null; 64 65 /** 66 * The list of other weblink categories. 67 * 68 * @access protected 69 * @var array 70 */ 71 protected $_categories = null; 72 73 /** 74 * Method to get a list of items. 75 * 76 * @return mixed An array of objects on success, false on failure. 77 */ 78 public function getItems() 79 { 80 // Invoke the parent getItems method to get the main list 81 $items = parent::getItems(); 82 83 // Convert the params field into an object, saving original in _params 84 for ($i = 0, $n = count($items); $i < $n; $i++) { 85 if (!isset($this->_params)) { 86 $params = new JRegistry; 87 $params->loadString($items[$i]->params); 88 $items[$i]->params = $params; 89 } 90 } 91 92 return $items; 93 } 94 95 /** 96 * Method to build an SQL query to load the list data. 97 * 98 * @return string An SQL query 99 * @since 1.6 100 */ 101 protected function getListQuery() 102 { 103 $user = JFactory::getUser(); 104 $groups = implode(',', $user->getAuthorisedViewLevels()); 105 106 // Create a new query object. 107 $db = $this->getDbo(); 108 $query = $db->getQuery(true); 109 110 // Select required fields from the categories. 111 $query->select($this->getState('list.select', 'a.*')); 112 $query->from($db->quoteName('#__weblinks').' AS a'); 113 $query->where('a.access IN ('.$groups.')'); 114 115 // Filter by category. 116 if ($categoryId = $this->getState('category.id')) { 117 $query->where('a.catid = '.(int) $categoryId); 118 $query->join('LEFT', '#__categories AS c ON c.id = a.catid'); 119 $query->where('c.access IN ('.$groups.')'); 120 121 //Filter by published category 122 $cpublished = $this->getState('filter.c.published'); 123 if (is_numeric($cpublished)) { 124 $query->where('c.published = '.(int) $cpublished); 125 } 126 } 127 128 // Join over the users for the author and modified_by names. 129 $query->select("CASE WHEN a.created_by_alias > ' ' THEN a.created_by_alias ELSE ua.name END AS author"); 130 $query->select("ua.email AS author_email"); 131 132 $query->join('LEFT', '#__users AS ua ON ua.id = a.created_by'); 133 $query->join('LEFT', '#__users AS uam ON uam.id = a.modified_by'); 134 135 // Filter by state 136 137 $state = $this->getState('filter.state'); 138 if (is_numeric($state)) { 139 $query->where('a.state = '.(int) $state); 140 } 141 // do not show trashed links on the front-end 142 $query->where('a.state != -2'); 143 144 // Filter by start and end dates. 145 $nullDate = $db->Quote($db->getNullDate()); 146 $date = JFactory::getDate(); 147 $nowDate = $db->Quote($date->toSql()); 148 149 if ($this->getState('filter.publish_date')){ 150 $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')'); 151 $query->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')'); 152 } 153 154 // Filter by language 155 if ($this->getState('filter.language')) { 156 $query->where('a.language in (' . $db->Quote(JFactory::getLanguage()->getTag()) . ',' . $db->Quote('*') . ')'); 157 } 158 159 // Add the list ordering clause. 160 $query->order($db->escape($this->getState('list.ordering', 'a.ordering')).' '.$db->escape($this->getState('list.direction', 'ASC'))); 161 return $query; 162 } 163 164 /** 165 * Method to auto-populate the model state. 166 * 167 * Note. Calling getState in this method will result in recursion. 168 * 169 * @since 1.6 170 */ 171 protected function populateState($ordering = null, $direction = null) 172 { 173 $app = JFactory::getApplication(); 174 $params = JComponentHelper::getParams('com_weblinks'); 175 176 // List state information 177 $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'uint'); 178 $this->setState('list.limit', $limit); 179 180 $limitstart = $app->input->get('limitstart', 0, 'uint'); 181 $this->setState('list.start', $limitstart); 182 183 $orderCol = $app->input->get('filter_order', 'ordering'); 184 if (!in_array($orderCol, $this->filter_fields)) { 185 $orderCol = 'ordering'; 186 } 187 $this->setState('list.ordering', $orderCol); 188 189 $listOrder = $app->input->get('filter_order_Dir', 'ASC'); 190 if (!in_array(strtoupper($listOrder), array('ASC', 'DESC', ''))) { 191 $listOrder = 'ASC'; 192 } 193 $this->setState('list.direction', $listOrder); 194 195 $id = $app->input->get('id', 0, 'int'); 196 $this->setState('category.id', $id); 197 198 $user = JFactory::getUser(); 199 if ((!$user->authorise('core.edit.state', 'com_weblinks')) && (!$user->authorise('core.edit', 'com_weblinks'))){ 200 // limit to published for people who can't edit or edit.state. 201 $this->setState('filter.state', 1); 202 203 // Filter by start and end dates. 204 $this->setState('filter.publish_date', true); 205 } 206 207 $this->setState('filter.language', $app->getLanguageFilter()); 208 209 // Load the parameters. 210 $this->setState('params', $params); 211 } 212 213 /** 214 * Method to get category data for the current category 215 * 216 * @param int An optional ID 217 * 218 * @return object 219 * @since 1.5 220 */ 221 public function getCategory() 222 { 223 if(!is_object($this->_item)) 224 { 225 $app = JFactory::getApplication(); 226 $menu = $app->getMenu(); 227 $active = $menu->getActive(); 228 $params = new JRegistry; 229 230 if($active) 231 { 232 $params->loadString($active->params); 233 } 234 235 $options = array(); 236 $options['countItems'] = $params->get('show_cat_num_links_cat', 1) || $params->get('show_empty_categories', 0); 237 $categories = JCategories::getInstance('Weblinks', $options); 238 $this->_item = $categories->get($this->getState('category.id', 'root')); 239 if(is_object($this->_item)) 240 { 241 $this->_children = $this->_item->getChildren(); 242 $this->_parent = false; 243 if($this->_item->getParent()) 244 { 245 $this->_parent = $this->_item->getParent(); 246 } 247 $this->_rightsibling = $this->_item->getSibling(); 248 $this->_leftsibling = $this->_item->getSibling(false); 249 } else { 250 $this->_children = false; 251 $this->_parent = false; 252 } 253 } 254 255 return $this->_item; 256 } 257 258 /** 259 * Get the parent category 260 * 261 * @param int An optional category id. If not supplied, the model state 'category.id' will be used. 262 * 263 * @return mixed An array of categories or false if an error occurs. 264 */ 265 public function getParent() 266 { 267 if(!is_object($this->_item)) 268 { 269 $this->getCategory(); 270 } 271 return $this->_parent; 272 } 273 274 /** 275 * Get the sibling (adjacent) categories. 276 * 277 * @return mixed An array of categories or false if an error occurs. 278 */ 279 function &getLeftSibling() 280 { 281 if(!is_object($this->_item)) 282 { 283 $this->getCategory(); 284 } 285 return $this->_leftsibling; 286 } 287 288 function &getRightSibling() 289 { 290 if(!is_object($this->_item)) 291 { 292 $this->getCategory(); 293 } 294 return $this->_rightsibling; 295 } 296 297 /** 298 * Get the child categories. 299 * 300 * @param int An optional category id. If not supplied, the model state 'category.id' will be used. 301 * 302 * @return mixed An array of categories or false if an error occurs. 303 */ 304 function &getChildren() 305 { 306 if(!is_object($this->_item)) 307 { 308 $this->getCategory(); 309 } 310 return $this->_children; 311 } 312}