EditCustomActionsAction

<?php require_once "autoload.php";

class EditCustomActionsAction extends ExploreAction
{
	public function __construct($model)
	{
		parent::__construct($model);
		$this->path = $model["path"];
		$this->selectionKey = "./USER/ACTIONS/";		
		$this->view = new EditCustomActionsView($this);		
	}

}
?>

ExploreAction

<?php require_once "autoload.php";

class ExploreAction extends ViewAction
{
	/*The path is the current directory the user has navigated to. The view populates data based on
	where we have pathed to.*/
	protected $path;
	
	/*The selectionKey represents objects that have already been selected by the user.  For example
	if a user adds a target to his "mobile albums," this target is stored in ./USER/TARGET_SELECTION.
	Setting the selectionKey to ./USER/TARGET_SELECTION will notify the view that the user has already selected
	the "mobile albums" target.*/
	protected $selectionKey;
	
	
	/*checkedIds represents objects that the user has selected in previous paths. */
	protected $checkedIds;
	
	
	protected $resourceCollection;
	
	
	/*selectedId represents a focused item. Used by the singleImageAction/view to select one image.*/
	protected $selectedId;
	

	public function __construct($model)
	{
		parent::__construct($model);
		$this->checkedIds = $model["checkedIds"];
		$this->path = $model["path"];
	}
	
	/* Equivalent to clicking a source. Details handles how we procede from the current state.
	   In this explore context, we execute SAME.
	*/
	public function detail($ids)
	{
		$this->path = $ids;
		
		$am = ApplicationManager::getInstance();
		return $am->notify("SAME", $this->getModel());
	}
			

	public function navigateDetail($ids)
	{
		$model["path"] = $ids;
		$model["selectedId"] = $this->selectedId;
		
		$am = ApplicationManager::getInstance();
		return $am->notify("NAVIGATE",$model);
	}

	public function imageDetail($ids)
	{
		$tm = TransactionManager::getInstance();
		$top = $tm->top();
		//echo print_r($ids,true);	
		$topModel = $top->get("model");
		$this->path = $topModel["path"];
		//echo "here".print_r($top->get("businessAction"),true);
		
		//$newModel["path"] = $this->path;
		//needs bound checking on size of $ids array
	//	echo "here".print_r($top->get("model"),true);
		$this->selectedId = $ids[0];
		
		//when i echo, the stack doesnt grow... seems weird
		
		//INFOUT($model["selectedId"]."Model selectedId");
		$am = ApplicationManager::getInstance();
		return $am->notify("VIEW_IMAGE", $this->getModel());
	}
	
	public function getModel()
	{
	//	echo("Expensive getModel() call used");
		if($this->path)
		$model["path"] = $this->path;
		if($this->selectionKey)
		$model["selectionKey"] = $this->selectionKey;
	//	$model["checkedIds"] = $this->checkedIds;
	//	if($this->resourceCollection)
	//	$model["resourceCollection"] = $this->resourceCollection;
		if($this->selectedId)
		$model["selectedId"] = $this->selectedId;
		return $model;
	}
	
	public function view()
	{
		if ($this->load())
			return parent::view();
		return false;
	}
	
	public function load()
	{
		$cm = ContentManager::getInstance();
		$result = $cm->getChildResources($this->path, $this->resourceCollection);
		
		if (!$result)
		{
			$am = ApplicationManager::getInstance();
			$am->notify("AUTHENTICATE", null);
			return false;
		}
		
		$pm = PersistenceManager::getInstance();
		$selectedResourceIds = $pm->load($this->selectionKey);
		if (!$selectedResourceIds)
			$selectedResourceIds = array();
		foreach ($this->resourceCollection as $resource)
		{
			$resource->set("selected", in_array($resource->get("id"), $selectedResourceIds));
			$index = array_search($resource->get("id"), $selectedResourceIds);
			if ($index === false)
				continue;
			unset($selectedResourceIds[$index]);
		}
		$pm->save($this->selectionKey, $selectedResourceIds);

		return true;
		
	}
	
	public function save()
	{
		$pm = PersistenceManager::getInstance();
		$selectedResourceIds = $pm->load($this->selectionKey);
		if (!$selectedResourceIds)
			$selectedResourceIds = array();
		if (!$this->checkedIds)
		{
			$this->checkedIds = array();

		}
		$selectedResourceIds = array_unique(array_merge($selectedResourceIds, $this->checkedIds));
		$pm->save($this->selectionKey, $selectedResourceIds);
		
		return true;
	}
	
	public function back()
	{
		//$this->save();
		$am = ApplicationManager::getInstance();
		$am->notify("BACK", null);
	}

	public function cancel()
	{
		echo "ExploreAction.cancel - not fully implemented";
		//$this->save();
		$am = ApplicationManager::getInstance();
		$am->notify("CANCEL", null);
	}

	public function done()
	{

		$am = ApplicationManager::getInstance();
		$am->notify("DONE", null);
	}


}
?>

ViewAction

<?php require_once "autoload.php";

abstract class ViewAction extends RouterAction
{
	protected $view;
	
	public abstract function save();
	
	public function execute()
	{
		$this->save();
		
		if (!$this->method)
			$this->method = "view";
		
		parent::execute();
	}
	
	
	public abstract function getModel();
	

	public function view()
	{
		$ret = false;
	
		do
		{
			$err = get_class($this)." - ViewAction:start - ";
		
			if (!$this->view)
			{
				ERROUT($err."no view defined for this action");
				break;
			}
			
			if (!$this->view->compose())
			{
				ERROUT($err."view ".get_class($this->view)." failed to compose");
				break;
			}
	
			$pm = PersistenceManager::getInstance();
			$pm->commit();

			if (!$this->view->render())
			{
				ERROUT($err."view ".get_class($this->view)." failed to render");
				break;
			}
			
			$ret = true;
		} while (false);
		
		return $ret;
	}
	
}
?>

RouterAction

<?php require_once "autoload.php";

abstract class RouterAction extends Action
{
	protected $method;
	protected $methodName;
	protected $args;
	
	public function __construct($model)
	{
		parent::__construct($model);
		do
		{
			$this->method = $model["method"];
			if (!$this->method)
			{
				break;
			}
			


		} while (false);
		
	}
	
	public function execute()
	{

		$ret = false;
		
		do
		{
			if (!$this->method)
			{
				ERROUT(get_class($this).":execute - Method not provided.");
				break;
			}

			$this->methodName = $this->method;

			if (is_array($this->method))
			{
				$this->methodName = reset(array_keys($this->method));
			}
		
			if (!is_string($this->methodName))
			{
				ERROUT(get_class($this)." - RouterAction:__construct - Method name incorrect format.");
				break;
			}

			preg_match("/^([^\(]+)(\((.*)\))?/", $this->methodName, $params);
			if ($params[1] != null)
			{
				$this->methodName = $params[1];
				$this->args = preg_split("/,/", $params[3]);
				
				
				foreach (array_keys($this->args) as $key)
				{
					preg_match("/{(.*)}/", $this->args[$key], $tokens);
					if (!$tokens)
						continue;
					$this->args[$key] = preg_split("/;/", $tokens[1]);
					
				}
			}
		

			$ret = call_user_func_array(array($this, $this->methodName), $this->args);
			
		} while (false);
		
		return $ret;
	}
}

?>

Action

<?php require_once "autoload.php";

abstract class Action extends Bean
{
	protected $action;
	
	public function __construct($model)
	{
		parent::__construct();
		//$this->map_to_vars($model);
	}

	public abstract function execute();
	
	

}
?>

Bean

<?php include_once "autoload.php";

abstract class Bean
{
	public function set($field, &$value) { $this->$field = $value; }
	public function &get($field) { return $this->$field; }

	public function __construct()
	{
	}

	public function map_to_vars(&$map)
	{
		foreach (array_keys(get_object_vars($this)) as $key)
		{
			$this->$key = $map[$key];
		}
	}
}
?>