/
categorie.class.php
1549 lines (1375 loc) · 44.2 KB
/
categorie.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2005 Matthieu Valleton <mv@seeschloss.org>
* Copyright (C) 2005 Davoleau Brice <brice.davoleau@gmail.com>
* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2007 Patrick Raguin <patrick.raguin@gmail.com>
* Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2013 Philippe Grand <philippe.grand@atoo-net.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/categories/class/categorie.class.php
* \ingroup categorie
* \brief File of class to manage categories
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
/**
* Class to manage categories
*/
class Categorie extends CommonObject
{
public $element='category';
public $table_element='categories';
var $id;
var $fk_parent;
var $label;
var $description;
var $socid;
var $type; // 0=Product, 1=Supplier, 2=Customer/Prospect, 3=Member, 4=Contact
var $import_key;
var $cats=array(); // Tableau en memoire des categories
var $motherof=array();
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
function __construct($db)
{
$this->db = $db;
}
/**
* Load category into memory from database
*
* @param int $id Id of category
* @param string $label Label of category
* @return int <0 if KO, >0 if OK
*/
function fetch($id,$label='')
{
global $conf;
$sql = "SELECT rowid, fk_parent, entity, label, description, fk_soc, visible, type";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie";
if ($id)
{
$sql.= " WHERE rowid = '".$id."'";
}
else
{
if ($label) $sql.= " WHERE label = '".$this->db->escape($label)."' AND entity IN (".getEntity('category',1).")";
}
dol_syslog(get_class($this)."::fetch sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql) > 0)
{
$res = $this->db->fetch_array($resql);
$this->id = $res['rowid'];
$this->fk_parent = $res['fk_parent'];
$this->label = $res['label'];
$this->description = $res['description'];
$this->socid = $res['fk_soc'];
$this->visible = $res['visible'];
$this->type = $res['type'];
$this->entity = $res['entity'];
$this->fetch_optionals($this->id,$extralabels);
$this->db->free($resql);
// multilangs
if (! empty($conf->global->MAIN_MULTILANGS)) $this->getMultiLangs();
return 1;
}
else
{
return 0;
}
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Add category into database
*
* @param User $user Object user
* @return int -1 : erreur SQL
* -2 : nouvel ID inconnu
* -3 : categorie invalide
* -4 : category already exists
*/
function create($user='')
{
global $conf,$langs,$hookmanager;
$langs->load('categories');
$error=0;
// Clean parameters
$this->label = trim($this->label);
$this->description = trim($this->description);
$this->import_key = trim($this->import_key);
if (empty($this->visible)) $this->visible=0;
$this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
if ($this->already_exists())
{
$this->error=$langs->trans("ImpossibleAddCat");
$this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel");
dol_syslog($this->error, LOG_ERR);
return -4;
}
$this->db->begin();
dol_syslog(get_class($this).'::create sql='.$sql);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie (";
$sql.= "fk_parent,";
$sql.= " label,";
$sql.= " description,";
if (! empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER))
{
$sql.= "fk_soc,";
}
$sql.= " visible,";
$sql.= " type,";
$sql.= " import_key,";
$sql.= " entity";
$sql.= ") VALUES (";
$sql.= $this->fk_parent.",";
$sql.= "'".$this->db->escape($this->label)."',";
$sql.= "'".$this->db->escape($this->description)."',";
if (! empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER))
{
$sql.= ($this->socid != -1 ? $this->socid : 'null').",";
}
$sql.= "'".$this->visible."',";
$sql.= $this->type.",";
$sql.= (! empty($this->import_key)?"'".$this->db->escape($this->import_key)."'":'null').",";
$sql.= $conf->entity;
$sql.= ")";
dol_syslog(get_class($this).'::create sql='.$sql);
$res = $this->db->query($sql);
if ($res)
{
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."categorie");
if ($id > 0)
{
$this->id = $id;
// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('HookModuleNamedao'));
$parameters=array('socid'=>$this->id);
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CATEGORY_CREATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$this->db->commit();
return $id;
}
else
{
$this->db->rollback();
return -2;
}
}
else
{
$this->error=$this->db->error();
dol_syslog(get_class($this)."::create error ".$this->error." sql=".$sql, LOG_ERR);
$this->db->rollback();
return -1;
}
}
/**
* Update category
*
* @param User $user Object user
* @return int 1 : OK
* -1 : SQL error
* -2 : invalid category
*/
function update($user='')
{
global $conf, $langs,$hookmanager;
$error=0;
// Clean parameters
$this->label=trim($this->label);
$this->description=trim($this->description);
$this->fk_parent = ($this->fk_parent != "" ? intval($this->fk_parent) : 0);
$this->visible = ($this->visible != "" ? intval($this->visible) : 0);
if ($this->already_exists())
{
$this->error=$langs->trans("ImpossibleUpdateCat");
$this->error.=" : ".$langs->trans("CategoryExistsAtSameLevel");
return -1;
}
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
$sql.= " SET label = '".$this->db->escape($this->label)."',";
$sql.= " description = '".$this->db->escape($this->description)."'";
if (! empty($conf->global->CATEGORY_ASSIGNED_TO_A_CUSTOMER))
{
$sql .= ", fk_soc = ".($this->socid != -1 ? $this->socid : 'null');
}
$sql .= ", visible = '".$this->visible."'";
$sql .= ", fk_parent = ".$this->fk_parent;
$sql .= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::update sql=".$sql);
if ($this->db->query($sql))
{
// Actions on extra fields (by external module or standard code)
// FIXME le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('HookCategorydao'));
$parameters=array();
$reshook=$hookmanager->executeHooks('insertExtraFields',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
if (empty($reshook))
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
}
else if ($reshook < 0) $error++;
$this->db->commit();
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CATEGORY_MODIFY',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
return 1;
}
else
{
$this->db->rollback();
dol_print_error($this->db);
return -1;
}
}
/**
* Delete a category from database
*
* @param User $user Object user that ask to delete
* @return void
*/
function delete($user)
{
global $conf,$langs;
$error=0;
dol_syslog(get_class($this)."::remove");
$this->db->begin();
/* FIX #1317 : Check for child cat and move up 1 level*/
if (! $error)
{
$sql = "UPDATE ".MAIN_DB_PREFIX."categorie";
$sql.= " SET fk_parent = ".$this->fk_parent;
$sql.= " WHERE fk_parent = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_societe";
$sql .= " WHERE fk_categorie = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_fournisseur";
$sql .= " WHERE fk_categorie = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_product";
$sql .= " WHERE fk_categorie = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_member";
$sql .= " WHERE fk_categorie = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact";
$sql .= " WHERE fk_categorie = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
}
// Delete category
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie";
$sql .= " WHERE rowid = ".$this->id;
if (!$this->db->query($sql))
{
$this->error=$this->db->lasterror();
dol_syslog("Error sql=".$sql." ".$this->error, LOG_ERR);
$error++;
}
else
{
// Removed extrafields
if (! $error)
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->deleteExtraFields();
if ($result < 0)
{
$error++;
dol_syslog(get_class($this)."::delete erreur ".$errorflag." ".$this->error, LOG_ERR);
}
}
}
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CATEGORY_DELETE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=join(',',$this->errors); }
// Fin appel triggers
}
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
return -1;
}
}
/**
* Link an object to the category
*
* @param Object $obj Object to link to category
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @return int 1 : OK, -1 : erreur SQL, -2 : id not defined, -3 : Already linked
*/
function add_type($obj,$type)
{
global $user,$langs,$conf;
$error=0;
if ($this->id == -1) return -2;
// For backward compatibility
if ($type == 'company') $type='societe';
if ($type == 'customer') $type='societe';
if ($type == 'supplier') $type='fournisseur';
$column_name=$type;
if ($type=='contact') $column_name='socpeople';
if ($type=='fournisseur') $column_name='societe';
$sql = "INSERT INTO ".MAIN_DB_PREFIX."categorie_".$type." (fk_categorie, fk_".$column_name.")";
$sql .= " VALUES (".$this->id.", ".$obj->id.")";
dol_syslog(get_class($this).'::add_type sql='.$sql);
if ($this->db->query($sql))
{
if (! empty($conf->global->CATEGORIE_RECURSIV_ADD))
{
$sql = 'SELECT fk_parent FROM '.MAIN_DB_PREFIX.'categorie';
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::add_type sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql) > 0)
{
$objparent = $this->db->fetch_object($resql);
if (!empty($objparent->fk_parent))
{
$cat = new Categorie($this->db);
$cat->id=$objparent->fk_parent;
$result=$cat->add_type($obj, $type);
if ($result < 0)
{
$this->error=$cat->error;
$error++;
}
}
}
}
else
{
$error++;
$this->error=$this->db->lasterror();
}
if ($error)
{
return -1;
}
}
// Save object we want to link category to into category instance to provide information to trigger
$this->linkto=$obj;
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CATEGORY_LINK',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; $this->error=$interface->error; }
// Fin appel triggers
if (! $error) return 1;
else return -2;
}
else
{
if ($this->db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
$this->error=$this->db->lasterrno();
return -3;
}
else
{
$this->error=$this->db->lasterror();
}
return -1;
}
}
/**
* Delete object from category
*
* @param Object $obj Object
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @return int 1 if OK, -1 if KO
*/
function del_type($obj,$type)
{
global $user,$langs,$conf;
$error=0;
// For backward compatibility
if ($type == 'company') $type='societe';
if ($type == 'customer') $type='societe';
if ($type == 'supplier') $type='fournisseur';
$column_name=$type;
if ($type=='contact') $column_name='socpeople';
if ($type=='fournisseur') $column_name='societe';
$sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_".$type;
$sql .= " WHERE fk_categorie = ".$this->id;
$sql .= " AND fk_".$column_name." = ".$obj->id;
dol_syslog(get_class($this).'::del_type sql='.$sql);
if ($this->db->query($sql))
{
// Save object we want to unlink category off into category instance to provide information to trigger
$this->unlinkoff=$obj;
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CATEGORY_UNLINK',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
if (! $error) return 1;
else return -2;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
/**
* Return list of fetched instance of elements having this category
*
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @return mixed -1 if KO, array of instance of object if OK
*/
function getObjectsInCateg($type)
{
$field=''; $classname=''; $category_table=''; $object_table='';
if ($type=='product') { $field='product'; $classname='Product'; }
if ($type=='customer') { $field='societe'; $classname='Societe'; }
if ($type=='supplier') { $field='societe'; $classname='Fournisseur'; $category_table='fournisseur'; }
if ($type=='member') { $field='member'; $classname='Adherent'; $category_table=''; $object_table='adherent'; }
if ($type=='contact') { $field='socpeople'; $classname='Contact'; $category_table='contact'; $object_table='socpeople'; }
$objs = array();
// Clean parameters
if (empty($category_table)) $category_table=$field;
if (empty($object_table)) $object_table=$field;
$sql = "SELECT c.fk_".$field;
$sql.= " FROM ".MAIN_DB_PREFIX."categorie_".$category_table." as c";
$sql.= ", ".MAIN_DB_PREFIX.$object_table." as o";
$sql.= " WHERE o.entity IN (".getEntity($field, 1).")";
$sql.= " AND c.fk_categorie = ".$this->id;
$sql.= " AND c.fk_".$field." = o.rowid";
dol_syslog(get_class($this)."::getObjectsInCateg sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
while ($rec = $this->db->fetch_array($resql))
{
$obj = new $classname($this->db);
$obj->fetch($rec['fk_'.$field]);
$objs[] = $obj;
}
return $objs;
}
else
{
$this->error=$this->db->error().' sql='.$sql;
dol_syslog(get_class($this)."::getObjectsInCateg ".$this->error, LOG_ERR);
return -1;
}
}
/**
* check for the presence of an object in a category
*
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @param int $object_id id of the object to search
* @return int number of occurrences
*/
function containsObject($type, $object_id)
{
$field = ''; $classname = ''; $category_table = ''; $object_table = '';
if ($type == 'product') {
$field = 'product';
}
if ($type == 'customer') {
$field = 'societe';
}
if ($type == 'supplier') {
$field = 'societe';
$category_table = 'fournisseur';
}
if ($type == 'member') {
$field = 'member';
$category_table = '';
}
if ($type == 'contact') {
$field = 'socpeople';
$category_table = 'contact';
}
if (empty($category_table)) {
$category_table = $field;
}
$sql = "SELECT COUNT(*) as nb FROM " . MAIN_DB_PREFIX . "categorie_" . $category_table;
$sql .= " WHERE fk_categorie = " . $this->id . " AND fk_" . $field . " = " . $object_id;
dol_syslog(get_class($this)."::containsObject sql=".$sql);
$resql = $this->db->query($sql);
if ($resql) {
return $this->db->fetch_object($resql)->nb;
} else {
$this->error=$this->db->error().' sql='.$sql;
dol_syslog(get_class($this)."::containsObject ".$this->error, LOG_ERR);
return -1;
}
}
/**
* Return childs of a category
*
* @return void
*/
function get_filles()
{
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE fk_parent = ".$this->id;
$res = $this->db->query($sql);
if ($res)
{
$cats = array ();
while ($rec = $this->db->fetch_array($res))
{
$cat = new Categorie($this->db);
$cat->fetch($rec['rowid']);
$cats[] = $cat;
}
return $cats;
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Load this->motherof that is array(id_son=>id_parent, ...)
*
* @return int <0 if KO, >0 if OK
*/
private function load_motherof()
{
global $conf;
$this->motherof=array();
// Load array[child]=parent
$sql = "SELECT fk_parent as id_parent, rowid as id_son";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE fk_parent != 0";
$sql.= " AND entity IN (".getEntity('category',1).")";
dol_syslog(get_class($this)."::load_motherof sql=".$sql);
$resql = $this->db->query($sql);
if ($resql)
{
while ($obj= $this->db->fetch_object($resql))
{
$this->motherof[$obj->id_son]=$obj->id_parent;
}
return 1;
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Reconstruit l'arborescence des categories sous la forme d'un tableau
* Renvoi un tableau de tableau('id','id_mere',...) trie selon arbre et avec:
* id = id de la categorie
* id_mere = id de la categorie mere
* id_children = tableau des id enfant
* label = nom de la categorie
* fulllabel = nom avec chemin complet de la categorie
* fullpath = chemin complet compose des id
*
* @param string $type Type of categories (0=product, 1=suppliers, 2=customers, 3=members)
* @param int $markafterid Removed all categories including the leaf $markafterid in category tree.
* @return array Array of categories. this->cats and this->motherof are set.
*/
function get_full_arbo($type,$markafterid=0)
{
global $langs;
$this->cats = array();
// Init this->motherof that is array(id_son=>id_parent, ...)
$this->load_motherof();
$current_lang = $langs->getDefaultLang();
// Init $this->cats array
$sql = "SELECT DISTINCT c.rowid, c.label, c.description, c.fk_parent"; // Distinct reduce pb with old tables with duplicates
if (! empty($conf->global->MAIN_MULTILANGS))
$sql.= ", t.label as label_trans, t.description as description_trans";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as c";
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."'";
$sql.= " WHERE c.entity IN (".getEntity('category',1).")";
$sql.= " AND c.type = ".$type;
dol_syslog(get_class($this)."::get_full_arbo get category list sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
$i=0;
while ($obj = $this->db->fetch_object($resql))
{
$this->cats[$obj->rowid]['rowid'] = $obj->rowid;
$this->cats[$obj->rowid]['id'] = $obj->rowid;
$this->cats[$obj->rowid]['fk_parent'] = $obj->fk_parent;
$this->cats[$obj->rowid]['label'] = ! empty($obj->label_trans) ? $obj->label_trans : $obj->label;
$this->cats[$obj->rowid]['description'] = ! empty($obj->description_trans) ? $obj->description_trans : $obj->description;
$i++;
}
}
else
{
dol_print_error($this->db);
return -1;
}
// We add the fullpath property to each elements of first level (no parent exists)
dol_syslog(get_class($this)."::get_full_arbo call to build_path_from_id_categ", LOG_DEBUG);
foreach($this->cats as $key => $val)
{
//print 'key='.$key.'<br>'."\n";
$this->build_path_from_id_categ($key,0); // Process a branch from the root category key (this category has no parent)
}
// Exclude leaf including $markafterid from tree
if ($markafterid)
{
//print "Look to discard category ".$markafterid."\n";
$keyfilter1='^'.$markafterid.'$';
$keyfilter2='_'.$markafterid.'$';
$keyfilter3='^'.$markafterid.'_';
$keyfilter4='_'.$markafterid.'_';
foreach($this->cats as $key => $val)
{
if (preg_match('/'.$keyfilter1.'/',$val['fullpath']) || preg_match('/'.$keyfilter2.'/',$val['fullpath'])
|| preg_match('/'.$keyfilter3.'/',$val['fullpath']) || preg_match('/'.$keyfilter4.'/',$val['fullpath']))
{
unset($this->cats[$key]);
}
}
}
dol_syslog(get_class($this)."::get_full_arbo dol_sort_array", LOG_DEBUG);
$this->cats=dol_sort_array($this->cats, 'fulllabel', 'asc', true, false);
//$this->debug_cats();
return $this->cats;
}
/**
* For category id_categ and its childs available in this->cats, define property fullpath and fulllabel
*
* @param int $id_categ id_categ entry to update
* @param int $protection Deep counter to avoid infinite loop
* @return void
*/
function build_path_from_id_categ($id_categ,$protection=1000)
{
dol_syslog(get_class($this)."::build_path_from_id_categ id_categ=".$id_categ." protection=".$protection, LOG_DEBUG);
if (! empty($this->cats[$id_categ]['fullpath']))
{
// Already defined
dol_syslog(get_class($this)."::build_path_from_id_categ fullpath and fulllabel already defined", LOG_WARNING);
return;
}
// First build full array $motherof
//$this->load_motherof(); // Disabled because already done by caller of build_path_from_id_categ
// Define fullpath and fulllabel
$this->cats[$id_categ]['fullpath'] = '_'.$id_categ;
$this->cats[$id_categ]['fulllabel'] = $this->cats[$id_categ]['label'];
$i=0; $cursor_categ=$id_categ;
//print 'Work for id_categ='.$id_categ.'<br>'."\n";
while ((empty($protection) || $i < $protection) && ! empty($this->motherof[$cursor_categ]))
{
//print ' cursor_categ='.$cursor_categ.' i='.$i.' '.$this->motherof[$cursor_categ].'<br>'."\n";
$this->cats[$id_categ]['fullpath'] = '_'.$this->motherof[$cursor_categ].$this->cats[$id_categ]['fullpath'];
$this->cats[$id_categ]['fulllabel'] = $this->cats[$this->motherof[$cursor_categ]]['label'].' >> '.$this->cats[$id_categ]['fulllabel'];
//print ' Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].' '.$this->cats[$id_categ]['fulllabel'].'<br>'."\n";
$i++; $cursor_categ=$this->motherof[$cursor_categ];
}
//print 'Result for id_categ='.$id_categ.' : '.$this->cats[$id_categ]['fullpath'].'<br>'."\n";
// We count number of _ to have level
$this->cats[$id_categ]['level']=dol_strlen(preg_replace('/[^_]/i','',$this->cats[$id_categ]['fullpath']));
return;
}
/**
* Affiche contenu de $this->cats
*
* @return void
*/
function debug_cats()
{
// Affiche $this->cats
foreach($this->cats as $key => $val)
{
print 'id: '.$this->cats[$key]['id'];
print ' label: '.$this->cats[$key]['label'];
print ' mother: '.$this->cats[$key]['fk_parent'];
//print ' children: '.(is_array($this->cats[$key]['id_children'])?join(',',$this->cats[$key]['id_children']):'');
print ' fullpath: '.$this->cats[$key]['fullpath'];
print ' fulllabel: '.$this->cats[$key]['fulllabel'];
print "<br>\n";
}
}
/**
* Retourne toutes les categories
*
* @param int $type Type of category
* @param boolean $parent Just parent categories if true
* @return array Tableau d'objet Categorie
*/
function get_all_categories($type=null, $parent=false)
{
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE entity IN (".getEntity('category',1).")";
if (! is_null($type))
$sql.= " AND type = ".$type;
if ($parent)
$sql.= " AND fk_parent = 0";
$res = $this->db->query($sql);
if ($res)
{
$cats = array ();
while ($rec = $this->db->fetch_array($res))
{
$cat = new Categorie($this->db);
$cat->fetch($rec['rowid']);
$cats[$rec['rowid']] = $cat;
}
return $cats;
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Retourne le nombre total de categories
*
* @return int Nombre de categories
* @deprecated function not used ?
*/
function get_nb_categories()
{
$sql = "SELECT count(rowid)";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie";
$sql.= " WHERE entity IN (".getEntity('category',1).")";
$res = $this->db->query($sql);
if ($res)
{
$res = $this->db->fetch_array($res);
return $res[0];
}
else
{
dol_print_error($this->db);
return -1;
}
}
/**
* Check if no category with same label already exists for this cat's parent or root and for this cat's type
*
* @return boolean 1 if already exist, 0 otherwise, -1 if error
*/
function already_exists()
{
/* We have to select any rowid from llx_categorie which category's mother and label
* are equals to those of the calling category
*/
$sql = "SELECT c.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as c ";
$sql.= " WHERE c.entity IN (".getEntity('category',1).")";
$sql.= " AND c.type = ".$this->type;
$sql.= " AND c.fk_parent = ".$this->fk_parent;
$sql.= " AND c.label = '".$this->db->escape($this->label)."'";
dol_syslog(get_class($this)."::already_exists sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql) > 0) // Checking for empty resql
{
$obj = $this->db->fetch_array($resql);
/* If object called create, obj cannot have is id.
* If object called update, he mustn't have the same label as an other category for this mother.
* So if the result have the same id, update is not for label, and if result have an other one,
* update may be for label.
*/
if($obj[0] > 0 && $obj[0] != $this->id)
{
dol_syslog(get_class($this)."::already_exists category with name=".$this->label." exist rowid=".$obj[0]." current_id=".$this->id, LOG_DEBUG);
return 1;