dolibarr 22.0.5
html.formsetup.class.php
1<?php
2/* Copyright (C) 2021 John BOTELLA <john.botella@atm-consulting.fr>
3 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25{
29 public $db;
30
32 public $entity;
33
35 public $items = array();
36
40 public $setupNotEmpty = 0;
41
43 public $langs;
44
46 public $form;
47
49 protected $maxItemRank;
50
55 public $htmlBeforeOutputForm = '';
56
61 public $htmlAfterOutputForm = '';
62
67 public $htmlOutputMoreButton = '';
68
69
73 public $formAttributes = array(
74 'action' => '', // set in __construct
75 'method' => 'POST'
76 );
77
82 public $formHiddenInputs = array();
83
87 public $errors = array();
88
89
96 public function __construct($db, $outputLangs = null)
97 {
98 global $conf, $langs;
99
100 $this->db = $db;
101
102 $this->form = new Form($this->db);
103 $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
104
105 $this->formHiddenInputs['token'] = newToken();
106 $this->formHiddenInputs['action'] = 'update';
107
108 $this->entity = (is_null($this->entity) ? $conf->entity : $this->entity);
109
110 if ($outputLangs) {
111 $this->langs = $outputLangs;
112 } else {
113 $this->langs = $langs;
114 }
115 }
116
123 public static function generateAttributesStringFromArray($attributes)
124 {
125 $Aattr = array();
126 if (is_array($attributes)) {
127 foreach ($attributes as $attribute => $value) {
128 if (is_array($value) || is_object($value)) {
129 continue;
130 }
131 $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
132 }
133 }
134
135 return !empty($Aattr) ? implode(' ', $Aattr) : '';
136 }
137
138
146 public function generateOutput($editMode = false, $hideTitle = false)
147 {
148 global $hookmanager, $action;
149
150 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
151
152 $parameters = array(
153 'editMode' => $editMode
154 );
155 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
156 if ($reshook < 0) {
157 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
158 }
159
160 if ($reshook > 0) {
161 return $hookmanager->resPrint;
162 } else {
163 $out = '<!-- Start generateOutput from FormSetup class -->';
164 $out .= $this->htmlBeforeOutputForm;
165
166 if ($editMode) {
167 $out .= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
168
169 // generate hidden values from $this->formHiddenInputs
170 if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
171 foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
172 $out .= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
173 }
174 }
175 }
176
177 // generate output table
178 $out .= $this->generateTableOutput($editMode, $hideTitle);
179
180
181 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
182 if ($reshook < 0) {
183 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
184 }
185
186 if ($reshook > 0) {
187 return $hookmanager->resPrint;
188 } elseif ($editMode) {
189 $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
190 $out .= $this->htmlOutputMoreButton;
191 $out .= '<input class="button button-save reposition" type="submit" value="' . $this->langs->trans("Save") . '">'; // Todo fix dolibarr style for <button and use <button instead of input
192 /*$out .= ' &nbsp;&nbsp; ';
193 $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$this->langs->trans('Cancel').'</a>';
194 */
195 $out .= '</div>';
196 }
197
198 if ($editMode) {
199 $out .= '</form>';
200 }
201
202 $out .= $this->htmlAfterOutputForm;
203
204 return $out;
205 }
206 }
207
215 public function generateTableOutput($editMode = false, $hideTitle = false)
216 {
217 global $hookmanager, $action;
218 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
219
220 $parameters = array(
221 'editMode' => $editMode
222 );
223 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
224 if ($reshook < 0) {
225 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
226 }
227
228 if ($reshook > 0) {
229 return $hookmanager->resPrint;
230 } else {
231 $out = '<table class="noborder centpercent">';
232 if (empty($hideTitle)) {
233 $out .= '<thead>';
234 $out .= '<tr class="liste_titre">';
235 $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
236 $out .= ' <td></td>';
237 $out .= '</tr>';
238 $out .= '</thead>';
239 }
240
241 // Sort items before render
242 $this->sortingItems();
243
244 $out .= '<tbody>';
245 foreach ($this->items as $item) {
246 $out .= $this->generateLineOutput($item, $editMode);
247 }
248 $out .= '</tbody>';
249
250 $out .= '</table>';
251 return $out;
252 }
253 }
254
261 public function saveConfFromPost($noMessageInUpdate = false)
262 {
263 global $hookmanager, $conf;
264
265 $parameters = array();
266 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
267 if ($reshook < 0) {
268 $this->errors = $hookmanager->errors;
269 return -1;
270 }
271 if ($reshook > 0) {
272 return $reshook;
273 }
274
275 if (empty($this->items)) {
276 return null;
277 }
278
279 $this->db->begin();
280 $error = 0;
281 foreach ($this->items as $item) {
282 if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
283 continue;
284 }
285
286 $res = $item->setValueFromPost();
287 if ($res > 0) {
288 $item->saveConfValue();
289 } elseif ($res < 0) {
290 $error++;
291 break;
292 }
293 }
294
295 if (!$error) {
296 $this->db->commit();
297 if (empty($noMessageInUpdate)) {
298 setEventMessages($this->langs->trans("SetupSaved"), null);
299 }
300 return 1;
301 } else {
302 $this->db->rollback();
303 if (empty($noMessageInUpdate)) {
304 setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
305 }
306 return -1;
307 }
308 }
309
317 public function generateLineOutput($item, $editMode = false)
318 {
319 $out = '';
320 if ($item->enabled == 1) {
321 $trClass = 'oddeven';
322 if ($item->getType() == 'title') {
323 $trClass = 'liste_titre';
324 }
325 if (!empty($item->fieldParams['trClass'])) {
326 $trClass .= ' '.$item->fieldParams['trClass'];
327 }
328
329 $this->setupNotEmpty++;
330 $out .= '<tr class="'.$trClass.'">';
331
332 $out .= '<td class="col-setup-title'.(!empty($item->fieldParams['isMandatory']) ? ' fieldrequired' : '').'">';
333 $out .= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
334 $out .= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, empty($item->fieldParams['helpText']) ? 'tootips'.$item->confKey : ($item->fieldParams['helpText'] != 'noclick' ? $item->fieldParams['helpText'] : ''));
335 $out .= '</span>';
336 $out .= '</td>';
337
338 $out .= '<td>';
339
340 if ($editMode) {
341 $out .= $item->generateInputField();
342 } else {
343 $out .= $item->generateOutputField();
344 }
345
346 if (!empty($item->errors)) {
347 // TODO : move set event message in a methode to be called by cards not by this class
348 setEventMessages(null, $item->errors, 'errors');
349 }
350
351 $out .= '</td>';
352 $out .= '</tr>';
353 }
354
355 return $out;
356 }
357
358
365 public function addItemsFromParamsArray($params)
366 {
367 if (!is_array($params) || empty($params)) {
368 return false;
369 }
370 foreach ($params as $confKey => $param) {
371 $this->addItemFromParams($confKey, $param); // todo manage error
372 }
373 return true;
374 }
375
376
385 public function addItemFromParams($confKey, $params)
386 {
387 if (empty($confKey) || empty($params['type'])) {
388 return false;
389 }
390
391 /*
392 * Example from old module builder setup page
393 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
394 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
395 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
396 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
397 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
398 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
399 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
400 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
401 */
402
403 $item = new FormSetupItem($confKey);
404 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developer to use object oriented usage
405 // @phan-suppress-next-line PhanDeprecatedFunction
406 $item->setTypeFromTypeString((string) $params['type']);
407
408 if (!empty($params['enabled']) && is_numeric($params['enabled'])) {
409 $item->enabled = (int) $params['enabled'];
410 }
411
412 if (!empty($params['css'])) {
413 $item->cssClass = (string) $params['css'];
414 }
415
416 $this->items[$item->confKey] = $item;
417
418 return true;
419 }
420
427 public function exportItemsAsParamsArray()
428 {
429 $arrayofparameters = array();
430 foreach ($this->items as $item) {
431 $arrayofparameters[$item->confKey] = array(
432 'type' => $item->getType(),
433 'enabled' => $item->enabled
434 );
435 }
436
437 return $arrayofparameters;
438 }
439
446 public function reloadConfs()
447 {
448 if (!array($this->items)) {
449 return false;
450 }
451 foreach ($this->items as $item) {
452 $item->loadValueFromConf();
453 }
454
455 return true;
456 }
457
458
468 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
469 {
470 $item = new FormSetupItem($confKey);
471
472 $item->entity = $this->entity;
473
474 // set item rank if not defined as last item
475 if (empty($item->rank)) {
476 $item->rank = $this->getCurentItemMaxRank() + 1;
477 $this->setItemMaxRank($item->rank); // set new max rank if needed
478 }
479
480 // try to get rank from target column, this will override item->rank
481 if (!empty($targetItemKey)) {
482 if (isset($this->items[$targetItemKey])) {
483 $targetItem = $this->items[$targetItemKey];
484 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
485 if ($targetItem->rank >= 0 && $insertAfterTarget) {
486 $item->rank++;
487 }
488 }
489
490 // calc new rank for each item to make place for new item
491 foreach ($this->items as $fItem) {
492 if ($item->rank <= $fItem->rank) {
493 $fItem->rank += 1;
494 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
495 }
496 }
497 }
498
499 $this->items[$item->confKey] = $item;
500 return $this->items[$item->confKey];
501 }
502
508 public function sortingItems()
509 {
510 // Sorting
511 return uasort($this->items, array($this, 'itemSort'));
512 }
513
520 public function getCurentItemMaxRank($cache = true)
521 {
522 if (empty($this->items)) {
523 return 0;
524 }
525
526 if ($cache && $this->maxItemRank > 0) {
527 return $this->maxItemRank;
528 }
529
530 $this->maxItemRank = 0;
531 foreach ($this->items as $item) {
532 $this->maxItemRank = max($this->maxItemRank, $item->rank);
533 }
534
535 return $this->maxItemRank;
536 }
537
538
545 public function setItemMaxRank($rank)
546 {
547 $this->maxItemRank = max($this->maxItemRank, $rank);
548 }
549
550
557 public function getLineRank($itemKey)
558 {
559 if (!isset($this->items[$itemKey]->rank)) {
560 return -1;
561 }
562 return $this->items[$itemKey]->rank;
563 }
564
565
573 public function itemSort(FormSetupItem $a, FormSetupItem $b)
574 {
575 if (empty($a->rank)) {
576 $a->rank = 0;
577 }
578 if (empty($b->rank)) {
579 $b->rank = 0;
580 }
581 if ($a->rank == $b->rank) {
582 return 0;
583 }
584 return ($a->rank < $b->rank) ? -1 : 1;
585 }
586}
587
588
593{
597 public $db;
598
600 public $langs;
601
603 public $entity;
604
606 public $form;
607
608
610 public $confKey;
611
613 public $nameText = false;
614
616 public $helpText = '';
617
619 public $picto = '';
620
622 public $fieldValue;
623
625 public $defaultFieldValue = null;
626
628 public $fieldAttr = array();
629
631 public $fieldOverride = false;
632
634 public $fieldInputOverride = false;
635
637 public $fieldOutputOverride = false;
638
640 public $rank = 0;
641
643 public $fieldOptions = array();
644
646 public $fieldParams = array();
647
649 public $saveCallBack;
650
652 public $setValueFromPostCallBack;
653
657 public $errors = array();
658
665 protected $type = 'string';
666
667
669 public $enabled = 1;
670
674 public $cssClass = '';
675
676
682 public function __construct($confKey)
683 {
684 global $langs, $db, $conf, $form;
685 $this->db = $db;
686
687 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
688 $this->form = $form;
689 } else {
690 $this->form = new Form($this->db);
691 }
692
693 $this->langs = $langs;
694 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
695
696 $this->confKey = $confKey;
697 $this->loadValueFromConf();
698 }
699
705 public function loadValueFromConf()
706 {
707 global $conf;
708 if (isset($conf->global->{$this->confKey})) {
709 $this->fieldValue = getDolGlobalString($this->confKey);
710 return true;
711 } else {
712 $this->fieldValue = null;
713 return false;
714 }
715 }
716
723 public function reloadValueFromConf()
724 {
725 return $this->loadValueFromConf();
726 }
727
728
734 public function saveConfValue()
735 {
736 global $hookmanager;
737
738 $parameters = array();
739 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
740 if ($reshook < 0) {
741 $this->setErrors($hookmanager->errors);
742 return -1;
743 }
744
745 if ($reshook > 0) {
746 return $reshook;
747 }
748
749
750 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
751 return call_user_func($this->saveCallBack, $this);
752 }
753
754 // Modify constant only if key was posted (avoid resetting key to the null value)
755 if ($this->type != 'title') {
756 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
757 if ($result < 0) {
758 return -1;
759 } else {
760 return 1;
761 }
762 }
763
764 return 0;
765 }
766
773 public function setSaveCallBack(callable $callBack)
774 {
775 $this->saveCallBack = $callBack;
776 }
777
784 public function setValueFromPostCallBack(callable $callBack)
785 {
786 $this->setValueFromPostCallBack = $callBack;
787 }
788
794 public function setValueFromPost()
795 {
796 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
797 return call_user_func($this->setValueFromPostCallBack);
798 }
799
800 // Modify constant only if key was posted (avoid resetting key to the null value)
801 if ($this->type != 'title') {
802 if (preg_match('/category:/', $this->type)) {
803 if (GETPOSTINT($this->confKey) == '-1') {
804 $val_const = '';
805 } else {
806 $val_const = GETPOSTINT($this->confKey);
807 }
808 } elseif ($this->type == 'multiselect') {
809 $val = GETPOST($this->confKey, 'array');
810 if ($val && is_array($val)) {
811 $val_const = implode(',', $val);
812 } else {
813 $val_const = '';
814 }
815 } elseif ($this->type == 'html') {
816 $val_const = GETPOST($this->confKey, 'restricthtml');
817 } elseif ($this->type == 'email') {
818 $val_const = GETPOST($this->confKey, 'alphawithlgt');
819 } elseif ($this->type == 'number') {
820 $val_const = GETPOSTINT($this->confKey);
821 } else {
822 $val_const = GETPOST($this->confKey, 'alphanohtml');
823 }
824
825 // TODO add value check with class validate
826 $this->fieldValue = $val_const;
827
828 return 1;
829 }
830
831 return 0;
832 }
833
839 public function getHelpText()
840 {
841 if (!empty($this->helpText)) {
842 return $this->helpText;
843 }
844 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
845 }
846
852 public function getNameText()
853 {
854 if (!empty($this->nameText)) {
855 return $this->nameText;
856 }
857 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
858
859 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
860 if (isModEnabled('multicompany') && $this->entity == 0) {
861 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
862 }
863
864 return $out;
865 }
866
872 public function generateInputField()
873 {
874 global $conf;
875
876 if (!empty($this->fieldOverride)) {
877 return $this->fieldOverride;
878 }
879
880 if (!empty($this->fieldInputOverride)) {
881 return $this->fieldInputOverride;
882 }
883
884 // Set default value
885 if (is_null($this->fieldValue)) {
886 $this->fieldValue = $this->defaultFieldValue;
887 }
888
889
890 $this->fieldAttr['name'] = $this->confKey;
891 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
892 $this->fieldAttr['value'] = $this->fieldValue;
893
894 $out = '';
895
896 if ($this->type == 'title') {
897 $out .= $this->generateOutputField(); // title have no input
898 } elseif ($this->type == 'multiselect') {
899 $out .= $this->generateInputFieldMultiSelect();
900 } elseif ($this->type == 'select') {
901 $out .= $this->generateInputFieldSelect();
902 } elseif ($this->type == 'selectUser') {
903 $out .= $this->generateInputFieldSelectUser();
904 } elseif ($this->type == 'textarea') {
905 $out .= $this->generateInputFieldTextarea();
906 } elseif ($this->type == 'html') {
907 $out .= $this->generateInputFieldHtml();
908 } elseif ($this->type == 'color') {
909 $out .= $this->generateInputFieldColor();
910 } elseif ($this->type == 'yesno') {
911 if (!empty($conf->use_javascript_ajax)) {
912 $input = $this->fieldParams['input'] ?? array();
913 $revertonoff = !empty($this->fieldParams['revertonoff']) ? 1 : 0;
914 $forcereload = !empty($this->fieldParams['forcereload']) ? 1 : 0;
915
916 $out .= ajax_constantonoff($this->confKey, $input, $this->entity, $revertonoff, 0, $forcereload, 2, 0, 0, '', '', $this->cssClass);
917 } else {
918 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1, false, 0, 0, $this->cssClass);
919 }
920 } elseif (preg_match('/emailtemplate:/', $this->type)) {
921 $out .= $this->generateInputFieldEmailTemplate();
922 } elseif (preg_match('/category:/', $this->type)) {
923 $out .= $this->generateInputFieldCategories();
924 } elseif (preg_match('/thirdparty_type/', $this->type)) {
925 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
926 $formcompany = new FormCompany($this->db);
927 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
928 } elseif ($this->type == 'securekey') {
929 $out .= $this->generateInputFieldSecureKey();
930 } elseif ($this->type == 'product') {
931 if (isModEnabled("product") || isModEnabled("service")) {
932 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
933 $out .= $this->form->select_produits((int) $selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
934 }
935 } elseif ($this->type == 'selectBankAccount') {
936 if (isModEnabled("bank")) {
937 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
938 $out .= img_picto('', 'bank', 'class="pictofixedwidth"').$this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
939 }
940 } elseif ($this->type == 'password') {
941 $out .= $this->generateInputFieldPassword('dolibarr');
942 } elseif ($this->type == 'genericpassword') {
943 $out .= $this->generateInputFieldPassword('generic');
944 } else {
945 $out .= $this->generateInputFieldText();
946 }
947
948 return $out;
949 }
950
956 public function generateInputFieldText()
957 {
958 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
959 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
960 }
961 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
962 }
963
970 {
971 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
972 $out .= dol_htmlentities($this->fieldValue);
973 $out .= "</textarea>\n";
974 return $out;
975 }
976
982 public function generateInputFieldHtml()
983 {
984 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
985 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
986 return $doleditor->Create(1);
987 }
988
995 {
996 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
997 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
998 $formother = new FormOther($this->db);
999
1000 $tmp = explode(':', $this->type);
1001 $out = img_picto('', 'category', 'class="pictofixedwidth"');
1002
1003 $label = 'Categories';
1004 if ($this->type == 'customer') {
1005 $label = 'CustomersProspectsCategoriesShort';
1006 }
1007 $out .= $formother->select_categories($tmp[1], (int) $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
1008
1009 return $out;
1010 }
1011
1017 {
1018 global $user;
1019
1020 $out = '';
1021 if (preg_match('/emailtemplate:/', $this->type)) {
1022 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1023 $formmail = new FormMail($this->db);
1024
1025 $tmp = explode(':', $this->type);
1026 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1027 $arrayOfMessageName = array();
1028 if (is_array($formmail->lines_model)) {
1029 foreach ($formmail->lines_model as $modelMail) {
1030 $moreonlabel = '';
1031 if (!empty($arrayOfMessageName[$modelMail->label])) {
1032 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1033 }
1034 // The 'label' is the key that is unique if we exclude the language
1035 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1036 }
1037 }
1038 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1039 }
1040
1041 return $out;
1042 }
1043
1044
1051 {
1052 global $conf;
1053 $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).'">';
1054
1055 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1056 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1057
1058 // Add button to autosuggest a key
1059 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1060 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1061 }
1062
1063 return $out;
1064 }
1065
1066
1074 public function generateInputFieldPassword($type = 'generic')
1075 {
1076 global $conf, $langs, $user;
1077
1078 $min = 6;
1079 $max = 50;
1080 if ($type == 'dolibarr') {
1081 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1082 if ($gen == 'none') {
1083 $gen = 'standard';
1084 }
1085 $nomclass = "modGeneratePass".ucfirst($gen);
1086 $nomfichier = $nomclass.".class.php";
1087 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1088 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1089 $min = $genhandler->length;
1090 $max = $genhandler->length2;
1091 }
1092 $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).'"';
1093 if ($min) {
1094 $out .= ' minlength="' . $min . '"';
1095 }
1096 if ($max) {
1097 $out .= ' maxlength="' . $max . '"';
1098 }
1099 $out .= '>';
1100 return $out;
1101 }
1102
1103
1104
1111 {
1112 $TSelected = array();
1113 if ($this->fieldValue) {
1114 $TSelected = explode(',', $this->fieldValue);
1115 }
1116
1117 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1118 }
1119
1120
1127 {
1128 $s = '';
1129 if ($this->picto) {
1130 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1131 }
1132
1133 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1134
1135 return $s;
1136 }
1137
1142 {
1143 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1144 }
1145
1153 public function getType()
1154 {
1155 return $this->type;
1156 }
1157
1167 public function setTypeFromTypeString($type)
1168 {
1169 $this->type = $type;
1170
1171 return true;
1172 }
1173
1180 public function setErrors($errors)
1181 {
1182 if (is_array($errors)) {
1183 if (!empty($errors)) {
1184 foreach ($errors as $error) {
1185 $this->setErrors($error);
1186 }
1187 }
1188 } elseif (!empty($errors)) {
1189 $this->errors[] = $errors;
1190 }
1191 return null;
1192 }
1193
1199 public function generateOutputField()
1200 {
1201 global $conf, $user, $langs;
1202
1203 if (!empty($this->fieldOverride)) {
1204 return $this->fieldOverride;
1205 }
1206
1207 if (!empty($this->fieldOutputOverride)) {
1208 return $this->fieldOutputOverride;
1209 }
1210
1211 $out = '';
1212
1213 if ($this->type == 'title') {
1214 // nothing to do
1215 } elseif ($this->type == 'textarea') {
1216 $out .= dol_nl2br($this->fieldValue);
1217 } elseif ($this->type == 'multiselect') {
1218 $out .= $this->generateOutputFieldMultiSelect();
1219 } elseif ($this->type == 'select') {
1220 $out .= $this->generateOutputFieldSelect();
1221 } elseif ($this->type == 'selectUser') {
1222 $out .= $this->generateOutputFieldSelectUser();
1223 } elseif ($this->type == 'html') {
1224 $out .= $this->fieldValue;
1225 } elseif ($this->type == 'color') {
1226 $out .= $this->generateOutputFieldColor();
1227 } elseif ($this->type == 'yesno') {
1228 if (!empty($conf->use_javascript_ajax)) {
1229 $revertonoff = empty($this->fieldParams['revertonoff']) ? 0 : 1;
1230 $forcereload = empty($this->fieldParams['forcereload']) ? 0 : 1;
1231
1232 $out .= ajax_constantonoff($this->confKey, array(), $this->entity, $revertonoff, 0, $forcereload, 2, 0, 0, '', '', $this->cssClass); // TODO possibility to add $input parameter
1233 } else {
1234 if ($this->fieldValue == 1) {
1235 $out .= $langs->trans('yes');
1236 } else {
1237 $out .= $langs->trans('no');
1238 }
1239 }
1240 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1241 if ($this->fieldValue > 0) {
1242 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1243 $formmail = new FormMail($this->db);
1244
1245 $tmp = explode(':', $this->type);
1246
1247 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, (int) $this->fieldValue);
1248 if (is_numeric($template) && $template < 0) {
1249 $this->setErrors($formmail->errors);
1250 }
1251 $out .= $this->langs->trans($template->label);
1252 }
1253 } elseif (preg_match('/category:/', $this->type)) {
1254 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1255 $c = new Categorie($this->db);
1256 $result = $c->fetch((int) $this->fieldValue);
1257 if ($result < 0) {
1258 $this->setErrors($c->errors);
1259 }
1260 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1261 $toprint = array();
1262 foreach ($ways as $way) {
1263 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1264 }
1265 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1266 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1267 if ($this->fieldValue == 2) {
1268 $out .= $this->langs->trans("Prospect");
1269 } elseif ($this->fieldValue == 3) {
1270 $out .= $this->langs->trans("ProspectCustomer");
1271 } elseif ($this->fieldValue == 1) {
1272 $out .= $this->langs->trans("Customer");
1273 } elseif ($this->fieldValue == 0) {
1274 $out .= $this->langs->trans("NorProspectNorCustomer");
1275 }
1276 } elseif ($this->type == 'product') {
1277 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1278
1279 $product = new Product($this->db);
1280 $resprod = $product->fetch((int) $this->fieldValue);
1281 if ($resprod > 0) {
1282 $out .= $product->getNomUrl(1, '', 0, -1, 0, '', 1);
1283 } elseif ($resprod < 0) {
1284 $this->setErrors($product->errors);
1285 }
1286 } elseif ($this->type == 'selectBankAccount') {
1287 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1288
1289 $bankaccount = new Account($this->db);
1290 $resbank = $bankaccount->fetch((int) $this->fieldValue);
1291 if ($resbank > 0) {
1292 $out .= $bankaccount->label;
1293 } elseif ($resbank < 0) {
1294 $this->setErrors($bankaccount->errors);
1295 }
1296 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1297 $out .= str_repeat('*', strlen($this->fieldValue));
1298 } else {
1299 $out .= dolPrintHTML($this->fieldValue);
1300 }
1301
1302 return $out;
1303 }
1304
1305
1312 {
1313 $outPut = '';
1314 $TSelected = array();
1315 if (!empty($this->fieldValue)) {
1316 $TSelected = explode(',', $this->fieldValue);
1317 }
1318
1319 if (!empty($TSelected)) {
1320 foreach ($TSelected as $selected) {
1321 if (!empty($this->fieldOptions[$selected])) {
1322 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1323 }
1324 }
1325 }
1326 return $outPut;
1327 }
1328
1335 {
1336 global $langs;
1337 $out = '';
1338 $this->fieldAttr['disabled'] = null;
1339 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1340 $useDefaultColor = false;
1341 if (!$color && !empty($this->defaultFieldValue)) {
1342 $color = $this->defaultFieldValue;
1343 $useDefaultColor = true;
1344 }
1345 if ($color) {
1346 $out .= '<input type="color" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; " value="#'.$color.'">';
1347 }
1348
1349 if ($useDefaultColor) {
1350 $out .= ' '.$langs->trans("Default");
1351 } else {
1352 $out .= ' #'.$color;
1353 }
1354
1355 return $out;
1356 }
1362 public function generateInputFieldColor()
1363 {
1364 $this->fieldAttr['type'] = 'color';
1365 $default = $this->defaultFieldValue;
1366 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1367 $formother = new FormOther($this->db);
1368 return $formother->selectColor(colorArrayToHex(colorStringToArray((string) $this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', (string) $default).' ';
1369 }
1370
1377 {
1378 $outPut = '';
1379 if (!empty($this->fieldOptions[$this->fieldValue])) {
1380 $outPut = $this->fieldOptions[$this->fieldValue];
1381 }
1382
1383 return $outPut;
1384 }
1385
1392 {
1393 $outPut = '';
1394 $user = new User($this->db);
1395 $user->fetch((int) $this->fieldValue);
1396 $outPut = $user->firstname . " " . $user->lastname;
1397 return $outPut;
1398 }
1399
1400 /*
1401 * METHODS FOR SETTING DISPLAY TYPE
1402 */
1403
1409 public function setAsString()
1410 {
1411 $this->type = 'string';
1412 return $this;
1413 }
1414
1423 public function setAsNumber($min = null, $max = null, $step = null)
1424 {
1425 $this->type = 'number'; //for GETPOSTINT
1426 $this->fieldAttr['type'] = 'number'; //generic thanks to generateAttributesStringFromArray
1427 if (!is_null($min)) {
1428 $this->fieldAttr['min'] = $min;
1429 }
1430 if (!is_null($max)) {
1431 $this->fieldAttr['max'] = $max;
1432 }
1433 if (!is_null($step)) {
1434 $this->fieldAttr['step'] = $step;
1435 }
1436 return $this;
1437 }
1438
1439
1445 public function setAsEmail()
1446 {
1447 $this->type = 'email';
1448 return $this;
1449 }
1450
1456 public function setAsColor()
1457 {
1458 $this->type = 'color';
1459 return $this;
1460 }
1461
1467 public function setAsTextarea()
1468 {
1469 $this->type = 'textarea';
1470 return $this;
1471 }
1472
1478 public function setAsHtml()
1479 {
1480 $this->type = 'html';
1481 return $this;
1482 }
1483
1490 public function setAsEmailTemplate($templateType)
1491 {
1492 $this->type = 'emailtemplate:'.$templateType;
1493 return $this;
1494 }
1495
1501 public function setAsThirdpartyType()
1502 {
1503 $this->type = 'thirdparty_type';
1504 return $this;
1505 }
1506
1512 public function setAsYesNo()
1513 {
1514 $this->type = 'yesno';
1515 return $this;
1516 }
1517
1523 public function setAsSecureKey()
1524 {
1525 $this->type = 'securekey';
1526 return $this;
1527 }
1528
1534 public function setAsProduct()
1535 {
1536 $this->type = 'product';
1537 return $this;
1538 }
1539
1547 public function setAsCategory($catType)
1548 {
1549 $this->type = 'category:'.$catType;
1550 return $this;
1551 }
1552
1558 public function setAsTitle()
1559 {
1560 $this->type = 'title';
1561 return $this;
1562 }
1563
1564
1571 public function setAsMultiSelect($fieldOptions)
1572 {
1573 if (is_array($fieldOptions)) {
1574 $this->fieldOptions = $fieldOptions;
1575 }
1576
1577 $this->type = 'multiselect';
1578 return $this;
1579 }
1580
1587 public function setAsSelect($fieldOptions)
1588 {
1589 if (is_array($fieldOptions)) {
1590 $this->fieldOptions = $fieldOptions;
1591 }
1592
1593 $this->type = 'select';
1594 return $this;
1595 }
1596
1602 public function setAsSelectUser()
1603 {
1604 $this->type = 'selectUser';
1605 return $this;
1606 }
1607
1613 public function setAsSelectBankAccount()
1614 {
1615 $this->type = 'selectBankAccount';
1616 return $this;
1617 }
1618
1625 public function setAsPassword()
1626 {
1627 $this->type = 'password';
1628 return $this;
1629 }
1630
1637 public function setAsGenericPassword()
1638 {
1639 $this->type = 'genericpassword';
1640 return $this;
1641 }
1642}
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
$c
Definition line.php:331
Class to manage bank accounts.
Class to manage categories.
Class to manage a WYSIWYG editor.
Class to build HTML component for third parties management Only common components are here.
Class to manage generation of HTML components Only common components must be here.
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
Class permettant la generation de composants html autre Only common components are here.
This class help you create setup render.
sortingItems()
Sort items according to rank.
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
newItem($confKey, $targetItemKey='', $insertAfterTarget=false)
Create a new item The target is useful with hooks : that allow externals modules to add setup items o...
__construct($db, $outputLangs=null)
Constructor.
setItemMaxRank($rank)
set new max rank if needed
exportItemsAsParamsArray()
Used to export param array for /core/actions_setmoduleoptions.inc.php template Method exists only for...
getLineRank($itemKey)
get item position rank from item key
addItemsFromParamsArray($params)
Method used to test module builder conversion to this form usage.
addItemFromParams($confKey, $params)
From old Method was used to test module builder conversion to this form usage.
generateOutput($editMode=false, $hideTitle=false)
Generate the form (in read or edit mode depending on $editMode)
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
generateTableOutput($editMode=false, $hideTitle=false)
generateTableOutput
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
generateLineOutput($item, $editMode=false)
generateLineOutput
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
This class help to create item for class formSetup.
reloadValueFromConf()
Reload conf value from databases is an alias of loadValueFromConf.
setSaveCallBack(callable $callBack)
Set an override function for saving data.
generateInputFieldTextarea()
generate input field for textarea
setAsString()
Set type of input as string.
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
setAsSecureKey()
Set type of input as secure key.
generateInputFieldMultiSelect()
generateInputFieldMultiSelect
generateOutputFieldColor()
generateOutputFieldColor
generateOutputField()
generateOutputField
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
loadValueFromConf()
load conf value from databases
setAsHtml()
Set type of input as html editor.
generateInputField()
generate input field
generateOutputFieldMultiSelect()
generateOutputFieldMultiSelect
setAsColor()
Set type of input as color.
generateOutputFieldSelectUser()
generateOutputFieldSelectUser
generateOutputFieldSelect()
generateOutputFieldSelect
setErrors($errors)
Add error.
getType()
get the type : used for old module builder setup conf style conversion and tests because this two cla...
setAsTitle()
Set type of input as a simple title.
generateInputFieldText()
Generate default input field.
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
setAsSelect($fieldOptions)
Set type of input as a simple title.
setAsSelectUser()
Set type of input as a simple title.
setAsSelectBankAccount()
Set type of input as a simple title.
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
setAsMultiSelect($fieldOptions)
Set type of input as a simple title.
generateInputFieldCategories()
generate input field for categories
setAsProduct()
Set type of input as product.
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
setAsPassword()
Set type of input as a password with dolibarr password rules apply.
generateInputFieldEmailTemplate()
generate input field for email template selector
setTypeFromTypeString($type)
set the type from string : used for old module builder setup conf style conversion and tests because ...
generateInputFieldColor()
generateInputFieldColor
generateInputFieldHtml()
generate input field for html
setAsEmail()
Set type of input as string.
__construct($confKey)
Constructor.
setAsNumber($min=null, $max=null, $step=null)
Set type of input as number.
setAsGenericPassword()
Set type of input as a generic password without dolibarr password rules (for external passwords for e...
getNameText()
Get field name text or generate it.
setAsYesNo()
Set type of input as Yes.
generateInputFieldSecureKey()
generate input field for secure key
generateInputFieldSelect()
generateInputFieldSelect
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
getHelpText()
Get help text or generate it.
generateInputFieldPassword($type='generic')
generate input field for a password
setAsTextarea()
Set type of input as textarea.
Class to manage products or services.
Class to manage Dolibarr users.
colorArrayToHex($arraycolor, $colorifnotfound='888888')
Convert an array with RGB value into hex RGB value.
colorStringToArray($stringcolor, $colorifnotfound=array(88, 88, 88))
Convert a string RGB value ('FFFFFF', '255,255,255') into an array RGB array(255,255,...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dolPrintHTML($s, $allowiframe=0)
Return a string (that can be on several lines) ready to be output on a HTML page.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:158
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.