Skip to content
Snippets Groups Projects
Commit 9d6b08f5 authored by SNA's avatar SNA
Browse files

Evol : ajout de la classe abstraite BaseObject

parent 5e9a3cbf
No related branches found
No related tags found
No related merge requests found
<?php
abstract class BaseObject {
private $data = array();
/**
* Initialize an object.
*/
function __construct(){
}
/**
* Set value of a property of current object.
* @param string $name Name of property to set
* @param object $value Value of property $name
*/
function __set($name, $value){
$this->data[$name] = $value;
}
/**
* Get value of a property of current object.
* @param string $name Name of property to get
* @return string Value of $name
* @exception $e Exception Sent if $name does not exist
*/
function __get($name) {
try {
return $this->data[$name];
} catch (Exception $e) {
echo 'Exception catched: '.$e->getMessage().', null returned<br/>';
return null;
}
}
public function __isset($name) {
if (isset($this->data[$name])) {
return (false === empty($this->data[$name]));
} else {
return false;
}
}
public function getArray() {
return $this->data;
}
abstract function toString();
}
?>
<?php
try {
require_once("core/class/BaseObject.php");
} catch (Exception $e){
echo $e->getMessage().' // ';
}
class Security extends BaseObject() {
function __toString(){
return $this->maarch_comment ;
}
}
?>
<?php
define ("_DEBUG", false);
/*
define("_CODE_SEPARATOR","/");
define("_CODE_INCREMENT",1);
*/
try {
require_once("core/class/class_db.php");
require_once("core/class/Security.php");
} catch (Exception $e){
echo $e->getMessage().' // ';
}
class SecurityControler{
private static $db;
private static $security_table;
public function connect()
{
$db = new dbquery();
$db->connect();
self::$db=$db;
}
public function disconnect()
{
self::$db->disconnect();
}
public function get($security_id)
{
if(empty($security_id))
{
// Nothing to get
return null;
}
// Querying database
$query = "select * from ".self::$security_table." where security_id = ".$security_id;
try{
if(_DEBUG){echo $select.' // ';}
self::$db->query($select);
} catch (Exception $e){
echo _NO_ACCESS_WITH_ID.' '.$security_id.' // ';
}
// Constructing object
$access=new Security();
$queryResult=self::$db->fetch_object();
foreach($queryResult as $key => $value){
$access->$key=$value;
}
return $access;
}
public function get_access_for_group($group_id)
{
if(empty($group_id))
{
// Nothing to get
return null;
}
// Querying database
$query = "select * from ".self::$security_table." where group_id = '".$group_id."'";
try{
if(_DEBUG){echo $select.' // ';}
self::$db->query($select);
} catch (Exception $e){
echo _NO_GROUP_WITH_ID.' '.$group_id.' // ';
}
$security = array();
while($queryResult = self::$db->fetch_object())
{
$access=new Security();
foreach($queryResult as $key => $value){
$access->$key=$value;
}
array_push($security, $access);
}
return $security;
}
public function save($security_id){
if($security->security_id > 0){
self::update($security);
} else {
self::insert($security);
}
}
private function insert($security)
{
$prep_query = self::insert_prepare($security);
// Inserting object
$query="insert into ".self::$security_table." ("
.$prep_query['COLUMNS']
.") values("
.$prep_query['VALUES']
.")";
try{
if(_DEBUG){ echo $query.' // '; }
self::$db->query($query);
} catch (Exception $e){
echo _CANNOT_INSERT_ACCESS." ".$security->toString().' // ';
}
}
private function update($security)
{
$query="update ".self::$security_table." "
.self::update_prepare($security)
." where security_id=".$security->security_id;
try{
if(_DEBUG){echo $query.' // ';}
self::$db->query($query);
} catch (Exception $e){
echo _CANNOT_UPDATE_ACCESS." ".$security->toString().' // ';
}
}
public function delete($security_id){
$query="delete from ".self::$security_table." where security_id=".$security_id;
try{
if(DEBUG){echo $query.' // ';}
self::$db->query($query);
} catch (Exception $e){
echo _CANNOT_DELETE_SECURITY_ID." ".$security_id.' // ';
}
}
private function update_prepare($security)
{
$prep_query = array('COLUMNS' => '', 'VALUES' => '');
$result=array();
foreach($security->getArray() as $key => $value)
{
// For now all fields in the usergroups table are strings or date excepts the security_id
if(!empty($value))
{
if($key <> 'security_id')
{
$result[]=$key."='".$value."'";
}
}
}
// Return created string minus last ", "
return implode(",",$result);
}
private function insert_prepare($security){
$columns=array();
$values=array();
foreach($security->getArray() as $key => $value)
{
// For now all fields in the usergroups table are strings or date excepts the security_id
if(!empty($value))
{
if($key <> 'security_id')
{
$columns[]=$key;
$values[]="'".$value."'";
}
}
}
return array('COLUMNS' => implode(",",$columns), 'VALUES' => implode(",",$values));
}
}
?>
<?php
try {
require_once("core/class/BaseObject.php");
} catch (Exception $e){
echo $e->getMessage().' // ';
}
class Service extends BaseObject{
public function __toString(){
return $this->name ;
}
}
?>
<?php
define ("_DEBUG", false);
/*
define("_CODE_SEPARATOR","/");
define("_CODE_INCREMENT",1);
*/
try {
require_once("core/class/Service.php");
} catch (Exception $e){
echo $e->getMessage().' // ';
}
class ServiceControler
{
public function get($service_id, $module_id)
{
if(empty($service_id))
{
// Nothing to get
return null;
}
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment