dolibarr 20.0.0
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
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
326 $this->setupNotEmpty++;
327 $out .= '<tr class="'.$trClass.'">';
328
329 $out .= '<td class="col-setup-title">';
330 $out .= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
331 $out .= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
332 $out .= '</span>';
333 $out .= '</td>';
334
335 $out .= '<td>';
336
337 if ($editMode) {
338 $out .= $item->generateInputField();
339 } else {
340 $out .= $item->generateOutputField();
341 }
342
343 if (!empty($item->errors)) {
344 // TODO : move set event message in a methode to be called by cards not by this class
345 setEventMessages(null, $item->errors, 'errors');
346 }
347
348 $out .= '</td>';
349 $out .= '</tr>';
350 }
351
352 return $out;
353 }
354
355
362 public function addItemsFromParamsArray($params)
363 {
364 if (!is_array($params) || empty($params)) {
365 return false;
366 }
367 foreach ($params as $confKey => $param) {
368 $this->addItemFromParams($confKey, $param); // todo manage error
369 }
370 return true;
371 }
372
373
382 public function addItemFromParams($confKey, $params)
383 {
384 if (empty($confKey) || empty($params['type'])) {
385 return false;
386 }
387
388 /*
389 * Example from old module builder setup page
390 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
391 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
392 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
393 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
394 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
395 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
396 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
397 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
398 */
399
400 $item = new FormSetupItem($confKey);
401 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developer to use object oriented usage
402 // @phan-suppress-next-line PhanDeprecatedFunction
403 $item->setTypeFromTypeString($params['type']);
404
405 if (!empty($params['enabled'])) {
406 $item->enabled = $params['enabled'];
407 }
408
409 if (!empty($params['css'])) {
410 $item->cssClass = $params['css'];
411 }
412
413 $this->items[$item->confKey] = $item;
414
415 return true;
416 }
417
424 public function exportItemsAsParamsArray()
425 {
426 $arrayofparameters = array();
427 foreach ($this->items as $item) {
428 $arrayofparameters[$item->confKey] = array(
429 'type' => $item->getType(),
430 'enabled' => $item->enabled
431 );
432 }
433
434 return $arrayofparameters;
435 }
436
443 public function reloadConfs()
444 {
445 if (!array($this->items)) {
446 return false;
447 }
448 foreach ($this->items as $item) {
449 $item->loadValueFromConf();
450 }
451
452 return true;
453 }
454
455
465 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
466 {
467 $item = new FormSetupItem($confKey);
468
469 $item->entity = $this->entity;
470
471 // set item rank if not defined as last item
472 if (empty($item->rank)) {
473 $item->rank = $this->getCurentItemMaxRank() + 1;
474 $this->setItemMaxRank($item->rank); // set new max rank if needed
475 }
476
477 // try to get rank from target column, this will override item->rank
478 if (!empty($targetItemKey)) {
479 if (isset($this->items[$targetItemKey])) {
480 $targetItem = $this->items[$targetItemKey];
481 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
482 if ($targetItem->rank >= 0 && $insertAfterTarget) {
483 $item->rank++;
484 }
485 }
486
487 // calc new rank for each item to make place for new item
488 foreach ($this->items as $fItem) {
489 if ($item->rank <= $fItem->rank) {
490 $fItem->rank = $fItem->rank + 1;
491 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
492 }
493 }
494 }
495
496 $this->items[$item->confKey] = $item;
497 return $this->items[$item->confKey];
498 }
499
505 public function sortingItems()
506 {
507 // Sorting
508 return uasort($this->items, array($this, 'itemSort'));
509 }
510
517 public function getCurentItemMaxRank($cache = true)
518 {
519 if (empty($this->items)) {
520 return 0;
521 }
522
523 if ($cache && $this->maxItemRank > 0) {
524 return $this->maxItemRank;
525 }
526
527 $this->maxItemRank = 0;
528 foreach ($this->items as $item) {
529 $this->maxItemRank = max($this->maxItemRank, $item->rank);
530 }
531
532 return $this->maxItemRank;
533 }
534
535
542 public function setItemMaxRank($rank)
543 {
544 $this->maxItemRank = max($this->maxItemRank, $rank);
545 }
546
547
554 public function getLineRank($itemKey)
555 {
556 if (!isset($this->items[$itemKey]->rank)) {
557 return -1;
558 }
559 return $this->items[$itemKey]->rank;
560 }
561
562
570 public function itemSort(FormSetupItem $a, FormSetupItem $b)
571 {
572 if (empty($a->rank)) {
573 $a->rank = 0;
574 }
575 if (empty($b->rank)) {
576 $b->rank = 0;
577 }
578 if ($a->rank == $b->rank) {
579 return 0;
580 }
581 return ($a->rank < $b->rank) ? -1 : 1;
582 }
583}
584
585
590{
594 public $db;
595
597 public $langs;
598
600 public $entity;
601
603 public $form;
604
605
607 public $confKey;
608
610 public $nameText = false;
611
613 public $helpText = '';
614
616 public $picto = '';
617
619 public $fieldValue;
620
622 public $defaultFieldValue = null;
623
625 public $fieldAttr = array();
626
628 public $fieldOverride = false;
629
631 public $fieldInputOverride = false;
632
634 public $fieldOutputOverride = false;
635
637 public $rank = 0;
638
640 public $fieldOptions = array();
641
643 public $fieldParams = array();
644
647
650
654 public $errors = array();
655
662 protected $type = 'string';
663
664 public $enabled = 1;
665
669 public $cssClass = '';
670
676 public function __construct($confKey)
677 {
678 global $langs, $db, $conf, $form;
679 $this->db = $db;
680
681 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
682 $this->form = $form;
683 } else {
684 $this->form = new Form($this->db);
685 }
686
687 $this->langs = $langs;
688 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
689
690 $this->confKey = $confKey;
691 $this->loadValueFromConf();
692 }
693
699 public function loadValueFromConf()
700 {
701 global $conf;
702 if (isset($conf->global->{$this->confKey})) {
703 $this->fieldValue = getDolGlobalString($this->confKey);
704 return true;
705 } else {
706 $this->fieldValue = '';
707 return false;
708 }
709 }
710
717 public function reloadValueFromConf()
718 {
719 return $this->loadValueFromConf();
720 }
721
722
728 public function saveConfValue()
729 {
730 global $hookmanager;
731
732 $parameters = array();
733 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
734 if ($reshook < 0) {
735 $this->setErrors($hookmanager->errors);
736 return -1;
737 }
738
739 if ($reshook > 0) {
740 return $reshook;
741 }
742
743
744 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
745 return call_user_func($this->saveCallBack, $this);
746 }
747
748 // Modify constant only if key was posted (avoid resetting key to the null value)
749 if ($this->type != 'title') {
750 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
751 if ($result < 0) {
752 return -1;
753 } else {
754 return 1;
755 }
756 }
757
758 return 0;
759 }
760
767 public function setSaveCallBack(callable $callBack)
768 {
769 $this->saveCallBack = $callBack;
770 }
771
778 public function setValueFromPostCallBack(callable $callBack)
779 {
780 $this->setValueFromPostCallBack = $callBack;
781 }
782
788 public function setValueFromPost()
789 {
790 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
791 return call_user_func($this->setValueFromPostCallBack);
792 }
793
794 // Modify constant only if key was posted (avoid resetting key to the null value)
795 if ($this->type != 'title') {
796 if (preg_match('/category:/', $this->type)) {
797 if (GETPOSTINT($this->confKey) == '-1') {
798 $val_const = '';
799 } else {
800 $val_const = GETPOSTINT($this->confKey);
801 }
802 } elseif ($this->type == 'multiselect') {
803 $val = GETPOST($this->confKey, 'array');
804 if ($val && is_array($val)) {
805 $val_const = implode(',', $val);
806 } else {
807 $val_const = '';
808 }
809 } elseif ($this->type == 'html') {
810 $val_const = GETPOST($this->confKey, 'restricthtml');
811 } else {
812 $val_const = GETPOST($this->confKey, 'alpha');
813 }
814
815 // TODO add value check with class validate
816 $this->fieldValue = $val_const;
817
818 return 1;
819 }
820
821 return 0;
822 }
823
829 public function getHelpText()
830 {
831 if (!empty($this->helpText)) {
832 return $this->helpText;
833 }
834 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
835 }
836
842 public function getNameText()
843 {
844 if (!empty($this->nameText)) {
845 return $this->nameText;
846 }
847 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
848
849 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
850 if (isModEnabled('multicompany') && $this->entity == 0) {
851 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
852 }
853
854 return $out;
855 }
856
862 public function generateInputField()
863 {
864 global $conf;
865
866 if (!empty($this->fieldOverride)) {
867 return $this->fieldOverride;
868 }
869
870 if (!empty($this->fieldInputOverride)) {
871 return $this->fieldInputOverride;
872 }
873
874 // Set default value
875 if (is_null($this->fieldValue)) {
876 $this->fieldValue = $this->defaultFieldValue;
877 }
878
879
880 $this->fieldAttr['name'] = $this->confKey;
881 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
882 $this->fieldAttr['value'] = $this->fieldValue;
883
884 $out = '';
885
886 if ($this->type == 'title') {
887 $out .= $this->generateOutputField(); // title have no input
888 } elseif ($this->type == 'multiselect') {
889 $out .= $this->generateInputFieldMultiSelect();
890 } elseif ($this->type == 'select') {
891 $out .= $this->generateInputFieldSelect();
892 } elseif ($this->type == 'selectUser') {
893 $out .= $this->generateInputFieldSelectUser();
894 } elseif ($this->type == 'textarea') {
895 $out .= $this->generateInputFieldTextarea();
896 } elseif ($this->type == 'html') {
897 $out .= $this->generateInputFieldHtml();
898 } elseif ($this->type == 'color') {
899 $out .= $this->generateInputFieldColor();
900 } elseif ($this->type == 'yesno') {
901 if (!empty($conf->use_javascript_ajax)) {
902 $out .= ajax_constantonoff($this->confKey);
903 } else {
904 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
905 }
906 } elseif (preg_match('/emailtemplate:/', $this->type)) {
907 $out .= $this->generateInputFieldEmailTemplate();
908 } elseif (preg_match('/category:/', $this->type)) {
909 $out .= $this->generateInputFieldCategories();
910 } elseif (preg_match('/thirdparty_type/', $this->type)) {
911 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
912 $formcompany = new FormCompany($this->db);
913 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
914 } elseif ($this->type == 'securekey') {
915 $out .= $this->generateInputFieldSecureKey();
916 } elseif ($this->type == 'product') {
917 if (isModEnabled("product") || isModEnabled("service")) {
918 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
919 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
920 }
921 } elseif ($this->type == 'selectBankAccount') {
922 if (isModEnabled("bank")) {
923 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
924 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
925 }
926 } elseif ($this->type == 'password') {
927 $out .= $this->generateInputFieldPassword('dolibarr');
928 } elseif ($this->type == 'genericpassword') {
929 $out .= $this->generateInputFieldPassword('generic');
930 } else {
931 $out .= $this->generateInputFieldText();
932 }
933
934 return $out;
935 }
936
942 public function generateInputFieldText()
943 {
944 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
945 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
946 }
947 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
948 }
949
956 {
957 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
958 $out .= dol_htmlentities($this->fieldValue);
959 $out .= "</textarea>\n";
960 return $out;
961 }
962
968 public function generateInputFieldHtml()
969 {
970 global $conf;
971 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
972 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
973 return $doleditor->Create(1);
974 }
975
982 {
983 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
984 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
985 $formother = new FormOther($this->db);
986
987 $tmp = explode(':', $this->type);
988 $out = img_picto('', 'category', 'class="pictofixedwidth"');
989
990 $label = 'Categories';
991 if ($this->type == 'customer') {
992 $label = 'CustomersProspectsCategoriesShort';
993 }
994 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
995
996 return $out;
997 }
998
1004 {
1005 global $user;
1006
1007 $out = '';
1008 if (preg_match('/emailtemplate:/', $this->type)) {
1009 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1010 $formmail = new FormMail($this->db);
1011
1012 $tmp = explode(':', $this->type);
1013 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1014 $arrayOfMessageName = array();
1015 if (is_array($formmail->lines_model)) {
1016 foreach ($formmail->lines_model as $modelMail) {
1017 $moreonlabel = '';
1018 if (!empty($arrayOfMessageName[$modelMail->label])) {
1019 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1020 }
1021 // The 'label' is the key that is unique if we exclude the language
1022 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1023 }
1024 }
1025 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1026 }
1027
1028 return $out;
1029 }
1030
1031
1038 {
1039 global $conf;
1040 $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).'">';
1041
1042 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1043 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1044
1045 // Add button to autosuggest a key
1046 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1047 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1048 }
1049
1050 return $out;
1051 }
1052
1053
1061 public function generateInputFieldPassword($type = 'generic')
1062 {
1063 global $conf, $langs, $user;
1064
1065 $min = 6;
1066 $max = 50;
1067 if ($type == 'dolibarr') {
1068 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1069 if ($gen == 'none') {
1070 $gen = 'standard';
1071 }
1072 $nomclass = "modGeneratePass".ucfirst($gen);
1073 $nomfichier = $nomclass.".class.php";
1074 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1075 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1076 $min = $genhandler->length;
1077 $max = $genhandler->length2;
1078 }
1079 $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).'"';
1080 if ($min) {
1081 $out .= ' minlength="' . $min . '"';
1082 }
1083 if ($max) {
1084 $out .= ' maxlength="' . $max . '"';
1085 }
1086 $out .= '>';
1087 return $out;
1088 }
1089
1090
1091
1098 {
1099 $TSelected = array();
1100 if ($this->fieldValue) {
1101 $TSelected = explode(',', $this->fieldValue);
1102 }
1103
1104 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1105 }
1106
1107
1114 {
1115 $s = '';
1116 if ($this->picto) {
1117 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1118 }
1119 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1120
1121 return $s;
1122 }
1123
1128 {
1129 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1130 }
1131
1139 public function getType()
1140 {
1141 return $this->type;
1142 }
1143
1153 public function setTypeFromTypeString($type)
1154 {
1155 $this->type = $type;
1156
1157 return true;
1158 }
1159
1166 public function setErrors($errors)
1167 {
1168 if (is_array($errors)) {
1169 if (!empty($errors)) {
1170 foreach ($errors as $error) {
1171 $this->setErrors($error);
1172 }
1173 }
1174 } elseif (!empty($errors)) {
1175 $this->errors[] = $errors;
1176 }
1177 return null;
1178 }
1179
1185 public function generateOutputField()
1186 {
1187 global $conf, $user, $langs;
1188
1189 if (!empty($this->fieldOverride)) {
1190 return $this->fieldOverride;
1191 }
1192
1193 if (!empty($this->fieldOutputOverride)) {
1194 return $this->fieldOutputOverride;
1195 }
1196
1197 $out = '';
1198
1199 if ($this->type == 'title') {
1200 // nothing to do
1201 } elseif ($this->type == 'textarea') {
1202 $out .= dol_nl2br($this->fieldValue);
1203 } elseif ($this->type == 'multiselect') {
1204 $out .= $this->generateOutputFieldMultiSelect();
1205 } elseif ($this->type == 'select') {
1206 $out .= $this->generateOutputFieldSelect();
1207 } elseif ($this->type == 'selectUser') {
1208 $out .= $this->generateOutputFieldSelectUser();
1209 } elseif ($this->type == 'html') {
1210 $out .= $this->fieldValue;
1211 } elseif ($this->type == 'color') {
1212 $out .= $this->generateOutputFieldColor();
1213 } elseif ($this->type == 'yesno') {
1214 if (!empty($conf->use_javascript_ajax)) {
1215 $out .= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1216 } else {
1217 if ($this->fieldValue == 1) {
1218 $out .= $langs->trans('yes');
1219 } else {
1220 $out .= $langs->trans('no');
1221 }
1222 }
1223 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1224 if ($this->fieldValue > 0) {
1225 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1226 $formmail = new FormMail($this->db);
1227
1228 $tmp = explode(':', $this->type);
1229
1230 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1231 if (is_numeric($template) && $template < 0) {
1232 $this->setErrors($formmail->errors);
1233 }
1234 $out .= $this->langs->trans($template->label);
1235 }
1236 } elseif (preg_match('/category:/', $this->type)) {
1237 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1238 $c = new Categorie($this->db);
1239 $result = $c->fetch($this->fieldValue);
1240 if ($result < 0) {
1241 $this->setErrors($c->errors);
1242 }
1243 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1244 $toprint = array();
1245 foreach ($ways as $way) {
1246 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1247 }
1248 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1249 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1250 if ($this->fieldValue == 2) {
1251 $out .= $this->langs->trans("Prospect");
1252 } elseif ($this->fieldValue == 3) {
1253 $out .= $this->langs->trans("ProspectCustomer");
1254 } elseif ($this->fieldValue == 1) {
1255 $out .= $this->langs->trans("Customer");
1256 } elseif ($this->fieldValue == 0) {
1257 $out .= $this->langs->trans("NorProspectNorCustomer");
1258 }
1259 } elseif ($this->type == 'product') {
1260 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1261
1262 $product = new Product($this->db);
1263 $resprod = $product->fetch($this->fieldValue);
1264 if ($resprod > 0) {
1265 $out .= $product->ref;
1266 } elseif ($resprod < 0) {
1267 $this->setErrors($product->errors);
1268 }
1269 } elseif ($this->type == 'selectBankAccount') {
1270 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1271
1272 $bankaccount = new Account($this->db);
1273 $resbank = $bankaccount->fetch($this->fieldValue);
1274 if ($resbank > 0) {
1275 $out .= $bankaccount->label;
1276 } elseif ($resbank < 0) {
1277 $this->setErrors($bankaccount->errors);
1278 }
1279 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1280 $out .= str_repeat('*', strlen($this->fieldValue));
1281 } else {
1282 $out .= $this->fieldValue;
1283 }
1284
1285 return $out;
1286 }
1287
1288
1295 {
1296 $outPut = '';
1297 $TSelected = array();
1298 if (!empty($this->fieldValue)) {
1299 $TSelected = explode(',', $this->fieldValue);
1300 }
1301
1302 if (!empty($TSelected)) {
1303 foreach ($TSelected as $selected) {
1304 if (!empty($this->fieldOptions[$selected])) {
1305 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1306 }
1307 }
1308 }
1309 return $outPut;
1310 }
1311
1318 {
1319 global $langs;
1320 $this->fieldAttr['disabled'] = null;
1321 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1322 if ($color) {
1323 return '<input type="text" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; background-color: #'.$color.'" value="'.$color.'">';
1324 }
1325 return $langs->trans("Default");
1326 }
1332 public function generateInputFieldColor()
1333 {
1334 $this->fieldAttr['type'] = 'color';
1335 $default = $this->defaultFieldValue;
1336 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1337 $formother = new FormOther($this->db);
1338 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1339 }
1340
1347 {
1348 $outPut = '';
1349 if (!empty($this->fieldOptions[$this->fieldValue])) {
1350 $outPut = $this->fieldOptions[$this->fieldValue];
1351 }
1352
1353 return $outPut;
1354 }
1355
1362 {
1363 $outPut = '';
1364 $user = new User($this->db);
1365 $user->fetch($this->fieldValue);
1366 $outPut = $user->firstname . " " . $user->lastname;
1367 return $outPut;
1368 }
1369
1370 /*
1371 * METHODS FOR SETTING DISPLAY TYPE
1372 */
1373
1379 public function setAsString()
1380 {
1381 $this->type = 'string';
1382 return $this;
1383 }
1384
1390 public function setAsColor()
1391 {
1392 $this->type = 'color';
1393 return $this;
1394 }
1395
1401 public function setAsTextarea()
1402 {
1403 $this->type = 'textarea';
1404 return $this;
1405 }
1406
1412 public function setAsHtml()
1413 {
1414 $this->type = 'html';
1415 return $this;
1416 }
1417
1424 public function setAsEmailTemplate($templateType)
1425 {
1426 $this->type = 'emailtemplate:'.$templateType;
1427 return $this;
1428 }
1429
1435 public function setAsThirdpartyType()
1436 {
1437 $this->type = 'thirdparty_type';
1438 return $this;
1439 }
1440
1446 public function setAsYesNo()
1447 {
1448 $this->type = 'yesno';
1449 return $this;
1450 }
1451
1457 public function setAsSecureKey()
1458 {
1459 $this->type = 'securekey';
1460 return $this;
1461 }
1462
1468 public function setAsProduct()
1469 {
1470 $this->type = 'product';
1471 return $this;
1472 }
1473
1481 public function setAsCategory($catType)
1482 {
1483 $this->type = 'category:'.$catType;
1484 return $this;
1485 }
1486
1492 public function setAsTitle()
1493 {
1494 $this->type = 'title';
1495 return $this;
1496 }
1497
1498
1505 public function setAsMultiSelect($fieldOptions)
1506 {
1507 if (is_array($fieldOptions)) {
1508 $this->fieldOptions = $fieldOptions;
1509 }
1510
1511 $this->type = 'multiselect';
1512 return $this;
1513 }
1514
1521 public function setAsSelect($fieldOptions)
1522 {
1523 if (is_array($fieldOptions)) {
1524 $this->fieldOptions = $fieldOptions;
1525 }
1526
1527 $this->type = 'select';
1528 return $this;
1529 }
1530
1536 public function setAsSelectUser()
1537 {
1538 $this->type = 'selectUser';
1539 return $this;
1540 }
1541
1547 public function setAsSelectBankAccount()
1548 {
1549 $this->type = 'selectBankAccount';
1550 return $this;
1551 }
1552
1559 public function setAsPassword()
1560 {
1561 $this->type = 'password';
1562 return $this;
1563 }
1564
1571 public function setAsGenericPassword()
1572 {
1573 $this->type = 'genericpassword';
1574 return $this;
1575 }
1576}
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:139
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.