Location: PHPKode > projects > Php open model view controller > phpopenmvc/controller.php
<?php

	class SystemControllerClass {
		var $controller;
		function getController(){
			return $this->controller;
		}
	}

	$SystemController = new SystemControllerClass();
	$GLOBALS['SystemController'] = $SystemController;

	class Validater {
		var $classname;
		var $method;
		var $resource;
		var $useSystemValidate=false;

		function &validate($var, $type=false, $pattern=false, $ignoreCase=true){
			if(!$type){
				return true;
			} else if($type=="boolean"){
				return Validater::validateBoolean($var);
			} else if($type=="numeric"){
				return Validater::validateNumeric($var);
			} else if($type=="int"){
				return Validater::validateInt($var);
			} else if($type=="integer"){
				return Validater::validateInteger($var);
			} else if($type=="float"){
				return Validater::validateFloat($var);
			} else if($type=="real"){
				return Validater::validateReal($var);
			} else if($type=="double"){
				return Validater::validateDouble($var);
			} else if($type=="long"){
				return Validater::validateLong($var);
			} else if($type=="string"){
				return Validater::validateString($var);
			} else if($type=="array"){
				return Validater::validateArray($var);
			} else if($type=="object"){
				return Validater::validateObject($var,$pattern);
			} else if($type=="email"){
				return Validater::validateEmail($var,$pattern);
			} else if($type=="date"){
				return Validater::validateDate($var,$pattern);
			} else if($type=="datetime"){
				return Validater::validateDatetime($var,$pattern);
			} else if($type=="time"){
				return Validater::validateTime($var,$pattern);
			} else if($type=="timestamp"){
				return Validater::validateTimestamp($var,$pattern);
			} else if($type=="reg"){
				return Validater::validateRegularExpression($var,$pattern,$ignoreCase);
			} else if($type=="special"){
				return true;
			} else if($type=="dynamic"){
				return true;
			} 
		}

		function &validateBoolean($var){return is_bool($var);}
		function &validateNumeric($var){return is_numeric($var);}
		function &validateInt($var){
			//return is_int($var); 
			return Validater::validateRegularExpression($var, "^[+-]?(0|[1-9][0-9]*)$");
		}
		function &validateInteger($var){return is_integer($var);}
		function &validateFloat($var){
			//return is_float($var);
			return Validater::validateRegularExpression($var, "^[-+]?(0|[1-9][0-9]*)\.?[0-9]+$");
		}
		function &validateReal($var){return is_real($var);}
		function &validateDouble($var){return is_double($var);}
		function &validateLong($var){return is_long($var);}
		function &validateString($var){return is_string($var);}
		function &validateArray($var){return is_array($var);}

		function &validateObject($object, $classname=false){
			if(is_object($object)){
				if(!$classname){
					return is_object($object);
				} else {
					if(get_class($object) == $classname){
						return true;
					} else {
						return false;
					}
				}
			} else return false;
		}

		function &validateEmail($email, $pattern='^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$'){
			return Validater::validateRegularExpression($email, $pattern, true);
		}

		function &validateDate($var, $format=false){
			if(!$format){
				return (!strtotime($var))?false:true;
			} else {
				return false;
			}
		}

		function &validateDatetime($var, $format=false){
			if(!$format){
				return (!strtotime($var))?false:true;
			} else {
				return false;
			}
		}

		function &validateTime($var, $format=false){
			if(!$format){
				return (!strtotime($var))?false:true;
			} else {
				return false;
			}
		}

		function &validateTimestamp($var, $format=false){
			if(!$format){
				return (!strtotime($var))?false:true;
			} else {
				return false;
			}
		}

		function &validateRegularExpression($string, $pattern, $ignoreCase=true){
			if($ignoreCase) return eregi($pattern, $string);
			if(!$ignoreCase) return ereg($pattern, $string);
		}
	}

	class Parameter {
		var $name;
		var $value;
		var $type;
		var $src;
		var $allowNull;
		var $defaultValue;
		var $forceValidate;
		var $validater;
		var $isValidateError;
		var $system_type = array('boolean', 'numeric', 'int', 'integer', 'float', 'real', 'double', 'long', 'string','array', 'object', 
			'email', 'date', 'datetime', 'time', 'timestamp', 'special', 'dynamic');
	}

	class ValidateErrorException {
		var $classname;
		var $method;
		var $resource;
		var $module;
		var $action;
	}

	class Action  {
		var $name;
		var $classname;
		var $method;
		var $actionview;
		var $parameters;
		var $onValidateError;
	}

	class Module  {
		var $name;
		var $resource;
		var $actions;
		var $output;
	}

	class View  {
		var $name;
		var $resource;
	}

	class Controller  {
		var $configFile;
		var $sess;
		var $onerror;
		var $validater;
		var $isValidateError = false;
		var $modules;
		var $views;
		var $activeModule;
		var $activeAction;

		// construct
		function Controller($configFile=false){
			if($configFile)  $this->loadConfig($configFile);
			$GLOBALS['SystemController']->controller =& $this;
		}

		function __construct($configFile=false){
			$this->Controller($configFile);
		}

		function execute($module=false, $action=false){
			$this->isValidateError = false;
			$this->activeModule = $module;
			$this->activeAction = $action;
			$this->invoke($module, $action);
		}

		function invoke($module=false, $action=false){
			if($module && isset($this->modules[$module])){
				$module = $this->modules[$module];
				$action = $module->actions[$action];

				if(isset($module->resource) && ($module->resource!="") && !class_exists($action->classname)) {
					require_once($module->resource);  // include dynamic class
				}

				if(isset($action->parameters) && sizeof($action->parameters)>0){
					$parameters = $this->prepareParameter($action->parameters);
					if($this->isValidateError){
						if(isset($action->onValidateError->classname) && isset($action->onValidateError->method)
							&& $action->onValidateError->classname!= "" && $action->onValidateError->method!="" ){
							if(isset($action->onValidateError->resource)  
								&& ($action->onValidateError->resource!="") && !class_exists($action->onValidateError->classname)) {
								require_once($action->onValidateError->resource);
							}
							empty($action);
							eval('$this->onerror = new '.$action->onValidateError->classname.'();');
							$result = call_user_method($action->onValidateError->method, $this->onerror);

						} else if(isset($action->onValidateError->module) && isset($action->onValidateError->action)){
							$exceptionModule = $action->onValidateError->module;
							$exceptionAction = $action->onValidateError->action;
							$module = $this->modules[$exceptionModule];
							$action = $module->actions[$exceptionAction];
							eval('$this->sess = new '.$action->classname.'();'); // invoke dynamic class
							$this->sess->output = $this->getOutput($action);
							call_user_method ($action->method, $this->sess);
						}
					}
				}

				if(!$this->isValidateError){
					eval('$this->sess = new '.$action->classname.'();'); // invoke dynamic class
					if(isset($action)){
						$this->sess->output = $this->getOutput($action);
						if(!isset($parameters)){ 
							call_user_method ($action->method, $this->sess);   // call action method
						} else {
							call_user_method_array($action->method, $this->sess, $parameters); 
						}
					}
				}
				Controller::display($this->sess, $action);
			}
		}

		function forward($module, $action, $parameter=false){
			$module = $this->modules[$module];
			$action = $module->actions[$action];
			eval('$this->sess = new '.$action->classname.'();');
			$this->sess->output = $this->getOutput($action);
			if(!$parameters){ 
				call_user_method ($action->method, $this->sess);   // call action method
			} else {
				call_user_method_array($action->method, $this->sess, $parameters); 
			}
			Controller::display($this->sess, $action);
		}

		function prepareParameter($params){
			$parameters = array();
			for($i=0;$i<sizeof($params);$i++){
				$paramObj = $params[$i];
				$src = $paramObj->src;
				eval('$buff = $'.$src.';');
				$value = $buff[$paramObj->name];
				if(isset($paramObj->defaultValue) && is_null($value)){
					$value = $paramObj->defaultValue;
				}
				
				if($paramObj->forceValidate){
					$result = false;
					if($paramObj->validater->useSystemValidate){
						$result = Validater::validate($value, $paramObj->type);
					} else {
						eval('$this->validater = new '.$paramObj->validater->classname.'();');
						$result = call_user_method_array($paramObj->validater->method, $this->validater, array($value));
					}
					if(!$result){
						$this->isValidateError = true;
					}
				}
				if(!$paramObj->allowNull &&  is_null($value)){
					$this->isValidateError = true;
				}

				$parameters[$i] = $value;
			}
			return $parameters;
		}

		function getOutput($action){
			if(isset($action) && isset($action->actionview) && $action->actionview!= "" && isset($this->views[$action->actionview])){
				//for crack template class
				$output = new Template_Lite;
				$output->force_compile = true;
				$output->compile_check = true;
				$output->cache = false;
				$output->cache_lifetime = 3600;
				$output->config_overwrite = false;
			} else {
				$output = "";
			}
			return $output;
		}

		function &display($sess, $action){
			if(is_object($sess->output)){
				$sess->output->display($this->views[$action->actionview]->resource);
			} else if(is_string($sess->output)){
				echo print_r($sess->output,true);
			}
		}

		function parseXmlConfig($configFile){
			$xmlObject = simplexml_load_file($this->configFile);
			if ($xmlObject === false) {
				echo 'Error while parsing the document';
				exit;
			}
			$this->parseConfig($xmlObject);
		}

		function loadConfig($configFile, $mode=false){
			$this->configFile = $configFile;
			if($mode==1){
				$arr = stat ($configFile);
				$serialModules = 'modules.ser';
				$serialViews = 'views.ser';
				if(!Controller::existSerialze($serialModules)||!Controller::existSerialze($serialViews)){
					$this->parseXmlConfig($this->configFile);
					Controller::saveSerialize($serialModules, $this->getModules());
					Controller::saveSerialize($serialViews, $this->getViews());
				} else {
					$this->modules = Controller::openSerialize($serialModules);
					$this->views = Controller::openSerialize($serialViews);
				}
			} else {
				$this->parseXmlConfig($this->configFile);
			}

		}


		function parseConfig($obj){
				if(is_object($obj->attributes())){
					foreach($obj->attributes() as $name => $value) { 
				}
			}

			foreach ($obj as $name => $value){
				if(strtolower($name)=="module"){
					$ret = $this->parseModule($value);
					$this->modules[$ret->name] = $ret;
				} else if(strtolower($name)=="view"){
					$ret = $this->parseView($value);
					$this->views[$ret->name] = $ret;
				} else {
					$this->parseConfig($value);
				}
			}

		}


		function parseModule($obj){
			$newModule = new Module();
			if(is_object($obj->attributes())){
				foreach($obj->attributes() as $name => $value) { 
					if(strtolower($name)=='name'){
						$newModule->name = (string) $value;
					} else if(strtolower($name)=='resource'  && isset($value) && $value!=""){
						$newModule->resource = (string) $value;
					} 
				}
			}

			foreach ($obj as $name => $value){
				if(strtolower($name)=="actions"){
					$newModule->actions= $this->parseAction($value);
				}
			}
			return $newModule;
		}

		function parseAction($obj){
			$arrAction = array();
			foreach ($obj as $name => $action){
				if(strtolower($name)=="action"){
					$newAction = new Action();

					if(is_object($action->attributes())){			
						foreach($action->attributes() as $key => $value) { 
							if(strtolower($key)=='name'){
								$newAction->name = (string) $value;
							} else if(strtolower($key)=='classname'  && isset($value) && $value!=""){
								$newAction->classname = (string) $value;
							} else if(strtolower($key)=='method'  && isset($value) && $value!=""){
								$newAction->method = (string) $value;
							} else if(strtolower($key)=='resource'  && isset($value) && $value!=""){
								$newAction->resource = (string) $value;
							} 
						}
					}

					foreach($action as $actionChild_name => $actionChild_value){
						if(strtolower($actionChild_name)=="actionview"){
							foreach($actionChild_value->attributes() as $key => $value) { 
								if(strtolower($key)=='view'){
									$newAction->actionview = (string) $value;
								}
							}

						} else if(strtolower($actionChild_name)=="parameters"){
							foreach ($actionChild_value as $param_name => $param){
								if(strtolower($param_name)=='param'){
									$newAction->parameters[] = $this->parseParameter($param);
								} else if(strtolower($param_name)=='onvalidateerror'){
									$newAction->onValidateError = $this->parseValidateErrorException($param);
								}
							}
						}
					}
					$arrAction[$newAction->name] = $newAction;
				}
			}
			return $arrAction;
		}


		function parseParameter($param){
			$newParam = new Parameter();
			if(is_object($param->attributes())){			
				foreach($param->attributes() as $key => $value) {
					$value = (string) $value;
					if(strtolower($key)=='name'){
						$newParam->name = $value;
					} else if(strtolower($key)=='src'  && isset($value) && $value!=""){
						$newParam->src = (!isset($value)|| $value=="")?"_REQUEST":strtoupper($value);
					} else if(strtolower($key)=='value'  && isset($value) && $value!=""){
						$newParam->value = $value;
					} else if(strtolower($key)=='type'  && isset($value) && $value!=""){
						$newParam->type = (!isset($value)||$value=="")?"dynamic":strtolower($value);
					} else if(strtolower($key)=='allownull'  && isset($value) && $value!=""){
						$newParam->allowNull = (!isset($value)||$value=="")?true:(strtoupper($value)=="YES"||strtoupper($value)=="TRUE")?true:false;
					} else if(strtolower($key)=='defaultvalue'  && isset($value) && $value!=""){
						$newParam->defaultValue = (string) $value;
					} else if(strtolower($key)=='forcevalidate'  && isset($value) && $value!=""){
						$newParam->forceValidate = (!isset($value) || $value=="")?false:(strtoupper($value)=="YES" || strtoupper($value)=="TRUE")?true:false;
					}
				}
			}


			foreach($param as $paramChild_name => $paramChild_value){
				if(strtolower($paramChild_name)=="validater"){
					$newParam->validater = new Validater();
					foreach($paramChild_value->attributes() as $key => $value) { 
						$value = (string) $value;
						if(strtolower($key)=='classname'  && isset($value) && $value!=""){
							$newParam->validater->classname =  $value;
						} else if(strtolower($key)=='method'  && isset($value) && $value!=""){
							$newParam->validater->method =  $value;
						} else if(strtolower($key)=='resource' && isset($value) && $value!=""){
							$newParam->validater->resource =  $value;
						}
					}
				}
			}

			if(!isset($newParam->validater) && is_null($newParam->validater)){
				$newParam->validater = new Validater();
				$newParam->validater->useSystemValidate = true;
			}



			return $newParam;
		}

		function parseValidateErrorException($obj){
			$exceptor = new ValidateErrorException();
			foreach($obj->attributes() as $key => $value){
				$value = (string) $value;
				if(strtolower($key)=='module'  && isset($value) && $value!=""){
					$exceptor->module =  $value;
				} else if(strtolower($key)=='action'  && isset($value) && $value!=""){
					$exceptor->action =  $value;
				} else if(strtolower($key)=='classname'  && isset($value) && $value!=""){
					$exceptor->classname =  $value;
				} else if(strtolower($key)=='method'  && isset($value) && $value!=""){
					$exceptor->method =  $value;
				} else if(strtolower($key)=='resource'  && isset($value) && $value!=""){
					$exceptor->resource =  $value;
				}				
			}
			return $exceptor;
		}


		function parseView($obj){
			$newView = new View();
			if(is_object($obj->attributes())){
				foreach($obj->attributes() as $name => $value) { 
					if(strtolower($name)=='name'){
						$newView->name = (string) $value;
					} else if(strtolower($name)=='resource'  && isset($value) && $value!=""){
						$newView->resource = (string) $value;
					} 
				}
			}
			return $newView;
		}


		function getModules(){
			return $this->modules;
		}

		function getViews(){
			return $this->views;
		}

		// include Serializer
		function &saveSerialize($filename, $object){
			if($f = @fopen($filename,"w")) {
			   if(@fwrite($f,serialize($object))) {
				   @fclose($f);
			   } else die("Could not write to file ".$this->filename." at Persistant::save");
			} else die("Could not open file ".$this->filename." for writing, at Persistant::save");
		}

		function &openSerialize($filename){
			$handle = @fopen($filename, "r");
			$contents = @fread($handle, filesize($filename));
			@fclose($handle);
			return unserialize($contents);
		}

		function &existSerialze($filename){
			if(is_file($filename)){
				return true;
			} else {
				return false;
			}
		}

	}

?>
Return current item: Php open model view controller