Back to Usergroup class

Method delete

public mixed
delete
(mixed $oid = null)
Delete this object and its dependencies
Parameters
  • int $oid The primary key of the user group to delete.
Returns
  • mixed Boolean or Exception.
Since
  • 1.7.0
-
  • \RuntimeException on database error.
  • \UnexpectedValueException on data error.
Class: Usergroup
Project: Joomla

Method delete - Source code

/**
 * Delete this object and its dependencies
 *
 * @param   integer  $oid  The primary key of the user group to delete.
 *
 * @return  mixed  Boolean or Exception.
 *
 * @since   1.7.0
 * @throws  \RuntimeException on database error.
 * @throws  \UnexpectedValueException on data error.
 */
public function delete($oid = null)
{
    if ($oid) {
        $this->load($oid);
    }
    if ($this->id == 0) {
        throw new \UnexpectedValueException('Usergroup not found');
    }
    if ($this->parent_id == 0) {
        throw new \UnexpectedValueException('Root usergroup cannot be deleted.');
    }
    if ($this->lft == 0 || $this->rgt == 0) {
        throw new \UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
    }
    $db = $this->_db;
    $lft = (int) $this->lft;
    $rgt = (int) $this->rgt;
    // Select the usergroup ID and its children
    $query = $db->getQuery(true)->select($db->quoteName('c.id'))->from($db->quoteName($this->_tbl, 'c'))->where($db->quoteName('c.lft') . ' >= :lft')->where($db->quoteName('c.rgt') . ' <= :rgt')->bind(':lft', $lft, ParameterType::INTEGER)->bind(':rgt', $rgt, ParameterType::INTEGER);
    $db->setQuery($query);
    $ids = $db->loadColumn();
    if (empty($ids)) {
        throw new \UnexpectedValueException('Left-Right data inconsistency. Cannot delete usergroup.');
    }
    // Delete the usergroup and its children
    $query->clear()->delete($db->quoteName($this->_tbl))->whereIn($db->quoteName('id'), $ids);
    $db->setQuery($query);
    $db->execute();
    // Rebuild the nested set tree.
    $this->rebuild();
    // Delete the usergroup in view levels
    $replace = array();
    foreach ($ids as $id) {
        $replace[] = ',' . $db->quote("[{$id},") . ',' . $db->quote('[');
        $replace[] = ',' . $db->quote(",{$id},") . ',' . $db->quote(',');
        $replace[] = ',' . $db->quote(",{$id}]") . ',' . $db->quote(']');
        $replace[] = ',' . $db->quote("[{$id}]") . ',' . $db->quote('[]');
    }
    $query->clear()->select([$db->quoteName('id'), $db->quoteName('rules')])->from($db->quoteName('#__viewlevels'));
    $db->setQuery($query);
    $rules = $db->loadObjectList();
    $matchIds = [];
    foreach ($rules as $rule) {
        foreach ($ids as $id) {
            if (strstr($rule->rules, '[' . $id) || strstr($rule->rules, ',' . $id) || strstr($rule->rules, $id . ']')) {
                $matchIds[] = $rule->id;
            }
        }
    }
    if (!empty($matchIds)) {
        $query->clear()->update($db->quoteName('#__viewlevels'))->set($db->quoteName('rules') . ' = ' . str_repeat('REPLACE(', 4 * \count($ids)) . $db->quoteName('rules') . implode(')', $replace) . ')')->whereIn($db->quoteName('id'), $matchIds);
        $db->setQuery($query);
        $db->execute();
    }
    // Delete the user to usergroup mappings for the group(s) from the database.
    $query->clear()->delete($db->quoteName('#__user_usergroup_map'))->whereIn($db->quoteName('group_id'), $ids);
    $db->setQuery($query);
    $db->execute();
    return true;
}