Passed
Pull Request — main (#3)
by Rafael
54:18
created

FormSetupItem::setAsSelectBankAccount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2021       John BOTELLA                <[email protected]>
4
 * Copyright (C) 2024		MDW							<[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace Dolibarr\Code\Core\Classes;
22
23
use Dolibarr\Code\Categories\Classes\Categorie;
24
use DoliDB;
25
26
/**
27
 * This class help to create item for class formSetup
28
 */
29
class FormSetupItem
30
{
31
    /**
32
     * @var DoliDB Database handler.
33
     */
34
    public $db;
35
36
    /** @var Translate */
37
    public $langs;
38
39
    /** @var int */
40
    public $entity;
41
42
    /** @var Form */
43
    public $form;
44
45
46
    /** @var string $confKey the conf key used in database */
47
    public $confKey;
48
49
    /** @var string|false $nameText */
50
    public $nameText = false;
51
52
    /** @var string $helpText */
53
    public $helpText = '';
54
55
    /** @var string $picto */
56
    public $picto = '';
57
58
    /** @var string $fieldValue */
59
    public $fieldValue;
60
61
    /** @var string $defaultFieldValue */
62
    public $defaultFieldValue = null;
63
64
    /** @var array $fieldAttr  fields attribute only for compatible fields like input text */
65
    public $fieldAttr = array();
66
67
    /** @var bool|string set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too */
68
    public $fieldOverride = false;
69
70
    /** @var bool|string set this var to override field input */
71
    public $fieldInputOverride = false;
72
73
    /** @var bool|string set this var to override field output */
74
    public $fieldOutputOverride = false;
75
76
    /** @var int $rank  */
77
    public $rank = 0;
78
79
    /** @var array set this var for options on select and multiselect items   */
80
    public $fieldOptions = array();
81
82
    /** @var array set this var to add more parameters */
83
    public $fieldParams = array();
84
85
    /** @var callable $saveCallBack  */
86
    public $saveCallBack;
87
88
    /** @var callable $setValueFromPostCallBack  */
89
    public $setValueFromPostCallBack;
90
91
    /**
92
     * @var string[] $errors
93
     */
94
    public $errors = array();
95
96
    /**
97
     * TODO each type must have setAs{type} method to help configuration
98
     *   And set var as protected when its done configuration must be done by method
99
     *   this is important for retrocompatibility of futures versions
100
     * @var string $type  'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type'
101
     */
102
    protected $type = 'string';
103
104
    public $enabled = 1;
105
106
    /**
107
     * @var string  The css to use on the input field of item
108
     */
109
    public $cssClass = '';
110
111
    /**
112
     * Constructor
113
     *
114
     * @param string    $confKey    the conf key used in database
115
     */
116
    public function __construct($confKey)
117
    {
118
        global $langs, $db, $conf, $form;
119
        $this->db = $db;
120
121
        if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
122
            $this->form = $form;
123
        } else {
124
            $this->form = new Form($this->db);
125
        }
126
127
        $this->langs = $langs;
128
        $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
129
130
        $this->confKey = $confKey;
131
        $this->loadValueFromConf();
132
    }
133
134
    /**
135
     * load conf value from databases
136
     *
137
     * @return bool
138
     */
139
    public function loadValueFromConf()
140
    {
141
        global $conf;
142
        if (isset($conf->global->{$this->confKey})) {
143
            $this->fieldValue = getDolGlobalString($this->confKey);
144
            return true;
145
        } else {
146
            $this->fieldValue = '';
147
            return false;
148
        }
149
    }
150
151
    /**
152
     * Reload conf value from databases is an alias of loadValueFromConf
153
     *
154
     * @deprecated
155
     * @return bool
156
     */
157
    public function reloadValueFromConf()
158
    {
159
        return $this->loadValueFromConf();
160
    }
161
162
163
    /**
164
     * Save const value based on htdocs/core/actions_setmoduleoptions.inc.php
165
     *
166
     * @return     int                  -1 if KO, 1 if OK
167
     */
168
    public function saveConfValue()
169
    {
170
        global $hookmanager;
171
172
        $parameters = array();
173
        $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
174
        if ($reshook < 0) {
175
            $this->setErrors($hookmanager->errors);
176
            return -1;
177
        }
178
179
        if ($reshook > 0) {
180
            return $reshook;
181
        }
182
183
184
        if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
185
            return call_user_func($this->saveCallBack, $this);
186
        }
187
188
        // Modify constant only if key was posted (avoid resetting key to the null value)
189
        if ($this->type != 'title') {
190
            $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
191
            if ($result < 0) {
192
                return -1;
193
            } else {
194
                return 1;
195
            }
196
        }
197
198
        return 0;
199
    }
200
201
    /**
202
     * Set an override function for saving data
203
     *
204
     * @param callable $callBack a callable function
205
     * @return void
206
     */
207
    public function setSaveCallBack(callable $callBack)
208
    {
209
        $this->saveCallBack = $callBack;
210
    }
211
212
    /**
213
     * Set an override function for get data from post
214
     *
215
     * @param callable $callBack a callable function
216
     * @return void
217
     */
218
    public function setValueFromPostCallBack(callable $callBack)
219
    {
220
        $this->setValueFromPostCallBack = $callBack;
221
    }
222
223
    /**
224
     * Save const value based on htdocs/core/actions_setmoduleoptions.inc.php
225
     *
226
     * @return     int                  -1 if KO, 0  nothing to do , 1 if OK
227
     */
228
    public function setValueFromPost()
229
    {
230
        if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
231
            return call_user_func($this->setValueFromPostCallBack);
232
        }
233
234
        // Modify constant only if key was posted (avoid resetting key to the null value)
235
        if ($this->type != 'title') {
236
            if (preg_match('/category:/', $this->type)) {
237
                if (GETPOSTINT($this->confKey) == '-1') {
238
                    $val_const = '';
239
                } else {
240
                    $val_const = GETPOSTINT($this->confKey);
241
                }
242
            } elseif ($this->type == 'multiselect') {
243
                $val = GETPOST($this->confKey, 'array');
244
                if ($val && is_array($val)) {
245
                    $val_const = implode(',', $val);
246
                } else {
247
                    $val_const = '';
248
                }
249
            } elseif ($this->type == 'html') {
250
                $val_const = GETPOST($this->confKey, 'restricthtml');
251
            } else {
252
                $val_const = GETPOST($this->confKey, 'alpha');
253
            }
254
255
            // TODO add value check with class validate
256
            $this->fieldValue = $val_const;
0 ignored issues
show
Documentation Bug introduced by
It seems like $val_const can also be of type array. However, the property $fieldValue is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
257
258
            return 1;
259
        }
260
261
        return 0;
262
    }
263
264
    /**
265
     * Get help text or generate it
266
     *
267
     * @return int|string
268
     */
269
    public function getHelpText()
270
    {
271
        if (!empty($this->helpText)) {
272
            return $this->helpText;
273
        }
274
        return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
275
    }
276
277
    /**
278
     * Get field name text or generate it
279
     *
280
     * @return false|int|string
281
     */
282
    public function getNameText()
283
    {
284
        if (!empty($this->nameText)) {
285
            return $this->nameText;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->nameText also could return the type boolean which is incompatible with the documented return type false|integer|string.
Loading history...
286
        }
287
        $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
288
289
        // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
290
        if (isModEnabled('multicompany') && $this->entity == 0) {
291
            $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
292
        }
293
294
        return $out;
295
    }
296
297
    /**
298
     * generate input field
299
     *
300
     * @return bool|string
301
     */
302
    public function generateInputField()
303
    {
304
        global $conf;
305
306
        if (!empty($this->fieldOverride)) {
307
            return $this->fieldOverride;
308
        }
309
310
        if (!empty($this->fieldInputOverride)) {
311
            return $this->fieldInputOverride;
312
        }
313
314
        // Set default value
315
        if (is_null($this->fieldValue)) {
316
            $this->fieldValue = $this->defaultFieldValue;
317
        }
318
319
320
        $this->fieldAttr['name'] = $this->confKey;
321
        $this->fieldAttr['id'] = 'setup-' . $this->confKey;
322
        $this->fieldAttr['value'] = $this->fieldValue;
323
324
        $out = '';
325
326
        if ($this->type == 'title') {
327
            $out .= $this->generateOutputField(); // title have no input
328
        } elseif ($this->type == 'multiselect') {
329
            $out .= $this->generateInputFieldMultiSelect();
330
        } elseif ($this->type == 'select') {
331
            $out .= $this->generateInputFieldSelect();
332
        } elseif ($this->type == 'selectUser') {
333
            $out .= $this->generateInputFieldSelectUser();
334
        } elseif ($this->type == 'textarea') {
335
            $out .= $this->generateInputFieldTextarea();
336
        } elseif ($this->type == 'html') {
337
            $out .= $this->generateInputFieldHtml();
338
        } elseif ($this->type == 'color') {
339
            $out .=  $this->generateInputFieldColor();
340
        } elseif ($this->type == 'yesno') {
341
            if (!empty($conf->use_javascript_ajax)) {
342
                $out .= ajax_constantonoff($this->confKey);
343
            } else {
344
                $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
345
            }
346
        } elseif (preg_match('/emailtemplate:/', $this->type)) {
347
            $out .= $this->generateInputFieldEmailTemplate();
348
        } elseif (preg_match('/category:/', $this->type)) {
349
            $out .= $this->generateInputFieldCategories();
350
        } elseif (preg_match('/thirdparty_type/', $this->type)) {
351
            $formcompany = new FormCompany($this->db);
352
            $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
353
        } elseif ($this->type == 'securekey') {
354
            $out .= $this->generateInputFieldSecureKey();
355
        } elseif ($this->type == 'product') {
356
            if (isModEnabled("product") || isModEnabled("service")) {
357
                $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
358
                $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
359
            }
360
        } elseif ($this->type == 'selectBankAccount') {
361
            if (isModEnabled("bank")) {
362
                $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
363
                $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
364
            }
365
        } elseif ($this->type == 'password') {
366
            $out .= $this->generateInputFieldPassword('dolibarr');
367
        } elseif ($this->type == 'genericpassword') {
368
            $out .= $this->generateInputFieldPassword('generic');
369
        } else {
370
            $out .= $this->generateInputFieldText();
371
        }
372
373
        return $out;
374
    }
375
376
    /**
377
     * generatec default input field
378
     *
379
     * @return string
380
     */
381
    public function generateInputFieldText()
382
    {
383
        if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
384
            $this->fieldAttr['class'] = 'flat ' . (empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
385
        }
386
        return '<input ' . FormSetup::generateAttributesStringFromArray($this->fieldAttr) . ' />';
387
    }
388
389
    /**
390
     * generate input field for textarea
391
     *
392
     * @return string
393
     */
394
    public function generateInputFieldTextarea()
395
    {
396
        $out = '<textarea class="flat" name="' . $this->confKey . '" id="' . $this->confKey . '" cols="50" rows="5" wrap="soft">' . "\n";
397
        $out .= dol_htmlentities($this->fieldValue);
398
        $out .= "</textarea>\n";
399
        return $out;
400
    }
401
402
    /**
403
     * generate input field for html
404
     *
405
     * @return string
406
     */
407
    public function generateInputFieldHtml()
408
    {
409
        global $conf;
410
            $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
411
        return $doleditor->Create(1);
412
    }
413
414
    /**
415
     * generate input field for categories
416
     *
417
     * @return string
418
     */
419
    public function generateInputFieldCategories()
420
    {
421
        $formother = new FormOther($this->db);
422
423
        $tmp = explode(':', $this->type);
424
        $out = img_picto('', 'category', 'class="pictofixedwidth"');
425
426
        $label = 'Categories';
427
        if ($this->type == 'customer') {
428
            $label = 'CustomersProspectsCategoriesShort';
429
        }
430
        $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
431
432
        return $out;
433
    }
434
435
    /**
436
     * generate input field for email template selector
437
     * @return string
438
     */
439
    public function generateInputFieldEmailTemplate()
440
    {
441
        global $user;
442
443
        $out = '';
444
        if (preg_match('/emailtemplate:/', $this->type)) {
445
            include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
446
            $formmail = new FormMail($this->db);
447
448
            $tmp = explode(':', $this->type);
449
            $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
450
            $arrayOfMessageName = array();
451
            if (is_array($formmail->lines_model)) {
452
                foreach ($formmail->lines_model as $modelMail) {
453
                    $moreonlabel = '';
454
                    if (!empty($arrayOfMessageName[$modelMail->label])) {
455
                        $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
456
                    }
457
                    // The 'label' is the key that is unique if we exclude the language
458
                    $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel;
459
                }
460
            }
461
            $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
462
        }
463
464
        return $out;
465
    }
466
467
468
    /**
469
     * generate input field for secure key
470
     *
471
     * @return string
472
     */
473
    public function generateInputFieldSecureKey()
474
    {
475
        global $conf;
476
        $out = '<input type="text" class="flat minwidth150' . ($this->cssClass ? ' ' . $this->cssClass : '') . '" id="' . $this->confKey . '" name="' . $this->confKey . '" value="' . (GETPOST($this->confKey, 'alpha') ? GETPOST($this->confKey, 'alpha') : $this->fieldValue) . '">';
477
478
        if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
479
            $out .= '&nbsp;' . img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token' . $this->confKey . '" class="linkobject"');
480
481
            // Add button to autosuggest a key
482
            include_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php';
483
            $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token' . $this->confKey);
484
        }
485
486
        return $out;
487
    }
488
489
490
    /**
491
     * generate input field for a password
492
     *
493
     * @param   string  $type  'dolibarr' (dolibarr password rules apply) or 'generic'
494
     *
495
     * @return  string
496
     */
497
    public function generateInputFieldPassword($type = 'generic')
498
    {
499
        global $conf, $langs, $user;
500
501
        $min = 6;
502
        $max = 50;
503
        if ($type == 'dolibarr') {
504
            $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
505
            if ($gen == 'none') {
506
                $gen = 'standard';
507
            }
508
            $nomclass = "modGeneratePass" . ucfirst($gen);
509
            $nomfichier = $nomclass . ".class.php";
510
            require_once DOL_DOCUMENT_ROOT . "/core/modules/security/generate/" . $nomfichier;
511
            $genhandler = new $nomclass($this->db, $conf, $langs, $user);
512
            $min = $genhandler->length;
513
            $max = $genhandler->length2;
514
        }
515
        $out = '<input required="required" type="password" class="flat" id="' . $this->confKey . '" name="' . $this->confKey . '" value="' . (GETPOST($this->confKey, 'alpha') ? GETPOST($this->confKey, 'alpha') : $this->fieldValue) . '"';
516
        if ($min) {
517
            $out .= ' minlength="' . $min . '"';
518
        }
519
        if ($max) {
520
            $out .= ' maxlength="' . $max . '"';
521
        }
522
        $out .= '>';
523
        return $out;
524
    }
525
526
527
528
    /**
529
     * generateInputFieldMultiSelect
530
     *
531
     * @return string
532
     */
533
    public function generateInputFieldMultiSelect()
534
    {
535
        $TSelected = array();
536
        if ($this->fieldValue) {
537
            $TSelected = explode(',', $this->fieldValue);
538
        }
539
540
        return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
541
    }
542
543
544
    /**
545
     * generateInputFieldSelect
546
     *
547
     * @return string
548
     */
549
    public function generateInputFieldSelect()
550
    {
551
        $s = '';
552
        if ($this->picto) {
553
            $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
554
        }
555
        $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
556
557
        return $s;
558
    }
559
560
    /**
561
     * @return string
562
     */
563
    public function generateInputFieldSelectUser()
564
    {
565
        return $this->form->select_dolusers($this->fieldValue, $this->confKey);
566
    }
567
568
    /**
569
     * get the type : used for old module builder setup conf style conversion and tests
570
     * because this two class will quickly evolve it's important to not set or get directly $this->type (will be protected) so this method exist
571
     * to be sure we can manage evolution easily
572
     *
573
     * @return string
574
     */
575
    public function getType()
576
    {
577
        return $this->type;
578
    }
579
580
    /**
581
     * set the type from string : used for old module builder setup conf style conversion and tests
582
     * because this two class will quickly evolve it's important to not set directly $this->type (will be protected) so this method exist
583
     * to be sure we can manage evolution easily
584
     *
585
     * @param       string  $type   Possible values based on old module builder setup : 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type'
586
     * @deprecated                  this setTypeFromTypeString came deprecated because it exists only for manage setup conversion
587
     * @return      bool
588
     */
589
    public function setTypeFromTypeString($type)
590
    {
591
        $this->type = $type;
592
593
        return true;
594
    }
595
596
    /**
597
     * Add error
598
     *
599
     * @param array|string $errors the error text
600
     * @return null
601
     */
602
    public function setErrors($errors)
603
    {
604
        if (is_array($errors)) {
605
            if (!empty($errors)) {
606
                foreach ($errors as $error) {
607
                    $this->setErrors($error);
608
                }
609
            }
610
        } elseif (!empty($errors)) {
611
            $this->errors[] = $errors;
612
        }
613
        return null;
614
    }
615
616
    /**
617
     * generateOutputField
618
     *
619
     * @return bool|string      Generate the output html for this item
620
     */
621
    public function generateOutputField()
622
    {
623
        global $conf, $user, $langs;
624
625
        if (!empty($this->fieldOverride)) {
626
            return $this->fieldOverride;
627
        }
628
629
        if (!empty($this->fieldOutputOverride)) {
630
            return $this->fieldOutputOverride;
631
        }
632
633
        $out = '';
634
635
        if ($this->type == 'title') {
636
            // nothing to do
637
        } elseif ($this->type == 'textarea') {
638
            $out .= dol_nl2br($this->fieldValue);
639
        } elseif ($this->type == 'multiselect') {
640
            $out .= $this->generateOutputFieldMultiSelect();
641
        } elseif ($this->type == 'select') {
642
            $out .= $this->generateOutputFieldSelect();
643
        } elseif ($this->type == 'selectUser') {
644
            $out .= $this->generateOutputFieldSelectUser();
645
        } elseif ($this->type == 'html') {
646
            $out .=  $this->fieldValue;
647
        } elseif ($this->type == 'color') {
648
            $out .=  $this->generateOutputFieldColor();
649
        } elseif ($this->type == 'yesno') {
650
            if (!empty($conf->use_javascript_ajax)) {
651
                $out .= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
652
            } else {
653
                if ($this->fieldValue == 1) {
654
                    $out .= $langs->trans('yes');
655
                } else {
656
                    $out .= $langs->trans('no');
657
                }
658
            }
659
        } elseif (preg_match('/emailtemplate:/', $this->type)) {
660
            if ($this->fieldValue > 0) {
661
                include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
662
                $formmail = new FormMail($this->db);
663
664
                $tmp = explode(':', $this->type);
665
666
                $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
667
                if (is_numeric($template) && $template < 0) {
668
                    $this->setErrors($formmail->errors);
669
                }
670
                $out .= $this->langs->trans($template->label);
671
            }
672
        } elseif (preg_match('/category:/', $this->type)) {
673
            $c = new Categorie($this->db);
674
            $result = $c->fetch($this->fieldValue);
675
            if ($result < 0) {
676
                $this->setErrors($c->errors);
677
            }
678
            $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
679
            $toprint = array();
680
            foreach ($ways as $way) {
681
                $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
682
            }
683
            $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
684
        } elseif (preg_match('/thirdparty_type/', $this->type)) {
685
            if ($this->fieldValue == 2) {
686
                $out .= $this->langs->trans("Prospect");
687
            } elseif ($this->fieldValue == 3) {
688
                $out .= $this->langs->trans("ProspectCustomer");
689
            } elseif ($this->fieldValue == 1) {
690
                $out .= $this->langs->trans("Customer");
691
            } elseif ($this->fieldValue == 0) {
692
                $out .= $this->langs->trans("NorProspectNorCustomer");
693
            }
694
        } elseif ($this->type == 'product') {
695
696
            $product = new Product($this->db);
697
            $resprod = $product->fetch($this->fieldValue);
698
            if ($resprod > 0) {
699
                $out .= $product->ref;
700
            } elseif ($resprod < 0) {
701
                $this->setErrors($product->errors);
702
            }
703
        } elseif ($this->type == 'selectBankAccount') {
704
705
            $bankaccount = new Account($this->db);
706
            $resbank = $bankaccount->fetch($this->fieldValue);
707
            if ($resbank > 0) {
708
                $out .= $bankaccount->label;
709
            } elseif ($resbank < 0) {
710
                $this->setErrors($bankaccount->errors);
711
            }
712
        } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
713
            $out .= str_repeat('*', strlen($this->fieldValue));
714
        } else {
715
            $out .= $this->fieldValue;
716
        }
717
718
        return $out;
719
    }
720
721
722
    /**
723
     * generateOutputFieldMultiSelect
724
     *
725
     * @return string
726
     */
727
    public function generateOutputFieldMultiSelect()
728
    {
729
        $outPut = '';
730
        $TSelected = array();
731
        if (!empty($this->fieldValue)) {
732
            $TSelected = explode(',', $this->fieldValue);
733
        }
734
735
        if (!empty($TSelected)) {
736
            foreach ($TSelected as $selected) {
737
                if (!empty($this->fieldOptions[$selected])) {
738
                    $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info') . ' ';
739
                }
740
            }
741
        }
742
        return $outPut;
743
    }
744
745
    /**
746
     * generateOutputFieldColor
747
     *
748
     * @return string
749
     */
750
    public function generateOutputFieldColor()
751
    {
752
        global $langs;
753
        $this->fieldAttr['disabled'] = null;
754
        $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
755
        if ($color) {
756
            return '<input type="text" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; background-color: #' . $color . '" value="' . $color . '">';
757
        }
758
        return $langs->trans("Default");
759
    }
760
    /**
761
     * generateInputFieldColor
762
     *
763
     * @return string
764
     */
765
    public function generateInputFieldColor()
766
    {
767
        $this->fieldAttr['type'] = 'color';
768
        $default = $this->defaultFieldValue;
769
        include_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
770
        $formother = new FormOther($this->db);
771
        return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default) . ' ';
772
    }
773
774
    /**
775
     * generateOutputFieldSelect
776
     *
777
     * @return string
778
     */
779
    public function generateOutputFieldSelect()
780
    {
781
        $outPut = '';
782
        if (!empty($this->fieldOptions[$this->fieldValue])) {
783
            $outPut = $this->fieldOptions[$this->fieldValue];
784
        }
785
786
        return $outPut;
787
    }
788
789
    /**
790
     * generateOutputFieldSelectUser
791
     *
792
     * @return string
793
     */
794
    public function generateOutputFieldSelectUser()
795
    {
796
        $outPut = '';
797
        $user = new User($this->db);
798
        $user->fetch($this->fieldValue);
799
        $outPut = $user->firstname . " " . $user->lastname;
800
        return $outPut;
801
    }
802
803
    /*
804
     * METHODS FOR SETTING DISPLAY TYPE
805
     */
806
807
    /**
808
     * Set type of input as string
809
     *
810
     * @return self
811
     */
812
    public function setAsString()
813
    {
814
        $this->type = 'string';
815
        return $this;
816
    }
817
818
    /**
819
     * Set type of input as color
820
     *
821
     * @return self
822
     */
823
    public function setAsColor()
824
    {
825
        $this->type = 'color';
826
        return $this;
827
    }
828
829
    /**
830
     * Set type of input as textarea
831
     *
832
     * @return self
833
     */
834
    public function setAsTextarea()
835
    {
836
        $this->type = 'textarea';
837
        return $this;
838
    }
839
840
    /**
841
     * Set type of input as html editor
842
     *
843
     * @return self
844
     */
845
    public function setAsHtml()
846
    {
847
        $this->type = 'html';
848
        return $this;
849
    }
850
851
    /**
852
     * Set type of input as emailtemplate selector
853
     *
854
     * @param string $templateType email template type
855
     * @return self
856
     */
857
    public function setAsEmailTemplate($templateType)
858
    {
859
        $this->type = 'emailtemplate:' . $templateType;
860
        return $this;
861
    }
862
863
    /**
864
     * Set type of input as thirdparty_type selector
865
     *
866
     * @return self
867
     */
868
    public function setAsThirdpartyType()
869
    {
870
        $this->type = 'thirdparty_type';
871
        return $this;
872
    }
873
874
    /**
875
     * Set type of input as Yes
876
     *
877
     * @return self
878
     */
879
    public function setAsYesNo()
880
    {
881
        $this->type = 'yesno';
882
        return $this;
883
    }
884
885
    /**
886
     * Set type of input as secure key
887
     *
888
     * @return self
889
     */
890
    public function setAsSecureKey()
891
    {
892
        $this->type = 'securekey';
893
        return $this;
894
    }
895
896
    /**
897
     * Set type of input as product
898
     *
899
     * @return self
900
     */
901
    public function setAsProduct()
902
    {
903
        $this->type = 'product';
904
        return $this;
905
    }
906
907
    /**
908
     * Set type of input as a category selector
909
     * TODO add default value
910
     *
911
     * @param   int     $catType        Type of category ('customer', 'supplier', 'contact', 'product', 'member'). Old mode (0, 1, 2, ...) is deprecated.
912
     * @return self
913
     */
914
    public function setAsCategory($catType)
915
    {
916
        $this->type = 'category:' . $catType;
917
        return $this;
918
    }
919
920
    /**
921
     * Set type of input as a simple title. No data to store
922
     *
923
     * @return self
924
     */
925
    public function setAsTitle()
926
    {
927
        $this->type = 'title';
928
        return $this;
929
    }
930
931
932
    /**
933
     * Set type of input as a simple title. No data to store
934
     *
935
     * @param array $fieldOptions A table of field options
936
     * @return self
937
     */
938
    public function setAsMultiSelect($fieldOptions)
939
    {
940
        if (is_array($fieldOptions)) {
941
            $this->fieldOptions = $fieldOptions;
942
        }
943
944
        $this->type = 'multiselect';
945
        return $this;
946
    }
947
948
    /**
949
     * Set type of input as a simple title. No data to store
950
     *
951
     * @param array $fieldOptions  A table of field options
952
     * @return self
953
     */
954
    public function setAsSelect($fieldOptions)
955
    {
956
        if (is_array($fieldOptions)) {
957
            $this->fieldOptions = $fieldOptions;
958
        }
959
960
        $this->type = 'select';
961
        return $this;
962
    }
963
964
    /**
965
     * Set type of input as a simple title. No data to store
966
     *
967
     * @return self
968
     */
969
    public function setAsSelectUser()
970
    {
971
        $this->type = 'selectUser';
972
        return $this;
973
    }
974
975
    /**
976
     * Set type of input as a simple title. No data to store
977
     *
978
     * @return self
979
     */
980
    public function setAsSelectBankAccount()
981
    {
982
        $this->type = 'selectBankAccount';
983
        return $this;
984
    }
985
986
    /**
987
     * Set type of input as a password with dolibarr password rules apply.
988
     * Hide entry on display.
989
     *
990
     * @return self
991
     */
992
    public function setAsPassword()
993
    {
994
        $this->type = 'password';
995
        return $this;
996
    }
997
998
    /**
999
     * Set type of input as a generic password without dolibarr password rules (for external passwords for example).
1000
     * Hide entry on display.
1001
     *
1002
     * @return self
1003
     */
1004
    public function setAsGenericPassword()
1005
    {
1006
        $this->type = 'genericpassword';
1007
        return $this;
1008
    }
1009
}
1010