dolibarr 21.0.0-beta
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
676
682 public function __construct($confKey)
683 {
684 global $langs, $db, $conf, $form;
685 $this->db = $db;
686
687 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
688 $this->form = $form;
689 } else {
690 $this->form = new Form($this->db);
691 }
692
693 $this->langs = $langs;
694 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
695
696 $this->confKey = $confKey;
697 $this->loadValueFromConf();
698 }
699
705 public function loadValueFromConf()
706 {
707 global $conf;
708 if (isset($conf->global->{$this->confKey})) {
709 $this->fieldValue = getDolGlobalString($this->confKey);
710 return true;
711 } else {
712 $this->fieldValue = '';
713 return false;
714 }
715 }
716
723 public function reloadValueFromConf()
724 {
725 return $this->loadValueFromConf();
726 }
727
728
734 public function saveConfValue()
735 {
736 global $hookmanager;
737
738 $parameters = array();
739 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
740 if ($reshook < 0) {
741 $this->setErrors($hookmanager->errors);
742 return -1;
743 }
744
745 if ($reshook > 0) {
746 return $reshook;
747 }
748
749
750 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
751 return call_user_func($this->saveCallBack, $this);
752 }
753
754 // Modify constant only if key was posted (avoid resetting key to the null value)
755 if ($this->type != 'title') {
756 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
757 if ($result < 0) {
758 return -1;
759 } else {
760 return 1;
761 }
762 }
763
764 return 0;
765 }
766
773 public function setSaveCallBack(callable $callBack)
774 {
775 $this->saveCallBack = $callBack;
776 }
777
784 public function setValueFromPostCallBack(callable $callBack)
785 {
786 $this->setValueFromPostCallBack = $callBack;
787 }
788
794 public function setValueFromPost()
795 {
796 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
797 return call_user_func($this->setValueFromPostCallBack);
798 }
799
800 // Modify constant only if key was posted (avoid resetting key to the null value)
801 if ($this->type != 'title') {
802 if (preg_match('/category:/', $this->type)) {
803 if (GETPOSTINT($this->confKey) == '-1') {
804 $val_const = '';
805 } else {
806 $val_const = GETPOSTINT($this->confKey);
807 }
808 } elseif ($this->type == 'multiselect') {
809 $val = GETPOST($this->confKey, 'array');
810 if ($val && is_array($val)) {
811 $val_const = implode(',', $val);
812 } else {
813 $val_const = '';
814 }
815 } elseif ($this->type == 'html') {
816 $val_const = GETPOST($this->confKey, 'restricthtml');
817 } else {
818 $val_const = GETPOST($this->confKey, 'alpha');
819 }
820
821 // TODO add value check with class validate
822 $this->fieldValue = $val_const;
823
824 return 1;
825 }
826
827 return 0;
828 }
829
835 public function getHelpText()
836 {
837 if (!empty($this->helpText)) {
838 return $this->helpText;
839 }
840 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
841 }
842
848 public function getNameText()
849 {
850 if (!empty($this->nameText)) {
851 return $this->nameText;
852 }
853 $out = (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
854
855 // if conf defined on entity 0, prepend a picto to indicate it will apply across all entities
856 if (isModEnabled('multicompany') && $this->entity == 0) {
857 $out = img_picto($this->langs->trans('AllEntities'), 'fa-globe-americas em088 opacityhigh') . '&nbsp;' . $out;
858 }
859
860 return $out;
861 }
862
868 public function generateInputField()
869 {
870 global $conf;
871
872 if (!empty($this->fieldOverride)) {
873 return $this->fieldOverride;
874 }
875
876 if (!empty($this->fieldInputOverride)) {
877 return $this->fieldInputOverride;
878 }
879
880 // Set default value
881 if (is_null($this->fieldValue)) {
882 $this->fieldValue = $this->defaultFieldValue;
883 }
884
885
886 $this->fieldAttr['name'] = $this->confKey;
887 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
888 $this->fieldAttr['value'] = $this->fieldValue;
889
890 $out = '';
891
892 if ($this->type == 'title') {
893 $out .= $this->generateOutputField(); // title have no input
894 } elseif ($this->type == 'multiselect') {
895 $out .= $this->generateInputFieldMultiSelect();
896 } elseif ($this->type == 'select') {
897 $out .= $this->generateInputFieldSelect();
898 } elseif ($this->type == 'selectUser') {
899 $out .= $this->generateInputFieldSelectUser();
900 } elseif ($this->type == 'textarea') {
901 $out .= $this->generateInputFieldTextarea();
902 } elseif ($this->type == 'html') {
903 $out .= $this->generateInputFieldHtml();
904 } elseif ($this->type == 'color') {
905 $out .= $this->generateInputFieldColor();
906 } elseif ($this->type == 'yesno') {
907 if (!empty($conf->use_javascript_ajax)) {
908 $input = $this->fieldParams['input'] ?? array();
909 $revertonoff = $this->fieldParams['revertonoff'] ? 1 : 0;
910 $forcereload = $this->fieldParams['forcereload'] ? 1 : 0;
911
912 $out .= ajax_constantonoff($this->confKey, $input, $this->entity, $revertonoff, 0, $forcereload);
913 } else {
914 $out .= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
915 }
916 } elseif (preg_match('/emailtemplate:/', $this->type)) {
917 $out .= $this->generateInputFieldEmailTemplate();
918 } elseif (preg_match('/category:/', $this->type)) {
919 $out .= $this->generateInputFieldCategories();
920 } elseif (preg_match('/thirdparty_type/', $this->type)) {
921 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
922 $formcompany = new FormCompany($this->db);
923 $out .= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
924 } elseif ($this->type == 'securekey') {
925 $out .= $this->generateInputFieldSecureKey();
926 } elseif ($this->type == 'product') {
927 if (isModEnabled("product") || isModEnabled("service")) {
928 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
929 $out .= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
930 }
931 } elseif ($this->type == 'selectBankAccount') {
932 if (isModEnabled("bank")) {
933 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
934 $out .= $this->form->select_comptes($selected, $this->confKey, 0, '', 0, '', 0, '', 1);
935 }
936 } elseif ($this->type == 'password') {
937 $out .= $this->generateInputFieldPassword('dolibarr');
938 } elseif ($this->type == 'genericpassword') {
939 $out .= $this->generateInputFieldPassword('generic');
940 } else {
941 $out .= $this->generateInputFieldText();
942 }
943
944 return $out;
945 }
946
952 public function generateInputFieldText()
953 {
954 if (empty($this->fieldAttr) || empty($this->fieldAttr['class'])) {
955 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
956 }
957 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
958 }
959
966 {
967 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
968 $out .= dol_htmlentities($this->fieldValue);
969 $out .= "</textarea>\n";
970 return $out;
971 }
972
978 public function generateInputFieldHtml()
979 {
980 global $conf;
981 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
982 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
983 return $doleditor->Create(1);
984 }
985
992 {
993 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
994 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
995 $formother = new FormOther($this->db);
996
997 $tmp = explode(':', $this->type);
998 $out = img_picto('', 'category', 'class="pictofixedwidth"');
999
1000 $label = 'Categories';
1001 if ($this->type == 'customer') {
1002 $label = 'CustomersProspectsCategoriesShort';
1003 }
1004 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans($label));
1005
1006 return $out;
1007 }
1008
1014 {
1015 global $user;
1016
1017 $out = '';
1018 if (preg_match('/emailtemplate:/', $this->type)) {
1019 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1020 $formmail = new FormMail($this->db);
1021
1022 $tmp = explode(':', $this->type);
1023 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
1024 $arrayOfMessageName = array();
1025 if (is_array($formmail->lines_model)) {
1026 foreach ($formmail->lines_model as $modelMail) {
1027 $moreonlabel = '';
1028 if (!empty($arrayOfMessageName[$modelMail->label])) {
1029 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
1030 }
1031 // The 'label' is the key that is unique if we exclude the language
1032 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
1033 }
1034 }
1035 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
1036 }
1037
1038 return $out;
1039 }
1040
1041
1048 {
1049 global $conf;
1050 $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).'">';
1051
1052 if (!empty($conf->use_javascript_ajax) && empty($this->fieldParams['hideGenerateButton'])) {
1053 $out .= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1054
1055 // Add button to autosuggest a key
1056 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1057 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1058 }
1059
1060 return $out;
1061 }
1062
1063
1071 public function generateInputFieldPassword($type = 'generic')
1072 {
1073 global $conf, $langs, $user;
1074
1075 $min = 6;
1076 $max = 50;
1077 if ($type == 'dolibarr') {
1078 $gen = getDolGlobalString('USER_PASSWORD_GENERATED', 'standard');
1079 if ($gen == 'none') {
1080 $gen = 'standard';
1081 }
1082 $nomclass = "modGeneratePass".ucfirst($gen);
1083 $nomfichier = $nomclass.".class.php";
1084 require_once DOL_DOCUMENT_ROOT."/core/modules/security/generate/".$nomfichier;
1085 $genhandler = new $nomclass($this->db, $conf, $langs, $user);
1086 $min = $genhandler->length;
1087 $max = $genhandler->length2;
1088 }
1089 $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).'"';
1090 if ($min) {
1091 $out .= ' minlength="' . $min . '"';
1092 }
1093 if ($max) {
1094 $out .= ' maxlength="' . $max . '"';
1095 }
1096 $out .= '>';
1097 return $out;
1098 }
1099
1100
1101
1108 {
1109 $TSelected = array();
1110 if ($this->fieldValue) {
1111 $TSelected = explode(',', $this->fieldValue);
1112 }
1113
1114 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1115 }
1116
1117
1124 {
1125 $s = '';
1126 if ($this->picto) {
1127 $s .= img_picto('', $this->picto, 'class="pictofixedwidth"');
1128 }
1129
1130 $s .= $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue, 0, 0, 0, '', 0, 0, 0, '', $this->cssClass);
1131
1132 return $s;
1133 }
1134
1139 {
1140 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1141 }
1142
1150 public function getType()
1151 {
1152 return $this->type;
1153 }
1154
1164 public function setTypeFromTypeString($type)
1165 {
1166 $this->type = $type;
1167
1168 return true;
1169 }
1170
1177 public function setErrors($errors)
1178 {
1179 if (is_array($errors)) {
1180 if (!empty($errors)) {
1181 foreach ($errors as $error) {
1182 $this->setErrors($error);
1183 }
1184 }
1185 } elseif (!empty($errors)) {
1186 $this->errors[] = $errors;
1187 }
1188 return null;
1189 }
1190
1196 public function generateOutputField()
1197 {
1198 global $conf, $user, $langs;
1199
1200 if (!empty($this->fieldOverride)) {
1201 return $this->fieldOverride;
1202 }
1203
1204 if (!empty($this->fieldOutputOverride)) {
1205 return $this->fieldOutputOverride;
1206 }
1207
1208 $out = '';
1209
1210 if ($this->type == 'title') {
1211 // nothing to do
1212 } elseif ($this->type == 'textarea') {
1213 $out .= dol_nl2br($this->fieldValue);
1214 } elseif ($this->type == 'multiselect') {
1215 $out .= $this->generateOutputFieldMultiSelect();
1216 } elseif ($this->type == 'select') {
1217 $out .= $this->generateOutputFieldSelect();
1218 } elseif ($this->type == 'selectUser') {
1219 $out .= $this->generateOutputFieldSelectUser();
1220 } elseif ($this->type == 'html') {
1221 $out .= $this->fieldValue;
1222 } elseif ($this->type == 'color') {
1223 $out .= $this->generateOutputFieldColor();
1224 } elseif ($this->type == 'yesno') {
1225 if (!empty($conf->use_javascript_ajax)) {
1226 $revertonoff = $this->fieldParams['revertonoff'] ? 1 : 0;
1227 $forcereload = $this->fieldParams['forcereload'] ? 1 : 0;
1228
1229 $out .= ajax_constantonoff($this->confKey, array(), $this->entity, $revertonoff, 0, $forcereload);
1230 } else {
1231 if ($this->fieldValue == 1) {
1232 $out .= $langs->trans('yes');
1233 } else {
1234 $out .= $langs->trans('no');
1235 }
1236 }
1237 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1238 if ($this->fieldValue > 0) {
1239 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1240 $formmail = new FormMail($this->db);
1241
1242 $tmp = explode(':', $this->type);
1243
1244 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1245 if (is_numeric($template) && $template < 0) {
1246 $this->setErrors($formmail->errors);
1247 }
1248 $out .= $this->langs->trans($template->label);
1249 }
1250 } elseif (preg_match('/category:/', $this->type)) {
1251 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1252 $c = new Categorie($this->db);
1253 $result = $c->fetch($this->fieldValue);
1254 if ($result < 0) {
1255 $this->setErrors($c->errors);
1256 }
1257 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formatted text
1258 $toprint = array();
1259 foreach ($ways as $way) {
1260 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1261 }
1262 $out .= '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1263 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1264 if ($this->fieldValue == 2) {
1265 $out .= $this->langs->trans("Prospect");
1266 } elseif ($this->fieldValue == 3) {
1267 $out .= $this->langs->trans("ProspectCustomer");
1268 } elseif ($this->fieldValue == 1) {
1269 $out .= $this->langs->trans("Customer");
1270 } elseif ($this->fieldValue == 0) {
1271 $out .= $this->langs->trans("NorProspectNorCustomer");
1272 }
1273 } elseif ($this->type == 'product') {
1274 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1275
1276 $product = new Product($this->db);
1277 $resprod = $product->fetch($this->fieldValue);
1278 if ($resprod > 0) {
1279 $out .= $product->ref;
1280 } elseif ($resprod < 0) {
1281 $this->setErrors($product->errors);
1282 }
1283 } elseif ($this->type == 'selectBankAccount') {
1284 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
1285
1286 $bankaccount = new Account($this->db);
1287 $resbank = $bankaccount->fetch($this->fieldValue);
1288 if ($resbank > 0) {
1289 $out .= $bankaccount->label;
1290 } elseif ($resbank < 0) {
1291 $this->setErrors($bankaccount->errors);
1292 }
1293 } elseif ($this->type == 'password' || $this->type == 'genericpassword') {
1294 $out .= str_repeat('*', strlen($this->fieldValue));
1295 } else {
1296 $out .= $this->fieldValue;
1297 }
1298
1299 return $out;
1300 }
1301
1302
1309 {
1310 $outPut = '';
1311 $TSelected = array();
1312 if (!empty($this->fieldValue)) {
1313 $TSelected = explode(',', $this->fieldValue);
1314 }
1315
1316 if (!empty($TSelected)) {
1317 foreach ($TSelected as $selected) {
1318 if (!empty($this->fieldOptions[$selected])) {
1319 $outPut .= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1320 }
1321 }
1322 }
1323 return $outPut;
1324 }
1325
1332 {
1333 global $langs;
1334 $this->fieldAttr['disabled'] = null;
1335 $color = colorArrayToHex(colorStringToArray($this->fieldValue, array()), '');
1336 if ($color) {
1337 return '<input type="text" class="colorthumb" disabled="disabled" style="padding: 1px; margin-top: 0; margin-bottom: 0; background-color: #'.$color.'" value="'.$color.'">';
1338 }
1339 return $langs->trans("Default");
1340 }
1346 public function generateInputFieldColor()
1347 {
1348 $this->fieldAttr['type'] = 'color';
1349 $default = $this->defaultFieldValue;
1350 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
1351 $formother = new FormOther($this->db);
1352 return $formother->selectColor(colorArrayToHex(colorStringToArray($this->fieldAttr['value'], array()), ''), $this->fieldAttr['name'], '', 1, array(), '', '', $default).' ';
1353 }
1354
1361 {
1362 $outPut = '';
1363 if (!empty($this->fieldOptions[$this->fieldValue])) {
1364 $outPut = $this->fieldOptions[$this->fieldValue];
1365 }
1366
1367 return $outPut;
1368 }
1369
1376 {
1377 $outPut = '';
1378 $user = new User($this->db);
1379 $user->fetch($this->fieldValue);
1380 $outPut = $user->firstname . " " . $user->lastname;
1381 return $outPut;
1382 }
1383
1384 /*
1385 * METHODS FOR SETTING DISPLAY TYPE
1386 */
1387
1393 public function setAsString()
1394 {
1395 $this->type = 'string';
1396 return $this;
1397 }
1398
1404 public function setAsColor()
1405 {
1406 $this->type = 'color';
1407 return $this;
1408 }
1409
1415 public function setAsTextarea()
1416 {
1417 $this->type = 'textarea';
1418 return $this;
1419 }
1420
1426 public function setAsHtml()
1427 {
1428 $this->type = 'html';
1429 return $this;
1430 }
1431
1438 public function setAsEmailTemplate($templateType)
1439 {
1440 $this->type = 'emailtemplate:'.$templateType;
1441 return $this;
1442 }
1443
1449 public function setAsThirdpartyType()
1450 {
1451 $this->type = 'thirdparty_type';
1452 return $this;
1453 }
1454
1460 public function setAsYesNo()
1461 {
1462 $this->type = 'yesno';
1463 return $this;
1464 }
1465
1471 public function setAsSecureKey()
1472 {
1473 $this->type = 'securekey';
1474 return $this;
1475 }
1476
1482 public function setAsProduct()
1483 {
1484 $this->type = 'product';
1485 return $this;
1486 }
1487
1495 public function setAsCategory($catType)
1496 {
1497 $this->type = 'category:'.$catType;
1498 return $this;
1499 }
1500
1506 public function setAsTitle()
1507 {
1508 $this->type = 'title';
1509 return $this;
1510 }
1511
1512
1519 public function setAsMultiSelect($fieldOptions)
1520 {
1521 if (is_array($fieldOptions)) {
1522 $this->fieldOptions = $fieldOptions;
1523 }
1524
1525 $this->type = 'multiselect';
1526 return $this;
1527 }
1528
1535 public function setAsSelect($fieldOptions)
1536 {
1537 if (is_array($fieldOptions)) {
1538 $this->fieldOptions = $fieldOptions;
1539 }
1540
1541 $this->type = 'select';
1542 return $this;
1543 }
1544
1550 public function setAsSelectUser()
1551 {
1552 $this->type = 'selectUser';
1553 return $this;
1554 }
1555
1561 public function setAsSelectBankAccount()
1562 {
1563 $this->type = 'selectBankAccount';
1564 return $this;
1565 }
1566
1573 public function setAsPassword()
1574 {
1575 $this->type = 'password';
1576 return $this;
1577 }
1578
1585 public function setAsGenericPassword()
1586 {
1587 $this->type = 'genericpassword';
1588 return $this;
1589 }
1590}
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()
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)
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:149
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Output javascript to autoset a generated password using default module into a HTML element.