<?php
class Sync_parts_association_model extends CI_Model
{
protected $db1;
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->db1 = $this->load->database('intranet',TRUE);
}
//--------------------------------------------------------------------------
function SupplierFieldList($supplier,$table)
{
$data = array();
$sql = "SELECT supplier_field, table_field FROM h_fields_association WHERE supplier_name = '$supplier' AND table_name = '$table' ORDER BY supplier_field ASC";
$query = $this->db1->query($sql);
foreach ($query->result_array() as $row)
{
$data[urlencode($row["supplier_field"])] = $row["table_field"];
}
return $data;
}
//--------------------------------------------------------------------------
function SupplierFieldListId($supplier,$table)
{
$data = array();
$sql = "SELECT id, supplier_field, table_field FROM h_fields_association WHERE supplier_name = '$supplier' AND table_name = '$table' ORDER BY supplier_field ASC";
$query = $this->db1->query($sql);
foreach ($query->result_array() as $row)
{
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["table_field"] = $row["table_field"];
$data[$row["supplier_field"]] = $tmp;
}
return $data;
}
//--------------------------------------------------------------------------
function add($PartProperties)
{
$PartId = 0;
$sql = "INSERT INTO h_fields_association (";
$properties_list = array_keys($PartProperties);
$properties_count = 0;
$sqlValues = "";
foreach($properties_list as $properties_name)
{
if($properties_count > 0)
{
$sql .= ", ";
$sqlValues .= ", ";
}
$sql .= $properties_name;
$sqlValues .= "'".addslashes($PartProperties[$properties_name])."'";
$properties_count++;
}
$sql .= " ) VALUES ( ";
$sql .= $sqlValues;
$sql .= " ) ";
//deb($PartProperties);
$query = $this->db1->query($sql);
//Trouver le ID du type
$PartId = $this->db1->insert_id();
return $PartId;
}
//--------------------------------------------------------------------------
function edit($PartProperties,$PartId)
{
$result = 0;
$sql = "UPDATE h_fields_association SET ";
$properties_list = array_keys($PartProperties);
$properties_count = 0;
foreach($properties_list as $properties_name)
{
if(($properties_name != "TableRef")&&($properties_name != "History")&&($properties_name != "History_value"))
{
if($properties_count > 0)
{
$sql .= ", ";
}
$sql .= $properties_name;
$sql .= " = '".addslashes($PartProperties[$properties_name])."'";
$properties_count++;
}
}
$sql .= " WHERE id = '$PartId'";
//echo '<p>'.$sql.'</p>';
$query = $this->db1->query($sql);
$result = $PartId;
return $result;
}
//--------------------------------------------------------------------------
//****************************************************************************
// Modifie le nom d'un champ dans une table
//****************************************************************************
function change_field_name($table,$old_field,$new_field)
{
$this->db1->update('h_fields_association',array('table_field' => $new_field),"table_name = '$table' AND table_field = '$old_field'");
} //--------------------------------------------------------------------------
function erase($Id)
{
$sql = "DELETE FROM ";
$sql .= "h_fields_association";
$sql .= " WHERE id";
$sql .= " = '$Id '";
//echo '<p>'.$sql.'</p>';
$query = $this->db1->query($sql);
}
//--------------------------------------------------------------------------
//****************************************************************************
// Nettoie la table en enlevant les champs qui n'existent plus
// ou les tables inexistantes
//****************************************************************************
function clean()
{
$CI =& get_instance();
$CI->load->model('pf_model');
//Initialisation des variables
$last_table = '';
$table_exist = 0;
$field_exist = 0;
$field_list = array();
//Aller chercher toutes les entr�es
$sql = "SELECT id, table_name, table_field FROM h_fields_association WHERE 1 ORDER BY table_name ASC, table_field ASC ";
$query = $this->db1->query($sql);
foreach ($query->result_array() as $row)
{
$table = $row['table_name'];
$field = $row['table_field'];
$line_id = $row['id'];
if($last_table != $table)
{
$field_list = array();
//V�rifier l'existence de la table
$table_exist = $CI->pf_model->exist($table);
if($table_exist)
{
$field_list = $CI->pf_model->columns($table);
}
//V�rifier l'existence du champ
$field_exist = array_search($field,$field_list);
//Effacer le champ si il y a un probleme
if(($table_exist === false)||($field_exist === false))
{
$this->erase($line_id);
}
}
$last_table = $table;
}
} //--------------------------------------------------------------------------
function test()
{
echo "a";
}
function fields($supplier, $table_name)
{
$CI =& get_instance();
$CI->load->model('pf_model');
//Aller chercher les champs associés
$tmp_fields_association = $this->SupplierFieldList($supplier, $table_name);
$table_fields = $this->pf_model->columns($table_name);
//S'assurer que les champs reçus sont bient dans la table
$fields_association = array();
foreach($tmp_fields_association as $fa_key => $fa_val)
{
$tf = array_search($fa_val,$table_fields);
if($tf !== false)
{
$fields_association[$fa_key] = $fa_val;
}
}
return $fields_association;
} //--------------------------------------------------------------------------
}
?>