<?php
/* Author: Leon Bukhman *******************************************/
/* System Base Class **********************************************/

/******************************************************************
This class provides the basic functionality for the application.
All pages must inherit from this class. This class is abstract and
cannot be instantiated.

Page Rendering Sequence:
    init()
    authenticate()
    if needed handleFormEvents()
    if needed printPage()
    destroy()

Public Methods (abstract):
    init()
        This is the first function called when page is rendered

    authenticate()
        Authenticate the page
        Returns Boolean

    handleFormEvents()
        used for form validation and processing
        
    printPage()
        sends page layout to client brouser
        
    destroy()
        close DB connection if needed
        
Public Methods (other):
    function redirect($page)
        redirect to another page in local domain
        stores where you came from in $_SESSION['LastPage']

Usage:
    Make a new class which inherits from this one
    The page can be optimazed using the following constants:
    1) define('NO_DB', 1) if persistent DB connection is not needed
        Pear DB will not be included and connection will not be made
    2) define('NO_PRINT', 1) if page does not get rendered

Requires:
    functions.php
    DB.php
    constants.php
    class_user.php
    class_template.php
    class_form.php

*******************************************************************/

session_start();

//includes (works with nested relative paths)
$path dirname(__FILE__);
if (!
defined('NO_DB'))
    require_once 
'DB.php';              //pear db class
require_once "$path/constants.php";     //defines
require_once "$path/functions.php";     //generic functions
require_once "$path/class_user.php";    //session handling
require_once "$path/class_template.php";//template handling
require_once "$path/class_form.php";    //form handling

class SystemBase {
    
/*Local Atributes**********************************************/
    
var $db   null;        // pointer to PEAR::DB object
    
var $user null;        // pointer to user object
    
var $page null;        // pointer to a page template
    
var $form null;        // pointer to form object

    
var $template '';      // path to page template

    /*Constructor/Destructor***************************************/
    
function SystemBase($template='generic.inc.php') {
        
//prevent instantionation of this class (force abstract)
        
if (!is_subclass_of($this,'SystemBase')) {
            
trigger_error('Base instantiation from non subclass',E_USER_ERROR);
            return 
NULL;
        }
        
        
//initialize attributes
        
if (!defined('NO_DB'))
            
$this->db db_connect();
        
$this->user = new User($this->db);
        if (
$template == 'generic.inc.php')
            
$this->page = new Template(APP_PATH.TEMPLATE_PATH);
        else
            
$this->page = new Template(TEMPLATE_PATH);
        
        
$this->template $template;
        
        
//run the page using abstract methods
        
$this->init();
        
$this->authenticate();
        if (!
is_null($this->form))
            
$this->handleFormEvents();
        if (!
defined('NO_PRINT'))
            
$this->printPage();
        
$this->destroy();
    }

    function 
destroy() {
        if (!
defined('NO_DB'))
            
$this->db->disconnect();
    }

    
/*Public Methods***********************************************/
    //This is the first function called by the constructor
    
function init() {}          //abstract

    //Authenticate the page
    
function authenticate() {}  //abstract

    //print page layout
    
function printPage() {      //abstract
        
if (!is_null($this->form)) {
            
$form $this->form->DisplayForm();
            
$this->page->set('form',$form);
        }
        echo 
$this->page->fetch($this->template);
    }

    
//handles form events
    
function handleFormEvents() {} //abstract

    //redirect to another page() and store where you came from
    
function redirect($page$force=false) {
        
//store where you came from in the session
        
if ($_SERVER['QUERY_STRING'] != '') {
            
$_SESSION['LastPage'] = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        } else {
            
$_SESSION['LastPage'] = $_SERVER['PHP_SELF'];
        }
        if (
$forceheader("HTTP/1.1 301 Moved Permanently");
        
header("Location: $page");
        
$this->destroy();
        exit();
    }

//end of class SystemBase
?>