Code: Select all
<?php
/****************************************
* *
* Creator: --- --- *
* Contact: shiznatix AT gmail DOT com *
* Drink: Beefeater and Tonic *
* *
****************************************/
class Base
{
public $db; //Database class
//public $Data;
public $Extensions; //Extensions class
public $Errors; //Errors array
public $View = ''; //Current View
//private $Config; //Configuration file to find other files
/**
* Just gets out db and extensions classes kiddies!
* This function is not always called as it is never initiated or whatever
*/
public function __construct()
{
global $db;
$this->db =& $db;
global $Extensions;
$this->Extensions =& $Extensions;
}
/**
* This function allows for getting a list from a database easily
* So we can stay away from writing the same queries over and over.
*
* @param fields: is the fields from the databse we are selecting
* @param conditions: is the "this = that" stuff
* @param mode: how to join the conditions together
* @param tail: the end of the query such as 'LIMIT' or whatever
* @return array of rows from fetch_assoc()
*/
public function GetList($fields = null, $conditions = null, $mode = 'AND', $tail = null)
{
$this->db->Select($this->Table, $fields, $conditions, $mode, $tail);
return $this->db->GetDBVarsList(get_class($this));
}
/**
* This function will get a list from a database easily
* then set the values from the DB to the Model class variables
* automatically.
*
* @param fields: is the fields from the databse we are selecting
* @param conditions: is the "this = that" stuff
* @param mode: how to join the conditions together
* @param tail: the end of the query such as 'LIMIT' or whatever
* @return true/false if it fails or not (she never fails to get the job done... OH!)
*/
public function Load($conditions = null, $mode = 'AND', $tail = null)//$tail = ' LIMIT 1 '
{
$this->db->Select($this->Table, false, $conditions, $mode, $tail);
$DBVars = $this->db->GetDBVars();
if (empty($DBVars))
{
$Exempt = array('db', 'Data', 'Extensions', 'Errors', 'View', 'Config', 'Table');
$ClassVars = get_class_vars(get_class($this));
foreach ($ClassVars as $key => $val)
{
if (!in_array($key, $Exempt))
{
$this->$key = '';
}
}
return false;
}
$ClassVars = get_class_vars(get_class($this));
foreach ($ClassVars as $key => $val)
{
if (array_key_exists($key, $DBVars))
{
$this->$key = $DBVars[$key];
}
}
return true;
}
/**
* Gets the count of affected or returned rows from
* the last executed query
*
* ToDo('Add optional query resource to get information from previous queries');
*
* @return int count
*/
public function GetCount()
{
return $this->db->GetCount();
}
/**
* Gets the last inserted Id for that db connection
*
* @return int Id
*/
public function GetLastInsertId()
{
return $this->db->GetLastInsertId();
}
/**
* This class will include then load another class (Module)
* All class objects are stored in the singleton ObjectStorage
*
* ToDo('Make sure that the class file exists before getting it');
*
* @param class is the name of the class to load (from the config file)
* @param leve is the Business, Model, or View level
*
* @return newly created object
*/
public function LoadClass($class, $level)
{
global $Config;
require_once $Config[$class]['Path'].$class.'.'.$level.'.class.php';
$ClassName = $class.$level;
$obj = ObjectStorage::GetObject($ClassName);
if (false !== $obj)
{
return $obj;
}
else
{
$obj = new $ClassName;
ObjectStorage::AddObject($ClassName, $obj);
return $obj;
}
}
/**
* This function will automatically set the values of the
* class vars that call it to the $vars (usually $_POST or $_GET)
*
* @param vars the array of variables to set to the class variables
*/
public function AutoSet($vars)
{
if (is_array($vars))
{
$ClassVars = get_class_vars(get_class($this));
foreach ($vars as $key => $val)
{
if (array_key_exists($key, $ClassVars))
{
$this->Set($key, $val);
}
}
return true;
}
else
{
return false;
}
}
/**
* This function will set a single class variable to a value
* that value can be any type of variable, even an array
*
* @param var is the variable in the class to be set
* @param val is the value you want the variable to be
*/
public function Set($var, $val)
{
$ClassVars = get_class_vars(get_class($this));
if (array_key_exists($var, $ClassVars))
{
if (is_array($val))
{
foreach ($val as $key => $value)
{
$val[$key] = trim($value);
}
}
else
{
$val = trim($val);
}
$this->$var = $val;
return true;
}
else
{
return false;
}
}
/**
* This function deletes a class variable
*
* @param var is the variable to delete
*/
public function DelVar($var)
{
$ClassVars = get_class_vars(get_class($this));
if (array_key_exists($var, $ClassVars))
{
unset($this->$var);
return true;
}
else
{
return false;
}
}
/**
* This function sets the view to be outputted to the user
* it gets the info to output from this modules View level class
*
* @param view the name of the function in the view level class to output
*/
public function SetView($view)
{
$this->View = $view;
}
/**
* This function gets the view to be shown then returns its output
*
* @return the output from the view function
*/
public function ShowView()
{
if ('' === $this->View)
{
$this->View = 'Main';
}
$function = 'View'.$this->View;
return $this->$function();
}
/**
* This function deletes a row from the database. It requires
* that this class have an Id given to it so it can delete only
* a single row at a time from the database to avoid a really stupid
* mistake.
*
* ToDo('be able to give an array of Ids to be deleted');
*
* @param mode is the mode the conditions will be joined together in
* @param tail is the end of the SQL query if you need one
*/
public function Remove($mode = 'AND', $tail = 'LIMIT 1')
{
if (empty($this->Id))
{
trigger_error('Id is required', E_USER_ERROR);
}
else
{
$conditions['Id'] = $this->Id;
$this->db->Remove($this->Table, $conditions, $mode, $tail);
}
}
/**
* This function will save the class variables to the database.
* If an Id exists, then it runs an UPDATE query, otherwise it runs
* an INSERT query
*/
public function Save()
{
$skip = array('Table', 'Data', 'Id', 'db', 'Extensions', 'Errors', 'View');
if (empty($this->Id))
{
foreach ($this as $key => $val)
{
if (!in_array($key, $skip) && '' != $val)
{
$fields[] = $key;
$values[] = $val;
}
}
$this->db->Insert($this->Table, $fields, $values);
}
else
{
foreach ($this as $key => $val)
{
if (!in_array($key, $skip) && '' != $val)
{
$set[$key] = $val;
}
}
$this->db->Update($this->Table, $set, array('Id' => $this->Id));
}
}
/**
* This function gets the value of a class variable
*
* @return the value of the class variable
*/
public function Get($var)
{
$ClassVars = get_class_vars(get_class($this));
if (array_key_exists($var, $ClassVars))
{
return $this->$var;
}
else
{
trigger_error($var.' Does not exist', E_USER_ERROR);
}
}
/**
* This function will load an extension from the extensions class
*
* @param extension is the name of the extension to load
* @param params is any parameters to pass to the extension to load with
* @return extension instance
*/
public function LoadExtension($extension, $params = false)
{
return $this->Extensions->$extension($params);
}
/**
* This function will save an error message to be displayed and/or
* delt with later on in execution
*
* @param message the error message you whish to show
* @param field is the optional field to associate the error with
*/
public function SetError($message, $field = false)
{
$this->Errors[] = $message;
if (false !== $field)
{
$this->Errors['ErrorFields'][] = $field;
}
}
/**
* Returns the array of errors
*/
public function GetErrors()
{
return $this->Errors;
}
/**
* This function will get the errors from another object
* and import them to its own error array
*
* @param object is the object whos errors you want
*/
public function ImportErrors($Object)
{
if (!is_object($Object))
{
trigger_error('Object required but '.gettype($Object).' was given', E_USER_ERROR);
}
if (false !== $Object->isErrors())
{
foreach ($Object->Errors as $val)
{
$this->SetError($val);
}
}
return true;
}
/**
* This function will check for any set errors
*
* @return true if there are errors, false if there are none
*/
public function isErrors()
{
if (is_array($this->Errors))
{
return true;
}
else
{
return false;
}
}
/**
* This function will redirect a user to another page with the option
* of sending variables though the url with them
*
* @param location is the url to send them to
* @param vars is the variable to put in the URL
*/
public function Redirect($location, $vars = false)
{
$tail = '';
if (false !== $vars)
{
if (is_array($vars))
{
$tail .= '?';
foreach ($vars as $key => $val)
{
$tail .= $key.'='.$val.'&';
}
$tail{strlen($tail)-1} = '';//remove the trailing & sign
}
}
header('Location: '.$location.'.php'.$tail);
die();
}
/**
* This function will add anything extra to the header of the HTML document
*/
public function AddHeaderHTML($Data)
{
$GLOBALS['HeadHTML'] .= $Data;
}
/**
* This will check to see if a user is 'logged in' based on
* the return value of function isLoggedIn() returns
*
* @param UserTypeId is the Id of the type of user that is allowed to access this page
*/
public function LoginRequired($UserTypeId = false)
{
if (false === isLoggedIn())
{
$this->Redirect('index');
}
else
{
if (false !== $UserTypeId)
{
if ($UserTypeId !== GetUserType())
{
$this->Redirect('index');
}
else
{
return true;
}
}
else
{
return true;
}
}
}
}
//END!
?>