dolibarr 21.0.4
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 if (!empty($item->fieldParams['trClass'])) {
325 $trClass .= ' '.$item->fieldParams['trClass'];
326 }
327
328 $this->setupNotEmpty++;
329 $out .= '<tr class="'.$trClass.'">';
330
331 $out .= '<td class="col-setup-title">';
332 $out .= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
333 $out .= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
334 $out .= '</span>';
335 $out .= '</td>';
336
337 $out .= '<td>';
338
339 if ($editMode) {
340 $out .= $item->generateInputField();
341 } else {
342 $out .= $item->generateOutputField();
343 }
344
345 if (!empty($item->errors)) {
346 // TODO : move set event message in a methode to be called by cards not by this class
347 setEventMessages(null, $item->errors, 'errors');
348 }
349
350 $out .= '</td>';
351 $out .= '</tr>';
352 }
353
354 return $out;
355 }
356
357
364 public function addItemsFromParamsArray($params)
365 {
366 if (!is_array($params) || empty($params)) {
367 return false;
368 }
369 foreach ($params as $confKey => $param) {
370 $this->addItemFromParams($confKey, $param); // todo manage error
371 }
372 return true;
373 }
374
375
384 public function addItemFromParams($confKey, $params)
385 {
386 if (empty($confKey) || empty($params['type'])) {
387 return false;
388 }
389
390 /*
391 * Example from old module builder setup page
392 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
393 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
394 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
395 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
396 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
397 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
398 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
399 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
400 */
401
402 $item = new FormSetupItem($confKey);
403 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developer to use object oriented usage
404 // @phan-suppress-next-line PhanDeprecatedFunction
405 $item->setTypeFromTypeString($params['type']);
406
407 if (!empty($params['enabled']) && is_numeric($params['enabled'])) {
408 $item->enabled = (int) $params['enabled'];
409 }
410
411 if (!empty($params['css'])) {
412 $item->cssClass = (string) $params['css'];
413 }
414
415 $this->items[$item->confKey] = $item;
416
417 return true;
418 }
419
426 public function exportItemsAsParamsArray()
427 {
428 $arrayofparameters = array();
429 foreach ($this->items as $item) {
430 $arrayofparameters[$item->confKey] = array(
431 'type' => $item->getType(),
432 'enabled' => $item->enabled
433 );
434 }
435
436 return $arrayofparameters;
437 }
438
445 public function reloadConfs()
446 {
447 if (!array($this->items)) {
448 return false;
449 }
450 foreach ($this->items as $item) {
451 $item->loadValueFromConf();
452 }
453
454 return true;
455 }
456
457
467 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
468 {
469 $item = new FormSetupItem($confKey);
470
471 $item->entity = $this->entity;
472
473 // set item rank if not defined as last item
474 if (empty($item->rank)) {
475 $item->rank = $this->getCurentItemMaxRank() + 1;
476 $this->setItemMaxRank($item->rank); // set new max rank if needed
477 }
478
479 // try to get rank from target column, this will override item->rank
480 if (!empty($targetItemKey)) {
481 if (isset($this->items[$targetItemKey])) {
482 $targetItem = $this->items[$targetItemKey];
483 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
484 if ($targetItem->rank >= 0 && $insertAfterTarget) {
485 $item->rank++;
486 }
487 }
488
489 // calc new rank for each item to make place for new item
490 foreach ($this->items as $fItem) {
491 if ($item->rank <= $fItem->rank) {
492 $fItem->rank += 1;
493 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
494 }
495 }
496 }
497
498 $this->items[$item->confKey] = $item;
499 return $this->items[$item->confKey];
500 }
501
507 public function sortingItems()
508 {
509 // Sorting
510 return uasort($this->items, array($this, 'itemSort'));
511 }
512
519 public function getCurentItemMaxRank($cache = true)
520 {
521 if (empty($this->items)) {
522 return 0;
523 }
524
525 if ($cache && $this->maxItemRank > 0) {
526 return $this->maxItemRank;
527 }
528
529 $this->maxItemRank = 0;
530 foreach ($this->items as $item) {
531 $this->maxItemRank = max($this->maxItemRank, $item->rank);
532 }
533
534 return $this->maxItemRank;
535 }
536
537
544 public function setItemMaxRank($rank)
545 {
546 $this->maxItemRank = max($this->maxItemRank, $rank);
547 }
548
549
556 public function getLineRank($itemKey)
557 {
558 if (!isset($this->items[$itemKey]->rank)) {
559 return -1;
560 }
561 return $this->items[$itemKey]->rank;
562 }
563
564
572 public function itemSort(FormSetupItem $a, FormSetupItem $b)
573 {
574 if (empty($a->rank)) {
575 $a->rank = 0;
576 }
577 if (empty($b->rank)) {
578 $b->rank = 0;
579 }
580 if ($a->rank == $b->rank) {
581 return 0;
582 }
583 return ($a->rank < $b->rank) ? -1 : 1;
584 }
585}
586
587
592{
596 public $db;
597
599 public $langs;
600
602 public $entity;
603
605 public $form;
606
607
609 public $confKey;
610
612 public $nameText = false;
613
615 public $helpText = '';
616
618 public $picto = '';
619
621 public $fieldValue;
622
624 public $defaultFieldValue = null;
625
627 public $fieldAttr = array();
628
630 public $fieldOverride = false;
631
633 public $fieldInputOverride = false;
634
636 public $fieldOutputOverride = false;
637
639 public $rank = 0;
640
642 public $fieldOptions = array();
643
645 public $fieldParams = array();
646
649
652
656 public $errors = array();
657
664 protected $type = 'string';
665
666
668 public $enabled = 1;
669
673 public $cssClass = '';
674
675
681 public function __construct($confKey)
682 {
683 global $langs, $db, $conf, $form;
684 $this->db = $db;
685
686 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
687 $this->form = $form;
688 } else {
689 $this->form = new Form($this->db);
690 }
691
692 $this->langs = $langs;
693 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
694
695 $this->confKey = $confKey;
696 $this->loadValueFromConf();
697 }
698
704 public function loadValueFromConf()
705 {
706 global $conf;
707 if (isset($conf->global->{$this->confKey})) {
708 $this->fieldValue = getDolGlobalString($this->confKey);
709 return true;
710 } else {
711 $this->fieldValue = null;
712 return false;
713 }
714 }
715
722 public function reloadValueFromConf()
723 {
724 return $this->loadValueFromConf();
725 }
726
727
733 public function saveConfValue()
734 {
735 global $hookmanager;
736
737 $parameters = array();
738 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
739 if ($reshook < 0) {
740 $this->setErrors($hookmanager->errors);
741 return -1;
742 }
743
744 if ($reshook > 0) {
745 return $reshook;
746 }
747
748
749 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
750 return call_user_func($this->saveCallBack, $this);
751 }
752
753 // Modify constant only if key was posted (avoid resetting key to the null value)
754 if ($this->type != 'title') {
755 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
756 if ($result < 0) {
757 return -1;
758 } else {
759 return 1;
760 }
761 }
762
763 return 0;
764 }
765
772 public function setSaveCallBack(callable $callBack)
773 {
774 $this->saveCallBack = $callBack;
775 }
776
783 public function setValueFromPostCallBack(callable $callBack)
784 {
785 $this->setValueFromPostCallBack = $callBack;
786 }
787
793 public function setValueFromPost()
794 {
795 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
796 return call_user_func($this->setValueFromPostCallBack);
797 }
798
799 // Modify constant only if key was posted (avoid resetting key to the null value)
800 if ($this->type != 'title') {
801 if (preg_match('/category:/', $this->type)) {
802 if (GETPOSTINT($this->confKey) == '-1') {
803 $val_const = '';
804 } else {
805 $val_const = GETPOSTINT($this->confKey);
806 }
807 } elseif ($this->type == 'multiselect') {
808 $val = GETPOST($this->confKey, 'array');
809 if ($val && is_array($val)) {
810 $val_const = implode(',', $val);
811 } else {
812 $val_const = '';
813 }
814 } elseif ($this->type == 'html') {
815 $val_const = GETPOST($this->confKey, 'restricthtml');
816 } else {
817 $val_const = GETPOST($this->confKey, 'alpha');
818 }
819
820 // TODO add value check with class validate
821 $this->fieldValue = $val_const;
822
823 return 1;
824 }
825
826 return 0;
827 }
828
834 public function getHelpText()
835 {
836 if (!empty($this->helpText)) {
837 return $this->helpText;
838 }
839 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
840 }
841
847 public function getNameText()
848 {
849 if (!empty($this->nameText)) {
850 return $this->nameText;
851 }
852 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
853
854 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
855 if (isModEnabled('multicompany') && $this->entity == 0) {
856 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
857 }
858
859 return $out;
860 }
861
867 public function generateInputField()
868 {
869 global $conf;
870
871 if (!empty($this->fieldOverride)) {
872 return $this->fieldOverride;
873 }
874
875 if (!empty($this->fieldInputOverride)) {
876 return $this->fieldInputOverride;
877 }
878
879 // Set default value
880 if (is_null($this->fieldValue)) {
881 $this->fieldValue = $this->defaultFieldValue;
882 }
883
884
885 $this->fieldAttr['name'] = $this->confKey;
886 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
887 $this->fieldAttr['value'] = $this->fieldValue;
888
889 $out = '';
890
891 if ($this->type == 'title') {
892 $out .= $this->generateOutputField(); // title have no input
893 } elseif ($this->type == 'multiselect') {
894 $out .= $this->generateInputFieldMultiSelect();
895 } elseif ($this->type == 'select') {
896 $out .= $this->generateInputFieldSelect();
897 } elseif ($this->type == 'selectUser') {
898 $out .= $this->generateInputFieldSelectUser();
899 } elseif ($this->type == 'textarea') {
900 $out .= $this->generateInputFieldTextarea();
901 } elseif ($this->type == 'html') {
902 $out .= $this->generateInputFieldHtml();
903 } elseif ($this->type == 'color') {
904 $out .= $this->generateInputFieldColor();
905 } elseif ($this->type == 'yesno') {
906 if (!empty($conf->use_javascript_ajax)) {
907 $input = $this->fieldParams['input'] ?? array();
908 $revertonoff = !empty($this->fieldParams['revertonoff']) ? 1 : 0;
909 $forcereload = !empty($this->fieldParams['forcereload']) ? 1 : 0;
910
911 $out .= ajax_constantonoff($this->confKey, $input, $this->entity, $revertonoff, 0, $forcereload, 2, 0, 0, '', '', $this->cssClass);
912 } else {
913 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1, false, 0, 0, $this->cssClass);
914 }
915 } elseif (preg_match('/emailtemplate:/', $this->type)) {
916 $out .= $this->generateInputFieldEmailTemplate();
917 } elseif (preg_match('/category:/', $this->type)) {
918 $out .= $this->generateInputFieldCategories();
919 } elseif (preg_match('/thirdparty_type/', $this->type)) {
920 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
921 $formcompany = new FormCompany($this->db);
922 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
923 } elseif ($this->type == 'securekey') {
924 $out .= $this->generateInputFieldSecureKey();
925 } elseif ($this->type == 'product') {
926 if (isModEnabled("product") || isModEnabled("service")) {
927 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
928 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
929 }
930 } elseif ($this->type == 'selectBankAccount') {
931 if (isModEnabled("bank")) {
932 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
933 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
934 }
935 } elseif ($this->type == 'password') {
936 $out .= $this->generateInputFieldPassword('dolibarr');
937 } elseif ($this->type == 'genericpassword') {
938 $out .= $this->generateInputFieldPassword('generic');
939 } else {
940 $out .= $this->generateInputFieldText();
941 }
942
943 return $out;
944 }
945
951 public function generateInputFieldText()
952 {
953 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
954 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
955 }
956 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
957 }
958
965 {
966 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
967 $out .= dol_htmlentities($this->fieldValue);
968 $out .= "</textarea>\n";
969 return $out;
970 }
971
977 public function generateInputFieldHtml()
978 {
979 global $conf;
980 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
981 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
982 return $doleditor->Create(1);
983 }
984
991 {
992 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
993 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
994 $formother = new FormOther($this->db);
995
996 $tmp = explode(':', $this->type);
997 $out = img_picto('', 'category', 'class="pictofixedwidth"');
998
999 $label = 'Categories';
1000 if ($this->type == 'customer') {
1001 $label = 'CustomersProspectsCategoriesShort';
1002 }
1003 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
1004
1005 return $out;
1006 }
1007
1013 {
1014 global $user;
1015
1016 $out = '';
1017 if (preg_match('/emailtemplate:/', $this->type)) {
1018 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1019 $formmail = new FormMail($this->db);
1020
1021 $tmp = explode(':', $this->type);
1022 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1023 $arrayOfMessageName = array();
1024 if (is_array($formmail->lines_model)) {
1025 foreach ($formmail->lines_model as $modelMail) {
1026 $moreonlabel = '';
1027 if (!empty($arrayOfMessageName[$modelMail->label])) {
1028 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1029 }
1030 // The 'label' is the key that is unique if we exclude the language
1031 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1032 }
1033 }
1034 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1035 }
1036
1037 return $out;
1038 }
1039
1040
1047 {
1048 global $conf;
1049 $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).'">';
1050
1051 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1052 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1053
1054 // Add button to autosuggest a key
1055 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1056 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1057 }
1058
1059 return $out;
1060 }
1061
1062
1070 public function generateInputFieldPassword($type = 'generic')
1071 {
1072 global $conf, $langs, $user;
1073
1074 $min = 6;
1075 $max = 50;
1076 if ($type == 'dolibarr') {
1077 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1078 if ($gen == 'none') {
1079 $gen = 'standard';
1080 }
1081 $nomclass = "modGeneratePass".ucfirst($gen);
1082 $nomfichier = $nomclass.".class.php";
1083 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1084 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1085 $min = $genhandler->length;
1086 $max = $genhandler->length2;
1087 }
1088 $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).'"';
1089 if ($min) {
1090 $out .= ' minlength="' . $min . '"';
1091 }
1092 if ($max) {
1093 $out .= ' maxlength="' . $max . '"';
1094 }
1095 $out .= '>';
1096 return $out;
1097 }
1098
1099
1100
1107 {
1108 $TSelected = array();
1109 if ($this->fieldValue) {
1110 $TSelected = explode(',', $this->fieldValue);
1111 }
1112
1113 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1114 }
1115
1116
1123 {
1124 $s = '';
1125 if ($this->picto) {
1126 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1127 }
1128
1129 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1130
1131 return $s;
1132 }
1133
1138 {
1139 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1140 }
1141
1149 public function getType()
1150 {
1151 return $this->type;
1152 }
1153
1163 public function setTypeFromTypeString($type)
1164 {
1165 $this->type = $type;
1166
1167 return true;
1168 }
1169
1176 public function setErrors($errors)
1177 {
1178 if (is_array($errors)) {
1179 if (!empty($errors)) {
1180 foreach ($errors as $error) {
1181 $this->setErrors($error);
1182 }
1183 }
1184 } elseif (!empty($errors)) {
1185 $this->errors[] = $errors;
1186 }
1187 return null;
1188 }
1189
1195 public function generateOutputField()
1196 {
1197 global $conf, $user, $langs;
1198
1199 if (!empty($this->fieldOverride)) {
1200 return $this->fieldOverride;
1201 }
1202
1203 if (!empty($this->fieldOutputOverride)) {
1204 return $this->fieldOutputOverride;
1205 }
1206
1207 $out = '';
1208
1209 if ($this->type == 'title') {
1210 // nothing to do
1211 } elseif ($this->type == 'textarea') {
1212 $out .= dol_nl2br($this->fieldValue);
1213 } elseif ($this->type == 'multiselect') {
1214 $out .= $this->generateOutputFieldMultiSelect();
1215 } elseif ($this->type == 'select') {
1216 $out .= $this->generateOutputFieldSelect();
1217 } elseif ($this->type == 'selectUser') {
1218 $out .= $this->generateOutputFieldSelectUser();
1219 } elseif ($this->type == 'html') {
1220 $out .= $this->fieldValue;
1221 } elseif ($this->type == 'color') {
1222 $out .= $this->generateOutputFieldColor();
1223 } elseif ($this->type == 'yesno') {
1224 if (!empty($conf->use_javascript_ajax)) {
1225 $revertonoff = empty($this->fieldParams['revertonoff']) ? 0 : 1;
1226 $forcereload = empty($this->fieldParams['forcereload']) ? 0 : 1;
1227
1228 $out .= ajax_constantonoff($this->confKey, array(), $this->entity, $revertonoff, 0, $forcereload, 2, 0, 0, '', '', $this->cssClass); // TODO possibility to add $input parameter
1229 } else {
1230 if ($this->fieldValue == 1) {
1231 $out .= $langs->trans('yes');
1232 } else {
1233 $out .= $langs->trans('no');
1234 }
1235 }
1236 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1237 if ($this->fieldValue > 0) {
1238 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1239 $formmail = new FormMail($this->db);
1240
1241 $tmp = explode(':', $this->type);
1242
1243 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1244 if (is_numeric($template) && $template < 0) {
1245 $this->setErrors($formmail->errors);
1246 }
1247 $out .= $this->langs->trans($template->label);
1248 }
1249 } elseif (preg_match('/category:/', $this->type)) {
1250 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1251 $c = new Categorie($this->db);
1252 $result = $c->fetch($this->fieldValue);
1253 if ($result < 0) {
1254 $this->setErrors($c->errors);
1255 }
1256 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1257 $toprint = array();
1258 foreach ($ways as $way) {
1259 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1260 }
1261 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1262 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1263 if ($this->fieldValue == 2) {
1264 $out .= $this->langs->trans("Prospect");
1265 } elseif ($this->fieldValue == 3) {
1266 $out .= $this->langs->trans("ProspectCustomer");
1267 } elseif ($this->fieldValue == 1) {
1268 $out .= $this->langs->trans("Customer");
1269 } elseif ($this->fieldValue == 0) {
1270 $out .= $this->langs->trans("NorProspectNorCustomer");
1271 }
1272 } elseif ($this->type == 'product') {
1273 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1274
1275 $product = new Product($this->db);
1276 $resprod = $product->fetch($this->fieldValue);
1277 if ($resprod > 0) {
1278 $out .= $product->ref;
1279 } elseif ($resprod < 0) {
1280 $this->setErrors($product->errors);
1281 }
1282 } elseif ($this->type == 'selectBankAccount') {
1283 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1284
1285 $bankaccount = new Account($this->db);
1286 $resbank = $bankaccount->fetch($this->fieldValue);
1287 if ($resbank > 0) {
1288 $out .= $bankaccount->label;
1289 } elseif ($resbank < 0) {
1290 $this->setErrors($bankaccount->errors);
1291 }
1292 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1293 $out .= str_repeat('*', strlen($this->fieldValue));
1294 } else {
1295 $out .= $this->fieldValue;
1296 }
1297
1298 return $out;
1299 }
1300
1301
1308 {
1309 $outPut = '';
1310 $TSelected = array();
1311 if (!empty($this->fieldValue)) {
1312 $TSelected = explode(',', $this->fieldValue);
1313 }
1314
1315 if (!empty($TSelected)) {
1316 foreach ($TSelected as $selected) {
1317 if (!empty($this->fieldOptions[$selected])) {
1318 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1319 }
1320 }
1321 }
1322 return $outPut;
1323 }
1324
1331 {
1332 global $langs;
1333 $out = '';
1334 $this->fieldAttr['disabled'] = null;
1335 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1336 $useDefaultColor = false;
1337 if (!$color && !empty($this->defaultFieldValue)) {
1338 $color = $this->defaultFieldValue;
1339 $useDefaultColor = true;
1340 }
1341 if ($color) {
1342 $out.= '<input type="color" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; " value="#'.$color.'">';
1343 }
1344
1345 if ($useDefaultColor) {
1346 $out.= ' '.$langs->trans("Default");
1347 } else {
1348 $out.= ' #'.$color;
1349 }
1350
1351 return $out;
1352 }
1358 public function generateInputFieldColor()
1359 {
1360 $this->fieldAttr['type'] = 'color';
1361 $default = $this->defaultFieldValue;
1362 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1363 $formother = new FormOther($this->db);
1364 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1365 }
1366
1373 {
1374 $outPut = '';
1375 if (!empty($this->fieldOptions[$this->fieldValue])) {
1376 $outPut = $this->fieldOptions[$this->fieldValue];
1377 }
1378
1379 return $outPut;
1380 }
1381
1388 {
1389 $outPut = '';
1390 $user = new User($this->db);
1391 $user->fetch($this->fieldValue);
1392 $outPut = $user->firstname . " " . $user->lastname;
1393 return $outPut;
1394 }
1395
1396 /*
1397 * METHODS FOR SETTING DISPLAY TYPE
1398 */
1399
1405 public function setAsString()
1406 {
1407 $this->type = 'string';
1408 return $this;
1409 }
1410
1416 public function setAsColor()
1417 {
1418 $this->type = 'color';
1419 return $this;
1420 }
1421
1427 public function setAsTextarea()
1428 {
1429 $this->type = 'textarea';
1430 return $this;
1431 }
1432
1438 public function setAsHtml()
1439 {
1440 $this->type = 'html';
1441 return $this;
1442 }
1443
1450 public function setAsEmailTemplate($templateType)
1451 {
1452 $this->type = 'emailtemplate:'.$templateType;
1453 return $this;
1454 }
1455
1461 public function setAsThirdpartyType()
1462 {
1463 $this->type = 'thirdparty_type';
1464 return $this;
1465 }
1466
1472 public function setAsYesNo()
1473 {
1474 $this->type = 'yesno';
1475 return $this;
1476 }
1477
1483 public function setAsSecureKey()
1484 {
1485 $this->type = 'securekey';
1486 return $this;
1487 }
1488
1494 public function setAsProduct()
1495 {
1496 $this->type = 'product';
1497 return $this;
1498 }
1499
1507 public function setAsCategory($catType)
1508 {
1509 $this->type = 'category:'.$catType;
1510 return $this;
1511 }
1512
1518 public function setAsTitle()
1519 {
1520 $this->type = 'title';
1521 return $this;
1522 }
1523
1524
1531 public function setAsMultiSelect($fieldOptions)
1532 {
1533 if (is_array($fieldOptions)) {
1534 $this->fieldOptions = $fieldOptions;
1535 }
1536
1537 $this->type = 'multiselect';
1538 return $this;
1539 }
1540
1547 public function setAsSelect($fieldOptions)
1548 {
1549 if (is_array($fieldOptions)) {
1550 $this->fieldOptions = $fieldOptions;
1551 }
1552
1553 $this->type = 'select';
1554 return $this;
1555 }
1556
1562 public function setAsSelectUser()
1563 {
1564 $this->type = 'selectUser';
1565 return $this;
1566 }
1567
1573 public function setAsSelectBankAccount()
1574 {
1575 $this->type = 'selectBankAccount';
1576 return $this;
1577 }
1578
1585 public function setAsPassword()
1586 {
1587 $this->type = 'password';
1588 return $this;
1589 }
1590
1597 public function setAsGenericPassword()
1598 {
1599 $this->type = 'genericpassword';
1600 return $this;
1601 }
1602}
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:327
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
__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,...
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.
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:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:150
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.