dolibarr 23.0.3
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-2025 Charlene Benke <charlene@patas-monkey.com>
13 * Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
14 * Copyright (C) 2022-2023 Solution Libre SAS <contact@solution-libre.fr>
15 * Copyright (C) 2023-2024 Benjamin Falière <benjamin.faliere@altairis.fr>
16 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
17 * Copyright (C) 2025 Alexandre Spangaro <alexandre@inovea-conseil.com>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 3 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <https://www.gnu.org/licenses/>.
31 */
32
39require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
40require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
41require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
42require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
43require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
44require_once DOL_DOCUMENT_ROOT.'/knowledgemanagement/class/knowledgerecord.class.php';
45
46
51{
52 // Categories types (we use string because we want to accept any modules/types in a future)
53 const TYPE_PRODUCT = 'product';
54 const TYPE_SUPPLIER = 'supplier';
55 const TYPE_CUSTOMER = 'customer';
56 const TYPE_MEMBER = 'member';
57 const TYPE_CONTACT = 'contact';
58 const TYPE_USER = 'user';
59 const TYPE_PROJECT = 'project';
60 const TYPE_ACCOUNT = 'bank_account';
61 const TYPE_BANK_LINE = 'bank_line';
62 const TYPE_WAREHOUSE = 'warehouse';
63 const TYPE_ACTIONCOMM = 'actioncomm';
64 const TYPE_WEBSITE_PAGE = 'website_page';
65 const TYPE_TICKET = 'ticket';
66 const TYPE_KNOWLEDGEMANAGEMENT = 'knowledgemanagement';
67 const TYPE_FICHINTER = 'fichinter';
68 const TYPE_ORDER = 'order';
69 const TYPE_INVOICE = 'invoice';
70 const TYPE_SUPPLIER_ORDER = 'supplier_order';
71 const TYPE_SUPPLIER_INVOICE = 'supplier_invoice';
72 const TYPE_SUPPLIER_PROPOSAL = 'supplier_proposal';
73 const TYPE_PROPOSAL = 'propal';
74 const TYPE_PROJECT_TASK = 'project_task';
75
76
80 public $picto = 'category';
81
82
86 public $MAP_ID = array(
87 'product' => 0,
88 'service' => 0,
89 'supplier' => 1,
90 'customer' => 2,
91 'member' => 3,
92 'contact' => 4,
93 'bank_account' => 5,
94 'project' => 6,
95 'user' => 7,
96 'bank_line' => 8,
97 'warehouse' => 9,
98 'actioncomm' => 10,
99 'website_page' => 11,
100 'ticket' => 12,
101 'knowledgemanagement' => 13,
102 'fichinter' => 14,
103 'order' => 16,
104 'invoice' => 17,
105 'supplier_order' => 20,
106 'supplier_invoice' => 21,
107 'supplier_proposal' => 22,
108 'propal' => 23,
109 'project_task' => 24,
110 );
111
117 /*
118 public static $MAP_ID_TO_CODE = array(
119 0 => 'product',
120 1 => 'supplier',
121 2 => 'customer',
122 3 => 'member',
123 4 => 'contact',
124 5 => 'bank_account',
125 6 => 'project',
126 7 => 'user',
127 8 => 'bank_line',
128 9 => 'warehouse',
129 10 => 'actioncomm',
130 11 => 'website_page',
131 12 => 'ticket',
132 13 => 'knowledgemanagement',
133 14 => 'fichinter',
134 16 => 'order',
135 17 => 'invoice',
136 20 => 'supplier_order',
137 21 => 'supplier_invoice',
138 22 => 'supplier_proposal',
139 23 => 'propal',
140 24 => 'project_task'
141 );
142 */
143
147 public $MAP_CAT_FK = array(
148 'customer' => 'soc',
149 'supplier' => 'soc',
150 'contact' => 'socpeople',
151 'bank_account' => 'account',
152 );
153
157 public $MAP_CAT_TABLE = array(
158 'customer' => 'societe',
159 'supplier' => 'fournisseur',
160 'bank_account' => 'account',
161 );
162
166 public $MAP_OBJ_CLASS = array(
167 'product' => 'Product',
168 'service' => 'Product',
169 'customer' => 'Societe',
170 'supplier' => 'Fournisseur',
171 'member' => 'Adherent',
172 'contact' => 'Contact',
173 'user' => 'User',
174 'account' => 'Account', // old for bank account
175 'bank_account' => 'Account',
176 'project' => 'Project',
177 'warehouse' => 'Entrepot',
178 'actioncomm' => 'ActionComm',
179 'website_page' => 'WebsitePage',
180 'ticket' => 'Ticket',
181 'knowledgemanagement' => 'KnowledgeRecord',
182 'fichinter' => 'Fichinter',
183 'order' => 'Commande',
184 'invoice' => 'Facture',
185 'supplier_order' => 'CommandeFournisseur',
186 'supplier_invoice' => 'FactureFournisseur',
187 'supplier_proposal' => 'SupplierProposal',
188 'propal' => 'Propal',
189 'project_task' => 'Task',
190 );
191
195 public static $MAP_TYPE_TITLE_AREA = array(
196 'product' => 'Products',
197 'service' => 'Services',
198 'customer' => 'ProspectsOrCustomers',
199 'supplier' => 'Suppliers',
200 'member' => 'Members',
201 'contact' => 'Contacts',
202 'user' => 'Users',
203 'account' => 'Accounts', // old for bank account
204 'bank_account' => 'BankAccounts',
205 'bank_line' => 'BankTransactions',
206 'project' => 'Projects',
207 'warehouse' => 'Warehouse',
208 'actioncomm' => 'AgendaEvents',
209 'website_page' => 'WebsitePages',
210 'ticket' => 'Tickets',
211 'knowledgemanagement' => 'KnowledgeRecords',
212 'fichinter' => 'Interventions',
213 'order' => 'Orders',
214 'invoice' => 'Invoices',
215 'supplier_order' => 'SuppliersOrders',
216 'supplier_invoice' => 'SuppliersInvoices',
217 'propal' => 'Proposals',
218 'supplier_proposal' => 'SupplierProposals',
219 'project_task' => 'Tasks'
220 );
221
226 public $MAP_OBJ_TABLE = array(
227 'customer' => 'societe',
228 'supplier' => 'societe',
229 'member' => 'adherent',
230 'contact' => 'socpeople',
231 'account' => 'bank_account', // old for bank account
232 'project' => 'projet',
233 'warehouse' => 'entrepot',
234 'knowledgemanagement' => 'knowledgemanagement_knowledgerecord',
235 'fichinter' => 'fichinter',
236 'order' => 'commande',
237 'invoice' => 'facture',
238 'supplier_order' => 'commande_fournisseur',
239 'supplier_invoice' => 'facture_fourn',
240 'project_task' => 'projet_task'
241 );
242
246 public $element = 'category';
247
251 public $table_element = 'categorie';
252
256 public $fk_parent;
257
261 public $label;
262
266 public $description;
267
271 public $color;
272
276 public $position;
277
281 public $visible;
282
286 public $socid;
287
311 public $type;
312
316 public $cats = array();
317
321 public $motherof = array();
322
326 public $childs = array();
327
331 public $multilangs = array();
332
336 public $imgWidth;
337
341 public $imgHeight;
342
367 public $fields = array(
368 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'visible' => 0,),
369 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => 0, 'default' => '1', 'notnull' => 1, 'index' => 1, 'position' => 5),
370 'fk_parent' => array('type' => 'integer', 'label' => 'ParentCategory', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 0, 'css' => 'maxwidth500 widthcentpercentminusxx',),
371 'label' => array('type' => 'varchar(180)', 'label' => 'Ref', 'enabled' => 1, 'position' => 25, 'notnull' => 1, 'visible' => 1, 'alwayseditable' => 1, 'css' => 'minwidth300', 'cssview' => 'wordbreak', 'csslist' => 'tdoverflowmax150', 'showoncombobox' => 1),
372 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'RefExt', 'enabled' => 1, 'position' => 30, 'notnull' => 0, 'visible' => 0, 'alwayseditable' => 1,),
373 'type' => array('type' => 'integer', 'label' => 'Type', 'enabled' => 1, 'position' => 35, 'notnull' => 1, 'visible' => 0, 'alwayseditable' => 1,),
374 'description' => array('type' => 'text', 'label' => 'Description', 'enabled' => 1, 'position' => 40, 'notnull' => 0, 'visible' => 1, 'alwayseditable' => 1,),
375 'color' => array('type' => 'varchar(8)', 'label' => 'Color', 'enabled' => 1, 'position' => 45, 'notnull' => 0, 'visible' => 1, 'alwayseditable' => 1,),
376 'position' => array('type' => 'integer', 'label' => 'Position', 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
377 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'picto' => 'company', 'enabled' => 1, 'position' => 55, 'notnull' => 0, 'visible' => 0, 'alwayseditable' => 1, 'css' => 'maxwidth500 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax150',),
378 'visible' => array('type' => 'integer', 'label' => 'Visible', 'enabled' => 1, 'position' => 60, 'notnull' => 1, 'visible' => 0, 'alwayseditable' => 1,),
379 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'position' => 900, 'notnull' => 0, 'visible' => -2, 'alwayseditable' => 1,),
380 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'position' => 70, 'notnull' => 0, 'visible' => -1, 'alwayseditable' => 1,),
381 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'position' => 75, 'notnull' => 1, 'visible' => -1, 'alwayseditable' => 1,),
382 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'position' => 80, 'notnull' => 0, 'visible' => -2, 'alwayseditable' => 1, 'css' => 'maxwidth500 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax150',),
383 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'position' => 85, 'notnull' => -1, 'visible' => -2, 'alwayseditable' => 1, 'css' => 'maxwidth500 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax150',),
384 );
385
391 public function __construct($db)
392 {
393 global $hookmanager;
394
395 $this->db = $db;
396
397 $this->ismultientitymanaged = 1;
398 $this->isextrafieldmanaged = 1;
399
400 if (is_object($hookmanager)) {
401 $hookmanager->initHooks(array('category'));
402 $parameters = array();
403 $reshook = $hookmanager->executeHooks('constructCategory', $parameters, $this); // Note that $action and $object may have been modified by some hooks
404 if ($reshook >= 0 && !empty($hookmanager->resArray)) {
405 foreach ($hookmanager->resArray as $mapList) {
407 $mapId = $mapList['id'];
408 $mapCode = $mapList['code'];
409 //self::$MAP_ID_TO_CODE[$mapId] = $mapCode;
410 $this->MAP_ID[$mapCode] = $mapId;
411 $this->MAP_CAT_FK[$mapCode] = isset($mapList['cat_fk']) ? $mapList['cat_fk'] : null;
412 $this->MAP_CAT_TABLE[$mapCode] = isset($mapList['cat_table']) ? $mapList['cat_table'] : null;
413 $this->MAP_OBJ_CLASS[$mapCode] = $mapList['obj_class'];
414 $this->MAP_OBJ_TABLE[$mapCode] = $mapList['obj_table'];
415 self::$MAP_TYPE_TITLE_AREA[$mapCode] = isset($mapList['label']) ? $mapList['label'] : null;
416 }
417 }
418 }
419 }
420
426 public function getMapList()
427 {
428 $mapList = array();
429
430 foreach ($this->MAP_ID as $mapCode => $mapId) {
431 $mapList[] = array(
432 'id' => $mapId,
433 'code' => $mapCode,
434 'cat_fk' => (empty($this->MAP_CAT_FK[$mapCode]) ? $mapCode : $this->MAP_CAT_FK[$mapCode]),
435 'cat_table' => (empty($this->MAP_CAT_TABLE[$mapCode]) ? $mapCode : $this->MAP_CAT_TABLE[$mapCode]),
436 'obj_class' => (empty($this->MAP_OBJ_CLASS[$mapCode]) ? $mapCode : $this->MAP_OBJ_CLASS[$mapCode]),
437 'obj_table' => (empty($this->MAP_OBJ_TABLE[$mapCode]) ? $mapCode : $this->MAP_OBJ_TABLE[$mapCode]),
438 );
439 }
440
441 return $mapList;
442 }
443
449 public function getMapId()
450 {
451 return $this->MAP_ID;
452 }
453
463 public function fetch($id, $label = '', $type = null, $ref_ext = '')
464 {
465 // Check parameters
466 if (empty($id) && empty($label) && empty($ref_ext)) {
467 $this->error = "No category to search for";
468 return -1;
469 }
470 if (!is_null($type) && !is_numeric($type)) {
471 $type = $this->MAP_ID[$type];
472 }
473
474 $sql = "SELECT rowid, fk_parent, entity, label, description, color, position, fk_soc, visible, type, ref_ext";
475 $sql .= ", date_creation, tms, fk_user_creat, fk_user_modif, import_key";
476 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
477 if ($id) {
478 $sql .= " WHERE rowid = ".((int) $id);
479 } elseif (!empty($ref_ext)) {
480 $sql .= " WHERE ref_ext LIKE '".$this->db->escape($ref_ext)."'";
481 } else {
482 $sql .= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")";
483 if (!is_null($type)) {
484 $sql .= " AND type = ".((int) $type);
485 }
486 }
487
488 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
489 $resql = $this->db->query($sql);
490 if ($resql) {
491 if ($this->db->num_rows($resql) > 0 && $res = $this->db->fetch_array($resql)) {
492 $this->id = $res['rowid'];
493 //$this->ref = $res['rowid'];
494 $this->fk_parent = (int) $res['fk_parent'];
495 $this->label = $res['label'];
496 $this->description = $res['description'];
497 $this->color = $res['color'];
498 $this->position = $res['position'];
499 $this->socid = (int) $res['fk_soc'];
500 $this->visible = (int) $res['visible'];
501 $this->type = $res['type'];
502 $this->ref_ext = $res['ref_ext'];
503 $this->entity = (int) $res['entity'];
504 $this->date_creation = $this->db->jdate($res['date_creation']);
505 $this->date_modification = $this->db->jdate($res['tms']);
506 $this->user_creation_id = (int) $res['fk_user_creat'];
507 $this->user_modification_id = (int) $res['fk_user_modif'];
508 $this->import_key = $res['import_key'];
509
510 // Retrieve all extrafields
511 // fetch optionals attributes and labels
512 $this->fetch_optionals();
513
514 $this->db->free($resql);
515
516 // multilangs
517 if (getDolGlobalInt('MAIN_MULTILANGS')) {
518 $this->getMultiLangs();
519 }
520
521 return 1;
522 } else {
523 $this->error = "No category found";
524 return 0;
525 }
526 } else {
527 dol_print_error($this->db);
528 $this->error = $this->db->lasterror;
529 $this->errors[] = $this->db->lasterror;
530 return -1;
531 }
532 }
533
544 public function create($user, $notrigger = 0)
545 {
546 global $conf, $langs;
547 $langs->load('categories');
548
549 $type = $this->type;
550
551 if (!is_numeric($type)) {
552 $type = $this->MAP_ID[$type];
553 }
554
555 $error = 0;
556
557 dol_syslog(get_class($this).'::create', LOG_DEBUG);
558
559 // Clean parameters
560 $this->label = trim($this->label);
561 $this->description = trim($this->description);
562 $this->color = trim($this->color);
563 $this->position = (int) $this->position;
564 if (isset($this->import_key)) {
565 $this->import_key = trim($this->import_key);
566 }
567 $this->ref_ext = trim($this->ref_ext);
568 if (empty($this->visible)) {
569 $this->visible = 0;
570 }
571 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
572
573 if ($this->already_exists()) {
574 $this->error = $langs->trans("ImpossibleAddCat", $this->label);
575 $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel");
576 dol_syslog($this->error, LOG_WARNING);
577 return -4;
578 }
579
580 $this->db->begin();
581 $now = dol_now();
582 $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (";
583 $sql .= "fk_parent,";
584 $sql .= " label,";
585 $sql .= " description,";
586 $sql .= " color,";
587 $sql .= " position,";
588 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
589 $sql .= "fk_soc,";
590 }
591 $sql .= " visible,";
592 $sql .= " type,";
593 $sql .= " import_key,";
594 $sql .= " ref_ext,";
595 $sql .= " entity,";
596 $sql .= " date_creation,";
597 $sql .= " fk_user_creat";
598 $sql .= ") VALUES (";
599 $sql .= (int) $this->fk_parent.",";
600 $sql .= "'".$this->db->escape($this->label)."', ";
601 $sql .= "'".$this->db->escape($this->description)."', ";
602 $sql .= "'".$this->db->escape($this->color)."', ";
603 $sql .= (int) $this->position.",";
604 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
605 $sql .= ($this->socid > 0 ? $this->socid : 'null').", ";
606 }
607 $sql .= "'".$this->db->escape((string) $this->visible)."', ";
608 $sql .= ((int) $type).", ";
609 $sql .= (!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : 'null').", ";
610 $sql .= (!empty($this->ref_ext) ? "'".$this->db->escape($this->ref_ext)."'" : 'null').", ";
611 $sql .= (int) $conf->entity.", ";
612 $sql .= "'".$this->db->idate($now)."', ";
613 $sql .= (int) $user->id;
614 $sql .= ")";
615
616 $res = $this->db->query($sql);
617 if ($res) {
618 $id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie");
619
620 if ($id > 0) {
621 $this->id = $id;
622
623 $action = 'create';
624
625 // Actions on extra fields
626 $result = $this->insertExtraFields();
627 if ($result < 0) {
628 $error++;
629 }
630
631 if (!$error && !$notrigger) {
632 // Call trigger
633 $result = $this->call_trigger('CATEGORY_CREATE', $user);
634 if ($result < 0) {
635 $error++;
636 }
637 // End call triggers
638 }
639
640 if (!$error) {
641 $this->db->commit();
642 return $id;
643 } else {
644 $this->db->rollback();
645 return -3;
646 }
647 } else {
648 $this->db->rollback();
649 return -2;
650 }
651 } else {
652 $this->error = $this->db->error();
653 $this->db->rollback();
654 return -1;
655 }
656 }
657
667 public function update(User $user, $notrigger = 0)
668 {
669 global $langs;
670
671 $error = 0;
672
673 // Clean parameters
674 $this->label = trim($this->label);
675 $this->description = trim($this->description);
676 $this->ref_ext = trim($this->ref_ext);
677 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
678 $this->visible = ($this->visible != "" ? intval($this->visible) : 0);
679
680 if ($this->fk_parent > 0 && $this->fk_parent == $this->id) {
681 $langs->load('categories');
682 $this->error = $langs->trans("ErrorCategoryCannotBeItsOwnParent");
683 dol_syslog($this->error, LOG_WARNING);
684 return -1;
685 }
686
687 if ($this->already_exists()) {
688 $this->error = $langs->trans("ImpossibleUpdateCat");
689 $this->error .= " : ".$langs->trans("CategoryExistsAtSameLevel");
690 return -1;
691 }
692
693 $this->db->begin();
694
695 $sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
696 $sql .= " SET label = '".$this->db->escape($this->label)."',";
697 $sql .= " description = '".$this->db->escape($this->description)."',";
698 $sql .= " ref_ext = '".$this->db->escape($this->ref_ext)."',";
699 $sql .= " color = '".$this->db->escape($this->color)."'";
700 $sql .= ", position = ".(int) $this->position;
701 if (getDolGlobalString('CATEGORY_ASSIGNED_TO_A_CUSTOMER')) {
702 $sql .= ", fk_soc = ".($this->socid > 0 ? $this->socid : 'null');
703 }
704 $sql .= ", visible = ".(int) $this->visible;
705 $sql .= ", fk_parent = ".(int) $this->fk_parent;
706 $sql .= ", fk_user_modif = ".(int) $user->id;
707 $sql .= " WHERE rowid = ".((int) $this->id);
708
709 dol_syslog(get_class($this)."::update", LOG_DEBUG);
710 if ($this->db->query($sql)) {
711 $action = 'update';
712
713 // Actions on extra fields
714 $result = $this->insertExtraFields();
715 if ($result < 0) {
716 $error++;
717 }
718
719 if (!$error && !$notrigger) {
720 // Call trigger
721 $result = $this->call_trigger('CATEGORY_MODIFY', $user);
722 if ($result < 0) {
723 $error++;
724 }
725 // End call triggers
726 }
727
728 if (!$error) {
729 $this->db->commit();
730 return 1;
731 } else {
732 $this->db->rollback();
733 return -1;
734 }
735 } else {
736 $this->db->rollback();
737 dol_print_error($this->db);
738 return -1;
739 }
740 }
741
749 public function delete($user, $notrigger = 0)
750 {
751 $error = 0;
752
753 // Clean parameters
754 $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
755
756 dol_syslog(get_class($this)."::remove");
757
758 $this->db->begin();
759
760 if (/* !$error && */ !$notrigger) {
761 // Call trigger
762 $result = $this->call_trigger('CATEGORY_DELETE', $user);
763 if ($result < 0) {
764 $error++;
765 }
766 // End call triggers
767 }
768
769 /* FIX #1317 : Check for child category and move up 1 level*/
770 if (!$error) {
771 $sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
772 $sql .= " SET fk_parent = ".((int) $this->fk_parent);
773 $sql .= " WHERE fk_parent = ".((int) $this->id);
774
775 if (!$this->db->query($sql)) {
776 $this->error = $this->db->lasterror();
777 $error++;
778 }
779 }
780
781 $arraydelete = array(
782 'categorie_account' => 'fk_categorie',
783 'categorie_actioncomm' => 'fk_categorie',
784 'categorie_contact' => 'fk_categorie',
785 'categorie_fournisseur' => 'fk_categorie',
786 'categorie_knowledgemanagement' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('knowledgemanagement')),
787 'categorie_fichinter' => array('field' => 'fk_categorie', 'enabled' => false ), // not yet fully implemented (wait 22) isModEnabled('intervention')),
788 'categorie_member' => 'fk_categorie',
789 'categorie_user' => 'fk_categorie',
790 'categorie_product' => 'fk_categorie',
791 'categorie_project' => 'fk_categorie',
792 'categorie_project_task' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('project')),
793 'categorie_societe' => 'fk_categorie',
794 'categorie_ticket' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('ticket')),
795 'categorie_warehouse' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('stock')),
796 'categorie_website_page' => array('field' => 'fk_categorie', 'enabled' => isModEnabled('website')),
797 'category_bankline' => 'fk_categ',
798 'categorie_lang' => 'fk_category',
799 'categorie' => 'rowid',
800 );
801 foreach ($arraydelete as $key => $value) {
802 if (is_array($value)) {
803 if (empty($value['enabled'])) {
804 continue;
805 }
806 $value = $value['field'];
807 }
808 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$key;
809 $sql .= " WHERE ".$value." = ".((int) $this->id);
810 if (!$this->db->query($sql)) {
811 $this->errors[] = $this->db->lasterror();
812 dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
813 $error++;
814 }
815 }
816
817 // Removed extrafields
818 if (!$error) {
819 $result = $this->deleteExtraFields();
820 if ($result < 0) {
821 $error++;
822 dol_syslog(get_class($this)."::delete erreur ".$this->error, LOG_ERR);
823 }
824 }
825
826 if (!$error) {
827 $this->db->commit();
828 return 1;
829 } else {
830 $this->db->rollback();
831 return -1;
832 }
833 }
834
835 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
844 public function add_type($obj, $type = '')
845 {
846 // phpcs:enable
847 global $user;
848
849 $error = 0;
850
851 if ($this->id == -1) {
852 return -2;
853 }
854
855 if (empty($type)) {
856 $type = $obj->element;
857 }
858
859 dol_syslog(get_class($this).'::add_type', LOG_DEBUG);
860
861 $this->db->begin();
862
863 $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
864 $sql .= " (fk_categorie, fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type]).")";
865 $sql .= " VALUES (".((int) $this->id).", ".((int) $obj->id).")";
866
867 if ($this->db->query($sql)) {
868 if (getDolGlobalString('CATEGORIE_RECURSIV_ADD')) {
869 $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie';
870 $sql .= " WHERE rowid = ".((int) $this->id);
871
872 dol_syslog(get_class($this)."::add_type", LOG_DEBUG);
873 $resql = $this->db->query($sql);
874 if ($resql) {
875 if ($this->db->num_rows($resql) > 0) {
876 $objparent = $this->db->fetch_object($resql);
877
878 if (!empty($objparent->fk_parent)) {
879 $cat = new Categorie($this->db);
880 $cat->id = $objparent->fk_parent;
881 if (!$cat->containsObject($type, $obj->id)) {
882 $result = $cat->add_type($obj, $type);
883 if ($result < 0) {
884 $this->error = $cat->error;
885 $error++;
886 }
887 }
888 }
889 }
890 } else {
891 $error++;
892 $this->error = $this->db->lasterror();
893 }
894
895 if ($error) {
896 $this->db->rollback();
897 return -1;
898 }
899 }
900
901 // Call trigger
902 $this->context = array('linkto' => $obj); // Save object we want to link category to into category instance to provide information to trigger
903 $result = $this->call_trigger('CATEGORY_MODIFY', $user);
904 if ($result < 0) {
905 $error++;
906 }
907 // End call triggers
908
909 if (!$error) {
910 $this->db->commit();
911 return 1;
912 } else {
913 $this->db->rollback();
914 return -2;
915 }
916 } else {
917 $this->db->rollback();
918 if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
919 $this->error = $this->db->lasterrno();
920 return -3;
921 } else {
922 $this->error = $this->db->lasterror();
923 }
924 return -1;
925 }
926 }
927
928 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
937 public function del_type($obj, $type)
938 {
939 // phpcs:enable
940 global $user;
941
942 $error = 0;
943
944 // For backward compatibility
945 if ($type == 'societe') {
946 $type = 'customer';
947 dol_syslog(get_class($this)."::del_type(): type 'societe' is deprecated, please use 'customer' instead", LOG_WARNING);
948 } elseif ($type == 'fournisseur') {
949 $type = 'supplier';
950 dol_syslog(get_class($this)."::del_type(): type 'fournisseur' is deprecated, please use 'supplier' instead", LOG_WARNING);
951 }
952
953 $this->db->begin();
954
955 $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
956 $sql .= " WHERE fk_categorie = ".((int) $this->id);
957 $sql .= " AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".((int) $obj->id);
958
959 dol_syslog(get_class($this).'::del_type', LOG_DEBUG);
960 if ($this->db->query($sql)) {
961 // Call trigger
962 $this->context = array('unlinkoff' => $obj); // Save object we want to link category to into category instance to provide information to trigger
963 $result = $this->call_trigger('CATEGORY_MODIFY', $user);
964 if ($result < 0) {
965 $error++;
966 }
967 // End call triggers
968
969 if (!$error) {
970 $this->db->commit();
971 return 1;
972 } else {
973 $this->db->rollback();
974 return -2;
975 }
976 } else {
977 $this->db->rollback();
978 $this->error = $this->db->lasterror();
979 return -1;
980 }
981 }
982
1001 public function getObjectsInCateg($type, $onlyids = 0, $limit = 0, $offset = 0, $sortfield = '', $sortorder = 'ASC', $filter = '', $filtermode = 'AND', $filterlang = '')
1002 {
1003 global $user;
1004
1005 if (empty($onlyids)) {
1006 dol_syslog("getObjectsInCateg: This method used with parameter onlyids=0 is deprecated. Try by using instead getListForItem().", LOG_WARNING);
1007 }
1008
1009 $objs = array();
1010
1011 $classnameforobj = $this->MAP_OBJ_CLASS[$type];
1012 if (!empty($classnameforobj) && class_exists($classnameforobj)) {
1013 $tmpobj = new $classnameforobj($this->db);
1016 $sql = "SELECT c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." as fk_object";
1017 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type])." as c";
1018 $sql .= ", ".MAIN_DB_PREFIX.(empty($this->MAP_OBJ_TABLE[$type]) ? $type : $this->MAP_OBJ_TABLE[$type])." as o";
1019 if (!empty($filterlang)) {
1020 $sql .= ", ".MAIN_DB_PREFIX.(empty($this->MAP_OBJ_TABLE[$type]) ? $type : $this->MAP_OBJ_TABLE[$type])."_lang as ol";
1021 }
1022 $sql .= " WHERE o.entity IN (".getEntity($tmpobj->element).")";
1023 $sql .= " AND c.fk_categorie = ".((int) $this->id);
1024 // Compatibility with actioncomm table which has id instead of rowid
1025 if ((array_key_exists($type, $this->MAP_OBJ_TABLE) && $this->MAP_OBJ_TABLE[$type] == "actioncomm") || $type == "actioncomm") {
1026 $sql .= " AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = o.id";
1027 } else {
1028 $sql .= " AND c.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = o.rowid";
1029 }
1030 if (!empty($filterlang)) {
1031 $sql .= " AND ol.fk_".(empty($this->MAP_OBJ_TABLE[$type]) ? $type : $this->MAP_OBJ_TABLE[$type])." = o.rowid";
1032 $sql .= " AND ol.lang = '".$this->db->escape($filterlang)."'";
1033 }
1034 // Protection for external users
1035 if (($type == 'customer' || $type == 'supplier') && $user->socid > 0) {
1036 $sql .= " AND o.rowid = ".((int) $user->socid);
1037 }
1038
1039 $errormessage = '';
1040 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
1041 if ($errormessage) {
1042 $this->errors[] = $errormessage;
1043 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
1044 return -1;
1045 }
1046
1047 $sql .= $this->db->order($sortfield, $sortorder);
1048 if ($limit > 0 || $offset > 0) {
1049 $sql .= $this->db->plimit($limit + 1, $offset);
1050 }
1051
1052 dol_syslog(get_class($this)."::getObjectsInCateg", LOG_DEBUG);
1053
1054 $resql = $this->db->query($sql);
1055 if ($resql) {
1056 while ($rec = $this->db->fetch_array($resql)) {
1057 if ($onlyids) {
1058 $objs[] = $rec['fk_object'];
1059 } else {
1060 $tmpobj = new $classnameforobj($this->db);
1061 $tmpobj->fetch($rec['fk_object']); // The fetch will set $tmpobj->id only if it succeed.
1062 // @phpstan-ignore-next-line
1063 if ($tmpobj->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.
1064 $objs[] = $tmpobj;
1065 }
1066 }
1067 }
1068 } else {
1069 $this->error = $this->db->error().' sql='.$sql;
1070 return -1;
1071 }
1072 }
1073
1074 return $objs;
1075 }
1076
1085 public function containsObject($type, $object_id)
1086 {
1087 $sql = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."categorie_".(empty($this->MAP_CAT_TABLE[$type]) ? $type : $this->MAP_CAT_TABLE[$type]);
1088 $sql .= " WHERE fk_categorie = ".((int) $this->id)." AND fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".((int) $object_id);
1089
1090 dol_syslog(get_class($this)."::containsObject", LOG_DEBUG);
1091
1092 $resql = $this->db->query($sql);
1093 if ($resql) {
1094 return $this->db->fetch_object($resql)->nb;
1095 } else {
1096 $this->error = $this->db->error();
1097 return -1;
1098 }
1099 }
1100
1113 public function getListForItem($id, $type = 'customer', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0)
1114 {
1115 $categories = array();
1116
1117 $type = sanitizeVal($type, 'aZ09');
1118
1119 $sub_type = $type;
1120 $subcol_name = "fk_".$type;
1121 if ($type == "customer") {
1122 $sub_type = "societe";
1123 $subcol_name = "fk_soc";
1124 }
1125 if ($type == "supplier") {
1126 $sub_type = "fournisseur";
1127 $subcol_name = "fk_soc";
1128 }
1129 if ($type == "contact") {
1130 $subcol_name = "fk_socpeople";
1131 }
1132
1133 $idoftype = (int) (array_key_exists($type, $this->MAP_ID) ? $this->MAP_ID[$type] : -1);
1134
1135 $sql = "SELECT s.rowid";
1136 $sqlfields = $sql; // $sql fields to remove for count total
1137 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as s, ".MAIN_DB_PREFIX."categorie_".$this->db->sanitize($sub_type)." as sub";
1138 $sql .= ' WHERE s.entity IN ('.getEntity('category').')';
1139 $sql .= ' AND s.type = '.((int) $idoftype);
1140 $sql .= ' AND s.rowid = sub.fk_categorie';
1141 $sql .= " AND sub.".$this->db->sanitize($subcol_name)." = ".((int) $id);
1142
1143 $offset = 0;
1144 $nbtotalofrecords = '';
1145 if (!getDolGlobalInt('MAIN_DISABLE_FULL_SCANLIST')) {
1146 $sqlforcount = preg_replace('/^'.preg_quote($sqlfields, '/').'/', 'SELECT COUNT(*) as nbtotalofrecords', $sql);
1147 $sqlforcount = preg_replace('/GROUP BY .*$/', '', $sqlforcount);
1148
1149 $resql = $this->db->query($sqlforcount);
1150 if ($resql) {
1151 $objforcount = $this->db->fetch_object($resql);
1152 $nbtotalofrecords = $objforcount->nbtotalofrecords;
1153 } else {
1154 dol_print_error($this->db);
1155 }
1156
1157 if (($limit >= (int) $nbtotalofrecords) && $page > 0) {
1158 return [];
1159 }
1160
1161 if (($page * $limit) >= (int) $nbtotalofrecords) { // if total resultset is smaller or equal then paging size (filtering), goto and load page 0
1162 $page = 0;
1163 $offset = 0;
1164 }
1165 $this->db->free($resql);
1166 }
1167
1168 $sql .= $this->db->order($sortfield, $sortorder);
1169 if ($limit) {
1170 if ($page < 0) {
1171 $page = 0;
1172 }
1173 $offset = $limit * $page;
1174
1175 $sql .= $this->db->plimit($limit + 1, $offset);
1176 }
1177
1178 $result = $this->db->query($sql);
1179 if ($result) {
1180 $i = 0;
1181 $num = $this->db->num_rows($result);
1182 $min = min($num, ($limit <= 0 ? $num : $limit));
1183 while ($i < $min) {
1184 $obj = $this->db->fetch_object($result);
1185 $category_static = new Categorie($this->db);
1186 if ($category_static->fetch($obj->rowid)) {
1187 $categories[$i]['id'] = $category_static->id;
1188 $categories[$i]['fk_parent'] = $category_static->fk_parent;
1189 $categories[$i]['label'] = $category_static->label;
1190 $categories[$i]['description'] = $category_static->description;
1191 $categories[$i]['color'] = $category_static->color;
1192 $categories[$i]['position'] = $category_static->position;
1193 $categories[$i]['socid'] = $category_static->socid;
1194 $categories[$i]['ref_ext'] = $category_static->ref_ext;
1195 $categories[$i]['visible'] = $category_static->visible;
1196 $categories[$i]['type'] = $category_static->type;
1197 $categories[$i]['entity'] = $category_static->entity;
1198 $categories[$i]['array_options'] = $category_static->array_options;
1199
1200 // multilangs
1201 if (getDolGlobalInt('MAIN_MULTILANGS') && isset($category_static->multilangs)) {
1202 $categories[$i]['multilangs'] = $category_static->multilangs;
1203 }
1204 }
1205 $i++;
1206 }
1207 } else {
1208 $this->error = $this->db->lasterror();
1209 return -1;
1210 }
1211 if (!count($categories)) {
1212 return [];
1213 }
1214
1215 return $categories;
1216 }
1217
1218 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1224 public function get_filles()
1225 {
1226 // phpcs:enable
1227 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1228 $sql .= " WHERE fk_parent = ".((int) $this->id);
1229 $sql .= " AND entity IN (".getEntity('category').")";
1230
1231 $res = $this->db->query($sql);
1232 if ($res) {
1233 $cats = array();
1234 while ($rec = $this->db->fetch_array($res)) {
1235 $cat = new Categorie($this->db);
1236 $cat->fetch($rec['rowid']);
1237 $cats[] = $cat;
1238 }
1239 return $cats;
1240 } else {
1241 dol_print_error($this->db);
1242 return -1;
1243 }
1244 }
1245
1246 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1253 protected function load_motherof()
1254 {
1255 // phpcs:enable
1256 $this->motherof = array();
1257
1258 // Load array[child]=parent
1259 $sql = "SELECT fk_parent as id_parent, rowid as id_son";
1260 $sql .= " FROM ".MAIN_DB_PREFIX."categorie";
1261 $sql .= " WHERE fk_parent <> 0";
1262 $sql .= " AND entity IN (".getEntity('category').")";
1263
1264 dol_syslog(get_class($this)."::load_motherof", LOG_DEBUG);
1265 $resql = $this->db->query($sql);
1266 if ($resql) {
1267 while ($obj = $this->db->fetch_object($resql)) {
1268 $this->motherof[$obj->id_son] = $obj->id_parent;
1269 }
1270 return 1;
1271 } else {
1272 dol_print_error($this->db);
1273 return -1;
1274 }
1275 }
1276
1277 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1298 public function get_full_arbo($type, $fromid = 0, $include = 0, $forcelangcode = '')
1299 {
1300 // phpcs:enable
1301 global $langs;
1302
1303 if (!is_numeric($type)) {
1304 $type = $this->MAP_ID[$type];
1305 }
1306 if (is_null($type)) {
1307 $this->error = 'BadValueForParameterType';
1308 return -1;
1309 }
1310
1311 if (is_string($fromid)) {
1312 $fromid = explode(',', $fromid);
1313 } elseif (is_numeric($fromid)) {
1314 if ($fromid > 0) {
1315 $fromid = array($fromid);
1316 } else {
1317 $fromid = array();
1318 }
1319 } elseif (!is_array($fromid)) {
1320 $fromid = array();
1321 }
1322
1323 $this->cats = array();
1324 $nbcateg = 0;
1325
1326 // Init this->motherof that is array(id_son=>id_parent, ...)
1327 $this->load_motherof();
1328
1329 if ($forcelangcode) {
1330 $current_lang = $forcelangcode;
1331 } else {
1332 $current_lang = $langs->getDefaultLang();
1333 }
1334
1335 // Init $this->cats array
1336 // Note: The DISTINCT reduces pb with old tables with duplicates but should not be used
1337 $sql = "SELECT DISTINCT c.rowid, c.label, c.ref_ext, c.description, c.color, c.position, c.fk_parent, c.visible";
1338 if (getDolGlobalInt('MAIN_MULTILANGS') && $current_lang !== 'none') {
1339 $sql .= ", t.label as label_trans, t.description as description_trans";
1340 }
1341 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c";
1342 if (getDolGlobalInt('MAIN_MULTILANGS') && $current_lang !== 'none') {
1343 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_lang as t ON t.fk_category = c.rowid AND t.lang = '".$this->db->escape($current_lang)."'";
1344 }
1345 $sql .= " WHERE c.entity IN (".getEntity('category').")";
1346 $sql .= " AND c.type = ".(int) $type;
1347
1348 dol_syslog(get_class($this)."::get_full_arbo get category list", LOG_DEBUG);
1349
1350 $resql = $this->db->query($sql);
1351 if ($resql) {
1352 $i = 0;
1353 $nbcateg = $this->db->num_rows($resql);
1354
1355 while ($obj = $this->db->fetch_object($resql)) {
1356 $this->cats[(int) $obj->rowid]
1357 = array(
1358 'rowid' => (int) $obj->rowid,
1359 'id' => (int) $obj->rowid,
1360 'fk_parent' => (int) $obj->fk_parent,
1361 'label' => !empty($obj->label_trans) ? (string) $obj->label_trans : (string) $obj->label,
1362 'description' => !empty($obj->description_trans) ? (string) $obj->description_trans : (string) $obj->description,
1363 'color' => (string) $obj->color,
1364 'position' => (string) $obj->position,
1365 'visible' => (int) $obj->visible,
1366 'ref_ext' => (string) $obj->ref_ext,
1367 'picto' => 'category',
1368 // fields are filled with buildPathFromId later
1369 'fullpath' => '',
1370 'fulllabel' => '',
1371 'level' => null,
1372 );
1373 $i++;
1374 }
1375 } else {
1376 dol_print_error($this->db);
1377 return -1;
1378 }
1379
1380 // We add the fullpath property to each elements of first level (no parent exists)
1381 dol_syslog(get_class($this)."::get_full_arbo call to buildPathFromId", LOG_DEBUG);
1382
1383 foreach ($this->cats as $key => $val) {
1384 //print 'key='.$key.'<br>'."\n";
1385 $this->buildPathFromId($key, $nbcateg); // Process a branch from the root category key (this category has no parent) and adds level to $this->cats items
1386 }
1387
1388 // Include or exclude leaf (including $fromid) from tree
1389 if (count($fromid) > 0) {
1390 $keyfiltercatid = '('.implode('|', $fromid).')';
1391
1392 //print "Look to discard category ".$fromid."\n";
1393 $keyfilter1 = '^'.$keyfiltercatid.'$';
1394 $keyfilter2 = '_'.$keyfiltercatid.'$';
1395 $keyfilter3 = '^'.$keyfiltercatid.'_';
1396 $keyfilter4 = '_'.$keyfiltercatid.'_';
1397 foreach (array_keys($this->cats) as $key) {
1398 $fullpath = (string) $this->cats[$key]['fullpath'];
1399 $test = (preg_match('/'.$keyfilter1.'/', $fullpath) || preg_match('/'.$keyfilter2.'/', $fullpath)
1400 || preg_match('/'.$keyfilter3.'/', $fullpath) || preg_match('/'.$keyfilter4.'/', $fullpath));
1401
1402 if (($test && !$include) || (!$test && $include)) {
1403 unset($this->cats[$key]); // @phpstan-ignore-line
1404 }
1405 }
1406 }
1407
1408 dol_syslog(get_class($this)."::get_full_arbo dol_sort_array", LOG_DEBUG);
1409
1410 $this->cats = dol_sort_array($this->cats, 'fulllabel', 'asc', 1, 0, 1); // Sort on full label like "Label 1 >> Sublabel a >> Subsublabel"
1411
1412 return $this->cats;
1413 }
1414
1425 private function buildPathFromId($id_categ, $protection = 1000)
1426 {
1427 //dol_syslog(get_class($this)."::buildPathFromId id_categ=".$id_categ." protection=".$protection, LOG_DEBUG);
1428
1429 if (!empty($this->cats[$id_categ]['fullpath'])) {
1430 // Already defined
1431 dol_syslog(get_class($this)."::buildPathFromId fullpath and fulllabel already defined", LOG_WARNING);
1432 return -1;
1433 }
1434
1435 // $this->cats[$id_categ] is supposed to be already an array. We just want to complete it with property fullpath and fulllabel
1436
1437 // Define fullpath and fulllabel
1438 $this->cats[$id_categ]['fullpath'] = '_'.$id_categ;
1439 $this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label'];
1440 $i = 0;
1441 $cursor_categ = $id_categ;
1442 //print 'Work for id_categ='.$id_categ.'<br>'."\n";
1443 while ((empty($protection) || $i < $protection) && !empty($this->motherof[$cursor_categ])) {
1444 //print '&nbsp; cursor_categ='.$cursor_categ.' i='.$i.' '.$this->motherof[$cursor_categ].'<br>'."\n";
1445 $this->cats[$id_categ]['fullpath'] = '_'.$this->motherof[$cursor_categ].$this->cats[$id_categ]['fullpath'];
1446 $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'];
1447 //print '&nbsp; Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].' '.$this->cats[$id_categ]['fulllabel'].'<br>'."\n";
1448 $i++;
1449 $cursor_categ = $this->motherof[$cursor_categ];
1450 }
1451 //print 'Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].'<br>'."\n";
1452
1453 // We count number of _ to have level
1454 $nbunderscore = substr_count($this->cats[$id_categ]['fullpath'], '_');
1455 $this->cats[$id_categ]['level'] = ($nbunderscore ? $nbunderscore : null);
1456
1457 return 1;
1458 }
1459
1460
1461 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1469 public function get_all_categories($type = null, $parent = false)
1470 {
1471 // phpcs:enable
1472 if (!is_numeric($type) && !is_null($type)) {
1473 $type = $this->MAP_ID[$type];
1474 }
1475
1476 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1477 $sql .= " WHERE entity IN (".getEntity('category').")";
1478 if (!is_null($type)) {
1479 $sql .= " AND type = ".(int) $type;
1480 }
1481 if ($parent) {
1482 $sql .= " AND fk_parent = 0";
1483 }
1484
1485 $res = $this->db->query($sql);
1486 if ($res) {
1487 $cats = array();
1488 while ($rec = $this->db->fetch_array($res)) {
1489 $cat = new Categorie($this->db);
1490 $cat->fetch($rec['rowid']);
1491 $cats[$rec['rowid']] = $cat;
1492 }
1493 return $cats;
1494 } else {
1495 dol_print_error($this->db);
1496 return -1;
1497 }
1498 }
1499
1500 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1507 public function get_main_categories($type = null)
1508 {
1509 // phpcs:enable
1510 return $this->get_all_categories($type, true);
1511 }
1512
1513 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1519 public function already_exists()
1520 {
1521 // phpcs:enable
1522 $type = $this->type;
1523
1524 if (!is_numeric($type)) {
1525 $type = $this->MAP_ID[$type];
1526 }
1527
1528 /* We have to select any rowid from llx_categorie which category's mother and label
1529 * are equals to those of the calling category
1530 */
1531 $sql = "SELECT c.rowid";
1532 $sql .= " FROM ".MAIN_DB_PREFIX."categorie as c ";
1533 $sql .= " WHERE c.entity IN (".getEntity('category').")";
1534 $sql .= " AND c.type = ".((int) $type);
1535 $sql .= " AND c.fk_parent = ".((int) $this->fk_parent);
1536 $sql .= " AND c.label = '".$this->db->escape($this->label)."'";
1537
1538 dol_syslog(get_class($this)."::already_exists", LOG_DEBUG);
1539
1540 $resql = $this->db->query($sql);
1541 if ($resql) {
1542 if ($this->db->num_rows($resql) > 0) { // Checking for empty resql
1543 $obj = $this->db->fetch_object($resql);
1544 /* If object called create, obj cannot have is id.
1545 * If object called update, he mustn't have the same label as an other category for this mother.
1546 * 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.
1547 */
1548 if (!empty($obj) && $obj->rowid > 0 && $obj->rowid != $this->id) {
1549 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);
1550 return 1;
1551 }
1552 }
1553 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);
1554 return 0;
1555 } else {
1556 $this->error = $this->db->error();
1557 return -1;
1558 }
1559 }
1560
1561
1562 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1574 public function print_all_ways($sep = 'auto', $url = '', $nocolor = 0, $addpicto = 0, $notrunc = 0)
1575 {
1576 // phpcs:enable
1577 $ways = array();
1578
1579 if ($sep == 'auto') {
1580 $sep = '&gt;';
1581 }
1582
1583 $all_ways = $this->get_all_ways(); // Load array of categories to reach this->id
1584
1585 foreach ($all_ways as $way) { // It seems we always have 1 entry in this array.
1586 $w = array();
1587 $i = 0;
1588 $forced_color = '';
1589 foreach ($way as $cat) { // Loop on each successive categories to reach the target of current category
1590 $i++;
1591
1592 if (empty($nocolor)) {
1593 $forced_color = 'colortoreplace';
1594 if ($i == count($way)) { // Last category in hierarchy
1595 // Check contrast with background and correct text color
1596 $forced_color = 'categtextwhite'; // We want color white because the getNomUrl of a tag is always called inside a dark background like '<span color="bbb"></span>' to show it as a tag. TODO Add this in param to force when called outside of span.
1597 if ($cat->color) {
1598 if (colorIsLight($cat->color)) {
1599 $forced_color = 'categtextblack';
1600 }
1601 }
1602 }
1603 }
1604
1605 if ($url == '') {
1606 if (($i < count($way) && empty($notrunc)) && $i > 1) {
1607 $link = '';
1608 $linkend = '';
1609 } else {
1610 $link = '<a href="'.DOL_URL_ROOT.'/categories/viewcat.php?id='.((int) $cat->id).'&type='.urlencode($cat->type).'" class="'.(($i < count($way) && empty($notrunc)) ? 'small ' : '').$forced_color.'">';
1611 $linkend = '</a>';
1612 }
1613 $s = $link.(($addpicto && $i == 1) ? img_object('', 'category', 'class="paddingright"') : '');
1614 $s .= (($i < count($way) && empty($notrunc)) ? ($i == 1 ? dol_trunc($cat->label, 3) : '') : $cat->label);
1615 $s .= $linkend;
1616 $w[] = $s;
1617 } elseif ($url == 'none') {
1618 if (($i < count($way) && empty($notrunc)) && $i > 1) {
1619 $link = '';
1620 $linkend = '';
1621 } else {
1622 $link = '<span class="valignmiddle '.($i < count($way) ? 'small ' : '').$forced_color.'">';
1623 $linkend = '</span>';
1624 }
1625 $s = $link.(($addpicto && $i == 1) ? img_object('', 'category', 'class="paddingright"') : '');
1626 $s .= (($i < count($way) && empty($notrunc)) ? ($i == 1 ? dol_trunc($cat->label, 3) : '') : $cat->label);
1627 $s .= $linkend;
1628 $w[] = $s;
1629 } else {
1630 $w[] = '<a class="valignmiddle '.($i < count($way) ? 'small ' : '').$forced_color.'" href="'.DOL_URL_ROOT.'/'.$url.'?catid='.((int) $cat->id).'">'.($addpicto ? img_object('', 'category') : '').$cat->label.'</a>';
1631 }
1632 }
1633 $newcategwithpath = preg_replace('/colortoreplace/', $forced_color, implode('<span class="inline-block valignmiddle paddingleft paddingright small '.$forced_color.'">'.$sep.'</span>', $w));
1634
1635 $ways[] = $newcategwithpath;
1636 }
1637
1638 return $ways;
1639 }
1640
1641
1642 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1649 public function get_meres()
1650 {
1651 // phpcs:enable
1652 $parents = array();
1653
1654 $sql = "SELECT fk_parent FROM ".MAIN_DB_PREFIX."categorie";
1655 $sql .= " WHERE rowid = ".((int) $this->id);
1656
1657 $res = $this->db->query($sql);
1658
1659 if ($res) {
1660 while ($rec = $this->db->fetch_array($res)) {
1661 if ($rec['fk_parent'] > 0) {
1662 $cat = new Categorie($this->db);
1663 $cat->fetch($rec['fk_parent']);
1664 $parents[] = $cat;
1665 }
1666 }
1667 return $parents;
1668 } else {
1669 dol_print_error($this->db);
1670 return -1;
1671 }
1672 }
1673
1674 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1681 public function get_all_ways()
1682 {
1683 // phpcs:enable
1684 $ways = array();
1685
1686 $parents = $this->get_meres();
1687 if (is_array($parents)) {
1688 foreach ($parents as $parent) {
1689 $all_ways = $parent->get_all_ways(); // recursivity. TODO Add a protection for infinite loop
1690 foreach ($all_ways as $way) {
1691 $w = $way;
1692 $w[] = $this;
1693 $ways[] = $w;
1694 }
1695 }
1696 }
1697
1698 if (count($ways) == 0) {
1699 $ways[0][0] = $this;
1700 }
1701
1702 return $ways;
1703 }
1704
1715 public function containing($id, $type, $mode = 'object')
1716 {
1717 $cats = array();
1718
1719 if (is_numeric($type)) {
1720 $type = array_search($type, $this->MAP_ID);
1721 }
1722
1723 if ($type === Categorie::TYPE_BANK_LINE) { // TODO Remove this after migration of llx_category_bankline into llx_categorie_bankline
1724 // Load bank categories
1725 $sql = "SELECT c.label, c.rowid";
1726 $sql .= " FROM ".MAIN_DB_PREFIX."category_bankline as a, ".MAIN_DB_PREFIX."categorie as c";
1727 $sql .= " WHERE a.lineid=".((int) $id)." AND a.fk_categ = c.rowid";
1728 $sql .= " AND c.entity IN (".getEntity('category').")";
1729 $sql .= " ORDER BY c.label";
1730
1731 $res = $this->db->query($sql);
1732 if ($res) {
1733 while ($obj = $this->db->fetch_object($res)) {
1734 if ($mode == 'id') {
1735 $cats[] = $obj->rowid;
1736 } elseif ($mode == 'label') {
1737 $cats[] = $obj->label;
1738 } else {
1739 $cat = new Categorie($this->db);
1740 $cat->id = $obj->rowid;
1741 $cat->label = $obj->label;
1742 $cats[] = $cat;
1743 }
1744 }
1745 } else {
1746 dol_print_error($this->db);
1747 return -1;
1748 }
1749 } else {
1750 $sql = "SELECT ct.fk_categorie, c.label, c.rowid";
1751 $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";
1752 $sql .= " WHERE ct.fk_categorie = c.rowid AND ct.fk_".(empty($this->MAP_CAT_FK[$type]) ? $type : $this->MAP_CAT_FK[$type])." = ".(int) $id;
1753 // This seems useless because the table already contains id of category of 1 unique type. So commented.
1754 // So now it works also with external added categories.
1755 //$sql .= " AND c.type = ".((int) $this->MAP_ID[$type]);
1756 $sql .= " AND c.entity IN (".getEntity('category').")";
1757
1758 $res = $this->db->query($sql);
1759 if ($res) {
1760 while ($obj = $this->db->fetch_object($res)) {
1761 if ($mode == 'id') {
1762 $cats[] = $obj->rowid;
1763 } elseif ($mode == 'label') {
1764 $cats[] = $obj->label;
1765 } else {
1766 $cat = new Categorie($this->db);
1767 $cat->fetch($obj->fk_categorie);
1768 $cats[] = $cat;
1769 }
1770 }
1771 } else {
1772 dol_print_error($this->db);
1773 return -1;
1774 }
1775 }
1776
1777 return $cats;
1778 }
1779
1791 public function rechercher($id, $nom, $type, $exact = false, $case = false)
1792 {
1793 // Deprecation warning
1794 if (is_numeric($type)) {
1795 dol_syslog(__METHOD__.': using numeric types is deprecated.', LOG_WARNING);
1796 }
1797
1798 $cats = array();
1799
1800 // For backward compatibility
1801 if (is_numeric($type)) {
1802 // We want to reverse lookup
1803 $map_type = array_flip($this->MAP_ID);
1804 $type = $map_type[$type];
1805 dol_syslog(get_class($this)."::rechercher(): numeric types are deprecated, please use string instead", LOG_WARNING);
1806 }
1807
1808 // Generation requete recherche
1809 $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
1810 $sql .= " WHERE type = ".((int) $this->MAP_ID[$type]);
1811 $sql .= " AND entity IN (".getEntity('category').")";
1812 if ($nom) {
1813 if (!$exact) {
1814 $nom = '%'.$this->db->escape(str_replace('*', '%', $nom)).'%';
1815 }
1816 if (!$case) {
1817 $sql .= " AND label LIKE '".$this->db->escape($nom)."'";
1818 } else {
1819 $sql .= " AND label LIKE BINARY '".$this->db->escape($nom)."'";
1820 }
1821 }
1822 if ($id) {
1823 $sql .= " AND rowid = ".((int) $id);
1824 }
1825
1826 $res = $this->db->query($sql);
1827 if ($res) {
1828 while ($rec = $this->db->fetch_array($res)) {
1829 $cat = new Categorie($this->db);
1830 $cat->fetch($rec['rowid']);
1831 $cats[] = $cat;
1832 }
1833
1834 return $cats;
1835 } else {
1836 $this->error = $this->db->error().' sql='.$sql;
1837 return -1;
1838 }
1839 }
1840
1847 public function isAnyPhotoAvailable($sdir)
1848 {
1849 include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
1850 include_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php';
1851
1852 $sdir .= '/' . get_exdir($this->id, 2, 0, 0, $this, 'category') . $this->id . "/photos/";
1853
1854 $dir_osencoded = dol_osencode($sdir);
1855 if (file_exists($dir_osencoded)) {
1856 $handle = opendir($dir_osencoded);
1857 if (is_resource($handle)) {
1858 while (($file = readdir($handle)) !== false) {
1859 if (!utf8_check($file)) {
1860 $file = mb_convert_encoding($file, 'UTF-8', 'ISO-8859-1'); // To be sure data is stored in UTF8 in memory
1861 }
1862 if (dol_is_file($sdir . $file) && image_format_supported($file) >= 0) {
1863 return true;
1864 }
1865 }
1866 }
1867 }
1868 return false;
1869 }
1870
1877 public function getTooltipContentArray($params)
1878 {
1879 global $langs;
1880
1881 $langs->load('categories');
1882
1883 $datas = [];
1884
1885 $datas['label'] = $langs->trans("ShowCategory").': '.($this->ref ? $this->ref : $this->label);
1886
1887 return $datas;
1888 }
1889
1903 public function getNomUrl($withpicto = 0, $option = '', $maxlength = 0, $moreparam = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = 0)
1904 {
1905 global $conf, $langs, $hookmanager;
1906
1907 if (!empty($conf->dol_no_mouse_hover)) {
1908 $notooltip = 1; // Force disable tooltips
1909 }
1910
1911 $result = '';
1912 $params = [
1913 'id' => $this->id,
1914 'objecttype' => $this->element,
1915 'option' => $option,
1916 ];
1917 $classfortooltip = 'classfortooltip';
1918 $dataparams = '';
1919 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1920 $classfortooltip = 'classforajaxtooltip';
1921 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1922 $label = '';
1923 } else {
1924 $label = implode($this->getTooltipContentArray($params));
1925 }
1926
1927 $url = DOL_URL_ROOT.'/categories/viewcat.php?id='.$this->id.'&type='.$this->type.$moreparam.'&backtopage='.urlencode($_SERVER['PHP_SELF'].($moreparam ? '?'.$moreparam : ''));
1928
1929 if ($option !== 'nolink') {
1930 // Add param to save lastsearch_values or not
1931 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1932 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1933 $add_save_lastsearch_values = 1;
1934 }
1935 if (/* $url && */ $add_save_lastsearch_values) {
1936 $url .= '&save_lastsearch_values=1';
1937 }
1938 }
1939
1940 // Check contrast with background and correct text color
1941 $forced_color = 'categtextwhite'; // We want color white because the getNomUrl of a tag is always called inside a dark background like '<span color="bbb"></span>' to show it as a tag. TODO Add this in param to force when called outside of span.
1942 if ($this->color) {
1943 if (colorIsLight($this->color)) { // If color is light, we force color to dark
1944 $forced_color = 'categtextblack';
1945 }
1946 }
1947
1948 $linkclose = '';
1949 if (empty($notooltip)) {
1950 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1951 $label = $langs->trans("ShowCategory");
1952 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1953 }
1954 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1955 $linkclose .= $dataparams.' class="'.$classfortooltip.' '.$forced_color.($morecss ? ' '.$morecss : '').'"';
1956 } else {
1957 $linkclose = ' class="'.$forced_color.($morecss ? ' '.$morecss : '').'"';
1958 }
1959
1960 if ($option == 'nolink' /* || empty($url) */) {
1961 $linkstart = '<span';
1962 } else {
1963 $linkstart = '<a href="'.$url.'"';
1964 }
1965 $linkstart .= $linkclose.'>';
1966 if ($option == 'nolink' /* || empty($url) */) {
1967 $linkend = '</span>';
1968 } else {
1969 $linkend = '</a>';
1970 }
1971
1972 $result .= $linkstart;
1973
1974 if ($withpicto) {
1975 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1976 }
1977
1978 if ($withpicto != 2) {
1979 $result .= dol_trunc(($this->ref ? $this->ref : $this->label), $maxlength);
1980 }
1981
1982 $result .= $linkend;
1983
1984 global $action;
1985 $hookmanager->initHooks(array($this->element . 'dao'));
1986 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1987 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1988 if ($reshook > 0) {
1989 $result = $hookmanager->resPrint;
1990 } else {
1991 $result .= $hookmanager->resPrint;
1992 }
1993 return $result;
1994 }
1995
1996
1997 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2005 public function add_photo($sdir, $file)
2006 {
2007 // phpcs:enable
2008 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2009
2010 $dir = $sdir.'/'.get_exdir($this->id, 2, 0, 0, $this, 'category').$this->id."/";
2011 $dir .= "photos/";
2012
2013 if (!file_exists($dir)) {
2014 dol_mkdir($dir);
2015 }
2016
2017 if (file_exists($dir)) {
2018 if (is_array($file['name'])) {
2019 $nbfile = count($file['name']);
2020 for ($i = 0; $i < $nbfile; $i++) {
2021 $originImage = $dir.$file['name'][$i];
2022
2023 // Cree fichier en taille origine
2024 dol_move_uploaded_file($file['tmp_name'][$i], $originImage, 1, 0, 0);
2025
2026 if (file_exists($originImage)) {
2027 // Create thumbs
2028 $this->addThumbs($originImage);
2029 }
2030 }
2031 } else {
2032 $originImage = $dir.$file['name'];
2033
2034 // Cree fichier en taille origine
2035 dol_move_uploaded_file($file['tmp_name'], $originImage, 1, 0, 0);
2036
2037 if (file_exists($originImage)) {
2038 // Create thumbs
2039 $this->addThumbs($originImage);
2040 }
2041 }
2042 }
2043 }
2044
2045 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2053 public function liste_photos($dir, $nbmax = 0)
2054 {
2055 // phpcs:enable
2056 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2057
2058 $nbphoto = 0;
2059 $tabobj = array();
2060
2061 $dirthumb = $dir.'thumbs/';
2062
2063 if (file_exists($dir)) {
2064 $handle = opendir($dir);
2065 if (is_resource($handle)) {
2066 while (($file = readdir($handle)) !== false) {
2067 if (dol_is_file($dir.$file) && preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $dir.$file)) {
2068 $nbphoto++;
2069 $photo = $file;
2070
2071 // On determine nom du fichier vignette
2072 $photo_vignette = '';
2073 $regs = array();
2074 if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $photo, $regs)) {
2075 $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $photo).'_small'.$regs[0];
2076 }
2077
2078 // Object
2079 $obj = array();
2080 $obj['photo'] = $photo;
2081 if ($photo_vignette && is_file($dirthumb.$photo_vignette)) {
2082 $obj['photo_vignette'] = 'thumbs/'.$photo_vignette;
2083 } else {
2084 $obj['photo_vignette'] = "";
2085 }
2086
2087 $tabobj[$nbphoto - 1] = $obj;
2088
2089 // On continue ou on arrete de boucler
2090 if ($nbmax && $nbphoto >= $nbmax) {
2091 break;
2092 }
2093 }
2094 }
2095
2096 closedir($handle);
2097 }
2098 }
2099
2100 return $tabobj;
2101 }
2102
2103 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2110 public function delete_photo($file)
2111 {
2112 // phpcs:enable
2113 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
2114
2115 $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
2116 $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
2117 $filename = preg_replace('/'.preg_quote($dir, '/').'/i', '', $file); // Nom du fichier
2118
2119 // On efface l'image d'origine
2120 dol_delete_file($file, 0); // do not use disableglob, ecmfiles will not be deleted
2121
2122 // Si elle existe, on efface la vignette
2123 $regs = array();
2124 if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i', $filename, $regs)) {
2125 $photo_vignette = preg_replace('/'.$regs[0].'/i', '', $filename).'_small'.$regs[0];
2126 if (file_exists($dirthumb.$photo_vignette)) {
2127 dol_delete_file($dirthumb.$photo_vignette, 1);
2128 }
2129 }
2130 }
2131
2132 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2139 public function get_image_size($file)
2140 {
2141 // phpcs:enable
2142 $infoImg = getimagesize($file); // Recuperation des infos de l'image
2143 $this->imgWidth = $infoImg[0]; // Largeur de l'image
2144 $this->imgHeight = $infoImg[1]; // Hauteur de l'image
2145 }
2146
2155 public function setMultiLangs(User $user, $notrigger = 0)
2156 {
2157 global $langs;
2158
2159 $langs_available = $langs->get_available_languages();
2160 $current_lang = $langs->getDefaultLang();
2161
2162 foreach ($langs_available as $key => $value) {
2163 $sql = "SELECT rowid";
2164 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_lang";
2165 $sql .= " WHERE fk_category=".((int) $this->id);
2166 $sql .= " AND lang = '".$this->db->escape($key)."'";
2167
2168 $result = $this->db->query($sql);
2169
2170 if ($key == $current_lang) {
2171 $sql2 = '';
2172 if ($this->db->num_rows($result)) { // if no line in database
2173 $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang";
2174 $sql2 .= " SET label = '".$this->db->escape($this->label)."',";
2175 $sql2 .= " description = '".$this->db->escape($this->description)."'";
2176 $sql2 .= " WHERE fk_category = ".((int) $this->id)." AND lang = '".$this->db->escape($key)."'";
2177 } elseif (isset($this->multilangs[$key])) {
2178 $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)";
2179 $sql2 .= " VALUES(".((int) $this->id).", '".$this->db->escape($key)."', '".$this->db->escape($this->label)."'";
2180 $sql2 .= ", '".$this->db->escape($this->multilangs[$key]["description"])."')";
2181 }
2182 dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG);
2183 if ($sql2 && !$this->db->query($sql2)) {
2184 $this->error = $this->db->lasterror();
2185 return -1;
2186 }
2187 } elseif (isset($this->multilangs[$key])) {
2188 if ($this->db->num_rows($result)) { // if no line in database
2189 $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang";
2190 $sql2 .= " SET label='".$this->db->escape($this->multilangs[$key]["label"])."',";
2191 $sql2 .= " description='".$this->db->escape($this->multilangs[$key]["description"])."'";
2192 $sql2 .= " WHERE fk_category=".((int) $this->id)." AND lang='".$this->db->escape($key)."'";
2193 } else {
2194 $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)";
2195 $sql2 .= " VALUES(".((int) $this->id).", '".$this->db->escape($key)."', '".$this->db->escape($this->multilangs[$key]["label"])."'";
2196 $sql2 .= ",'".$this->db->escape($this->multilangs[$key]["description"])."')";
2197 }
2198
2199 // on ne sauvegarde pas des champs vides
2200 if ($this->multilangs[$key]["label"] || $this->multilangs[$key]["description"] || $this->multilangs[$key]["note"]) {
2201 dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG);
2202 }
2203 if (!$this->db->query($sql2)) {
2204 $this->error = $this->db->lasterror();
2205 return -1;
2206 }
2207 }
2208 }
2209
2210 // Call trigger
2211 if (!$notrigger) {
2212 $result = $this->call_trigger('CATEGORY_SET_MULTILANGS', $user);
2213 if ($result < 0) {
2214 $this->error = $this->db->lasterror();
2215 return -1;
2216 }
2217 }
2218 // End call triggers
2219
2220 return 1;
2221 }
2222
2231 public function delMultiLangs($langtodelete, $user)
2232 {
2233 $sql = "DELETE FROM ".$this->db->prefix()."categorie_lang";
2234 $sql .= " WHERE fk_category = ".((int) $this->id)." AND lang = '".$this->db->escape($langtodelete)."'";
2235
2236 dol_syslog(get_class($this).'::delMultiLangs', LOG_DEBUG);
2237 $result = $this->db->query($sql);
2238 if ($result) {
2239 // Call trigger
2240 $result = $this->call_trigger('CATEGORY_DEL_MULTILANGS', $user);
2241 if ($result < 0) {
2242 $this->error = $this->db->lasterror();
2243 dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
2244 return -1;
2245 }
2246 // End call triggers
2247 return 1;
2248 } else {
2249 $this->error = $this->db->lasterror();
2250 dol_syslog(get_class($this).'::delMultiLangs error='.$this->error, LOG_ERR);
2251 return -1;
2252 }
2253 }
2254
2260 public function getMultiLangs()
2261 {
2262 global $langs;
2263
2264 $current_lang = $langs->getDefaultLang();
2265
2266 $sql = "SELECT lang, label, description";
2267 $sql .= " FROM ".MAIN_DB_PREFIX."categorie_lang";
2268 $sql .= " WHERE fk_category=".((int) $this->id);
2269
2270 $result = $this->db->query($sql);
2271 if ($result) {
2272 while ($obj = $this->db->fetch_object($result)) {
2273 //print 'lang='.$obj->lang.' current='.$current_lang.'<br>';
2274 if ($obj->lang == $current_lang) { // si on a les traduct. dans la langue courante on les charge en infos principales.
2275 $this->label = $obj->label;
2276 $this->description = $obj->description;
2277 }
2278 $this->multilangs[$obj->lang]["label"] = $obj->label;
2279 $this->multilangs[$obj->lang]["description"] = $obj->description;
2280 }
2281 return 1;
2282 } else {
2283 $this->error = $langs->trans("Error")." : ".$this->db->error()." - ".$sql;
2284 return -1;
2285 }
2286 }
2287
2294 public function getLibStatut($mode)
2295 {
2296 return '';
2297 }
2298
2299
2307 public function initAsSpecimen()
2308 {
2309 dol_syslog(get_class($this)."::initAsSpecimen");
2310
2311 // Initialise parameters
2312 $this->id = 0;
2313 $this->fk_parent = 0;
2314 $this->label = 'SPECIMEN';
2315 $this->specimen = 1;
2316 $this->description = 'This is a description';
2317 $this->socid = 1;
2318 $this->type = self::TYPE_PRODUCT;
2319
2320 return 1;
2321 }
2322
2331 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
2332 {
2333 $tables = array(
2334 'categorie_societe'
2335 );
2336
2337 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables, 1);
2338 }
2339
2348 public static function getFilterJoinQuery($type, $rowIdName)
2349 {
2350 if ($type == 'bank_account') {
2351 $type = 'account';
2352 }
2353
2354 return " LEFT JOIN ".MAIN_DB_PREFIX."categorie_".$type." as cp ON ".$rowIdName." = cp.fk_".$type;
2355 }
2356
2366 public static function getFilterSelectQuery($type, $rowIdName, $searchList)
2367 {
2368 if ($type == 'bank_account') {
2369 $type = 'account';
2370 }
2371 if ($type == 'customer') {
2372 $type = 'societe';
2373 }
2374 if ($type == 'supplier') {
2375 $type = 'fournisseur';
2376 }
2377
2378 if (empty($searchList) && !is_array($searchList)) {
2379 return "";
2380 }
2381
2382 $searchCategorySqlList = array();
2383 foreach ($searchList as $searchCategory) {
2384 if (intval($searchCategory) == -2) {
2385 $searchCategorySqlList[] = " cp.fk_categorie IS NULL";
2386 } elseif (intval($searchCategory) > 0) {
2387 $searchCategorySqlList[] = " ".$rowIdName." IN (SELECT fk_".$type." FROM ".MAIN_DB_PREFIX."categorie_".$type." WHERE fk_categorie = ".((int) $searchCategory).")";
2388 }
2389 }
2390
2391 if (!empty($searchCategorySqlList)) {
2392 return " AND (".implode(' AND ', $searchCategorySqlList).")";
2393 } else {
2394 return "";
2395 }
2396 }
2397
2403 public function countNbOfCategories()
2404 {
2405 dol_syslog(get_class($this)."::count_all_categories", LOG_DEBUG);
2406 $sql = "SELECT COUNT(rowid) FROM ".MAIN_DB_PREFIX."categorie";
2407 $sql .= " WHERE entity IN (".getEntity('category').")";
2408
2409 $res = $this->db->query($sql);
2410 if ($res) {
2411 $obj = $this->db->fetch_object($res);
2412 return $obj->count;
2413 } else {
2414 dol_print_error($this->db);
2415 return -1;
2416 }
2417 }
2418}
print $object position
Definition edit.php:207
$object ref
Definition info.php:90
Class to manage categories.
getListForItem($id, $type='customer', $sortfield="s.rowid", $sortorder='ASC', $limit=0, $page=0)
Return the list of the categories of a given element (a product, a customer, ...).
get_image_size($file)
Load size of image file.
load_motherof()
Load the array this->motherof that is array(id_son=>id_parent, ...), so array of all child categories...
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 a given object having id $id and typ...
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.
delMultiLangs($langtodelete, $user)
Delete a language for this category.
create($user, $notrigger=0)
Add category into database.
rechercher($id, $nom, $type, $exact=false, $case=false)
Returns categories whose id or name matches.
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 a given object in the current category.
get_main_categories($type=null)
Returns the first level categories (which are not child)
getMapId()
Get MAP_ID.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
setMultiLangs(User $user, $notrigger=0)
Create or Update translations of categories labels.
getMapList()
Get map list.
$fields
'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter...
get_meres()
Returns an array containing the list of parent categories Note: A category can only have one parent b...
get_full_arbo($type, $fromid=0, $include=0, $forcelangcode='')
Rebuilding the category tree as an array Return an array of table('id','id_mere',....
add_type($obj, $type='')
Link an object to the category.
print_all_ways($sep='auto', $url='', $nocolor=0, $addpicto=0, $notrunc=0)
Returns the path of the category, with the names of the categories separated by $sep (" >> " by defau...
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 array all possible paths to go to the category starting with the major categories repres...
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.
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.
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.
addThumbs($file, $quality=50)
Build thumb.
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_is_file($pathoffile)
Return if path is a file.
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $keyforsourcefile='addedfile', $upload_dir='', $mode=0)
Check validity of a file upload from an GUI page, and move it to its final destination.
dol_now($mode='gmt')
Return date for now.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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 a 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.
isModEnabled($module)
Is Dolibarr module enabled.
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|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:125