/administrator/components/com_users/models/levels.php
PHP | 214 lines | 107 code | 30 blank | 77 comment | 10 complexity | 84f5448e7d2d7662d480f3aa2e9be693 MD5 | raw file
Possible License(s): LGPL-2.1
1<?php 2/** 3 * @package Joomla.Administrator 4 * @subpackage com_users 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 * Methods supporting a list of user access level records. 14 * 15 * @package Joomla.Administrator 16 * @subpackage com_users 17 * @since 1.6 18 */ 19class UsersModelLevels extends JModelList 20{ 21 /** 22 * Constructor. 23 * 24 * @param array An optional associative array of configuration settings. 25 * @see JController 26 * @since 1.6 27 */ 28 public function __construct($config = array()) 29 { 30 if (empty($config['filter_fields'])) { 31 $config['filter_fields'] = array( 32 'id', 'a.id', 33 'title', 'a.title', 34 'ordering', 'a.ordering', 35 ); 36 } 37 38 parent::__construct($config); 39 } 40 41 /** 42 * Method to auto-populate the model state. 43 * 44 * Note. Calling getState in this method will result in recursion. 45 * 46 * @since 1.6 47 */ 48 protected function populateState($ordering = null, $direction = null) 49 { 50 $app = JFactory::getApplication('administrator'); 51 52 // Load the filter state. 53 $search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search'); 54 $this->setState('filter.search', $search); 55 56 // Load the parameters. 57 $params = JComponentHelper::getParams('com_users'); 58 $this->setState('params', $params); 59 60 // List state information. 61 parent::populateState('a.title', 'asc'); 62 } 63 64 /** 65 * Method to get a store id based on model configuration state. 66 * 67 * This is necessary because the model is used by the component and 68 * different modules that might need different sets of data or different 69 * ordering requirements. 70 * 71 * @param string $id A prefix for the store id. 72 * 73 * @return string A store id. 74 */ 75 protected function getStoreId($id = '') 76 { 77 // Compile the store id. 78 $id .= ':'.$this->getState('filter.search'); 79 80 return parent::getStoreId($id); 81 } 82 83 /** 84 * Build an SQL query to load the list data. 85 * 86 * @return JDatabaseQuery 87 */ 88 protected function getListQuery() 89 { 90 // Create a new query object. 91 $db = $this->getDbo(); 92 $query = $db->getQuery(true); 93 94 // Select the required fields from the table. 95 $query->select( 96 $this->getState( 97 'list.select', 98 'a.*' 99 ) 100 ); 101 $query->from($db->quoteName('#__viewlevels').' AS a'); 102 103 // Add the level in the tree. 104 $query->group('a.id, a.title, a.ordering, a.rules'); 105 106 // Filter the items over the search string if set. 107 $search = $this->getState('filter.search'); 108 if (!empty($search)) { 109 if (stripos($search, 'id:') === 0) { 110 $query->where('a.id = '.(int) substr($search, 3)); 111 } else { 112 $search = $db->Quote('%'.$db->escape($search, true).'%'); 113 $query->where('a.title LIKE '.$search); 114 } 115 } 116 117 $query->group('a.id'); 118 119 // Add the list ordering clause. 120 $query->order($db->escape($this->getState('list.ordering', 'a.lft')).' '.$db->escape($this->getState('list.direction', 'ASC'))); 121 122 //echo nl2br(str_replace('#__','jos_',$query)); 123 return $query; 124 } 125 126 /** 127 * Method to adjust the ordering of a row. 128 * 129 * @param int The ID of the primary key to move. 130 * @param integer Increment, usually +1 or -1 131 * @return boolean False on failure or error, true otherwise. 132 */ 133 public function reorder($pk, $direction = 0) 134 { 135 // Sanitize the id and adjustment. 136 $pk = (!empty($pk)) ? $pk : (int) $this->getState('level.id'); 137 $user = JFactory::getUser(); 138 139 // Get an instance of the record's table. 140 $table = JTable::getInstance('viewlevel'); 141 142 // Load the row. 143 if (!$table->load($pk)) { 144 $this->setError($table->getError()); 145 return false; 146 } 147 148 // Access checks. 149 $allow = $user->authorise('core.edit.state', 'com_users'); 150 151 if (!$allow) 152 { 153 $this->setError(JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED')); 154 return false; 155 } 156 157 // Move the row. 158 // TODO: Where clause to restrict category. 159 $table->move($pk); 160 161 return true; 162 } 163 164 /** 165 * Saves the manually set order of records. 166 * 167 * @param array An array of primary key ids. 168 * @param int +/-1 169 */ 170 public function saveorder($pks, $order) 171 { 172 $table = JTable::getInstance('viewlevel'); 173 $user = JFactory::getUser(); 174 $conditions = array(); 175 176 if (empty($pks)) { 177 return JError::raiseWarning(500, JText::_('COM_USERS_ERROR_LEVELS_NOLEVELS_SELECTED')); 178 } 179 180 // update ordering values 181 foreach ($pks as $i => $pk) 182 { 183 $table->load((int) $pk); 184 185 // Access checks. 186 $allow = $user->authorise('core.edit.state', 'com_users'); 187 188 if (!$allow) 189 { 190 // Prune items that you can't change. 191 unset($pks[$i]); 192 JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED')); 193 } 194 elseif ($table->ordering != $order[$i]) 195 { 196 $table->ordering = $order[$i]; 197 if (!$table->store()) 198 { 199 $this->setError($table->getError()); 200 return false; 201 } 202 } 203 } 204 205 // Execute reorder for each category. 206 foreach ($conditions as $cond) 207 { 208 $table->load($cond[0]); 209 $table->reorder($cond[1]); 210 } 211 212 return true; 213 } 214}