dolibarr 21.0.0-alpha
categorie.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
3 * Copyright (C) 2005 Davoleau Brice <brice.davoleau@gmail.com>
4 * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5 * Copyright (C) 2006-2012 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
8 * Copyright (C) 2013-2016 Juanjo Menent <jmenent@2byte.es>
9 * Copyright (C) 2013-2018 Philippe Grand <philippe.grand@atoo-net.com>
10 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
11 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
12 * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
13 * Copyright (C) 2018-2024 Frédéric France <frederic.france@free.fr>
14 * Copyright (C) 2023-2024 Benjamin Falière <benjamin.faliere@altairis.fr>
15 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
37require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
38require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
39require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
40require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
41require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
42require_once DOL_DOCUMENT_ROOT.'/knowledgemanagement/class/knowledgerecord.class.php';
43
44
49{
50 // Categories types (we use string because we want to accept any modules/types in a future)
51 const TYPE_PRODUCT = 'product';
52 const TYPE_SUPPLIER = 'supplier';
53 const TYPE_CUSTOMER = 'customer';
54 const TYPE_MEMBER = 'member';
55 const TYPE_CONTACT = 'contact';
56 const TYPE_USER = 'user';
57 const TYPE_PROJECT = 'project';
58 const TYPE_ACCOUNT = 'bank_account';
59 const TYPE_BANK_LINE = 'bank_line';
60 const TYPE_WAREHOUSE = 'warehouse';
61 const TYPE_ACTIONCOMM = 'actioncomm';
62 const TYPE_WEBSITE_PAGE = 'website_page';
63 const TYPE_TICKET = 'ticket';
64 const TYPE_KNOWLEDGEMANAGEMENT = 'knowledgemanagement';
65
69 public $picto = 'category';
70
71
75 protected $MAP_ID = array(
76 'product' => 0,
77 'supplier' => 1,
78 'customer' => 2,
79 'member' => 3,
80 'contact' => 4,
81 'bank_account' => 5,
82 'project' => 6,
83 'user' => 7,
84 'bank_line' => 8,
85 'warehouse' => 9,
86 'actioncomm' => 10,
87 'website_page' => 11,
88 'ticket' => 12,
89 'knowledgemanagement' => 13
90 );
91
97 public static $MAP_ID_TO_CODE = array(
98 0 => 'product',
99 1 => 'supplier',
100 2 => 'customer',
101 3 => 'member',
102 4 => 'contact',
103 5 => 'bank_account',
104 6 => 'project',
105 7 => 'user',
106 8 => 'bank_line',
107 9 => 'warehouse',
108 10 => 'actioncomm',
109 11 => 'website_page',
110 12 => 'ticket',
111 13 => 'knowledgemanagement'
112 );
113
119 public $MAP_CAT_FK = array(
120 'customer' => 'soc',
121 'supplier' => 'soc',
122 'contact' => 'socpeople',
123 'bank_account' => 'account',
124 );
125
131 public $MAP_CAT_TABLE = array(
132 'customer' => 'societe',
133 'supplier' => 'fournisseur',
134 'bank_account' => 'account',
135 );
136
142 public $MAP_OBJ_CLASS = array(
143 'product' => 'Product',
144 'customer' => 'Societe',
145 'supplier' => 'Fournisseur',
146 'member' => 'Adherent',
147 'contact' => 'Contact',
148 'user' => 'User',
149 'account' => 'Account', // old for bank account
150 'bank_account' => 'Account',
151 'project' => 'Project',
152 'warehouse' => 'Entrepot',
153 'actioncomm' => 'ActionComm',
154 'website_page' => 'WebsitePage',
155 'ticket' => 'Ticket',
156 'knowledgemanagement' => 'KnowledgeRecord'
157 );
158
164 public static $MAP_TYPE_TITLE_AREA = array(
165 'product' => 'ProductsCategoriesArea',
166 'customer' => 'CustomersCategoriesArea',
167 'supplier' => 'SuppliersCategoriesArea',
168 'member' => 'MembersCategoriesArea',
169 'contact' => 'ContactsCategoriesArea',
170 'user' => 'UsersCategoriesArea',
171 'account' => 'AccountsCategoriesArea', // old for bank account
172 'bank_account' => 'AccountsCategoriesArea',
173 'project' => 'ProjectsCategoriesArea',
174 'warehouse' => 'StocksCategoriesArea',
175 'actioncomm' => 'ActioncommCategoriesArea',
176 'website_page' => 'WebsitePageCategoriesArea'
177 );
178
183 public $MAP_OBJ_TABLE = array(
184 'customer' => 'societe',
185 'supplier' => 'societe',
186 'member' => 'adherent',
187 'contact' => 'socpeople',
188 'account' => 'bank_account', // old for bank account
189 'project' => 'projet',
190 'warehouse' => 'entrepot',
191 'knowledgemanagement' => 'knowledgemanagement_knowledgerecord'
192 );
193
197 public $element = 'category';
198
202 public $table_element = 'categorie';
203
207 public $fk_parent;
208
212 public $label;
213
217 public $description;
218
222 public $color;
223
227 public $position;
228
232 public $visible;
233
237 public $socid;
238
256 public $type;
257
261 public $cats = array();
262
266 public $motherof = array();
267
271 public $childs = array();
272
276 public $multilangs;
277
281 public $imgWidth;
282
286 public $imgHeight;
287
293 public function __construct($db)
294 {
295 global $hookmanager;
296
297 $this->db = $db;
298
299 if (is_object($hookmanager)) {
300 $hookmanager->initHooks(array('category'));
301 $parameters = array();
302 $reshook = $hookmanager->executeHooks('constructCategory', $parameters, $this); // Note that $action and $object may have been modified by some hooks
303 if ($reshook >= 0 && !empty($hookmanager->resArray)) {
304 foreach ($hookmanager->resArray as $mapList) {
305 $mapId = $mapList['id'];
306 $mapCode = $mapList['code'];
307 self::$MAP_ID_TO_CODE[$mapId] = $mapCode;
308 $this->MAP_ID[$mapCode] = $mapId;
309 $this->MAP_CAT_FK[$mapCode] = isset($mapList['cat_fk']) ? $mapList['cat_fk'] : null;
310 $this->MAP_CAT_TABLE[$mapCode] = isset($mapList['cat_table']) ? $mapList['cat_table'] : null;
311 $this->MAP_OBJ_CLASS[$mapCode] = $mapList['obj_class'];
312 $this->MAP_OBJ_TABLE[$mapCode] = $mapList['obj_table'];
313 }
314 }
315 }
316 }
317
323 public function getMapList()
324 {
325 $mapList = array();
326
327 foreach ($this->MAP_ID as $mapCode => $mapId) {
328 $mapList[] = array(
329 'id' => $mapId,
330 'code' => $mapCode,
331 'cat_fk' => (empty($this->MAP_CAT_FK[$mapCode]) ? $mapCode : $this->MAP_CAT_FK[$mapCode]),
332 'cat_table' => (empty($this->MAP_CAT_TABLE[$mapCode]) ? $mapCode : $this->MAP_CAT_TABLE[$mapCode]),
333 'obj_class' => (empty($this->MAP_OBJ_CLASS[$mapCode]) ? $mapCode : $this->MAP_OBJ_CLASS[$mapCode]),
334 'obj_table' => (empty($this->MAP_OBJ_TABLE[$mapCode]) ? $mapCode : $this->MAP_OBJ_TABLE[$mapCode])
335 );
336 }
337
338 return $mapList;
339 }
340
350 public function fetch($id, $label = '', $type = null, $ref_ext = '')
351 {
352 // Check parameters
353 if (empty($id) && empty($label) && empty($ref_ext)) {
354 $this->error = "No category to search for";
355 return -1;
356 }
357 if (!is_null($type) && !is_numeric($type)) {
358 $type = $this->MAP_ID[$type];
359 }
360
361 $sql = "SELECT rowid, fk_parent, entity, label, description, color, position, fk_soc, visible, type, ref_ext";
362 $sql .= ", date_creation, tms, fk_user_creat, fk_user_modif";
363 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
364 if ($id) {
365 $sql .= " WHERE rowid = ".((int) $id);
366 } elseif (!empty($ref_ext)) {
367 $sql .= " WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."'";
368 } else {
369 $sql .= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")";
370 if (!is_null($type)) {
371 $sql .= " AND type = ".((int) $type);
372 }
373 }
374
375 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
376 $resql = $this->db->query($sql);
377 if ($resql) {
378 if ($this->db->num_rows($resql) > 0 && $res = $this->db->fetch_array($resql)) {
379 $this->id = $res['rowid'];
380 //$this->ref = $res['rowid'];
381 $this->fk_parent = (int) $res['fk_parent'];
382 $this->label = $res['label'];
383 $this->description = $res['description'];
384 $this->color = $res['color'];
385 $this->position = $res['position'];
386 $this->socid = (int) $res['fk_soc'];
387 $this->visible = (int) $res['visible'];
388 $this->type = $res['type'];
389 $this->ref_ext = $res['ref_ext'];
390 $this->entity = (int) $res['entity'];
391 $this->date_creation = $this->db->jdate($res['date_creation']);
392 $this->date_modification = $this->db->jdate($res['tms']);
393 $this->user_creation_id = (int) $res['fk_user_creat'];
394 $this->user_modification_id = (int) $res['fk_user_modif'];
395
396 // Retrieve all extrafield
397 // fetch optionals attributes and labels
398 $this->fetch_optionals();
399
400 $this->db->free($resql);
401
402 // multilangs
403 if (getDolGlobalInt('MAIN_MULTILANGS')) {
404 $this->getMultiLangs();
405 }
406
407 return 1;
408 } else {
409 $this->error = "No category found";
410 return 0;
411 }
412 } else {
413 dol_print_error($this->db);
414 $this->error = $this->db->lasterror;
415 $this->errors[] = $this->db->lasterror;
416 return -1;
417 }
418 }
419
430 public function create($user, $notrigger = 0)
431 {
432 global $conf, $langs, $hookmanager;
433 $langs->load('categories');
434
435 $type = $this->type;
436
437 if (!is_numeric($type)) {
438 $type = $this->MAP_ID[$type];
439 }
440
441 $error = 0;
442
443 dol_syslog(get_class($this).'::create', LOG_DEBUG);
444
445 // Clean parameters
446 $this->label = trim($this->label);
447 $this->description = trim($this->description);
448 $this->color = trim($this->color);
449 $this->position = (int) $this->position;
450 $this->import_key = trim($this->import_key);
451 $this->ref_ext = trim($this->ref_ext);
452 if (empty($this->visible)) {
453 $this->visible = 0;
454 }
455 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
456
457 if ($this->already_exists()) {
458 $this->error = $langs->trans("ImpossibleAddCat", $this->label);
459 $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel");
460 dol_syslog($this->error, LOG_WARNING);
461 return -4;
462 }
463
464 $this->db->begin();
465 $now = dol_now();
466 $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (";
467 $sql .= "fk_parent,";
468 $sql .= " label,";
469 $sql .= " description,";
470 $sql .= " color,";
471 $sql .= " position,";
472 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
473 $sql .= "fk_soc,";
474 }
475 $sql .= " visible,";
476 $sql .= " type,";
477 $sql .= " import_key,";
478 $sql .= " ref_ext,";
479 $sql .= " entity,";
480 $sql .= " date_creation,";
481 $sql .= " fk_user_creat";
482 $sql .= ") VALUES (";
483 $sql .= (int) $this->fk_parent.",";
484 $sql .= "'".$this->db->escape($this->label)."', ";
485 $sql .= "'".$this->db->escape($this->description)."', ";
486 $sql .= "'".$this->db->escape($this->color)."', ";
487 $sql .= (int) $this->position.",";
488 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
489 $sql .= ($this->socid > 0 ? $this->socid : 'null').", ";
490 }
491 $sql .= "'".$this->db->escape($this->visible)."', ";
492 $sql .= ((int) $type).", ";
493 $sql .= (!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : 'null').", ";
494 $sql .= (!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : 'null').", ";
495 $sql .= (int) $conf->entity.", ";
496 $sql .= "'".$this->db->idate($now)."', ";
497 $sql .= (int) $user->id;
498 $sql .= ")";
499
500 $res = $this->db->query($sql);
501 if ($res) {
502 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie");
503
504 if ($id > 0) {
505 $this->id = $id;
506
507 $action = 'create';
508
509 // Actions on extra fields
510 if (!$error) {
511 $result = $this->insertExtraFields();
512 if ($result < 0) {
513 $error++;
514 }
515 }
516
517 if (!$error && !$notrigger) {
518 // Call trigger
519 $result = $this->call_trigger('CATEGORY_CREATE', $user);
520 if ($result < 0) {
521 $error++;
522 }
523 // End call triggers
524 }
525
526 if (!$error) {
527 $this->db->commit();
528 return $id;
529 } else {
530 $this->db->rollback();
531 return -3;
532 }
533 } else {
534 $this->db->rollback();
535 return -2;
536 }
537 } else {
538 $this->error = $this->db->error();
539 $this->db->rollback();
540 return -1;
541 }
542 }
543
553 public function update(User $user, $notrigger = 0)
554 {
555 global $langs;
556
557 $error = 0;
558
559 // Clean parameters
560 $this->label = trim($this->label);
561 $this->description = trim($this->description);
562 $this->ref_ext = trim($this->ref_ext);
563 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
564 $this->visible = ($this->visible != "" ? intval($this->visible) : 0);
565
566 if ($this->already_exists()) {
567 $this->error = $langs->trans("ImpossibleUpdateCat");
568 $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel");
569 return -1;
570 }
571
572 $this->db->begin();
573
574 $sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
575 $sql .= " SET label = '".$this->db->escape($this->label)."',";
576 $sql .= " description = '".$this->db->escape($this->description)."',";
577 $sql .= " ref_ext = '".$this->db->escape($this->ref_ext)."',";
578 $sql .= " color = '".$this->db->escape($this->color)."'";
579 $sql .= ", position = ".(int) $this->position;
580 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
581 $sql .= ", fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
582 }
583 $sql .= ", visible = ".(int) $this->visible;
584 $sql .= ", fk_parent = ".(int) $this->fk_parent;
585 $sql .= ", fk_user_modif = ".(int) $user->id;
586 $sql .= " WHERE rowid = ".((int) $this->id);
587
588 dol_syslog(get_class($this)."::update", LOG_DEBUG);
589 if ($this->db->query($sql)) {
590 $action = 'update';
591
592 // Actions on extra fields
593 if (!$error) {
594 $result = $this->insertExtraFields();
595 if ($result < 0) {
596 $error++;
597 }
598 }
599
600 if (!$error && !$notrigger) {
601 // Call trigger
602 $result = $this->call_trigger('CATEGORY_MODIFY', $user);
603 if ($result < 0) {
604 $error++;
605 }
606 // End call triggers
607 }
608
609 if (!$error) {
610 $this->db->commit();
611 return 1;
612 } else {
613 $this->db->rollback();
614 return -1;
615 }
616 } else {
617 $this->db->rollback();
618 dol_print_error($this->db);
619 return -1;
620 }
621 }
622
630 public function delete($user, $notrigger = 0)
631 {
632 $error = 0;
633
634 // Clean parameters
635 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
636
637 dol_syslog(get_class($this)."::remove");
638
639 $this->db->begin();
640
641 if (!$error && !$notrigger) {
642 // Call trigger
643 $result = $this->call_trigger('CATEGORY_DELETE', $user);
644 if ($result < 0) {
645 $error++;
646 }
647 // End call triggers
648 }
649
650 /* FIX #1317 : Check for child category and move up 1 level*/
651 if (!$error) {
652 $sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
653 $sql .= " SET fk_parent = ".((int) $this->fk_parent);
654 $sql .= " WHERE fk_parent = ".((int) $this->id);
655
656 if (!$this->db->query($sql)) {
657 $this->error = $this->db->lasterror();
658 $error++;
659 }
660 }
661
662 $arraydelete = array(
663 'categorie_account' => 'fk_categorie',
664 'categorie_actioncomm' => 'fk_categorie',
665 'categorie_contact' => 'fk_categorie',
666 'categorie_fournisseur' => 'fk_categorie',
667 'categorie_knowledgemanagement' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('knowledgemanagement')),
668 'categorie_member' => 'fk_categorie',
669 'categorie_user' => 'fk_categorie',
670 'categorie_product' => 'fk_categorie',
671 'categorie_project' => 'fk_categorie',
672 'categorie_societe' => 'fk_categorie',
673 'categorie_ticket' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('ticket')),
674 'categorie_warehouse' => 'fk_categorie',
675 'categorie_website_page' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('website')),
676 'bank_class' => 'fk_categ',
677 'categorie_lang' => 'fk_category',
678 'categorie' => 'rowid',
679 );
680 foreach ($arraydelete as $key => $value) {
681 if (is_array($value)) {
682 if (empty($value['enabled'])) {
683 continue;
684 }
685 $value = $value['field'];
686 }
687 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$key;
688 $sql .= " WHERE ".$value." = ".((int) $this->id);
689 if (!$this->db->query($sql)) {
690 $this->errors[] = $this->db->lasterror();
691 dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
692 $error++;
693 }
694 }
695
696 // Removed extrafields
697 if (!$error) {
698 $result = $this->deleteExtraFields();
699 if ($result < 0) {
700 $error++;
701 dol_syslog(get_class($this)."::delete erreur ".$this->error, LOG_ERR);
702 }
703 }
704
705 if (!$error) {
706 $this->db->commit();
707 return 1;
708 } else {
709 $this->db->rollback();
710 return -1;
711 }
712 }
713
714 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
723 public function add_type($obj, $type = '')
724 {
725 // phpcs:enable
726 global $user;
727
728 $error = 0;
729
730 if ($this->id == -1) {
731 return -2;
732 }
733
734 if (empty($type)) {
735 $type = $obj->element;
736 }
737
738 dol_syslog(get_class($this).'::add_type', LOG_DEBUG);
739
740 $this->db->begin();
741
742 $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
743 $sql .= " (fk_categorie, fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).")";
744 $sql .= " VALUES (".((int) $this->id).", ".((int) $obj->id).")";
745
746 if ($this->db->query($sql)) {
747 if (getDolGlobalString('CATEGORIE_RECURSIV_ADD')) {
748 $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie';
749 $sql .= " WHERE rowid = ".((int) $this->id);
750
751 dol_syslog(get_class($this)."::add_type", LOG_DEBUG);
752 $resql = $this->db->query($sql);
753 if ($resql) {
754 if ($this->db->num_rows($resql) > 0) {
755 $objparent = $this->db->fetch_object($resql);
756
757 if (!empty($objparent->fk_parent)) {
758 $cat = new Categorie($this->db);
759 $cat->id = $objparent->fk_parent;
760 if (!$cat->containsObject($type, $obj->id)) {
761 $result = $cat->add_type($obj, $type);
762 if ($result < 0) {
763 $this->error = $cat->error;
764 $error++;
765 }
766 }
767 }
768 }
769 } else {
770 $error++;
771 $this->error = $this->db->lasterror();
772 }
773
774 if ($error) {
775 $this->db->rollback();
776 return -1;
777 }
778 }
779
780 // Call trigger
781 $this->context = array('linkto' => $obj); // Save object we want to link category to into category instance to provide information to trigger
782 $result = $this->call_trigger('CATEGORY_LINK', $user);
783 if ($result < 0) {
784 $error++;
785 }
786 // End call triggers
787
788 if (!$error) {
789 $this->db->commit();
790 return 1;
791 } else {
792 $this->db->rollback();
793 return -2;
794 }
795 } else {
796 $this->db->rollback();
797 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
798 $this->error = $this->db->lasterrno();
799 return -3;
800 } else {
801 $this->error = $this->db->lasterror();
802 }
803 return -1;
804 }
805 }
806
807 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
816 public function del_type($obj, $type)
817 {
818 // phpcs:enable
819 global $user;
820
821 $error = 0;
822
823 // For backward compatibility
824 if ($type == 'societe') {
825 $type = 'customer';
826 dol_syslog(get_class($this)."::del_type(): type 'societe' is deprecated, please use 'customer' instead", LOG_WARNING);
827 } elseif ($type == 'fournisseur') {
828 $type = 'supplier';
829 dol_syslog(get_class($this)."::del_type(): type 'fournisseur' is deprecated, please use 'supplier' instead", LOG_WARNING);
830 }
831
832 $this->db->begin();
833
834 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
835 $sql .= " WHERE fk_categorie = ".((int) $this->id);
836 $sql .= " AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".((int) $obj->id);
837
838 dol_syslog(get_class($this).'::del_type', LOG_DEBUG);
839 if ($this->db->query($sql)) {
840 // Call trigger
841 $this->context = array('unlinkoff' => $obj); // Save object we want to link category to into category instance to provide information to trigger
842 $result = $this->call_trigger('CATEGORY_UNLINK', $user);
843 if ($result < 0) {
844 $error++;
845 }
846 // End call triggers
847
848 if (!$error) {
849 $this->db->commit();
850 return 1;
851 } else {
852 $this->db->rollback();
853 return -2;
854 }
855 } else {
856 $this->db->rollback();
857 $this->error = $this->db->lasterror();
858 return -1;
859 }
860 }
861
877 public function getObjectsInCateg($type, $onlyids = 0, $limit = 0, $offset = 0, $sortfield = '', $sortorder = 'ASC', $filter = '', $filtermode = 'AND')
878 {
879 global $user;
880
881 $objs = array();
882
883 $classnameforobj = $this->MAP_OBJ_CLASS[$type];
884 $obj = new $classnameforobj($this->db);
885
886 $sql = "SELECT c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." as fk_object";
887 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type])." as c";
888 $sql .= ", ".MAIN_DB_PREFIX.(empty($this->MAP_OBJ_TABLE[$type]) ? $type : $this->MAP_OBJ_TABLE[$type])." as o";
889 $sql .= " WHERE o.entity IN (".getEntity($obj->element).")";
890 $sql .= " AND c.fk_categorie = ".((int) $this->id);
891 // Compatibility with actioncomm table which has id instead of rowid
892 if ((array_key_exists($type, $this->MAP_OBJ_TABLE) && $this->MAP_OBJ_TABLE[$type] == "actioncomm") || $type == "actioncomm") {
893 $sql .= " AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = o.id";
894 } else {
895 $sql .= " AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = o.rowid";
896 }
897 // Protection for external users
898 if (($type == 'customer' || $type == 'supplier') && $user->socid > 0) {
899 $sql .= " AND o.rowid = ".((int) $user->socid);
900 }
901
902 $errormessage = '';
903 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
904 if ($errormessage) {
905 $this->errors[] = $errormessage;
906 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
907 return -1;
908 }
909
910 $sql .= $this->db->order($sortfield, $sortorder);
911 if ($limit > 0 || $offset > 0) {
912 $sql .= $this->db->plimit($limit + 1, $offset);
913 }
914
915 dol_syslog(get_class($this)."::getObjectsInCateg", LOG_DEBUG);
916
917 $resql = $this->db->query($sql);
918 if ($resql) {
919 while ($rec = $this->db->fetch_array($resql)) {
920 if ($onlyids) {
921 $objs[] = $rec['fk_object'];
922 } else {
923 $classnameforobj = $this->MAP_OBJ_CLASS[$type];
924
925 $obj = new $classnameforobj($this->db);
926 $obj->fetch($rec['fk_object']);
927 if ($obj->id > 0) { // Failing fetch may happen for example when a category supplier was set and third party was moved as customer only. The object supplier can't be loaded.
928 $objs[] = $obj;
929 }
930 }
931 }
932 return $objs;
933 } else {
934 $this->error = $this->db->error().' sql='.$sql;
935 return -1;
936 }
937 }
938
947 public function containsObject($type, $object_id)
948 {
949 $sql = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
950 $sql .= " WHERE fk_categorie = ".((int) $this->id)." AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".((int) $object_id);
951
952 dol_syslog(get_class($this)."::containsObject", LOG_DEBUG);
953
954 $resql = $this->db->query($sql);
955 if ($resql) {
956 return $this->db->fetch_object($resql)->nb;
957 } else {
958 $this->error = $this->db->error();
959 return -1;
960 }
961 }
962
974 public function getListForItem($id, $type = 'customer', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
975 {
976 $categories = array();
977
978 $type = sanitizeVal($type, 'aZ09');
979
980 $sub_type = $type;
981 $subcol_name = "fk_".$type;
982 if ($type == "customer") {
983 $sub_type = "societe";
984 $subcol_name = "fk_soc";
985 }
986 if ($type == "supplier") {
987 $sub_type = "fournisseur";
988 $subcol_name = "fk_soc";
989 }
990 if ($type == "contact") {
991 $subcol_name = "fk_socpeople";
992 }
993
994 $idoftype = array_search($type, self::$MAP_ID_TO_CODE);
995
996 $sql = "SELECT s.rowid";
997 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as s, ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub";
998 $sql .= ' WHERE s.entity IN ('.getEntity('category').')';
999 $sql .= ' AND s.type='.((int) $idoftype);
1000 $sql .= ' AND s.rowid = sub.fk_categorie';
1001 $sql .= " AND sub.".$subcol_name." = ".((int) $id);
1002
1003 $sql .= $this->db->order($sortfield, $sortorder);
1004
1005 $offset = 0;
1006 $nbtotalofrecords = '';
1007 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
1008 $result = $this->db->query($sql);
1009 $nbtotalofrecords = $this->db->num_rows($result);
1010 if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
1011 $page = 0;
1012 $offset = 0;
1013 }
1014 }
1015
1016 if ($limit) {
1017 if ($page < 0) {
1018 $page = 0;
1019 }
1020 $offset = $limit * $page;
1021
1022 $sql .= $this->db->plimit($limit + 1, $offset);
1023 }
1024
1025 $result = $this->db->query($sql);
1026 if ($result) {
1027 $i = 0;
1028 $num = $this->db->num_rows($result);
1029 $min = min($num, ($limit <= 0 ? $num : $limit));
1030 while ($i < $min) {
1031 $obj = $this->db->fetch_object($result);
1032 $category_static = new Categorie($this->db);
1033 if ($category_static->fetch($obj->rowid)) {
1034 $categories[$i]['id'] = $category_static->id;
1035 $categories[$i]['fk_parent'] = $category_static->fk_parent;
1036 $categories[$i]['label'] = $category_static->label;
1037 $categories[$i]['description'] = $category_static->description;
1038 $categories[$i]['color'] = $category_static->color;
1039 $categories[$i]['position'] = $category_static->position;
1040 $categories[$i]['socid'] = $category_static->socid;
1041 $categories[$i]['ref_ext'] = $category_static->ref_ext;
1042 $categories[$i]['visible'] = $category_static->visible;
1043 $categories[$i]['type'] = $category_static->type;
1044 $categories[$i]['entity'] = $category_static->entity;
1045 $categories[$i]['array_options'] = $category_static->array_options;
1046
1047 // multilangs
1048 if (getDolGlobalInt('MAIN_MULTILANGS') && isset($category_static->multilangs)) {
1049 $categories[$i]['multilangs'] = $category_static->multilangs;
1050 }
1051 }
1052 $i++;
1053 }
1054 } else {
1055 $this->error = $this->db->lasterror();
1056 return -1;
1057 }
1058 if (!count($categories)) {
1059 return 0;
1060 }
1061
1062 return $categories;
1063 }
1064
1065 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1071 public function get_filles()
1072 {
1073 // phpcs:enable
1074 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1075 $sql .= " WHERE fk_parent = ".((int) $this->id);
1076 $sql .= " AND entity IN (".getEntity('category').")";
1077
1078 $res = $this->db->query($sql);
1079 if ($res) {
1080 $cats = array();
1081 while ($rec = $this->db->fetch_array($res)) {
1082 $cat = new Categorie($this->db);
1083 $cat->fetch($rec['rowid']);
1084 $cats[] = $cat;
1085 }
1086 return $cats;
1087 } else {
1088 dol_print_error($this->db);
1089 return -1;
1090 }
1091 }
1092
1093 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1099 protected function load_motherof()
1100 {
1101 // phpcs:enable
1102 $this->motherof = array();
1103
1104 // Load array[child]=parent
1105 $sql = "SELECT fk_parent as id_parent, rowid as id_son";
1106 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
1107 $sql .= " WHERE fk_parent != 0";
1108 $sql .= " AND entity IN (".getEntity('category').")";
1109
1110 dol_syslog(get_class($this)."::load_motherof", LOG_DEBUG);
1111 $resql = $this->db->query($sql);
1112 if ($resql) {
1113 while ($obj = $this->db->fetch_object($resql)) {
1114 $this->motherof[$obj->id_son] = $obj->id_parent;
1115 }
1116 return 1;
1117 } else {
1118 dol_print_error($this->db);
1119 return -1;
1120 }
1121 }
1122
1123 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1143 public function get_full_arbo($type, $fromid = 0, $include = 0)
1144 {
1145 // phpcs:enable
1146 global $langs;
1147
1148 if (!is_numeric($type)) {
1149 $type = $this->MAP_ID[$type];
1150 }
1151 if (is_null($type)) {
1152 $this->error = 'BadValueForParameterType';
1153 return -1;
1154 }
1155
1156 if (is_string($fromid)) {
1157 $fromid = explode(',', $fromid);
1158 } elseif (is_numeric($fromid)) {
1159 if ($fromid > 0) {
1160 $fromid = array($fromid);
1161 } else {
1162 $fromid = array();
1163 }
1164 } elseif (!is_array($fromid)) {
1165 $fromid = array();
1166 }
1167
1168 $this->cats = array();
1169 $nbcateg = 0;
1170
1171 // Init this->motherof that is array(id_son=>id_parent, ...)
1172 $this->load_motherof();
1173 $current_lang = $langs->getDefaultLang();
1174
1175 // Init $this->cats array
1176 $sql = "SELECT DISTINCT c.rowid, c.label, c.ref_ext, c.description, c.color, c.position, c.fk_parent, c.visible"; // Distinct reduce pb with old tables with duplicates
1177 if (getDolGlobalInt('MAIN_MULTILANGS')) {
1178 $sql .= ", t.label as label_trans, t.description as description_trans";
1179 }
1180 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c";
1181 if (getDolGlobalInt('MAIN_MULTILANGS')) {
1182 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_lang as t ON t.fk_category=c.rowid AND t.lang='".$this->db->escape($current_lang)."'";
1183 }
1184 $sql .= " WHERE c.entity IN (".getEntity('category').")";
1185 $sql .= " AND c.type = ".(int) $type;
1186
1187 dol_syslog(get_class($this)."::get_full_arbo get category list", LOG_DEBUG);
1188 $resql = $this->db->query($sql);
1189 if ($resql) {
1190 $i = 0;
1191 $nbcateg = $this->db->num_rows($resql);
1192
1193 while ($obj = $this->db->fetch_object($resql)) {
1194 $this->cats[$obj->rowid]['rowid'] = $obj->rowid;
1195 $this->cats[$obj->rowid]['id'] = $obj->rowid;
1196 $this->cats[$obj->rowid]['fk_parent'] = $obj->fk_parent;
1197 $this->cats[$obj->rowid]['label'] = !empty($obj->label_trans) ? $obj->label_trans : $obj->label;
1198 $this->cats[$obj->rowid]['description'] = !empty($obj->description_trans) ? $obj->description_trans : $obj->description;
1199 $this->cats[$obj->rowid]['color'] = $obj->color;
1200 $this->cats[$obj->rowid]['position'] = $obj->position;
1201 $this->cats[$obj->rowid]['visible'] = $obj->visible;
1202 $this->cats[$obj->rowid]['ref_ext'] = $obj->ref_ext;
1203 $this->cats[$obj->rowid]['picto'] = 'category';
1204 // fields are filled with buildPathFromId
1205 $this->cats[$obj->rowid]['fullpath'] = '';
1206 $this->cats[$obj->rowid]['fulllabel'] = '';
1207 $i++;
1208 }
1209 } else {
1210 dol_print_error($this->db);
1211 return -1;
1212 }
1213
1214 // We add the fullpath property to each elements of first level (no parent exists)
1215 dol_syslog(get_class($this)."::get_full_arbo call to buildPathFromId", LOG_DEBUG);
1216 foreach ($this->cats as $key => $val) {
1217 //print 'key='.$key.'<br>'."\n";
1218 $this->buildPathFromId($key, $nbcateg); // Process a branch from the root category key (this category has no parent)
1219 }
1220
1221 // Include or exclude leaf (including $fromid) from tree
1222 if (count($fromid) > 0) {
1223 $keyfiltercatid = '('.implode('|', $fromid).')';
1224
1225 //print "Look to discard category ".$fromid."\n";
1226 $keyfilter1 = '^'.$keyfiltercatid.'$';
1227 $keyfilter2 = '_'.$keyfiltercatid.'$';
1228 $keyfilter3 = '^'.$keyfiltercatid.'_';
1229 $keyfilter4 = '_'.$keyfiltercatid.'_';
1230 foreach (array_keys($this->cats) as $key) {
1231 $fullpath = (string) $this->cats[$key]['fullpath'];
1232 $test = (preg_match('/'.$keyfilter1.'/', $fullpath) || preg_match('/'.$keyfilter2.'/', $fullpath)
1233 || preg_match('/'.$keyfilter3.'/', $fullpath) || preg_match('/'.$keyfilter4.'/', $fullpath));
1234
1235 if (($test && !$include) || (!$test && $include)) {
1236 unset($this->cats[$key]);
1237 }
1238 }
1239 }
1240
1241 dol_syslog(get_class($this)."::get_full_arbo dol_sort_array", LOG_DEBUG);
1242 $this->cats = dol_sort_array($this->cats, 'fulllabel', 'asc', true, false);
1243
1244 return $this->cats;
1245 }
1246
1257 private function buildPathFromId($id_categ, $protection = 1000)
1258 {
1259 //dol_syslog(get_class($this)."::buildPathFromId id_categ=".$id_categ." protection=".$protection, LOG_DEBUG);
1260
1261 if (!empty($this->cats[$id_categ]['fullpath'])) {
1262 // Already defined
1263 dol_syslog(get_class($this)."::buildPathFromId fullpath and fulllabel already defined", LOG_WARNING);
1264 return -1;
1265 }
1266
1267 // First build full array $motherof
1268 //$this->load_motherof(); // Disabled because already done by caller of buildPathFromId
1269
1270 // $this->cats[$id_categ] is supposed to be already an array. We just want to complete it with property fullpath and fulllabel
1271
1272 // Define fullpath and fulllabel
1273 $this->cats[$id_categ]['fullpath'] = '_'.$id_categ;
1274 $this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label'];
1275 $i = 0;
1276 $cursor_categ = $id_categ;
1277 //print 'Work for id_categ='.$id_categ.'<br>'."\n";
1278 while ((empty($protection) || $i < $protection) && !empty($this->motherof[$cursor_categ])) {
1279 //print '&nbsp; cursor_categ='.$cursor_categ.' i='.$i.' '.$this->motherof[$cursor_categ].'<br>'."\n";
1280 $this->cats[$id_categ]['fullpath'] = '_'.$this->motherof[$cursor_categ].$this->cats[$id_categ]['fullpath'];
1281 $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'];
1282 //print '&nbsp; Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].' '.$this->cats[$id_categ]['fulllabel'].'<br>'."\n";
1283 $i++;
1284 $cursor_categ = $this->motherof[$cursor_categ];
1285 }
1286 //print 'Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].'<br>'."\n";
1287
1288 // We count number of _ to have level
1289 $nbunderscore = substr_count($this->cats[$id_categ]['fullpath'], '_');
1290 $this->cats[$id_categ]['level'] = ($nbunderscore ? $nbunderscore : null);
1291
1292 return 1;
1293 }
1294
1295
1296 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1304 public function get_all_categories($type = null, $parent = false)
1305 {
1306 // phpcs:enable
1307 if (!is_numeric($type)) {
1308 $type = $this->MAP_ID[$type];
1309 }
1310
1311 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1312 $sql .= " WHERE entity IN (".getEntity('category').")";
1313 if (!is_null($type)) {
1314 $sql .= " AND type = ".(int) $type;
1315 }
1316 if ($parent) {
1317 $sql .= " AND fk_parent = 0";
1318 }
1319
1320 $res = $this->db->query($sql);
1321 if ($res) {
1322 $cats = array();
1323 while ($rec = $this->db->fetch_array($res)) {
1324 $cat = new Categorie($this->db);
1325 $cat->fetch($rec['rowid']);
1326 $cats[$rec['rowid']] = $cat;
1327 }
1328 return $cats;
1329 } else {
1330 dol_print_error($this->db);
1331 return -1;
1332 }
1333 }
1334
1335 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1342 public function get_main_categories($type = null)
1343 {
1344 // phpcs:enable
1345 return $this->get_all_categories($type, true);
1346 }
1347
1348 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1354 public function already_exists()
1355 {
1356 // phpcs:enable
1357 $type = $this->type;
1358
1359 if (!is_numeric($type)) {
1360 $type = $this->MAP_ID[$type];
1361 }
1362
1363 /* We have to select any rowid from llx_categorie which category's mother and label
1364 * are equals to those of the calling category
1365 */
1366 $sql = "SELECT c.rowid";
1367 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c ";
1368 $sql .= " WHERE c.entity IN (".getEntity('category').")";
1369 $sql .= " AND c.type = ".((int) $type);
1370 $sql .= " AND c.fk_parent = ".((int) $this->fk_parent);
1371 $sql .= " AND c.label = '".$this->db->escape($this->label)."'";
1372
1373 dol_syslog(get_class($this)."::already_exists", LOG_DEBUG);
1374
1375 $resql = $this->db->query($sql);
1376 if ($resql) {
1377 if ($this->db->num_rows($resql) > 0) { // Checking for empty resql
1378 $obj = $this->db->fetch_object($resql);
1379 /* If object called create, obj cannot have is id.
1380 * If object called update, he mustn't have the same label as an other category for this mother.
1381 * So if the result has the same id, update is not for label, and if result has an other one, update may be for label.
1382 */
1383 if (!empty($obj) && $obj->rowid > 0 && $obj->rowid != $this->id) {
1384 dol_syslog(get_class($this)."::already_exists category with name=".$this->label." and parent ".$this->fk_parent." exists: rowid=".$obj->rowid." current_id=".$this->id, LOG_DEBUG);
1385 return 1;
1386 }
1387 }
1388 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);
1389 return 0;
1390 } else {
1391 $this->error = $this->db->error();
1392 return -1;
1393 }
1394 }
1395
1396
1397 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1408 public function print_all_ways($sep = '&gt;&gt;', $url = '', $nocolor = 0, $addpicto = 0)
1409 {
1410 // phpcs:enable
1411 $ways = array();
1412
1413 $all_ways = $this->get_all_ways(); // Load array of categories
1414 foreach ($all_ways as $way) {
1415 $w = array();
1416 $i = 0;
1417 $forced_color = '';
1418 foreach ($way as $cat) {
1419 $i++;
1420
1421 if (empty($nocolor)) {
1422 $forced_color = 'colortoreplace';
1423 if ($i == count($way)) { // Last category in hierarchy
1424 // Check contrast with background and correct text color
1425 $forced_color = 'categtextwhite';
1426 if ($cat->color) {
1427 if (colorIsLight($cat->color)) {
1428 $forced_color = 'categtextblack';
1429 }
1430 }
1431 }
1432 }
1433
1434 if ($url == '') {
1435 $link = '<a href="'.DOL_URL_ROOT.'/categories/viewcat.php?id='.$cat->id.'&type='.$cat->type.'" class="'.$forced_color.'">';
1436 $linkend = '</a>';
1437 $w[] = $link.(($addpicto && $i == 1) ? img_object('', 'category', 'class="paddingright"') : '').$cat->label.$linkend;
1438 } elseif ($url == 'none') {
1439 $link = '<span class="'.$forced_color.'">';
1440 $linkend = '</span>';
1441 $w[] = $link.(($addpicto && $i == 1) ? img_object('', 'category', 'class="paddingright"') : '').$cat->label.$linkend;
1442 } else {
1443 $w[] = '<a class="'.$forced_color.'" href="'.DOL_URL_ROOT.'/'.$url.'?catid='.$cat->id.'">'.($addpicto ? img_object('', 'category') : '').$cat->label.'</a>';
1444 }
1445 }
1446 $newcategwithpath = preg_replace('/colortoreplace/', $forced_color, implode('<span class="inline-block valignmiddle paddingleft paddingright '.$forced_color.'">'.$sep.'</span>', $w));
1447
1448 $ways[] = $newcategwithpath;
1449 }
1450
1451 return $ways;
1452 }
1453
1454
1455 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1461 public function get_meres()
1462 {
1463 // phpcs:enable
1464 $parents = array();
1465
1466 $sql = "SELECT fk_parent FROM ".MAIN_DB_PREFIX."categorie";
1467 $sql .= " WHERE rowid = ".((int) $this->id);
1468
1469 $res = $this->db->query($sql);
1470
1471 if ($res) {
1472 while ($rec = $this->db->fetch_array($res)) {
1473 if ($rec['fk_parent'] > 0) {
1474 $cat = new Categorie($this->db);
1475 $cat->fetch($rec['fk_parent']);
1476 $parents[] = $cat;
1477 }
1478 }
1479 return $parents;
1480 } else {
1481 dol_print_error($this->db);
1482 return -1;
1483 }
1484 }
1485
1486 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1493 public function get_all_ways()
1494 {
1495 // phpcs:enable
1496 $ways = array();
1497
1498 $parents = $this->get_meres();
1499 if (is_array($parents)) {
1500 foreach ($parents as $parent) {
1501 $all_ways = $parent->get_all_ways();
1502 foreach ($all_ways as $way) {
1503 $w = $way;
1504 $w[] = $this;
1505 $ways[] = $w;
1506 }
1507 }
1508 }
1509
1510 if (count($ways) == 0) {
1511 $ways[0][0] = $this;
1512 }
1513
1514 return $ways;
1515 }
1516
1527 public function containing($id, $type, $mode = 'object')
1528 {
1529 $cats = array();
1530
1531 if (is_numeric($type)) {
1532 $type = Categorie::$MAP_ID_TO_CODE[$type];
1533 }
1534
1535 if ($type === Categorie::TYPE_BANK_LINE) { // TODO Remove this with standard category code after migration of llx_category_bank into llx_categorie
1536 // Load bank categories
1537 $sql = "SELECT c.label, c.rowid";
1538 $sql .= " FROM ".MAIN_DB_PREFIX."bank_class as a, ".MAIN_DB_PREFIX."category_bank as c";
1539 $sql .= " WHERE a.lineid=".((int) $id)." AND a.fk_categ = c.rowid";
1540 $sql .= " AND c.entity IN (".getEntity('category').")";
1541 $sql .= " ORDER BY c.label";
1542
1543 $res = $this->db->query($sql);
1544 if ($res) {
1545 while ($obj = $this->db->fetch_object($res)) {
1546 if ($mode == 'id') {
1547 $cats[] = $obj->rowid;
1548 } elseif ($mode == 'label') {
1549 $cats[] = $obj->label;
1550 } else {
1551 $cat = new Categorie($this->db);
1552 $cat->id = $obj->rowid;
1553 $cat->label = $obj->label;
1554 $cats[] = $cat;
1555 }
1556 }
1557 } else {
1558 dol_print_error($this->db);
1559 return -1;
1560 }
1561 } else {
1562 $sql = "SELECT ct.fk_categorie, c.label, c.rowid";
1563 $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";
1564 $sql .= " WHERE ct.fk_categorie = c.rowid AND ct.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".(int) $id;
1565 // This seems useless because the table already contains id of category of 1 unique type. So commented.
1566 // So now it works also with external added categories.
1567 //$sql .= " AND c.type = ".((int) $this->MAP_ID[$type]);
1568 $sql .= " AND c.entity IN (".getEntity('category').")";
1569
1570 $res = $this->db->query($sql);
1571 if ($res) {
1572 while ($obj = $this->db->fetch_object($res)) {
1573 if ($mode == 'id') {
1574 $cats[] = $obj->rowid;
1575 } elseif ($mode == 'label') {
1576 $cats[] = $obj->label;
1577 } else {
1578 $cat = new Categorie($this->db);
1579 $cat->fetch($obj->fk_categorie);
1580 $cats[] = $cat;
1581 }
1582 }
1583 } else {
1584 dol_print_error($this->db);
1585 return -1;
1586 }
1587 }
1588
1589 return $cats;
1590 }
1591
1603 public function rechercher($id, $nom, $type, $exact = false, $case = false)
1604 {
1605 // Deprecation warning
1606 if (is_numeric($type)) {
1607 dol_syslog(__METHOD__.': using numeric types is deprecated.', LOG_WARNING);
1608 }
1609
1610 $cats = array();
1611
1612 // For backward compatibility
1613 if (is_numeric($type)) {
1614 // We want to reverse lookup
1615 $map_type = array_flip($this->MAP_ID);
1616 $type = $map_type[$type];
1617 dol_syslog(get_class($this)."::rechercher(): numeric types are deprecated, please use string instead", LOG_WARNING);
1618 }
1619
1620 // Generation requete recherche
1621 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1622 $sql .= " WHERE type = ".((int) $this->MAP_ID[$type]);
1623 $sql .= " AND entity IN (".getEntity('category').")";
1624 if ($nom) {
1625 if (!$exact) {
1626 $nom = '%'.$this->db->escape(str_replace('*', '%', $nom)).'%';
1627 }
1628 if (!$case) {
1629 $sql .= " AND label LIKE '".$this->db->escape($nom)."'";
1630 } else {
1631 $sql .= " AND label LIKE BINARY '".$this->db->escape($nom)."'";
1632 }
1633 }
1634 if ($id) {
1635 $sql .= " AND rowid = ".((int) $id);
1636 }
1637
1638 $res = $this->db->query($sql);
1639 if ($res) {
1640 while ($rec = $this->db->fetch_array($res)) {
1641 $cat = new Categorie($this->db);
1642 $cat->fetch($rec['rowid']);
1643 $cats[] = $cat;
1644 }
1645
1646 return $cats;
1647 } else {
1648 $this->error = $this->db->error().' sql='.$sql;
1649 return -1;
1650 }
1651 }
1652
1659 public function isAnyPhotoAvailable($sdir)
1660 {
1661 include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
1662 include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php';
1663
1664 $sdir .= '/' . get_exdir($this->id, 2, 0, 0, $this, 'category') . $this->id . "/photos/";
1665
1666 $dir_osencoded = dol_osencode($sdir);
1667 if (file_exists($dir_osencoded)) {
1668 $handle = opendir($dir_osencoded);
1669 if (is_resource($handle)) {
1670 while (($file = readdir($handle)) !== false) {
1671 if (!utf8_check($file)) {
1672 $file = mb_convert_encoding($file, 'UTF-8', 'ISO-8859-1'); // To be sure data is stored in UTF8 in memory
1673 }
1674 if (dol_is_file($sdir . $file) && image_format_supported($file) >= 0) {
1675 return true;
1676 }
1677 }
1678 }
1679 }
1680 return false;
1681 }
1682
1689 public function getTooltipContentArray($params)
1690 {
1691 global $langs;
1692
1693 $langs->load('categories');
1694
1695 $datas = [];
1696
1697 $datas['label'] = $langs->trans("ShowCategory").': '.($this->ref ? $this->ref : $this->label);
1698
1699 return $datas;
1700 }
1701
1715 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $moreparam = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = 0)
1716 {
1717 global $conf, $langs, $hookmanager;
1718
1719 if (!empty($conf->dol_no_mouse_hover)) {
1720 $notooltip = 1; // Force disable tooltips
1721 }
1722
1723 $result = '';
1724 $params = [
1725 'id' => $this->id,
1726 'objecttype' => $this->element,
1727 'option' => $option,
1728 ];
1729 $classfortooltip = 'classfortooltip';
1730 $dataparams = '';
1731 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1732 $classfortooltip = 'classforajaxtooltip';
1733 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1734 $label = '';
1735 } else {
1736 $label = implode($this->getTooltipContentArray($params));
1737 }
1738
1739 $url = DOL_URL_ROOT.'/categories/viewcat.php?id='.$this->id.'&type='.$this->type.$moreparam.'&backtopage='.urlencode($_SERVER['PHP_SELF'].($moreparam ? '?'.$moreparam : ''));
1740
1741 if ($option !== 'nolink') {
1742 // Add param to save lastsearch_values or not
1743 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1744 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1745 $add_save_lastsearch_values = 1;
1746 }
1747 if ($url && $add_save_lastsearch_values) {
1748 $url .= '&save_lastsearch_values=1';
1749 }
1750 }
1751
1752 // Check contrast with background and correct text color
1753 $forced_color = 'categtextwhite';
1754 if ($this->color) {
1755 if (colorIsLight($this->color)) {
1756 $forced_color = 'categtextblack';
1757 }
1758 }
1759
1760 $linkclose = '';
1761 if (empty($notooltip)) {
1762 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1763 $label = $langs->trans("ShowMyObject");
1764 $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
1765 }
1766 $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
1767 $linkclose .= $dataparams.' class="'.$classfortooltip.' '.$forced_color.($morecss ? ' '.$morecss : '').'"';
1768 } else {
1769 $linkclose = ($morecss ? ' class="'.$forced_color.($morecss ? ' '.$morecss : '').'"' : '');
1770 }
1771
1772 if ($option == 'nolink' || empty($url)) {
1773 $linkstart = '<span';
1774 } else {
1775 $linkstart = '<a href="'.$url.'"';
1776 }
1777 $linkstart .= $linkclose.'>';
1778 if ($option == 'nolink' || empty($url)) {
1779 $linkend = '</span>';
1780 } else {
1781 $linkend = '</a>';
1782 }
1783
1784 $result .= $linkstart;
1785
1786 if ($withpicto) {
1787 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1788 }
1789
1790 if ($withpicto != 2) {
1791 $result .= dol_trunc(($this->ref ? $this->ref : $this->label), $maxlength);
1792 }
1793
1794 $result .= $linkend;
1795
1796 global $action;
1797 $hookmanager->initHooks(array($this->element . 'dao'));
1798 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1799 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1800 if ($reshook > 0) {
1801 $result = $hookmanager->resPrint;
1802 } else {
1803 $result .= $hookmanager->resPrint;
1804 }
1805 return $result;
1806 }
1807
1808
1809 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1817 public function add_photo($sdir, $file)
1818 {
1819 // phpcs:enable
1820 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1821
1822 $dir = $sdir.'/'.get_exdir($this->id, 2, 0, 0, $this, 'category').$this->id."/";
1823 $dir .= "photos/";
1824
1825 if (!file_exists($dir)) {
1826 dol_mkdir($dir);
1827 }
1828
1829 if (file_exists($dir)) {
1830 if (is_array($file['name']) && count($file['name']) > 0) {
1831 $nbfile = count($file['name']);
1832 for ($i = 0; $i < $nbfile; $i++) {
1833 $originImage = $dir.$file['name'][$i];
1834
1835 // Cree fichier en taille origine
1836 dol_move_uploaded_file($file['tmp_name'][$i], $originImage, 1, 0, 0);
1837
1838 if (file_exists($originImage)) {
1839 // Create thumbs
1840 $this->addThumbs($originImage);
1841 }
1842 }
1843 } else {
1844 $originImage = $dir.$file['name'];
1845
1846 // Cree fichier en taille origine
1847 dol_move_uploaded_file($file['tmp_name'], $originImage, 1, 0, 0);
1848
1849 if (file_exists($originImage)) {
1850 // Create thumbs
1851 $this->addThumbs($originImage);
1852 }
1853 }
1854 }
1855 }
1856
1857 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1865 public function liste_photos($dir, $nbmax = 0)
1866 {
1867 // phpcs:enable
1868 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1869
1870 $nbphoto = 0;
1871 $tabobj = array();
1872
1873 $dirthumb = $dir.'thumbs/';
1874
1875 if (file_exists($dir)) {
1876 $handle = opendir($dir);
1877 if (is_resource($handle)) {
1878 while (($file = readdir($handle)) !== false) {
1879 if (dol_is_file($dir.$file) && preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) {
1880 $nbphoto++;
1881 $photo = $file;
1882
1883 // On determine nom du fichier vignette
1884 $photo_vignette = '';
1885 $regs = array();
1886 if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) {
1887 $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0];
1888 }
1889
1890 // Object
1891 $obj = array();
1892 $obj['photo'] = $photo;
1893 if ($photo_vignette && is_file($dirthumb.$photo_vignette)) {
1894 $obj['photo_vignette'] = 'thumbs/'.$photo_vignette;
1895 } else {
1896 $obj['photo_vignette'] = "";
1897 }
1898
1899 $tabobj[$nbphoto - 1] = $obj;
1900
1901 // On continue ou on arrete de boucler
1902 if ($nbmax && $nbphoto >= $nbmax) {
1903 break;
1904 }
1905 }
1906 }
1907
1908 closedir($handle);
1909 }
1910 }
1911
1912 return $tabobj;
1913 }
1914
1915 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1922 public function delete_photo($file)
1923 {
1924 // phpcs:enable
1925 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1926
1927 $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
1928 $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
1929 $filename = preg_replace('/'.preg_quote($dir, '/').'/i', '', $file); // Nom du fichier
1930
1931 // On efface l'image d'origine
1932 dol_delete_file($file, 1);
1933
1934 // Si elle existe, on efface la vignette
1935 $regs = array();
1936 if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $filename, $regs)) {
1937 $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $filename).'_small'.$regs[0];
1938 if (file_exists($dirthumb.$photo_vignette)) {
1939 dol_delete_file($dirthumb.$photo_vignette, 1);
1940 }
1941 }
1942 }
1943
1944 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1951 public function get_image_size($file)
1952 {
1953 // phpcs:enable
1954 $infoImg = getimagesize($file); // Recuperation des infos de l'image
1955 $this->imgWidth = $infoImg[0]; // Largeur de l'image
1956 $this->imgHeight = $infoImg[1]; // Hauteur de l'image
1957 }
1958
1967 public function setMultiLangs(User $user, $notrigger = 0)
1968 {
1969 global $langs;
1970
1971 $langs_available = $langs->get_available_languages();
1972 $current_lang = $langs->getDefaultLang();
1973
1974 foreach ($langs_available as $key => $value) {
1975 $sql = "SELECT rowid";
1976 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_lang";
1977 $sql .= " WHERE fk_category=".((int) $this->id);
1978 $sql .= " AND lang = '".$this->db->escape($key)."'";
1979
1980 $result = $this->db->query($sql);
1981
1982 if ($key == $current_lang) {
1983 $sql2 = '';
1984 if ($this->db->num_rows($result)) { // si aucune ligne dans la base
1985 $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang";
1986 $sql2 .= " SET label = '".$this->db->escape($this->label)."',";
1987 $sql2 .= " description = '".$this->db->escape($this->description)."'";
1988 $sql2 .= " WHERE fk_category = ".((int) $this->id)." AND lang = '".$this->db->escape($key)."'";
1989 } elseif (isset($this->multilangs[$key])) {
1990 $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)";
1991 $sql2 .= " VALUES(".((int) $this->id).", '".$this->db->escape($key)."', '".$this->db->escape($this->label)."'";
1992 $sql2 .= ", '".$this->db->escape($this->multilangs[$key]["description"])."')";
1993 }
1994 dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG);
1995 if ($sql2 && !$this->db->query($sql2)) {
1996 $this->error = $this->db->lasterror();
1997 return -1;
1998 }
1999 } elseif (isset($this->multilangs[$key])) {
2000 if ($this->db->num_rows($result)) { // si aucune ligne dans la base
2001 $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang";
2002 $sql2 .= " SET label='".$this->db->escape($this->multilangs[$key]["label"])."',";
2003 $sql2 .= " description='".$this->db->escape($this->multilangs[$key]["description"])."'";
2004 $sql2 .= " WHERE fk_category=".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
2005 } else {
2006 $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)";
2007 $sql2 .= " VALUES(".((int) $this->id).", '".$this->db->escape($key)."', '".$this->db->escape($this->multilangs[$key]["label"])."'";
2008 $sql2 .= ",'".$this->db->escape($this->multilangs[$key]["description"])."')";
2009 }
2010
2011 // on ne sauvegarde pas des champs vides
2012 if ($this->multilangs[$key]["label"] || $this->multilangs[$key]["description"] || $this->multilangs[$key]["note"]) {
2013 dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG);
2014 }
2015 if (!$this->db->query($sql2)) {
2016 $this->error = $this->db->lasterror();
2017 return -1;
2018 }
2019 }
2020 }
2021
2022 // Call trigger
2023 if (!$notrigger) {
2024 $result = $this->call_trigger('CATEGORY_SET_MULTILANGS', $user);
2025 if ($result < 0) {
2026 $this->error = $this->db->lasterror();
2027 return -1;
2028 }
2029 }
2030 // End call triggers
2031
2032 return 1;
2033 }
2034
2040 public function getMultiLangs()
2041 {
2042 global $langs;
2043
2044 $current_lang = $langs->getDefaultLang();
2045
2046 $sql = "SELECT lang, label, description";
2047 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_lang";
2048 $sql .= " WHERE fk_category=".((int) $this->id);
2049
2050 $result = $this->db->query($sql);
2051 if ($result) {
2052 while ($obj = $this->db->fetch_object($result)) {
2053 //print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
2054 if ($obj->lang == $current_lang) { // si on a les traduct. dans la langue courante on les charge en infos principales.
2055 $this->label = $obj->label;
2056 $this->description = $obj->description;
2057 }
2058 $this->multilangs[$obj->lang]["label"] = $obj->label;
2059 $this->multilangs[$obj->lang]["description"] = $obj->description;
2060 }
2061 return 1;
2062 } else {
2063 $this->error = $langs->trans("Error")." : ".$this->db->error()." - ".$sql;
2064 return -1;
2065 }
2066 }
2067
2074 public function getLibStatut($mode)
2075 {
2076 return '';
2077 }
2078
2079
2087 public function initAsSpecimen()
2088 {
2089 dol_syslog(get_class($this)."::initAsSpecimen");
2090
2091 // Initialise parameters
2092 $this->id = 0;
2093 $this->fk_parent = 0;
2094 $this->label = 'SPECIMEN';
2095 $this->specimen = 1;
2096 $this->description = 'This is a description';
2097 $this->socid = 1;
2098 $this->type = self::TYPE_PRODUCT;
2099
2100 return 1;
2101 }
2102
2111 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2112 {
2113 $tables = array(
2114 'categorie_societe'
2115 );
2116
2117 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables, 1);
2118 }
2119
2128 public static function getFilterJoinQuery($type, $rowIdName)
2129 {
2130 if ($type == 'bank_account') {
2131 $type = 'account';
2132 }
2133
2134 return " LEFT JOIN ".MAIN_DB_PREFIX."categorie_".$type." as cp ON ".$rowIdName." = cp.fk_".$type;
2135 }
2136
2146 public static function getFilterSelectQuery($type, $rowIdName, $searchList)
2147 {
2148 if ($type == 'bank_account') {
2149 $type = 'account';
2150 }
2151 if ($type == 'customer') {
2152 $type = 'societe';
2153 }
2154 if ($type == 'supplier') {
2155 $type = 'fournisseur';
2156 }
2157
2158 if (empty($searchList) && !is_array($searchList)) {
2159 return "";
2160 }
2161
2162 $searchCategorySqlList = array();
2163 foreach ($searchList as $searchCategory) {
2164 if (intval($searchCategory) == -2) {
2165 $searchCategorySqlList[] = " cp.fk_categorie IS NULL";
2166 } elseif (intval($searchCategory) > 0) {
2167 $searchCategorySqlList[] = " ".$rowIdName." IN (SELECT fk_".$type." FROM ".MAIN_DB_PREFIX."categorie_".$type." WHERE fk_categorie = ".((int) $searchCategory).")";
2168 }
2169 }
2170
2171 if (!empty($searchCategorySqlList)) {
2172 return " AND (".implode(' AND ', $searchCategorySqlList).")";
2173 } else {
2174 return "";
2175 }
2176 }
2177
2183 public function countNbOfCategories()
2184 {
2185 dol_syslog(get_class($this)."::count_all_categories", LOG_DEBUG);
2186 $sql = "SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX."categorie";
2187 $sql .= " WHERE entity IN (".getEntity('category').")";
2188
2189 $res = $this->db->query($sql);
2190 if ($res) {
2191 $obj = $this->db->fetch_object($res);
2192 return $obj->count;
2193 } else {
2194 dol_print_error($this->db);
2195 return -1;
2196 }
2197 }
2198}
print $object position
Definition edit.php:195
$object ref
Definition info.php:79
Class to manage categories.
getListForItem($id, $type='customer', $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
List categories of an element id.
get_image_size($file)
Load size of image file.
load_motherof()
Load the array this->motherof that is array(id_son=>id_parent, ...)
get_full_arbo($type, $fromid=0, $include=0)
Rebuilding the category tree as an array Return an array of table('id','id_mere',....
getLibStatut($mode)
Return label of contact status.
delete_photo($file)
Efface la photo de la categorie et sa vignette.
containing($id, $type, $mode='object')
Return list of categories (object instances or labels) linked to element of id $id and type $type Sho...
getTooltipContentArray($params)
getTooltipContentArray
countNbOfCategories()
Count all categories.
del_type($obj, $type)
Delete object from category.
buildPathFromId($id_categ, $protection=1000)
For category id_categ and its children available in this->cats, define property fullpath and fulllabe...
static getFilterSelectQuery($type, $rowIdName, $searchList)
Return the additional SQL SELECT query for filtering a list by a category.
create($user, $notrigger=0)
Add category into database.
rechercher($id, $nom, $type, $exact=false, $case=false)
Returns categories whose id or name match add wildcards in the name unless $exact = true.
getMultiLangs()
Load array this->multilangs.
isAnyPhotoAvailable($sdir)
Return if at least one photo is available.
already_exists()
Check if a category with same label already exists for this cat's parent or root and for this cat's t...
get_filles()
Return direct children ids of a category into an array.
initAsSpecimen()
Initialise an instance with random values.
get_all_categories($type=null, $parent=false)
Returns all categories.
static getFilterJoinQuery($type, $rowIdName)
Return the additional SQL JOIN query for filtering a list by a category.
containsObject($type, $object_id)
Check for the presence of an object in a category.
get_main_categories($type=null)
Returns the top level categories (which are not child)
__construct($db)
Constructor.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
setMultiLangs(User $user, $notrigger=0)
Update ou cree les traductions des infos produits.
getObjectsInCateg($type, $onlyids=0, $limit=0, $offset=0, $sortfield='', $sortorder='ASC', $filter='', $filtermode='AND')
Return list of fetched instance of elements having this category.
getMapList()
Get map list.
get_meres()
Returns an array containing the list of parent categories.
add_type($obj, $type='')
Link an object to the category.
update(User $user, $notrigger=0)
Update category.
liste_photos($dir, $nbmax=0)
Return an array with all photos inside the directory.
get_all_ways()
Returns in a table all possible paths to get to the category starting with the major categories repre...
getNomUrl($withpicto=0, $option='', $maxlength=0, $moreparam='', $notooltip=0, $morecss='', $save_lastsearch_value=0)
Return name and link of category (with picto) Use ->id, ->ref, ->label, ->color.
print_all_ways($sep='&gt;&gt;', $url='', $nocolor=0, $addpicto=0)
Returns the path of the category, with the names of the categories separated by $sep (" >> " by defau...
add_photo($sdir, $file)
Add the image uploaded as $file to the directory $sdir/<category>-<id>/photos/.
fetch($id, $label='', $type=null, $ref_ext='')
Load category into memory from database.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
deleteExtraFields()
Delete all extra fields values for the current object.
addThumbs($file)
Build thumb.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile', $upload_dir='')
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_is_file($pathoffile)
Return if path is a file.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
colorIsLight($stringcolor)
Return true if the color is light.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
utf8_check($str)
Check if a string is in UTF8.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:139