type_obj = $type ; $this->object_id = $obj ; $this->all_audit=array() ; $this->info=array() ; $this->info['fields']=array(); } // --------------------------------------------------------------- // get_all () : récupération toutes informations // --------------------------------------------------------------- function get_all() { global $dbh, $pmb_type_audit, $msg ; if (!$pmb_type_audit) return 0; $query = "select user_id, user_name, type_modif, quand, date_format(quand, '".$msg["format_date_heure"]."') as aff_quand, concat(prenom, ' ', nom) as prenom_nom ,info from audit left join users on user_id=userid where "; $query .= "type_obj='$this->type_obj' AND "; $query .= "object_id='$this->object_id' "; $query .= "order by quand "; $result = @mysql_query($query, $dbh); if(!$result) die("can't select from table audit left join users :
$query "); while ($audit=mysql_fetch_object($result)) { $this->all_audit[] = $audit ; } } // --------------------------------------------------------------- // get_creation () : récupération création // --------------------------------------------------------------- function get_creation () { global $dbh, $pmb_type_audit ; if (!$pmb_type_audit) return 0; return $this->all_audit[0]; } // --------------------------------------------------------------- // get_last () : récupération dernière modification // --------------------------------------------------------------- function get_last () { global $dbh, $pmb_type_audit ; if (!$pmb_type_audit) return 0; return $this->all_audit[(count($this->all_audit)-1)]; } // --------------------------------------------------------------- // insert_creation ($type=0, $obj=0) : // --------------------------------------------------------------- static function insert_creation ($type=0, $obj=0,$info="") { global $dbh, $PMBuserid, $PMBusername, $pmb_type_audit ; if (!$pmb_type_audit) return 0; $query = "INSERT INTO audit SET "; $query .= "type_obj='$type', "; $query .= "object_id='$obj', "; $query .= "user_id='$PMBuserid', "; $query .= "user_name='$PMBusername', "; $query .= "type_modif=1, "; $query .= "info='".addslashes($info)."' "; $result = @mysql_query($query, $dbh); if(!$result) die("can't INSERT into table audit :
$query "); return 1; } // --------------------------------------------------------------- // insert_modif ($type=0, $obj=0) : // --------------------------------------------------------------- static function insert_modif ($type=0, $obj=0,$info="") { global $dbh, $PMBuserid, $PMBusername, $pmb_type_audit ; if (!$pmb_type_audit) return 0; if ($pmb_type_audit=='1') { $query = "DELETE FROM audit WHERE "; $query .= "type_obj='$type' AND "; $query .= "object_id='$obj' AND "; $query .= "type_modif=2 "; $result = @mysql_query($query, $dbh); if(!$result) die("can't DELETE FROM table audit :
$query "); } $query = "INSERT INTO audit SET "; $query .= "type_obj='$type', "; $query .= "object_id='$obj', "; $query .= "user_id='$PMBuserid', "; $query .= "user_name='$PMBusername', "; $query .= "type_modif=2, "; $query .= "info='".addslashes($info)."' "; $result = @mysql_query($query, $dbh); return 1; } // --------------------------------------------------------------- // delete_audit ($type=0, $obj=0) : // --------------------------------------------------------------- function delete_audit ($type=0, $obj=0) { global $dbh ; $query = "DELETE FROM audit WHERE "; $query .= "type_obj='$type' AND "; $query .= "object_id in ($obj) "; $result = @mysql_query($query, $dbh); return 1; } // --------------------------------------------------------------- // A appeler avant l'update de la table pour mémoriser les valeurs // --------------------------------------------------------------- function get_old_infos($requete){ global $dbh ; $old_data=array(); if($this->info['fields']) foreach($this->info['fields'] as $field =>$value){ $this->info['fields'][$field]['old']=""; } $res = mysql_query($requete, $dbh); if (($line = mysql_fetch_array($res))) { $old_data=$line; } foreach($old_data as $field =>$value){ if(is_numeric($field)) continue; $this->info['fields'][$field]['old']=$value; } } // --------------------------------------------------------------- // A appeler après l'update de la table pour mémoriser les valeurs // --------------------------------------------------------------- function get_new_infos($requete){ global $dbh ; $old_data=array(); if($this->info['fields']) foreach($this->info['fields'] as $field =>$value){ $this->info['fields'][$field]['new']=""; } $res = mysql_query($requete, $dbh); if (($line = mysql_fetch_array($res))) { $old_data=$line; } $i=0; foreach($old_data as $field =>$value){ if(is_numeric($field)) continue; $this->info['fields'][$field]['new']=$value; } } // --------------------------------------------------------------- // Insére l'audit avec les champs modifiés // --------------------------------------------------------------- function save_info_modif($type=0, $obj=0,$comment=""){ $info['comment']=$comment; foreach($this->info['fields'] as $field =>$value){ // on ne garde que les champs modifiés if($this->info['fields'][$field]['old']==$this->info['fields'][$field]['new'])continue; $info['fields'][$field]['old']=$this->info['fields'][$field]['old']; $info['fields'][$field]['new']=$this->info['fields'][$field]['new']; } $this->insert_modif ($type, $obj, json_encode($info)); } } // fin if !define } // class audit