Location: PHPKode > projects > QuickerBooks > QuickerBooks/index.php
<?php /* Created on Jan 14, 2006 */
 
// load Classes
require_once("autoload.php");

$main = new Main();
$main->start($_REQUEST);

/**
 * - Requests Come In:
 *    - example: Main.php?page={page}[&action={action}][&ajax=0|1]
 * 
 * - Request is mapped to the controller
 *    - page => controller (default controller will be called if no controller is specified)
 *    - action => method (default action will be called if no action is specified)
 *    - ajax is turned on or off on the controller object
 * 
 * - the controller will perform actions and set variables for smarty
 * 
 * - if a page is in the html location, it will be loaded with smarty and sent out
 *    - Normal pages are fetched in public/html
 *    - Ajax pages are fetched in public/ajax
 * 
 * - finished
 * 
 * @since 1 - Jun 13, 2007
 */
class Main {
	
	private $options;
	public static $me;
	
	public function __construct() {
		session_start();
		Main::$me = $this;
	}
	
	public function start($params) {
		$page = '';
		
		try {
			// load up the config file
			$options = new PropertyFile('config/config.ini');
			$this->options = $options;
			
			// load the database
			Database::init(
				$options->getProperty('Database_Host'),
				$options->getProperty('Database_User'),
				$options->getProperty('Database_Password'),
				$options->getProperty('Database'));
			
			// do we have a controller?
			$controller = self::getValue($params, 'Controller');
			$controllerClass = $controller.'Controller';
							
			// load controller
			if (@include('controllers/'.$controllerClass.'.php')) {
				$controllerClass = new $controllerClass();
				$smarty = new Smarty();
				$smarty->template_dir = 'public/html/';
				$smarty->compile_dir = 'smarty/compiled/';
				$smarty->plugins_dir = array(
					'smarty/plugins/',
					'C:\Documents and Settings\John Leith\My Documents\wwwroot\php\PEAR\Smarty\libs\plugins'
				);
				//$smarty->left_delimiter = '<s:';
				//$smarty->right_delimiter = '>';
				if ($options->getProperty('Debug') == 1) {
					$smarty->clear_compiled_tpl();
				}
				
				// do we have a action?
				$action = self::getValue($params, 'Action');
				
				// validate user
				$user = Authenticator::update();
				//Debug::display($user->getColumnValues());	
										
				// validate action
				if (! Authenticator::isPermitted($user, $controller, $action)) {
					$controller = $this->options->getProperty('Controller_Default');
					$controllerClass = $controller.'Controller';
					include('controllers/'.$controllerClass.'.php');
					$controllerClass = new $controllerClass();
					$action = $this->options->getProperty('Action_Default');
					Debug::message('Not permitted');
				}	
				
				// perform the action			
				if (is_callable(array($controllerClass, $action))) {
					$pagefile = $controllerClass->$action($params, $smarty, $user);	
				}
				else {
					
					// perform default action
					$action = $this->options->getProperty('Action_Default');
					$pagefile = $controllerClass->$action($params, $smarty, $user);
				}	
				
				//Debug::message('controller: '.$controllerClass.' action: '.$action.' pagefile: '.$pagefile);
				
				// render the page
				if ($pagefile != null) {
					$smarty->assign('this_page', Main::url());
					if ($smarty->template_exists($pagefile)) {
						if ($pagefile != 'index.htm') {
							$smarty->assign('content_template', $pagefile);
						}
						else {
							Debug::message('infinite loop detected! you cannot specify index.htm for the template file');
						}
						$page = $smarty->fetch('index.htm');
					}
					else {
						throw new Exception('Warning: template file `'.$pagefile.'` does not exist!');	
					}
				}
			}
			else {
				$this->clearValue('Controller');
				throw new Exception('There is no controller named `'.$controller.'`.');	
			}
		
		}
		catch (Exception $ex) {
			$page = 'Exception Caught: ' .$ex->getMessage();
		}
			
		print $page;		
	}
		
	/**
	 * looks for a option value first in the params, then session, then default
	 * after the value is had, it will save it to the session
	 */
	public function getValue($params, $name) {
		$default = $this->options->getProperty($name.'_Default');
		$name = $this->options->getProperty($name.'_Name');
		if (isset($params[$name])) {
			$value = $params[$name];
			if ($value != null) {
				$_SESSION[$name] = $value;
			}
		}
		else if(isset($_SESSION[$name])) {
			$value = $_SESSION[$name];
			if ($value != null) {
				$_SESSION[$name] = $value;
			}
		}
		else {
			$value = $default;
		}
		return $value;
	}
		
	public function clearValue($name) {
		$name = $this->options->getProperty($name.'_Name');
		unset($GLOBALS['_SESSION'][$name]); 	
	}	
	
	public static function url($params = array()) {
		$replace = array(
				'c' => Main::$me->options->getProperty('Controller_Name'),
				'a' => Main::$me->options->getProperty('Action_Name'),
			);
		$keys = array_keys($replace);
		
		$url = $_SERVER['PHP_SELF'];
		//Debug::display($_SERVER);
		$count = 0;
		foreach($params as $name=>$value) {
			if (in_array($name, $keys)) {
				$name = $replace[$name];
			}
			($count == 0 ) ? $url .= '?' : $url .= '&';
			$url .= $name . '=' . $value;
			$count += 1;
		}
		return $url;
	}

	public static function redirect($params) {
		header('location: '.Main::url($params));
		exit;	
	}
}

?>
		
Return current item: QuickerBooks