[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/database/table/ -> usergroup.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Database
   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
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.database.table');
  13  
  14  /**
  15   * Usergroup table class.
  16   *
  17   * @package     Joomla.Platform
  18   * @subpackage  Database
  19   * @since       11.1
  20   */
  21  class JTableUsergroup extends JTable
  22  {
  23      /**
  24       * Constructor
  25       *
  26       * @param   JDatabase  &$db  A database connector object
  27       *
  28       * @since   11.1
  29       */
  30  	public function __construct(&$db)
  31      {
  32          parent::__construct('#__usergroups', 'id', $db);
  33      }
  34  
  35      /**
  36       * Method to check the current record to save
  37       *
  38       * @return  boolean  True on success
  39       *
  40       * @since   11.1
  41       */
  42  	public function check()
  43      {
  44          // Validate the title.
  45          if ((trim($this->title)) == '')
  46          {
  47              $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE'));
  48              return false;
  49          }
  50  
  51          // Check for a duplicate parent_id, title.
  52          // There is a unique index on the (parent_id, title) field in the table.
  53          $db = $this->_db;
  54          $query = $db->getQuery(true)
  55              ->select('COUNT(title)')
  56              ->from($this->_tbl)
  57              ->where('title = ' . $db->quote(trim($this->title)))
  58              ->where('parent_id = ' . (int) $this->parent_id)
  59              ->where('id <> ' . (int) $this->id);
  60          $db->setQuery($query);
  61  
  62          if ($db->loadResult() > 0)
  63          {
  64              $this->setError(JText::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE_EXISTS'));
  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          // if there is an update failure, return false to break out of the recursion
 110          if (!$db->query())
 111          {
 112              return false;
 113          }
 114  
 115          // return the right value of this node + 1
 116          return $right + 1;
 117      }
 118  
 119      /**
 120       * Inserts a new row if id is zero or updates an existing row in the database table
 121       *
 122       * @param   boolean  $updateNulls  If false, null object variables are not updated
 123       *
 124       * @return  boolean  True if successful, false otherwise and an internal error message is set
 125       *
 126       * @since   11.1
 127       */
 128  	public function store($updateNulls = false)
 129      {
 130          if ($result = parent::store($updateNulls))
 131          {
 132              // Rebuild the nested set tree.
 133              $this->rebuild();
 134          }
 135  
 136          return $result;
 137      }
 138  
 139      /**
 140       * Delete this object and its dependencies
 141       *
 142       * @param   integer  $oid  The primary key of the user group to delete.
 143       *
 144       * @return  mixed  Boolean or Exception.
 145       *
 146       * @since   11.1
 147       */
 148  	public function delete($oid = null)
 149      {
 150          if ($oid)
 151          {
 152              $this->load($oid);
 153          }
 154          if ($this->id == 0)
 155          {
 156              return new JException(JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
 157          }
 158          if ($this->parent_id == 0)
 159          {
 160              return new JException(JText::_('JLIB_DATABASE_ERROR_DELETE_ROOT_CATEGORIES'));
 161          }
 162          if ($this->lft == 0 or $this->rgt == 0)
 163          {
 164              return new JException(JText::_('JLIB_DATABASE_ERROR_DELETE_CATEGORY'));
 165          }
 166  
 167          $db = $this->_db;
 168  
 169          // Select the category ID and it's children
 170          $query = $db->getQuery(true);
 171          $query->select($db->quoteName('c') . '.' . $db->quoteName('id'));
 172          $query->from($db->quoteName($this->_tbl) . 'AS c');
 173          $query->where($db->quoteName('c') . '.' . $db->quoteName('lft') . ' >= ' . (int) $this->lft);
 174          $query->where($db->quoteName('c') . '.' . $db->quoteName('rgt') . ' <= ' . (int) $this->rgt);
 175          $db->setQuery($query);
 176          $ids = $db->loadColumn();
 177          if (empty($ids))
 178          {
 179              return new JException(JText::_('JLIB_DATABASE_ERROR_DELETE_CATEGORY'));
 180          }
 181  
 182          // Delete the category dependencies
 183          // @todo Remove all related threads, posts and subscriptions
 184  
 185          // Delete the category and its children
 186          $query->clear();
 187          $query->delete();
 188          $query->from($db->quoteName($this->_tbl));
 189          $query->where($db->quoteName('id') . ' IN (' . implode(',', $ids) . ')');
 190          $db->setQuery($query);
 191          if (!$db->query())
 192          {
 193              $this->setError($db->getErrorMsg());
 194              return false;
 195          }
 196  
 197          // Delete the usergroup in view levels
 198          $replace = array();
 199          foreach ($ids as $id)
 200          {
 201              $replace[] = ',' . $db->quote("[$id,") . ',' . $db->quote("[") . ')';
 202              $replace[] = ',' . $db->quote(",$id,") . ',' . $db->quote(",") . ')';
 203              $replace[] = ',' . $db->quote(",$id]") . ',' . $db->quote("]") . ')';
 204              $replace[] = ',' . $db->quote("[$id]") . ',' . $db->quote("[]") . ')';
 205          }
 206  
 207          $query->clear();
 208          //sqlsrv change. Alternative for regexp
 209          $query->select('id, rules');
 210          $query->from('#__viewlevels');
 211          $db->setQuery($query);
 212          $rules = $db->loadObjectList();
 213  
 214          $match_ids = array();
 215          foreach ($rules as $rule)
 216          {
 217              foreach ($ids as $id)
 218              {
 219                  if (strstr($rule->rules, '[' . $id) || strstr($rule->rules, ',' . $id) || strstr($rule->rules, $id . ']'))
 220                  {
 221                      $match_ids[] = $rule->id;
 222                  }
 223              }
 224          }
 225  
 226          if (!empty($match_ids))
 227          {
 228              $query = $db->getQuery(true);
 229              $query->set('rules=' . str_repeat('replace(', 4 * count($ids)) . 'rules' . implode('', $replace));
 230              $query->update('#__viewlevels');
 231              $query->where('id IN (' . implode(',', $match_ids) . ')');
 232              $db->setQuery($query);
 233              if (!$db->query())
 234              {
 235                  $this->setError($db->getErrorMsg());
 236                  return false;
 237              }
 238          }
 239  
 240          // Delete the user to usergroup mappings for the group(s) from the database.
 241          $query->clear();
 242          $query->delete();
 243          $query->from($db->quoteName('#__user_usergroup_map'));
 244          $query->where($db->quoteName('group_id') . ' IN (' . implode(',', $ids) . ')');
 245          $db->setQuery($query);
 246          $db->query();
 247  
 248          // Check for a database error.
 249          if ($db->getErrorNum())
 250          {
 251              $this->setError($db->getErrorMsg());
 252              return false;
 253          }
 254  
 255          return true;
 256      }
 257  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1