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'])) {
409 $item->enabled = $params['enabled'];
410 }
411
412 if (!empty($params['css'])) {
413 $item->cssClass = $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 = $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 public $enabled = 1;
668
672 public $cssClass = '';
673
679 public function __construct($confKey)
680 {
681 global $langs, $db, $conf, $form;
682 $this->db = $db;
683
684 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
685 $this->form = $form;
686 } else {
687 $this->form = new Form($this->db);
688 }
689
690 $this->langs = $langs;
691 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
692
693 $this->confKey = $confKey;
694 $this->loadValueFromConf();
695 }
696
702 public function loadValueFromConf()
703 {
704 global $conf;
705 if (isset($conf->global->{$this->confKey})) {
706 $this->fieldValue = getDolGlobalString($this->confKey);
707 return true;
708 } else {
709 $this->fieldValue = '';
710 return false;
711 }
712 }
713
720 public function reloadValueFromConf()
721 {
722 return $this->loadValueFromConf();
723 }
724
725
731 public function saveConfValue()
732 {
733 global $hookmanager;
734
735 $parameters = array();
736 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
737 if ($reshook < 0) {
738 $this->setErrors($hookmanager->errors);
739 return -1;
740 }
741
742 if ($reshook > 0) {
743 return $reshook;
744 }
745
746
747 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
748 return call_user_func($this->saveCallBack, $this);
749 }
750
751 // Modify constant only if key was posted (avoid resetting key to the null value)
752 if ($this->type != 'title') {
753 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
754 if ($result < 0) {
755 return -1;
756 } else {
757 return 1;
758 }
759 }
760
761 return 0;
762 }
763
770 public function setSaveCallBack(callable $callBack)
771 {
772 $this->saveCallBack = $callBack;
773 }
774
781 public function setValueFromPostCallBack(callable $callBack)
782 {
783 $this->setValueFromPostCallBack = $callBack;
784 }
785
791 public function setValueFromPost()
792 {
793 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
794 return call_user_func($this->setValueFromPostCallBack);
795 }
796
797 // Modify constant only if key was posted (avoid resetting key to the null value)
798 if ($this->type != 'title') {
799 if (preg_match('/category:/', $this->type)) {
800 if (GETPOSTINT($this->confKey) == '-1') {
801 $val_const = '';
802 } else {
803 $val_const = GETPOSTINT($this->confKey);
804 }
805 } elseif ($this->type == 'multiselect') {
806 $val = GETPOST($this->confKey, 'array');
807 if ($val && is_array($val)) {
808 $val_const = implode(',', $val);
809 } else {
810 $val_const = '';
811 }
812 } elseif ($this->type == 'html') {
813 $val_const = GETPOST($this->confKey, 'restricthtml');
814 } else {
815 $val_const = GETPOST($this->confKey, 'alpha');
816 }
817
818 // TODO add value check with class validate
819 $this->fieldValue = $val_const;
820
821 return 1;
822 }
823
824 return 0;
825 }
826
832 public function getHelpText()
833 {
834 if (!empty($this->helpText)) {
835 return $this->helpText;
836 }
837 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
838 }
839
845 public function getNameText()
846 {
847 if (!empty($this->nameText)) {
848 return $this->nameText;
849 }
850 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
851
852 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
853 if (isModEnabled('multicompany') && $this->entity == 0) {
854 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
855 }
856
857 return $out;
858 }
859
865 public function generateInputField()
866 {
867 global $conf;
868
869 if (!empty($this->fieldOverride)) {
870 return $this->fieldOverride;
871 }
872
873 if (!empty($this->fieldInputOverride)) {
874 return $this->fieldInputOverride;
875 }
876
877 // Set default value
878 if (is_null($this->fieldValue)) {
879 $this->fieldValue = $this->defaultFieldValue;
880 }
881
882
883 $this->fieldAttr['name'] = $this->confKey;
884 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
885 $this->fieldAttr['value'] = $this->fieldValue;
886
887 $out = '';
888
889 if ($this->type == 'title') {
890 $out .= $this->generateOutputField(); // title have no input
891 } elseif ($this->type == 'multiselect') {
892 $out .= $this->generateInputFieldMultiSelect();
893 } elseif ($this->type == 'select') {
894 $out .= $this->generateInputFieldSelect();
895 } elseif ($this->type == 'selectUser') {
896 $out .= $this->generateInputFieldSelectUser();
897 } elseif ($this->type == 'textarea') {
898 $out .= $this->generateInputFieldTextarea();
899 } elseif ($this->type == 'html') {
900 $out .= $this->generateInputFieldHtml();
901 } elseif ($this->type == 'color') {
902 $out .= $this->generateInputFieldColor();
903 } elseif ($this->type == 'yesno') {
904 if (!empty($conf->use_javascript_ajax)) {
905 $out .= ajax_constantonoff($this->confKey);
906 } else {
907 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
908 }
909 } elseif (preg_match('/emailtemplate:/', $this->type)) {
910 $out .= $this->generateInputFieldEmailTemplate();
911 } elseif (preg_match('/category:/', $this->type)) {
912 $out .= $this->generateInputFieldCategories();
913 } elseif (preg_match('/thirdparty_type/', $this->type)) {
914 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
915 $formcompany = new FormCompany($this->db);
916 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
917 } elseif ($this->type == 'securekey') {
918 $out .= $this->generateInputFieldSecureKey();
919 } elseif ($this->type == 'product') {
920 if (isModEnabled("product") || isModEnabled("service")) {
921 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
922 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
923 }
924 } elseif ($this->type == 'selectBankAccount') {
925 if (isModEnabled("bank")) {
926 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
927 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
928 }
929 } elseif ($this->type == 'password') {
930 $out .= $this->generateInputFieldPassword('dolibarr');
931 } elseif ($this->type == 'genericpassword') {
932 $out .= $this->generateInputFieldPassword('generic');
933 } else {
934 $out .= $this->generateInputFieldText();
935 }
936
937 return $out;
938 }
939
945 public function generateInputFieldText()
946 {
947 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
948 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
949 }
950 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
951 }
952
959 {
960 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
961 $out .= dol_htmlentities($this->fieldValue);
962 $out .= "</textarea>\n";
963 return $out;
964 }
965
971 public function generateInputFieldHtml()
972 {
973 global $conf;
974 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
975 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
976 return $doleditor->Create(1);
977 }
978
985 {
986 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
987 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
988 $formother = new FormOther($this->db);
989
990 $tmp = explode(':', $this->type);
991 $out = img_picto('', 'category', 'class="pictofixedwidth"');
992
993 $label = 'Categories';
994 if ($this->type == 'customer') {
995 $label = 'CustomersProspectsCategoriesShort';
996 }
997 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
998
999 return $out;
1000 }
1001
1007 {
1008 global $user;
1009
1010 $out = '';
1011 if (preg_match('/emailtemplate:/', $this->type)) {
1012 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1013 $formmail = new FormMail($this->db);
1014
1015 $tmp = explode(':', $this->type);
1016 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1017 $arrayOfMessageName = array();
1018 if (is_array($formmail->lines_model)) {
1019 foreach ($formmail->lines_model as $modelMail) {
1020 $moreonlabel = '';
1021 if (!empty($arrayOfMessageName[$modelMail->label])) {
1022 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1023 }
1024 // The 'label' is the key that is unique if we exclude the language
1025 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1026 }
1027 }
1028 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1029 }
1030
1031 return $out;
1032 }
1033
1034
1041 {
1042 global $conf;
1043 $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).'">';
1044
1045 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1046 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1047
1048 // Add button to autosuggest a key
1049 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1050 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1051 }
1052
1053 return $out;
1054 }
1055
1056
1064 public function generateInputFieldPassword($type = 'generic')
1065 {
1066 global $conf, $langs, $user;
1067
1068 $min = 6;
1069 $max = 50;
1070 if ($type == 'dolibarr') {
1071 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1072 if ($gen == 'none') {
1073 $gen = 'standard';
1074 }
1075 $nomclass = "modGeneratePass".ucfirst($gen);
1076 $nomfichier = $nomclass.".class.php";
1077 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1078 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1079 $min = $genhandler->length;
1080 $max = $genhandler->length2;
1081 }
1082 $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).'"';
1083 if ($min) {
1084 $out .= ' minlength="' . $min . '"';
1085 }
1086 if ($max) {
1087 $out .= ' maxlength="' . $max . '"';
1088 }
1089 $out .= '>';
1090 return $out;
1091 }
1092
1093
1094
1101 {
1102 $TSelected = array();
1103 if ($this->fieldValue) {
1104 $TSelected = explode(',', $this->fieldValue);
1105 }
1106
1107 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1108 }
1109
1110
1117 {
1118 $s = '';
1119 if ($this->picto) {
1120 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1121 }
1122 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1123
1124 return $s;
1125 }
1126
1131 {
1132 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1133 }
1134
1142 public function getType()
1143 {
1144 return $this->type;
1145 }
1146
1156 public function setTypeFromTypeString($type)
1157 {
1158 $this->type = $type;
1159
1160 return true;
1161 }
1162
1169 public function setErrors($errors)
1170 {
1171 if (is_array($errors)) {
1172 if (!empty($errors)) {
1173 foreach ($errors as $error) {
1174 $this->setErrors($error);
1175 }
1176 }
1177 } elseif (!empty($errors)) {
1178 $this->errors[] = $errors;
1179 }
1180 return null;
1181 }
1182
1188 public function generateOutputField()
1189 {
1190 global $conf, $user, $langs;
1191
1192 if (!empty($this->fieldOverride)) {
1193 return $this->fieldOverride;
1194 }
1195
1196 if (!empty($this->fieldOutputOverride)) {
1197 return $this->fieldOutputOverride;
1198 }
1199
1200 $out = '';
1201
1202 if ($this->type == 'title') {
1203 // nothing to do
1204 } elseif ($this->type == 'textarea') {
1205 $out .= dol_nl2br($this->fieldValue);
1206 } elseif ($this->type == 'multiselect') {
1207 $out .= $this->generateOutputFieldMultiSelect();
1208 } elseif ($this->type == 'select') {
1209 $out .= $this->generateOutputFieldSelect();
1210 } elseif ($this->type == 'selectUser') {
1211 $out .= $this->generateOutputFieldSelectUser();
1212 } elseif ($this->type == 'html') {
1213 $out .= $this->fieldValue;
1214 } elseif ($this->type == 'color') {
1215 $out .= $this->generateOutputFieldColor();
1216 } elseif ($this->type == 'yesno') {
1217 if (!empty($conf->use_javascript_ajax)) {
1218 $out .= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1219 } else {
1220 if ($this->fieldValue == 1) {
1221 $out .= $langs->trans('yes');
1222 } else {
1223 $out .= $langs->trans('no');
1224 }
1225 }
1226 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1227 if ($this->fieldValue > 0) {
1228 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1229 $formmail = new FormMail($this->db);
1230
1231 $tmp = explode(':', $this->type);
1232
1233 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1234 if (is_numeric($template) && $template < 0) {
1235 $this->setErrors($formmail->errors);
1236 }
1237 $out .= $this->langs->trans($template->label);
1238 }
1239 } elseif (preg_match('/category:/', $this->type)) {
1240 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1241 $c = new Categorie($this->db);
1242 $result = $c->fetch($this->fieldValue);
1243 if ($result < 0) {
1244 $this->setErrors($c->errors);
1245 }
1246 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1247 $toprint = array();
1248 foreach ($ways as $way) {
1249 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1250 }
1251 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1252 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1253 if ($this->fieldValue == 2) {
1254 $out .= $this->langs->trans("Prospect");
1255 } elseif ($this->fieldValue == 3) {
1256 $out .= $this->langs->trans("ProspectCustomer");
1257 } elseif ($this->fieldValue == 1) {
1258 $out .= $this->langs->trans("Customer");
1259 } elseif ($this->fieldValue == 0) {
1260 $out .= $this->langs->trans("NorProspectNorCustomer");
1261 }
1262 } elseif ($this->type == 'product') {
1263 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1264
1265 $product = new Product($this->db);
1266 $resprod = $product->fetch($this->fieldValue);
1267 if ($resprod > 0) {
1268 $out .= $product->ref;
1269 } elseif ($resprod < 0) {
1270 $this->setErrors($product->errors);
1271 }
1272 } elseif ($this->type == 'selectBankAccount') {
1273 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1274
1275 $bankaccount = new Account($this->db);
1276 $resbank = $bankaccount->fetch($this->fieldValue);
1277 if ($resbank > 0) {
1278 $out .= $bankaccount->label;
1279 } elseif ($resbank < 0) {
1280 $this->setErrors($bankaccount->errors);
1281 }
1282 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1283 $out .= str_repeat('*', strlen($this->fieldValue));
1284 } else {
1285 $out .= $this->fieldValue;
1286 }
1287
1288 return $out;
1289 }
1290
1291
1298 {
1299 $outPut = '';
1300 $TSelected = array();
1301 if (!empty($this->fieldValue)) {
1302 $TSelected = explode(',', $this->fieldValue);
1303 }
1304
1305 if (!empty($TSelected)) {
1306 foreach ($TSelected as $selected) {
1307 if (!empty($this->fieldOptions[$selected])) {
1308 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1309 }
1310 }
1311 }
1312 return $outPut;
1313 }
1314
1321 {
1322 global $langs;
1323 $this->fieldAttr['disabled'] = null;
1324 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1325 if ($color) {
1326 return '<input type="text" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; background-color: #'.$color.'" value="'.$color.'">';
1327 }
1328 return $langs->trans("Default");
1329 }
1335 public function generateInputFieldColor()
1336 {
1337 $this->fieldAttr['type'] = 'color';
1338 $default = $this->defaultFieldValue;
1339 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1340 $formother = new FormOther($this->db);
1341 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1342 }
1343
1350 {
1351 $outPut = '';
1352 if (!empty($this->fieldOptions[$this->fieldValue])) {
1353 $outPut = $this->fieldOptions[$this->fieldValue];
1354 }
1355
1356 return $outPut;
1357 }
1358
1365 {
1366 $outPut = '';
1367 $user = new User($this->db);
1368 $user->fetch($this->fieldValue);
1369 $outPut = $user->firstname . " " . $user->lastname;
1370 return $outPut;
1371 }
1372
1373 /*
1374 * METHODS FOR SETTING DISPLAY TYPE
1375 */
1376
1382 public function setAsString()
1383 {
1384 $this->type = 'string';
1385 return $this;
1386 }
1387
1393 public function setAsColor()
1394 {
1395 $this->type = 'color';
1396 return $this;
1397 }
1398
1404 public function setAsTextarea()
1405 {
1406 $this->type = 'textarea';
1407 return $this;
1408 }
1409
1415 public function setAsHtml()
1416 {
1417 $this->type = 'html';
1418 return $this;
1419 }
1420
1427 public function setAsEmailTemplate($templateType)
1428 {
1429 $this->type = 'emailtemplate:'.$templateType;
1430 return $this;
1431 }
1432
1438 public function setAsThirdpartyType()
1439 {
1440 $this->type = 'thirdparty_type';
1441 return $this;
1442 }
1443
1449 public function setAsYesNo()
1450 {
1451 $this->type = 'yesno';
1452 return $this;
1453 }
1454
1460 public function setAsSecureKey()
1461 {
1462 $this->type = 'securekey';
1463 return $this;
1464 }
1465
1471 public function setAsProduct()
1472 {
1473 $this->type = 'product';
1474 return $this;
1475 }
1476
1484 public function setAsCategory($catType)
1485 {
1486 $this->type = 'category:'.$catType;
1487 return $this;
1488 }
1489
1495 public function setAsTitle()
1496 {
1497 $this->type = 'title';
1498 return $this;
1499 }
1500
1501
1508 public function setAsMultiSelect($fieldOptions)
1509 {
1510 if (is_array($fieldOptions)) {
1511 $this->fieldOptions = $fieldOptions;
1512 }
1513
1514 $this->type = 'multiselect';
1515 return $this;
1516 }
1517
1524 public function setAsSelect($fieldOptions)
1525 {
1526 if (is_array($fieldOptions)) {
1527 $this->fieldOptions = $fieldOptions;
1528 }
1529
1530 $this->type = 'select';
1531 return $this;
1532 }
1533
1539 public function setAsSelectUser()
1540 {
1541 $this->type = 'selectUser';
1542 return $this;
1543 }
1544
1550 public function setAsSelectBankAccount()
1551 {
1552 $this->type = 'selectBankAccount';
1553 return $this;
1554 }
1555
1562 public function setAsPassword()
1563 {
1564 $this->type = 'password';
1565 return $this;
1566 }
1567
1574 public function setAsGenericPassword()
1575 {
1576 $this->type = 'genericpassword';
1577 return $this;
1578 }
1579}
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.
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.