[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/controller/components/dbacl/models/ -> aclnode.php (source)

   1  <?php
   2  /* SVN FILE: $Id: aclnode.php 4450 2007-02-05 05:18:05Z phpnut $ */
   3  /**
   4   * Short description for file.
   5   *
   6   * Long description for file
   7   *
   8   * PHP versions 4 and 5
   9   *
  10   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
  11   * Copyright 2005-2007, Cake Software Foundation, Inc.
  12   *                                1785 E. Sahara Avenue, Suite 490-204
  13   *                                Las Vegas, Nevada 89104
  14   *
  15   * Licensed under The MIT License
  16   * Redistributions of files must retain the above copyright notice.
  17   *
  18   * @filesource
  19   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  20   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21   * @package            cake
  22   * @subpackage        cake.cake.libs.controller.components.dbacl.models
  23   * @since            CakePHP(tm) v 0.10.0.1232
  24   * @version            $Revision: 4450 $
  25   * @modifiedby        $LastChangedBy: phpnut $
  26   * @lastmodified    $Date: 2007-02-04 23:18:05 -0600 (Sun, 04 Feb 2007) $
  27   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  28   */
  29  /**
  30   * Load AppModel class
  31   */
  32  loadModel();
  33  /**
  34   * Short description for file.
  35   *
  36   * Long description for file
  37   *
  38   * @package        cake
  39   * @subpackage    cake.cake.libs.controller.components.dbacl.models
  40   *
  41   */
  42  class AclNode extends AppModel {
  43  /**
  44   * Database configuration to use
  45   *
  46   * @var string
  47   */
  48      var $useDbConfig = ACL_DATABASE;
  49  /**
  50   * Cache Queries
  51   *
  52   * @var boolean
  53   */
  54      var $cacheQueries = false;
  55  /**
  56   * Creates a new ARO/ACO node
  57   *
  58   * @param int $linkId
  59   * @param mixed $parentId
  60   * @param string $alias
  61   * @return boolean True on success, false on fail
  62   * @access public
  63   */
  64  	function create($linkId = 0, $parentId = null, $alias = '') {
  65          parent::create();
  66          if (strtolower(get_class($this)) == "aclnode") {
  67              trigger_error("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", E_USER_ERROR);
  68              return null;
  69          }
  70          extract ($this->__dataVars());
  71  
  72          if ($parentId == null || $parentId === 0) {
  73              $parent = $this->find(null, 'MAX(rght) as rght', null, -1);
  74              $parent['lft'] = $parent[0]['rght'];
  75  
  76              if ($parent[0]['rght'] == null || !$parent[0]['rght']) {
  77                  $parent['lft'] = 0;
  78              }
  79          } else {
  80              $parent = $this->find($this->_resolveID($parentId), null, null, 0);
  81              if ($parent == null || count($parent) == 0) {
  82                  trigger_error("Null parent in {$class}::create()", E_USER_WARNING);
  83                  return null;
  84              }
  85              $parent = $parent[$class];
  86              $this->_syncTable(1, $parent['lft'], $parent['lft']);
  87          }
  88          $return = $this->save(array($class => array($secondary_id => $linkId,
  89                                                                      'alias' => $alias,
  90                                                                      'lft' => $parent['lft'] + 1,
  91                                                                      'rght' => $parent['lft'] + 2)));
  92          $this->id  = $this->getLastInsertID();
  93          return $return;
  94      }
  95  /**
  96   * Deletes the ARO/ACO node with the given ID
  97   *
  98   * @param mixed $id    The id or alias of an ARO/ACO node
  99   * @return boolean True on success, false on fail
 100   * @access public
 101   */
 102  	function delete($id) {
 103          extract ($this->__dataVars());
 104          $db =& ConnectionManager::getDataSource($this->useDbConfig);
 105  
 106          $result = $this->find($this->_resolveID($id));
 107          $object = $result[$class];
 108          if ($object == null || count($object) == 0) {
 109              return false;
 110          }
 111  
 112          $children = $this->findAll(array("{$class}.rght" => "< {$result[$class]['rght']}", "{$class}.lft" => "> {$result[$class]['lft']}"), 'id', null, null, null, -1);
 113          $idList = DataSource::getFieldValue($children, '{n}.' . $class . '.id');
 114          $idList[] = $result[$class]['id'];
 115  
 116          $this->ArosAco->query('DELETE FROM ' . $db->fullTableName($this->ArosAco) . " WHERE {$class}_id in (" . implode(', ', $idList) . ')');
 117  
 118          $table = $db->fullTableName($this);
 119          $this->query("DELETE FROM {$table} WHERE {$table}.lft >= {$result[$class]['lft']} AND {$table}.rght <= {$result[$class]['rght']}");
 120  
 121          $shift = 1 + $result[$class]['rght'] - $result[$class]['lft'];
 122          $this->query('UPDATE ' . $table . ' SET ' . $db->name('rght') . ' = ' . $db->name('rght') . ' - ' . $shift . ' WHERE ' . $db->name('rght') . ' > ' . $result[$class]['rght']);
 123          $this->query('UPDATE ' . $table . ' SET ' . $db->name('lft') . ' = ' . $db->name('lft') . ' - ' . $shift . ' WHERE ' . $db->name('lft') . ' > ' . $result[$class]['lft']);
 124          return true;
 125      }
 126  /**
 127   * Sets the parent of the given node
 128   *
 129   * @param mixed $parentId
 130   * @param mixed $id
 131   * @return boolean True on success, false on failure
 132   * @access public
 133   */
 134  	function setParent($parentId = null, $id = null) {
 135          if (strtolower(get_class($this)) == "aclnode") {
 136              trigger_error("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", E_USER_ERROR);
 137              return null;
 138          }
 139          extract ($this->__dataVars());
 140  
 141          if ($id == null && $this->id == false) {
 142              return false;
 143          } elseif ($id == null) {
 144              $id = $this->id;
 145          }
 146          $object = $this->find($this->_resolveID($id), null, null, 0);
 147  
 148          if ($object == null || count($object) == 0) {
 149              return false;
 150          }
 151          $object = $object[$class];
 152          $parent = $this->getParent($id);
 153  
 154          if (($parent == null && $parentId == null) || ($parentId == $parent[$class][$secondary_id] && $parentId != null) || ($parentId == $parent[$class]['alias'] && $parentId != null)) {
 155              return false;
 156          }
 157  
 158          if ($parentId == null) {
 159              $newParent = $this->find(null, 'MAX(rght) as lft', null, -1);
 160              $newParent = $newParent[0];
 161              $newParent['rght'] = $newParent['lft'];
 162          } else {
 163              $newParent = $this->find($this->_resolveID($parentId), null, null, 0);
 164              $newParent = $newParent[$class];
 165          }
 166  
 167          if ($parentId != null && $newParent['lft'] <= $object['lft'] && $newParent['rght'] >= $object['rght']) {
 168              return false;
 169          }
 170          $this->_syncTable(0, $object['lft'], $object['lft']);
 171  
 172          if ($object['lft'] < $newParent['lft']) {
 173              $newParent['lft'] = $newParent['lft'] - 2;
 174              $newParent['rght'] = $newParent['rght'] - 2;
 175          }
 176  
 177          if ($parentId != null) {
 178              $this->_syncTable(1, $newParent['lft'], $newParent['lft']);
 179          }
 180          $object['lft'] = $newParent['lft'] + 1;
 181          $object['rght'] = $newParent['lft'] + 2;
 182          $this->save(array($class => $object));
 183  
 184          if ($newParent['lft'] == 0) {
 185              $this->_syncTable(2, $newParent['lft'], $newParent['lft']);
 186          }
 187          return true;
 188      }
 189  /**
 190   * Get the parent node of the given Aro or Aco
 191   *
 192   * @param moxed $id
 193   * @return array
 194   * @access public
 195   */
 196  	function getParent($id) {
 197          $path = $this->getPath($id);
 198          if ($path == null || count($path) < 2) {
 199              return null;
 200          } else {
 201              return $path[count($path) - 2];
 202          }
 203      }
 204  /**
 205   * Gets the path to the given Aro or Aco
 206   *
 207   * @param mixed $id
 208   * @return array
 209   * @access public
 210   */
 211  	function getPath($id) {
 212          if (strtolower(get_class($this)) == "aclnode") {
 213              trigger_error("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", E_USER_ERROR);
 214              return null;
 215          }
 216          extract ($this->__dataVars());
 217          $item = $this->find($this->_resolveID($id), null, null, 0);
 218  
 219          if ($item == null || count($item) == 0) {
 220              return null;
 221          }
 222          return $this->findAll(array($class . '.lft' => '<= ' . $item[$class]['lft'], $class . '.rght' => '>= ' . $item[$class]['rght']), null, array($class . '.lft' => 'ASC'), null, null, 0);
 223      }
 224  /**
 225   * Get the child nodes of the given Aro or Aco
 226   *
 227   * @param mixed $id
 228   * @return array
 229   * @access public
 230   */
 231  	function getChildren($id) {
 232          if (strtolower(get_class($this)) == "aclnode") {
 233              trigger_error("[acl_base] The AclBase class constructor has been called, or the class was instantiated. This class must remain abstract. Please refer to the Cake docs for ACL configuration.", E_USER_ERROR);
 234              return null;
 235          }
 236  
 237          extract ($this->__dataVars());
 238          $item = $this->find($this->_resolveID($id), null, null, 0);
 239          return $this->findAll(array($class . '.lft' => '> ' . $item[$class]['lft'], $class . '.rght' => '< ' . $item[$class]['rght']), null, null, null, null, null, 0);
 240      }
 241  /**
 242   * Gets a conditions array to find an Aro or Aco, based on the given id or alias
 243   *
 244   * @param mixed $id
 245   * @return array Conditions array for a find/findAll call
 246   * @access public
 247   */
 248  	function _resolveID($id) {
 249          extract($this->__dataVars());
 250          $key = (is_numeric($id) ? $secondary_id : 'alias');
 251          return array($this->name . '.' . $key => $id);
 252      }
 253  /**
 254   * Shifts the left and right values of the aro/aco tables
 255   * when a node is added or removed
 256   *
 257   * @param unknown_type $dir
 258   * @param unknown_type $lft
 259   * @param unknown_type $rght
 260   * @access protected
 261   */
 262  	function _syncTable($dir, $lft, $rght) {
 263          $db =& ConnectionManager::getDataSource($this->useDbConfig);
 264  
 265          if ($dir == 2) {
 266              $shift = 1;
 267              $dir = '+';
 268          } else {
 269              $shift = 2;
 270  
 271              if ($dir > 0) {
 272                  $dir = '+';
 273              } else {
 274                  $dir = '-';
 275              }
 276          }
 277          $db->query('UPDATE ' . $db->fullTableName($this) . ' SET ' . $db->name('rght') . ' = ' . $db->name('rght') . ' ' . $dir . ' ' . $shift . ' WHERE ' . $db->name('rght') . ' > ' . $rght);
 278          $db->query('UPDATE ' . $db->fullTableName($this) . ' SET ' . $db->name('lft') . ' = ' . $db->name('lft') . '  ' . $dir . ' ' . $shift . ' WHERE ' . $db->name('lft') . '  > ' . $lft);
 279      }
 280  /**
 281   * Infers data based on the currently-intantiated subclass.
 282   *
 283   * @return array
 284   * @access private
 285   */
 286  	function __dataVars() {
 287          $vars = array();
 288          $class = strtolower(get_class($this));
 289          if ($class == 'aro') {
 290              $vars['secondary_id'] = 'foreign_key';
 291          } else {
 292              $vars['secondary_id'] = 'object_id';
 293          }
 294          $vars['class'] = ucwords($class);
 295          return $vars;
 296      }
 297  }
 298  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7