Location: PHPKode > projects > simpleMVC > myGifts/includes/Controler.class.php
<?php

class Controler {
	
	function registerHandler($handler, $method, $class, $params = null, $needsLogin = false)
	{
		global $G_CTRL_HANDLERS, $G_CTRL_METHOD, $G_CTRL_PARAMS, $G_CTRL_NEEDS_LOGIN, $logger;

		$logger->logMessage($logger->LOG_INFO, "registerHandler($handler, $method, $class, ...)");

		if (!is_array($G_CTRL_HANDLERS)) {
			$G_CTRL_HANDLERS = array();
		}
		$G_CTRL_HANDLERS[$handler] = $class;

		if (!is_array($G_CTRL_METHOD)) {
			$G_CTRL_METHOD = array();
		}
		$G_CTRL_METHOD[$handler] = $method;

		if (!is_array($G_CTRL_PARAMS)) {
			$G_CTRL_PARAMS = array();
		}
		$G_CTRL_PARAMS[$handler] = $params;

		if (!is_array($G_CTRL_NEEDS_LOGIN)) {
			$G_CTRL_NEEDS_LOGIN = array();
		}
		$G_CTRL_NEEDS_LOGIN[$handler] = $needsLogin;
	}


	function registerNextAction($handler, $nextAction)
	{
		global $G_CTRL_NEXT_ACTION;
		if (!is_array($G_CTRL_NEXT_ACTION)) {
			$G_CTRL_NEXT_ACTION = array();
		}
		$G_CTRL_NEXT_ACTION[$handler] = $nextAction;
	}

	function getHandler($handler)
	{
		global $G_CTRL_HANDLERS, $logger;
		$logger->logMessage($logger->LOG_INFO, "getHandler($handler) =>".$G_CTRL_HANDLERS[$handler]);
		return $G_CTRL_HANDLERS[$handler];
	}

	function &getObject($handler, $id="")
	{
		global $G_CTRL_PARAMS, $user, $logger;

		if (empty($id) && is_array( $G_CTRL_PARAMS[$handler])) {
			$paramName = $G_CTRL_PARAMS[$handler][0];
			$logger->logMessage($logger->LOG_INFO, "id = $paramName");
			if ($paramName == "user")
				$id = $user->id;
			else if (!empty($paramName))
				$id = Tools::getHttpParam($paramName);
		}
		if (empty($id))
			$id = Tools::getHttpParam("id");
			
		$class = Controler::getHandler($handler);

		$logger->logMessage($logger->LOG_INFO, "getObject ($id) -> $class");
		
		if (!empty($id))
			return new $class($id);
		else
			return new $class();
	}
	
	function &getParams($handler)
	{
		global $G_CTRL_PARAMS, $user;

		$params = array();
		
		if (is_array($G_CTRL_PARAMS[$handler]))
		foreach (array_slice($G_CTRL_PARAMS[$handler], 1) as $paramName) {
			if ($paramName == "user")
				$params["user"] = $user->id;
			else
				$params["$paramName"] = Tools::getHttpParam($paramName);
		}
		
		return $params;
	}
	
	function callAction(&$object, $handler)
	{
		global $logger;

		$logger->logMessage($logger->LOG_INFO, "callAction $handler");
		
		if (!is_object($object))
			return false;
			
		$params = Controler::getParams($handler);
		//print_r($params);
		return call_user_func_array(array($object, $handler), $params);
	}
	
	function getParamsAction(&$object, $handler)
	{
		global $logger;
		
		$logger->logMessage($logger->LOG_INFO, "getParamsAction $handler");
		if (!is_object($object))
			return false;
			
		$params = Controler::getParams($handler);
		//print_r($params);
		return call_user_func_array(array($object, "getParams"), $params);
	}
	
	function needsLogin($handler)
	{
		global $G_CTRL_NEEDS_LOGIN;
		return $G_CTRL_NEEDS_LOGIN[$handler];
	}

	function nextAction($handler)
	{
		global $G_CTRL_NEXT_ACTION, $logger;
		
		$logger->logMessage($logger->LOG_INFO, "nextAction =".$G_CTRL_NEXT_ACTION[$handler]);
		return $G_CTRL_NEXT_ACTION[$handler];
	}
	
	function goto($handler)
	{
		global $logger;
		$logger->logMessage($logger->LOG_INFO, "goto : ".$_SERVER["SCRIPT_NAME"]."?handler=".urlencode($handler));
		//echo "<a href=\"".$_SERVER["SCRIPT_NAME"]."?handler=".urlencode($handler)."\">suite</a>";
		header("Location: ".$_SERVER["SCRIPT_NAME"]."?handler=".urlencode($handler)); 
	}

	function gotoPrevious($error = "")
	{
		if (!empty($_SERVER["HTTP_REFERER"])) {
			$nextUri = $_SERVER["HTTP_REFERER"];
			if (!empty($error))
				$nextUri = Tools::appendUri($nextUri, "error", $error);
			//echo "<a href=\"$nextUri\">suite</a>";
			header("Location: $nextUri");
		} else
			Controler::goto(DEFAULT_HANDLER);
	}

	function displayTemplate($template, &$user, &$object, $nextAction = "")
	{
		echo Controler::processTemplate($template, $user, $object, $nextAction);
	}
	
	function processTemplate($template, &$user, &$object, $nextAction = "")
	{
		global $smarty, $strings, $logger, $app, $handler;

		$logger->logMessage($logger->LOG_INFO, "display $template.tpl");

		if (!isset($smarty)) {
			$smarty = new Smarty;
			$smarty->template_dir = "./skins/".SKIN."/";
			$smarty->compile_dir = "./cache/skins/";
			$smarty->use_sub_dirs = false;
			if (DEBUG) {
				//$smarty->force_compile = true;
				//$smarty->debugging = true;
			}
		}
		$smarty->assign('app', $app);
		$smarty->assign('strings', $strings);
		$smarty->assign('currentAction', $handler);
		$smarty->assign('nextAction', $nextAction);
		$smarty->assign('user', $user);
		$smarty->assign('object', $object);
		$error = Tools::getHttpParam("error");
		if (!empty($error))
			$smarty->assign('error', $strings[$error]);
		$logger->logMessage($logger->LOG_INFO, "error =".$strings[$error]);

		//$smarty->display("$handler.tpl");
		return $smarty->fetch("$template.tpl");
	}

	function displayError($error)
	{
		global $smarty, $strings, $logger;

		$logger->logMessage($logger->LOG_INFO, "display error");

		if (!isset($smarty)) {
			$smarty = new Smarty;
			$smarty->template_dir = "./skins/".SKIN."/";
			$smarty->compile_dir = "./cache/skins/";
			$smarty->use_sub_dirs = false;
			if (DEBUG) {
				//$smarty->force_compile = true;
				//$smarty->debugging = true;
			}
		}
		$smarty->assign('strings', $strings);
		$smarty->assign('nextAction', $nextAction);
		$smarty->assign('title', $strings["ERROR"]);
		$smarty->assign('text', $strings[$error]);
		$smarty->display("message.tpl"); 
	}
}

?>
Return current item: simpleMVC