[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Table; 11 12 use Joomla\CMS\Language\Text; 13 use Joomla\Database\DatabaseDriver; 14 use Joomla\Database\Exception\ExecutionFailureException; 15 use Joomla\Database\ParameterType; 16 17 // phpcs:disable PSR1.Files.SideEffects 18 \defined('JPATH_PLATFORM') or die; 19 // phpcs:enable PSR1.Files.SideEffects 20 21 /** 22 * Usergroup table class. 23 * 24 * @since 1.7.0 25 */ 26 class Usergroup extends Table 27 { 28 /** 29 * Constructor 30 * 31 * @param DatabaseDriver $db Database driver object. 32 * 33 * @since 1.7.0 34 */ 35 public function __construct(DatabaseDriver $db) 36 { 37 parent::__construct('#__usergroups', 'id', $db); 38 } 39 40 /** 41 * Method to check the current record to save 42 * 43 * @return boolean True on success 44 * 45 * @since 1.7.0 46 */ 47 public function check() 48 { 49 try { 50 parent::check(); 51 } catch (\Exception $e) { 52 $this->setError($e->getMessage()); 53 54 return false; 55 } 56 57 // Validate the title. 58 if ((trim($this->title)) == '') { 59 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE')); 60 61 return false; 62 } 63 64 // The parent_id can not be equal to the current id 65 if ($this->id === (int) $this->parent_id) { 66 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID')); 67 68 return false; 69 } 70 71 // Check for a duplicate parent_id, title. 72 // There is a unique index on the (parent_id, title) field in the table. 73 $db = $this->_db; 74 $parentId = (int) $this->parent_id; 75 $title = trim($this->title); 76 $id = (int) $this->id; 77 $query = $db->getQuery(true) 78 ->select('COUNT(title)') 79 ->from($this->_tbl) 80 ->where($db->quoteName('title') . ' = :title') 81 ->where($db->quoteName('parent_id') . ' = :parentid') 82 ->where($db->quoteName('id') . ' <> :id') 83 ->bind(':title', $title) 84 ->bind(':parentid', $parentId, ParameterType::INTEGER) 85 ->bind(':id', $id, ParameterType::INTEGER); 86 $db->setQuery($query); 87 88 if ($db->loadResult() > 0) { 89 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE_EXISTS')); 90 91 return false; 92 } 93 94 // We do not allow to move non public to root and public to non-root 95 if (!empty($this->id)) { 96 $table = self::getInstance('Usergroup', 'JTable', array('dbo' => $this->getDbo())); 97 98 $table->load($this->id); 99 100 if ((!$table->parent_id && $this->parent_id) || ($table->parent_id && !$this->parent_id)) { 101 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID')); 102 103 return false; 104 } 105 } elseif (!$this->parent_id) { 106 // New entry should always be greater 0 107 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID')); 108 109 return false; 110 } 111 112 // The new parent_id has to be a valid group 113 if ($this->parent_id) { 114 $table = self::getInstance('Usergroup', 'JTable', array('dbo' => $this->getDbo())); 115 $table->load($this->parent_id); 116 117 if ($table->id != $this->parent_id) { 118 $this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID')); 119 120 return false; 121 } 122 } 123 124 return true; 125 } 126 127 /** 128 * Method to recursively rebuild the nested set tree. 129 * 130 * @param integer $parentId The root of the tree to rebuild. 131 * @param integer $left The left id to start with in building the tree. 132 * 133 * @return boolean True on success 134 * 135 * @since 1.7.0 136 */ 137 public function rebuild($parentId = 0, $left = 0) 138 { 139 // Get the database object 140 $db = $this->_db; 141 $query = $db->getQuery(true); 142 $parentId = (int) $parentId; 143 144 // Get all children of this node 145 $query->clear() 146 ->select($db->quoteName('id')) 147 ->from($db->quoteName($this->_tbl)) 148 ->where($db->quoteName('parent_id') . ' = :parentid') 149 ->bind(':parentid', $parentId, ParameterType::INTEGER) 150 ->order([$db->quoteName('parent_id'), $db->quoteName('title')]); 151 152 $db->setQuery($query); 153 $children = $db->loadColumn(); 154 155 // The right value of this node is the left value + 1 156 $right = $left + 1; 157 158 // Execute this function recursively over all children 159 for ($i = 0, $n = \count($children); $i < $n; $i++) { 160 // $right is the current right value, which is incremented on recursion return 161 $right = $this->rebuild($children[$i], $right); 162 163 // If there is an update failure, return false to break out of the recursion 164 if ($right === false) { 165 return false; 166 } 167 } 168 169 $left = (int) $left; 170 $right = (int) $right; 171 172 // We've got the left value, and now that we've processed 173 // the children of this node we also know the right value 174 $query->clear() 175 ->update($db->quoteName($this->_tbl)) 176 ->set($db->quoteName('lft') . ' = :lft') 177 ->set($db->quoteName('rgt') . ' = :rgt') 178 ->where($db->quoteName('id') . ' = :id') 179 ->bind(':lft', $left, ParameterType::INTEGER) 180 ->bind(':rgt', $right, ParameterType::INTEGER) 181 ->bind(':id', $parentId, ParameterType::INTEGER); 182 $db->setQuery($query); 183 184 // If there is an update failure, return false to break out of the recursion 185 try { 186 $db->execute(); 187 } catch (ExecutionFailureException $e) { 188 return false; 189 } 190 191 // Return the right value of this node + 1 192 return $right + 1; 193 } 194 195 /** 196 * Inserts a new row if id is zero or updates an existing row in the database table 197 * 198 * @param boolean $updateNulls If false, null object variables are not updated 199 * 200 * @return boolean True if successful, false otherwise and an internal error message is set 201 * 202 * @since 1.7.0 203 */ 204 public function store($updateNulls = false) 205 { 206 if ($result = parent::store($updateNulls)) { 207 // Rebuild the nested set tree. 208 $this->rebuild(); 209 } 210 211 return $result; 212 } 213 214 /** 215 * Delete this object and its dependencies 216 * 217 * @param integer $oid The primary key of the user group to delete. 218 * 219 * @return mixed Boolean or Exception. 220 * 221 * @since 1.7.0 222 * @throws \RuntimeException on database error. 223 * @throws \UnexpectedValueException on data error. 224 */ 225 public function delete($oid = null) 226 { 227 if ($oid) { 228 $this->load($oid); 229 } 230 231 if ($this->id == 0) { 232 throw new \UnexpectedValueException('Usergroup not found'); 233 } 234 235 if ($this->parent_id == 0) { 236 throw new \UnexpectedValueException('Root usergroup cannot be deleted.'); 237 } 238 239 if ($this->lft == 0 || $this->rgt == 0) { 240 throw new \UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.'); 241 } 242 243 $db = $this->_db; 244 245 $lft = (int) $this->lft; 246 $rgt = (int) $this->rgt; 247 248 // Select the usergroup ID and its children 249 $query = $db->getQuery(true) 250 ->select($db->quoteName('c.id')) 251 ->from($db->quoteName($this->_tbl, 'c')) 252 ->where($db->quoteName('c.lft') . ' >= :lft') 253 ->where($db->quoteName('c.rgt') . ' <= :rgt') 254 ->bind(':lft', $lft, ParameterType::INTEGER) 255 ->bind(':rgt', $rgt, ParameterType::INTEGER); 256 $db->setQuery($query); 257 $ids = $db->loadColumn(); 258 259 if (empty($ids)) { 260 throw new \UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.'); 261 } 262 263 // Delete the usergroup and its children 264 $query->clear() 265 ->delete($db->quoteName($this->_tbl)) 266 ->whereIn($db->quoteName('id'), $ids); 267 $db->setQuery($query); 268 $db->execute(); 269 270 // Rebuild the nested set tree. 271 $this->rebuild(); 272 273 // Delete the usergroup in view levels 274 $replace = array(); 275 276 foreach ($ids as $id) { 277 $replace[] = ',' . $db->quote("[$id,") . ',' . $db->quote('['); 278 $replace[] = ',' . $db->quote(",$id,") . ',' . $db->quote(','); 279 $replace[] = ',' . $db->quote(",$id]") . ',' . $db->quote(']'); 280 $replace[] = ',' . $db->quote("[$id]") . ',' . $db->quote('[]'); 281 } 282 283 $query->clear() 284 ->select( 285 [ 286 $db->quoteName('id'), 287 $db->quoteName('rules'), 288 ] 289 ) 290 ->from($db->quoteName('#__viewlevels')); 291 $db->setQuery($query); 292 $rules = $db->loadObjectList(); 293 294 $matchIds = []; 295 296 foreach ($rules as $rule) { 297 foreach ($ids as $id) { 298 if (strstr($rule->rules, '[' . $id) || strstr($rule->rules, ',' . $id) || strstr($rule->rules, $id . ']')) { 299 $matchIds[] = $rule->id; 300 } 301 } 302 } 303 304 if (!empty($matchIds)) { 305 $query->clear() 306 ->update($db->quoteName('#__viewlevels')) 307 ->set($db->quoteName('rules') . ' = ' . str_repeat('REPLACE(', 4 * \count($ids)) . $db->quoteName('rules') . implode(')', $replace) . ')') 308 ->whereIn($db->quoteName('id'), $matchIds); 309 $db->setQuery($query); 310 $db->execute(); 311 } 312 313 // Delete the user to usergroup mappings for the group(s) from the database. 314 $query->clear() 315 ->delete($db->quoteName('#__user_usergroup_map')) 316 ->whereIn($db->quoteName('group_id'), $ids); 317 $db->setQuery($query); 318 $db->execute(); 319 320 return true; 321 } 322 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |