/
extrafields.class.php
2168 lines (1985 loc) · 84.1 KB
/
extrafields.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) 2002-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2013 Florian Henry <forian.henry@open-concept.pro>
* Copyright (C) 2015 Charles-Fr BENKE <charles.fr@benke.fr>
* Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2017 Nicolas ZABOURI <info@inovea-conseil.com>
* Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
*
* 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/core/class/extrafields.class.php
* \ingroup core
* \brief File of class to manage extra fields
*/
/**
* Class to manage standard extra fields
*/
class ExtraFields
{
/**
* @var DoliDB Database handler.
*/
public $db;
/**
* @var string type of element (for what object is the extrafield)
* @deprecated
*/
public $attribute_elementtype;
/**
* @var array Array with type of the extra field
* @deprecated
*/
public $attribute_type;
/**
* @var array Array with label of extra field
* @deprecated
*/
public $attribute_label;
/**
* @var array Array with size of extra field
* @deprecated
*/
public $attribute_size;
/**
* @var array Array with list of possible values for some types of extra fields
* @deprecated
*/
public $attribute_choice;
/**
* @var array Array to store compute formula for computed fields
* @deprecated
*/
public $attribute_computed;
/**
* @var array Array to store default value
* @deprecated
*/
public $attribute_default;
/**
* @var array Array to store if attribute is unique or not
* @deprecated
*/
public $attribute_unique;
/**
* @var array Array to store if attribute is required or not
* @deprecated
*/
public $attribute_required;
/**
* @var array Array to store parameters of attribute (used in select type)
* @deprecated
*/
public $attribute_param;
/**
* @var array Array to store position of attribute
* @deprecated
*/
public $attribute_pos;
/**
* @var array Array to store if attribute is editable regardless of the document status
* @deprecated
*/
public $attribute_alwayseditable;
/**
* @var array Array to store permission to check
* @deprecated
*/
public $attribute_perms;
/**
* @var array Array to store language file to translate label of values
* @deprecated
*/
public $attribute_langfile;
/**
* @var array Array to store if field is visible by default on list
* @deprecated
*/
public $attribute_list;
/**
* @var array New array to store extrafields definition
*/
public $attributes;
/**
* @var string Error code (or message)
*/
public $error='';
/**
* @var string[] Array of Error code (or message)
*/
public $errors = array();
/**
* @var string DB Error number
*/
public $errno;
public static $type2label=array(
'varchar'=>'String',
'text'=>'TextLong',
'html'=>'HtmlText',
'int'=>'Int',
'double'=>'Float',
'date'=>'Date',
'datetime'=>'DateAndTime',
'boolean'=>'Boolean',
'price'=>'ExtrafieldPrice',
'phone'=>'ExtrafieldPhone',
'mail'=>'ExtrafieldMail',
'url'=>'ExtrafieldUrl',
'password' => 'ExtrafieldPassword',
'select' => 'ExtrafieldSelect',
'sellist' => 'ExtrafieldSelectList',
'radio' => 'ExtrafieldRadio',
'checkbox' => 'ExtrafieldCheckBox',
'chkbxlst' => 'ExtrafieldCheckBoxFromList',
'link' => 'ExtrafieldLink',
'separate' => 'ExtrafieldSeparator',
);
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
$this->error = '';
$this->errors = array();
$this->attributes = array();
// For old usage
$this->attribute_elementtype = array();
$this->attribute_type = array();
$this->attribute_label = array();
$this->attribute_size = array();
$this->attribute_computed = array();
$this->attribute_default = array();
$this->attribute_unique = array();
$this->attribute_required = array();
$this->attribute_perms = array();
$this->attribute_langfile = array();
$this->attribute_list = array();
}
/**
* Add a new extra field parameter
*
* @param string $attrname Code of attribute
* @param string $label label of attribute
* @param int $type Type of attribute ('boolean','int','varchar','text','html','date','datehour','price','phone','mail','password','url','select','checkbox','separate',...)
* @param int $pos Position of attribute
* @param string $size Size/length definition of attribute ('5', '24,8', ...). For float, it contains 2 numeric separated with a comma.
* @param string $elementtype Element type. Same value than object->table_element (Example 'member', 'product', 'thirdparty', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param string $default_value Defaulted value (In database. use the default_value feature for default value on screen. Example: '', '0', 'null', 'avalue')
* @param array|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
* @param int $alwayseditable Is attribute always editable regardless of the document status
* @param string $perms Permission to check
* @param string $list Visibilty ('0'=never visible, '1'=visible on list+forms, '2'=list only, '3'=form only or 'eval string')
* @param string $help Text with help tooltip
* @param string $computed Computed value
* @param string $entity Entity of extrafields (for multicompany modules)
* @param string $langfile Language file
* @param string $enabled Condition to have the field enabled or not
* @return int <=0 if KO, >0 if OK
*/
public function addExtraField($attrname, $label, $type, $pos, $size, $elementtype, $unique = 0, $required = 0, $default_value = '', $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $computed = '', $entity = '', $langfile = '', $enabled = '1')
{
if (empty($attrname)) return -1;
if (empty($label)) return -1;
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
// Create field into database except for separator type which is not stored in database
if ($type != 'separate')
{
$result=$this->create($attrname, $type, $size, $elementtype, $unique, $required, $default_value, $param, $perms, $list, $computed, $help);
}
$err1=$this->errno;
if ($result > 0 || $err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' || $type == 'separate')
{
// Add declaration of field into table
$result2=$this->create_label($attrname, $label, $type, $pos, $size, $elementtype, $unique, $required, $param, $alwayseditable, $perms, $list, $help, $default_value, $computed, $entity, $langfile, $enabled);
$err2=$this->errno;
if ($result2 > 0 || ($err1 == 'DB_ERROR_COLUMN_ALREADY_EXISTS' && $err2 == 'DB_ERROR_RECORD_ALREADY_EXISTS'))
{
$this->error='';
$this->errno=0;
return 1;
}
else return -2;
}
else
{
return -1;
}
}
/**
* Add a new optional attribute.
* This is a private method. For public method, use addExtraField.
*
* @param string $attrname code of attribute
* @param int $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datehour','price','phone','mail','password','url','select','checkbox', ...)
* @param string $length Size/length of attribute ('5', '24,8', ...)
* @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param string $default_value Default value for field (in database)
* @param array $param Params for field (ex for select list : array('options'=>array('value'=>'label of option'))
* @param string $perms Permission
* @param string $list Into list view by default
* @param string $computed Computed value
* @return int <=0 if KO, >0 if OK
*/
private function create($attrname, $type = 'varchar', $length = 255, $elementtype = 'member', $unique = 0, $required = 0, $default_value = '', $param = '', $perms = '', $list = '0', $computed = '')
{
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
$table=$elementtype.'_extrafields';
if ($elementtype == 'categorie') $table='categories_extrafields';
if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9_]*$/", $attrname) && ! is_numeric($attrname))
{
if ($type=='boolean') {
$typedb='int';
$lengthdb='1';
} elseif($type=='price') {
$typedb='double';
$lengthdb='24,8';
} elseif($type=='phone') {
$typedb='varchar';
$lengthdb='20';
} elseif($type=='mail') {
$typedb='varchar';
$lengthdb='128';
} elseif($type=='url') {
$typedb='varchar';
$lengthdb='255';
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') ||($type=='checkbox') ||($type=='chkbxlst')){
$typedb='varchar';
$lengthdb='255';
} elseif ($type=='link') {
$typedb='int';
$lengthdb='11';
} elseif ($type=='html') {
$typedb='text';
$lengthdb=$length;
} elseif($type=='password') {
$typedb='varchar';
$lengthdb='128';
} else {
$typedb=$type;
$lengthdb=$length;
if ($type == 'varchar' && empty($lengthdb)) $lengthdb='255';
}
$field_desc = array(
'type'=>$typedb,
'value'=>$lengthdb,
'null'=>($required?'NOT NULL':'NULL'),
'default' => $default_value
);
$result=$this->db->DDLAddField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
if ($result > 0)
{
if ($unique)
{
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
$resql=$this->db->query($sql, 1, 'dml');
}
return 1;
}
else
{
$this->error=$this->db->lasterror();
$this->errno=$this->db->lasterrno();
return -1;
}
}
else
{
return 0;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Add description of a new optional attribute
*
* @param string $attrname code of attribute
* @param string $label label of attribute
* @param int $type Type of attribute ('int', 'varchar', 'text', 'html', 'date', 'datehour', 'float')
* @param int $pos Position of attribute
* @param string $size Size/length of attribute ('5', '24,8', ...)
* @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param array|string $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
* @param int $alwayseditable Is attribute always editable regardless of the document status
* @param string $perms Permission to check
* @param string $list Visibily
* @param string $help Help on tooltip
* @param string $default Default value (in database. use the default_value feature for default value on screen).
* @param string $computed Computed value
* @param string $entity Entity of extrafields
* @param string $langfile Language file
* @param string $enabled Condition to have the field enabled or not
* @return int <=0 if KO, >0 if OK
*/
private function create_label($attrname, $label = '', $type = '', $pos = 0, $size = 0, $elementtype = 'member', $unique = 0, $required = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '-1', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1')
{
// phpcs:enable
global $conf,$user;
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
// Clean parameters
if (empty($pos)) $pos=0;
if (empty($list)) $list='0';
if (empty($required)) $required=0;
if (empty($unique)) $unique=0;
if (empty($alwayseditable)) $alwayseditable=0;
if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname) && ! is_numeric($attrname))
{
if (is_array($param) && count($param) > 0)
{
$params = serialize($param);
}
elseif (strlen($param) > 0)
{
$params = trim($param);
}
else
{
$params='';
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
$sql.= " name,";
$sql.= " label,";
$sql.= " type,";
$sql.= " pos,";
$sql.= " size,";
$sql.= " entity,";
$sql.= " elementtype,";
$sql.= " fieldunique,";
$sql.= " fieldrequired,";
$sql.= " param,";
$sql.= " alwayseditable,";
$sql.= " perms,";
$sql.= " langs,";
$sql.= " list,";
$sql.= " fielddefault,";
$sql.= " fieldcomputed,";
$sql.= " fk_user_author,";
$sql.= " fk_user_modif,";
$sql.= " datec,";
$sql.= " enabled,";
$sql.= " help";
$sql.= " )";
$sql.= " VALUES('".$attrname."',";
$sql.= " '".$this->db->escape($label)."',";
$sql.= " '".$this->db->escape($type)."',";
$sql.= " ".$pos.",";
$sql.= " '".$this->db->escape($size)."',";
$sql.= " ".($entity===''?$conf->entity:$entity).",";
$sql.= " '".$this->db->escape($elementtype)."',";
$sql.= " ".$unique.",";
$sql.= " ".$required.",";
$sql.= " '".$this->db->escape($params)."',";
$sql.= " ".$alwayseditable.",";
$sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
$sql.= " ".($langfile?"'".$this->db->escape($langfile)."'":"null").",";
$sql.= " '".$this->db->escape($list)."',";
$sql.= " ".($default?"'".$this->db->escape($default)."'":"null").",";
$sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null").",";
$sql .= " " . (is_object($user) ? $user->id : 0). ",";
$sql .= " " . (is_object($user) ? $user->id : 0). ",";
$sql .= "'" . $this->db->idate(dol_now()) . "',";
$sql.= " ".($enabled?"'".$this->db->escape($enabled)."'":"1").",";
$sql.= " ".($help?"'".$this->db->escape($help)."'":"null");
$sql.=')';
dol_syslog(get_class($this)."::create_label", LOG_DEBUG);
if ($this->db->query($sql))
{
return 1;
}
else
{
$this->error=$this->db->lasterror();
$this->errno=$this->db->lasterrno();
return -1;
}
}
}
/**
* Delete an optional attribute
*
* @param string $attrname Code of attribute to delete
* @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
* @return int < 0 if KO, 0 if nothing is done, 1 if OK
*/
public function delete($attrname, $elementtype = 'member')
{
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
$table=$elementtype.'_extrafields';
if ($elementtype == 'categorie') $table='categories_extrafields';
$error=0;
if (! empty($attrname) && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname))
{
$result=$this->delete_label($attrname, $elementtype);
if ($result < 0)
{
$this->error=$this->db->lasterror();
$this->errors[]=$this->db->lasterror();
$error++;
}
if (! $error)
{
$sql = "SELECT COUNT(rowid) as nb";
$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
$sql.= " WHERE elementtype = '".$elementtype."'";
$sql.= " AND name = '".$attrname."'";
//$sql.= " AND entity IN (0,".$conf->entity.")"; Do not test on entity here. We want to see if there is still on field remaning in other entities before deleting field in table
$resql = $this->db->query($sql);
if ($resql)
{
$obj = $this->db->fetch_object($resql);
if ($obj->nb <= 0)
{
$result=$this->db->DDLDropField(MAIN_DB_PREFIX.$table, $attrname); // This also drop the unique key
if ($result < 0)
{
$this->error=$this->db->lasterror();
$this->errors[]=$this->db->lasterror();
$error++;
}
}
}
}
return $result;
}
else
{
return 0;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Delete description of an optional attribute
*
* @param string $attrname Code of attribute to delete
* @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
* @return int < 0 if KO, 0 if nothing is done, 1 if OK
*/
private function delete_label($attrname, $elementtype = 'member')
{
// phpcs:enable
global $conf;
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname))
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
$sql.= " WHERE name = '".$attrname."'";
$sql.= " AND entity IN (0,".$conf->entity.')';
$sql.= " AND elementtype = '".$elementtype."'";
dol_syslog(get_class($this)."::delete_label", LOG_DEBUG);
$resql=$this->db->query($sql);
if ($resql)
{
return 1;
}
else
{
print dol_print_error($this->db);
return -1;
}
}
else
{
return 0;
}
}
/**
* Modify type of a personalized attribute
*
* @param string $attrname Name of attribute
* @param string $label Label of attribute
* @param string $type Type of attribute ('boolean', 'int', 'varchar', 'text', 'html', 'date', 'datehour','price','phone','mail','password','url','select','checkbox', ...)
* @param int $length Length of attribute
* @param string $elementtype Element type ('member', 'product', 'thirdparty', 'contact', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param int $pos Position of attribute
* @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
* @param int $alwayseditable Is attribute always editable regardless of the document status
* @param string $perms Permission to check
* @param string $list Visibility
* @param string $help Help on tooltip
* @param string $default Default value (in database. use the default_value feature for default value on screen).
* @param string $computed Computed value
* @param string $entity Entity of extrafields
* @param string $langfile Language file
* @param string $enabled Condition to have the field enabled or not
* @param int $totalizable Is extrafield totalizable on list
* @return int >0 if OK, <=0 if KO
*/
public function update($attrname, $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0)
{
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
$table=$elementtype.'_extrafields';
if ($elementtype == 'categorie') $table='categories_extrafields';
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname))
{
if ($type=='boolean') {
$typedb='int';
$lengthdb='1';
} elseif($type=='price') {
$typedb='double';
$lengthdb='24,8';
} elseif($type=='phone') {
$typedb='varchar';
$lengthdb='20';
} elseif($type=='mail') {
$typedb='varchar';
$lengthdb='128';
} elseif($type=='url') {
$typedb='varchar';
$lengthdb='255';
} elseif (($type=='select') || ($type=='sellist') || ($type=='radio') || ($type=='checkbox') || ($type=='chkbxlst')) {
$typedb='varchar';
$lengthdb='255';
} elseif ($type == 'html') {
$typedb='text';
} elseif ($type=='link') {
$typedb='int';
$lengthdb='11';
} elseif($type=='password') {
$typedb='varchar';
$lengthdb='50';
} else {
$typedb=$type;
$lengthdb=$length;
}
$field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL'), 'default'=>$default);
if ($type != 'separate') // No table update when separate type
{
$result=$this->db->DDLUpdateField(MAIN_DB_PREFIX.$table, $attrname, $field_desc);
}
if ($result > 0 || $type == 'separate')
{
if ($label)
{
$result=$this->update_label($attrname, $label, $type, $length, $elementtype, $unique, $required, $pos, $param, $alwayseditable, $perms, $list, $help, $default, $computed, $entity, $langfile, $enabled, $totalizable);
}
if ($result > 0)
{
$sql='';
if ($unique)
{
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." ADD UNIQUE INDEX uk_".$table."_".$attrname." (".$attrname.")";
}
else
{
$sql="ALTER TABLE ".MAIN_DB_PREFIX.$table." DROP INDEX uk_".$table."_".$attrname;
}
dol_syslog(get_class($this).'::update', LOG_DEBUG);
$resql=$this->db->query($sql, 1, 'dml');
return 1;
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
else
{
$this->error=$this->db->lasterror();
return -1;
}
}
else
{
return 0;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Modify description of personalized attribute
*
* @param string $attrname Name of attribute
* @param string $label Label of attribute
* @param string $type Type of attribute
* @param int $size Length of attribute
* @param string $elementtype Element type ('member', 'product', 'thirdparty', ...)
* @param int $unique Is field unique or not
* @param int $required Is field required or not
* @param int $pos Position of attribute
* @param array $param Params for field (ex for select list : array('options' => array(value'=>'label of option')) )
* @param int $alwayseditable Is attribute always editable regardless of the document status
* @param string $perms Permission to check
* @param string $list Visiblity
* @param string $help Help on tooltip.
* @param string $default Default value (in database. use the default_value feature for default value on screen).
* @param string $computed Computed value
* @param string $entity Entity of extrafields
* @param string $langfile Language file
* @param string $enabled Condition to have the field enabled or not
* @param int $totalizable Is extrafield totalizable on list
* @return int <=0 if KO, >0 if OK
*/
private function update_label($attrname, $label, $type, $size, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '0', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0)
{
// phpcs:enable
global $conf, $user;
dol_syslog(get_class($this)."::update_label ".$attrname.", ".$label.", ".$type.", ".$size.", ".$elementtype.", ".$unique.", ".$required.", ".$pos.", ".$alwayseditable.", ".$perms.", ".$list.", ".$default.", ".$computed.", ".$entity.", ".$langfile.", ".$enabled.", ".$totalizable);
// Clean parameters
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
if (empty($pos)) $pos=0;
if (empty($list)) $list='0';
if (empty($totalizable)) {
$totalizable = 0;
}
if (empty($required)) $required=0;
if (empty($unique)) $unique=0;
if (empty($alwayseditable)) $alwayseditable=0;
if (isset($attrname) && $attrname != '' && preg_match("/^\w[a-zA-Z0-9-_]*$/", $attrname))
{
$this->db->begin();
if (is_array($param) && count($param) > 0)
{
$params = serialize($param);
}
elseif (strlen($param) > 0)
{
$params = trim($param);
}
else
{
$params='';
}
if ($entity === '' || $entity != '0')
{
// We dont want on all entities, we delete all and current
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
$sql_del.= " WHERE name = '".$attrname."'";
$sql_del.= " AND entity IN (0, ".($entity===''?$conf->entity:$entity).")";
$sql_del.= " AND elementtype = '".$elementtype."'";
}
else
{
// We want on all entities ($entities = '0'), we delete on all only (we keep setup specific to each entity)
$sql_del = "DELETE FROM ".MAIN_DB_PREFIX."extrafields";
$sql_del.= " WHERE name = '".$attrname."'";
$sql_del.= " AND entity = 0";
$sql_del.= " AND elementtype = '".$elementtype."'";
}
$resql1=$this->db->query($sql_del);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."extrafields(";
$sql.= " name,"; // This is code
$sql.= " entity,";
$sql.= " label,";
$sql.= " type,";
$sql.= " size,";
$sql.= " elementtype,";
$sql.= " fieldunique,";
$sql.= " fieldrequired,";
$sql.= " perms,";
$sql.= " langs,";
$sql.= " pos,";
$sql.= " alwayseditable,";
$sql.= " param,";
$sql.= " list,";
$sql.= " totalizable,";
$sql.= " fielddefault,";
$sql.= " fieldcomputed,";
$sql.= " fk_user_author,";
$sql.= " fk_user_modif,";
$sql.= " datec,";
$sql.= " enabled,";
$sql.= " help";
$sql.= ") VALUES (";
$sql.= "'".$attrname."',";
$sql.= " ".($entity===''?$conf->entity:$entity).",";
$sql.= " '".$this->db->escape($label)."',";
$sql.= " '".$this->db->escape($type)."',";
$sql.= " '".$this->db->escape($size)."',";
$sql.= " '".$this->db->escape($elementtype)."',";
$sql.= " ".$unique.",";
$sql.= " ".$required.",";
$sql.= " ".($perms?"'".$this->db->escape($perms)."'":"null").",";
$sql.= " ".($langfile?"'".$this->db->escape($langfile)."'":"null").",";
$sql.= " ".$pos.",";
$sql.= " '".$this->db->escape($alwayseditable)."',";
$sql.= " '".$this->db->escape($params)."',";
$sql.= " '".$this->db->escape($list)."', ";
$sql.= " ".$totalizable.",";
$sql.= " ".(($default!='')?"'".$this->db->escape($default)."'":"null").",";
$sql.= " ".($computed?"'".$this->db->escape($computed)."'":"null").",";
$sql .= " " . $user->id . ",";
$sql .= " " . $user->id . ",";
$sql .= "'" . $this->db->idate(dol_now()) . "',";
$sql .= "'" . $this->db->escape($enabled). "',";
$sql.= " ".($help?"'".$this->db->escape($help)."'":"null");
$sql.= ")";
$resql2=$this->db->query($sql);
if ($resql1 && $resql2)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
print dol_print_error($this->db);
return -1;
}
}
else
{
return 0;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Load array this->attributes, or old this->attribute_xxx like attribute_label, attribute_type, ...
*
* @param string $elementtype Type of element ('adherent', 'commande', 'thirdparty', 'facture', 'propal', 'product', ...).
* @param boolean $forceload Force load of extra fields whatever is option MAIN_EXTRAFIELDS_DISABLED. Deprecated. Should not be required.
* @return array Array of attributes keys+label for all extra fields.
*/
public function fetch_name_optionals_label($elementtype, $forceload = false)
{
// phpcs:enable
global $conf;
if (empty($elementtype)) return array();
if ($elementtype == 'thirdparty') $elementtype='societe';
if ($elementtype == 'contact') $elementtype='socpeople';
if ($elementtype == 'order_supplier') $elementtype='commande_fournisseur';
$array_name_label=array();
// To avoid conflicts with external modules. TODO Remove this.
if (!$forceload && !empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) return $array_name_label;
// Set array of label of entity
// TODO Remove completely loading of label. This should be done by presentation.
$labelmulticompany=array();
if (!empty($conf->multicompany->enabled))
{
$sql_entity_name='SELECT rowid, label FROM '.MAIN_DB_PREFIX.'entity WHERE rowid in (0,'.$conf->entity.')';
$resql_entity_name=$this->db->query($sql_entity_name);
if ($resql_entity_name)
{
while ($obj = $this->db->fetch_object($resql_entity_name))
{
$labelmulticompany[$obj->rowid]=$obj->label;
}
}
}
// We should not have several time this log. If we have, there is some optimization to do by calling a simple $object->fetch_optionals() that include cache management.
dol_syslog("fetch_name_optionals_label elementtype=".$elementtype);
$sql = "SELECT rowid,name,label,type,size,elementtype,fieldunique,fieldrequired,param,pos,alwayseditable,perms,langs,list,totalizable,fielddefault,fieldcomputed,entity,enabled,help";
$sql.= " FROM ".MAIN_DB_PREFIX."extrafields";
//$sql.= " WHERE entity IN (0,".$conf->entity.")"; // Filter is done later
if ($elementtype) $sql.= " WHERE elementtype = '".$elementtype."'"; // Filed with object->table_element
$sql.= " ORDER BY pos";
$resql=$this->db->query($sql);
if ($resql)
{
if ($this->db->num_rows($resql))
{
while ($tab = $this->db->fetch_object($resql))
{
if ($tab->entity != 0 && $tab->entity != $conf->entity)
{
// This field is not in current entity. We discard but before we save it into the array of mandatory fields if it is a mandatory field without default value
if ($tab->fieldrequired && is_null($tab->fielddefault))
{
$this->attributes[$tab->elementtype]['mandatoryfieldsofotherentities'][$tab->name]=$tab->type;
}
continue;
}
// We can add this attribute to object. TODO Remove this and return $this->attributes[$elementtype]['label']
if ($tab->type != 'separate')
{
$array_name_label[$tab->name]=$tab->label;
}
// Old usage
$this->attribute_type[$tab->name]=$tab->type;
$this->attribute_label[$tab->name]=$tab->label;
$this->attribute_size[$tab->name]=$tab->size;
$this->attribute_elementtype[$tab->name]=$tab->elementtype;
$this->attribute_default[$tab->name]=$tab->fielddefault;
$this->attribute_computed[$tab->name]=$tab->fieldcomputed;
$this->attribute_unique[$tab->name]=$tab->fieldunique;
$this->attribute_required[$tab->name]=$tab->fieldrequired;
$this->attribute_param[$tab->name]=($tab->param ? unserialize($tab->param) : '');
$this->attribute_pos[$tab->name]=$tab->pos;
$this->attribute_alwayseditable[$tab->name]=$tab->alwayseditable;
$this->attribute_perms[$tab->name]=(strlen($tab->perms) == 0 ? 1 : $tab->perms);
$this->attribute_langfile[$tab->name]=$tab->langs;
$this->attribute_list[$tab->name]=$tab->list;
$this->attribute_totalizable[$tab->name]=$tab->totalizable;
$this->attribute_entityid[$tab->name]=$tab->entity;
$this->attribute_entitylabel[$tab->name]=(empty($labelmulticompany[$tab->entity])?'Entity'.$tab->entity:$labelmulticompany[$tab->entity]);
// New usage
$this->attributes[$tab->elementtype]['type'][$tab->name]=$tab->type;
$this->attributes[$tab->elementtype]['label'][$tab->name]=$tab->label;
$this->attributes[$tab->elementtype]['size'][$tab->name]=$tab->size;
$this->attributes[$tab->elementtype]['elementtype'][$tab->name]=$tab->elementtype;
$this->attributes[$tab->elementtype]['default'][$tab->name]=$tab->fielddefault;
$this->attributes[$tab->elementtype]['computed'][$tab->name]=$tab->fieldcomputed;
$this->attributes[$tab->elementtype]['unique'][$tab->name]=$tab->fieldunique;
$this->attributes[$tab->elementtype]['required'][$tab->name]=$tab->fieldrequired;
$this->attributes[$tab->elementtype]['param'][$tab->name]=($tab->param ? unserialize($tab->param) : '');
$this->attributes[$tab->elementtype]['pos'][$tab->name]=$tab->pos;
$this->attributes[$tab->elementtype]['alwayseditable'][$tab->name]=$tab->alwayseditable;
$this->attributes[$tab->elementtype]['perms'][$tab->name]=(strlen($tab->perms) == 0 ? 1 : $tab->perms);
$this->attributes[$tab->elementtype]['langfile'][$tab->name]=$tab->langs;
$this->attributes[$tab->elementtype]['list'][$tab->name]=$tab->list;
$this->attributes[$tab->elementtype]['totalizable'][$tab->name]=$tab->totalizable;
$this->attributes[$tab->elementtype]['entityid'][$tab->name]=$tab->entity;
$this->attributes[$tab->elementtype]['entitylabel'][$tab->name]=(empty($labelmulticompany[$tab->entity])?'Entity'.$tab->entity:$labelmulticompany[$tab->entity]);
$this->attributes[$tab->elementtype]['enabled'][$tab->name]=$tab->enabled;
$this->attributes[$tab->elementtype]['help'][$tab->name]=$tab->help;
$this->attributes[$tab->elementtype]['loaded']=1;
}
}
if ($elementtype) $this->attributes[$elementtype]['loaded']=1; // If nothing found, we also save tag 'loaded'
}
else
{
$this->error=$this->db->lasterror();
dol_syslog(get_class($this)."::fetch_name_optionals_label ".$this->error, LOG_ERR);
}
return $array_name_label;
}
/**
* Return HTML string to put an input field into a page
* Code very similar with showInputField of common object
*
* @param string $key Key of attribute
* @param string $value Preselected value to show (for date type it must be in timestamp format, for amount or price it must be a php numeric value)
* @param string $moreparam To add more parametes on html input tag
* @param string $keysuffix Prefix string to add after name and id of field (can be used to avoid duplicate names)
* @param string $keyprefix Suffix string to add before name and id of field (can be used to avoid duplicate names)
* @param string $morecss More css (to defined size of field. Old behaviour: may also be a numeric)
* @param int $objectid Current object id
* @param string $extrafieldsobjectkey If defined (for example $object->table_element), use the new method to get extrafields data
* @return string
*/
public function showInputField($key, $value, $moreparam = '', $keysuffix = '', $keyprefix = '', $morecss = '', $objectid = 0, $extrafieldsobjectkey = '')
{
global $conf,$langs,$form;
if (! is_object($form))
{
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
$form=new Form($this->db);
}
$out='';
if (! preg_match('/options_$/', $keyprefix)) // Because we work on extrafields, we add 'options_' to prefix if not already added
{
$keyprefix = $keyprefix.'options_';
}
if (! empty($extrafieldsobjectkey))
{
$label=$this->attributes[$extrafieldsobjectkey]['label'][$key];
$type=$this->attributes[$extrafieldsobjectkey]['type'][$key];
$size=$this->attributes[$extrafieldsobjectkey]['size'][$key];
$default=$this->attributes[$extrafieldsobjectkey]['default'][$key];
$computed=$this->attributes[$extrafieldsobjectkey]['computed'][$key];
$unique=$this->attributes[$extrafieldsobjectkey]['unique'][$key];
$required=$this->attributes[$extrafieldsobjectkey]['required'][$key];
$param=$this->attributes[$extrafieldsobjectkey]['param'][$key];
$perms=dol_eval($this->attributes[$extrafieldsobjectkey]['perms'][$key], 1);
$langfile=$this->attributes[$extrafieldsobjectkey]['langfile'][$key];
$list=dol_eval($this->attributes[$extrafieldsobjectkey]['list'][$key], 1);
$totalizable=$this->attributes[$extrafieldsobjectkey]['totalizable'][$key];
$help=$this->attributes[$extrafieldsobjectkey]['help'][$key];
$hidden=(empty($list) ? 1 : 0); // If empty, we are sure it is hidden, otherwise we show. If it depends on mode (view/create/edit form or list, this must be filtered by caller)
}
else // Old usage
{
$label=$this->attribute_label[$key];
$type =$this->attribute_type[$key];