Location: PHPKode > projects > Todo Manager > todo/classes/controller.class.php
<?php
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/library/util.lib.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/library/session.lib.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/library/request.lib.php";
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/messages.class.php";

class Controller {
	var $defaultParams;
	var $params;
	var $messages;
	var $errors;
	var $redirect;

	function Controller() {
		set_error_handler(array(&$this, "errorHandler"));
		
		$this->debug = FALSE;
		$this->params = array();
		$this->defaultParams = array();
		
		$this->redirect = TRUE;
		$this->errors = new Messages();
		$this->messages = new Messages();
		$this->init();
		if ($_SERVER["REQUEST_METHOD"] == "GET") {
			$this->onGet();
		} else
		if ($_SERVER["REQUEST_METHOD"] == "POST") {
			if ($this->getAction() == "__next_page__") {
				$this->nextPage($this->getValue());
			} else
			if ($this->getAction() == "__previous_page__") {
				$this->previousPage();
			} else
			if ($this->getAction() == "__set_param__") {
				$name = $this->getValue("name");
				$value = $this->getValue("value");
				$this->setParam($name, $value);
				util_redirect($this->getCurrentPage());
			} else {
				$this->onPost();
				if ($this->redirect)
					util_redirect($this->getCurrentPage());
			}
		}
	}
	
	function errorHandler($errno, $errstr, $errfile, $errline) {
		$this->redirect = FALSE;
		
		echo "<br />\n";
		echo "<strong>Warning</strong>: ".$errstr." in ";
		echo "<strong>".$errfile."</strong> on line <strong>".$errline."</strong><br />\n";
	}
	
	function loadParams() {
		$args = func_get_args();
		$this->defaultParams = request_getparams($args);
	}
	
	function loadMessages($filename, $language) {
		$this->messages->loadFile($filename, $language);
	}
	
	function getMessage($name, $className = NULL) {
		return $this->messages->get($name, $className);
	}
	
	function getMessageLn($name, $className = NULL) {
		return $this->messages->getLn($name, $className);
	}
	
	function getRawMessage($name) {
		return $this->messages->getRaw($name);
	}
	
	function setMessage($name, $value) {
		$this->redirect = FALSE;
		$this->messages->set($name, $value);
	}
	
	function getError($name, $className = NULL) {
		return $this->errors->get($name, $className);
	}
	
	function getErrorLn($name, $className = NULL) {
		return $this->errors->getLn($name, $className);
	}
	
	function getRawError($name) {
		return $this->errors->getRaw($name);
	}
	
	function setError($name, $value) {
		$this->redirect = FALSE;
		$this->errors->set($name, $value);
	}
	
	function getParam($name) {
		if (array_key_exists($name, $this->defaultParams))
			return $this->defaultParams[$name];
		if (array_key_exists($name, $this->params))
			return $this->params[$name];
		return request_get($name);
	}
	
	function setParam($name, $value) {
		if (array_key_exists($name, $this->defaultParams))
			$this->defaultParams[$name] = $value;
		else
			$this->params[$name] = $value;
	}
	
	function getAction() {
		return util_getfield($_REQUEST, "__action__");
	}
	
	function getValue($name = NULL) {
		if (!util_empty($name)) {
			$params = array();
			$data = urldecode($this->getParam("__ids__"));
			$items = split("&", $data);
			foreach($items as $item) {
				$item = urldecode($item);
				$split = split("=", $item);
				$params[$split[0]] = $split[1];
			}
			return util_getfield($params, $name);
		}
		return $this->getParam("__id__");
	}
	
	function getCurrentPage() {
		return util_composeurl($_SERVER["PHP_SELF"], $this->defaultParams);
	}
	
	function getPreviousPage() {
		if (util_empty(session_get("__return__")))
			return NULL;
		
		$return = unserialize(session_get("__return__"));
		return array_pop($return);
	}
	
	function previousPage() {
		if (util_empty(session_get("__return__")))
			return NULL;
		
		$return = unserialize(session_get("__return__"));
		$previous_page = array_pop($return);
		session_set("__return__", serialize($return));
		
		util_redirect($previous_page);
	}
	
	function nextPage($url, $params = NULL, $previous_page = NULL) {
		if (util_empty($previous_page))
			$previous_page = $this->getCurrentPage();
		$return = util_empty(session_get("__return__"))? array(): unserialize(session_get("__return__"));
		array_push($return, $previous_page);
		session_set("__return__", serialize($return));
		
		util_redirect($url, $params);
	}
	
	function redirect($url, $params = NULL) {
		session_set("__return__", NULL);
		util_redirect($url, $params);
	}
	
	// overrride this function in the extended class
	function init() {}
	
	// override this function in the extended class
	function onGet() {}
	
	// override this function in the extended class
	function onPost() {}
}
?>
Return current item: Todo Manager