File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/lib/class.permission.php
<?php
/**
* This file contains the Permission class, tailored for storage provided by the app it is embedded into (in this case, IWP)
*
* @package IWP
* @subpackage Permissions
*/
define('PermissionError', 1);
define('PermissionWarning', 2);
define('PermissionNotice', 3);
/**
* Permission class
*
* This class contains core permission data model class for IWP
*
* @package IWP
* @subpackage Permissions
*/
class Permission extends iwp_engine
{
protected $data = array();
protected $table;
protected $_db;
protected $tableFields = array(
"permid",
"scopetype",
"itemcsv",
"granularity",
"flag",
"scopename",
"scopeitem"
);
protected $primaryKey = "permid";
protected $validation = array();
public function __construct() { }
/**
* To handle working with member variables for the permission
*
* @param String $name The name of the item
* @param Mixed $val The value to set the item to
* @throws PermissionException
*/
public function Set($name, $val)
{
if (!in_array($name, $this->tableFields)) {
throw new PermissionException("Table field does not exist.", PermissionError);
}
if ($name === "permid" && !is_int($val)) {
throw new PermissionException("The Permission ID field requires an Integer.", PermissionError);
}
$this->data[$name] = $val;
}
/**
* Will retrieve the variable
*
* @throws PermissionException
*/
public function Get ($name)
{
if (!isset($this->data[$name])) {
throw new PermissionException('Invalid variable "'. $name .'" retrieved.', PermissionWarning);
}
return $this->data[$name];
}
/**
* To set the name of the table in the database to be used
*
* @param String $table The table name in use
* @throws PermissionException
*/
public function setTable ($table)
{
if (is_string($table)) {
$this->Set('table', $table);
} else {
throw new PermissionException("Table name requires a string.", PermissionError);
}
}
/**
* Will specify the DB object to be used
*
* @param Object $db The database object in use
* @throws PermissionException
*/
public function setDb (&$_db)
{
if (is_object($_db)) {
$this->_db = $_db;
} else {
throw new PermissionException("A valid database object is required.", PermissionError);
}
}
/**
* Will save the current permission to the database
*
* @return Integer permission id of updated or inserted permission
*/
public function Save ()
{
// Check to ensure all data is set
if(!$this->isComplete()) {
throw new PermissionException("All data for the permission must be set", PermissionError);
}
if ($this->data['permid'] > 0) {
// update existing permission
$permid = $this->data['permid'];
unset($this->data['permid']);
if(!$this->db->UpdateQuery(IWP_TABLE_GROUPPERMS, $this->data, "`permid`='" . $this->db->Quote($permid) . "'")) {
throw new PermissionException($this->db->GetErrorMsg());
} else {
return $permid;
}
} else {
// insert new permission
if(!($id = $this->db->InsertQuery(IWP_TABLE_GROUPPERMS, $this->data))) {
throw new PermissionException(GetLang('PermissionSaveProblem') . $this->db->GetErrorMsg());
} else {
return $id;
}
}
/*
$sql = '';
if (is_null($this->data['permid'])) {
// Creating the permission
$sql = 'INSERT INTO ' . $this->Get('tableName') . ' (`scopetype`) VALUES (\'' . $this->db->Quote($this->Get('scopetype')) . '\')';
} else {
// Running an update
$sql = 'UPDATE ' . $this->Get('tableName') . ' SET `scopetype`=\'' . $this->db->Quote($this->Get('scopetype')) . '\', ``=\'\' WHERE `permid`=\'' . $this->db->Quote($this->Get('permid')) . '\'';
}
// Run the query
$this->runQuery($sql);
*/
}
/**
* Checks the contents of the data array and ensures values have been added where necessary
*
* @return Boolean
*/
private function isComplete ()
{
reset($this->tableFields);
while(list(,$val) = each($this->tableFields)) {
switch ($val) {
case "permid":
case "itemcsv":
case "catcsv":
case "granularity":
break;
default:
if (!$this->Get($val) || $this->Get($val) == "")
return false;
break;
}
}
reset($this->tableFields);
return true;
}
/**
* Will load permission data from the
*
* @param String|Integer $permission The id to load from the DB
* @return Boolean Success / Failure
* @throws PermissionException
*/
public function Load ($permission)
{
// Check for type of $permission
if(is_int($permission)) {
// Load using ID
}
// Carry out query
// Load into member vars
// Return success
}
/**
* Will run the provided SQL, this function is designed to be changed in a single location to make life easier for use with different DB objects
*
* @param String $sql The SQL statement to be run
* @return Resource The result set from the provided query
* @throws PermissionException
*/
private function runQuery ($sql)
{
if(!is_object($this->db)) {
throw new PermissionException("A valid database object must be loaded.", PermissionError);
}
$res = $this->db->Query($sql);
return $res;
}
/**
* Fetch a row from a result resource
*
* @param resource $resource The resource to fetch the next result from
* @return Array The next row from the result
* @throws PermissionException
*/
public function fetchResult (&$resource)
{
// Fetch the next result
$res = $this->_db->Fetch($resource);
return $res;
}
}
/**
* Permission Exception class
*
* This class contains the core permission exception class for IWP
*
* @package IWP
* @subpackage Permissions
*/
class PermissionException extends Exception
{
/**
* A variable to hold the severity of the error
*
* @var String $severity
*/
private $severity;
/**
* Changing the constructor to handle the severity
*
* @param String $message The exception message being thrown
* @param Integer $severity The degree of severity for this exception
*/
public function __construct ($message, $severity)
{
$this->message = $message;
$this->severity = $severity;
}
/**
* Will retrieve the severity that was placed for this exception
*
* @return Integer The severity of the exception
*/
public function getSeverity ()
{
return $this->severity;
}
}