dolibarr 21.0.0-alpha
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 if (!empty($item->fieldParams['trClass'])) {
326 $trClass .= ' '.$item->fieldParams['trClass'];
327 }
328
329 $this->setupNotEmpty++;
330 $out .= '<tr class="'.$trClass.'">';
331
332 $out .= '<td class="col-setup-title">';
333 $out .= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
334 $out .= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
335 $out .= '</span>';
336 $out .= '</td>';
337
338 $out .= '<td>';
339
340 if ($editMode) {
341 $out .= $item->generateInputField();
342 } else {
343 $out .= $item->generateOutputField();
344 }
345
346 if (!empty($item->errors)) {
347 // TODO : move set event message in a methode to be called by cards not by this class
348 setEventMessages(null, $item->errors, 'errors');
349 }
350
351 $out .= '</td>';
352 $out .= '</tr>';
353 }
354
355 return $out;
356 }
357
358
365 public function addItemsFromParamsArray($params)
366 {
367 if (!is_array($params) || empty($params)) {
368 return false;
369 }
370 foreach ($params as $confKey => $param) {
371 $this->addItemFromParams($confKey, $param); // todo manage error
372 }
373 return true;
374 }
375
376
385 public function addItemFromParams($confKey, $params)
386 {
387 if (empty($confKey) || empty($params['type'])) {
388 return false;
389 }
390
391 /*
392 * Example from old module builder setup page
393 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
394 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
395 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
396 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
397 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
398 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
399 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
400 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
401 */
402
403 $item = new FormSetupItem($confKey);
404 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developer to use object oriented usage
405 // @phan-suppress-next-line PhanDeprecatedFunction
406 $item->setTypeFromTypeString($params['type']);
407
408 if (!empty($params['enabled']) && is_numeric($params['enabled'])) {
409 $item->enabled = (int) $params['enabled'];
410 }
411
412 if (!empty($params['css'])) {
413 $item->cssClass = (string) $params['css'];
414 }
415
416 $this->items[$item->confKey] = $item;
417
418 return true;
419 }
420
427 public function exportItemsAsParamsArray()
428 {
429 $arrayofparameters = array();
430 foreach ($this->items as $item) {
431 $arrayofparameters[$item->confKey] = array(
432 'type' => $item->getType(),
433 'enabled' => $item->enabled
434 );
435 }
436
437 return $arrayofparameters;
438 }
439
446 public function reloadConfs()
447 {
448 if (!array($this->items)) {
449 return false;
450 }
451 foreach ($this->items as $item) {
452 $item->loadValueFromConf();
453 }
454
455 return true;
456 }
457
458
468 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
469 {
470 $item = new FormSetupItem($confKey);
471
472 $item->entity = $this->entity;
473
474 // set item rank if not defined as last item
475 if (empty($item->rank)) {
476 $item->rank = $this->getCurentItemMaxRank() + 1;
477 $this->setItemMaxRank($item->rank); // set new max rank if needed
478 }
479
480 // try to get rank from target column, this will override item->rank
481 if (!empty($targetItemKey)) {
482 if (isset($this->items[$targetItemKey])) {
483 $targetItem = $this->items[$targetItemKey];
484 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
485 if ($targetItem->rank >= 0 && $insertAfterTarget) {
486 $item->rank++;
487 }
488 }
489
490 // calc new rank for each item to make place for new item
491 foreach ($this->items as $fItem) {
492 if ($item->rank <= $fItem->rank) {
493 $fItem->rank += 1;
494 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
495 }
496 }
497 }
498
499 $this->items[$item->confKey] = $item;
500 return $this->items[$item->confKey];
501 }
502
508 public function sortingItems()
509 {
510 // Sorting
511 return uasort($this->items, array($this, 'itemSort'));
512 }
513
520 public function getCurentItemMaxRank($cache = true)
521 {
522 if (empty($this->items)) {
523 return 0;
524 }
525
526 if ($cache && $this->maxItemRank > 0) {
527 return $this->maxItemRank;
528 }
529
530 $this->maxItemRank = 0;
531 foreach ($this->items as $item) {
532 $this->maxItemRank = max($this->maxItemRank, $item->rank);
533 }
534
535 return $this->maxItemRank;
536 }
537
538
545 public function setItemMaxRank($rank)
546 {
547 $this->maxItemRank = max($this->maxItemRank, $rank);
548 }
549
550
557 public function getLineRank($itemKey)
558 {
559 if (!isset($this->items[$itemKey]->rank)) {
560 return -1;
561 }
562 return $this->items[$itemKey]->rank;
563 }
564
565
573 public function itemSort(FormSetupItem $a, FormSetupItem $b)
574 {
575 if (empty($a->rank)) {
576 $a->rank = 0;
577 }
578 if (empty($b->rank)) {
579 $b->rank = 0;
580 }
581 if ($a->rank == $b->rank) {
582 return 0;
583 }
584 return ($a->rank < $b->rank) ? -1 : 1;
585 }
586}
587
588
593{
597 public $db;
598
600 public $langs;
601
603 public $entity;
604
606 public $form;
607
608
610 public $confKey;
611
613 public $nameText = false;
614
616 public $helpText = '';
617
619 public $picto = '';
620
622 public $fieldValue;
623
625 public $defaultFieldValue = null;
626
628 public $fieldAttr = array();
629
631 public $fieldOverride = false;
632
634 public $fieldInputOverride = false;
635
637 public $fieldOutputOverride = false;
638
640 public $rank = 0;
641
643 public $fieldOptions = array();
644
646 public $fieldParams = array();
647
650
653
657 public $errors = array();
658
665 protected $type = 'string';
666
667
669 public $enabled = 1;
670
674 public $cssClass = '';
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 = '';
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 $out .= ajax_constantonoff($this->confKey);
908 } else {
909 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
910 }
911 } elseif (preg_match('/emailtemplate:/', $this->type)) {
912 $out .= $this->generateInputFieldEmailTemplate();
913 } elseif (preg_match('/category:/', $this->type)) {
914 $out .= $this->generateInputFieldCategories();
915 } elseif (preg_match('/thirdparty_type/', $this->type)) {
916 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
917 $formcompany = new FormCompany($this->db);
918 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
919 } elseif ($this->type == 'securekey') {
920 $out .= $this->generateInputFieldSecureKey();
921 } elseif ($this->type == 'product') {
922 if (isModEnabled("product") || isModEnabled("service")) {
923 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
924 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
925 }
926 } elseif ($this->type == 'selectBankAccount') {
927 if (isModEnabled("bank")) {
928 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
929 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
930 }
931 } elseif ($this->type == 'password') {
932 $out .= $this->generateInputFieldPassword('dolibarr');
933 } elseif ($this->type == 'genericpassword') {
934 $out .= $this->generateInputFieldPassword('generic');
935 } else {
936 $out .= $this->generateInputFieldText();
937 }
938
939 return $out;
940 }
941
947 public function generateInputFieldText()
948 {
949 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
950 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
951 }
952 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
953 }
954
961 {
962 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
963 $out .= dol_htmlentities($this->fieldValue);
964 $out .= "</textarea>\n";
965 return $out;
966 }
967
973 public function generateInputFieldHtml()
974 {
975 global $conf;
976 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
977 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
978 return $doleditor->Create(1);
979 }
980
987 {
988 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
989 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
990 $formother = new FormOther($this->db);
991
992 $tmp = explode(':', $this->type);
993 $out = img_picto('', 'category', 'class="pictofixedwidth"');
994
995 $label = 'Categories';
996 if ($this->type == 'customer') {
997 $label = 'CustomersProspectsCategoriesShort';
998 }
999 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
1000
1001 return $out;
1002 }
1003
1009 {
1010 global $user;
1011
1012 $out = '';
1013 if (preg_match('/emailtemplate:/', $this->type)) {
1014 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1015 $formmail = new FormMail($this->db);
1016
1017 $tmp = explode(':', $this->type);
1018 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1019 $arrayOfMessageName = array();
1020 if (is_array($formmail->lines_model)) {
1021 foreach ($formmail->lines_model as $modelMail) {
1022 $moreonlabel = '';
1023 if (!empty($arrayOfMessageName[$modelMail->label])) {
1024 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1025 }
1026 // The 'label' is the key that is unique if we exclude the language
1027 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1028 }
1029 }
1030 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1031 }
1032
1033 return $out;
1034 }
1035
1036
1043 {
1044 global $conf;
1045 $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).'">';
1046
1047 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1048 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1049
1050 // Add button to autosuggest a key
1051 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1052 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1053 }
1054
1055 return $out;
1056 }
1057
1058
1066 public function generateInputFieldPassword($type = 'generic')
1067 {
1068 global $conf, $langs, $user;
1069
1070 $min = 6;
1071 $max = 50;
1072 if ($type == 'dolibarr') {
1073 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1074 if ($gen == 'none') {
1075 $gen = 'standard';
1076 }
1077 $nomclass = "modGeneratePass".ucfirst($gen);
1078 $nomfichier = $nomclass.".class.php";
1079 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1080 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1081 $min = $genhandler->length;
1082 $max = $genhandler->length2;
1083 }
1084 $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).'"';
1085 if ($min) {
1086 $out .= ' minlength="' . $min . '"';
1087 }
1088 if ($max) {
1089 $out .= ' maxlength="' . $max . '"';
1090 }
1091 $out .= '>';
1092 return $out;
1093 }
1094
1095
1096
1103 {
1104 $TSelected = array();
1105 if ($this->fieldValue) {
1106 $TSelected = explode(',', $this->fieldValue);
1107 }
1108
1109 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1110 }
1111
1112
1119 {
1120 $s = '';
1121 if ($this->picto) {
1122 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1123 }
1124
1125 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1126
1127 return $s;
1128 }
1129
1134 {
1135 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1136 }
1137
1145 public function getType()
1146 {
1147 return $this->type;
1148 }
1149
1159 public function setTypeFromTypeString($type)
1160 {
1161 $this->type = $type;
1162
1163 return true;
1164 }
1165
1172 public function setErrors($errors)
1173 {
1174 if (is_array($errors)) {
1175 if (!empty($errors)) {
1176 foreach ($errors as $error) {
1177 $this->setErrors($error);
1178 }
1179 }
1180 } elseif (!empty($errors)) {
1181 $this->errors[] = $errors;
1182 }
1183 return null;
1184 }
1185
1191 public function generateOutputField()
1192 {
1193 global $conf, $user, $langs;
1194
1195 if (!empty($this->fieldOverride)) {
1196 return $this->fieldOverride;
1197 }
1198
1199 if (!empty($this->fieldOutputOverride)) {
1200 return $this->fieldOutputOverride;
1201 }
1202
1203 $out = '';
1204
1205 if ($this->type == 'title') {
1206 // nothing to do
1207 } elseif ($this->type == 'textarea') {
1208 $out .= dol_nl2br($this->fieldValue);
1209 } elseif ($this->type == 'multiselect') {
1210 $out .= $this->generateOutputFieldMultiSelect();
1211 } elseif ($this->type == 'select') {
1212 $out .= $this->generateOutputFieldSelect();
1213 } elseif ($this->type == 'selectUser') {
1214 $out .= $this->generateOutputFieldSelectUser();
1215 } elseif ($this->type == 'html') {
1216 $out .= $this->fieldValue;
1217 } elseif ($this->type == 'color') {
1218 $out .= $this->generateOutputFieldColor();
1219 } elseif ($this->type == 'yesno') {
1220 if (!empty($conf->use_javascript_ajax)) {
1221 $out .= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1222 } else {
1223 if ($this->fieldValue == 1) {
1224 $out .= $langs->trans('yes');
1225 } else {
1226 $out .= $langs->trans('no');
1227 }
1228 }
1229 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1230 if ($this->fieldValue > 0) {
1231 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1232 $formmail = new FormMail($this->db);
1233
1234 $tmp = explode(':', $this->type);
1235
1236 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1237 if (is_numeric($template) && $template < 0) {
1238 $this->setErrors($formmail->errors);
1239 }
1240 $out .= $this->langs->trans($template->label);
1241 }
1242 } elseif (preg_match('/category:/', $this->type)) {
1243 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1244 $c = new Categorie($this->db);
1245 $result = $c->fetch($this->fieldValue);
1246 if ($result < 0) {
1247 $this->setErrors($c->errors);
1248 }
1249 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1250 $toprint = array();
1251 foreach ($ways as $way) {
1252 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1253 }
1254 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1255 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1256 if ($this->fieldValue == 2) {
1257 $out .= $this->langs->trans("Prospect");
1258 } elseif ($this->fieldValue == 3) {
1259 $out .= $this->langs->trans("ProspectCustomer");
1260 } elseif ($this->fieldValue == 1) {
1261 $out .= $this->langs->trans("Customer");
1262 } elseif ($this->fieldValue == 0) {
1263 $out .= $this->langs->trans("NorProspectNorCustomer");
1264 }
1265 } elseif ($this->type == 'product') {
1266 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1267
1268 $product = new Product($this->db);
1269 $resprod = $product->fetch($this->fieldValue);
1270 if ($resprod > 0) {
1271 $out .= $product->ref;
1272 } elseif ($resprod < 0) {
1273 $this->setErrors($product->errors);
1274 }
1275 } elseif ($this->type == 'selectBankAccount') {
1276 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1277
1278 $bankaccount = new Account($this->db);
1279 $resbank = $bankaccount->fetch($this->fieldValue);
1280 if ($resbank > 0) {
1281 $out .= $bankaccount->label;
1282 } elseif ($resbank < 0) {
1283 $this->setErrors($bankaccount->errors);
1284 }
1285 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1286 $out .= str_repeat('*', strlen($this->fieldValue));
1287 } else {
1288 $out .= $this->fieldValue;
1289 }
1290
1291 return $out;
1292 }
1293
1294
1301 {
1302 $outPut = '';
1303 $TSelected = array();
1304 if (!empty($this->fieldValue)) {
1305 $TSelected = explode(',', $this->fieldValue);
1306 }
1307
1308 if (!empty($TSelected)) {
1309 foreach ($TSelected as $selected) {
1310 if (!empty($this->fieldOptions[$selected])) {
1311 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1312 }
1313 }
1314 }
1315 return $outPut;
1316 }
1317
1324 {
1325 global $langs;
1326 $this->fieldAttr['disabled'] = null;
1327 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1328 if ($color) {
1329 return '<input type="text" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; background-color: #'.$color.'" value="'.$color.'">';
1330 }
1331 return $langs->trans("Default");
1332 }
1338 public function generateInputFieldColor()
1339 {
1340 $this->fieldAttr['type'] = 'color';
1341 $default = $this->defaultFieldValue;
1342 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1343 $formother = new FormOther($this->db);
1344 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1345 }
1346
1353 {
1354 $outPut = '';
1355 if (!empty($this->fieldOptions[$this->fieldValue])) {
1356 $outPut = $this->fieldOptions[$this->fieldValue];
1357 }
1358
1359 return $outPut;
1360 }
1361
1368 {
1369 $outPut = '';
1370 $user = new User($this->db);
1371 $user->fetch($this->fieldValue);
1372 $outPut = $user->firstname . " " . $user->lastname;
1373 return $outPut;
1374 }
1375
1376 /*
1377 * METHODS FOR SETTING DISPLAY TYPE
1378 */
1379
1385 public function setAsString()
1386 {
1387 $this->type = 'string';
1388 return $this;
1389 }
1390
1396 public function setAsColor()
1397 {
1398 $this->type = 'color';
1399 return $this;
1400 }
1401
1407 public function setAsTextarea()
1408 {
1409 $this->type = 'textarea';
1410 return $this;
1411 }
1412
1418 public function setAsHtml()
1419 {
1420 $this->type = 'html';
1421 return $this;
1422 }
1423
1430 public function setAsEmailTemplate($templateType)
1431 {
1432 $this->type = 'emailtemplate:'.$templateType;
1433 return $this;
1434 }
1435
1441 public function setAsThirdpartyType()
1442 {
1443 $this->type = 'thirdparty_type';
1444 return $this;
1445 }
1446
1452 public function setAsYesNo()
1453 {
1454 $this->type = 'yesno';
1455 return $this;
1456 }
1457
1463 public function setAsSecureKey()
1464 {
1465 $this->type = 'securekey';
1466 return $this;
1467 }
1468
1474 public function setAsProduct()
1475 {
1476 $this->type = 'product';
1477 return $this;
1478 }
1479
1487 public function setAsCategory($catType)
1488 {
1489 $this->type = 'category:'.$catType;
1490 return $this;
1491 }
1492
1498 public function setAsTitle()
1499 {
1500 $this->type = 'title';
1501 return $this;
1502 }
1503
1504
1511 public function setAsMultiSelect($fieldOptions)
1512 {
1513 if (is_array($fieldOptions)) {
1514 $this->fieldOptions = $fieldOptions;
1515 }
1516
1517 $this->type = 'multiselect';
1518 return $this;
1519 }
1520
1527 public function setAsSelect($fieldOptions)
1528 {
1529 if (is_array($fieldOptions)) {
1530 $this->fieldOptions = $fieldOptions;
1531 }
1532
1533 $this->type = 'select';
1534 return $this;
1535 }
1536
1542 public function setAsSelectUser()
1543 {
1544 $this->type = 'selectUser';
1545 return $this;
1546 }
1547
1553 public function setAsSelectBankAccount()
1554 {
1555 $this->type = 'selectBankAccount';
1556 return $this;
1557 }
1558
1565 public function setAsPassword()
1566 {
1567 $this->type = 'password';
1568 return $this;
1569 }
1570
1577 public function setAsGenericPassword()
1578 {
1579 $this->type = 'genericpassword';
1580 return $this;
1581 }
1582}
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,...
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)
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.
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...
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.