getMessage().' // '; } define ("_DEBUG", false); define ("_ADVANCED_DEBUG",false); /** * Class for controling mr_aggregation objects from database * data, and vice-versa. * @author boulio * */ class mr_aggregation_controler extends ClassifiedObjectControler implements ObjectControlerIF { /** * Save given object in database: * - make an update if object already exists, * - make an insert if new object. * Return updated object. * @param mr_aggregation $mr_aggregation * @return boolean */ public function save($mr_aggregation){ self::set_foolish_ids(array()); self::set_specific_id(null); if($mr_aggregation->mr_aggregation_id > 0){ // Update existing mr_aggregation return self::update($mr_aggregation); } else { // Insert new mr_aggregation return self::insert($mr_aggregation); } } /////////////////////////////////////////////////////// INSERT BLOCK /** * Add given mr_aggregation to database. * @param mr_aggregation $mr_aggregation */ private function insert($mr_aggregation){ // Giving automatised values $mr_aggregation->mr_aggregation_id=time()*1000+rand()%1000; $mr_aggregation->mr_creation_date=date("Y-m-d H:i"); $parent=self::get($mr_aggregation->parent_mr_aggregation_id); $children=self::getChildren($parent,""); $mr_aggregation->mr_full_classification_code=self::new_full_classification_code($mr_aggregation, $children); $mr_aggregation->mr_classification_code=self::update_relative_code($mr_aggregation); $mr_aggregation->level_number=self::get_level($mr_aggregation->mr_full_classification_code); $mr_aggregation->can_have_records=true; $mr_aggregation->is_filled_with_records=false; // Inserting object $result = self::advanced_insert($mr_aggregation); if(self::check_parent($mr_aggregation)){ // Parent needs to be updated if($parent->can_have_records) { /* * According to one of main MoReq2 rules, parent * cannot have records anymore. */ $parent->can_have_records=false; $result=$result && self::save($parent); } } return $result; } /////////////////////////////////////////////////////// UPDATE BLOCK /** * Update given mr_aggregation informations in database. * @param mr_aggregation $mr_aggregation */ private function update($mr_aggregation){ // Updating automatised values of given object $is_parent_updated=self::check_parent($mr_aggregation); if($is_parent_updated){ $children=self::getChildren($mr_aggregation,""); $mr_aggregation->mr_full_classification_code=self::new_full_classification_code($mr_aggregation,$children); $parent=self::get($mr_aggregation->parent_mr_aggregation_id); if($parent->can_have_records) { /* * According to one of main MoReq2 rules, parent * cannot have records anymore. */ $parent->can_have_records=false; self::save($parent); } $mr_aggregation->level_number=self::get_level($mr_aggregation->mr_full_classification_code); $mr_aggregation->mr_classification_code=self::update_relative_code($mr_aggregation); } // Update given mr_aggregation in database $result = self::advanced_update($mr_aggregation); // Updating children - needs to be done after main object update if($is_parent_updated){ $result = $result && self::update_children($mr_aggregation); } } /** * Update full classification code of given mr_aggregation * and of all children. * Return true if succeeded. * @param mr_aggregation $mr_aggregation * @return boolean */ private function update_children($mr_aggregation){ $children=self::getChildren($mr_aggregation,""); foreach($children as $child){ $child->mr_full_classification_code=$mr_aggregation->mr_full_classification_code._CODE_SEPARATOR.$child->mr_classification_code; $child->level_number=self::get_level($child->mr_full_classification_code); $result = self::advanced_update($child); if(_DEBUG){echo "Child: ".$child->mr_title." updated // ";} $result = $result && self::update_children($child); } return $result; } /** * Check parent format. * Return true if any update occurred. * * @param mr_aggregation $mr_aggregation * @return Boolean */ private function check_parent($mr_aggregation){ if(empty($mr_aggregation->parent_mr_aggregation_id)){ $mr_aggregation->parent_mr_aggregation_id=0; } // Checking former parent $former_parent=self::getParent($mr_aggregation->mr_aggregation_id); $update=false; if(is_null($former_parent)){ if($mr_aggregation->parent_mr_aggregation_id>0){ $update=true; } } else { if($mr_aggregation->parent_mr_aggregation_id!=$former_parent->mr_aggregation_id){ $update=true; } } if(_ADVANCED_DEBUG){ echo "Parent update needed: ".$update." // "; } return $update; } /** * Return last particule of given mr_aggregation * full classification code. * MAY BE TRANSFERRED within ClassifiedObjectAbstract * @param mr_aggregation $mr_aggregation * @return string */ private function update_relative_code($mr_aggregation){ // There is at least one separator position to indicate root $separatorPos=strrpos($mr_aggregation->mr_full_classification_code,_CODE_SEPARATOR); return substr($mr_aggregation->mr_full_classification_code,$separatorPos+1); /* Test regexp to be upgraded… return preg_replace("|(^([^/]+)$|/(.+?)$)|","$1",$full_classification_code); */ } /////////////////////////////////////////////// GET BLOCK /** * Get mr_aggregation with given id. * Can return null if no corresponding object. * @param $id Id of mr_aggregation to get * @return mr_aggregation */ public function get($id) { self::set_foolish_ids(array()); self::set_specific_id(null); return self::advanced_get($id,_AGGREGATION_TABLE_NAME); } /** * Get children of mr_aggregation with given id. * @param mr_aggregation $mr_aggregation Parent mr_aggregation * @param string $orderBy Column to order result * @return mr_aggregation[] */ public function getChildren($mr_aggregation, $orderBy){ return self::advanced_getChildren($mr_aggregation,$orderBy); } /** * Give parent of mr_aggregation with given id. Can return : * - null if top mr_aggregation of serial mr_aggregation * - null if top mr_aggregation of it * - else mr_aggregation * @param Long $mr_aggregation_id * @return mr_aggregation */ public function getParent($mr_aggregation_id){ if(empty($mr_aggregation_id)){ // Nothing to get return null; } self::$db=new dbquery(); self::$db->connect(); // Querying database $select="select * from "._AGGREGATION_TABLE_NAME ." where mr_aggregation_id=( select parent_mr_aggregation_id from "._AGGREGATION_TABLE_NAME ." where mr_aggregation_id=$mr_aggregation_id )"; try{ if(_DEBUG){echo "getParent: $select // ";} self::$db->query($select); // Constructing result $mr_aggreg=new mr_aggregation(); $queryResult=self::$db->fetch_object(); foreach((array)$queryResult as $key => $value){ if(_ADVANCED_DEBUG){ echo "Getting property: $key with value: $value // "; } $mr_aggreg->$key=$value; } } catch (Exception $e){ echo "Impossible to get aggregation with id=$mr_aggregation_id // "; } self::$db->disconnect(); return $mr_aggreg; } ///////////////////////////////////////////////////// DELETE BLOCK /** * Delete given mr_aggregation from database. * @param mr_aggregation $mr_aggregation */ public function delete($mr_aggregation){ $parent=self::get($mr_aggregation->parent_mr_aggregation_id); // Deletion of given mr_aggregation $result = self::advanced_delete($mr_aggregation); $children=self::getChildren($parent,""); if(count($children)==0){ /* * According to one of main MoReq2 rules, parent * can have records from now on */ if($parent!==null){ $parent->can_have_records=true; $result = $result && self::save($parent); } } return $result; } ////////////////////////////////////////////// OTHER PRIVATE BLOCK } ?>