/administrator/components/com_users/models/levels.php

https://bitbucket.org/eternaware/joomus · PHP · 214 lines · 107 code · 30 blank · 77 comment · 10 complexity · 84f5448e7d2d7662d480f3aa2e9be693 MD5 · raw file

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