1 | <?php |
||
2 | /* Copyright (C) 2005 Matthieu Valleton <[email protected]> |
||
3 | * Copyright (C) 2005 Davoleau Brice <[email protected]> |
||
4 | * Copyright (C) 2005 Rodolphe Quiedeville <[email protected]> |
||
5 | * Copyright (C) 2006-2012 Regis Houssin <[email protected]> |
||
6 | * Copyright (C) 2006-2012 Laurent Destailleur <[email protected]> |
||
7 | * Copyright (C) 2007 Patrick Raguin <[email protected]> |
||
8 | * Copyright (C) 2013-2016 Juanjo Menent <[email protected]> |
||
9 | * Copyright (C) 2013-2018 Philippe Grand <[email protected]> |
||
10 | * Copyright (C) 2015 Marcos García <[email protected]> |
||
11 | * Copyright (C) 2015 Raphaël Doursenaud <[email protected]> |
||
12 | * Copyright (C) 2016 Charlie Benke <[email protected]> |
||
13 | * Copyright (C) 2018 Frédéric France <[email protected]> |
||
14 | * |
||
15 | * This program is free software; you can redistribute it and/or modify |
||
16 | * it under the terms of the GNU General Public License as published by |
||
17 | * the Free Software Foundation; either version 3 of the License, or |
||
18 | * (at your option) any later version. |
||
19 | * |
||
20 | * This program is distributed in the hope that it will be useful, |
||
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
23 | * GNU General Public License for more details. |
||
24 | * |
||
25 | * You should have received a copy of the GNU General Public License |
||
26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
27 | */ |
||
28 | namespace Alixar\Base; |
||
29 | |||
30 | /** |
||
31 | * \file htdocs/categories/class/categorie.class.php |
||
32 | * \ingroup categorie |
||
33 | * \brief File of class to manage categories |
||
34 | */ |
||
35 | |||
36 | /* |
||
37 | * require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; |
||
38 | * require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; |
||
39 | * require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php'; |
||
40 | * require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; |
||
41 | */ |
||
42 | |||
43 | use Alixar\Base\CommonObject; |
||
44 | |||
45 | /** |
||
46 | * Class to manage categories |
||
47 | */ |
||
48 | class Categorie extends CommonObject |
||
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 | |||
61 | /** |
||
62 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
63 | */ |
||
64 | public $picto = 'category'; |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @var array ID mapping from type string |
||
69 | * |
||
70 | * @note This array should be remove in future, once previous constants are moved to the string value. Deprecated |
||
71 | */ |
||
72 | protected $MAP_ID = array( |
||
73 | 'product' => 0, |
||
74 | 'supplier' => 1, |
||
75 | 'customer' => 2, |
||
76 | 'member' => 3, |
||
77 | 'contact' => 4, |
||
78 | 'bank_account' => 5, |
||
79 | 'project' => 6, |
||
80 | 'user' => 7, |
||
81 | 'bank_line' => 8, |
||
82 | ); |
||
83 | |||
84 | /** |
||
85 | * @var array Code mapping from ID |
||
86 | */ |
||
87 | public static $MAP_ID_TO_CODE = array( |
||
88 | 0 => 'product', |
||
89 | 1 => 'supplier', |
||
90 | 2 => 'customer', |
||
91 | 3 => 'member', |
||
92 | 4 => 'contact', |
||
93 | 5 => 'bank_account', |
||
94 | 6 => 'project', |
||
95 | 7 => 'user', |
||
96 | 8 => 'bank_line', |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * @var array Foreign keys mapping from type string |
||
101 | * |
||
102 | * @note Move to const array when PHP 5.6 will be our minimum target |
||
103 | */ |
||
104 | protected $MAP_CAT_FK = array( |
||
105 | 'product' => 'product', |
||
106 | 'customer' => 'soc', |
||
107 | 'supplier' => 'soc', |
||
108 | 'member' => 'member', |
||
109 | 'contact' => 'socpeople', |
||
110 | 'user' => 'user', |
||
111 | 'account' => 'account', // old for bank_account |
||
112 | 'bank_account' => 'account', |
||
113 | 'project' => 'project', |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * @var array Category tables mapping from type string |
||
118 | * |
||
119 | * @note Move to const array when PHP 5.6 will be our minimum target |
||
120 | */ |
||
121 | protected $MAP_CAT_TABLE = array( |
||
122 | 'product' => 'product', |
||
123 | 'customer' => 'societe', |
||
124 | 'supplier' => 'fournisseur', |
||
125 | 'member' => 'member', |
||
126 | 'contact' => 'contact', |
||
127 | 'user' => 'user', |
||
128 | 'account' => 'account', // old for bank_account |
||
129 | 'bank_account'=> 'account', |
||
130 | 'project' => 'project', |
||
131 | ); |
||
132 | |||
133 | /** |
||
134 | * @var array Object class mapping from type string |
||
135 | * |
||
136 | * @note Move to const array when PHP 5.6 will be our minimum target |
||
137 | */ |
||
138 | protected $MAP_OBJ_CLASS = array( |
||
139 | 'product' => 'Product', |
||
140 | 'customer' => 'Societe', |
||
141 | 'supplier' => 'Fournisseur', |
||
142 | 'member' => 'Adherent', |
||
143 | 'contact' => 'Contact', |
||
144 | 'user' => 'User', |
||
145 | 'account' => 'Account', // old for bank account |
||
146 | 'bank_account' => 'Account', |
||
147 | 'project' => 'Project', |
||
148 | ); |
||
149 | |||
150 | /** |
||
151 | * @var array Object table mapping from type string |
||
152 | * |
||
153 | * @note Move to const array when PHP 5.6 will be our minimum target |
||
154 | */ |
||
155 | protected $MAP_OBJ_TABLE = array( |
||
156 | 'product' => 'product', |
||
157 | 'customer' => 'societe', |
||
158 | 'supplier' => 'societe', |
||
159 | 'member' => 'adherent', |
||
160 | 'contact' => 'socpeople', |
||
161 | 'user' => 'user', |
||
162 | 'account' => 'bank_account', |
||
163 | 'project' => 'projet', |
||
164 | ); |
||
165 | |||
166 | /** |
||
167 | * @var string ID to identify managed object |
||
168 | */ |
||
169 | public $element='category'; |
||
170 | |||
171 | /** |
||
172 | * @var string Name of table without prefix where object is stored |
||
173 | */ |
||
174 | public $table_element='categorie'; |
||
175 | |||
176 | /** |
||
177 | * @var int ID |
||
178 | */ |
||
179 | public $fk_parent; |
||
180 | |||
181 | /** |
||
182 | * @var string Category label |
||
183 | */ |
||
184 | public $label; |
||
185 | |||
186 | /** |
||
187 | * @var string description |
||
188 | */ |
||
189 | public $description; |
||
190 | |||
191 | /** |
||
192 | * @var string Color |
||
193 | */ |
||
194 | public $color; |
||
195 | /** |
||
196 | * @var ??? |
||
197 | */ |
||
198 | public $socid; |
||
199 | /** |
||
200 | * @var string Category type |
||
201 | * |
||
202 | * @see Categorie::TYPE_PRODUCT |
||
203 | * @see Categorie::TYPE_SUPPLIER |
||
204 | * @see Categorie::TYPE_CUSTOMER |
||
205 | * @see Categorie::TYPE_MEMBER |
||
206 | * @see Categorie::TYPE_CONTACT |
||
207 | * @see Categorie::TYPE_USER |
||
208 | * @see Categorie::TYPE_ACCOUNT |
||
209 | * @see Categorie::TYPE_PROJECT |
||
210 | */ |
||
211 | public $type; |
||
212 | |||
213 | /** |
||
214 | * @var array Categories table in memory |
||
215 | */ |
||
216 | public $cats = array(); |
||
217 | |||
218 | /** |
||
219 | * @var array Mother of table |
||
220 | */ |
||
221 | public $motherof = array(); |
||
222 | |||
223 | /** |
||
224 | * Constructor |
||
225 | * |
||
226 | * @param DoliDB $db Database handler |
||
227 | */ |
||
228 | function __construct() |
||
229 | { |
||
230 | // $this->db = $db; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Load category into memory from database |
||
235 | * |
||
236 | * @param int $id Id of category |
||
237 | * @param string $label Label of category |
||
238 | * @param string $type Type of category ('product', '...') or (0, 1, ...) |
||
239 | * @return int <0 if KO, >0 if OK |
||
240 | */ |
||
241 | function fetch($id, $label='', $type=null) |
||
242 | { |
||
243 | global $conf; |
||
244 | |||
245 | // Check parameters |
||
246 | if (empty($id) && empty($label)) return -1; |
||
247 | if (! is_numeric($type)) $type=$this->MAP_ID[$type]; |
||
248 | |||
249 | $sql = "SELECT rowid, fk_parent, entity, label, description, color, fk_soc, visible, type"; |
||
250 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie"; |
||
251 | if ($id > 0) |
||
252 | { |
||
253 | $sql.= " WHERE rowid = ".$id; |
||
254 | } |
||
255 | else |
||
256 | { |
||
257 | $sql.= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category').")"; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
258 | if (! is_null($type)) $sql.= " AND type = ".$this->db->escape($type); |
||
259 | } |
||
260 | |||
261 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
262 | $resql = $this->db->query($sql); |
||
263 | if ($resql) |
||
264 | { |
||
265 | if ($this->db->num_rows($resql) > 0) |
||
266 | { |
||
267 | $res = $this->db->fetch_array($resql); |
||
268 | |||
269 | $this->id = $res['rowid']; |
||
270 | //$this->ref = $res['rowid']; |
||
271 | $this->fk_parent = $res['fk_parent']; |
||
272 | $this->label = $res['label']; |
||
273 | $this->description = $res['description']; |
||
274 | $this->color = $res['color']; |
||
275 | $this->socid = $res['fk_soc']; |
||
276 | $this->visible = $res['visible']; |
||
277 | $this->type = $res['type']; |
||
278 | $this->entity = $res['entity']; |
||
279 | |||
280 | // Retreive all extrafield |
||
281 | // fetch optionals attributes and labels |
||
282 | $this->fetch_optionals(); |
||
283 | |||
284 | $this->db->free($resql); |
||
285 | |||
286 | // multilangs |
||
287 | if (!empty(Globals::$conf->global->MAIN_MULTILANGS)) |
||
288 | $this->getMultiLangs(); |
||
289 | |||
290 | return 1; |
||
291 | } |
||
292 | else |
||
293 | { |
||
294 | return 0; |
||
295 | } |
||
296 | } |
||
297 | else |
||
298 | { |
||
299 | dol_print_error($this->db); |
||
300 | return -1; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Add category into database |
||
306 | * |
||
307 | * @param User $user Object user |
||
308 | * @return int -1 : SQL error |
||
309 | * -2 : new ID unknown |
||
310 | * -3 : Invalid category |
||
311 | * -4 : category already exists |
||
312 | */ |
||
313 | function create($user) |
||
314 | { |
||
315 | global $conf,$langs,$hookmanager; |
||
316 | $langs->load('categories'); |
||
317 | |||
318 | $type=$this->type; |
||
319 | |||
320 | if (! is_numeric($type)) $type=$this->MAP_ID[$type]; |
||
321 | |||
322 | $error=0; |
||
323 | |||
324 | dol_syslog(get_class($this).'::create', LOG_DEBUG); |
||
325 | |||
326 | // Clean parameters |
||
327 | $this->label = trim($this->label); |
||
328 | $this->description = trim($this->description); |
||
329 | $this->color = trim($this->color); |
||
330 | $this->import_key = trim($this->import_key); |
||
331 | if (empty($this->visible)) $this->visible=0; |
||
332 | $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0); |
||
333 | |||
334 | if ($this->already_exists()) |
||
335 | { |
||
336 | $this->error=$langs->trans("ImpossibleAddCat", $this->label); |
||
337 | $this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel"); |
||
338 | dol_syslog($this->error, LOG_WARNING); |
||
339 | return -4; |
||
340 | } |
||
341 | |||
342 | $this->db->begin(); |
||
0 ignored issues
–
show
|
|||
343 | |||
344 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie ("; |
||
345 | $sql.= "fk_parent,"; |
||
346 | $sql.= " label,"; |
||
347 | $sql.= " description,"; |
||
348 | $sql.= " color,"; |
||
349 | if (!empty(Globals::$conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { |
||
350 | $sql.= "fk_soc,"; |
||
351 | } |
||
352 | $sql.= " visible,"; |
||
353 | $sql.= " type,"; |
||
354 | $sql.= " import_key,"; |
||
355 | $sql.= " entity"; |
||
356 | $sql.= ") VALUES ("; |
||
357 | $sql.= $this->db->escape($this->fk_parent).","; |
||
358 | $sql.= "'".$this->db->escape($this->label)."',"; |
||
359 | $sql.= "'".$this->db->escape($this->description)."',"; |
||
360 | $sql.= "'".$this->db->escape($this->color)."',"; |
||
361 | if (!empty(Globals::$conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { |
||
362 | $sql.= ($this->socid != -1 ? $this->socid : 'null').","; |
||
363 | } |
||
364 | $sql.= "'".$this->db->escape($this->visible)."',"; |
||
365 | $sql.= $this->db->escape($type).","; |
||
366 | $sql.= (! empty($this->import_key)?"'".$this->db->escape($this->import_key)."'":'null').","; |
||
367 | $sql .= $this->db->escape(Globals::$conf->entity); |
||
368 | $sql.= ")"; |
||
369 | |||
370 | $res = $this->db->query($sql); |
||
371 | if ($res) |
||
372 | { |
||
373 | $id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie"); |
||
374 | |||
375 | if ($id > 0) |
||
376 | { |
||
377 | $this->id = $id; |
||
378 | |||
379 | $action='create'; |
||
380 | |||
381 | // Actions on extra fields |
||
382 | if (empty(Globals::$conf->global->MAIN_EXTRAFIELDS_DISABLED)) { // For avoid conflicts if trigger used{ |
||
383 | $result=$this->insertExtraFields(); |
||
384 | if ($result < 0) |
||
385 | { |
||
386 | $error++; |
||
387 | } |
||
388 | } |
||
389 | |||
390 | if (! $error) |
||
391 | { |
||
392 | // Call trigger |
||
393 | $result=$this->call_trigger('CATEGORY_CREATE',$user); |
||
394 | if ($result < 0) { $error++; } |
||
395 | // End call triggers |
||
396 | } |
||
397 | |||
398 | if ( ! $error ) |
||
399 | { |
||
400 | $this->db->commit(); |
||
401 | return $id; |
||
402 | } |
||
403 | else |
||
404 | { |
||
405 | $this->db->rollback(); |
||
406 | return -3; |
||
407 | } |
||
408 | } |
||
409 | else |
||
410 | { |
||
411 | $this->db->rollback(); |
||
412 | return -2; |
||
413 | } |
||
414 | } |
||
415 | else |
||
416 | { |
||
417 | $this->error=$this->db->error(); |
||
418 | $this->db->rollback(); |
||
419 | return -1; |
||
420 | } |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Update category |
||
425 | * |
||
426 | * @param User $user Object user |
||
427 | * @return int 1 : OK |
||
428 | * -1 : SQL error |
||
429 | * -2 : invalid category |
||
430 | */ |
||
431 | function update(User $user) |
||
432 | { |
||
433 | global $conf, $langs,$hookmanager; |
||
434 | |||
435 | $error=0; |
||
436 | |||
437 | // Clean parameters |
||
438 | $this->label=trim($this->label); |
||
439 | $this->description=trim($this->description); |
||
440 | $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0); |
||
441 | $this->visible = ($this->visible != "" ? intval($this->visible) : 0); |
||
442 | |||
443 | if ($this->already_exists()) |
||
444 | { |
||
445 | $this->error=$langs->trans("ImpossibleUpdateCat"); |
||
446 | $this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel"); |
||
447 | return -1; |
||
448 | } |
||
449 | |||
450 | $this->db->begin(); |
||
0 ignored issues
–
show
|
|||
451 | |||
452 | $sql = "UPDATE ".MAIN_DB_PREFIX."categorie"; |
||
453 | $sql.= " SET label = '".$this->db->escape($this->label)."',"; |
||
454 | $sql.= " description = '".$this->db->escape($this->description)."',"; |
||
455 | $sql.= " color = '".$this->db->escape($this->color)."'"; |
||
456 | if (!empty(Globals::$conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER)) { |
||
457 | $sql .= ", fk_soc = ".($this->socid != -1 ? $this->socid : 'null'); |
||
458 | } |
||
459 | $sql .= ", visible = '".$this->db->escape($this->visible)."'"; |
||
460 | $sql .= ", fk_parent = ".$this->fk_parent; |
||
461 | $sql .= " WHERE rowid = ".$this->id; |
||
462 | |||
463 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
464 | if ($this->db->query($sql)) |
||
465 | { |
||
466 | $action='update'; |
||
467 | |||
468 | // Actions on extra fields |
||
469 | if (empty(Globals::$conf->global->MAIN_EXTRAFIELDS_DISABLED)) { // For avoid conflicts if trigger used{ |
||
470 | $result=$this->insertExtraFields(); |
||
471 | if ($result < 0) |
||
472 | { |
||
473 | $error++; |
||
474 | } |
||
475 | } |
||
476 | |||
477 | if (! $error) |
||
478 | { |
||
479 | // Call trigger |
||
480 | $result=$this->call_trigger('CATEGORY_MODIFY',$user); |
||
481 | if ($result < 0) { $error++; $this->db->rollback(); return -1; } |
||
482 | // End call triggers |
||
483 | } |
||
484 | |||
485 | $this->db->commit(); |
||
486 | |||
487 | return 1; |
||
488 | } |
||
489 | else |
||
490 | { |
||
491 | $this->db->rollback(); |
||
492 | dol_print_error($this->db); |
||
493 | return -1; |
||
494 | } |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Delete a category from database |
||
499 | * |
||
500 | * @param User $user Object user that ask to delete |
||
501 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
502 | * @return int <0 KO >0 OK |
||
503 | */ |
||
504 | function delete($user, $notrigger=0) |
||
505 | { |
||
506 | global $conf,$langs; |
||
507 | |||
508 | $error=0; |
||
509 | |||
510 | // Clean parameters |
||
511 | $this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0); |
||
512 | |||
513 | dol_syslog(get_class($this)."::remove"); |
||
514 | |||
515 | $this->db->begin(); |
||
0 ignored issues
–
show
|
|||
516 | |||
517 | if (! $error && ! $notrigger) |
||
518 | { |
||
519 | // Call trigger |
||
520 | $result=$this->call_trigger('CATEGORY_DELETE',$user); |
||
521 | if ($result < 0) $error++; |
||
522 | // End call triggers |
||
523 | } |
||
524 | |||
525 | /* FIX #1317 : Check for child category and move up 1 level*/ |
||
526 | if (! $error) |
||
527 | { |
||
528 | $sql = "UPDATE ".MAIN_DB_PREFIX."categorie"; |
||
529 | $sql.= " SET fk_parent = ".$this->fk_parent; |
||
530 | $sql.= " WHERE fk_parent = ".$this->id; |
||
531 | |||
532 | if (!$this->db->query($sql)) |
||
533 | { |
||
534 | $this->error=$this->db->lasterror(); |
||
535 | $error++; |
||
536 | } |
||
537 | } |
||
538 | |||
539 | $arraydelete = array( |
||
540 | 'categorie_societe' => 'fk_categorie', |
||
541 | 'categorie_fournisseur' => 'fk_categorie', |
||
542 | 'categorie_product' => 'fk_categorie', |
||
543 | 'categorie_member' => 'fk_categorie', |
||
544 | 'categorie_contact' => 'fk_categorie', |
||
545 | 'categorie_account' => 'fk_categorie', |
||
546 | 'bank_class' => 'fk_categ', |
||
547 | 'categorie_lang' => 'fk_category', |
||
548 | 'categorie' => 'rowid', |
||
549 | ); |
||
550 | foreach ($arraydelete as $key => $value) { |
||
551 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . $key; |
||
552 | $sql .= " WHERE ".$value." = ".$this->id; |
||
553 | if (!$this->db->query($sql)) { |
||
554 | $this->errors[] = $this->db->lasterror(); |
||
555 | dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR); |
||
556 | $error++; |
||
557 | } |
||
558 | } |
||
559 | |||
560 | // Removed extrafields |
||
561 | if (!$error && empty(Globals::$conf->global->MAIN_EXTRAFIELDS_DISABLED)) { // For avoid conflicts if trigger used{ |
||
562 | $result=$this->deleteExtraFields(); |
||
563 | if ($result < 0) |
||
564 | { |
||
565 | $error++; |
||
566 | dol_syslog(get_class($this)."::delete erreur ".$this->error, LOG_ERR); |
||
567 | } |
||
568 | } |
||
569 | |||
570 | if (! $error) |
||
571 | { |
||
572 | $this->db->commit(); |
||
573 | return 1; |
||
574 | } |
||
575 | else |
||
576 | { |
||
577 | $this->db->rollback(); |
||
578 | return -1; |
||
579 | } |
||
580 | } |
||
581 | |||
582 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
583 | /** |
||
584 | * Link an object to the category |
||
585 | * |
||
586 | * @param CommonObject $obj Object to link to category |
||
587 | * @param string $type Type of category ('product', ...) |
||
588 | * @return int 1 : OK, -1 : erreur SQL, -2 : id not defined, -3 : Already linked |
||
589 | */ |
||
590 | function add_type($obj, $type) |
||
591 | { |
||
592 | // phpcs:enable |
||
593 | global $user,$langs,$conf; |
||
594 | |||
595 | $error=0; |
||
596 | |||
597 | if ($this->id == -1) return -2; |
||
598 | |||
599 | $this->db->begin(); |
||
0 ignored issues
–
show
|
|||
600 | |||
601 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type]; |
||
602 | $sql .= " (fk_categorie, fk_" . $this->MAP_CAT_FK[$type] . ")"; |
||
603 | $sql .= " VALUES (" . $this->id . ", " . $obj->id . ")"; |
||
604 | |||
605 | dol_syslog(get_class($this).'::add_type', LOG_DEBUG); |
||
606 | if ($this->db->query($sql)) |
||
607 | { |
||
608 | if (!empty(Globals::$conf->global->CATEGORIE_RECURSIV_ADD)) { |
||
609 | $sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie'; |
||
610 | $sql.= " WHERE rowid = ".$this->id; |
||
611 | |||
612 | dol_syslog(get_class($this)."::add_type", LOG_DEBUG); |
||
613 | $resql=$this->db->query($sql); |
||
614 | if ($resql) |
||
615 | { |
||
616 | if ($this->db->num_rows($resql) > 0) |
||
617 | { |
||
618 | $objparent = $this->db->fetch_object($resql); |
||
619 | |||
620 | if (!empty($objparent->fk_parent)) |
||
621 | { |
||
622 | $cat = new Categorie($this->db); |
||
623 | $cat->id = $objparent->fk_parent; |
||
624 | if (!$cat->containsObject($type, $obj->id)) { |
||
625 | $result = $cat->add_type($obj, $type); |
||
626 | if ($result < 0) |
||
627 | { |
||
628 | $this->error = $cat->error; |
||
629 | $error++; |
||
630 | } |
||
631 | } |
||
632 | } |
||
633 | } |
||
634 | } |
||
635 | else |
||
636 | { |
||
637 | $error++; |
||
638 | $this->error=$this->db->lasterror(); |
||
639 | } |
||
640 | |||
641 | if ($error) |
||
642 | { |
||
643 | $this->db->rollback(); |
||
644 | return -1; |
||
645 | } |
||
646 | } |
||
647 | |||
648 | |||
649 | |||
650 | // Call trigger |
||
651 | $this->context=array('linkto'=>$obj); // Save object we want to link category to into category instance to provide information to trigger |
||
652 | $result=$this->call_trigger('CATEGORY_LINK',$user); |
||
653 | if ($result < 0) { $error++; } |
||
654 | // End call triggers |
||
655 | |||
656 | if (! $error) |
||
657 | { |
||
658 | $this->db->commit(); |
||
659 | return 1; |
||
660 | } |
||
661 | else |
||
662 | { |
||
663 | $this->db->rollback(); |
||
664 | return -2; |
||
665 | } |
||
666 | } |
||
667 | else |
||
668 | { |
||
669 | $this->db->rollback(); |
||
670 | if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') |
||
671 | { |
||
672 | $this->error=$this->db->lasterrno(); |
||
673 | return -3; |
||
674 | } |
||
675 | else |
||
676 | { |
||
677 | $this->error=$this->db->lasterror(); |
||
678 | } |
||
679 | return -1; |
||
680 | } |
||
681 | } |
||
682 | |||
683 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
684 | /** |
||
685 | * Delete object from category |
||
686 | * |
||
687 | * @param CommonObject $obj Object |
||
688 | * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') |
||
689 | * |
||
690 | * @return int 1 if OK, -1 if KO |
||
691 | */ |
||
692 | function del_type($obj,$type) |
||
693 | { |
||
694 | // phpcs:enable |
||
695 | global $user,$langs,$conf; |
||
696 | |||
697 | $error=0; |
||
698 | |||
699 | // For backward compatibility |
||
700 | if ($type == 'societe') { |
||
701 | $type = 'customer'; |
||
702 | dol_syslog( get_class( $this ) . "::del_type(): type 'societe' is deprecated, please use 'customer' instead", LOG_WARNING); |
||
703 | } elseif ($type == 'fournisseur') { |
||
704 | $type = 'supplier'; |
||
705 | dol_syslog( get_class( $this ) . "::del_type(): type 'fournisseur' is deprecated, please use 'supplier' instead", LOG_WARNING); |
||
706 | } |
||
707 | |||
708 | $this->db->begin(); |
||
0 ignored issues
–
show
|
|||
709 | |||
710 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type]; |
||
711 | $sql .= " WHERE fk_categorie = " . $this->id; |
||
712 | $sql .= " AND fk_" . $this->MAP_CAT_FK[$type] . " = " . $obj->id; |
||
713 | |||
714 | dol_syslog(get_class($this).'::del_type', LOG_DEBUG); |
||
715 | if ($this->db->query($sql)) |
||
716 | { |
||
717 | // Call trigger |
||
718 | $this->context=array('unlinkoff'=>$obj); // Save object we want to link category to into category instance to provide information to trigger |
||
719 | $result=$this->call_trigger('CATEGORY_UNLINK',$user); |
||
720 | if ($result < 0) { $error++; } |
||
721 | // End call triggers |
||
722 | |||
723 | if (! $error) |
||
724 | { |
||
725 | $this->db->commit(); |
||
726 | return 1; |
||
727 | } |
||
728 | else |
||
729 | { |
||
730 | $this->db->rollback(); |
||
731 | return -2; |
||
732 | } |
||
733 | } |
||
734 | else |
||
735 | { |
||
736 | $this->db->rollback(); |
||
737 | $this->error=$this->db->lasterror(); |
||
738 | return -1; |
||
739 | } |
||
740 | } |
||
741 | |||
742 | /** |
||
743 | * Return list of fetched instance of elements having this category |
||
744 | * |
||
745 | * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') |
||
746 | * @param int $onlyids Return only ids of objects (consume less memory) |
||
747 | * @return array|int -1 if KO, array of instance of object if OK |
||
748 | * @see containsObject |
||
749 | */ |
||
750 | function getObjectsInCateg($type, $onlyids=0) |
||
751 | { |
||
752 | $objs = array(); |
||
753 | |||
754 | $obj = new $this->MAP_OBJ_CLASS[$type]( $this->db ); |
||
0 ignored issues
–
show
|
|||
755 | |||
756 | $sql = "SELECT c.fk_" . $this->MAP_CAT_FK[$type]; |
||
757 | $sql .= " FROM " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type] . " as c"; |
||
758 | $sql .= ", " . MAIN_DB_PREFIX . $this->MAP_OBJ_TABLE[$type] . " as o"; |
||
759 | $sql .= " WHERE o.entity IN (" . getEntity( $obj->element).")"; |
||
760 | $sql.= " AND c.fk_categorie = ".$this->id; |
||
761 | $sql .= " AND c.fk_" . $this->MAP_CAT_FK[$type] . " = o.rowid"; |
||
762 | |||
763 | dol_syslog(get_class($this)."::getObjectsInCateg", LOG_DEBUG); |
||
764 | $resql = $this->db->query($sql); |
||
765 | if ($resql) |
||
766 | { |
||
767 | while ($rec = $this->db->fetch_array($resql)) |
||
768 | { |
||
769 | if ($onlyids) |
||
770 | { |
||
771 | $objs[] = $rec['fk_' . $this->MAP_CAT_FK[$type]]; |
||
772 | } |
||
773 | else |
||
774 | { |
||
775 | $obj = new $this->MAP_OBJ_CLASS[$type]( $this->db ); |
||
776 | $obj->fetch( $rec['fk_' . $this->MAP_CAT_FK[$type]]); |
||
777 | $objs[] = $obj; |
||
778 | } |
||
779 | } |
||
780 | return $objs; |
||
781 | } |
||
782 | else |
||
783 | { |
||
784 | $this->error=$this->db->error().' sql='.$sql; |
||
785 | return -1; |
||
786 | } |
||
787 | } |
||
788 | |||
789 | /** |
||
790 | * Check for the presence of an object in a category |
||
791 | * |
||
792 | * @param string $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') |
||
793 | * @param int $object_id Id of the object to search |
||
794 | * @return int Number of occurrences |
||
795 | * @see getObjectsInCateg |
||
796 | */ |
||
797 | function containsObject($type, $object_id) |
||
798 | { |
||
799 | $sql = "SELECT COUNT(*) as nb FROM " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type]; |
||
800 | $sql .= " WHERE fk_categorie = " . $this->id . " AND fk_" . $this->MAP_CAT_FK[$type] . " = " . $object_id; |
||
801 | dol_syslog(get_class($this)."::containsObject", LOG_DEBUG); |
||
802 | $resql = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
803 | if ($resql) { |
||
804 | return $this->db->fetch_object($resql)->nb; |
||
805 | } else { |
||
806 | $this->error=$this->db->error().' sql='.$sql; |
||
807 | return -1; |
||
808 | } |
||
809 | } |
||
810 | |||
811 | /** |
||
812 | * List categories of an element id |
||
813 | * |
||
814 | * @param int $id Id of element |
||
815 | * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact') |
||
816 | * @param string $sortfield Sort field |
||
817 | * @param string $sortorder Sort order |
||
818 | * @param int $limit Limit for list |
||
819 | * @param int $page Page number |
||
820 | * @return array|int Array of categories, 0 if no cat, -1 on error |
||
821 | */ |
||
822 | function getListForItem($id, $type='customer', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) |
||
823 | { |
||
824 | global $conf; |
||
825 | |||
826 | $categories = array(); |
||
827 | |||
828 | $sub_type = $type; |
||
829 | $subcol_name = "fk_".$type; |
||
830 | if ($type=="customer") { |
||
831 | $sub_type="societe"; |
||
832 | $subcol_name="fk_soc"; |
||
833 | } |
||
834 | if ($type=="supplier") { |
||
835 | $sub_type="fournisseur"; |
||
836 | $subcol_name="fk_soc"; |
||
837 | } |
||
838 | if ($type=="contact") { |
||
839 | $subcol_name="fk_socpeople"; |
||
840 | } |
||
841 | $sql = "SELECT s.rowid"; |
||
842 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie as s"; |
||
843 | $sql.= " , ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub "; |
||
844 | $sql.= ' WHERE s.entity IN ('.getEntity('category').')'; |
||
845 | $sql.= ' AND s.type='.array_search($type, self::$MAP_ID_TO_CODE); |
||
846 | $sql.= ' AND s.rowid = sub.fk_categorie'; |
||
847 | $sql.= ' AND sub.'.$subcol_name.' = '.$id; |
||
848 | |||
849 | $sql.= $this->db->order($sortfield, $sortorder); |
||
0 ignored issues
–
show
|
|||
850 | |||
851 | $offset = 0; |
||
852 | $nbtotalofrecords = ''; |
||
853 | if (empty(Globals::$conf->global->MAIN_DISABLE_FULL_SCANLIST)) { |
||
854 | $result = $this->db->query($sql); |
||
855 | $nbtotalofrecords = $this->db->num_rows($result); |
||
856 | if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0 |
||
857 | { |
||
858 | $page = 0; |
||
859 | $offset = 0; |
||
860 | } |
||
861 | } |
||
862 | |||
863 | $sql.= $this->db->plimit($limit + 1, $offset); |
||
864 | |||
865 | $result = $this->db->query($sql); |
||
866 | if ($result) |
||
867 | { |
||
868 | $i=0; |
||
869 | $num = $this->db->num_rows($result); |
||
870 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
871 | while ($i < $min) |
||
872 | { |
||
873 | $obj = $this->db->fetch_object($result); |
||
874 | $category_static = new Categorie($this->db); |
||
875 | if ($category_static->fetch($obj->rowid)) |
||
876 | { |
||
877 | $categories[$i]['id'] = $category_static->id; |
||
878 | $categories[$i]['fk_parent'] = $category_static->fk_parent; |
||
879 | $categories[$i]['label'] = $category_static->label; |
||
880 | $categories[$i]['description'] = $category_static->description; |
||
881 | $categories[$i]['color'] = $category_static->color; |
||
882 | $categories[$i]['socid'] = $category_static->socid; |
||
883 | $categories[$i]['visible'] = $category_static->visible; |
||
884 | $categories[$i]['type'] = $category_static->type; |
||
885 | $categories[$i]['entity'] = $category_static->entity; |
||
886 | $categories[$i]['array_options'] = $category_static->array_options; |
||
887 | |||
888 | // multilangs |
||
889 | if (!empty(Globals::$conf->global->MAIN_MULTILANGS)) { |
||
890 | $categories[$i]['multilangs'] = $category_static->multilangs; |
||
891 | } |
||
892 | } |
||
893 | $i++; |
||
894 | } |
||
895 | } |
||
896 | else { |
||
897 | $this->error = $this->db->lasterror(); |
||
898 | return -1; |
||
899 | } |
||
900 | if ( ! count($categories)) { |
||
901 | return 0; |
||
902 | } |
||
903 | |||
904 | return $categories; |
||
905 | } |
||
906 | |||
907 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
908 | /** |
||
909 | * Return childs of a category |
||
910 | * |
||
911 | * @return array|int <0 KO, array ok |
||
912 | */ |
||
913 | function get_filles() |
||
914 | { |
||
915 | // phpcs:enable |
||
916 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; |
||
917 | $sql.= " WHERE fk_parent = ".$this->id; |
||
918 | |||
919 | $res = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
920 | if ($res) |
||
921 | { |
||
922 | $cats = array (); |
||
923 | while ($rec = $this->db->fetch_array($res)) |
||
924 | { |
||
925 | $cat = new Categorie($this->db); |
||
926 | $cat->fetch($rec['rowid']); |
||
927 | $cats[] = $cat; |
||
928 | } |
||
929 | return $cats; |
||
930 | } |
||
931 | else |
||
932 | { |
||
933 | dol_print_error($this->db); |
||
934 | return -1; |
||
935 | } |
||
936 | } |
||
937 | |||
938 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
939 | /** |
||
940 | * Load this->motherof that is array(id_son=>id_parent, ...) |
||
941 | * |
||
942 | * @return int <0 if KO, >0 if OK |
||
943 | */ |
||
944 | protected function load_motherof() |
||
945 | { |
||
946 | // phpcs:enable |
||
947 | global $conf; |
||
948 | |||
949 | $this->motherof=array(); |
||
950 | |||
951 | // Load array[child]=parent |
||
952 | $sql = "SELECT fk_parent as id_parent, rowid as id_son"; |
||
953 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie"; |
||
954 | $sql.= " WHERE fk_parent != 0"; |
||
955 | $sql.= " AND entity IN (".getEntity('category').")"; |
||
956 | |||
957 | dol_syslog(get_class($this)."::load_motherof", LOG_DEBUG); |
||
958 | $resql = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
959 | if ($resql) |
||
960 | { |
||
961 | while ($obj= $this->db->fetch_object($resql)) |
||
962 | { |
||
963 | $this->motherof[$obj->id_son]=$obj->id_parent; |
||
964 | } |
||
965 | return 1; |
||
966 | } |
||
967 | else |
||
968 | { |
||
969 | dol_print_error($this->db); |
||
970 | return -1; |
||
971 | } |
||
972 | } |
||
973 | |||
974 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
975 | /** |
||
976 | * Rebuilding the category tree as an array |
||
977 | * Return an array of table('id','id_mere',...) trie selon arbre et avec: |
||
978 | * id = id de la categorie |
||
979 | * id_mere = id de la categorie mere |
||
980 | * id_children = tableau des id enfant |
||
981 | * label = nom de la categorie |
||
982 | * fulllabel = nom avec chemin complet de la categorie |
||
983 | * fullpath = chemin complet compose des id |
||
984 | * |
||
985 | * @param string $type Type of categories ('customer', 'supplier', 'contact', 'product', 'member') or (0, 1, 2, ...). |
||
986 | * @param int $markafterid Removed all categories including the leaf $markafterid in category tree. |
||
987 | * |
||
988 | * @return array|int Array of categories. this->cats and this->motherof are set, -1 on error |
||
989 | */ |
||
990 | function get_full_arbo($type, $markafterid=0) |
||
991 | { |
||
992 | // phpcs:enable |
||
993 | global $conf, $langs; |
||
994 | |||
995 | if (! is_numeric($type)) $type = $this->MAP_ID[$type]; |
||
996 | |||
997 | $this->cats = array(); |
||
998 | |||
999 | // Init this->motherof that is array(id_son=>id_parent, ...) |
||
1000 | $this->load_motherof(); |
||
1001 | $current_lang = $langs->getDefaultLang(); |
||
1002 | |||
1003 | // Init $this->cats array |
||
1004 | $sql = "SELECT DISTINCT c.rowid, c.label, c.description, c.color, c.fk_parent, c.visible"; // Distinct reduce pb with old tables with duplicates |
||
1005 | if (!empty(Globals::$conf->global->MAIN_MULTILANGS)) |
||
1006 | $sql .= ", t.label as label_trans, t.description as description_trans"; |
||
1007 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie as c"; |
||
1008 | if (!empty(Globals::$conf->global->MAIN_MULTILANGS)) |
||
1009 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "categorie_lang as t ON t.fk_category=c.rowid AND t.lang='" . $current_lang . "'"; |
||
1010 | $sql .= " WHERE c.entity IN (" . getEntity( 'category') . ")"; |
||
1011 | $sql .= " AND c.type = " . $type; |
||
1012 | |||
1013 | dol_syslog(get_class($this)."::get_full_arbo get category list", LOG_DEBUG); |
||
1014 | $resql = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1015 | if ($resql) |
||
1016 | { |
||
1017 | $i=0; |
||
1018 | while ($obj = $this->db->fetch_object($resql)) |
||
1019 | { |
||
1020 | $this->cats[$obj->rowid]['rowid'] = $obj->rowid; |
||
1021 | $this->cats[$obj->rowid]['id'] = $obj->rowid; |
||
1022 | $this->cats[$obj->rowid]['fk_parent'] = $obj->fk_parent; |
||
1023 | $this->cats[$obj->rowid]['label'] = ! empty($obj->label_trans) ? $obj->label_trans : $obj->label; |
||
1024 | $this->cats[$obj->rowid]['description'] = ! empty($obj->description_trans) ? $obj->description_trans : $obj->description; |
||
1025 | $this->cats[$obj->rowid]['color'] = $obj->color; |
||
1026 | $this->cats[$obj->rowid]['visible'] = $obj->visible; |
||
1027 | $i++; |
||
1028 | } |
||
1029 | } |
||
1030 | else |
||
1031 | { |
||
1032 | dol_print_error($this->db); |
||
1033 | return -1; |
||
1034 | } |
||
1035 | |||
1036 | // We add the fullpath property to each elements of first level (no parent exists) |
||
1037 | dol_syslog(get_class($this)."::get_full_arbo call to build_path_from_id_categ", LOG_DEBUG); |
||
1038 | foreach($this->cats as $key => $val) |
||
1039 | { |
||
1040 | //print 'key='.$key.'<br>'."\n"; |
||
1041 | $this->build_path_from_id_categ($key,0); // Process a branch from the root category key (this category has no parent) |
||
1042 | } |
||
1043 | |||
1044 | // Exclude leaf including $markafterid from tree |
||
1045 | if ($markafterid) |
||
1046 | { |
||
1047 | //print "Look to discard category ".$markafterid."\n"; |
||
1048 | $keyfilter1='^'.$markafterid.'$'; |
||
1049 | $keyfilter2='_'.$markafterid.'$'; |
||
1050 | $keyfilter3='^'.$markafterid.'_'; |
||
1051 | $keyfilter4='_'.$markafterid.'_'; |
||
1052 | foreach($this->cats as $key => $val) |
||
1053 | { |
||
1054 | if (preg_match('/'.$keyfilter1.'/',$val['fullpath']) || preg_match('/'.$keyfilter2.'/',$val['fullpath']) |
||
1055 | || preg_match('/'.$keyfilter3.'/',$val['fullpath']) || preg_match('/'.$keyfilter4.'/',$val['fullpath'])) |
||
1056 | { |
||
1057 | unset($this->cats[$key]); |
||
1058 | } |
||
1059 | } |
||
1060 | } |
||
1061 | |||
1062 | dol_syslog(get_class($this)."::get_full_arbo dol_sort_array", LOG_DEBUG); |
||
1063 | $this->cats=dol_sort_array($this->cats, 'fulllabel', 'asc', true, false); |
||
1064 | |||
1065 | //$this->debug_cats(); |
||
1066 | |||
1067 | return $this->cats; |
||
1068 | } |
||
1069 | |||
1070 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1071 | /** |
||
1072 | * For category id_categ and its childs available in this->cats, define property fullpath and fulllabel |
||
1073 | * |
||
1074 | * @param int $id_categ id_categ entry to update |
||
1075 | * @param int $protection Deep counter to avoid infinite loop |
||
1076 | * @return void |
||
1077 | */ |
||
1078 | function build_path_from_id_categ($id_categ,$protection=1000) |
||
1079 | { |
||
1080 | // phpcs:enable |
||
1081 | dol_syslog(get_class($this)."::build_path_from_id_categ id_categ=".$id_categ." protection=".$protection, LOG_DEBUG); |
||
1082 | |||
1083 | if (! empty($this->cats[$id_categ]['fullpath'])) |
||
1084 | { |
||
1085 | // Already defined |
||
1086 | dol_syslog(get_class($this)."::build_path_from_id_categ fullpath and fulllabel already defined", LOG_WARNING); |
||
1087 | return; |
||
1088 | } |
||
1089 | |||
1090 | // First build full array $motherof |
||
1091 | //$this->load_motherof(); // Disabled because already done by caller of build_path_from_id_categ |
||
1092 | |||
1093 | // Define fullpath and fulllabel |
||
1094 | $this->cats[$id_categ]['fullpath'] = '_'.$id_categ; |
||
1095 | $this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label']; |
||
1096 | $i=0; $cursor_categ=$id_categ; |
||
1097 | //print 'Work for id_categ='.$id_categ.'<br>'."\n"; |
||
1098 | while ((empty($protection) || $i < $protection) && ! empty($this->motherof[$cursor_categ])) |
||
1099 | { |
||
1100 | //print ' cursor_categ='.$cursor_categ.' i='.$i.' '.$this->motherof[$cursor_categ].'<br>'."\n"; |
||
1101 | $this->cats[$id_categ]['fullpath'] = '_'.$this->motherof[$cursor_categ].$this->cats[$id_categ]['fullpath']; |
||
1102 | $this->cats[$id_categ]['fulllabel'] = $this->cats[$this->motherof[$cursor_categ]]['label'].' >> '.$this->cats[$id_categ]['fulllabel']; |
||
1103 | //print ' Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].' '.$this->cats[$id_categ]['fulllabel'].'<br>'."\n"; |
||
1104 | $i++; $cursor_categ=$this->motherof[$cursor_categ]; |
||
1105 | } |
||
1106 | //print 'Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].'<br>'."\n"; |
||
1107 | |||
1108 | // We count number of _ to have level |
||
1109 | $this->cats[$id_categ]['level']=dol_strlen(preg_replace('/[^_]/i','',$this->cats[$id_categ]['fullpath'])); |
||
1110 | |||
1111 | return; |
||
1112 | } |
||
1113 | |||
1114 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1115 | /** |
||
1116 | * Display content of $this->cats |
||
1117 | * |
||
1118 | * @return void |
||
1119 | */ |
||
1120 | function debug_cats() |
||
1121 | { |
||
1122 | // phpcs:enable |
||
1123 | // Display $this->cats |
||
1124 | foreach($this->cats as $key => $val) |
||
1125 | { |
||
1126 | print 'id: '.$this->cats[$key]['id']; |
||
1127 | print ' label: '.$this->cats[$key]['label']; |
||
1128 | print ' mother: '.$this->cats[$key]['fk_parent']; |
||
1129 | //print ' children: '.(is_array($this->cats[$key]['id_children'])?join(',',$this->cats[$key]['id_children']):''); |
||
1130 | print ' fullpath: '.$this->cats[$key]['fullpath']; |
||
1131 | print ' fulllabel: '.$this->cats[$key]['fulllabel']; |
||
1132 | print "<br>\n"; |
||
1133 | } |
||
1134 | } |
||
1135 | |||
1136 | |||
1137 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1138 | /** |
||
1139 | * Returns all categories |
||
1140 | * |
||
1141 | * @param int $type Type of category (0, 1, ...) |
||
1142 | * @param boolean $parent Just parent categories if true |
||
1143 | * @return array|int Table of Object Category, -1 on error |
||
1144 | */ |
||
1145 | function get_all_categories($type=null, $parent=false) |
||
1146 | { |
||
1147 | // phpcs:enable |
||
1148 | if (! is_numeric($type)) $type = $this->MAP_ID[$type]; |
||
1149 | |||
1150 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie"; |
||
1151 | $sql.= " WHERE entity IN (".getEntity('category').")"; |
||
1152 | if (! is_null($type)) |
||
1153 | $sql.= " AND type = ".$type; |
||
1154 | if ($parent) |
||
1155 | $sql.= " AND fk_parent = 0"; |
||
1156 | |||
1157 | $res = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1158 | if ($res) |
||
1159 | { |
||
1160 | $cats = array (); |
||
1161 | while ($rec = $this->db->fetch_array($res)) |
||
1162 | { |
||
1163 | $cat = new Categorie($this->db); |
||
1164 | $cat->fetch($rec['rowid']); |
||
1165 | $cats[$rec['rowid']] = $cat; |
||
1166 | } |
||
1167 | return $cats; |
||
1168 | } |
||
1169 | else |
||
1170 | { |
||
1171 | dol_print_error($this->db); |
||
1172 | return -1; |
||
1173 | } |
||
1174 | } |
||
1175 | |||
1176 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1177 | /** |
||
1178 | * Check if no category with same label already exists for this cat's parent or root and for this cat's type |
||
1179 | * |
||
1180 | * @return integer 1 if already exist, 0 otherwise, -1 if error |
||
1181 | */ |
||
1182 | function already_exists() |
||
1183 | { |
||
1184 | // phpcs:enable |
||
1185 | $type=$this->type; |
||
1186 | |||
1187 | if (! is_numeric($type)) $type=$this->MAP_ID[$type]; |
||
1188 | |||
1189 | /* We have to select any rowid from llx_categorie which category's mother and label |
||
1190 | * are equals to those of the calling category |
||
1191 | */ |
||
1192 | $sql = "SELECT c.rowid"; |
||
1193 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie as c "; |
||
1194 | $sql.= " WHERE c.entity IN (".getEntity('category').")"; |
||
1195 | $sql.= " AND c.type = ".$type; |
||
1196 | $sql.= " AND c.fk_parent = ".$this->fk_parent; |
||
1197 | $sql.= " AND c.label = '".$this->db->escape($this->label)."'"; |
||
0 ignored issues
–
show
|
|||
1198 | |||
1199 | dol_syslog(get_class($this)."::already_exists", LOG_DEBUG); |
||
1200 | $resql = $this->db->query($sql); |
||
1201 | if ($resql) |
||
1202 | { |
||
1203 | if ($this->db->num_rows($resql) > 0) // Checking for empty resql |
||
1204 | { |
||
1205 | $obj = $this->db->fetch_array($resql); |
||
1206 | /* If object called create, obj cannot have is id. |
||
1207 | * If object called update, he mustn't have the same label as an other category for this mother. |
||
1208 | * So if the result have the same id, update is not for label, and if result have an other one, |
||
1209 | * update may be for label. |
||
1210 | */ |
||
1211 | if($obj[0] > 0 && $obj[0] != $this->id) |
||
1212 | { |
||
1213 | dol_syslog(get_class($this)."::already_exists category with name=".$this->label." and parent ".$this->fk_parent." exists: rowid=".$obj[0]." current_id=".$this->id, LOG_DEBUG); |
||
1214 | return 1; |
||
1215 | } |
||
1216 | } |
||
1217 | 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); |
||
1218 | return 0; |
||
1219 | } |
||
1220 | else |
||
1221 | { |
||
1222 | $this->error=$this->db->error(); |
||
1223 | return -1; |
||
1224 | } |
||
1225 | } |
||
1226 | |||
1227 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1228 | /** |
||
1229 | * Returns the top level categories (which are not girls) |
||
1230 | * |
||
1231 | * @param int $type Type of category (0, 1, ...) |
||
1232 | * @return array |
||
1233 | */ |
||
1234 | function get_main_categories($type=null) |
||
1235 | { |
||
1236 | // phpcs:enable |
||
1237 | return $this->get_all_categories($type, true); |
||
1238 | } |
||
1239 | |||
1240 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1241 | /** |
||
1242 | * Returns the path of the category, with the names of the categories |
||
1243 | * separated by $sep (" >> " by default) |
||
1244 | * |
||
1245 | * @param string $sep Separator |
||
1246 | * @param string $url Url |
||
1247 | * @param int $nocolor 0 |
||
1248 | * @return array |
||
1249 | */ |
||
1250 | function print_all_ways($sep = " >> ", $url='', $nocolor=0) |
||
1251 | { |
||
1252 | // phpcs:enable |
||
1253 | $ways = array(); |
||
1254 | |||
1255 | $allways = $this->get_all_ways(); // Load array of categories |
||
1256 | foreach ($allways as $way) |
||
1257 | { |
||
1258 | $w = array(); |
||
1259 | $i = 0; |
||
1260 | $forced_color=''; |
||
1261 | foreach ($way as $cat) |
||
1262 | { |
||
1263 | $i++; |
||
1264 | |||
1265 | if (empty($nocolor)) |
||
1266 | { |
||
1267 | $forced_color='toreplace'; |
||
1268 | if ($i == count($way)) |
||
1269 | { |
||
1270 | // Check contrast with background and correct text color |
||
1271 | $forced_color='categtextwhite'; |
||
1272 | if ($cat->color) |
||
1273 | { |
||
1274 | if (colorIsLight($cat->color)) $forced_color='categtextblack'; |
||
1275 | } |
||
1276 | } |
||
1277 | } |
||
1278 | |||
1279 | if ($url == '') |
||
1280 | { |
||
1281 | $link = '<a href="'.DOL_URL_ROOT.'/categories/viewcat.php?id='.$cat->id.'&type='.$cat->type.'" class="'.$forced_color .'">'; |
||
1282 | $linkend='</a>'; |
||
1283 | $w[] = $link.$cat->label.$linkend; |
||
1284 | } |
||
1285 | else |
||
1286 | { |
||
1287 | $w[] = "<a href='".DOL_URL_ROOT."/$url?catid=".$cat->id."'>".$cat->label."</a>"; |
||
1288 | } |
||
1289 | } |
||
1290 | $newcategwithpath = preg_replace('/toreplace/', $forced_color, implode($sep, $w)); |
||
1291 | |||
1292 | $ways[] = $newcategwithpath; |
||
1293 | } |
||
1294 | |||
1295 | return $ways; |
||
1296 | } |
||
1297 | |||
1298 | |||
1299 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1300 | /** |
||
1301 | * Returns an array containing the list of parent categories |
||
1302 | * |
||
1303 | * @return int|array <0 KO, array OK |
||
1304 | */ |
||
1305 | function get_meres() |
||
1306 | { |
||
1307 | // phpcs:enable |
||
1308 | $parents = array(); |
||
1309 | |||
1310 | $sql = "SELECT fk_parent FROM ".MAIN_DB_PREFIX."categorie"; |
||
1311 | $sql.= " WHERE rowid = ".$this->id; |
||
1312 | |||
1313 | $res = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1314 | |||
1315 | if ($res) |
||
1316 | { |
||
1317 | while ($rec = $this->db->fetch_array($res)) |
||
1318 | { |
||
1319 | if ($rec['fk_parent'] > 0) |
||
1320 | { |
||
1321 | $cat = new Categorie($this->db); |
||
1322 | $cat->fetch($rec['fk_parent']); |
||
1323 | $parents[] = $cat; |
||
1324 | } |
||
1325 | } |
||
1326 | return $parents; |
||
1327 | } |
||
1328 | else |
||
1329 | { |
||
1330 | dol_print_error($this->db); |
||
1331 | return -1; |
||
1332 | } |
||
1333 | } |
||
1334 | |||
1335 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1336 | /** |
||
1337 | * Returns in a table all possible paths to get to the category |
||
1338 | * starting with the major categories represented by Tables of categories |
||
1339 | * |
||
1340 | * @return array |
||
1341 | */ |
||
1342 | function get_all_ways() |
||
1343 | { |
||
1344 | // phpcs:enable |
||
1345 | $ways = array(); |
||
1346 | |||
1347 | $parents=$this->get_meres(); |
||
1348 | if (! empty($parents)) |
||
1349 | { |
||
1350 | foreach ($parents as $parent) |
||
1351 | { |
||
1352 | $allways=$parent->get_all_ways(); |
||
1353 | foreach ($allways as $way) |
||
1354 | { |
||
1355 | $w = $way; |
||
1356 | $w[] = $this; |
||
1357 | $ways[] = $w; |
||
1358 | } |
||
1359 | } |
||
1360 | } |
||
1361 | |||
1362 | if (count($ways) == 0) |
||
1363 | $ways[0][0] = $this; |
||
1364 | |||
1365 | return $ways; |
||
1366 | } |
||
1367 | |||
1368 | /** |
||
1369 | * Return list of categories (object instances or labels) linked to element of id $id and type $type |
||
1370 | * Should be named getListOfCategForObject |
||
1371 | * |
||
1372 | * @param int $id Id of element |
||
1373 | * @param string|int $type Type of category ('customer', 'supplier', 'contact', 'product', 'member') or (0, 1, 2, ...) |
||
1374 | * @param string $mode 'id'=Get array of category ids, 'object'=Get array of fetched category instances, 'label'=Get array of category |
||
1375 | * labels, 'id'= Get array of category IDs |
||
1376 | * @return array|int Array of category objects or < 0 if KO |
||
1377 | */ |
||
1378 | function containing($id, $type, $mode='object') |
||
1379 | { |
||
1380 | $cats = array(); |
||
1381 | |||
1382 | if (is_numeric($type)) $type = Categorie::$MAP_ID_TO_CODE[$type]; |
||
1383 | |||
1384 | if ($type === Categorie::TYPE_BANK_LINE) // TODO Remove this with standard category code |
||
1385 | { |
||
1386 | // Load bank groups |
||
1387 | $sql = "SELECT c.label, c.rowid"; |
||
1388 | $sql.= " FROM ".MAIN_DB_PREFIX."bank_class as a, ".MAIN_DB_PREFIX."bank_categ as c"; |
||
1389 | $sql.= " WHERE a.lineid=".$id." AND a.fk_categ = c.rowid"; |
||
1390 | $sql.= " ORDER BY c.label"; |
||
1391 | |||
1392 | $res = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1393 | if ($res) |
||
1394 | { |
||
1395 | while ($obj = $this->db->fetch_object($res)) |
||
1396 | { |
||
1397 | if ($mode == 'id') { |
||
1398 | $cats[] = $obj->rowid; |
||
1399 | } else if ($mode == 'label') { |
||
1400 | $cats[] = $obj->label; |
||
1401 | } else { |
||
1402 | $cat = new Categorie($this->db); |
||
1403 | $cat->id = $obj->rowid; |
||
1404 | $cat->label = $obj->label; |
||
1405 | $cats[] = $cat; |
||
1406 | } |
||
1407 | } |
||
1408 | } |
||
1409 | else |
||
1410 | { |
||
1411 | dol_print_error($this->db); |
||
1412 | return -1; |
||
1413 | } |
||
1414 | } |
||
1415 | else |
||
1416 | { |
||
1417 | $sql = "SELECT ct.fk_categorie, c.label, c.rowid"; |
||
1418 | $sql .= " FROM " . MAIN_DB_PREFIX . "categorie_" . $this->MAP_CAT_TABLE[$type] . " as ct, " . MAIN_DB_PREFIX . "categorie as c"; |
||
1419 | $sql .= " WHERE ct.fk_categorie = c.rowid AND ct.fk_" . $this->MAP_CAT_FK[$type] . " = " . (int) $id . " AND c.type = " . $this->MAP_ID[$type]; |
||
1420 | $sql .= " AND c.entity IN (" . getEntity( 'category') . ")"; |
||
1421 | |||
1422 | $res = $this->db->query($sql); |
||
1423 | if ($res) |
||
1424 | { |
||
1425 | while ($obj = $this->db->fetch_object($res)) |
||
1426 | { |
||
1427 | if ($mode == 'id') { |
||
1428 | $cats[] = $obj->rowid; |
||
1429 | } else if ($mode == 'label') { |
||
1430 | $cats[] = $obj->label; |
||
1431 | } else { |
||
1432 | $cat = new Categorie($this->db); |
||
1433 | $cat->fetch($obj->fk_categorie); |
||
1434 | $cats[] = $cat; |
||
1435 | } |
||
1436 | } |
||
1437 | } |
||
1438 | else |
||
1439 | { |
||
1440 | dol_print_error($this->db); |
||
1441 | return -1; |
||
1442 | } |
||
1443 | } |
||
1444 | |||
1445 | return $cats; |
||
1446 | } |
||
1447 | |||
1448 | |||
1449 | /** |
||
1450 | * Returns categories whose id or name match |
||
1451 | * add wildcards in the name unless $exact = true |
||
1452 | * |
||
1453 | * @param int $id Id |
||
1454 | * @param string $nom Name |
||
1455 | * @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact'). Old mode (0, 1, 2, ...) is deprecated. |
||
1456 | * @param boolean $exact Exact string search (true/false) |
||
1457 | * @param boolean $case Case sensitive (true/false) |
||
1458 | * @return array|int Array of category id, -1 if error |
||
1459 | */ |
||
1460 | function rechercher($id, $nom, $type, $exact = false, $case = false) |
||
1461 | { |
||
1462 | // Deprecation warning |
||
1463 | if (is_numeric($type)) { |
||
1464 | dol_syslog(__METHOD__ . ': using numeric types is deprecated.', LOG_WARNING); |
||
1465 | } |
||
1466 | |||
1467 | $cats = array(); |
||
1468 | |||
1469 | // For backward compatibility |
||
1470 | if (is_numeric($type)) { |
||
1471 | // We want to reverse lookup |
||
1472 | $map_type = array_flip( $this->MAP_ID ); |
||
1473 | $type = $map_type[$type]; |
||
1474 | dol_syslog( get_class( $this ) . "::rechercher(): numeric types are deprecated, please use string instead", |
||
1475 | LOG_WARNING ); |
||
1476 | } |
||
1477 | |||
1478 | // Generation requete recherche |
||
1479 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "categorie"; |
||
1480 | $sql .= " WHERE type = " . $this->MAP_ID[$type]; |
||
1481 | $sql .= " AND entity IN (" . getEntity( 'category') . ")"; |
||
1482 | if ($nom) |
||
1483 | { |
||
1484 | if (! $exact) |
||
1485 | $nom = '%'.str_replace('*', '%', $nom).'%'; |
||
1486 | if (! $case) |
||
1487 | $sql.= " AND label LIKE '".$this->db->escape($nom)."'"; |
||
0 ignored issues
–
show
|
|||
1488 | else |
||
1489 | $sql.= " AND label LIKE BINARY '".$this->db->escape($nom)."'"; |
||
1490 | } |
||
1491 | if ($id) |
||
1492 | { |
||
1493 | $sql.=" AND rowid = '".$id."'"; |
||
1494 | } |
||
1495 | |||
1496 | $res = $this->db->query($sql); |
||
1497 | if ($res) |
||
1498 | { |
||
1499 | while ($rec = $this->db->fetch_array($res)) |
||
1500 | { |
||
1501 | $cat = new Categorie($this->db); |
||
1502 | $cat->fetch($rec['rowid']); |
||
1503 | $cats[] = $cat; |
||
1504 | } |
||
1505 | |||
1506 | return $cats; |
||
1507 | } |
||
1508 | else |
||
1509 | { |
||
1510 | $this->error=$this->db->error().' sql='.$sql; |
||
1511 | return -1; |
||
1512 | } |
||
1513 | } |
||
1514 | |||
1515 | /** |
||
1516 | * Return name and link of category (with picto) |
||
1517 | * Use ->id, ->ref, ->label, ->color |
||
1518 | * |
||
1519 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
1520 | * @param string $option Sur quoi pointe le lien ('', 'xyz') |
||
1521 | * @param int $maxlength Max length of text |
||
1522 | * @return string Chaine avec URL |
||
1523 | */ |
||
1524 | function getNomUrl($withpicto=0,$option='',$maxlength=0) |
||
1525 | { |
||
1526 | global $langs; |
||
1527 | |||
1528 | $result=''; |
||
1529 | $label=$langs->trans("ShowCategory").': '. ($this->ref?$this->ref:$this->label); |
||
1530 | |||
1531 | // Check contrast with background and correct text color |
||
1532 | $forced_color='categtextwhite'; |
||
1533 | if ($this->color) |
||
1534 | { |
||
1535 | if (colorIsLight($this->color)) $forced_color='categtextblack'; |
||
1536 | } |
||
1537 | |||
1538 | $link = '<a href="'.DOL_URL_ROOT.'/categories/viewcat.php?id='.$this->id.'&type='.$this->type.'&backtopage='.urlencode($_SERVER['PHP_SELF']).'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip '.$forced_color .'">'; |
||
1539 | $linkend='</a>'; |
||
1540 | |||
1541 | $picto='category'; |
||
1542 | |||
1543 | |||
1544 | if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend); |
||
1545 | if ($withpicto && $withpicto != 2) $result.=' '; |
||
1546 | if ($withpicto != 2) $result.=$link.dol_trunc(($this->ref?$this->ref:$this->label),$maxlength).$linkend; |
||
1547 | return $result; |
||
1548 | } |
||
1549 | |||
1550 | |||
1551 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1552 | /** |
||
1553 | * Deplace fichier uploade sous le nom $files dans le repertoire sdir |
||
1554 | * |
||
1555 | * @param string $sdir Repertoire destination finale |
||
1556 | * @param string $file Nom du fichier uploade |
||
1557 | * @return void |
||
1558 | */ |
||
1559 | function add_photo($sdir, $file) |
||
1560 | { |
||
1561 | // phpcs:enable |
||
1562 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
1563 | |||
1564 | $dir = $sdir .'/'. get_exdir($this->id,2,0,0,$this,'category') . $this->id ."/"; |
||
1565 | $dir .= "photos/"; |
||
1566 | |||
1567 | if (! file_exists($dir)) |
||
1568 | { |
||
1569 | dol_mkdir($dir); |
||
1570 | } |
||
1571 | |||
1572 | if (file_exists($dir)) { |
||
1573 | if (is_array($file['name']) && count($file['name']) > 0) |
||
1574 | { |
||
1575 | $nbfile = count($file['name']); |
||
1576 | for ($i = 0; $i <= $nbfile; $i ++) { |
||
1577 | |||
1578 | $originImage = $dir . $file['name'][$i]; |
||
1579 | |||
1580 | // Cree fichier en taille origine |
||
1581 | dol_move_uploaded_file($file['tmp_name'][$i], $originImage, 1, 0, 0); |
||
1582 | |||
1583 | if (file_exists($originImage)) { |
||
1584 | // Create thumbs |
||
1585 | $this->addThumbs($originImage); |
||
1586 | } |
||
1587 | } |
||
1588 | } else { |
||
1589 | $originImage = $dir . $file['name']; |
||
1590 | |||
1591 | // Cree fichier en taille origine |
||
1592 | dol_move_uploaded_file($file['tmp_name'], $originImage, 1, 0, 0); |
||
1593 | |||
1594 | if (file_exists($originImage)) { |
||
1595 | // Create thumbs |
||
1596 | $this->addThumbs($originImage); |
||
1597 | } |
||
1598 | } |
||
1599 | } |
||
1600 | } |
||
1601 | |||
1602 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1603 | /** |
||
1604 | * Return tableau de toutes les photos de la categorie |
||
1605 | * |
||
1606 | * @param string $dir Repertoire a scanner |
||
1607 | * @param int $nbmax Nombre maximum de photos (0=pas de max) |
||
1608 | * @return array Tableau de photos |
||
1609 | */ |
||
1610 | function liste_photos($dir,$nbmax=0) |
||
1611 | { |
||
1612 | // phpcs:enable |
||
1613 | include_once DOL_DOCUMENT_ROOT .'/core/lib/files.lib.php'; |
||
1614 | |||
1615 | $nbphoto=0; |
||
1616 | $tabobj=array(); |
||
1617 | |||
1618 | $dirthumb = $dir.'thumbs/'; |
||
1619 | |||
1620 | if (file_exists($dir)) |
||
1621 | { |
||
1622 | $handle=opendir($dir); |
||
1623 | if (is_resource($handle)) |
||
1624 | { |
||
1625 | while (($file = readdir($handle)) !== false) |
||
1626 | { |
||
1627 | if (dol_is_file($dir.$file) && preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i',$dir.$file)) |
||
1628 | { |
||
1629 | $nbphoto++; |
||
1630 | $photo = $file; |
||
1631 | |||
1632 | // On determine nom du fichier vignette |
||
1633 | $photo_vignette=''; |
||
1634 | if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i',$photo,$regs)) |
||
1635 | { |
||
1636 | $photo_vignette=preg_replace('/'.$regs[0].'/i','',$photo).'_small'.$regs[0]; |
||
1637 | } |
||
1638 | |||
1639 | // Objet |
||
1640 | $obj=array(); |
||
1641 | $obj['photo']=$photo; |
||
1642 | if ($photo_vignette && is_file($dirthumb.$photo_vignette)) $obj['photo_vignette']='thumbs/' . $photo_vignette; |
||
1643 | else $obj['photo_vignette']=""; |
||
1644 | |||
1645 | $tabobj[$nbphoto-1]=$obj; |
||
1646 | |||
1647 | // On continue ou on arrete de boucler |
||
1648 | if ($nbmax && $nbphoto >= $nbmax) break; |
||
1649 | } |
||
1650 | } |
||
1651 | |||
1652 | closedir($handle); |
||
1653 | } |
||
1654 | } |
||
1655 | |||
1656 | return $tabobj; |
||
1657 | } |
||
1658 | |||
1659 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1660 | /** |
||
1661 | * Efface la photo de la categorie et sa vignette |
||
1662 | * |
||
1663 | * @param string $file Path to file |
||
1664 | * @return void |
||
1665 | */ |
||
1666 | function delete_photo($file) |
||
1667 | { |
||
1668 | // phpcs:enable |
||
1669 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
1670 | |||
1671 | $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine |
||
1672 | $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette |
||
1673 | $filename = preg_replace('/'.preg_quote($dir,'/').'/i','',$file); // Nom du fichier |
||
1674 | |||
1675 | // On efface l'image d'origine |
||
1676 | dol_delete_file($file,1); |
||
1677 | |||
1678 | // Si elle existe, on efface la vignette |
||
1679 | if (preg_match('/(\.jpeg|\.jpg|\.bmp|\.gif|\.png|\.tiff)$/i',$filename,$regs)) |
||
1680 | { |
||
1681 | $photo_vignette=preg_replace('/'.$regs[0].'/i','',$filename).'_small'.$regs[0]; |
||
1682 | if (file_exists($dirthumb.$photo_vignette)) |
||
1683 | { |
||
1684 | dol_delete_file($dirthumb.$photo_vignette,1); |
||
1685 | } |
||
1686 | } |
||
1687 | } |
||
1688 | |||
1689 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1690 | /** |
||
1691 | * Load size of image file |
||
1692 | * |
||
1693 | * @param string $file Path to file |
||
1694 | * @return void |
||
1695 | */ |
||
1696 | function get_image_size($file) |
||
1697 | { |
||
1698 | // phpcs:enable |
||
1699 | $infoImg = getimagesize($file); // Recuperation des infos de l'image |
||
1700 | $this->imgWidth = $infoImg[0]; // Largeur de l'image |
||
1701 | $this->imgHeight = $infoImg[1]; // Hauteur de l'image |
||
1702 | } |
||
1703 | |||
1704 | /** |
||
1705 | * Update ou cree les traductions des infos produits |
||
1706 | * |
||
1707 | * @param User $user Object user |
||
1708 | * |
||
1709 | * @return int <0 if KO, >0 if OK |
||
1710 | */ |
||
1711 | function setMultiLangs($user) |
||
1712 | { |
||
1713 | global $langs; |
||
1714 | |||
1715 | $langs_available = $langs->get_available_languages(); |
||
1716 | $current_lang = $langs->getDefaultLang(); |
||
1717 | |||
1718 | foreach ($langs_available as $key => $value) |
||
1719 | { |
||
1720 | $sql = "SELECT rowid"; |
||
1721 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie_lang"; |
||
1722 | $sql.= " WHERE fk_category=".$this->id; |
||
1723 | $sql.= " AND lang='".$key."'"; |
||
1724 | |||
1725 | $result = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1726 | |||
1727 | if ($key == $current_lang) |
||
1728 | { |
||
1729 | if ($this->db->num_rows($result)) // si aucune ligne dans la base |
||
1730 | { |
||
1731 | $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang"; |
||
1732 | $sql2.= " SET label='".$this->db->escape($this->label)."',"; |
||
1733 | $sql2.= " description='".$this->db->escape($this->description)."'"; |
||
1734 | $sql2.= " WHERE fk_category=".$this->id." AND lang='".$this->db->escape($key)."'"; |
||
1735 | } |
||
1736 | else |
||
1737 | { |
||
1738 | $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)"; |
||
1739 | $sql2.= " VALUES(".$this->id.",'".$key."','". $this->db->escape($this->label); |
||
1740 | $sql2.= "','".$this->db->escape($this->multilangs["$key"]["description"])."')"; |
||
1741 | } |
||
1742 | dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG); |
||
1743 | if (! $this->db->query($sql2)) |
||
1744 | { |
||
1745 | $this->error=$this->db->lasterror(); |
||
1746 | return -1; |
||
1747 | } |
||
1748 | } |
||
1749 | else if (isset($this->multilangs["$key"])) |
||
1750 | { |
||
1751 | if ($this->db->num_rows($result)) // si aucune ligne dans la base |
||
1752 | { |
||
1753 | $sql2 = "UPDATE ".MAIN_DB_PREFIX."categorie_lang"; |
||
1754 | $sql2.= " SET label='".$this->db->escape($this->multilangs["$key"]["label"])."',"; |
||
1755 | $sql2.= " description='".$this->db->escape($this->multilangs["$key"]["description"])."'"; |
||
1756 | $sql2.= " WHERE fk_category=".$this->id." AND lang='".$this->db->escape($key)."'"; |
||
1757 | } |
||
1758 | else |
||
1759 | { |
||
1760 | $sql2 = "INSERT INTO ".MAIN_DB_PREFIX."categorie_lang (fk_category, lang, label, description)"; |
||
1761 | $sql2.= " VALUES(".$this->id.",'".$key."','". $this->db->escape($this->multilangs["$key"]["label"]); |
||
1762 | $sql2.= "','".$this->db->escape($this->multilangs["$key"]["description"])."')"; |
||
1763 | } |
||
1764 | |||
1765 | // on ne sauvegarde pas des champs vides |
||
1766 | if ( $this->multilangs["$key"]["label"] || $this->multilangs["$key"]["description"] || $this->multilangs["$key"]["note"] ) |
||
1767 | dol_syslog(get_class($this).'::setMultiLangs', LOG_DEBUG); |
||
1768 | if (! $this->db->query($sql2)) |
||
1769 | { |
||
1770 | $this->error=$this->db->lasterror(); |
||
1771 | return -1; |
||
1772 | } |
||
1773 | } |
||
1774 | } |
||
1775 | |||
1776 | // Call trigger |
||
1777 | $result = $this->call_trigger('CATEGORY_SET_MULTILANGS',$user); |
||
1778 | if ($result < 0) { |
||
1779 | $this->error = $this->db->lasterror(); |
||
1780 | return -1; |
||
1781 | } |
||
1782 | // End call triggers |
||
1783 | |||
1784 | return 1; |
||
1785 | } |
||
1786 | |||
1787 | /** |
||
1788 | * Load array this->multilangs |
||
1789 | * |
||
1790 | * @return int <0 if KO, >0 if OK |
||
1791 | */ |
||
1792 | function getMultiLangs() |
||
1793 | { |
||
1794 | global $langs; |
||
1795 | |||
1796 | $current_lang = $langs->getDefaultLang(); |
||
1797 | |||
1798 | $sql = "SELECT lang, label, description"; |
||
1799 | $sql.= " FROM ".MAIN_DB_PREFIX."categorie_lang"; |
||
1800 | $sql.= " WHERE fk_category=".$this->id; |
||
1801 | |||
1802 | $result = $this->db->query($sql); |
||
0 ignored issues
–
show
|
|||
1803 | if ($result) |
||
1804 | { |
||
1805 | while ( $obj = $this->db->fetch_object($result) ) |
||
1806 | { |
||
1807 | //print 'lang='.$obj->lang.' current='.$current_lang.'<br>'; |
||
1808 | if( $obj->lang == $current_lang ) // si on a les traduct. dans la langue courante on les charge en infos principales. |
||
1809 | { |
||
1810 | $this->label = $obj->label; |
||
1811 | $this->description = $obj->description; |
||
1812 | } |
||
1813 | $this->multilangs["$obj->lang"]["label"] = $obj->label; |
||
1814 | $this->multilangs["$obj->lang"]["description"] = $obj->description; |
||
1815 | } |
||
1816 | return 1; |
||
1817 | } |
||
1818 | else |
||
1819 | { |
||
1820 | $this->error=$langs->trans("Error")." : ".$this->db->error()." - ".$sql; |
||
1821 | return -1; |
||
1822 | } |
||
1823 | } |
||
1824 | |||
1825 | /** |
||
1826 | * Return label of contact status |
||
1827 | * |
||
1828 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
1829 | * @return string Label of contact status |
||
1830 | */ |
||
1831 | function getLibStatut($mode) |
||
1832 | { |
||
1833 | return ''; |
||
1834 | } |
||
1835 | |||
1836 | |||
1837 | /** |
||
1838 | * Initialise an instance with random values. |
||
1839 | * Used to build previews or test instances. |
||
1840 | * id must be 0 if object instance is a specimen. |
||
1841 | * |
||
1842 | * @return void |
||
1843 | */ |
||
1844 | function initAsSpecimen() |
||
1845 | { |
||
1846 | dol_syslog(get_class($this)."::initAsSpecimen"); |
||
1847 | |||
1848 | // Initialise parametres |
||
1849 | $this->id=0; |
||
1850 | $this->fk_parent=0; |
||
1851 | $this->label = 'SPECIMEN'; |
||
1852 | $this->specimen=1; |
||
1853 | $this->description = 'This is a description'; |
||
1854 | $this->socid = 1; |
||
1855 | $this->type = self::TYPE_PRODUCT; |
||
1856 | } |
||
1857 | |||
1858 | /** |
||
1859 | * Function used to replace a thirdparty id with another one. |
||
1860 | * |
||
1861 | * @param DoliDB $db Database handler |
||
1862 | * @param int $origin_id Old thirdparty id |
||
1863 | * @param int $dest_id New thirdparty id |
||
1864 | * @return bool |
||
1865 | */ |
||
1866 | public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id) |
||
1867 | { |
||
1868 | $tables = array( |
||
1869 | 'categorie_societe' |
||
1870 | ); |
||
1871 | |||
1872 | return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables, 1); |
||
1873 | } |
||
1874 | } |
||
1875 |