dolibarr 19.0.4
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 if ($reshook > 0) {
265 return $reshook;
266 }
267
268
269 if (empty($this->items)) {
270 return null;
271 }
272
273 $this->db->begin();
274 $error = 0;
275 foreach ($this->items as $item) {
276 if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
277 continue;
278 }
279
280 $res = $item->setValueFromPost();
281 if ($res > 0) {
282 $item->saveConfValue();
283 } elseif ($res < 0) {
284 $error++;
285 break;
286 }
287 }
288
289 if (!$error) {
290 $this->db->commit();
291 if (empty($noMessageInUpdate)) {
292 setEventMessages($this->langs->trans("SetupSaved"), null);
293 }
294 return 1;
295 } else {
296 $this->db->rollback();
297 if (empty($noMessageInUpdate)) {
298 setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
299 }
300 return -1;
301 }
302 }
303
311 public function generateLineOutput($item, $editMode = false)
312 {
313 $out = '';
314 if ($item->enabled==1) {
315 $trClass = 'oddeven';
316 if ($item->getType() == 'title') {
317 $trClass = 'liste_titre';
318 }
319
320 $this->setupNotEmpty++;
321 $out.= '<tr class="'.$trClass.'">';
322
323 $out.= '<td class="col-setup-title">';
324 $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
325 $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
326 $out.= '</span>';
327 $out.= '</td>';
328
329 $out.= '<td>';
330
331 if ($editMode) {
332 $out.= $item->generateInputField();
333 } else {
334 $out.= $item->generateOutputField();
335 }
336
337 if (!empty($item->errors)) {
338 // TODO : move set event message in a methode to be called by cards not by this class
339 setEventMessages(null, $item->errors, 'errors');
340 }
341
342 $out.= '</td>';
343 $out.= '</tr>';
344 }
345
346 return $out;
347 }
348
349
356 public function addItemsFromParamsArray($params)
357 {
358 if (!is_array($params) || empty($params)) {
359 return false;
360 }
361 foreach ($params as $confKey => $param) {
362 $this->addItemFromParams($confKey, $param); // todo manage error
363 }
364 return true;
365 }
366
367
376 public function addItemFromParams($confKey, $params)
377 {
378 if (empty($confKey) || empty($params['type'])) {
379 return false;
380 }
381
382 /*
383 * Exemple from old module builder setup page
384 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
385 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
386 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
387 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
388 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
389 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
390 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
391 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
392 */
393
394 $item = new FormSetupItem($confKey);
395 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage
396 $item->setTypeFromTypeString($params['type']);
397
398 if (!empty($params['enabled'])) {
399 $item->enabled = $params['enabled'];
400 }
401
402 if (!empty($params['css'])) {
403 $item->cssClass = $params['css'];
404 }
405
406 $this->items[$item->confKey] = $item;
407
408 return true;
409 }
410
417 public function exportItemsAsParamsArray()
418 {
419 $arrayofparameters = array();
420 foreach ($this->items as $item) {
421 $arrayofparameters[$item->confKey] = array(
422 'type' => $item->getType(),
423 'enabled' => $item->enabled
424 );
425 }
426
427 return $arrayofparameters;
428 }
429
436 public function reloadConfs()
437 {
438 if (!array($this->items)) {
439 return false;
440 }
441 foreach ($this->items as $item) {
442 $item->loadValueFromConf();
443 }
444
445 return true;
446 }
447
448
458 public function newItem($confKey, $targetItemKey = '', $insertAfterTarget = false)
459 {
460 $item = new FormSetupItem($confKey);
461
462 $item->entity = $this->entity;
463
464 // set item rank if not defined as last item
465 if (empty($item->rank)) {
466 $item->rank = $this->getCurentItemMaxRank() + 1;
467 $this->setItemMaxRank($item->rank); // set new max rank if needed
468 }
469
470 // try to get rank from target column, this will override item->rank
471 if (!empty($targetItemKey)) {
472 if (isset($this->items[$targetItemKey])) {
473 $targetItem = $this->items[$targetItemKey];
474 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
475 if ($targetItem->rank >= 0 && $insertAfterTarget) {
476 $item->rank++;
477 }
478 }
479
480 // calc new rank for each item to make place for new item
481 foreach ($this->items as $fItem) {
482 if ($item->rank <= $fItem->rank) {
483 $fItem->rank = $fItem->rank + 1;
484 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
485 }
486 }
487 }
488
489 $this->items[$item->confKey] = $item;
490 return $this->items[$item->confKey];
491 }
492
498 public function sortingItems()
499 {
500 // Sorting
501 return uasort($this->items, array($this, 'itemSort'));
502 }
503
510 public function getCurentItemMaxRank($cache = true)
511 {
512 if (empty($this->items)) {
513 return 0;
514 }
515
516 if ($cache && $this->maxItemRank > 0) {
517 return $this->maxItemRank;
518 }
519
520 $this->maxItemRank = 0;
521 foreach ($this->items as $item) {
522 $this->maxItemRank = max($this->maxItemRank, $item->rank);
523 }
524
525 return $this->maxItemRank;
526 }
527
528
535 public function setItemMaxRank($rank)
536 {
537 $this->maxItemRank = max($this->maxItemRank, $rank);
538 }
539
540
547 public function getLineRank($itemKey)
548 {
549 if (!isset($this->items[$itemKey]->rank)) {
550 return -1;
551 }
552 return $this->items[$itemKey]->rank;
553 }
554
555
563 public function itemSort(FormSetupItem $a, FormSetupItem $b)
564 {
565 if (empty($a->rank)) {
566 $a->rank = 0;
567 }
568 if (empty($b->rank)) {
569 $b->rank = 0;
570 }
571 if ($a->rank == $b->rank) {
572 return 0;
573 }
574 return ($a->rank < $b->rank) ? -1 : 1;
575 }
576}
577
582{
586 public $db;
587
589 public $langs;
590
592 public $entity;
593
595 public $form;
596
598 public $confKey;
599
601 public $nameText = false;
602
604 public $helpText = '';
605
608
610 public $defaultFieldValue = null;
611
613 public $fieldAttr = array();
614
616 public $fieldOverride = false;
617
619 public $fieldInputOverride = false;
620
622 public $fieldOutputOverride = false;
623
625 public $rank = 0;
626
628 public $fieldOptions = array();
629
632
635
639 public $errors = array();
640
647 protected $type = 'string';
648
649 public $enabled = 1;
650
651 public $cssClass = '';
652
658 public function __construct($confKey)
659 {
660 global $langs, $db, $conf, $form;
661 $this->db = $db;
662
663 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
664 $this->form = $form;
665 } else {
666 $this->form = new Form($this->db);
667 }
668
669 $this->langs = $langs;
670 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
671
672 $this->confKey = $confKey;
673 $this->loadValueFromConf();
674 }
675
681 public function loadValueFromConf()
682 {
683 global $conf;
684 if (isset($conf->global->{$this->confKey})) {
685 $this->fieldValue = getDolGlobalString($this->confKey);
686 return true;
687 } else {
688 $this->fieldValue = null;
689 return false;
690 }
691 }
692
699 public function reloadValueFromConf()
700 {
701 return $this->loadValueFromConf();
702 }
703
704
710 public function saveConfValue()
711 {
712 global $hookmanager;
713
714 $parameters = array();
715 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
716 if ($reshook < 0) {
717 $this->setErrors($hookmanager->errors);
718 return -1;
719 }
720
721 if ($reshook > 0) {
722 return $reshook;
723 }
724
725
726 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
727 return call_user_func($this->saveCallBack, $this);
728 }
729
730 // Modify constant only if key was posted (avoid resetting key to the null value)
731 if ($this->type != 'title') {
732 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
733 if ($result < 0) {
734 return -1;
735 } else {
736 return 1;
737 }
738 }
739
740 return 0;
741 }
742
749 public function setSaveCallBack(callable $callBack)
750 {
751 $this->saveCallBack = $callBack;
752 }
753
760 public function setValueFromPostCallBack(callable $callBack)
761 {
762 $this->setValueFromPostCallBack = $callBack;
763 }
764
770 public function setValueFromPost()
771 {
772 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
773 return call_user_func($this->setValueFromPostCallBack);
774 }
775
776 // Modify constant only if key was posted (avoid resetting key to the null value)
777 if ($this->type != 'title') {
778 if (preg_match('/category:/', $this->type)) {
779 if (GETPOST($this->confKey, 'int') == '-1') {
780 $val_const = '';
781 } else {
782 $val_const = GETPOST($this->confKey, 'int');
783 }
784 } elseif ($this->type == 'multiselect') {
785 $val = GETPOST($this->confKey, 'array');
786 if ($val && is_array($val)) {
787 $val_const = implode(',', $val);
788 } else {
789 $val_const = '';
790 }
791 } elseif ($this->type == 'html') {
792 $val_const = GETPOST($this->confKey, 'restricthtml');
793 } else {
794 $val_const = GETPOST($this->confKey, 'alpha');
795 }
796
797 // TODO add value check with class validate
798 $this->fieldValue = $val_const;
799
800 return 1;
801 }
802
803 return 0;
804 }
805
811 public function getHelpText()
812 {
813 if (!empty($this->helpText)) {
814 return $this->helpText;
815 }
816 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
817 }
818
824 public function getNameText()
825 {
826 if (!empty($this->nameText)) {
827 return $this->nameText;
828 }
829 return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
830 }
831
837 public function generateInputField()
838 {
839 global $conf;
840
841 if (!empty($this->fieldOverride)) {
842 return $this->fieldOverride;
843 }
844
845 if (!empty($this->fieldInputOverride)) {
846 return $this->fieldInputOverride;
847 }
848
849 // Set default value
850 if (is_null($this->fieldValue)) {
851 $this->fieldValue = $this->defaultFieldValue;
852 }
853
854
855 $this->fieldAttr['name'] = $this->confKey;
856 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
857 $this->fieldAttr['value'] = $this->fieldValue;
858
859 $out = '';
860
861 if ($this->type == 'title') {
862 $out.= $this->generateOutputField(); // title have no input
863 } elseif ($this->type == 'multiselect') {
864 $out.= $this->generateInputFieldMultiSelect();
865 } elseif ($this->type == 'select') {
866 $out.= $this->generateInputFieldSelect();
867 } elseif ($this->type == 'selectUser') {
868 $out.= $this->generateInputFieldSelectUser();
869 } elseif ($this->type == 'textarea') {
870 $out.= $this->generateInputFieldTextarea();
871 } elseif ($this->type== 'html') {
872 $out.= $this->generateInputFieldHtml();
873 } elseif ($this->type== 'color') {
874 $out.= $this->generateInputFieldColor();
875 } elseif ($this->type == 'yesno') {
876 if (!empty($conf->use_javascript_ajax)) {
877 $out.= ajax_constantonoff($this->confKey);
878 } else {
879 $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
880 }
881 } elseif (preg_match('/emailtemplate:/', $this->type)) {
882 $out.= $this->generateInputFieldEmailTemplate();
883 } elseif (preg_match('/category:/', $this->type)) {
884 $out.=$this->generateInputFieldCategories();
885 } elseif (preg_match('/thirdparty_type/', $this->type)) {
886 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
887 $formcompany = new FormCompany($this->db);
888 $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
889 } elseif ($this->type == 'securekey') {
890 $out.= $this->generateInputFieldSecureKey();
891 } elseif ($this->type == 'product') {
892 if (isModEnabled("product") || isModEnabled("service")) {
893 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
894 $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
895 }
896 } else {
897 $out.= $this->generateInputFieldText();
898 }
899
900 return $out;
901 }
902
908 public function generateInputFieldText()
909 {
910 if (empty($this->fieldAttr)) {
911 $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass);
912 }
913 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
914 }
915
922 {
923 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
924 $out.= dol_htmlentities($this->fieldValue);
925 $out.= "</textarea>\n";
926 return $out;
927 }
928
934 public function generateInputFieldHtml()
935 {
936 global $conf;
937 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
938 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
939 return $doleditor->Create(1);
940 }
941
947 {
948 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
949 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
950 $formother = new FormOther($this->db);
951
952 $tmp = explode(':', $this->type);
953 $out = img_picto('', 'category', 'class="pictofixedwidth"');
954 $out .= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
955
956 return $out;
957 }
958
964 {
965 global $user;
966
967 $out = '';
968 if (preg_match('/emailtemplate:/', $this->type)) {
969 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
970 $formmail = new FormMail($this->db);
971
972 $tmp = explode(':', $this->type);
973 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
974 $arrayOfMessageName = array();
975 if (is_array($formmail->lines_model)) {
976 foreach ($formmail->lines_model as $modelMail) {
977 $moreonlabel = '';
978 if (!empty($arrayOfMessageName[$modelMail->label])) {
979 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
980 }
981 // The 'label' is the key that is unique if we exclude the language
982 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
983 }
984 }
985 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
986 }
987
988 return $out;
989 }
990
991
998 {
999 global $conf;
1000 $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">';
1001 if (!empty($conf->use_javascript_ajax)) {
1002 $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
1003 }
1004
1005 // Add button to autosuggest a key
1006 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
1007 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
1008
1009 return $out;
1010 }
1011
1012
1019 {
1020 $TSelected = array();
1021 if ($this->fieldValue) {
1022 $TSelected = explode(',', $this->fieldValue);
1023 }
1024
1025 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1026 }
1027
1028
1035 {
1036 return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
1037 }
1038
1043 {
1044 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1045 }
1046
1054 public function getType()
1055 {
1056 return $this->type;
1057 }
1058
1068 public function setTypeFromTypeString($type)
1069 {
1070 $this->type = $type;
1071
1072 return true;
1073 }
1074
1081 public function setErrors($errors)
1082 {
1083 if (is_array($errors)) {
1084 if (!empty($errors)) {
1085 foreach ($errors as $error) {
1086 $this->setErrors($error);
1087 }
1088 }
1089 } elseif (!empty($errors)) {
1090 $this->errors[] = $errors;
1091 }
1092 }
1093
1099 public function generateOutputField()
1100 {
1101 global $conf, $user, $langs;
1102
1103 if (!empty($this->fieldOverride)) {
1104 return $this->fieldOverride;
1105 }
1106
1107 if (!empty($this->fieldOutputOverride)) {
1108 return $this->fieldOutputOverride;
1109 }
1110
1111 $out = '';
1112
1113 if ($this->type == 'title') {
1114 // nothing to do
1115 } elseif ($this->type == 'textarea') {
1116 $out.= dol_nl2br($this->fieldValue);
1117 } elseif ($this->type == 'multiselect') {
1118 $out.= $this->generateOutputFieldMultiSelect();
1119 } elseif ($this->type == 'select') {
1120 $out.= $this->generateOutputFieldSelect();
1121 } elseif ($this->type == 'selectUser') {
1122 $out.= $this->generateOutputFieldSelectUser();
1123 } elseif ($this->type == 'html') {
1124 $out.= $this->fieldValue;
1125 } elseif ($this->type == 'color') {
1126 $out.= $this->generateOutputFieldColor();
1127 } elseif ($this->type == 'yesno') {
1128 if (!empty($conf->use_javascript_ajax)) {
1129 $out.= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1130 } else {
1131 if ($this->fieldValue == 1) {
1132 $out.= $langs->trans('yes');
1133 } else {
1134 $out.= $langs->trans('no');
1135 }
1136 }
1137 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1138 if ($this->fieldValue > 0) {
1139 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1140 $formmail = new FormMail($this->db);
1141
1142 $tmp = explode(':', $this->type);
1143
1144 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1145 if (is_numeric($template) && $template < 0) {
1146 $this->setErrors($formmail->errors);
1147 }
1148 $out.= $this->langs->trans($template->label);
1149 }
1150 } elseif (preg_match('/category:/', $this->type)) {
1151 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1152 $c = new Categorie($this->db);
1153 $result = $c->fetch($this->fieldValue);
1154 if ($result < 0) {
1155 $this->setErrors($c->errors);
1156 }
1157 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1158 $toprint = array();
1159 foreach ($ways as $way) {
1160 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1161 }
1162 $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1163 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1164 if ($this->fieldValue==2) {
1165 $out.= $this->langs->trans("Prospect");
1166 } elseif ($this->fieldValue==3) {
1167 $out.= $this->langs->trans("ProspectCustomer");
1168 } elseif ($this->fieldValue==1) {
1169 $out.= $this->langs->trans("Customer");
1170 } elseif ($this->fieldValue==0) {
1171 $out.= $this->langs->trans("NorProspectNorCustomer");
1172 }
1173 } elseif ($this->type == 'product') {
1174 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1175
1176 $product = new Product($this->db);
1177 $resprod = $product->fetch($this->fieldValue);
1178 if ($resprod > 0) {
1179 $out.= $product->ref;
1180 } elseif ($resprod < 0) {
1181 $this->setErrors($product->errors);
1182 }
1183 } else {
1184 $out.= $this->fieldValue;
1185 }
1186
1187 return $out;
1188 }
1189
1190
1197 {
1198 $outPut = '';
1199 $TSelected = array();
1200 if (!empty($this->fieldValue)) {
1201 $TSelected = explode(',', $this->fieldValue);
1202 }
1203
1204 if (!empty($TSelected)) {
1205 foreach ($TSelected as $selected) {
1206 if (!empty($this->fieldOptions[$selected])) {
1207 $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1208 }
1209 }
1210 }
1211 return $outPut;
1212 }
1213
1220 {
1221 $this->fieldAttr['disabled']=null;
1222 return $this->generateInputField();
1223 }
1229 public function generateInputFieldColor()
1230 {
1231 $this->fieldAttr['type']= 'color';
1232 return $this->generateInputFieldText();
1233 }
1234
1241 {
1242 $outPut = '';
1243 if (!empty($this->fieldOptions[$this->fieldValue])) {
1244 $outPut = $this->fieldOptions[$this->fieldValue];
1245 }
1246
1247 return $outPut;
1248 }
1249
1256 {
1257 $outPut = '';
1258 $user = new User($this->db);
1259 $user->fetch($this->fieldValue);
1260 $outPut = $user->firstname . " " . $user->lastname;
1261 return $outPut;
1262 }
1263
1264 /*
1265 * METHODS FOR SETTING DISPLAY TYPE
1266 */
1267
1273 public function setAsString()
1274 {
1275 $this->type = 'string';
1276 return $this;
1277 }
1278
1284 public function setAsColor()
1285 {
1286 $this->type = 'color';
1287 return $this;
1288 }
1289
1295 public function setAsTextarea()
1296 {
1297 $this->type = 'textarea';
1298 return $this;
1299 }
1300
1306 public function setAsHtml()
1307 {
1308 $this->type = 'html';
1309 return $this;
1310 }
1311
1318 public function setAsEmailTemplate($templateType)
1319 {
1320 $this->type = 'emailtemplate:'.$templateType;
1321 return $this;
1322 }
1323
1329 public function setAsThirdpartyType()
1330 {
1331 $this->type = 'thirdparty_type';
1332 return $this;
1333 }
1334
1340 public function setAsYesNo()
1341 {
1342 $this->type = 'yesno';
1343 return $this;
1344 }
1345
1351 public function setAsSecureKey()
1352 {
1353 $this->type = 'securekey';
1354 return $this;
1355 }
1356
1362 public function setAsProduct()
1363 {
1364 $this->type = 'product';
1365 return $this;
1366 }
1367
1375 public function setAsCategory($catType)
1376 {
1377 $this->type = 'category:'.$catType;
1378 return $this;
1379 }
1380
1386 public function setAsTitle()
1387 {
1388 $this->type = 'title';
1389 return $this;
1390 }
1391
1392
1399 public function setAsMultiSelect($fieldOptions)
1400 {
1401 if (is_array($fieldOptions)) {
1402 $this->fieldOptions = $fieldOptions;
1403 }
1404
1405 $this->type = 'multiselect';
1406 return $this;
1407 }
1408
1415 public function setAsSelect($fieldOptions)
1416 {
1417 if (is_array($fieldOptions)) {
1418 $this->fieldOptions = $fieldOptions;
1419 }
1420
1421 $this->type = 'select';
1422 return $this;
1423 }
1424
1430 public function setAsSelectUser()
1431 {
1432 $this->type = 'selectUser';
1433 return $this;
1434 }
1435}
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.