Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
usergroup.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Table
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Usergroup table class.
14  *
15  * @package Joomla.Platform
16  * @subpackage Table
17  * @since 11.1
18  */
19 class JTableUsergroup extends JTable
20 {
21  /**
22  * Constructor
23  *
24  * @param JDatabaseDriver $db Database driver object.
25  *
26  * @since 11.1
27  */
28  public function __construct(JDatabaseDriver $db)
29  {
30  parent::__construct('#__usergroups', 'id', $db);
31  }
32 
33  /**
34  * Method to check the current record to save
35  *
36  * @return boolean True on success
37  *
38  * @since 11.1
39  */
40  public function check()
41  {
42  // Validate the title.
43  if ((trim($this->title)) == '')
44  {
45  $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE'));
46 
47  return false;
48  }
49 
50  // Check for a duplicate parent_id, title.
51  // There is a unique index on the (parent_id, title) field in the table.
52  $db = $this->_db;
53  $query = $db->getQuery(true)
54  ->select('COUNT(title)')
55  ->from($this->_tbl)
56  ->where('title = ' . $db->quote(trim($this->title)))
57  ->where('parent_id = ' . (int) $this->parent_id)
58  ->where('id <> ' . (int) $this->id);
59  $db->setQuery($query);
60 
61  if ($db->loadResult() > 0)
62  {
63  $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE_EXISTS'));
64 
65  return false;
66  }
67 
68  return true;
69  }
70 
71  /**
72  * Method to recursively rebuild the nested set tree.
73  *
74  * @param integer $parent_id The root of the tree to rebuild.
75  * @param integer $left The left id to start with in building the tree.
76  *
77  * @return boolean True on success
78  *
79  * @since 11.1
80  */
81  public function rebuild($parent_id = 0, $left = 0)
82  {
83  // Get the database object
84  $db = $this->_db;
85 
86  // Get all children of this node
87  $db->setQuery('SELECT id FROM ' . $this->_tbl . ' WHERE parent_id=' . (int) $parent_id . ' ORDER BY parent_id, title');
88  $children = $db->loadColumn();
89 
90  // The right value of this node is the left value + 1
91  $right = $left + 1;
92 
93  // Execute this function recursively over all children
94  for ($i = 0, $n = count($children); $i < $n; $i++)
95  {
96  // $right is the current right value, which is incremented on recursion return
97  $right = $this->rebuild($children[$i], $right);
98 
99  // If there is an update failure, return false to break out of the recursion
100  if ($right === false)
101  {
102  return false;
103  }
104  }
105 
106  // We've got the left value, and now that we've processed
107  // the children of this node we also know the right value
108  $db->setQuery('UPDATE ' . $this->_tbl . ' SET lft=' . (int) $left . ', rgt=' . (int) $right . ' WHERE id=' . (int) $parent_id);
109 
110  // If there is an update failure, return false to break out of the recursion
111  if (!$db->execute())
112  {
113  return false;
114  }
115 
116  // Return the right value of this node + 1
117  return $right + 1;
118  }
119 
120  /**
121  * Inserts a new row if id is zero or updates an existing row in the database table
122  *
123  * @param boolean $updateNulls If false, null object variables are not updated
124  *
125  * @return boolean True if successful, false otherwise and an internal error message is set
126  *
127  * @since 11.1
128  */
129  public function store($updateNulls = false)
130  {
131  if ($result = parent::store($updateNulls))
132  {
133  // Rebuild the nested set tree.
134  $this->rebuild();
135  }
136 
137  return $result;
138  }
139 
140  /**
141  * Delete this object and its dependencies
142  *
143  * @param integer $oid The primary key of the user group to delete.
144  *
145  * @return mixed Boolean or Exception.
146  *
147  * @since 11.1
148  * @throws RuntimeException on database error.
149  * @throws UnexpectedValueException on data error.
150  */
151  public function delete($oid = null)
152  {
153  if ($oid)
154  {
155  $this->load($oid);
156  }
157 
158  if ($this->id == 0)
159  {
160  throw new UnexpectedValueException('Global Category not found');
161  }
162 
163  if ($this->parent_id == 0)
164  {
165  throw new UnexpectedValueException('Root categories cannot be deleted.');
166  }
167 
168  if ($this->lft == 0 || $this->rgt == 0)
169  {
170  throw new UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
171  }
172 
173  $db = $this->_db;
174 
175  // Select the usergroup ID and its children
176  $query = $db->getQuery(true)
177  ->select($db->quoteName('c.id'))
178  ->from($db->quoteName($this->_tbl) . 'AS c')
179  ->where($db->quoteName('c.lft') . ' >= ' . (int) $this->lft)
180  ->where($db->quoteName('c.rgt') . ' <= ' . (int) $this->rgt);
181  $db->setQuery($query);
182  $ids = $db->loadColumn();
183 
184  if (empty($ids))
185  {
186  throw new UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
187  }
188 
189  // Delete the category dependencies
190  // @todo Remove all related threads, posts and subscriptions
191 
192  // Delete the usergroup and its children
193  $query->clear()
194  ->delete($db->quoteName($this->_tbl))
195  ->where($db->quoteName('id') . ' IN (' . implode(',', $ids) . ')');
196  $db->setQuery($query);
197  $db->execute();
198 
199  // Delete the usergroup in view levels
200  $replace = array();
201 
202  foreach ($ids as $id)
203  {
204  $replace[] = ',' . $db->quote("[$id,") . ',' . $db->quote("[") . ')';
205  $replace[] = ',' . $db->quote(",$id,") . ',' . $db->quote(",") . ')';
206  $replace[] = ',' . $db->quote(",$id]") . ',' . $db->quote("]") . ')';
207  $replace[] = ',' . $db->quote("[$id]") . ',' . $db->quote("[]") . ')';
208  }
209 
210  $query->clear()
211  ->select('id, rules')
212  ->from('#__viewlevels');
213  $db->setQuery($query);
214  $rules = $db->loadObjectList();
215 
216  $match_ids = array();
217 
218  foreach ($rules as $rule)
219  {
220  foreach ($ids as $id)
221  {
222  if (strstr($rule->rules, '[' . $id) || strstr($rule->rules, ',' . $id) || strstr($rule->rules, $id . ']'))
223  {
224  $match_ids[] = $rule->id;
225  }
226  }
227  }
228 
229  if (!empty($match_ids))
230  {
231  $query->clear()
232  ->set('rules=' . str_repeat('replace(', 4 * count($ids)) . 'rules' . implode('', $replace))
233  ->update('#__viewlevels')
234  ->where('id IN (' . implode(',', $match_ids) . ')');
235  $db->setQuery($query);
236  $db->execute();
237  }
238 
239  // Delete the user to usergroup mappings for the group(s) from the database.
240  $query->clear()
241  ->delete($db->quoteName('#__user_usergroup_map'))
242  ->where($db->quoteName('group_id') . ' IN (' . implode(',', $ids) . ')');
243  $db->setQuery($query);
244  $db->execute();
245 
246  return true;
247  }
248 }