type_obj = $type+0;
$this->object_id = $obj+0;
$this->all_audit=array() ;
$this->info=array() ;
$this->info['fields']=array();
}
// ---------------------------------------------------------------
// get_all () : récupération toutes informations
// ---------------------------------------------------------------
public function get_all() {
global $pmb_type_audit, $msg ;
if (!$pmb_type_audit) return 0;
$query = "select user_id, user_name, type_user, type_modif, quand, date_format(quand, '".$msg["format_date_heure"]."') as aff_quand, if(type_user=1, concat(empr_prenom, ' ', empr_nom), concat(prenom, ' ', nom )) as prenom_nom ,info from audit
left join users on user_id=userid and type_user=0
left join empr on user_id = id_empr and type_user = 1 where ";
$query .= "type_obj='$this->type_obj' AND ";
$query .= "object_id='$this->object_id' ";
$query .= "order by quand ";
$result = @pmb_mysql_query($query);
if(!$result) die("can't select from table audit left join users :
$query ");
if(pmb_mysql_num_rows($result)){
while ($audit=pmb_mysql_fetch_object($result)) {
$this->all_audit[] = $audit ;
}
}
}
// ---------------------------------------------------------------
// get_creation () : récupération création
// ---------------------------------------------------------------
public function get_creation () {
global $dbh, $pmb_type_audit ;
if (!$pmb_type_audit || !isset($this->all_audit[0])) return 0;
return $this->all_audit[0];
}
// ---------------------------------------------------------------
// get_last () : récupération dernière modification
// ---------------------------------------------------------------
public function get_last () {
global $pmb_type_audit ;
if (!$pmb_type_audit || !isset($this->all_audit[(count($this->all_audit)-1)])) return 0;
return $this->all_audit[(count($this->all_audit)-1)];
}
// ---------------------------------------------------------------
// insert_creation ($type=0, $obj=0) :
// ---------------------------------------------------------------
public static function insert_creation ($type=0, $obj=0,$info="") {
global $PMBuserid, $PMBusername, $pmb_type_audit ;
if (!$pmb_type_audit) return 0;
$type += 0;
$obj += 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 = @pmb_mysql_query($query);
if(!$result) return 0;
return 1;
}
// ---------------------------------------------------------------
// insert_modif ($type=0, $obj=0) :
// ---------------------------------------------------------------
public static function insert_modif ($type=0, $obj=0,$info="") {
global $PMBuserid, $PMBusername, $pmb_type_audit ;
if (!$pmb_type_audit) return 0;
$type += 0;
$obj += 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 = @pmb_mysql_query($query);
if(!$result) 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=2, ";
$query .= "info='".addslashes($info)."' ";
$result = @pmb_mysql_query($query);
return 1;
}
// ---------------------------------------------------------------
// delete_audit ($type=0, $obj=0) :
// ---------------------------------------------------------------
public static function delete_audit($type=0, $obj=0) {
$type += 0;
$obj += 0;
$query = "select info FROM audit WHERE type_obj=".$type." AND object_id=".$obj;
$result = pmb_mysql_query($query);
if(pmb_mysql_num_rows($result)) {
while ($audit = pmb_mysql_fetch_object($result)) {
if(!$audit->info) continue;
$info = json_decode($audit->info);
if($info && $info->uri) {
contribution_area_forms_controller::delete_uri($info->uri);
}
}
}
$query = "DELETE FROM audit WHERE ";
$query .= "type_obj='$type' AND ";
$query .= "object_id in ($obj) ";
$result = @pmb_mysql_query($query);
return 1;
}
// ---------------------------------------------------------------
// A appeler avant l'update de la table pour mémoriser les valeurs
// ---------------------------------------------------------------
public 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 = pmb_mysql_query($requete, $dbh);
if (($line = pmb_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
// ---------------------------------------------------------------
public 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 = pmb_mysql_query($requete, $dbh);
if (($line = pmb_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
// ---------------------------------------------------------------
public 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));
}
public static function get_dialog_button($object_id=0, $object_type=1) {
global $msg;
return " ";
}
} // fin if !define
} // class audit