dolibarr 19.0.3
html.formsetup.class.php
1<?php
2/* Copyright (C) 2021 John BOTELLA <john.botella@atm-consulting.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
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
94 public function __construct($db, $outputLangs = null)
95 {
96 global $conf, $langs;
97
98 $this->db = $db;
99
100 $this->form = new Form($this->db);
101 $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
102
103 $this->formHiddenInputs['token'] = newToken();
104 $this->formHiddenInputs['action'] = 'update';
105
106 $this->entity = (is_null($this->entity) ? $conf->entity : $this->entity);
107
108 if ($outputLangs) {
109 $this->langs = $outputLangs;
110 } else {
111 $this->langs = $langs;
112 }
113 }
114
121 public static function generateAttributesStringFromArray($attributes)
122 {
123 $Aattr = array();
124 if (is_array($attributes)) {
125 foreach ($attributes as $attribute => $value) {
126 if (is_array($value) || is_object($value)) {
127 continue;
128 }
129 $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
130 }
131 }
132
133 return !empty($Aattr) ? implode(' ', $Aattr) : '';
134 }
135
136
143 public function generateOutput($editMode = false)
144 {
145 global $hookmanager, $action, $langs;
146 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
147
148 $parameters = array(
149 'editMode' => $editMode
150 );
151 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
152 if ($reshook < 0) {
153 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
154 }
155
156 if ($reshook > 0) {
157 return $hookmanager->resPrint;
158 } else {
159 $out = '<!-- Start generateOutput from FormSetup class -->';
160 $out.= $this->htmlBeforeOutputForm;
161
162 if ($editMode) {
163 $out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
164
165 // generate hidden values from $this->formHiddenInputs
166 if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
167 foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
168 $out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
169 }
170 }
171 }
172
173 // generate output table
174 $out .= $this->generateTableOutput($editMode);
175
176
177 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
178 if ($reshook < 0) {
179 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
180 }
181
182 if ($reshook > 0) {
183 return $hookmanager->resPrint;
184 } elseif ($editMode) {
185 $out .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
186 $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
187 $out.= $this->htmlOutputMoreButton;
188 $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
189 $out .= ' &nbsp;&nbsp; ';
190 $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$langs->trans('Cancel').'</a>';
191 $out .= '</div>';
192 }
193
194 if ($editMode) {
195 $out .= '</form>';
196 }
197
198 $out.= $this->htmlAfterOutputForm;
199
200 return $out;
201 }
202 }
203
210 public function generateTableOutput($editMode = false)
211 {
212 global $hookmanager, $action;
213 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
214
215 $parameters = array(
216 'editMode' => $editMode
217 );
218 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
219 if ($reshook < 0) {
220 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
221 }
222
223 if ($reshook > 0) {
224 return $hookmanager->resPrint;
225 } else {
226 $out = '<table class="noborder centpercent">';
227 $out .= '<thead>';
228 $out .= '<tr class="liste_titre">';
229 $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
230 $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
231 $out .= '</tr>';
232 $out .= '</thead>';
233
234 // Sort items before render
235 $this->sortingItems();
236
237 $out .= '<tbody>';
238 foreach ($this->items as $item) {
239 $out .= $this->generateLineOutput($item, $editMode);
240 }
241 $out .= '</tbody>';
242
243 $out .= '</table>';
244 return $out;
245 }
246 }
247
254 public function saveConfFromPost($noMessageInUpdate = false)
255 {
256 global $hookmanager, $conf;
257
258 $parameters = array();
259 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
260 if ($reshook < 0) {
261 $this->errors = $hookmanager->errors;
262 return -1;
263 }
264
265 if ($reshook > 0) {
266 return $reshook;
267 }
268
269
270 if (empty($this->items)) {
271 return null;
272 }
273
274 $this->db->begin();
275 $error = 0;
276 foreach ($this->items as $item) {
277 if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
278 continue;
279 }
280
281 $res = $item->setValueFromPost();
282 if ($res > 0) {
283 $item->saveConfValue();
284 } elseif ($res < 0) {
285 $error++;
286 break;
287 }
288 }
289
290 if (!$error) {
291 $this->db->commit();
292 if (empty($noMessageInUpdate)) {
293 setEventMessages($this->langs->trans("SetupSaved"), null);
294 }
295 return 1;
296 } else {
297 $this->db->rollback();
298 if (empty($noMessageInUpdate)) {
299 setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
300 }
301 return -1;
302 }
303 }
304
312 public function generateLineOutput($item, $editMode = false)
313 {
314 $out = '';
315 if ($item->enabled==1) {
316 $trClass = 'oddeven';
317 if ($item->getType() == 'title') {
318 $trClass = 'liste_titre';
319 }
320
321 $this->setupNotEmpty++;
322 $out.= '<tr class="'.$trClass.'">';
323
324 $out.= '<td class="col-setup-title">';
325 $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
326 $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
327 $out.= '</span>';
328 $out.= '</td>';
329
330 $out.= '<td>';
331
332 if ($editMode) {
333 $out.= $item->generateInputField();
334 } else {
335 $out.= $item->generateOutputField();
336 }
337
338 if (!empty($item->errors)) {
339 // TODO : move set event message in a methode to be called by cards not by this class
340 setEventMessages(null, $item->errors, 'errors');
341 }
342
343 $out.= '</td>';
344 $out.= '</tr>';
345 }
346
347 return $out;
348 }
349
350
357 public function addItemsFromParamsArray($params)
358 {
359 if (!is_array($params) || empty($params)) {
360 return false;
361 }
362 foreach ($params as $confKey => $param) {
363 $this->addItemFromParams($confKey, $param); // todo manage error
364 }
365 return true;
366 }
367
368
377 public function addItemFromParams($confKey, $params)
378 {
379 if (empty($confKey) || empty($params['type'])) {
380 return false;
381 }
382
383 /*
384 * Exemple from old module builder setup page
385 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
386 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
387 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
388 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
389 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
390 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
391 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
392 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
393 */
394
395 $item = new FormSetupItem($confKey);
396 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage
397 $item->setTypeFromTypeString($params['type']);
398
399 if (!empty($params['enabled'])) {
400 $item->enabled = $params['enabled'];
401 }
402
403 if (!empty($params['css'])) {
404 $item->cssClass = $params['css'];
405 }
406
407 $this->items[$item->confKey] = $item;
408
409 return true;
410 }
411
418 public function exportItemsAsParamsArray()
419 {
420 $arrayofparameters = array();
421 foreach ($this->items as $item) {
422 $arrayofparameters[$item->confKey] = array(
423 'type' => $item->getType(),
424 'enabled' => $item->enabled
425 );
426 }
427
428 return $arrayofparameters;
429 }
430
437 public function reloadConfs()
438 {
439 if (!array($this->items)) {
440 return false;
441 }
442 foreach ($this->items as $item) {
443 $item->loadValueFromConf();
444 }
445
446 return true;
447 }
448
449
459 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
460 {
461 $item = new FormSetupItem($confKey);
462
463 $item->entity = $this->entity;
464
465 // set item rank if not defined as last item
466 if (empty($item->rank)) {
467 $item->rank = $this->getCurentItemMaxRank() + 1;
468 $this->setItemMaxRank($item->rank); // set new max rank if needed
469 }
470
471 // try to get rank from target column, this will override item->rank
472 if (!empty($targetItemKey)) {
473 if (isset($this->items[$targetItemKey])) {
474 $targetItem = $this->items[$targetItemKey];
475 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
476 if ($targetItem->rank >= 0 && $insertAfterTarget) {
477 $item->rank++;
478 }
479 }
480
481 // calc new rank for each item to make place for new item
482 foreach ($this->items as $fItem) {
483 if ($item->rank <= $fItem->rank) {
484 $fItem->rank = $fItem->rank + 1;
485 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
486 }
487 }
488 }
489
490 $this->items[$item->confKey] = $item;
491 return $this->items[$item->confKey];
492 }
493
499 public function sortingItems()
500 {
501 // Sorting
502 return uasort($this->items, array($this, 'itemSort'));
503 }
504
511 public function getCurentItemMaxRank($cache = true)
512 {
513 if (empty($this->items)) {
514 return 0;
515 }
516
517 if ($cache && $this->maxItemRank > 0) {
518 return $this->maxItemRank;
519 }
520
521 $this->maxItemRank = 0;
522 foreach ($this->items as $item) {
523 $this->maxItemRank = max($this->maxItemRank, $item->rank);
524 }
525
526 return $this->maxItemRank;
527 }
528
529
536 public function setItemMaxRank($rank)
537 {
538 $this->maxItemRank = max($this->maxItemRank, $rank);
539 }
540
541
548 public function getLineRank($itemKey)
549 {
550 if (!isset($this->items[$itemKey]->rank)) {
551 return -1;
552 }
553 return $this->items[$itemKey]->rank;
554 }
555
556
564 public function itemSort(FormSetupItem $a, FormSetupItem $b)
565 {
566 if (empty($a->rank)) {
567 $a->rank = 0;
568 }
569 if (empty($b->rank)) {
570 $b->rank = 0;
571 }
572 if ($a->rank == $b->rank) {
573 return 0;
574 }
575 return ($a->rank < $b->rank) ? -1 : 1;
576 }
577}
578
583{
587 public $db;
588
590 public $langs;
591
593 public $entity;
594
596 public $form;
597
599 public $confKey;
600
602 public $nameText = false;
603
605 public $helpText = '';
606
609
611 public $defaultFieldValue = null;
612
614 public $fieldAttr = array();
615
617 public $fieldOverride = false;
618
620 public $fieldInputOverride = false;
621
623 public $fieldOutputOverride = false;
624
626 public $rank = 0;
627
629 public $fieldOptions = array();
630
633
636
640 public $errors = array();
641
648 protected $type = 'string';
649
650 public $enabled = 1;
651
652 public $cssClass = '';
653
659 public function __construct($confKey)
660 {
661 global $langs, $db, $conf, $form;
662 $this->db = $db;
663
664 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
665 $this->form = $form;
666 } else {
667 $this->form = new Form($this->db);
668 }
669
670 $this->langs = $langs;
671 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
672
673 $this->confKey = $confKey;
674 $this->loadValueFromConf();
675 }
676
682 public function loadValueFromConf()
683 {
684 global $conf;
685 if (isset($conf->global->{$this->confKey})) {
686 $this->fieldValue = getDolGlobalString($this->confKey);
687 return true;
688 } else {
689 $this->fieldValue = null;
690 return false;
691 }
692 }
693
700 public function reloadValueFromConf()
701 {
702 return $this->loadValueFromConf();
703 }
704
705
711 public function saveConfValue()
712 {
713 global $hookmanager;
714
715 $parameters = array();
716 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
717 if ($reshook < 0) {
718 $this->setErrors($hookmanager->errors);
719 return -1;
720 }
721
722 if ($reshook > 0) {
723 return $reshook;
724 }
725
726
727 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
728 return call_user_func($this->saveCallBack, $this);
729 }
730
731 // Modify constant only if key was posted (avoid resetting key to the null value)
732 if ($this->type != 'title') {
733 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
734 if ($result < 0) {
735 return -1;
736 } else {
737 return 1;
738 }
739 }
740
741 return 0;
742 }
743
750 public function setSaveCallBack(callable $callBack)
751 {
752 $this->saveCallBack = $callBack;
753 }
754
761 public function setValueFromPostCallBack(callable $callBack)
762 {
763 $this->setValueFromPostCallBack = $callBack;
764 }
765
771 public function setValueFromPost()
772 {
773 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
774 return call_user_func($this->setValueFromPostCallBack);
775 }
776
777 // Modify constant only if key was posted (avoid resetting key to the null value)
778 if ($this->type != 'title') {
779 if (preg_match('/category:/', $this->type)) {
780 if (GETPOST($this->confKey, 'int') == '-1') {
781 $val_const = '';
782 } else {
783 $val_const = GETPOST($this->confKey, 'int');
784 }
785 } elseif ($this->type == 'multiselect') {
786 $val = GETPOST($this->confKey, 'array');
787 if ($val && is_array($val)) {
788 $val_const = implode(',', $val);
789 } else {
790 $val_const = '';
791 }
792 } elseif ($this->type == 'html') {
793 $val_const = GETPOST($this->confKey, 'restricthtml');
794 } else {
795 $val_const = GETPOST($this->confKey, 'alpha');
796 }
797
798 // TODO add value check with class validate
799 $this->fieldValue = $val_const;
800
801 return 1;
802 }
803
804 return 0;
805 }
806
812 public function getHelpText()
813 {
814 if (!empty($this->helpText)) {
815 return $this->helpText;
816 }
817 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
818 }
819
825 public function getNameText()
826 {
827 if (!empty($this->nameText)) {
828 return $this->nameText;
829 }
830 return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
831 }
832
838 public function generateInputField()
839 {
840 global $conf;
841
842 if (!empty($this->fieldOverride)) {
843 return $this->fieldOverride;
844 }
845
846 if (!empty($this->fieldInputOverride)) {
847 return $this->fieldInputOverride;
848 }
849
850 // Set default value
851 if (is_null($this->fieldValue)) {
852 $this->fieldValue = $this->defaultFieldValue;
853 }
854
855
856 $this->fieldAttr['name'] = $this->confKey;
857 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
858 $this->fieldAttr['value'] = $this->fieldValue;
859
860 $out = '';
861
862 if ($this->type == 'title') {
863 $out.= $this->generateOutputField(); // title have no input
864 } elseif ($this->type == 'multiselect') {
865 $out.= $this->generateInputFieldMultiSelect();
866 } elseif ($this->type == 'select') {
867 $out.= $this->generateInputFieldSelect();
868 } elseif ($this->type == 'selectUser') {
869 $out.= $this->generateInputFieldSelectUser();
870 } elseif ($this->type == 'textarea') {
871 $out.= $this->generateInputFieldTextarea();
872 } elseif ($this->type== 'html') {
873 $out.= $this->generateInputFieldHtml();
874 } elseif ($this->type== 'color') {
875 $out.= $this->generateInputFieldColor();
876 } elseif ($this->type == 'yesno') {
877 if (!empty($conf->use_javascript_ajax)) {
878 $out.= ajax_constantonoff($this->confKey);
879 } else {
880 $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
881 }
882 } elseif (preg_match('/emailtemplate:/', $this->type)) {
883 $out.= $this->generateInputFieldEmailTemplate();
884 } elseif (preg_match('/category:/', $this->type)) {
885 $out.=$this->generateInputFieldCategories();
886 } elseif (preg_match('/thirdparty_type/', $this->type)) {
887 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
888 $formcompany = new FormCompany($this->db);
889 $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
890 } elseif ($this->type == 'securekey') {
891 $out.= $this->generateInputFieldSecureKey();
892 } elseif ($this->type == 'product') {
893 if (isModEnabled("product") || isModEnabled("service")) {
894 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
895 $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
896 }
897 } else {
898 $out.= $this->generateInputFieldText();
899 }
900
901 return $out;
902 }
903
909 public function generateInputFieldText()
910 {
911 if (empty($this->fieldAttr)) {
912 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
913 }
914 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
915 }
916
923 {
924 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
925 $out.= dol_htmlentities($this->fieldValue);
926 $out.= "</textarea>\n";
927 return $out;
928 }
929
935 public function generateInputFieldHtml()
936 {
937 global $conf;
938 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
939 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
940 return $doleditor->Create(1);
941 }
942
948 {
949 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
950 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
951 $formother = new FormOther($this->db);
952
953 $tmp = explode(':', $this->type);
954 $out = img_picto('', 'category', 'class="pictofixedwidth"');
955 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
956
957 return $out;
958 }
959
965 {
966 global $user;
967
968 $out = '';
969 if (preg_match('/emailtemplate:/', $this->type)) {
970 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
971 $formmail = new FormMail($this->db);
972
973 $tmp = explode(':', $this->type);
974 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
975 $arrayOfMessageName = array();
976 if (is_array($formmail->lines_model)) {
977 foreach ($formmail->lines_model as $modelMail) {
978 $moreonlabel = '';
979 if (!empty($arrayOfMessageName[$modelMail->label])) {
980 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
981 }
982 // The 'label' is the key that is unique if we exclude the language
983 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
984 }
985 }
986 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
987 }
988
989 return $out;
990 }
991
992
999 {
1000 global $conf;
1001 $out = '<input required="required" type="text" class="flat" id="'.$this->confKey.'" name="'.$this->confKey.'" value="'.(GETPOST($this->confKey, 'alpha') ? GETPOST($this->confKey, 'alpha') : $this->fieldValue).'" size="40">';
1002 if (!empty($conf->use_javascript_ajax)) {
1003 $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1004 }
1005
1006 // Add button to autosuggest a key
1007 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1008 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1009
1010 return $out;
1011 }
1012
1013
1020 {
1021 $TSelected = array();
1022 if ($this->fieldValue) {
1023 $TSelected = explode(',', $this->fieldValue);
1024 }
1025
1026 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1027 }
1028
1029
1036 {
1037 return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
1038 }
1039
1044 {
1045 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1046 }
1047
1055 public function getType()
1056 {
1057 return $this->type;
1058 }
1059
1069 public function setTypeFromTypeString($type)
1070 {
1071 $this->type = $type;
1072
1073 return true;
1074 }
1075
1082 public function setErrors($errors)
1083 {
1084 if (is_array($errors)) {
1085 if (!empty($errors)) {
1086 foreach ($errors as $error) {
1087 $this->setErrors($error);
1088 }
1089 }
1090 } elseif (!empty($errors)) {
1091 $this->errors[] = $errors;
1092 }
1093 }
1094
1100 public function generateOutputField()
1101 {
1102 global $conf, $user, $langs;
1103
1104 if (!empty($this->fieldOverride)) {
1105 return $this->fieldOverride;
1106 }
1107
1108 if (!empty($this->fieldOutputOverride)) {
1109 return $this->fieldOutputOverride;
1110 }
1111
1112 $out = '';
1113
1114 if ($this->type == 'title') {
1115 // nothing to do
1116 } elseif ($this->type == 'textarea') {
1117 $out.= dol_nl2br($this->fieldValue);
1118 } elseif ($this->type == 'multiselect') {
1119 $out.= $this->generateOutputFieldMultiSelect();
1120 } elseif ($this->type == 'select') {
1121 $out.= $this->generateOutputFieldSelect();
1122 } elseif ($this->type == 'selectUser') {
1123 $out.= $this->generateOutputFieldSelectUser();
1124 } elseif ($this->type == 'html') {
1125 $out.= $this->fieldValue;
1126 } elseif ($this->type == 'color') {
1127 $out.= $this->generateOutputFieldColor();
1128 } elseif ($this->type == 'yesno') {
1129 if (!empty($conf->use_javascript_ajax)) {
1130 $out.= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1131 } else {
1132 if ($this->fieldValue == 1) {
1133 $out.= $langs->trans('yes');
1134 } else {
1135 $out.= $langs->trans('no');
1136 }
1137 }
1138 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1139 if ($this->fieldValue > 0) {
1140 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1141 $formmail = new FormMail($this->db);
1142
1143 $tmp = explode(':', $this->type);
1144
1145 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1146 if (is_numeric($template) && $template < 0) {
1147 $this->setErrors($formmail->errors);
1148 }
1149 $out.= $this->langs->trans($template->label);
1150 }
1151 } elseif (preg_match('/category:/', $this->type)) {
1152 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1153 $c = new Categorie($this->db);
1154 $result = $c->fetch($this->fieldValue);
1155 if ($result < 0) {
1156 $this->setErrors($c->errors);
1157 }
1158 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1159 $toprint = array();
1160 foreach ($ways as $way) {
1161 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1162 }
1163 $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1164 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1165 if ($this->fieldValue==2) {
1166 $out.= $this->langs->trans("Prospect");
1167 } elseif ($this->fieldValue==3) {
1168 $out.= $this->langs->trans("ProspectCustomer");
1169 } elseif ($this->fieldValue==1) {
1170 $out.= $this->langs->trans("Customer");
1171 } elseif ($this->fieldValue==0) {
1172 $out.= $this->langs->trans("NorProspectNorCustomer");
1173 }
1174 } elseif ($this->type == 'product') {
1175 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1176
1177 $product = new Product($this->db);
1178 $resprod = $product->fetch($this->fieldValue);
1179 if ($resprod > 0) {
1180 $out.= $product->ref;
1181 } elseif ($resprod < 0) {
1182 $this->setErrors($product->errors);
1183 }
1184 } else {
1185 $out.= $this->fieldValue;
1186 }
1187
1188 return $out;
1189 }
1190
1191
1198 {
1199 $outPut = '';
1200 $TSelected = array();
1201 if (!empty($this->fieldValue)) {
1202 $TSelected = explode(',', $this->fieldValue);
1203 }
1204
1205 if (!empty($TSelected)) {
1206 foreach ($TSelected as $selected) {
1207 if (!empty($this->fieldOptions[$selected])) {
1208 $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1209 }
1210 }
1211 }
1212 return $outPut;
1213 }
1214
1221 {
1222 $this->fieldAttr['disabled']=null;
1223 return $this->generateInputField();
1224 }
1230 public function generateInputFieldColor()
1231 {
1232 $this->fieldAttr['type']= 'color';
1233 return $this->generateInputFieldText();
1234 }
1235
1242 {
1243 $outPut = '';
1244 if (!empty($this->fieldOptions[$this->fieldValue])) {
1245 $outPut = $this->fieldOptions[$this->fieldValue];
1246 }
1247
1248 return $outPut;
1249 }
1250
1257 {
1258 $outPut = '';
1259 $user = new User($this->db);
1260 $user->fetch($this->fieldValue);
1261 $outPut = $user->firstname . " " . $user->lastname;
1262 return $outPut;
1263 }
1264
1265 /*
1266 * METHODS FOR SETTING DISPLAY TYPE
1267 */
1268
1274 public function setAsString()
1275 {
1276 $this->type = 'string';
1277 return $this;
1278 }
1279
1285 public function setAsColor()
1286 {
1287 $this->type = 'color';
1288 return $this;
1289 }
1290
1296 public function setAsTextarea()
1297 {
1298 $this->type = 'textarea';
1299 return $this;
1300 }
1301
1307 public function setAsHtml()
1308 {
1309 $this->type = 'html';
1310 return $this;
1311 }
1312
1319 public function setAsEmailTemplate($templateType)
1320 {
1321 $this->type = 'emailtemplate:'.$templateType;
1322 return $this;
1323 }
1324
1330 public function setAsThirdpartyType()
1331 {
1332 $this->type = 'thirdparty_type';
1333 return $this;
1334 }
1335
1341 public function setAsYesNo()
1342 {
1343 $this->type = 'yesno';
1344 return $this;
1345 }
1346
1352 public function setAsSecureKey()
1353 {
1354 $this->type = 'securekey';
1355 return $this;
1356 }
1357
1363 public function setAsProduct()
1364 {
1365 $this->type = 'product';
1366 return $this;
1367 }
1368
1376 public function setAsCategory($catType)
1377 {
1378 $this->type = 'category:'.$catType;
1379 return $this;
1380 }
1381
1387 public function setAsTitle()
1388 {
1389 $this->type = 'title';
1390 return $this;
1391 }
1392
1393
1400 public function setAsMultiSelect($fieldOptions)
1401 {
1402 if (is_array($fieldOptions)) {
1403 $this->fieldOptions = $fieldOptions;
1404 }
1405
1406 $this->type = 'multiselect';
1407 return $this;
1408 }
1409
1416 public function setAsSelect($fieldOptions)
1417 {
1418 if (is_array($fieldOptions)) {
1419 $this->fieldOptions = $fieldOptions;
1420 }
1421
1422 $this->type = 'select';
1423 return $this;
1424 }
1425
1431 public function setAsSelectUser()
1432 {
1433 $this->type = 'selectUser';
1434 return $this;
1435 }
1436}
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 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.
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Classe 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 tagret 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 convertion to this form usage.
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
generateOutput($editMode=false)
generateOutput
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
generateLineOutput($item, $editMode=false)
generateLineOutput
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
generateTableOutput($editMode=false)
generateTableOutput
This class help to create item for class formSetup.
reloadValueFromConf()
reload conf value from databases is an aliase 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.
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.
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.
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.
setAsTextarea()
Set type of input as textarea.
Class to manage products or services.
Class to manage Dolibarr users.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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:121
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Ouput javacript to autoset a generated password using default module into a HTML element.