cakephp2-php8/cake/libs/controller/components/dbacl/models/aclnode.php
phpnut 32f9e1e0e5 Merging fixes into trunk.
Revision: [2522]
renamed Bake class file

Revision: [2521]
removing old bake scripts

Revision: [2520]
Adding new Bake script

Revision: [2519]
Adding fix for Ticket #622

Revision: [2518]
Adding fix for Ticket #535

Revision: [2517]
Adding fix for Ticket #648, associated records are now post-processed by afterFind() method in associated model

Revision: [2516]
Adding fix for Ticket #618

Revision: [2515]
More error page fixes

Revision: [2514]
Adding fix for Ticket #690

Revision: [2513]
Removing empty method

Revision: [2512]
Adding fix for Ticket #675

Revision: [2511]
More fixes for Ticket #670

Revision: [2510]
Adding fix for Ticket #670, and removing pointless part of Helper::output(), since print() *always* returns 1.

Revision: [2509]
Adding fixes for Tickets #677 and #681

Revision: [2508]
Added patch from Ticket #632

Revision: [2502]
Adding fix to prevent model ID from getting overwritten when saving HABTM data [TEST ME] - thanks sdevore

Revision: [2501]
Rewriting foreach loops in queryAssociation()

Revision: [2493]
Adding fix for Ticket #673

Revision: [2492]
Adding reconnect() method, to reconnect to database with different settings

Revision: [2491]
Adding Microsoft SQL Server driver [EXPERIMENTAL]

Revision: [2490]
Fixing a bug that occurs when connecting to two different databases on the same server, and enabling cross-database model associations

Revision: [2488]
Adding fix for Ticket #651

Revision: [2487]
Adding fix for Ticket #665

Revision: [2486]
Adding fix for Ticket #655

Revision: [2485]
Forcing DboMysql to validate disconnect, and adding fix for Ticket #663

git-svn-id: https://svn.cakephp.org/repo/trunk/cake@2528 3807eeeb-6ff5-0310-8944-8be069107fe0
2006-04-24 16:24:57 +00:00

336 lines
No EOL
8.8 KiB
PHP

<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
* Copyright (c) 2006, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
* @package cake
* @subpackage cake.cake.libs.controller.components.dbacl.models
* @since CakePHP v 0.10.0.1232
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Short description for file.
*
* Long description for file
*
* @package cake
* @subpackage cake.cake.libs.controller.components.dbacl.models
* @since CakePHP v 0.10.0.1232
*
*/
class AclNode extends AppModel
{
var $cacheQueries = false;
/**
* Enter description here...
*
*/
function __construct()
{
$this->setSource();
parent::__construct();
}
/**
* Enter description here...
*
* @param unknown_type $link_id
* @param unknown_type $parent_id
* @param unknown_type $alias
* @return unknown
*/
function create($link_id = 0, $parent_id = null, $alias = '')
{
parent::create();
if (strtolower(get_class($this)) == "aclnode")
{
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);
return null;
}
extract($this->__dataVars());
if($parent_id == null || $parent_id === 0)
{
$parent = $this->find(null, "MAX(rght)", null, null, 0);
$parent['lft'] = $parent[0]['MAX(rght)'];
if($parent[0]['MAX(rght)'] == null)
{
// The tree is empty
$parent['lft'] = 0;
}
}
else
{
$parent = $this->find($this->_resolveID($parent_id), null, null, 0);
if($parent == null || count($parent) == 0)
{
trigger_error("Null parent in {$class}::create()", E_USER_WARNING);
return null;
}
$parent = $parent[$class];
$this->_syncTable($table_name, 1, $parent['lft'], $parent['lft']);
}
$return = $this->save(array($class => array(
$secondary_id => $link_id,
'alias' => $alias,
'lft' => $parent['lft'] + 1,
'rght' => $parent['lft'] + 2
)));
$this->setId($this->getLastInsertID());
return $return;
}
/**
* Enter description here...
*
* @param unknown_type $parent_id
* @param unknown_type $id
* @return unknown
*/
function setParent($parent_id = null, $id = null)
{
if (strtolower(get_class($this)) == "aclnode")
{
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);
return null;
}
extract($this->__dataVars());
if($id == null && $this->id == false)
{
return false;
}
else if($id == null)
{
$id = $this->id;
}
$object = $this->find($this->_resolveID($id), null, null, 0);
if($object == null || count($object) == 0)
{
// Couldn't find object
return false;
}
$object = $object[$class];
$parent = $this->getParent($id);
// Node is already at root, or new parent == old parent
if(($parent == null && $parent_id == null) || ($parent_id == $parent[$class][$secondary_id] && $parent_id != null) || ($parent_id == $parent[$class]['alias'] && $parent_id != null))
{
return false;
}
if($parent_id == null)
{
$newParent = $this->find(null, "MAX(rght) as lft", null, 0);
$newParent = $newParent[0];
$newParent['rght'] = $newParent['lft'];
}
else
{
$newParent = $this->find($this->_resolveID($parent_id), null, null, 0);
$newParent = $newParent[$class];
}
if($parent_id != null && $newParent['lft'] <= $object['lft'] && $newParent['rght'] >= $object['rght'])
{
// Can't move object inside self or own child
return false;
}
$this->_syncTable($table_name, 0, $object['lft'], $object['lft']);
if ($object['lft'] < $newParent['lft'])
{
$newParent['lft'] = $newParent['lft'] - 2;
$newParent['rght'] = $newParent['rght'] - 2;
}
if ($parent_id != null)
{
$this->_syncTable($table_name, 1, $newParent['lft'], $newParent['lft']);
}
$object['lft'] = $newParent['lft'] + 1;
$object['rght'] = $newParent['lft'] + 2;
$this->save(array($class => $object));
if($newParent['lft'] == 0)
{
$this->_syncTable($table_name, 2, $newParent['lft'], $newParent['lft']);
}
return true;
}
/**
* Get the parent node of the given Aro or Aco
*
* @param moxed $id
* @return array
*/
function getParent($id)
{
$path = $this->getPath($id);
if($path == null || count($path) < 2)
{
return null;
}
else
{
return $path[count($path) - 2];
}
}
/**
* Gets the path to the given Aro or Aco
*
* @param mixed $id
* @return array
*/
function getPath($id)
{
if (strtolower(get_class($this)) == "aclnode")
{
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);
return null;
}
extract($this->__dataVars());
$item = $this->find($this->_resolveID($id), null, null, 0);
if($item == null || count($item) == 0)
{
return null;
}
return $this->findAll(array($class.'.lft' => '<= '.$item[$class]['lft'], $class.'.rght' => '>= '.$item[$class]['rght']), null, array($class.'.lft' => 'ASC'), null, null, 0);
}
/**
* Get the child nodes of the given Aro or Aco
*
* @param mixed $id
* @return array
*/
function getChildren($id)
{
if (strtolower(get_class($this)) == "aclnode")
{
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);
return null;
}
extract($this->__dataVars());
$item = $this->find($this->_resolveID($id), null, null, 0);
return $this->findAll(array($class.'.lft' => '> '.$item[$class]['lft'], $class.'.rght' => '< '.$item[$class]['rght']), null, null, null, null, null, 0);
}
/**
* Gets a conditions array to find an Aro or Aco, based on the given id or alias
*
* @param mixed $id
* @return array Conditions array for a find/findAll call
*/
function _resolveID($id)
{
extract($this->__dataVars());
$key = (is_string($id) ? 'alias' : $secondary_id);
return array($this->name.'.'.$key => $id);
}
/**
* Shifts the left and right values of the aro/aco tables
* when a node is added or removed
*
* @param unknown_type $table
* @param unknown_type $dir
* @param unknown_type $lft
* @param unknown_type $rght
*/
function _syncTable($table, $dir, $lft, $rght)
{
$db =& ConnectionManager::getDataSource($this->useDbConfig);
if ($dir == 2)
{
$shift = 1;
$dir = '+';
}
else
{
$shift = 2;
if ($dir > 0)
{
$dir = '+';
}
else
{
$dir = '-';
}
}
$db->query('UPDATE '.$table.' SET rght = rght '.$dir.' '.$shift.' WHERE rght > '.$rght);
$db->query('UPDATE '.$table.' SET lft = lft '.$dir.' '.$shift.' WHERE lft > '.$lft);
}
/**
* Enter description here...
*
* @return unknown
*/
function __dataVars()
{
$vars = array();
$class = strtolower(get_class($this));
if ($class == 'aro')
{
$vars['secondary_id'] = 'user_id';
}
else
{
$vars['secondary_id'] = 'object_id';
}
$vars['table_name'] = $class . 's';
$vars['class'] = ucwords($class);
return $vars;
}
/**
* Enter description here...
*
*/
function setSource()
{
$this->table = strtolower(get_class($this)) . "s";
}
}
?>