<?php
/*
	-------------------------------------------------------
	ACTION CLUSTER
	-------------------------------------------------------

	E.J. Klein Ovink <emiel@sparkle6.com>
	http://www.sparkle6.com
	
	Copyright (c) 2007, All rights reserved.
	
 	-------------------------------------------------------
	- I don't need a link or anything, just keep the stuff above in tact
	
	
	
	// 	DESCRIPTION
	
		This class allows you to create clusters of actions. It is a memory you can store actions in, so they can
		be launched all together at a specific moment.
		If you load modules in the header of a content managament system wich contain functions you want to be launched later or
		on another moment in the header this can be handy.		
		You can also store shutdown functions in a cluster and launch this cluster on shutdown, wich allows you to register and 
		unregister shutdown functions. This is not possible with php's standard functions.
		
		For extra flexibility you can arrange the actions in different sequences to make sure the actions will be launched in the 
		prefered order.
		Within the sequences the actions are launched in the order they are added to the action cluster.
			

		- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		
		Example for registering a shutdown function:
		
		
			
			function foo($bar) {
				print $bar;
			}
				
			
			$actions	=	new ActionCluster();
			register_shutdown_function(array(&$actions, 'launch'));

			$actions->add('foo' , "<!-- If you check your html source, you see this printed -->");
	

		- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/




	
	class ActionCluster {

		var	$actions,	// 	(array)	Actions storage
			$sequences,	// 	(array)	Sequence storage
			$seq,		// 	(int)	Default sequence
			$id;		//	(int)	The id counter

		
		// Constructor
		function ActionCluster() {				
			$this->initialize();
		}

		function initialize() {
			$this->actions		=	array();	
			$this->sequences	=	array();
			$this->seq		=	1;			
			$this->id		=	1;						
		}
		
		
		
		
		/*	Add actions to action storage
		 *	
		 *	This method can be used to add new actions to the current action storage. Everytime an action
		 *	is added the id increases by 1
		 *	An action is added by giving its name as first argument followed by the actions own parameters.
		 *
		 */				
		function add() {			
	
			$parameters	= 	func_get_args();
			
			$this->saveSequence($this->seq);
			$this->saveAction($parameters, $this->seq);
		
		}
	

		/*	Add actions to a specified sequence
		 *	
		 *	Same as add() only this enables you to give a sequence as first parameter, followed by the action
		 *	and its parameters.
		 *
		 */		
		function addTo($seq) {			
	
			$parameters	= 	func_get_args();
			
			if( is_int($parameters[0]) )
				$seq	= 	array_shift($parameters); // The first argument can be used to set sequences
			else
				trigger_error("Given variable <code>\$seq = $seq</code> but should be an integer.");
			
			$this->saveSequence($seq);
			$this->saveAction($parameters, $seq);

		
		}
	
	
		/*	Save sequence
		 *	
		 *	This function saves the sequence
		 *	$seq = integer
		 *
		 */			
		function saveSequence($seq) {
			
			if( !isset($this->sequences[$seq]) )
				$this->sequences[$seq]	=	array();
			
			array_unshift($this->sequences[$seq], $this->id);		
		}
	
	
	
	
		/*	Save sequence
		 *	
		 *	This function saves the action and its parameters.
		 *	$parameters	= array , 0 => name , followed by parameters in the other keys
		 *
		 */		
		function saveAction($parameters, $seq) {
		
			$action	=	array_shift($parameters);	

			if( !is_callable($action) ) {
				trigger_error("<code>$action()</code> is no valid callback action");
				exit;
			} else {
				$this->actions[$this->id]	=	array('action' => $action, 'parameters' => $parameters, 'sequences' => $seq);	
				$this->id++;	
			}		
		}
	
	
	
	
		/*	Delete an action
		 *	
		 *	When a valid action id is passed as argument this function will delete that action
		 *	from the action cluster.
		 *
		 *	$id	= integer
		 *
		 */	
		function delete($id) {		
			$seq	=	$this->actions[$id]['sequences'];
			
			if( in_array($id, $this->sequences[$seq]) ){		
				list( $key )	=	 array_keys( $this->sequences[$seq], $id );		
				unset($this->actions[$id]);
				unset($this->sequences[$seq][$key]);
	
			} else {
				trigger_error("Id <code>$id</code> could not be found in sequence");
				exit;
			}
		}
	
		
		
		
		/*	Launch all actions
		 *
		 *	This method launches all the actions stored in the current action cluster. The sequence with the 
		 *	highest value first, and every action in a sequence in the order they are added.
		 *
		 */	
		function launch() {			
			krsort($this->sequences);
			
			foreach($this->sequences as $seq => $actions){
				
				sort($this->sequences[$seq]);	
				
				foreach ( $this->sequences[$seq] as $seq => $id ) {
					if( isset($this->actions[$id]['action']) )
						call_user_func_array($this->actions[$id]['action'], $this->actions[$id]['parameters']);
					else 
						trigger_error("Action where <code>\$id = $id</code> could not be launched.");
				}
								
			}
		}			



	}

?>