diff --git a/htdocs/categories/class/categorie.class.php b/htdocs/categories/class/categorie.class.php
index abf85d69cde..a2f4a7b0e50 100644
--- a/htdocs/categories/class/categorie.class.php
+++ b/htdocs/categories/class/categorie.class.php
@@ -40,15 +40,13 @@ class Categorie
public $table_element='category';
var $id;
- var $id_mere;
+ var $fk_parent;
var $label;
var $description;
var $socid;
var $type; // 0=Product, 1=Supplier, 2=Customer/Prospect, 3=Member
- var $parentId;
var $cats=array(); // Tableau en memoire des categories
- var $motherof = array(); // Tableau des correspondances id_fille -> id_mere
/**
@@ -69,7 +67,7 @@ class Categorie
*/
function fetch($id)
{
- $sql = "SELECT rowid, entity, label, description, fk_soc, visible, type";
+ $sql = "SELECT rowid, fk_parent, entity, label, description, fk_soc, visible, type";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE rowid = ".$id;
@@ -80,6 +78,7 @@ class Categorie
$res = $this->db->fetch_array($resql);
$this->id = $res['rowid'];
+ $this->fk_parent = $res['fk_parent'];
$this->label = $res['label'];
$this->description = $res['description'];
$this->socid = $res['fk_soc'];
@@ -88,25 +87,8 @@ class Categorie
$this->entity = $res['entity'];
$this->db->free($resql);
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- $sql = "SELECT fk_categorie_mere";
- $sql.= " FROM ".MAIN_DB_PREFIX."categorie_association";
- $sql.= " WHERE fk_categorie_fille = ".$id;
-
- dol_syslog("Categorie::fetch sql=".$sql);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $res = $this->db->fetch_array($resql);
- $this->id_mere = $res['fk_categorie_mere'];
- $this->parentId = $res['fk_categorie_mere'] ? $res['fk_categorie_mere'] : 0;
- return $this->id;
+ return 1;
}
else
{
@@ -133,7 +115,7 @@ class Categorie
// Clean parameters
if (empty($this->visible)) $this->visible=0;
- $this->parentId = ($this->id_mere) != "" ? intval($this->id_mere) : 0;
+ $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
if ($this->already_exists())
{
@@ -146,7 +128,7 @@ class Categorie
$this->db->begin();
dol_syslog(get_class($this).'::create sql='.$sql);
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (label, description,";
+ $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (fk_parent, label, description,";
if (! empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER))
{
$sql.= "fk_soc,";
@@ -154,15 +136,13 @@ class Categorie
$sql.= " visible,";
$sql.= " type,";
$sql.= " entity";
- //$sql.= ", fk_parent_id";
$sql.= ")";
- $sql.= " VALUES ('".$this->db->escape($this->label)."', '".$this->db->escape($this->description)."',";
+ $sql.= " VALUES (".$this->fk_parent.",'".$this->db->escape($this->label)."', '".$this->db->escape($this->description)."',";
if (! empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER))
{
$sql.= ($this->socid != -1 ? $this->socid : 'null').",";
}
$sql.= "'".$this->visible."',".$this->type.",".$conf->entity;
- //$sql.= ",".$this->parentId;
$sql.= ")";
dol_syslog(get_class($this).'::create sql='.$sql);
@@ -174,15 +154,6 @@ class Categorie
if ($id > 0)
{
$this->id = $id;
- if($this->id_mere != "")
- {
- if($this->add_fille() < 0)
- {
- $this->error=$langs->trans("ImpossibleAssociateCategory");
- $this->db->rollback();
- return -3;
- }
- }
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
@@ -226,8 +197,8 @@ class Categorie
// Clean parameters
$this->label=trim($this->label);
$this->description=trim($this->description);
- $this->parentId = ($this->id_mere) != "" ? intval($this->id_mere) : 0;
- $this->visible = ($this->visible) != "" ? intval($this->visible) : 0;
+ $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
+ $this->visible = ($this->visible != "" ? intval($this->visible) : 0);
if ($this->already_exists())
{
@@ -238,34 +209,9 @@ class Categorie
$this->db->begin();
- $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'categorie_association';
- $sql.= ' WHERE fk_categorie_fille = '.$this->id;
-
- dol_syslog("Categorie::update sql=".$sql);
- if (! $this->db->query($sql))
- {
- $this->db->rollback();
- dol_print_error($this->db);
- return -1;
- }
-
- if($this->id_mere !="" && $this->id_mere!=$this->id)
- {
- $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'categorie_association(fk_categorie_mere,fk_categorie_fille)';
- $sql.= ' VALUES ('.$this->id_mere.', '.$this->id.')';
-
- dol_syslog("Categorie::update sql=".$sql);
- if (! $this->db->query($sql))
- {
- $this->db->rollback();
- dol_print_error($this->db);
- return -1;
- }
- }
-
$sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
$sql.= " SET label = '".$this->db->escape($this->label)."'";
- if ($this->description)
+ if (! empty($this->description))
{
$sql .= ", description = '".$this->db->escape($this->description)."'";
}
@@ -274,7 +220,7 @@ class Categorie
$sql .= ", fk_soc = ".($this->socid != -1 ? $this->socid : 'null');
}
$sql .= ", visible = '".$this->visible."'";
- //$sql .= ", fk_parent_id = ".$this->parentId;
+ $sql .= ", fk_parent = ".$this->fk_parent;
$sql .= " WHERE rowid = ".$this->id;
dol_syslog("Categorie::update sql=".$sql);
@@ -360,20 +306,6 @@ class Categorie
}
}
- // Link childs to parent
- if (! $error)
- {
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
- $sql .= " WHERE fk_categorie_mere = ".$this->id;
- $sql .= " OR fk_categorie_fille = ".$this->id;
- if (!$this->db->query($sql))
- {
- $this->error=$this->db->lasterror();
- dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
- $error++;
- }
- }
-
// Delete category
if (! $error)
{
@@ -408,58 +340,6 @@ class Categorie
}
}
-
- /**
- * Ajout d'une sous-categorie
- *
- * @return int 1 : OK
- * -2 : $fille est deja dans la famille de $this
- * -3 : categorie ($this ou $fille) invalide
- */
- function add_fille()
- {
- $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_association (fk_categorie_mere, fk_categorie_fille)";
- $sql.= " VALUES (".$this->id_mere.", ".$this->id.")";
-
- if ($this->db->query($sql))
- {
- return 1;
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
- }
-
- /**
- * Suppression d'une sous-categorie (seulement "desassociation")
- *
- * @param Category $fille Objet category
- * @return int 1 : OK
- * -3 : categorie ($this ou $fille) invalide
- */
- function del_fille($fille)
- {
- if (!$this->check() || !$fille->check())
- {
- return -3;
- }
-
- $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_association";
- $sql .= " WHERE fk_categorie_mere = ".$this->id." and fk_categorie_fille = ".$fille->id;
-
- if ($this->db->query($sql))
- {
- return 1;
- }
- else
- {
- $this->error=$this->db->error().' sql='.$sql;
- return -1;
- }
- }
-
/**
* Link an object to the category
*
@@ -540,9 +420,9 @@ class Categorie
$sql = "SELECT c.fk_".$field;
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$category_table." as c";
$sql.= ", ".MAIN_DB_PREFIX.$object_table." as o";
- $sql.= " WHERE c.fk_categorie = ".$this->id;
+ $sql.= " WHERE o.entity IN (".getEntity($field, 1).")";
+ $sql.= " AND c.fk_categorie = ".$this->id;
$sql.= " AND c.fk_".$field." = o.rowid";
- $sql.= " AND o.entity IN (".getEntity($field, 1).")";
dol_syslog(get_class($this)."::get_type sql=".$sql);
$resql = $this->db->query($sql);
@@ -572,8 +452,8 @@ class Categorie
*/
function get_filles()
{
- $sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association ";
- $sql .= "WHERE fk_categorie_mere = ".$this->id;
+ $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
+ $sql.= " WHERE fk_parent = ".$this->id;
$res = $this->db->query($sql);
@@ -583,7 +463,7 @@ class Categorie
while ($rec = $this->db->fetch_array($res))
{
$cat = new Categorie($this->db);
- $cat->fetch($rec['fk_categorie_fille']);
+ $cat->fetch($rec['rowid']);
$cats[] = $cat;
}
return $cats;
@@ -601,6 +481,7 @@ class Categorie
*
* @param int $cate Category id
* @return string Description
+ * @deprecated function not used ?
*/
function get_desc($cate)
{
@@ -616,13 +497,14 @@ class Categorie
/**
* La categorie $fille est-elle une fille de cette categorie ?
*
- * @param Category $fille Object category
+ * @param Category $child Object category
* @return void
+ * @deprecated function not used ?
*/
- function is_fille($fille)
+ function is_fille($child)
{
- $sql = "SELECT count(fk_categorie_fille) FROM ".MAIN_DB_PREFIX."categorie_association ";
- $sql .= "WHERE fk_categorie_mere = ".$this->id." AND fk_categorie_fille = ".$fille->id;
+ $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
+ $sql.= " WHERE fk_parent = ".$this->id." AND rowid = ".$child->id;
$res = $this->db->query($sql);
$n = $this->db->fetch_array($res);
@@ -649,37 +531,11 @@ class Categorie
{
$this->cats = array();
- // Charge tableau des meres
- $sql = "SELECT ca.fk_categorie_mere as id_mere, ca.fk_categorie_fille as id_fille";
- $sql.= " FROM ".MAIN_DB_PREFIX."categorie_association ca";
- $sql.= ", ".MAIN_DB_PREFIX."categorie as c";
- $sql.= " WHERE ca.fk_categorie_mere = c.rowid";
- $sql.= " AND c.entity IN (".getEntity('category',1).")";
-
- // Load array this->motherof
- dol_syslog("Categorie::get_full_arbo build motherof array sql=".$sql, LOG_DEBUG);
- $resql = $this->db->query($sql);
- if ($resql)
- {
- while ($obj=$this->db->fetch_object($resql))
- {
- $this->motherof[$obj->id_fille]=$obj->id_mere;
- }
- }
- else
- {
- dol_print_error($this->db);
- return -1;
- }
-
// Init $this->cats array
- $sql = "SELECT DISTINCT c.rowid, c.label as label, ca.fk_categorie_fille as rowid_fille"; // Distinct reduce pb with old tables with duplicates
+ $sql = "SELECT DISTINCT c.rowid, c.label, c.description, c.fk_parent"; // Distinct reduce pb with old tables with duplicates
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as c";
- $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_association as ca";
- $sql.= " ON c.rowid = ca.fk_categorie_mere";
- $sql.= " WHERE c.type = ".$type;
- $sql.= " AND c.entity IN (".getEntity('category',1).")";
- $sql.= " ORDER BY c.label, c.rowid";
+ $sql.= " WHERE c.entity IN (".getEntity('category',1).")";
+ $sql.= " AND c.type = ".$type;
dol_syslog("Categorie::get_full_arbo get category list sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
@@ -688,16 +544,12 @@ class Categorie
$i=0;
while ($obj = $this->db->fetch_object($resql))
{
+ $this->cats[$obj->rowid]['rowid'] = $obj->rowid;
$this->cats[$obj->rowid]['id'] = $obj->rowid;
- if (isset($this->motherof[$obj->rowid])) $this->cats[$obj->rowid]['id_mere'] = $this->motherof[$obj->rowid];
+ $this->cats[$obj->rowid]['fk_parent'] = $obj->fk_parent;
$this->cats[$obj->rowid]['label'] = $obj->label;
-
- if ($obj->rowid_fille)
- {
- $this->cats[$obj->rowid]['id_children'][]=$obj->rowid_fille;
- }
+ $this->cats[$obj->rowid]['description'] = $obj->description;
$i++;
-
}
}
else
@@ -710,7 +562,6 @@ class Categorie
dol_syslog("Categorie::get_full_arbo call to build_path_from_id_categ", LOG_DEBUG);
foreach($this->cats as $key => $val)
{
- if (isset($this->motherof[$key])) continue;
$this->build_path_from_id_categ($key,0); // Process a branch from the root category key (this category has no parent)
}
@@ -761,21 +612,22 @@ class Categorie
//}
// Define fullpath and fulllabel
- if (isset($this->cats[$id_categ]['id_mere']))
+ if (! empty($this->cats[$id_categ]['fk_parent']))
{
- $this->cats[$id_categ]['fullpath'] =$this->cats[$this->cats[$id_categ]['id_mere']]['fullpath'];
- $this->cats[$id_categ]['fullpath'].='_'.$id_categ;
- $this->cats[$id_categ]['fulllabel'] =$this->cats[$this->cats[$id_categ]['id_mere']]['fulllabel'];
- $this->cats[$id_categ]['fulllabel'].=' >> '.$this->cats[$id_categ]['label'];
+ $this->cats[$id_categ]['fullpath'] = $this->cats[$this->cats[$id_categ]['fk_parent']]['fullpath'];
+ $this->cats[$id_categ]['fullpath'].= '_'.$id_categ;
+ $this->cats[$id_categ]['fulllabel'] = $this->cats[$this->cats[$id_categ]['fk_parent']]['fulllabel'];
+ $this->cats[$id_categ]['fulllabel'].= ' >> '.$this->cats[$id_categ]['label'];
}
else
{
- $this->cats[$id_categ]['fullpath']='_'.$id_categ;
- $this->cats[$id_categ]['fulllabel']=$this->cats[$id_categ]['label'];
+ $this->cats[$id_categ]['fullpath'] = '_'.$id_categ;
+ $this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label'];
}
// We count number of _ to have level
$this->cats[$id_categ]['level']=dol_strlen(preg_replace('/[^_]/i','',$this->cats[$id_categ]['fullpath']));
+ /*
// Process all childs on several levels of this category
$protection++;
if ($protection > 10) return; // On ne traite pas plus de 10 niveaux de profondeurs
@@ -791,6 +643,7 @@ class Categorie
$this->build_path_from_id_categ($idchild,$protection);
}
+ */
return;
}
@@ -807,8 +660,8 @@ class Categorie
{
print 'id: '.$this->cats[$key]['id'];
print ' label: '.$this->cats[$key]['label'];
- print ' mother: '.$this->cats[$key]['id_mere'];
- print ' children: '.(is_array($this->cats[$key]['id_children'])?join(',',$this->cats[$key]['id_children']):'');
+ print ' mother: '.$this->cats[$key]['fk_parent'];
+ //print ' children: '.(is_array($this->cats[$key]['id_children'])?join(',',$this->cats[$key]['id_children']):'');
print ' fullpath: '.$this->cats[$key]['fullpath'];
print ' fulllabel: '.$this->cats[$key]['fulllabel'];
print "
\n";
@@ -819,12 +672,18 @@ class Categorie
/**
* Retourne toutes les categories
*
+ * @param int Type of category
+ * @param boolean Just parent categories if true
* @return array Tableau d'objet Categorie
*/
- function get_all_categories ()
+ function get_all_categories($type=null, $parent=false)
{
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE entity IN (".getEntity('category',1).")";
+ if (! is_null($type))
+ $sql.= " AND type = ".$type;
+ if ($parent)
+ $sql.= " AND fk_parent = 0";
$res = $this->db->query($sql);
if ($res)
@@ -834,7 +693,7 @@ class Categorie
{
$cat = new Categorie($this->db);
$cat->fetch($rec['rowid']);
- $cats[$record['rowid']] = $cat;
+ $cats[$rec['rowid']] = $cat;
}
return $cats;
}
@@ -849,6 +708,7 @@ class Categorie
* Retourne le nombre total de categories
*
* @return int Nombre de categories
+ * @deprecated function not used ?
*/
function get_nb_categories()
{
@@ -870,37 +730,21 @@ class Categorie
/**
* Check if no category with same label already exists for this cat's parent or root and for this cat's type
- * TODO For the moment, the unique key is on the type, label, entity. We must remove table llx_categorie_association
- * to replace with a field fk_parent. This will allow to extend unique key with the level.
*
* @return boolean 1 if already exist, 0 otherwise, -1 if error
*/
function already_exists()
{
- if($this->id_mere != "") // mother_id defined
- {
- /* We have to select any rowid from llx_categorie which category's mother and label
- * are equals to those of the calling category
- */
- $sql = "SELECT c.rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."categorie as c ";
- $sql.= " JOIN ".MAIN_DB_PREFIX."categorie_association as ca";
- $sql.= " ON c.rowid=ca.fk_categorie_fille";
- $sql.= " WHERE ca.fk_categorie_mere=".$this->id_mere;
- $sql.= " AND c.label='".$this->db->escape($this->label)."'";
- $sql.= " AND c.entity IN (".getEntity('category',1).")";
- }
- else // mother_id undefined (so it's root)
- {
- /* We have to select any rowid from llx_categorie that is not at root level
- */
- $sql = "SELECT c.rowid";
- $sql.= " FROM ".MAIN_DB_PREFIX."categorie as c ";
- $sql.= " WHERE c.type=".$this->type;
- $sql.= " AND c.label='".$this->db->escape($this->label)."'";
- $sql.= " AND c.entity IN (".getEntity('category',1).")";
- $sql.= " AND c.rowid NOT IN (SELECT ca.fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association as ca)";
- }
+ /* We have to select any rowid from llx_categorie which category's mother and label
+ * are equals to those of the calling category
+ */
+ $sql = "SELECT c.rowid";
+ $sql.= " FROM ".MAIN_DB_PREFIX."categorie as c ";
+ $sql.= " WHERE c.entity IN (".getEntity('category',1).")";
+ $sql.= " AND c.type = ".$this->type;
+ $sql.= " AND c.fk_parent = ".$this->fk_parent;
+ $sql.= " AND c.label = '".$this->db->escape($this->label)."'";
+
dol_syslog(get_class($this)."::already_exists sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
@@ -933,30 +777,12 @@ class Categorie
/**
* Retourne les categories de premier niveau (qui ne sont pas filles)
*
+ * @param int Type of category
* @return void
*/
- function get_main_categories()
+ function get_main_categories($type=null)
{
- $allcats = $this->get_all_categories();
- $maincats = array ();
- $filles = array ();
-
- $sql = "SELECT fk_categorie_fille FROM ".MAIN_DB_PREFIX."categorie_association";
- $res = $this->db->query($sql);
- while ($res = $this->db->fetch_array($res))
- {
- $filles[] = $res['fk_categorie_fille'];
- }
-
- foreach ($allcats as $cat)
- {
- if (! in_array($cat->id, $filles))
- {
- $maincats[] = $cat;
- }
- }
-
- return $maincats;
+ return $this->get_all_categories($type, true);
}
/**
@@ -967,7 +793,7 @@ class Categorie
* @param string $url Url
* @return void
*/
- function print_all_ways ($sep = " >> ", $url='')
+ function print_all_ways($sep = " >> ", $url='')
{
$ways = array ();
@@ -998,10 +824,11 @@ class Categorie
* @param int $id Id of category
* @param string $type Type of category
* @return void
+ * @deprecated function not used ?
*/
function get_primary_way($id, $type="")
{
- $primary_way = Array("taille"=>-1,"chemin"=>Array());
+ $primary_way = array("taille" => -1, "chemin" => array());
$meres = $this->containing($id,$type);
foreach ($meres as $mere)
{
@@ -1026,10 +853,11 @@ class Categorie
* @param string $url Url
* @param string $type Type
* @return void
+ * @deprecated function not used ?
*/
function print_primary_way($id, $sep= " >> ", $url="", $type="")
{
- $primary_way = Array();
+ $primary_way = array();
$way = $this->get_primary_way($id,$type);
$w = array();
foreach ($way as $cat)
@@ -1054,10 +882,10 @@ class Categorie
*/
function get_meres()
{
- $meres = array();
+ $parents = array();
- $sql = "SELECT fk_categorie_mere FROM ".MAIN_DB_PREFIX."categorie_association ";
- $sql .= "WHERE fk_categorie_fille = ".$this->id;
+ $sql = "SELECT fk_parent FROM ".MAIN_DB_PREFIX."categorie";
+ $sql.= " WHERE rowid = ".$this->id;
$res = $this->db->query($sql);
@@ -1065,11 +893,14 @@ class Categorie
{
while ($rec = $this->db->fetch_array($res))
{
- $cat = new Categorie($this->db);
- $cat->fetch($rec['fk_categorie_mere']);
- $meres[] = $cat;
+ if ($rec['fk_parent'] > 0)
+ {
+ $cat = new Categorie($this->db);
+ $cat->fetch($rec['fk_parent']);
+ $parents[] = $cat;
+ }
}
- return $meres;
+ return $parents;
}
else
{
@@ -1084,7 +915,7 @@ class Categorie
*
* @return void
*/
- function get_all_ways ()
+ function get_all_ways()
{
$ways = array();
@@ -1398,7 +1229,7 @@ class Categorie
// Initialise parametres
$this->id=0;
- $this->id_mere=0;
+ $this->fk_parent=0;
$this->label = 'SPECIMEN';
$this->specimen=1;
$this->description = 'This is a description';
diff --git a/htdocs/categories/edit.php b/htdocs/categories/edit.php
index cec7444503e..1ec9a15a465 100644
--- a/htdocs/categories/edit.php
+++ b/htdocs/categories/edit.php
@@ -38,7 +38,7 @@ $socid=GETPOST('socid','int');
$nom=GETPOST('nom');
$description=GETPOST('description');
$visible=GETPOST('visible');
-$catMere=GETPOST('catMere');
+$parent=GETPOST('parent');
if ($id == "")
{
@@ -66,20 +66,20 @@ if ($action == 'update' && $user->rights->categorie->creer)
$categorie->socid = ($socid ? $socid : 'null');
$categorie->visible = $visible;
- if ($catMere != "-1")
- $categorie->id_mere = $catMere;
+ if ($parent != "-1")
+ $categorie->fk_parent = $parent;
else
- $categorie->id_mere = "";
+ $categorie->fk_parent = "";
- if (! $categorie->label)
+ if (empty($categorie->label))
{
- $_GET["action"] = 'create';
+ $action = 'create';
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("Label"));
}
- if (! $categorie->description)
+ if (empty($categorie->description))
{
- $_GET["action"] = 'create';
+ $action = 'create';
$mesg = $langs->trans("ErrorFieldRequired",$langs->transnoentities("Description"));
}
if (empty($categorie->error))
@@ -149,7 +149,7 @@ print '';
// Parent category
print '