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