1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class JTableUsergroup extends JTable
18 {
19 20 21 22 23 24 25
26 public function __construct($db)
27 {
28 parent::__construct('#__usergroups', 'id', $db);
29 }
30
31 32 33 34 35 36 37
38 public function check()
39 {
40
41 if ((trim($this->title)) == '')
42 {
43 $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE'));
44
45 return false;
46 }
47
48
49
50 $db = $this->_db;
51 $query = $db->getQuery(true)
52 ->select('COUNT(title)')
53 ->from($this->_tbl)
54 ->where('title = ' . $db->quote(trim($this->title)))
55 ->where('parent_id = ' . (int) $this->parent_id)
56 ->where('id <> ' . (int) $this->id);
57 $db->setQuery($query);
58
59 if ($db->loadResult() > 0)
60 {
61 $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE_EXISTS'));
62
63 return false;
64 }
65
66 return true;
67 }
68
69 70 71 72 73 74 75 76 77 78
79 public function rebuild($parent_id = 0, $left = 0)
80 {
81
82 $db = $this->_db;
83
84
85 $db->setQuery('SELECT id FROM ' . $this->_tbl . ' WHERE parent_id=' . (int) $parent_id . ' ORDER BY parent_id, title');
86 $children = $db->loadColumn();
87
88
89 $right = $left + 1;
90
91
92 for ($i = 0, $n = count($children); $i < $n; $i++)
93 {
94
95 $right = $this->rebuild($children[$i], $right);
96
97
98 if ($right === false)
99 {
100 return false;
101 }
102 }
103
104
105
106 $db->setQuery('UPDATE ' . $this->_tbl . ' SET lft=' . (int) $left . ', rgt=' . (int) $right . ' WHERE id=' . (int) $parent_id);
107
108
109 try
110 {
111 $db->execute();
112 }
113 catch (JDatabaseExceptionExecuting $e)
114 {
115 return false;
116 }
117
118
119 return $right + 1;
120 }
121
122 123 124 125 126 127 128 129 130
131 public function store($updateNulls = false)
132 {
133 if ($result = parent::store($updateNulls))
134 {
135
136 $this->rebuild();
137 }
138
139 return $result;
140 }
141
142 143 144 145 146 147 148 149 150 151 152
153 public function delete($oid = null)
154 {
155 if ($oid)
156 {
157 $this->load($oid);
158 }
159
160 if ($this->id == 0)
161 {
162 throw new UnexpectedValueException('Global Category not found');
163 }
164
165 if ($this->parent_id == 0)
166 {
167 throw new UnexpectedValueException('Root categories cannot be deleted.');
168 }
169
170 if ($this->lft == 0 || $this->rgt == 0)
171 {
172 throw new UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
173 }
174
175 $db = $this->_db;
176
177
178 $query = $db->getQuery(true)
179 ->select($db->quoteName('c.id'))
180 ->from($db->quoteName($this->_tbl) . 'AS c')
181 ->where($db->quoteName('c.lft') . ' >= ' . (int) $this->lft)
182 ->where($db->quoteName('c.rgt') . ' <= ' . (int) $this->rgt);
183 $db->setQuery($query);
184 $ids = $db->loadColumn();
185
186 if (empty($ids))
187 {
188 throw new UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
189 }
190
191
192
193
194
195 $query->clear()
196 ->delete($db->quoteName($this->_tbl))
197 ->where($db->quoteName('id') . ' IN (' . implode(',', $ids) . ')');
198 $db->setQuery($query);
199 $db->execute();
200
201
202 $replace = array();
203
204 foreach ($ids as $id)
205 {
206 $replace[] = ',' . $db->quote("[$id,") . ',' . $db->quote('[') . ')';
207 $replace[] = ',' . $db->quote(",$id,") . ',' . $db->quote(',') . ')';
208 $replace[] = ',' . $db->quote(",$id]") . ',' . $db->quote(']') . ')';
209 $replace[] = ',' . $db->quote("[$id]") . ',' . $db->quote('[]') . ')';
210 }
211
212 $query->clear()
213 ->select('id, rules')
214 ->from('#__viewlevels');
215 $db->setQuery($query);
216 $rules = $db->loadObjectList();
217
218 $match_ids = array();
219
220 foreach ($rules as $rule)
221 {
222 foreach ($ids as $id)
223 {
224 if (strstr($rule->rules, '[' . $id) || strstr($rule->rules, ',' . $id) || strstr($rule->rules, $id . ']'))
225 {
226 $match_ids[] = $rule->id;
227 }
228 }
229 }
230
231 if (!empty($match_ids))
232 {
233 $query->clear()
234 ->set('rules=' . str_repeat('replace(', 4 * count($ids)) . 'rules' . implode('', $replace))
235 ->update('#__viewlevels')
236 ->where('id IN (' . implode(',', $match_ids) . ')');
237 $db->setQuery($query);
238 $db->execute();
239 }
240
241
242 $query->clear()
243 ->delete($db->quoteName('#__user_usergroup_map'))
244 ->where($db->quoteName('group_id') . ' IN (' . implode(',', $ids) . ')');
245 $db->setQuery($query);
246 $db->execute();
247
248 return true;
249 }
250 }
251