Start to normalize code for category to not use named table, columns but

only type of category.
This commit is contained in:
Laurent Destailleur 2014-01-12 15:54:28 +01:00
parent 1cf058b8e9
commit 51b455b85a
2 changed files with 42 additions and 31 deletions

View File

@ -528,16 +528,20 @@ class Categorie
} }
/** /**
* Return list of id of elements having this category * Return list of fetched instance of elements having this category
* *
* @param string $field Field name for select in table. Full field name will be fk_field. * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @param string $classname PHP Class of object to store entity
* @param string $category_table Table name for select in table. Full table name will be PREFIX_categorie_table.
* @param string $object_table Table name for select in table. Full table name will be PREFIX_table.
* @return mixed -1 if KO, array of instance of object if OK * @return mixed -1 if KO, array of instance of object if OK
*/ */
function get_type($field,$classname,$category_table='',$object_table='') function getObjectsInCateg($type)
{ {
$field=''; $classname=''; $category_table=''; $object_table='';
if ($type=='product') { $field='product'; $classname='Product'; }
if ($type=='customer') { $field='societe'; $classname='Societe'; }
if ($type=='supplier') { $field='societe'; $classname='Fournisseur'; $category_table='fournisseur'; }
if ($type=='member') { $field='member'; $classname='Adherent'; $category_table=''; $object_table='adherent'; }
if ($type=='contact') { $field='socpeople'; $classname='Contact'; $category_table='contact'; $object_table='socpeople'; }
$objs = array(); $objs = array();
// Clean parameters // Clean parameters
@ -551,7 +555,7 @@ class Categorie
$sql.= " AND c.fk_categorie = ".$this->id; $sql.= " AND c.fk_categorie = ".$this->id;
$sql.= " AND c.fk_".$field." = o.rowid"; $sql.= " AND c.fk_".$field." = o.rowid";
dol_syslog(get_class($this)."::get_type sql=".$sql); dol_syslog(get_class($this)."::getObjectsInCateg sql=".$sql);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -566,7 +570,7 @@ class Categorie
else else
{ {
$this->error=$this->db->error().' sql='.$sql; $this->error=$this->db->error().' sql='.$sql;
dol_syslog(get_class($this)."::get_type ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::getObjectsInCateg ".$this->error, LOG_ERR);
return -1; return -1;
} }
} }
@ -583,7 +587,6 @@ class Categorie
$sql.= " WHERE fk_parent = ".$this->id; $sql.= " WHERE fk_parent = ".$this->id;
$res = $this->db->query($sql); $res = $this->db->query($sql);
if ($res) if ($res)
{ {
$cats = array (); $cats = array ();
@ -1054,23 +1057,24 @@ class Categorie
} }
/** /**
* Return list of categories linked to element of id $id and type $typeid * Return list of categories (id or instances) linked to element of id $id and type $type
* Should be named getListOfCategForObject
* *
* @param int $id Id of element * @param int $id Id of element
* @param int $typeid Type of link (0 or 'product', 1 or 'supplier', 2 or 'customer', 3 or 'member', ...) * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact'). Old mode (0, 1, 2, ...) is deprecated.
* @param string $mode 'object'=Get array of categories, 'label'=Get array of category labels * @param string $mode 'object'=Get array of fetched category instances, 'label'=Get array of category labels
* @return mixed Array of category objects or < 0 if KO * @return mixed Array of category objects or < 0 if KO
*/ */
function containing($id,$typeid,$mode='object') function containing($id,$type,$mode='object')
{ {
$cats = array(); $cats = array();
$table=''; $type=''; $typeid=-1; $table='';;
if ($typeid == 0 || $typeid == 'product') { $typeid=0; $table='product'; $type='product'; } if ($type == '0' || $type == 'product') { $typeid=0; $table='product'; $type='product'; }
else if ($typeid == 1 || $typeid == 'supplier') { $typeid=1; $table='societe'; $type='fournisseur'; } else if ($type == '1' || $type == 'supplier') { $typeid=1; $table='societe'; $type='fournisseur'; }
else if ($typeid == 2 || $typeid == 'customer') { $typeid=2; $table='societe'; $type='societe'; } else if ($type == '2' || $type == 'customer') { $typeid=2; $table='societe'; $type='societe'; }
else if ($typeid == 3 || $typeid == 'member') { $typeid=3; $table='member'; $type='member'; } else if ($type == '3' || $type == 'member') { $typeid=3; $table='member'; $type='member'; }
else if ($typeid == 4 || $typeid == 'contact') { $typeid=4; $table='socpeople'; $type='contact'; } else if ($type == '4' || $type == 'contact') { $typeid=4; $table='socpeople'; $type='contact'; }
$sql = "SELECT ct.fk_categorie, c.label"; $sql = "SELECT ct.fk_categorie, c.label";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$type." as ct, ".MAIN_DB_PREFIX."categorie as c"; $sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$type." as ct, ".MAIN_DB_PREFIX."categorie as c";
@ -1110,7 +1114,7 @@ class Categorie
* *
* @param int $id Id * @param int $id Id
* @param string $nom Name * @param string $nom Name
* @param string $type Type * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact'). Old mode (0, 1, 2, ...) is deprecated.
* @param boolean $exact Exact string search (true/false) * @param boolean $exact Exact string search (true/false)
* @param boolean $case Case sensitive (true/false) * @param boolean $case Case sensitive (true/false)
* @return array Array of category id * @return array Array of category id
@ -1119,9 +1123,16 @@ class Categorie
{ {
$cats = array(); $cats = array();
$typeid=-1;
if ($type == 0 || $type == 'product') { $typeid=0; }
else if ($type == 1 || $type == 'supplier') { $typeid=1; }
else if ($type == 2 || $type == 'customer') { $typeid=2; }
else if ($type == 3 || $type == 'member') { $typeid=3; }
else if ($type == 4 || $type == 'contact') { $typeid=4; }
// Generation requete recherche // Generation requete recherche
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE type = ".$type." "; $sql.= " WHERE type = ".$typeid;
$sql.= " AND entity IN (".getEntity('category',1).")"; $sql.= " AND entity IN (".getEntity('category',1).")";
if ($nom) if ($nom)
{ {

View File

@ -247,7 +247,7 @@ else
if ($object->type == 0) if ($object->type == 0)
{ {
$prods = $object->get_type("product","Product"); $prods = $object->getObjectsInCateg("product");
if ($prods < 0) if ($prods < 0)
{ {
dol_print_error(); dol_print_error();
@ -297,7 +297,7 @@ if ($object->type == 0)
if ($object->type == 1) if ($object->type == 1)
{ {
$socs = $object->get_type("societe","Fournisseur","fournisseur"); $socs = $object->getObjectsInCateg("supplier");
if ($socs < 0) if ($socs < 0)
{ {
dol_print_error(); dol_print_error();
@ -348,7 +348,7 @@ if ($object->type == 1)
if($object->type == 2) if($object->type == 2)
{ {
$socs = $object->get_type("societe","Societe"); $socs = $object->getObjectsInCateg("customer");
if ($socs < 0) if ($socs < 0)
{ {
dol_print_error(); dol_print_error();
@ -404,7 +404,7 @@ if ($object->type == 3)
{ {
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php'; require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
$prods = $object->get_type("member","Adherent","","adherent"); $prods = $object->getObjectsInCateg("member");
if ($prods < 0) if ($prods < 0)
{ {
dol_print_error($db,$object->error); dol_print_error($db,$object->error);
@ -456,7 +456,7 @@ if ($object->type == 3)
//Categorie contact //Categorie contact
if($object->type == 4) if($object->type == 4)
{ {
$contacts = $object->get_type("socpeople","Contact",'contact',"socpeople"); $contacts = $object->getObjectsInCateg("contact");
if ($contacts < 0) if ($contacts < 0)
{ {
dol_print_error(); dol_print_error();