getMessage().' // '; } /** * @brief Controler of the Resources Object * * */ class ResourcesControler { /** * Dbquery object used to connnect to the database */ static $db; /** * Resources table */ static $resources_table; /** * Opens a database connexion and values the tables variables */ public function connect() { $db = new dbquery(); $db->connect(); self::$resources_table = $_SESSION['ressources'][0]['tablename']; self::$db=$db; } /** * Close the database connexion */ public function disconnect() { self::$db->disconnect(); } /** * Returns an Resources Object based on a resources identifier * * @param $res_id bigint Resources identifier * @return Resources object with properties from the database or null */ public function get($res_id) { // If no res_id specified return null if(empty($res_id)) return null; self::connect(); $query = "select * from ".self::$resources_table." where res_id = '".$res_id."' "; if(!$can_be_disabled) $query .= " and enabled = 'Y'"; try{ if($_ENV['DEBUG']) echo $query.' // '; self::$db->query($query); } catch (Exception $e){ echo _NO_RESOURCE_WITH_ID.' '.$res_id.' // '; } if(self::$db->nb_result() > 0) { $resources=new Resources(); $queryResult=self::$db->fetch_object(); foreach($queryResult as $key => $value){ $resources->$key=$value; } self::disconnect(); return $resources; } else { self::disconnect(); return null; } } /** * Saves in the database a Resources object * * @param $resources Resources object to be saved * @param $mode string Saving mode : add or up * @return bool true if the save is complete, false otherwise */ public function save($resources, $mode) { if(!isset($resources)) return false; if($mode == "up") return self::update($resources); elseif($mode =="add") return self::insert($resources); return false; } /** * Inserts in the database (Resources table) a Resources object * * @param $resources Resources object * @return bool true if the insertion is complete, false otherwise */ private function insert($resources) { if(!isset($resources)) return false; self::connect(); $prep_query = self::insert_prepare($resources); // Inserting object $query="insert into ".self::$resources_table." (" .$prep_query['COLUMNS'] .") values(" .$prep_query['VALUES'] .")"; try{ if($_ENV['DEBUG']){ echo $query.' // '; } self::$db->query($query); $ok = true; } catch (Exception $e){ echo _CANNOT_INSERT_RESOURCES." ".$resources->toString().' // '; $ok = false; } self::disconnect(); return $ok; } /** * Updates a Resources in the database (Resources table) with a Resources object * * @param $resources Resources object * @return bool true if the update is complete, false otherwise */ private function update($resources) { if(!isset($resources) ) return false; self::connect(); $query="update ".self::$resources_table." set " .self::update_prepare($resources) ." where res_id='".$resources->res_id."'"; try{ if($_ENV['DEBUG']){echo $query.' // ';} self::$db->query($query); $ok = true; } catch (Exception $e){ echo _CANNOT_UPDATE_RESOURCES." ".$resources->toString().' // '; $ok = false; } self::disconnect(); return $ok; } /** * Prepares the update query for a given Resources object * * @param $resources Resources object * @return String containing the fields and the values */ private function update_prepare($resources) { $result=array(); foreach($resources->getArray() as $key => $value) { // For now all fields in the Resources table are strings if(!empty($value)) { $result[]=$key."='".$value."'"; } } return implode(",",$result); } /** * Prepares the insert query for a given Resources object * * @param $resources Resources object * @return Array containing the fields and the values */ private function insert_prepare($resources) { $columns=array(); $values=array(); foreach($resources->getArray() as $key => $value) { // For now all fields in the Resources table are strings if(!empty($value)) { $columns[]=$key; $values[]="'".$value."'"; } } return array('COLUMNS' => implode(",",$columns), 'VALUES' => implode(",",$values)); } /** * Asserts if a given Resources (res_id) exists in the database * * @param $res_id String Resources identifier * @return bool true if the Resources exists, false otherwise */ public function resExists($res_id) { if(!isset($res_id) || empty($res_id)) return false; self::connect(); $query = "select res_id from ".self::$resources_table." where res_id = '".$res_id."'"; try{ if($_ENV['DEBUG']){echo $query.' // ';} self::$db->query($query); } catch (Exception $e){ echo _UNKNOWN._RESOURCES." ".$res_id.' // '; } if(self::$db->nb_result() > 0) { self::disconnect(); return true; } self::disconnect(); return false; } /** * Convert an object to an array * @param $object object to convert */ public function object2array($object) { $return = NULL; if(is_array($object)) { foreach($object as $key => $value) { $return[$key] = $this->object2array($value); } } else { if(is_object($object)) { $var = get_object_vars($object); if($var) { foreach($var as $key => $value) { $return[$key] = ($key && !$value) ? NULL : $this->object2array($value); } } else return $object; } else return $object; } return $return; } /** * Load into session vars all the config of resources */ public function loadResourcesConfig($path) { return $this->object2array(simplexml_load_file($path)); } } ?>