ApplicationManager

<?php require_once "autoload.php";
class ApplicationManager
{
	private static $instance;
	public static function getInstance()
	{
		if (!isset(self::$instance))
		{
			$c = __CLASS__;
			self::$instance = new $c;
		}
		return self::$instance;
	}
	
	public $tm;
	
	public function __construct()
	{
		$this->tm = TransactionManager::getInstance();
	}
	
	public function notify($businessAction, $model)
	{
		$ret = false;
		$action = null;
		
		switch ($businessAction)
		{
		case "SELF":
			$forward = $this->tm->top();
			return $this->notify($forward->get("businessAction"), $forward->get("model"));
		case "SAME":
			$forward = $this->tm->top();
			if (!$forward)
				return $this->notify("EXPLORE", $model);
			return $this->notify($forward->get("businessAction"), $model);

		case "BACK":
			return $this->back($model);

		case "CANCEL":
			return $this->done($model);

		case "DONE":
			return $this->cancel($model);

		case "AUTHENTICATE":
			$action = $this->authenticate($model);
			break;
			
		case "EXPLORE":
			$action = $this->explore($model);
			break;

		case "SHARE":
			$action = $this->share($model);
			break;
			
		case "EDIT_SOURCES":
			$action = $this->editSources($model);
			break;
			
		case "EDIT_ACTIONS":
			$action = $this->editActions($model);
			break;

		
		default:
			$action = $this->unknown($model);
			break;
		}
		
		$topForward = $this->tm->top();
		
		if (!$topForward || ($topForward->get("businessAction") != $businessAction))
		{
			$tx = new Transaction();
			$this->tm->pushTx($tx);
		}

		$tx = $this->tm->topTx();

		$forward = new Forward($businessAction, $model);
		if ($forward != $this->tm->top())
			$this->tm->push($forward);
		
		$ret = $action->execute();
		return $ret;
	}
	
	private function unknown($model)
	{
		$action = new ErrorAction($model);
		return $action;
	}
	
	private function back($model)
	{
		$top = $this->tm->pop();
		$prev = $this->tm->pop();
		if (!$prev)
		{
			Main::stop();
		}

		return $this->notify($prev->get("businessAction"), $prev->get("model"));
	}
	
	
	private function authenticate($model)
	{
	print_r($model);
		return new SnapAuthenticationAction($model);
	}
	
	private function editSources($model)
	{
		if (!$model["path"])
			$model["path"] = array("");
		return new EditCustomSourcesAction($model);
	}
	
	private function editActions($model)
	{
		if (!$model["path"])
			$model["path"] = array("./ACTIONS/");
		return new EditCustomActionsAction($model);
	}

	private function share($model)
	{
		if (!$model["path"])
			$model["path"] = array("./USER/TARGETS/");
		return new TargetsAction($model);
	}

	private function explore($model)
	{
		$action = null;

		do
		{
			if (!$model)
			{
				$action = new CustomSourcesAction($model);
				break;
			}
	
			$newModel["path"] = $model["path"];
			
			switch ($model["path"])
			{
			case array("./USER/PRINTS/"):
				$action = new PrintQueueAction($newModel);
				break;
			default:
				$action = new SourcesAction($newModel);
				break;
			}
		
		} while (false);
		
		return $action;
	}
	
	public function startAction($context, $model)
	{
		$ret = false;
		
		do
		{
			$tm = TransactionManager::getInstance();
			$topTx = $tm->topTx();
			if ($topTx)
				$prevContext = $topTx->get("context");
	
			if ($prevContext != $context)
			{
				$tx = new Transaction($context);
				$tm->pushTx($tx);
			}

			$af = ActionFactory::getInstance();
			$action = $af->getAction($context, $model);

			$tm->push(get_class($action));
			$ret = $action->execute();
		
		} while (false);
		
		return $ret;		
	}	
}
?>
<?php require_once "autoload.php";
class ApplicationManager
{
	private static $instance;
	public static function getInstance()
	{
		if (!isset(self::$instance))
		{
			$c = __CLASS__;
			self::$instance = new $c;
		}
		return self::$instance;
	}
	
	public $tm;
	
	public function __construct()
	{
		$this->tm = TransactionManager::getInstance();
	}
	
	public function notify($businessAction, $model)
	{
		$ret = false;
		/*THIS IS A HACK 
		It allows me to keep the stack from growing when users iterate through an image list. 
		*/
		$viewImageHack;
		$action = null;
		switch ($businessAction)
		{
		case "SELF":
			$forward = $this->tm->top();		
			return $this->notify($forward->get("businessAction"), $forward->get("model"));
		case "SAME":
			$forward = $this->tm->top();
			if (!$forward)
				{
					return $this->notify("EXPLORE", $model);
				}		
			return $this->notify($forward->get("businessAction"), $model);
	
		case "NAVIGATE":
			$tm = TransactionManager::getInstance();
			$forward = $this->tm->cutTo($model["path"]);
			return $this->notify($forward->get("businessAction"),$forward->get("model"));
	
		case "BACK":
			return $this->back($model);

		case "CANCEL":
			return $this->done($model);

		case "DONE":
			return $this->done($model);

		case "AUTHENTICATE":
			$action = $this->authenticate($model);
			break;
			
		case "EXPLORE":
			$action = $this->explore($model);
			break;

		case "SHARE":
			$action = $this->share($model);
			break;
		case "EDIT_PRINTS":
			$action = $this->editPrints($model);
			break;
			
		case "EDIT_SOURCES":
			$action = $this->editSources($model);
			break;
			
		case "EDIT_ACTIONS":
			$action = $this->editActions($model);
			break;
		case "VIEW_IMAGE":
			$businessAction = "EXPLORE";
			$action = $this->viewImage($model);
			$viewImageHack = true;
			break;
			
		case "EDIT_TARGETS":
			$action = $this->editTargets($model);
			break;
		
		default:
			$action = $this->unknown($model);
			break;
		}
		
		$topForward = $this->tm->top();
		
		
		/*If the next forward movement is a different context, we push it into its own transaction stack
		*/
		
		if (!$topForward || ($topForward->get("businessAction") != $businessAction))
		{
			$tx = new Transaction();
			$this->tm->pushTx($tx);
		
		}

		$tx = $this->tm->topTx();
		$forward = new Forward($businessAction, $action->getModel());
		if ($forward != $this->tm->top() && !$viewImageHack)
		{
			$this->tm->push($forward);

		}
		$ret = $action->execute();
		return $ret;
	}

	private function unknown($model)
	{
		$action = new ErrorAction($model);
		return $action;
	}
	private function editTargets($model)
	{
		/*When exploring deeper into the target tree, model["path"] will be set...
		If it is not, we will start the root exploration at custom and online sources
		*/
		if (!$model["path"])
		$model["path"] = array("/","./");
		return new EditCustomTargetsAction($model);
	}
	private function back($model)
	{
		$top = $this->tm->pop();
		$prev = $this->tm->pop();
		if (!$prev)
		{
			Main::stop();
		}
		
		return $this->notify($prev->get("businessAction"), $prev->get("model"));
	}
	
	
	private function authenticate($model)
	{
		print_r($model);
		return new SnapAuthenticationAction($model);
	}
	
	private function editSources($model)
	{
		if (!$model["path"])
			$model["path"] = array("./USER/SOURCES/");
		return new EditCustomSourcesAction($model);
	}
	private function editPrints($model)
	{
		if(!$model["path"])
			$model["path"] = array("./USER/PRINTS/");
			return new EditPrintQueueAction($model);
		
	}
	private function editActions($model)
	{
		if (!$model["path"])
			$model["path"] = array("./ACTIONS/");
		
		
		//	echo "heres model<br>:".print_r($model,true);
		return new EditCustomActionsAction($model);
	}

	private function share($model)
	{
		if (!$model["path"])
			$model["path"] = array("./USER/TARGETS/");
			/*The ACTIVE_SELECTION is the objects selected to share.  We must persist these 
			so that we can finally complete the share operation once a successful target has been
			selected.
			*/
			$pm = PersistenceManager::getInstance();
			$pm->save("./USER/ACTIVE_SELECTION/",$model["checkedIds"]);
		return new CustomTargetsAction($model);
	}
	/* Done represents completion of some user's action.  The transaction is completed and we remove it,
	and resume execution at the top of the previous transaction.
	*/
	private function done($model)
	{
		
		$this->tm->popTx();
	//	if (!$forward)
	//	return $this->notify("EXPLORE", $model);
		return $this->notify("SAME", $model);
	
	}
	private function explore($model)
	{
		$action = null;

		do
		{
//		
			if (!$model)
			{
				$action = new CustomSourcesAction($model);
				break;
			}
	
			$newModel["path"] = $model["path"];
			
			switch ($model["path"])
			{
			case array("./USER/PRINTS/"):
				$action = new PrintQueueAction($newModel);
				break;
			case array("./USER/SOURCES/"):
				$action = new CustomSourcesAction($model);
				break;

			default:
				$action = new SourcesAction($newModel);
				break;
			}
		
		} while (false);
		
		return $action;
	}
	private function viewImage($model)
	{
		return new SingleImageAction($model);
	}
	/*
	public function startAction($context, $model)
	{
		$ret = false;
		
		do
		{
			$tm = TransactionManager::getInstance();
			$topTx = $tm->topTx();
			if ($topTx)
				$prevContext = $topTx->get("context");
	
			if ($prevContext != $context)
			{
				$tx = new Transaction($context);
				$tm->pushTx($tx);
			}

			$af = ActionFactory::getInstance();
			$action = $af->getAction($context, $model);

			$tm->push(get_class($action));
			$ret = $action->execute();
		
		} while (false);
		
		return $ret;		
	}
	*/
} 
?>