Location: PHPKode > projects > ArasPhp Framework > ArasPhp-0.03/core/libs/Controller.php
<?php

/***************************************************************
 * ARASPHP WEB DEVELOPING FRAMEWORK
 * 
 * Website:	www.arasphp.org
 * Author:	Arturo López Pérez
 *			hide@address.com
 * Version: 	0.02
 ***************************************************************
 *
 * This file it's part of ArasPhp Web developing framework.
 *
 * This project is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This project is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * AppController implements all functions that should be shared between all Controllers
 * A view has to return an associative array to the caller, and must implement the method to append things to it
 * The parameters sent to the action has a max of 10. (What the heck is gonna take more than that?)
 */
 
 abstract class Controller extends ErrorLogger {
 
 	// To return
 	public $legacy = array();

	// Error tracking returned in the legacy array
	public $errors = NULL;
	public $dataErrors = NULL;
	public $flash = NULL;
	
	// Title for layout
	public $title_for_layout;
 	
 	/**
 	 * Appends whatever received to the associative array
 	 */
 	 
 	 protected function set($index, $whatever)
 	 {
 	 	$this->legacy[$index] = $whatever;
 	 }
 	
 	/**
 	 * Executes the desired action passing the received arguments up to 10
 	 * @param string action to perform in the controller
 	 */
 	 
 	 public function execute($Action, $args = array())
 	 {
 	 	/*
 	 	* We call the action switching the parameters
 	 	*/
 	 	$amount = count($args);
 	 	
 	 	// We try to execute it
 
 	 	switch($amount)
 	 	{
 	 		case 0: 
 	 		$this->$Action(); break;
 	 		
 	 		case 1: 
 	 		$this->$Action($args[0]); break;
 	 		
 	 		case 2: 
 	 		$this->$Action($args[0], $args[1]); break;
 	 		
 	 		case 3:
 	 		$this->$Action($args[0], $args[1], $args[2]); break;
 	 		
 	 		case 4:
 	 		$this->$Action($args[0], $args[1], $args[2], $args[3]); break;
 	 		
 	 		case 5:
			$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4]); break; 	 		
 	 		
 	 		case 6:
			$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]); break; 	 	 		
 	 		case 7:
 	 		$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], 
 	 					$args[6]); break; 	 	 		
 	 		case 8:
			$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], 
						$args[6], $args[7]); break; 	 	 		 	 		
 	 		case 9:
			$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], 
						$args[6], $args[7], $args[8]); break; 	  	 		
 	 		case 10:
 	 		$this->$Action($args[0], $args[1], $args[2], $args[3], $args[4], $args[5], 
 	 					$args[6], $args[7], $args[8], $args[9]); break; 	
 	 		default: echo "Critical error: More than 10 arguments passed."; exit();  	 		
 	 		
 	 	}

 	 	return $this->giveBack();
 	 }
 	 
 	 /**
 	  * Adds a flash message to the controller
 	  * @param string message OR array of strings
 	  */
 	  
 	  protected function addFlash($msg)
 	  {
 	  	if(is_array($msg))
 	  	{
 	  		foreach($msg as $item)
 	  		{
 	  			$this->flash[] = $item;
 	  		}
 	  		
 	  	} else {
 	  	
 	  		$this->flash[] = $msg;
 	  	
 	  	}
 	  }
 	  
 	 /**
 	  * Adds a error message to the controller
 	  * @param string message OR array of strings
 	  */
 	  
 	  protected function addError($msg)
 	  {
 	  	if(is_array($msg))
 	  	{
 	  		foreach($msg as $item)
 	  		{
 	  			$this->errors[] = $item;
 	  		}
 	  		
 	  	} else {
 	  	
 	  		$this->errors[] = $msg;
 	  	
 	  	}
 	  	
 	  	// We set error title
 	  	$this->setTitle(TITLE . CONNECTOR . "An error has ocurred");
 	  }
 	  
 	 /**
 	  * Adds a data validation message to the controller
 	  * @param string message OR array of strings
 	  */
 	  
 	  protected function addDataError($msg)
 	  {
 	  	if(is_array($msg))
 	  	{
 	  		foreach($msg as $item)
 	  		{
 	  			$this->dataErrors[] = $item;
 	  		}
 	  		
 	  	} else {
 	  	
 	  		$this->dataErrors[] = $msg;
 	  	
 	  	}
 	  }
 	  
 	  /**
 	  * @return wether dataError failed
 	  */
 	  
 	  public function dataErrorsOk()
 	  {
 	  	if($this->dataErrors == NULL)
 	  	{
 	  		return true;
 	  	} else {
 	  	
 	  		return false;
 	  	
 	  	}
 	  }
 	  
 	  /**
 	  * Sets the layout title
 	  * @param string title
 	  */
 	  
 	  protected function setTitle($title)
 	  {
 	  	$this->title_for_layout = $title;
 	  }
 	  
 	   /**
 	  * Forces to load anoher view instead of the associated one
 	  * No error checking. Caution.
 	  *
 	  * (!) Used by static controller
 	  * @param string name of the view
 	  */
 	  
 	  protected function setView($name)
 	  {
 	  	$this->set("setView", VIEW . $name . ".php");
 	  }
 	  
 	  /**
 	  * Forces to load another layout.
 	  * No error check. Caution
 	  */
 	  
 	  protected function setLayout($name)
 	  {
 	  	$this->set("setLayout", LAYOUT . $name . ".php");
 	  }	
 	  
 	 /**
 	  * Adds all messages generated to the array and returns it
 	  * @return asocciatve array
 	  */
 	  
 	 public function giveBack()
	{
		self::set('errors', $this->errors);
		self::set('dataErrors', $this->dataErrors);
		self::set('flash', $this->flash);
		self::set('title_for_layout', $this->title_for_layout);
		return $this->legacy;
	}
 	 
 }

?>
Return current item: ArasPhp Framework