dolibarr 20.0.5
html.formsetup.class.php
1<?php
2/* Copyright (C) 2021 John BOTELLA <john.botella@atm-consulting.fr>
3 * Copyright (C) 2024 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
23{
27 public $db;
28
30 public $entity;
31
33 public $items = array();
34
38 public $setupNotEmpty = 0;
39
41 public $langs;
42
44 public $form;
45
47 protected $maxItemRank;
48
53 public $htmlBeforeOutputForm = '';
54
59 public $htmlAfterOutputForm = '';
60
65 public $htmlOutputMoreButton = '';
66
67
72 public $formAttributes = array(
73 'action' => '', // set in __construct
74 'method' => 'POST'
75 );
76
81 public $formHiddenInputs = array();
82
86 public $errors = array();
87
88
95 public function __construct($db, $outputLangs = null)
96 {
97 global $conf, $langs;
98
99 $this->db = $db;
100
101 $this->form = new Form($this->db);
102 $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
103
104 $this->formHiddenInputs['token'] = newToken();
105 $this->formHiddenInputs['action'] = 'update';
106
107 $this->entity = (is_null($this->entity) ? $conf->entity : $this->entity);
108
109 if ($outputLangs) {
110 $this->langs = $outputLangs;
111 } else {
112 $this->langs = $langs;
113 }
114 }
115
122 public static function generateAttributesStringFromArray($attributes)
123 {
124 $Aattr = array();
125 if (is_array($attributes)) {
126 foreach ($attributes as $attribute => $value) {
127 if (is_array($value) || is_object($value)) {
128 continue;
129 }
130 $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
131 }
132 }
133
134 return !empty($Aattr) ? implode(' ', $Aattr) : '';
135 }
136
137
145 public function generateOutput($editMode = false, $hideTitle = false)
146 {
147 global $hookmanager, $action;
148
149 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
150
151 $parameters = array(
152 'editMode' => $editMode
153 );
154 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
155 if ($reshook < 0) {
156 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
157 }
158
159 if ($reshook > 0) {
160 return $hookmanager->resPrint;
161 } else {
162 $out = '<!-- Start generateOutput from FormSetup class -->';
163 $out .= $this->htmlBeforeOutputForm;
164
165 if ($editMode) {
166 $out .= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
167
168 // generate hidden values from $this->formHiddenInputs
169 if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
170 foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
171 $out .= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
172 }
173 }
174 }
175
176 // generate output table
177 $out .= $this->generateTableOutput($editMode, $hideTitle);
178
179
180 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
181 if ($reshook < 0) {
182 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
183 }
184
185 if ($reshook > 0) {
186 return $hookmanager->resPrint;
187 } elseif ($editMode) {
188 $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
189 $out .= $this->htmlOutputMoreButton;
190 $out .= '<input class="button button-save" type="submit" value="' . $this->langs->trans("Save") . '">'; // Todo fix dolibarr style for <button and use <button instead of input
191 /*$out .= ' &nbsp;&nbsp; ';
192 $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$this->langs->trans('Cancel').'</a>';
193 */
194 $out .= '</div>';
195 }
196
197 if ($editMode) {
198 $out .= '</form>';
199 }
200
201 $out .= $this->htmlAfterOutputForm;
202
203 return $out;
204 }
205 }
206
214 public function generateTableOutput($editMode = false, $hideTitle = false)
215 {
216 global $hookmanager, $action;
217 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
218
219 $parameters = array(
220 'editMode' => $editMode
221 );
222 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
223 if ($reshook < 0) {
224 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
225 }
226
227 if ($reshook > 0) {
228 return $hookmanager->resPrint;
229 } else {
230 $out = '<table class="noborder centpercent">';
231 if (empty($hideTitle)) {
232 $out .= '<thead>';
233 $out .= '<tr class="liste_titre">';
234 $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
235 $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
236 $out .= '</tr>';
237 $out .= '</thead>';
238 }
239
240 // Sort items before render
241 $this->sortingItems();
242
243 $out .= '<tbody>';
244 foreach ($this->items as $item) {
245 $out .= $this->generateLineOutput($item, $editMode);
246 }
247 $out .= '</tbody>';
248
249 $out .= '</table>';
250 return $out;
251 }
252 }
253
260 public function saveConfFromPost($noMessageInUpdate = false)
261 {
262 global $hookmanager, $conf;
263
264 $parameters = array();
265 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
266 if ($reshook < 0) {
267 $this->errors = $hookmanager->errors;
268 return -1;
269 }
270 if ($reshook > 0) {
271 return $reshook;
272 }
273
274 if (empty($this->items)) {
275 return null;
276 }
277
278 $this->db->begin();
279 $error = 0;
280 foreach ($this->items as $item) {
281 if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
282 continue;
283 }
284
285 $res = $item->setValueFromPost();
286 if ($res > 0) {
287 $item->saveConfValue();
288 } elseif ($res < 0) {
289 $error++;
290 break;
291 }
292 }
293
294 if (!$error) {
295 $this->db->commit();
296 if (empty($noMessageInUpdate)) {
297 setEventMessages($this->langs->trans("SetupSaved"), null);
298 }
299 return 1;
300 } else {
301 $this->db->rollback();
302 if (empty($noMessageInUpdate)) {
303 setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
304 }
305 return -1;
306 }
307 }
308
316 public function generateLineOutput($item, $editMode = false)
317 {
318 $out = '';
319 if ($item->enabled == 1) {
320 $trClass = 'oddeven';
321 if ($item->getType() == 'title') {
322 $trClass = 'liste_titre';
323 }
324
325 $this->setupNotEmpty++;
326 $out .= '<tr class="'.$trClass.'">';
327
328 $out .= '<td class="col-setup-title">';
329 $out .= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
330 $out .= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
331 $out .= '</span>';
332 $out .= '</td>';
333
334 $out .= '<td>';
335
336 if ($editMode) {
337 $out .= $item->generateInputField();
338 } else {
339 $out .= $item->generateOutputField();
340 }
341
342 if (!empty($item->errors)) {
343 // TODO : move set event message in a methode to be called by cards not by this class
344 setEventMessages(null, $item->errors, 'errors');
345 }
346
347 $out .= '</td>';
348 $out .= '</tr>';
349 }
350
351 return $out;
352 }
353
354
361 public function addItemsFromParamsArray($params)
362 {
363 if (!is_array($params) || empty($params)) {
364 return false;
365 }
366 foreach ($params as $confKey => $param) {
367 $this->addItemFromParams($confKey, $param); // todo manage error
368 }
369 return true;
370 }
371
372
381 public function addItemFromParams($confKey, $params)
382 {
383 if (empty($confKey) || empty($params['type'])) {
384 return false;
385 }
386
387 /*
388 * Example from old module builder setup page
389 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
390 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
391 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
392 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
393 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
394 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
395 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
396 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
397 */
398
399 $item = new FormSetupItem($confKey);
400 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developer to use object oriented usage
401 // @phan-suppress-next-line PhanDeprecatedFunction
402 $item->setTypeFromTypeString($params['type']);
403
404 if (!empty($params['enabled'])) {
405 $item->enabled = $params['enabled'];
406 }
407
408 if (!empty($params['css'])) {
409 $item->cssClass = $params['css'];
410 }
411
412 $this->items[$item->confKey] = $item;
413
414 return true;
415 }
416
423 public function exportItemsAsParamsArray()
424 {
425 $arrayofparameters = array();
426 foreach ($this->items as $item) {
427 $arrayofparameters[$item->confKey] = array(
428 'type' => $item->getType(),
429 'enabled' => $item->enabled
430 );
431 }
432
433 return $arrayofparameters;
434 }
435
442 public function reloadConfs()
443 {
444 if (!array($this->items)) {
445 return false;
446 }
447 foreach ($this->items as $item) {
448 $item->loadValueFromConf();
449 }
450
451 return true;
452 }
453
454
464 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
465 {
466 $item = new FormSetupItem($confKey);
467
468 $item->entity = $this->entity;
469
470 // set item rank if not defined as last item
471 if (empty($item->rank)) {
472 $item->rank = $this->getCurentItemMaxRank() + 1;
473 $this->setItemMaxRank($item->rank); // set new max rank if needed
474 }
475
476 // try to get rank from target column, this will override item->rank
477 if (!empty($targetItemKey)) {
478 if (isset($this->items[$targetItemKey])) {
479 $targetItem = $this->items[$targetItemKey];
480 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
481 if ($targetItem->rank >= 0 && $insertAfterTarget) {
482 $item->rank++;
483 }
484 }
485
486 // calc new rank for each item to make place for new item
487 foreach ($this->items as $fItem) {
488 if ($item->rank <= $fItem->rank) {
489 $fItem->rank = $fItem->rank + 1;
490 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
491 }
492 }
493 }
494
495 $this->items[$item->confKey] = $item;
496 return $this->items[$item->confKey];
497 }
498
504 public function sortingItems()
505 {
506 // Sorting
507 return uasort($this->items, array($this, 'itemSort'));
508 }
509
516 public function getCurentItemMaxRank($cache = true)
517 {
518 if (empty($this->items)) {
519 return 0;
520 }
521
522 if ($cache && $this->maxItemRank > 0) {
523 return $this->maxItemRank;
524 }
525
526 $this->maxItemRank = 0;
527 foreach ($this->items as $item) {
528 $this->maxItemRank = max($this->maxItemRank, $item->rank);
529 }
530
531 return $this->maxItemRank;
532 }
533
534
541 public function setItemMaxRank($rank)
542 {
543 $this->maxItemRank = max($this->maxItemRank, $rank);
544 }
545
546
553 public function getLineRank($itemKey)
554 {
555 if (!isset($this->items[$itemKey]->rank)) {
556 return -1;
557 }
558 return $this->items[$itemKey]->rank;
559 }
560
561
569 public function itemSort(FormSetupItem $a, FormSetupItem $b)
570 {
571 if (empty($a->rank)) {
572 $a->rank = 0;
573 }
574 if (empty($b->rank)) {
575 $b->rank = 0;
576 }
577 if ($a->rank == $b->rank) {
578 return 0;
579 }
580 return ($a->rank < $b->rank) ? -1 : 1;
581 }
582}
583
584
589{
593 public $db;
594
596 public $langs;
597
599 public $entity;
600
602 public $form;
603
604
606 public $confKey;
607
609 public $nameText = false;
610
612 public $helpText = '';
613
615 public $picto = '';
616
618 public $fieldValue;
619
621 public $defaultFieldValue = null;
622
624 public $fieldAttr = array();
625
627 public $fieldOverride = false;
628
630 public $fieldInputOverride = false;
631
633 public $fieldOutputOverride = false;
634
636 public $rank = 0;
637
639 public $fieldOptions = array();
640
642 public $fieldParams = array();
643
646
649
653 public $errors = array();
654
661 protected $type = 'string';
662
663 public $enabled = 1;
664
668 public $cssClass = '';
669
675 public function __construct($confKey)
676 {
677 global $langs, $db, $conf, $form;
678 $this->db = $db;
679
680 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
681 $this->form = $form;
682 } else {
683 $this->form = new Form($this->db);
684 }
685
686 $this->langs = $langs;
687 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
688
689 $this->confKey = $confKey;
690 $this->loadValueFromConf();
691 }
692
698 public function loadValueFromConf()
699 {
700 global $conf;
701 if (isset($conf->global->{$this->confKey})) {
702 $this->fieldValue = getDolGlobalString($this->confKey);
703 return true;
704 } else {
705 $this->fieldValue = null;
706 return false;
707 }
708 }
709
716 public function reloadValueFromConf()
717 {
718 return $this->loadValueFromConf();
719 }
720
721
727 public function saveConfValue()
728 {
729 global $hookmanager;
730
731 $parameters = array();
732 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
733 if ($reshook < 0) {
734 $this->setErrors($hookmanager->errors);
735 return -1;
736 }
737
738 if ($reshook > 0) {
739 return $reshook;
740 }
741
742
743 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
744 return call_user_func($this->saveCallBack, $this);
745 }
746
747 // Modify constant only if key was posted (avoid resetting key to the null value)
748 if ($this->type != 'title') {
749 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
750 if ($result < 0) {
751 return -1;
752 } else {
753 return 1;
754 }
755 }
756
757 return 0;
758 }
759
766 public function setSaveCallBack(callable $callBack)
767 {
768 $this->saveCallBack = $callBack;
769 }
770
777 public function setValueFromPostCallBack(callable $callBack)
778 {
779 $this->setValueFromPostCallBack = $callBack;
780 }
781
787 public function setValueFromPost()
788 {
789 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
790 return call_user_func($this->setValueFromPostCallBack);
791 }
792
793 // Modify constant only if key was posted (avoid resetting key to the null value)
794 if ($this->type != 'title') {
795 if (preg_match('/category:/', $this->type)) {
796 if (GETPOSTINT($this->confKey) == '-1') {
797 $val_const = '';
798 } else {
799 $val_const = GETPOSTINT($this->confKey);
800 }
801 } elseif ($this->type == 'multiselect') {
802 $val = GETPOST($this->confKey, 'array');
803 if ($val && is_array($val)) {
804 $val_const = implode(',', $val);
805 } else {
806 $val_const = '';
807 }
808 } elseif ($this->type == 'html') {
809 $val_const = GETPOST($this->confKey, 'restricthtml');
810 } else {
811 $val_const = GETPOST($this->confKey, 'alpha');
812 }
813
814 // TODO add value check with class validate
815 $this->fieldValue = $val_const;
816
817 return 1;
818 }
819
820 return 0;
821 }
822
828 public function getHelpText()
829 {
830 if (!empty($this->helpText)) {
831 return $this->helpText;
832 }
833 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
834 }
835
841 public function getNameText()
842 {
843 if (!empty($this->nameText)) {
844 return $this->nameText;
845 }
846 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
847
848 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
849 if (isModEnabled('multicompany') && $this->entity == 0) {
850 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
851 }
852
853 return $out;
854 }
855
861 public function generateInputField()
862 {
863 global $conf;
864
865 if (!empty($this->fieldOverride)) {
866 return $this->fieldOverride;
867 }
868
869 if (!empty($this->fieldInputOverride)) {
870 return $this->fieldInputOverride;
871 }
872
873 // Set default value
874 if (is_null($this->fieldValue)) {
875 $this->fieldValue = $this->defaultFieldValue;
876 }
877
878
879 $this->fieldAttr['name'] = $this->confKey;
880 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
881 $this->fieldAttr['value'] = $this->fieldValue;
882
883 $out = '';
884
885 if ($this->type == 'title') {
886 $out .= $this->generateOutputField(); // title have no input
887 } elseif ($this->type == 'multiselect') {
888 $out .= $this->generateInputFieldMultiSelect();
889 } elseif ($this->type == 'select') {
890 $out .= $this->generateInputFieldSelect();
891 } elseif ($this->type == 'selectUser') {
892 $out .= $this->generateInputFieldSelectUser();
893 } elseif ($this->type == 'textarea') {
894 $out .= $this->generateInputFieldTextarea();
895 } elseif ($this->type == 'html') {
896 $out .= $this->generateInputFieldHtml();
897 } elseif ($this->type == 'color') {
898 $out .= $this->generateInputFieldColor();
899 } elseif ($this->type == 'yesno') {
900 if (!empty($conf->use_javascript_ajax)) {
901 $out .= ajax_constantonoff($this->confKey, array(), null, 0, 0, 0, 2, 0, 0, '', '', $this->cssClass);
902 } else {
903 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1, false, 0, 0, $this->cssClass);
904 }
905 } elseif (preg_match('/emailtemplate:/', $this->type)) {
906 $out .= $this->generateInputFieldEmailTemplate();
907 } elseif (preg_match('/category:/', $this->type)) {
908 $out .= $this->generateInputFieldCategories();
909 } elseif (preg_match('/thirdparty_type/', $this->type)) {
910 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
911 $formcompany = new FormCompany($this->db);
912 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
913 } elseif ($this->type == 'securekey') {
914 $out .= $this->generateInputFieldSecureKey();
915 } elseif ($this->type == 'product') {
916 if (isModEnabled("product") || isModEnabled("service")) {
917 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
918 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
919 }
920 } elseif ($this->type == 'selectBankAccount') {
921 if (isModEnabled("bank")) {
922 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
923 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
924 }
925 } elseif ($this->type == 'password') {
926 $out .= $this->generateInputFieldPassword('dolibarr');
927 } elseif ($this->type == 'genericpassword') {
928 $out .= $this->generateInputFieldPassword('generic');
929 } else {
930 $out .= $this->generateInputFieldText();
931 }
932
933 return $out;
934 }
935
941 public function generateInputFieldText()
942 {
943 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
944 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
945 }
946 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
947 }
948
955 {
956 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
957 $out .= dol_htmlentities($this->fieldValue);
958 $out .= "</textarea>\n";
959 return $out;
960 }
961
967 public function generateInputFieldHtml()
968 {
969 global $conf;
970 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
971 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
972 return $doleditor->Create(1);
973 }
974
981 {
982 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
983 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
984 $formother = new FormOther($this->db);
985
986 $tmp = explode(':', $this->type);
987 $out = img_picto('', 'category', 'class="pictofixedwidth"');
988
989 $label = 'Categories';
990 if ($this->type == 'customer') {
991 $label = 'CustomersProspectsCategoriesShort';
992 }
993 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
994
995 return $out;
996 }
997
1003 {
1004 global $user;
1005
1006 $out = '';
1007 if (preg_match('/emailtemplate:/', $this->type)) {
1008 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1009 $formmail = new FormMail($this->db);
1010
1011 $tmp = explode(':', $this->type);
1012 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1013 $arrayOfMessageName = array();
1014 if (is_array($formmail->lines_model)) {
1015 foreach ($formmail->lines_model as $modelMail) {
1016 $moreonlabel = '';
1017 if (!empty($arrayOfMessageName[$modelMail->label])) {
1018 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1019 }
1020 // The 'label' is the key that is unique if we exclude the language
1021 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1022 }
1023 }
1024 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1025 }
1026
1027 return $out;
1028 }
1029
1030
1037 {
1038 global $conf;
1039 $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).'">';
1040
1041 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1042 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1043
1044 // Add button to autosuggest a key
1045 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1046 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1047 }
1048
1049 return $out;
1050 }
1051
1052
1060 public function generateInputFieldPassword($type = 'generic')
1061 {
1062 global $conf, $langs, $user;
1063
1064 $min = 6;
1065 $max = 50;
1066 if ($type == 'dolibarr') {
1067 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1068 if ($gen == 'none') {
1069 $gen = 'standard';
1070 }
1071 $nomclass = "modGeneratePass".ucfirst($gen);
1072 $nomfichier = $nomclass.".class.php";
1073 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1074 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1075 $min = $genhandler->length;
1076 $max = $genhandler->length2;
1077 }
1078 $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).'"';
1079 if ($min) {
1080 $out .= ' minlength="' . $min . '"';
1081 }
1082 if ($max) {
1083 $out .= ' maxlength="' . $max . '"';
1084 }
1085 $out .= '>';
1086 return $out;
1087 }
1088
1089
1090
1097 {
1098 $TSelected = array();
1099 if ($this->fieldValue) {
1100 $TSelected = explode(',', $this->fieldValue);
1101 }
1102
1103 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1104 }
1105
1106
1113 {
1114 $s = '';
1115 if ($this->picto) {
1116 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1117 }
1118 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1119
1120 return $s;
1121 }
1122
1127 {
1128 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1129 }
1130
1138 public function getType()
1139 {
1140 return $this->type;
1141 }
1142
1152 public function setTypeFromTypeString($type)
1153 {
1154 $this->type = $type;
1155
1156 return true;
1157 }
1158
1165 public function setErrors($errors)
1166 {
1167 if (is_array($errors)) {
1168 if (!empty($errors)) {
1169 foreach ($errors as $error) {
1170 $this->setErrors($error);
1171 }
1172 }
1173 } elseif (!empty($errors)) {
1174 $this->errors[] = $errors;
1175 }
1176 return null;
1177 }
1178
1184 public function generateOutputField()
1185 {
1186 global $conf, $user, $langs;
1187
1188 if (!empty($this->fieldOverride)) {
1189 return $this->fieldOverride;
1190 }
1191
1192 if (!empty($this->fieldOutputOverride)) {
1193 return $this->fieldOutputOverride;
1194 }
1195
1196 $out = '';
1197
1198 if ($this->type == 'title') {
1199 // nothing to do
1200 } elseif ($this->type == 'textarea') {
1201 $out .= dol_nl2br($this->fieldValue);
1202 } elseif ($this->type == 'multiselect') {
1203 $out .= $this->generateOutputFieldMultiSelect();
1204 } elseif ($this->type == 'select') {
1205 $out .= $this->generateOutputFieldSelect();
1206 } elseif ($this->type == 'selectUser') {
1207 $out .= $this->generateOutputFieldSelectUser();
1208 } elseif ($this->type == 'html') {
1209 $out .= $this->fieldValue;
1210 } elseif ($this->type == 'color') {
1211 $out .= $this->generateOutputFieldColor();
1212 } elseif ($this->type == 'yesno') {
1213 if (!empty($conf->use_javascript_ajax)) {
1214 $out .= ajax_constantonoff($this->confKey, array(), $this->entity, 0, 0, 0, 2, 0, 0, '', '', $this->cssClass); // TODO possibility to add $input parameter
1215 } else {
1216 if ($this->fieldValue == 1) {
1217 $out .= $langs->trans('yes');
1218 } else {
1219 $out .= $langs->trans('no');
1220 }
1221 }
1222 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1223 if ($this->fieldValue > 0) {
1224 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1225 $formmail = new FormMail($this->db);
1226
1227 $tmp = explode(':', $this->type);
1228
1229 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1230 if (is_numeric($template) && $template < 0) {
1231 $this->setErrors($formmail->errors);
1232 }
1233 $out .= $this->langs->trans($template->label);
1234 }
1235 } elseif (preg_match('/category:/', $this->type)) {
1236 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1237 $c = new Categorie($this->db);
1238 $result = $c->fetch($this->fieldValue);
1239 if ($result < 0) {
1240 $this->setErrors($c->errors);
1241 }
1242 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1243 $toprint = array();
1244 foreach ($ways as $way) {
1245 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1246 }
1247 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1248 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1249 if ($this->fieldValue == 2) {
1250 $out .= $this->langs->trans("Prospect");
1251 } elseif ($this->fieldValue == 3) {
1252 $out .= $this->langs->trans("ProspectCustomer");
1253 } elseif ($this->fieldValue == 1) {
1254 $out .= $this->langs->trans("Customer");
1255 } elseif ($this->fieldValue == 0) {
1256 $out .= $this->langs->trans("NorProspectNorCustomer");
1257 }
1258 } elseif ($this->type == 'product') {
1259 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1260
1261 $product = new Product($this->db);
1262 $resprod = $product->fetch($this->fieldValue);
1263 if ($resprod > 0) {
1264 $out .= $product->ref;
1265 } elseif ($resprod < 0) {
1266 $this->setErrors($product->errors);
1267 }
1268 } elseif ($this->type == 'selectBankAccount') {
1269 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1270
1271 $bankaccount = new Account($this->db);
1272 $resbank = $bankaccount->fetch($this->fieldValue);
1273 if ($resbank > 0) {
1274 $out .= $bankaccount->label;
1275 } elseif ($resbank < 0) {
1276 $this->setErrors($bankaccount->errors);
1277 }
1278 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1279 $out .= str_repeat('*', strlen($this->fieldValue));
1280 } else {
1281 $out .= $this->fieldValue;
1282 }
1283
1284 return $out;
1285 }
1286
1287
1294 {
1295 $outPut = '';
1296 $TSelected = array();
1297 if (!empty($this->fieldValue)) {
1298 $TSelected = explode(',', $this->fieldValue);
1299 }
1300
1301 if (!empty($TSelected)) {
1302 foreach ($TSelected as $selected) {
1303 if (!empty($this->fieldOptions[$selected])) {
1304 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1305 }
1306 }
1307 }
1308 return $outPut;
1309 }
1310
1317 {
1318 global $langs;
1319 $out = '';
1320 $this->fieldAttr['disabled'] = null;
1321 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1322 $useDefaultColor = false;
1323 if (!$color && !empty($this->defaultFieldValue)) {
1324 $color = $this->defaultFieldValue;
1325 $useDefaultColor = true;
1326 }
1327 if ($color) {
1328 $out.= '<input type="color" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; " value="#'.$color.'">';
1329 }
1330
1331 if ($useDefaultColor) {
1332 $out.= ' '.$langs->trans("Default");
1333 } else {
1334 $out.= ' #'.$color;
1335 }
1336
1337 return $out;
1338 }
1344 public function generateInputFieldColor()
1345 {
1346 $this->fieldAttr['type'] = 'color';
1347 $default = $this->defaultFieldValue;
1348 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1349 $formother = new FormOther($this->db);
1350 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1351 }
1352
1359 {
1360 $outPut = '';
1361 if (!empty($this->fieldOptions[$this->fieldValue])) {
1362 $outPut = $this->fieldOptions[$this->fieldValue];
1363 }
1364
1365 return $outPut;
1366 }
1367
1374 {
1375 $outPut = '';
1376 $user = new User($this->db);
1377 $user->fetch($this->fieldValue);
1378 $outPut = $user->firstname . " " . $user->lastname;
1379 return $outPut;
1380 }
1381
1382 /*
1383 * METHODS FOR SETTING DISPLAY TYPE
1384 */
1385
1391 public function setAsString()
1392 {
1393 $this->type = 'string';
1394 return $this;
1395 }
1396
1402 public function setAsColor()
1403 {
1404 $this->type = 'color';
1405 return $this;
1406 }
1407
1413 public function setAsTextarea()
1414 {
1415 $this->type = 'textarea';
1416 return $this;
1417 }
1418
1424 public function setAsHtml()
1425 {
1426 $this->type = 'html';
1427 return $this;
1428 }
1429
1436 public function setAsEmailTemplate($templateType)
1437 {
1438 $this->type = 'emailtemplate:'.$templateType;
1439 return $this;
1440 }
1441
1447 public function setAsThirdpartyType()
1448 {
1449 $this->type = 'thirdparty_type';
1450 return $this;
1451 }
1452
1458 public function setAsYesNo()
1459 {
1460 $this->type = 'yesno';
1461 return $this;
1462 }
1463
1469 public function setAsSecureKey()
1470 {
1471 $this->type = 'securekey';
1472 return $this;
1473 }
1474
1480 public function setAsProduct()
1481 {
1482 $this->type = 'product';
1483 return $this;
1484 }
1485
1493 public function setAsCategory($catType)
1494 {
1495 $this->type = 'category:'.$catType;
1496 return $this;
1497 }
1498
1504 public function setAsTitle()
1505 {
1506 $this->type = 'title';
1507 return $this;
1508 }
1509
1510
1517 public function setAsMultiSelect($fieldOptions)
1518 {
1519 if (is_array($fieldOptions)) {
1520 $this->fieldOptions = $fieldOptions;
1521 }
1522
1523 $this->type = 'multiselect';
1524 return $this;
1525 }
1526
1533 public function setAsSelect($fieldOptions)
1534 {
1535 if (is_array($fieldOptions)) {
1536 $this->fieldOptions = $fieldOptions;
1537 }
1538
1539 $this->type = 'select';
1540 return $this;
1541 }
1542
1548 public function setAsSelectUser()
1549 {
1550 $this->type = 'selectUser';
1551 return $this;
1552 }
1553
1559 public function setAsSelectBankAccount()
1560 {
1561 $this->type = 'selectBankAccount';
1562 return $this;
1563 }
1564
1571 public function setAsPassword()
1572 {
1573 $this->type = 'password';
1574 return $this;
1575 }
1576
1583 public function setAsGenericPassword()
1584 {
1585 $this->type = 'genericpassword';
1586 return $this;
1587 }
1588}
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).
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()
generatec 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
__construct($confKey)
Constructor.
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,...
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return 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...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.