<?php

/**
 * Included libraries.
 *
 */
	if (!class_exists('Object')) {
		 uses ('object');
	}
/**
 * Menu Permission Resolver.
 *
 * Takes Menu Permission records from the database and returns the allowed menu.
 *
 * @package		cake
 * @subpackage	cake.cake.libs
 */
class Permission extends Object{
	
	/**
	 * array of menu permission records to parse.
	 *
	 * @var string
	 */
		var $records = null;
		
	/**
	 * array consisting of the current record being resloved.
	 *
	 * @var array
	 */
		var $c_record = null;	
		
	/**
	 * array consisting of the record of the current Component being resolved.
	 *
	 * @var array
	 */		
		var $base = null;
		
	/**
	 * holds the index of current permission record during resolutions.
	 *
	 * @var int
	 */
		var $r_index = 0;
		
	/**
	 * holds the index of the permission record starting the current component during resolution.
	 *
	 * @var int
	 */
		var $c_index = 0;
	
	/**
	 * holds the total number of permission records.
	 *
	 * @var int
	 */
		var $t = 0;
		
		
	
	/**
	 * Constructor.
	 *
	 * @param array $r - the records to parse permissions from
	 */
		function __construct($r = null) {
			
			$this->records = $r;
			$this->t=count($r);
			parent::__construct();
		}
	
	/**
	 * Parse the allowed menu for the current set of permission records
	 *
	 * @return void
	 */
		function resolveMenu() {
			
			if( !$this->records ) {
				return false;
			}
			
			while( $this->r_index < $this->t ) {
				
				$this->c_record =& $this->records[$this->r_index];
				
				// if the current record is the same comp as the current comp
				// the we must check for permissions
				if( $this->base && $this->c_record['Menu']['id'] == $this->base['Menu']['id'] ) {
					switch(	$this->c_record['Perm']['allow'] ) {
						case 1:
							//comp is allowed again check for new action perms and unset current record
							$this->resloveActions();
						break;
						//case 0:
							//in herit do nothing and let it be unset
						//break;
						case 2:
							//so unset the current base comp and let the current record be unset as well
							unset( $this->records[ $this->c_index] );
						break;
					}
					unset($this->records[$this->r_index]);
				}
				//else we have a new allowed comp
				else {
					switch(	$this->c_record['Perm']['allow'] ) {
						case 1:
							//comp is allowed so now its the current comp
							$this->c_index = $this->r_index;
							$this->c_record['Action']['actions'] = array();
							$this->base =& $this->c_record;
							$this->resloveActions();
						break;
						case 0://in herit
						case 2://or deny both unset the comp
							unset($this->records[$this->r_index]);
						break;
					}
					
				}	
				$this->r_index++;
			}		
		}
		
	/**
	 * Private
	 * filters the current record for allowed actions
	 *
	 * @return void
	 */
		function resloveActions() {
			
			//first create the action group if its not already set
			if( !array_key_exists($this->c_record['PermAct']['group'], $this->base['Action']['actions']) ) {
				$this->base['Action']['actions'][$this->c_record['PermAct']['group'] ] = array();
			}
			
			$actionC =& $this->base['Action']['actions'][$this->c_record['PermAct']['group'] ];
			
			switch($this->c_record['PermAct']['allow']) {
				case 2:// deny action perm so remove it from the actions array if its there
					if( in_array( $this->c_record['Action']['class'], $actionC ) ) {
						for($t=0,$M=count($actionC); $t<$M; $t++ ) {
							if( $actionC[$t] == $this->c_record['Action']['class'] ) {
								unset($this->base['Action']['actions'][$this->c_record['PermAct']['group'] ][$t]);
							}
						}
					}
				break;
				case 1://allow action perm so add it to the actions array if its not there
					if( !in_array($this->c_record['Action']['class'], $actionC) ) {
						$actionC[] = $this->c_record['Action']['class'];
					}								
				break;
				//case 0://inherit action perm from bellow role so do nothing
			}
		}
	
	/**
	 * returns the new filtered menu item array
	 *
	 * @return array
	 */
		function getResults() {
			
			return array_values( $this->records );
		}
}
?>