2005-11-05 04:08:14 +00:00
< ? php
2005-12-22 01:07:28 +00:00
/* SVN FILE: $Id$ */
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Short description for file .
2006-01-12 02:10:47 +00:00
*
2005-12-22 01:07:28 +00:00
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP : Rapid Development Framework < http :// www . cakephp . org />
2006-01-20 07:46:14 +00:00
* Copyright ( c ) 2006 , Cake Software Foundation , Inc .
2005-12-23 21:57:26 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2006-01-12 02:10:47 +00:00
*
2005-12-22 01:07:28 +00:00
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
2006-01-12 02:10:47 +00:00
* @ filesource
2006-01-20 07:46:14 +00:00
* @ copyright Copyright ( c ) 2006 , Cake Software Foundation , Inc .
2005-12-23 21:57:26 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP Project
2005-12-22 01:07:28 +00:00
* @ 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
*/
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Short description for file .
2006-01-12 02:10:47 +00:00
*
2005-12-22 01:07:28 +00:00
* Long description for file
*
* @ package cake
* @ subpackage cake . cake . libs . controller . components . dbacl . models
* @ since CakePHP v 0.10 . 0.1232
*
*/
2005-11-05 04:08:14 +00:00
class AclNode extends AppModel
2006-01-12 02:10:47 +00:00
{
2005-11-05 04:08:14 +00:00
2006-03-19 03:26:43 +00:00
var $cacheQueries = false ;
2005-12-22 01:07:28 +00:00
/**
* Enter description here ...
*
*/
2006-02-18 23:42:21 +00:00
function __construct ()
{
2006-01-18 05:35:14 +00:00
$this -> setSource ();
2005-11-05 04:08:14 +00:00
parent :: __construct ();
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Enter description here ...
*
2006-01-12 02:10:47 +00:00
* @ param unknown_type $link_id
* @ param unknown_type $parent_id
* @ param unknown_type $alias
2005-12-22 01:07:28 +00:00
* @ return unknown
*/
2006-02-18 23:42:21 +00:00
function create ( $link_id = 0 , $parent_id = null , $alias = '' )
{
2006-01-12 02:10:47 +00:00
parent :: create ();
2005-11-05 04:08:14 +00:00
if ( strtolower ( get_class ( $this )) == " aclnode " )
{
2006-01-12 02:10:47 +00:00
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 );
2006-03-28 02:44:55 +00:00
return null ;
2005-11-05 04:08:14 +00:00
}
extract ( $this -> __dataVars ());
if ( $parent_id == null || $parent_id === 0 )
{
$parent = $this -> find ( null , " MAX(rght) " );
$parent [ 'lft' ] = $parent [ 0 ][ 'MAX(rght)' ];
if ( $parent [ 0 ][ 'MAX(rght)' ] == null )
{
2006-02-18 23:42:21 +00:00
// The tree is empty
2005-11-05 04:08:14 +00:00
$parent [ 'lft' ] = 0 ;
}
}
else
{
2006-03-28 02:44:55 +00:00
$parent = $this -> find ( $this -> _resolveID ( $parent_id ));
2005-11-05 04:08:14 +00:00
if ( $parent == null || count ( $parent ) == 0 )
{
trigger_error ( " Null parent in { $class } ::create() " , E_USER_ERROR );
}
$parent = $parent [ $class ];
$this -> _syncTable ( $table_name , 1 , $parent [ 'lft' ], $parent [ 'lft' ]);
}
$return = $this -> save ( array ( $class => array (
$secondary_id => $link_id ,
2006-02-18 23:42:21 +00:00
'alias' => $alias ,
2005-11-05 04:08:14 +00:00
'lft' => $parent [ 'lft' ] + 1 ,
'rght' => $parent [ 'lft' ] + 2
)));
$this -> setId ( $this -> getLastInsertID ());
return $return ;
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Enter description here ...
*
* @ param unknown_type $parent_id
* @ param unknown_type $id
* @ return unknown
*/
2006-02-18 23:42:21 +00:00
function setParent ( $parent_id = null , $id = null )
{
2005-11-05 04:08:14 +00:00
if ( strtolower ( get_class ( $this )) == " aclnode " )
{
2006-01-12 02:10:47 +00:00
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 );
2005-11-05 04:08:14 +00:00
return null ;
}
extract ( $this -> __dataVars ());
if ( $id == null && $this -> id == false )
{
return false ;
}
else if ( $id == null )
{
$id = $this -> id ;
}
2006-03-28 02:44:55 +00:00
$object = $this -> find ( $this -> _resolveID ( $id ));
2005-11-05 04:08:14 +00:00
if ( $object == null || count ( $object ) == 0 )
{
2006-02-18 23:42:21 +00:00
// Couldn't find object
2005-11-05 04:08:14 +00:00
return false ;
}
$parent = $this -> getParent ( intval ( $object [ $class ][ $secondary_id ]));
2006-02-18 23:42:21 +00:00
// Node is already at root, or new parent == old parent
2005-11-05 04:08:14 +00:00
if (( $parent == null && $parent_id == null ) || ( $parent_id == $parent [ $class ][ $secondary_id ]) || ( $parent_id == $parent [ $class ][ 'alias' ]))
{
return false ;
}
if ( $parent_id != null && $parent [ $class ][ 'lft' ] <= $object [ $class ][ 'lft' ] && $parent [ $class ][ 'rght' ] >= $object [ $class ][ 'rght' ])
{
2006-02-18 23:42:21 +00:00
// Can't move object inside self or own child
2005-11-05 04:08:14 +00:00
return false ;
}
$this -> _syncTable ( $table_name , 0 , $object [ $class ][ 'lft' ], $object [ $class ][ 'lft' ]);
if ( $parent_id == null )
{
$parent = $this -> find ( null , " MAX(rght) " );
$parent [ 'lft' ] = $parent [ 0 ][ 'MAX(rght)' ];
}
else
{
2006-03-28 02:44:55 +00:00
$parent = $this -> find ( $this -> _resolveID ( $parent_id ));
2005-11-05 04:08:14 +00:00
$parent = $parent [ $class ];
$this -> _syncTable ( $table_name , 1 , $parent [ 'lft' ], $parent [ 'lft' ]);
}
$object [ $class ][ 'lft' ] = $parent [ 'lft' ] + 1 ;
$object [ $class ][ 'rght' ] = $parent [ 'lft' ] + 2 ;
$this -> save ( $object );
if ( $parent [ 'lft' ] == 0 )
{
$this -> _syncTable ( $table_name , 2 , $parent [ 'lft' ], $parent [ 'lft' ]);
}
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
2006-03-28 02:44:55 +00:00
* Get the parent node of the given Aro or Aco
2005-12-22 01:07:28 +00:00
*
2006-03-28 02:44:55 +00:00
* @ param moxed $id
* @ return array
2005-12-22 01:07:28 +00:00
*/
2006-02-18 23:42:21 +00:00
function getParent ( $id )
{
2005-11-05 04:08:14 +00:00
$path = $this -> getPath ( $id );
if ( $path == null || count ( $path ) < 2 )
{
return null ;
}
else
{
return $path [ count ( $path ) - 2 ];
}
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
2006-03-28 02:44:55 +00:00
* Gets the path to the given Aro or Aco
2005-12-22 01:07:28 +00:00
*
2006-03-28 02:44:55 +00:00
* @ param mixed $id
* @ return array
2005-12-22 01:07:28 +00:00
*/
2006-02-18 23:42:21 +00:00
function getPath ( $id )
{
2005-11-05 04:08:14 +00:00
if ( strtolower ( get_class ( $this )) == " aclnode " )
{
2006-01-12 02:10:47 +00:00
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 );
2006-03-28 02:44:55 +00:00
return null ;
2005-11-05 04:08:14 +00:00
}
extract ( $this -> __dataVars ());
2006-03-28 02:44:55 +00:00
$item = $this -> find ( $this -> _resolveID ( $id ));
2005-12-22 01:17:03 +00:00
if ( $item == null || count ( $item ) == 0 )
{
return null ;
}
2006-03-28 02:44:55 +00:00
return $this -> findAll ( array ( $class . '.lft' => '<= ' . $item [ $class ][ 'lft' ], $class . '.rght' => '>= ' . $item [ $class ][ 'rght' ]));
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
2006-03-28 02:44:55 +00:00
* Get the child nodes of the given Aro or Aco
2005-12-22 01:07:28 +00:00
*
2006-03-28 02:44:55 +00:00
* @ param mixed $id
* @ return array
2005-12-22 01:07:28 +00:00
*/
2006-02-18 23:42:21 +00:00
function getChildren ( $id )
{
2006-03-28 02:44:55 +00:00
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 ));
return $this -> findAll ( array ( $class . '.lft' => '> ' . $item [ $class ][ 'lft' ], $class . '.rght' => '< ' . $item [ $class ][ 'rght' ]));
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
2006-03-28 02:44:55 +00:00
* Gets a conditions array to find an Aro or Aco , based on the given id or alias
2005-12-22 01:07:28 +00:00
*
2006-03-28 02:44:55 +00:00
* @ param mixed $id
* @ return array Conditions array for a find / findAll call
2005-12-22 01:07:28 +00:00
*/
2006-03-28 02:44:55 +00:00
function _resolveID ( $id )
2006-02-18 23:42:21 +00:00
{
2006-03-28 02:44:55 +00:00
extract ( $this -> __dataVars ());
$key = ( is_string ( $id ) ? 'alias' : $secondary_id );
return array ( $this -> name . '.' . $key => $id );
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
2006-03-28 02:44:55 +00:00
* Shifts the left and right values of the aro / aco tables
* when a node is added or removed
2005-12-22 01:07:28 +00:00
*
2006-01-12 02:10:47 +00:00
* @ param unknown_type $table
* @ param unknown_type $dir
* @ param unknown_type $lft
* @ param unknown_type $rght
2005-12-22 01:07:28 +00:00
*/
2006-02-18 23:42:21 +00:00
function _syncTable ( $table , $dir , $lft , $rght )
{
2006-03-15 20:45:59 +00:00
$db =& ConnectionManager :: getDataSource ( $this -> useDbConfig );
2006-03-28 02:44:55 +00:00
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 );
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Enter description here ...
*
* @ return unknown
*/
2006-02-18 23:42:21 +00:00
function __dataVars ()
{
2005-11-05 04:08:14 +00:00
$vars = array ();
2006-03-28 02:44:55 +00:00
$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 );
2005-11-05 04:08:14 +00:00
return $vars ;
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
2005-12-22 01:07:28 +00:00
/**
* Enter description here ...
*
*/
2006-02-18 23:42:21 +00:00
function setSource ()
{
2006-03-19 03:26:43 +00:00
$this -> table = strtolower ( get_class ( $this )) . " s " ;
2006-02-18 23:42:21 +00:00
}
2005-11-05 04:08:14 +00:00
}
?>