36 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/ticket/class/ticket.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/contact/class/contact.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/knowledgemanagement/class/knowledgerecord.class.php';
50 const TYPE_PRODUCT =
'product';
51 const TYPE_SUPPLIER =
'supplier';
52 const TYPE_CUSTOMER =
'customer';
53 const TYPE_MEMBER =
'member';
54 const TYPE_CONTACT =
'contact';
55 const TYPE_USER =
'user';
56 const TYPE_PROJECT =
'project';
57 const TYPE_ACCOUNT =
'bank_account';
58 const TYPE_BANK_LINE =
'bank_line';
59 const TYPE_WAREHOUSE =
'warehouse';
60 const TYPE_ACTIONCOMM =
'actioncomm';
61 const TYPE_WEBSITE_PAGE =
'website_page';
62 const TYPE_TICKET =
'ticket';
63 const TYPE_KNOWLEDGEMANAGEMENT =
'knowledgemanagement';
68 public $picto =
'category';
74 protected $MAP_ID = array(
88 'knowledgemanagement' => 13
96 public static $MAP_ID_TO_CODE = array(
108 11 =>
'website_page',
110 13 =>
'knowledgemanagement'
118 public $MAP_CAT_FK = array(
121 'contact' =>
'socpeople',
122 'bank_account' =>
'account',
130 public $MAP_CAT_TABLE = array(
131 'customer' =>
'societe',
132 'supplier' =>
'fournisseur',
133 'bank_account'=>
'account',
141 public $MAP_OBJ_CLASS = array(
142 'product' =>
'Product',
143 'customer' =>
'Societe',
144 'supplier' =>
'Fournisseur',
145 'member' =>
'Adherent',
146 'contact' =>
'Contact',
148 'account' =>
'Account',
149 'bank_account' =>
'Account',
150 'project' =>
'Project',
151 'warehouse'=>
'Entrepot',
152 'actioncomm' =>
'ActionComm',
153 'website_page' =>
'WebsitePage',
154 'ticket' =>
'Ticket',
155 'knowledgemanagement' =>
'KnowledgeRecord'
163 public static $MAP_TYPE_TITLE_AREA = array(
164 'product' =>
'ProductsCategoriesArea',
165 'customer' =>
'CustomersCategoriesArea',
166 'supplier' =>
'SuppliersCategoriesArea',
167 'member' =>
'MembersCategoriesArea',
168 'contact' =>
'ContactsCategoriesArea',
169 'user' =>
'UsersCategoriesArea',
170 'account' =>
'AccountsCategoriesArea',
171 'bank_account' =>
'AccountsCategoriesArea',
172 'project' =>
'ProjectsCategoriesArea',
173 'warehouse'=>
'StocksCategoriesArea',
174 'actioncomm' =>
'ActioncommCategoriesArea',
175 'website_page' =>
'WebsitePageCategoriesArea'
182 public $MAP_OBJ_TABLE = array(
183 'customer' =>
'societe',
184 'supplier' =>
'societe',
185 'member' =>
'adherent',
186 'contact' =>
'socpeople',
187 'account' =>
'bank_account',
188 'project' =>
'projet',
189 'warehouse'=>
'entrepot',
190 'knowledgemanagement' =>
'knowledgemanagement_knowledgerecord'
196 public $element =
'category';
201 public $table_element =
'categorie';
255 public $cats = array();
260 public $motherof = array();
265 public $childs = array();
279 if (is_object($hookmanager)) {
280 $hookmanager->initHooks(array(
'category'));
281 $parameters = array();
282 $reshook = $hookmanager->executeHooks(
'constructCategory', $parameters, $this);
283 if ($reshook >= 0 && !empty($hookmanager->resArray)) {
284 foreach ($hookmanager->resArray as $mapList) {
285 $mapId = $mapList[
'id'];
286 $mapCode = $mapList[
'code'];
287 self::$MAP_ID_TO_CODE[$mapId] = $mapCode;
288 $this->MAP_ID[$mapCode] = $mapId;
289 $this->MAP_CAT_FK[$mapCode] = $mapList[
'cat_fk'];
290 $this->MAP_CAT_TABLE[$mapCode] = $mapList[
'cat_table'];
291 $this->MAP_OBJ_CLASS[$mapCode] = $mapList[
'obj_class'];
292 $this->MAP_OBJ_TABLE[$mapCode] = $mapList[
'obj_table'];
307 foreach ($this->MAP_ID as $mapCode => $mapId) {
311 'cat_fk' => (empty($this->MAP_CAT_FK[$mapCode]) ? $mapCode : $this->MAP_CAT_FK[$mapCode]),
312 'cat_table' => (empty($this->MAP_CAT_TABLE[$mapCode]) ? $mapCode : $this->MAP_CAT_TABLE[$mapCode]),
313 'obj_class' => (empty($this->MAP_OBJ_CLASS[$mapCode]) ? $mapCode : $this->MAP_OBJ_CLASS[$mapCode]),
314 'obj_table' => (empty($this->MAP_OBJ_TABLE[$mapCode]) ? $mapCode : $this->MAP_OBJ_TABLE[$mapCode])
330 public function fetch($id, $label =
'', $type =
null, $ref_ext =
'')
335 if (empty($id) && empty($label) && empty($ref_ext)) {
336 $this->error =
"No category to search for";
339 if (!is_null($type) && !is_numeric($type)) {
340 $type = $this->MAP_ID[$type];
343 $sql =
"SELECT rowid, fk_parent, entity, label, description, color, fk_soc, visible, type, ref_ext";
344 $sql .=
", date_creation, tms, fk_user_creat, fk_user_modif";
345 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie";
347 $sql .=
" WHERE rowid = ".((int) $id);
348 } elseif (!empty($ref_ext)) {
349 $sql .=
" WHERE ref_ext LIKE '".$this->db->escape($ref_ext).
"'";
351 $sql .=
" WHERE label = '".$this->db->escape($label).
"' AND entity IN (".
getEntity(
'category').
")";
352 if (!is_null($type)) {
353 $sql .=
" AND type = ".((int) $type);
357 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
360 if ($this->
db->num_rows(
$resql) > 0) {
361 $res = $this->
db->fetch_array(
$resql);
363 $this->
id = $res[
'rowid'];
365 $this->fk_parent = (int) $res[
'fk_parent'];
366 $this->label = $res[
'label'];
368 $this->color = $res[
'color'];
369 $this->socid = (int) $res[
'fk_soc'];
370 $this->visible = (int) $res[
'visible'];
371 $this->
type = (int) $res[
'type'];
372 $this->ref_ext = $res[
'ref_ext'];
373 $this->entity = (int) $res[
'entity'];
374 $this->date_creation = $this->
db->jdate($res[
'date_creation']);
375 $this->date_modification = $this->
db->jdate($res[
'tms']);
376 $this->user_creation_id = (int) $res[
'fk_user_creat'];
377 $this->user_modification_id = (int) $res[
'fk_user_modif'];
378 $this->user_creation = (int) $res[
'fk_user_creat'];
379 $this->user_modification = (int) $res[
'fk_user_modif'];
394 $this->error =
"No category found";
399 $this->error = $this->
db->lasterror;
400 $this->errors[] = $this->
db->lasterror;
416 global $conf, $langs, $hookmanager;
417 $langs->load(
'categories');
421 if (!is_numeric($type)) {
422 $type = $this->MAP_ID[$type];
427 dol_syslog(get_class($this).
'::create', LOG_DEBUG);
430 $this->label = trim($this->label);
432 $this->color = trim($this->color);
433 $this->import_key = trim($this->import_key);
434 $this->ref_ext = trim($this->ref_ext);
435 if (empty($this->visible)) {
438 $this->fk_parent = ($this->fk_parent !=
"" ? intval($this->fk_parent) : 0);
441 $this->error = $langs->trans(
"ImpossibleAddCat", $this->label);
442 $this->error .=
" : ".$langs->trans(
"CategoryExistsAtSameLevel");
449 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"categorie (";
450 $sql .=
"fk_parent,";
452 $sql .=
" description,";
454 if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) {
459 $sql .=
" import_key,";
462 $sql .=
" date_creation,";
463 $sql .=
" fk_user_creat";
464 $sql .=
") VALUES (";
465 $sql .= (int) $this->fk_parent.
",";
466 $sql .=
"'".$this->db->escape($this->label).
"', ";
467 $sql .=
"'".$this->db->escape($this->
description).
"', ";
468 $sql .=
"'".$this->db->escape($this->color).
"', ";
469 if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) {
470 $sql .= ($this->socid > 0 ? $this->socid :
'null').
", ";
472 $sql .=
"'".$this->db->escape($this->visible).
"', ";
473 $sql .= ((int) $type).
", ";
474 $sql .= (!empty($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
'null').
", ";
475 $sql .= (!empty($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
'null').
", ";
476 $sql .= (int) $conf->entity.
", ";
477 $sql .=
"'".$this->db->idate($now).
"', ";
478 $sql .= (int) $user->id;
481 $res = $this->db->query($sql);
483 $id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"categorie");
500 $result = $this->
call_trigger(
'CATEGORY_CREATE', $user);
511 $this->
db->rollback();
515 $this->
db->rollback();
519 $this->error = $this->
db->error();
520 $this->
db->rollback();
535 global $conf, $langs, $hookmanager;
540 $this->label = trim($this->label);
542 $this->ref_ext = trim($this->ref_ext);
543 $this->fk_parent = ($this->fk_parent !=
"" ? intval($this->fk_parent) : 0);
544 $this->visible = ($this->visible !=
"" ? intval($this->visible) : 0);
547 $this->error = $langs->trans(
"ImpossibleUpdateCat");
548 $this->error .=
" : ".$langs->trans(
"CategoryExistsAtSameLevel");
554 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"categorie";
555 $sql .=
" SET label = '".$this->db->escape($this->label).
"',";
556 $sql .=
" description = '".$this->db->escape($this->
description).
"',";
557 $sql .=
" ref_ext = '".$this->db->escape($this->ref_ext).
"',";
558 $sql .=
" color = '".$this->db->escape($this->color).
"'";
559 if (!empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) {
560 $sql .=
", fk_soc = ".($this->socid > 0 ? $this->socid :
'null');
562 $sql .=
", visible = ".(int) $this->visible;
563 $sql .=
", fk_parent = ".(int) $this->fk_parent;
564 $sql .=
", fk_user_modif = ".(int) $user->id;
565 $sql .=
" WHERE rowid = ".((int) $this->id);
567 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
568 if ($this->
db->query($sql)) {
581 $result = $this->
call_trigger(
'CATEGORY_MODIFY', $user);
583 $error++; $this->
db->rollback();
return -1;
592 $this->
db->rollback();
605 public function delete($user, $notrigger = 0)
607 global $conf, $langs;
612 $this->fk_parent = ($this->fk_parent !=
"" ? intval($this->fk_parent) : 0);
618 if (!$error && !$notrigger) {
620 $result = $this->
call_trigger(
'CATEGORY_DELETE', $user);
629 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"categorie";
630 $sql .=
" SET fk_parent = ".((int) $this->fk_parent);
631 $sql .=
" WHERE fk_parent = ".((int) $this->
id);
633 if (!$this->
db->query($sql)) {
634 $this->error = $this->
db->lasterror();
639 $arraydelete = array(
640 'categorie_account' =>
'fk_categorie',
641 'categorie_actioncomm' =>
'fk_categorie',
642 'categorie_contact' =>
'fk_categorie',
643 'categorie_fournisseur' =>
'fk_categorie',
644 'categorie_knowledgemanagement' => array(
'field' =>
'fk_categorie',
'enabled' =>
isModEnabled(
'knowledgemanagement')),
645 'categorie_member' =>
'fk_categorie',
646 'categorie_user' =>
'fk_categorie',
647 'categorie_product' =>
'fk_categorie',
648 'categorie_project' =>
'fk_categorie',
649 'categorie_societe' =>
'fk_categorie',
650 'categorie_ticket' => array(
'field' =>
'fk_categorie',
'enabled' =>
isModEnabled(
'ticket')),
651 'categorie_warehouse' =>
'fk_categorie',
652 'categorie_website_page' => array(
'field' =>
'fk_categorie',
'enabled' =>
isModEnabled(
'website')),
653 'bank_class' =>
'fk_categ',
654 'categorie_lang' =>
'fk_category',
655 'categorie' =>
'rowid',
657 foreach ($arraydelete as $key => $value) {
658 if (is_array($value)) {
659 if (empty($value[
'enabled'])) {
662 $value = $value[
'field'];
664 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.$key;
665 $sql .=
" WHERE ".$value.
" = ".((int) $this->
id);
666 if (!$this->
db->query($sql)) {
667 $this->errors[] = $this->
db->lasterror();
668 dol_syslog(
"Error sql=".$sql.
" ".$this->error, LOG_ERR);
678 dol_syslog(get_class($this).
"::delete erreur ".$this->error, LOG_ERR);
686 $this->
db->rollback();
703 global $user, $langs, $conf;
707 if ($this->
id == -1) {
712 $type = $obj->element;
715 dol_syslog(get_class($this).
'::add_type', LOG_DEBUG);
719 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
720 $sql .=
" (fk_categorie, fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
")";
721 $sql .=
" VALUES (".((int) $this->
id).
", ".((int) $obj->id).
")";
723 if ($this->
db->query($sql)) {
724 if (!empty($conf->global->CATEGORIE_RECURSIV_ADD)) {
725 $sql =
'SELECT fk_parent FROM '.MAIN_DB_PREFIX.
'categorie';
726 $sql .=
" WHERE rowid = ".((int) $this->
id);
728 dol_syslog(get_class($this).
"::add_type", LOG_DEBUG);
731 if ($this->
db->num_rows(
$resql) > 0) {
732 $objparent = $this->
db->fetch_object(
$resql);
734 if (!empty($objparent->fk_parent)) {
736 $cat->id = $objparent->fk_parent;
737 if (!$cat->containsObject($type, $obj->id)) {
738 $result = $cat->add_type($obj, $type);
740 $this->error = $cat->error;
748 $this->error = $this->
db->lasterror();
752 $this->
db->rollback();
758 $this->context = array(
'linkto'=>$obj);
769 $this->
db->rollback();
773 $this->
db->rollback();
774 if ($this->
db->lasterrno() ==
'DB_ERROR_RECORD_ALREADY_EXISTS') {
775 $this->error = $this->
db->lasterrno();
778 $this->error = $this->
db->lasterror();
796 global $user, $langs, $conf;
801 if ($type ==
'societe') {
803 dol_syslog(get_class($this).
"::del_type(): type 'societe' is deprecated, please use 'customer' instead", LOG_WARNING);
804 } elseif ($type ==
'fournisseur') {
806 dol_syslog(get_class($this).
"::del_type(): type 'fournisseur' is deprecated, please use 'supplier' instead", LOG_WARNING);
811 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
812 $sql .=
" WHERE fk_categorie = ".((int) $this->
id);
813 $sql .=
" AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
" = ".((
int) $obj->id);
815 dol_syslog(get_class($this).
'::del_type', LOG_DEBUG);
816 if ($this->
db->query($sql)) {
818 $this->context = array(
'unlinkoff'=>$obj);
819 $result = $this->
call_trigger(
'CATEGORY_UNLINK', $user);
829 $this->
db->rollback();
833 $this->
db->rollback();
834 $this->error = $this->
db->lasterror();
851 public function getObjectsInCateg($type, $onlyids = 0, $limit = 0, $offset = 0, $sortfield =
'', $sortorder =
'ASC')
857 $classnameforobj = $this->MAP_OBJ_CLASS[$type];
858 $obj =
new $classnameforobj($this->
db);
860 $sql =
"SELECT c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]);
861 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]).
" as c";
862 $sql .=
", ".MAIN_DB_PREFIX.(empty($this->MAP_OBJ_TABLE[$type]) ? $type : $this->MAP_OBJ_TABLE[$type]).
" as o";
863 $sql .=
" WHERE o.entity IN (".getEntity($obj->element).
")";
864 $sql .=
" AND c.fk_categorie = ".((int) $this->
id);
866 if ($this->MAP_OBJ_TABLE[$type] ==
"actioncomm" || $type ==
"actioncomm") {
867 $sql .=
" AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
" = o.id";
869 $sql .=
" AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
" = o.rowid";
872 if (($type ==
'customer' || $type ==
'supplier') && $user->socid > 0) {
873 $sql .=
" AND o.rowid = ".((int) $user->socid);
875 $sql .= $this->
db->order($sortfield, $sortorder);
876 if ($limit > 0 || $offset > 0) {
877 $sql .= $this->
db->plimit($limit + 1, $offset);
880 dol_syslog(get_class($this).
"::getObjectsInCateg", LOG_DEBUG);
883 while ($rec = $this->
db->fetch_array(
$resql)) {
885 $objs[] = $rec[
'fk_'.(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])];
887 $classnameforobj = $this->MAP_OBJ_CLASS[$type];
889 $obj =
new $classnameforobj($this->
db);
890 $obj->fetch($rec[
'fk_'.(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])]);
897 $this->error = $this->
db->error().
' sql='.$sql;
912 $sql =
"SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX.
"categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
913 $sql .=
" WHERE fk_categorie = ".((int) $this->
id).
" AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
" = ".((
int) $object_id);
914 dol_syslog(get_class($this).
"::containsObject", LOG_DEBUG);
917 return $this->
db->fetch_object(
$resql)->nb;
919 $this->error = $this->
db->error().
' sql='.$sql;
935 public function getListForItem($id, $type =
'customer', $sortfield =
"s.rowid", $sortorder =
'ASC', $limit = 0, $page = 0)
939 $categories = array();
944 $subcol_name =
"fk_".$type;
945 if ($type ==
"customer") {
946 $sub_type =
"societe";
947 $subcol_name =
"fk_soc";
949 if ($type ==
"supplier") {
950 $sub_type =
"fournisseur";
951 $subcol_name =
"fk_soc";
953 if ($type ==
"contact") {
954 $subcol_name =
"fk_socpeople";
957 $idoftype = array_search($type, self::$MAP_ID_TO_CODE);
959 $sql =
"SELECT s.rowid";
960 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie as s, ".MAIN_DB_PREFIX.
"categorie_".$sub_type.
" as sub";
961 $sql .=
' WHERE s.entity IN ('.getEntity(
'category').
')';
962 $sql .=
' AND s.type='.((int) $idoftype);
963 $sql .=
' AND s.rowid = sub.fk_categorie';
964 $sql .=
" AND sub.".$subcol_name.
" = ".((int) $id);
966 $sql .= $this->
db->order($sortfield, $sortorder);
970 if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
971 $result = $this->
db->query($sql);
983 $offset = $limit * $page;
985 $sql .= $this->
db->plimit($limit + 1, $offset);
988 $result = $this->
db->query($sql);
991 $num = $this->
db->num_rows($result);
992 $min = min($num, ($limit <= 0 ? $num : $limit));
994 $obj = $this->
db->fetch_object($result);
996 if ($category_static->fetch($obj->rowid)) {
997 $categories[$i][
'id'] = $category_static->id;
998 $categories[$i][
'fk_parent'] = $category_static->fk_parent;
999 $categories[$i][
'label'] = $category_static->label;
1000 $categories[$i][
'description'] = $category_static->description;
1001 $categories[$i][
'color'] = $category_static->color;
1002 $categories[$i][
'socid'] = $category_static->socid;
1003 $categories[$i][
'ref_ext'] = $category_static->ref_ext;
1004 $categories[$i][
'visible'] = $category_static->visible;
1005 $categories[$i][
'type'] = $category_static->type;
1006 $categories[$i][
'entity'] = $category_static->entity;
1007 $categories[$i][
'array_options'] = $category_static->array_options;
1010 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && isset($category_static->multilangs)) {
1011 $categories[$i][
'multilangs'] = $category_static->multilangs;
1017 $this->error = $this->
db->lasterror();
1020 if (!count($categories)) {
1036 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"categorie";
1037 $sql .=
" WHERE fk_parent = ".((int) $this->
id);
1038 $sql .=
" AND entity IN (".getEntity(
'category').
")";
1040 $res = $this->
db->query($sql);
1043 while ($rec = $this->
db->fetch_array($res)) {
1045 $cat->fetch($rec[
'rowid']);
1064 $this->motherof = array();
1067 $sql =
"SELECT fk_parent as id_parent, rowid as id_son";
1068 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie";
1069 $sql .=
" WHERE fk_parent != 0";
1070 $sql .=
" AND entity IN (".getEntity(
'category').
")";
1072 dol_syslog(get_class($this).
"::load_motherof", LOG_DEBUG);
1075 while ($obj = $this->
db->fetch_object(
$resql)) {
1076 $this->motherof[$obj->id_son] = $obj->id_parent;
1108 global $conf, $langs;
1110 if (!is_numeric($type)) {
1111 $type = $this->MAP_ID[$type];
1113 if (is_null($type)) {
1114 $this->error =
'BadValueForParameterType';
1118 if (is_string($markafterid)) {
1119 $markafterid = explode(
',', $markafterid);
1120 } elseif (is_numeric($markafterid)) {
1121 if ($markafterid > 0) {
1122 $markafterid = array($markafterid);
1124 $markafterid = array();
1126 } elseif (!is_array($markafterid)) {
1127 $markafterid = array();
1130 $this->cats = array();
1134 $current_lang = $langs->getDefaultLang();
1137 $sql =
"SELECT DISTINCT c.rowid, c.label, c.ref_ext, c.description, c.color, c.fk_parent, c.visible";
1139 $sql .=
", t.label as label_trans, t.description as description_trans";
1141 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie as c";
1143 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_lang as t ON t.fk_category=c.rowid AND t.lang='".$this->
db->escape($current_lang).
"'";
1145 $sql .=
" WHERE c.entity IN (".getEntity(
'category').
")";
1146 $sql .=
" AND c.type = ".(int) $type;
1148 dol_syslog(get_class($this).
"::get_full_arbo get category list", LOG_DEBUG);
1152 while ($obj = $this->
db->fetch_object(
$resql)) {
1153 $this->cats[$obj->rowid][
'rowid'] = $obj->rowid;
1154 $this->cats[$obj->rowid][
'id'] = $obj->rowid;
1155 $this->cats[$obj->rowid][
'fk_parent'] = $obj->fk_parent;
1156 $this->cats[$obj->rowid][
'label'] = !empty($obj->label_trans) ? $obj->label_trans : $obj->label;
1157 $this->cats[$obj->rowid][
'description'] = !empty($obj->description_trans) ? $obj->description_trans : $obj->description;
1158 $this->cats[$obj->rowid][
'color'] = $obj->color;
1159 $this->cats[$obj->rowid][
'visible'] = $obj->visible;
1160 $this->cats[$obj->rowid][
'ref_ext'] = $obj->ref_ext;
1161 $this->cats[$obj->rowid][
'picto'] =
'category';
1170 dol_syslog(get_class($this).
"::get_full_arbo call to buildPathFromId", LOG_DEBUG);
1171 foreach ($this->cats as $key => $val) {
1177 if (count($markafterid) > 0) {
1178 $keyfiltercatid =
'('.implode(
'|', $markafterid).
')';
1181 $keyfilter1 =
'^'.$keyfiltercatid.
'$';
1182 $keyfilter2 =
'_'.$keyfiltercatid.
'$';
1183 $keyfilter3 =
'^'.$keyfiltercatid.
'_';
1184 $keyfilter4 =
'_'.$keyfiltercatid.
'_';
1185 foreach ($this->cats as $key => $val) {
1186 $test = (preg_match(
'/'.$keyfilter1.
'/', $val[
'fullpath']) || preg_match(
'/'.$keyfilter2.
'/', $val[
'fullpath'])
1187 || preg_match(
'/'.$keyfilter3.
'/', $val[
'fullpath']) || preg_match(
'/'.$keyfilter4.
'/', $val[
'fullpath']));
1189 if (($test && !$include) || (!$test && $include)) {
1190 unset($this->cats[$key]);
1195 dol_syslog(get_class($this).
"::get_full_arbo dol_sort_array", LOG_DEBUG);
1196 $this->cats =
dol_sort_array($this->cats,
'fulllabel',
'asc',
true,
false);
1217 if (!empty($this->cats[$id_categ][
'fullpath'])) {
1219 dol_syslog(get_class($this).
"::buildPathFromId fullpath and fulllabel already defined", LOG_WARNING);
1229 $this->cats[$id_categ][
'fullpath'] =
'_'.$id_categ;
1230 $this->cats[$id_categ][
'fulllabel'] = $this->cats[$id_categ][
'label'];
1231 $i = 0; $cursor_categ = $id_categ;
1233 while ((empty($protection) || $i < $protection) && !empty($this->motherof[$cursor_categ])) {
1235 $this->cats[$id_categ][
'fullpath'] =
'_'.$this->motherof[$cursor_categ].$this->cats[$id_categ][
'fullpath'];
1236 $this->cats[$id_categ][
'fulllabel'] = (empty($this->cats[$this->motherof[$cursor_categ]]) ?
'NotFound' : $this->cats[$this->motherof[$cursor_categ]][
'label']).
' >> '.$this->cats[$id_categ][
'fulllabel'];
1239 $cursor_categ = $this->motherof[$cursor_categ];
1244 $nbunderscore = substr_count($this->cats[$id_categ][
'fullpath'],
'_');
1245 $this->cats[$id_categ][
'level'] = ($nbunderscore ? $nbunderscore :
null);
1260 foreach ($this->cats as $key => $val) {
1261 print
'id: '.$this->cats[$key][
'id'];
1262 print
' label: '.$this->cats[$key][
'label'];
1263 print
' mother: '.$this->cats[$key][
'fk_parent'];
1265 print
' fullpath: '.$this->cats[$key][
'fullpath'];
1266 print
' fulllabel: '.$this->cats[$key][
'fulllabel'];
1283 if (!is_numeric($type)) {
1284 $type = $this->MAP_ID[$type];
1287 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"categorie";
1288 $sql .=
" WHERE entity IN (".getEntity(
'category').
")";
1289 if (!is_null($type)) {
1290 $sql .=
" AND type = ".(int) $type;
1293 $sql .=
" AND fk_parent = 0";
1296 $res = $this->
db->query($sql);
1299 while ($rec = $this->
db->fetch_array($res)) {
1301 $cat->fetch($rec[
'rowid']);
1302 $cats[$rec[
'rowid']] = $cat;
1333 $type = $this->type;
1335 if (!is_numeric($type)) {
1336 $type = $this->MAP_ID[$type];
1342 $sql =
"SELECT c.rowid";
1343 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie as c ";
1344 $sql .=
" WHERE c.entity IN (".getEntity(
'category').
")";
1345 $sql .=
" AND c.type = ".((int) $type);
1346 $sql .=
" AND c.fk_parent = ".((int) $this->fk_parent);
1347 $sql .=
" AND c.label = '".$this->db->escape($this->label).
"'";
1349 dol_syslog(get_class($this).
"::already_exists", LOG_DEBUG);
1352 if ($this->
db->num_rows(
$resql) > 0) {
1353 $obj = $this->
db->fetch_array(
$resql);
1359 if ($obj[0] > 0 && $obj[0] != $this->
id) {
1360 dol_syslog(get_class($this).
"::already_exists category with name=".$this->label.
" and parent ".$this->fk_parent.
" exists: rowid=".$obj[0].
" current_id=".$this->id, LOG_DEBUG);
1364 dol_syslog(get_class($this).
"::already_exists no category with same name=".$this->label.
" and same parent ".$this->fk_parent.
" than category id=".$this->id, LOG_DEBUG);
1367 $this->error = $this->
db->error();
1384 public function print_all_ways($sep =
'>>', $url =
'', $nocolor = 0, $addpicto = 0)
1390 foreach ($allways as $way) {
1394 foreach ($way as $cat) {
1397 if (empty($nocolor)) {
1398 $forced_color =
'colortoreplace';
1399 if ($i == count($way)) {
1401 $forced_color =
'categtextwhite';
1404 $forced_color =
'categtextblack';
1411 $link =
'<a href="'.DOL_URL_ROOT.
'/categories/viewcat.php?id='.$cat->id.
'&type='.$cat->type.
'" class="'.$forced_color.
'">';
1413 $w[] = $link.(($addpicto && $i == 1) ?
img_object(
'',
'category',
'class="paddingright"') :
'').$cat->label.$linkend;
1414 } elseif ($url ==
'none') {
1415 $link =
'<span class="'.$forced_color.
'">';
1416 $linkend =
'</span>';
1417 $w[] = $link.(($addpicto && $i == 1) ?
img_object(
'',
'category',
'class="paddingright"') :
'').$cat->label.$linkend;
1419 $w[] =
'<a class="'.$forced_color.
'" href="'.DOL_URL_ROOT.
'/'.$url.
'?catid='.$cat->id.
'">'.($addpicto ?
img_object(
'',
'category') :
'').$cat->label.
'</a>';
1422 $newcategwithpath = preg_replace(
'/colortoreplace/', $forced_color, implode(
'<span class="inline-block valignmiddle paddingleft paddingright '.$forced_color.
'">'.$sep.
'</span>', $w));
1424 $ways[] = $newcategwithpath;
1442 $sql =
"SELECT fk_parent FROM ".MAIN_DB_PREFIX.
"categorie";
1443 $sql .=
" WHERE rowid = ".((int) $this->
id);
1445 $res = $this->
db->query($sql);
1448 while ($rec = $this->
db->fetch_array($res)) {
1449 if ($rec[
'fk_parent'] > 0) {
1451 $cat->fetch($rec[
'fk_parent']);
1475 if (is_array($parents)) {
1476 foreach ($parents as $parent) {
1477 $allways = $parent->get_all_ways();
1478 foreach ($allways as $way) {
1486 if (count($ways) == 0) {
1487 $ways[0][0] = $this;
1507 if (is_numeric($type)) {
1508 $type = Categorie::$MAP_ID_TO_CODE[$type];
1511 if ($type === Categorie::TYPE_BANK_LINE) {
1513 $sql =
"SELECT c.label, c.rowid";
1514 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank_class as a, ".MAIN_DB_PREFIX.
"bank_categ as c";
1515 $sql .=
" WHERE a.lineid=".((int) $id).
" AND a.fk_categ = c.rowid";
1516 $sql .=
" AND c.entity IN (".getEntity(
'category').
")";
1517 $sql .=
" ORDER BY c.label";
1519 $res = $this->
db->query($sql);
1521 while ($obj = $this->
db->fetch_object($res)) {
1522 if ($mode ==
'id') {
1523 $cats[] = $obj->rowid;
1524 } elseif ($mode ==
'label') {
1525 $cats[] = $obj->label;
1528 $cat->id = $obj->rowid;
1529 $cat->label = $obj->label;
1538 $sql =
"SELECT ct.fk_categorie, c.label, c.rowid";
1539 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]).
" as ct, ".MAIN_DB_PREFIX.
"categorie as c";
1540 $sql .=
" WHERE ct.fk_categorie = c.rowid AND ct.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).
" = ".(
int) $id;
1544 $sql .=
" AND c.entity IN (".getEntity(
'category').
")";
1546 $res = $this->
db->query($sql);
1548 while ($obj = $this->
db->fetch_object($res)) {
1549 if ($mode ==
'id') {
1550 $cats[] = $obj->rowid;
1551 } elseif ($mode ==
'label') {
1552 $cats[] = $obj->label;
1555 $cat->fetch($obj->fk_categorie);
1579 public function rechercher($id, $nom, $type, $exact =
false, $case =
false)
1582 if (is_numeric($type)) {
1583 dol_syslog(__METHOD__.
': using numeric types is deprecated.', LOG_WARNING);
1589 if (is_numeric($type)) {
1591 $map_type = array_flip($this->MAP_ID);
1592 $type = $map_type[$type];
1593 dol_syslog(get_class($this).
"::rechercher(): numeric types are deprecated, please use string instead", LOG_WARNING);
1597 $sql =
"SELECT rowid FROM ".MAIN_DB_PREFIX.
"categorie";
1598 $sql .=
" WHERE type = ".((int) $this->MAP_ID[$type]);
1599 $sql .=
" AND entity IN (".getEntity(
'category').
")";
1602 $nom =
'%'.$this->db->escape(str_replace(
'*',
'%', $nom)).
'%';
1605 $sql .=
" AND label LIKE '".$this->db->escape($nom).
"'";
1607 $sql .=
" AND label LIKE BINARY '".$this->db->escape($nom).
"'";
1611 $sql .=
" AND rowid = ".((int) $id);
1614 $res = $this->
db->query($sql);
1616 while ($rec = $this->
db->fetch_array($res)) {
1618 $cat->fetch($rec[
'rowid']);
1624 $this->error = $this->
db->error().
' sql='.$sql;
1639 public function getNomUrl($withpicto = 0, $option =
'', $maxlength = 0, $moreparam =
'')
1641 global $langs, $hookmanager;
1644 $label = $langs->trans(
"ShowCategory").
': '.($this->
ref ? $this->
ref : $this->label);
1647 $forced_color =
'categtextwhite';
1650 $forced_color =
'categtextblack';
1654 $link =
'<a href="'.DOL_URL_ROOT.
'/categories/viewcat.php?id='.$this->
id.
'&type='.$this->
type.$moreparam.
'&backtopage='.urlencode($_SERVER[
'PHP_SELF'].($moreparam ?
'?'.$moreparam :
'')).
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip '.$forced_color.
'">';
1657 $picto =
'category';
1661 $result .= ($link.img_object($label, $picto,
'class="classfortooltip"').$linkend);
1663 if ($withpicto && $withpicto != 2) {
1666 if ($withpicto != 2) {
1667 $result .= $link.dol_trunc(($this->
ref ? $this->
ref : $this->label), $maxlength).$linkend;
1670 $hookmanager->initHooks(array($this->element .
'dao'));
1671 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1672 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1674 $result = $hookmanager->resPrint;
1676 $result .= $hookmanager->resPrint;
1693 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1695 $dir = $sdir.
'/'.
get_exdir($this->
id, 2, 0, 0, $this,
'category').$this->id.
"/";
1698 if (!file_exists($dir)) {
1702 if (file_exists($dir)) {
1703 if (is_array($file[
'name']) && count($file[
'name']) > 0) {
1704 $nbfile = count($file[
'name']);
1705 for ($i = 0; $i <= $nbfile; $i++) {
1706 $originImage = $dir.$file[
'name'][$i];
1711 if (file_exists($originImage)) {
1717 $originImage = $dir.$file[
'name'];
1722 if (file_exists($originImage)) {
1741 include_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1746 $dirthumb = $dir.
'thumbs/';
1748 if (file_exists($dir)) {
1749 $handle = opendir($dir);
1750 if (is_resource($handle)) {
1751 while (($file = readdir($handle)) !==
false) {
1752 if (
dol_is_file($dir.$file) && preg_match(
'/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) {
1757 $photo_vignette =
'';
1758 if (preg_match(
'/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) {
1759 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $photo).
'_small'.$regs[0];
1764 $obj[
'photo'] = $photo;
1765 if ($photo_vignette && is_file($dirthumb.$photo_vignette)) {
1766 $obj[
'photo_vignette'] =
'thumbs/'.$photo_vignette;
1768 $obj[
'photo_vignette'] =
"";
1771 $tabobj[$nbphoto - 1] = $obj;
1774 if ($nbmax && $nbphoto >= $nbmax) {
1797 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
1799 $dir = dirname($file).
'/';
1800 $dirthumb = $dir.
'/thumbs/';
1801 $filename = preg_replace(
'/'.preg_quote($dir,
'/').
'/i',
'', $file);
1807 if (preg_match(
'/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $filename, $regs)) {
1808 $photo_vignette = preg_replace(
'/'.$regs[0].
'/i',
'', $filename).
'_small'.$regs[0];
1809 if (file_exists($dirthumb.$photo_vignette)) {
1825 $infoImg = getimagesize($file);
1826 $this->imgWidth = $infoImg[0];
1827 $this->imgHeight = $infoImg[1];
1841 $langs_available = $langs->get_available_languages();
1842 $current_lang = $langs->getDefaultLang();
1844 foreach ($langs_available as $key => $value) {
1845 $sql =
"SELECT rowid";
1846 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie_lang";
1847 $sql .=
" WHERE fk_category=".((int) $this->
id);
1848 $sql .=
" AND lang = '".$this->db->escape($key).
"'";
1850 $result = $this->
db->query($sql);
1852 if ($key == $current_lang) {
1853 if ($this->
db->num_rows($result)) {
1854 $sql2 =
"UPDATE ".MAIN_DB_PREFIX.
"categorie_lang";
1855 $sql2 .=
" SET label = '".$this->db->escape($this->label).
"',";
1856 $sql2 .=
" description = '".$this->db->escape($this->
description).
"'";
1857 $sql2 .=
" WHERE fk_category = ".((int) $this->
id).
" AND lang = '".$this->
db->escape($key).
"'";
1859 $sql2 =
"INSERT INTO ".MAIN_DB_PREFIX.
"categorie_lang (fk_category, lang, label, description)";
1860 $sql2 .=
" VALUES(".((int) $this->
id).
", '".$this->
db->escape($key).
"', '".$this->
db->escape($this->label).
"'";
1861 $sql2 .=
", '".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"')";
1863 dol_syslog(get_class($this).
'::setMultiLangs', LOG_DEBUG);
1864 if (!$this->
db->query($sql2)) {
1865 $this->error = $this->
db->lasterror();
1868 } elseif (isset($this->multilangs[
"$key"])) {
1869 if ($this->
db->num_rows($result)) {
1870 $sql2 =
"UPDATE ".MAIN_DB_PREFIX.
"categorie_lang";
1871 $sql2 .=
" SET label='".$this->db->escape($this->multilangs[
"$key"][
"label"]).
"',";
1872 $sql2 .=
" description='".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"'";
1873 $sql2 .=
" WHERE fk_category=".((int) $this->
id).
" AND lang='".$this->
db->escape($key).
"'";
1875 $sql2 =
"INSERT INTO ".MAIN_DB_PREFIX.
"categorie_lang (fk_category, lang, label, description)";
1876 $sql2 .=
" VALUES(".((int) $this->
id).
", '".$this->
db->escape($key).
"', '".$this->
db->escape($this->multilangs[
"$key"][
"label"]).
"'";
1877 $sql2 .=
",'".$this->db->escape($this->multilangs[
"$key"][
"description"]).
"')";
1881 if ($this->multilangs[
"$key"][
"label"] || $this->multilangs[
"$key"][
"description"] || $this->multilangs[
"$key"][
"note"]) {
1882 dol_syslog(get_class($this).
'::setMultiLangs', LOG_DEBUG);
1884 if (!$this->
db->query($sql2)) {
1885 $this->error = $this->
db->lasterror();
1892 $result = $this->
call_trigger(
'CATEGORY_SET_MULTILANGS', $user);
1894 $this->error = $this->
db->lasterror();
1911 $current_lang = $langs->getDefaultLang();
1913 $sql =
"SELECT lang, label, description";
1914 $sql .=
" FROM ".MAIN_DB_PREFIX.
"categorie_lang";
1915 $sql .=
" WHERE fk_category=".((int) $this->
id);
1917 $result = $this->
db->query($sql);
1919 while ($obj = $this->
db->fetch_object($result)) {
1921 if ($obj->lang == $current_lang) {
1922 $this->label = $obj->label;
1925 $this->multilangs[
"$obj->lang"][
"label"] = $obj->label;
1926 $this->multilangs[
"$obj->lang"][
"description"] = $obj->description;
1930 $this->error = $langs->trans(
"Error").
" : ".$this->
db->error().
" - ".$sql;
1956 dol_syslog(get_class($this).
"::initAsSpecimen");
1960 $this->fk_parent = 0;
1961 $this->label =
'SPECIMEN';
1962 $this->specimen = 1;
1965 $this->
type = self::TYPE_PRODUCT;
1995 if ($type ==
'bank_account') {
1999 return " LEFT JOIN ".MAIN_DB_PREFIX.
"categorie_".$type.
" as cp ON ".$rowIdName.
" = cp.fk_".$type;
2013 if ($type ==
'bank_account') {
2016 if ($type ==
'customer') {
2019 if ($type ==
'supplier') {
2020 $type =
'fournisseur';
2023 if (empty($searchList) && !is_array($searchList)) {
2027 $searchCategorySqlList = array();
2028 foreach ($searchList as $searchCategory) {
2029 if (intval($searchCategory) == -2) {
2030 $searchCategorySqlList[] =
" cp.fk_categorie IS NULL";
2031 } elseif (intval($searchCategory) > 0) {
2032 $searchCategorySqlList[] =
" ".$rowIdName.
" IN (SELECT fk_".$type.
" FROM ".MAIN_DB_PREFIX.
"categorie_".$type.
" WHERE fk_categorie = ".((int) $searchCategory).
")";
2036 if (!empty($searchCategorySqlList)) {
2037 return " AND (".implode(
' AND ', $searchCategorySqlList).
")";
2050 dol_syslog(get_class($this).
"::count_all_categories", LOG_DEBUG);
2051 $sql =
"SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX.
"categorie";
2052 $sql .=
" WHERE entity IN (".getEntity(
'category').
")";
2054 $res = $this->
db->query($sql);
2056 $obj = $this->
db->fetch_object($res);