dolibarr 18.0.8
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 = false)
95 {
96 global $conf, $langs;
97
98 $this->db = $db;
99 $this->form = new Form($this->db);
100 $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
101
102 $this->formHiddenInputs['token'] = newToken();
103 $this->formHiddenInputs['action'] = 'update';
104
105 $this->entity = (is_null($this->entity) ? $conf->entity : $this->entity);
106
107 if ($outputLangs) {
108 $this->langs = $outputLangs;
109 } else {
110 $this->langs = $langs;
111 }
112 }
113
120 static public function generateAttributesStringFromArray($attributes)
121 {
122 $Aattr = array();
123 if (is_array($attributes)) {
124 foreach ($attributes as $attribute => $value) {
125 if (is_array($value) || is_object($value)) {
126 continue;
127 }
128 $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
129 }
130 }
131
132 return !empty($Aattr)?implode(' ', $Aattr):'';
133 }
134
135
142 public function generateOutput($editMode = false)
143 {
144 global $hookmanager, $action, $langs;
145 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
146
147 $parameters = array(
148 'editMode' => $editMode
149 );
150 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
151 if ($reshook < 0) {
152 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
153 }
154
155 if ($reshook > 0) {
156 return $hookmanager->resPrint;
157 } else {
158 $out = '<!-- Start generateOutput from FormSetup class -->';
159 $out.= $this->htmlBeforeOutputForm;
160
161 if ($editMode) {
162 $out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
163
164 // generate hidden values from $this->formHiddenInputs
165 if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
166 foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
167 $out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
168 }
169 }
170 }
171
172 // generate output table
173 $out .= $this->generateTableOutput($editMode);
174
175
176 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
177 if ($reshook < 0) {
178 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
179 }
180
181 if ($reshook > 0) {
182 return $hookmanager->resPrint;
183 } elseif ($editMode) {
184 $out .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
185 $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
186 $out.= $this->htmlOutputMoreButton;
187 $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
188 $out .= ' &nbsp;&nbsp; ';
189 $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$langs->trans('Cancel').'</a>';
190 $out .= '</div>';
191 }
192
193 if ($editMode) {
194 $out .= '</form>';
195 }
196
197 $out.= $this->htmlAfterOutputForm;
198
199 return $out;
200 }
201 }
202
209 public function generateTableOutput($editMode = false)
210 {
211 global $hookmanager, $action;
212 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
213
214 $parameters = array(
215 'editMode' => $editMode
216 );
217 $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
218 if ($reshook < 0) {
219 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
220 }
221
222 if ($reshook > 0) {
223 return $hookmanager->resPrint;
224 } else {
225 $out = '<table class="noborder centpercent">';
226 $out .= '<thead>';
227 $out .= '<tr class="liste_titre">';
228 $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
229 $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
230 $out .= '</tr>';
231 $out .= '</thead>';
232
233 // Sort items before render
234 $this->sortingItems();
235
236 $out .= '<tbody>';
237 foreach ($this->items as $item) {
238 $out .= $this->generateLineOutput($item, $editMode);
239 }
240 $out .= '</tbody>';
241
242 $out .= '</table>';
243 return $out;
244 }
245 }
246
253 public function saveConfFromPost($noMessageInUpdate = false)
254 {
255 global $hookmanager, $conf;
256
257 $parameters = array();
258 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
259 if ($reshook < 0) {
260 $this->errors = $hookmanager->errors;
261 return -1;
262 }
263 if ($reshook > 0) {
264 return $reshook;
265 }
266
267
268 if (empty($this->items)) {
269 return null;
270 }
271
272 $this->db->begin();
273 $error = 0;
274 foreach ($this->items as $item) {
275 if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
276 continue;
277 }
278
279 $res = $item->setValueFromPost();
280 if ($res > 0) {
281 $item->saveConfValue();
282 } elseif ($res < 0) {
283 $error++;
284 break;
285 }
286 }
287
288 if (!$error) {
289 $this->db->commit();
290 if (empty($noMessageInUpdate)) {
291 setEventMessages($this->langs->trans("SetupSaved"), null);
292 }
293 return 1;
294 } else {
295 $this->db->rollback();
296 if (empty($noMessageInUpdate)) {
297 setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
298 }
299 return -1;
300 }
301 }
302
310 public function generateLineOutput($item, $editMode = false)
311 {
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)) { return false; }
359 foreach ($params as $confKey => $param) {
360 $this->addItemFromParams($confKey, $param); // todo manage error
361 }
362 return true;
363 }
364
365
374 public function addItemFromParams($confKey, $params)
375 {
376 if (empty($confKey) || empty($params['type'])) { return false; }
377
378 /*
379 * Exemple from old module builder setup page
380 * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
381 // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
382 //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
383 //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
384 //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
385 //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
386 //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
387 //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
388 */
389
390 $item = new FormSetupItem($confKey);
391 // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage
392 $item->setTypeFromTypeString($params['type']);
393
394 if (!empty($params['enabled'])) {
395 $item->enabled = $params['enabled'];
396 }
397
398 if (!empty($params['css'])) {
399 $item->cssClass = $params['css'];
400 }
401
402 $this->items[$item->confKey] = $item;
403
404 return true;
405 }
406
413 public function exportItemsAsParamsArray()
414 {
415 $arrayofparameters = array();
416 foreach ($this->items as $item) {
417 $arrayofparameters[$item->confKey] = array(
418 'type' => $item->getType(),
419 'enabled' => $item->enabled
420 );
421 }
422
423 return $arrayofparameters;
424 }
425
432 public function reloadConfs()
433 {
434
435 if (!array($this->items)) { return false; }
436 foreach ($this->items as $item) {
437 $item->loadValueFromConf();
438 }
439
440 return true;
441 }
442
443
453 public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
454 {
455 $item = new FormSetupItem($confKey);
456
457 $item->entity = $this->entity;
458
459 // set item rank if not defined as last item
460 if (empty($item->rank)) {
461 $item->rank = $this->getCurentItemMaxRank() + 1;
462 $this->setItemMaxRank($item->rank); // set new max rank if needed
463 }
464
465 // try to get rank from target column, this will override item->rank
466 if (!empty($targetItemKey)) {
467 if (isset($this->items[$targetItemKey])) {
468 $targetItem = $this->items[$targetItemKey];
469 $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
470 if ($targetItem->rank >= 0 && $insertAfterTarget) {
471 $item->rank++;
472 }
473 }
474
475 // calc new rank for each item to make place for new item
476 foreach ($this->items as $fItem) {
477 if ($item->rank <= $fItem->rank) {
478 $fItem->rank = $fItem->rank + 1;
479 $this->setItemMaxRank($fItem->rank); // set new max rank if needed
480 }
481 }
482 }
483
484 $this->items[$item->confKey] = $item;
485 return $this->items[$item->confKey];
486 }
487
493 public function sortingItems()
494 {
495 // Sorting
496 return uasort($this->items, array($this, 'itemSort'));
497 }
498
505 public function getCurentItemMaxRank($cache = true)
506 {
507 if (empty($this->items)) {
508 return 0;
509 }
510
511 if ($cache && $this->maxItemRank > 0) {
512 return $this->maxItemRank;
513 }
514
515 $this->maxItemRank = 0;
516 foreach ($this->items as $item) {
517 $this->maxItemRank = max($this->maxItemRank, $item->rank);
518 }
519
520 return $this->maxItemRank;
521 }
522
523
530 public function setItemMaxRank($rank)
531 {
532 $this->maxItemRank = max($this->maxItemRank, $rank);
533 }
534
535
542 public function getLineRank($itemKey)
543 {
544 if (!isset($this->items[$itemKey]->rank)) {
545 return -1;
546 }
547 return $this->items[$itemKey]->rank;
548 }
549
550
558 public function itemSort(FormSetupItem $a, FormSetupItem $b)
559 {
560 if (empty($a->rank)) {
561 $a->rank = 0;
562 }
563 if (empty($b->rank)) {
564 $b->rank = 0;
565 }
566 if ($a->rank == $b->rank) {
567 return 0;
568 }
569 return ($a->rank < $b->rank) ? -1 : 1;
570 }
571}
572
577{
581 public $db;
582
584 public $langs;
585
587 public $entity;
588
590 public $form;
591
593 public $confKey;
594
596 public $nameText = false;
597
599 public $helpText = '';
600
603
605 public $defaultFieldValue = null;
606
608 public $fieldAttr = array();
609
611 public $fieldOverride = false;
612
614 public $fieldInputOverride = false;
615
617 public $fieldOutputOverride = false;
618
620 public $rank = 0;
621
623 public $fieldOptions = array();
624
627
630
634 public $errors = array();
635
642 protected $type = 'string';
643
644 public $enabled = 1;
645
646 public $cssClass = '';
647
653 public function __construct($confKey)
654 {
655 global $langs, $db, $conf, $form;
656 $this->db = $db;
657
658 if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
659 $this->form = $form;
660 } else {
661 $this->form = new Form($this->db);
662 }
663
664 $this->langs = $langs;
665 $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
666
667 $this->confKey = $confKey;
668 $this->loadValueFromConf();
669 }
670
675 public function loadValueFromConf()
676 {
677 global $conf;
678 if (isset($conf->global->{$this->confKey})) {
679 $this->fieldValue = getDolGlobalString($this->confKey);
680 return true;
681 } else {
682 $this->fieldValue = null;
683 return false;
684 }
685 }
686
692 public function reloadValueFromConf()
693 {
694 return $this->loadValueFromConf();
695 }
696
697
703 public function saveConfValue()
704 {
705 global $hookmanager;
706
707 $parameters = array();
708 $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
709 if ($reshook < 0) {
710 $this->setErrors($hookmanager->errors);
711 return -1;
712 }
713
714 if ($reshook > 0) {
715 return $reshook;
716 }
717
718
719 if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
720 return call_user_func($this->saveCallBack, $this);
721 }
722
723 // Modify constant only if key was posted (avoid resetting key to the null value)
724 if ($this->type != 'title') {
725 $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
726 if ($result < 0) {
727 return -1;
728 } else {
729 return 1;
730 }
731 }
732
733 return 0;
734 }
735
742 public function setSaveCallBack(callable $callBack)
743 {
744 $this->saveCallBack = $callBack;
745 }
746
752 public function setValueFromPostCallBack(callable $callBack)
753 {
754 $this->setValueFromPostCallBack = $callBack;
755 }
756
761 public function setValueFromPost()
762 {
763 if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
764 return call_user_func($this->setValueFromPostCallBack);
765 }
766
767 // Modify constant only if key was posted (avoid resetting key to the null value)
768 if ($this->type != 'title') {
769 if (preg_match('/category:/', $this->type)) {
770 if (GETPOST($this->confKey, 'int') == '-1') {
771 $val_const = '';
772 } else {
773 $val_const = GETPOST($this->confKey, 'int');
774 }
775 } elseif ($this->type == 'multiselect') {
776 $val = GETPOST($this->confKey, 'array');
777 if ($val && is_array($val)) {
778 $val_const = implode(',', $val);
779 } else {
780 $val_const = '';
781 }
782 } elseif ($this->type == 'html') {
783 $val_const = GETPOST($this->confKey, 'restricthtml');
784 } else {
785 $val_const = GETPOST($this->confKey, 'alpha');
786 }
787
788 // TODO add value check with class validate
789 $this->fieldValue = $val_const;
790
791 return 1;
792 }
793
794 return 0;
795 }
796
801 public function getHelpText()
802 {
803 if (!empty($this->helpText)) { return $this->helpText; }
804 return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
805 }
806
811 public function getNameText()
812 {
813 if (!empty($this->nameText)) { return $this->nameText; }
814 return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
815 }
816
821 public function generateInputField()
822 {
823 global $conf;
824
825 if (!empty($this->fieldOverride)) {
826 return $this->fieldOverride;
827 }
828
829 if (!empty($this->fieldInputOverride)) {
830 return $this->fieldInputOverride;
831 }
832
833 // Set default value
834 if (is_null($this->fieldValue)) {
835 $this->fieldValue = $this->defaultFieldValue;
836 }
837
838
839 $this->fieldAttr['name'] = $this->confKey;
840 $this->fieldAttr['id'] = 'setup-'.$this->confKey;
841 $this->fieldAttr['value'] = $this->fieldValue;
842
843 $out = '';
844
845 if ($this->type == 'title') {
846 $out.= $this->generateOutputField(); // title have no input
847 } elseif ($this->type == 'multiselect') {
848 $out.= $this->generateInputFieldMultiSelect();
849 } elseif ($this->type == 'select') {
850 $out.= $this->generateInputFieldSelect();
851 } elseif ($this->type == 'selectUser') {
852 $out.= $this->generateInputFieldSelectUser();
853 } elseif ($this->type == 'textarea') {
854 $out.= $this->generateInputFieldTextarea();
855 } elseif ($this->type== 'html') {
856 $out.= $this->generateInputFieldHtml();
857 } elseif ($this->type== 'color') {
858 $out.= $this->generateInputFieldColor();
859 } elseif ($this->type == 'yesno') {
860 if (!empty($conf->use_javascript_ajax)) {
861 $out.= ajax_constantonoff($this->confKey);
862 } else {
863 $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
864 }
865 } elseif (preg_match('/emailtemplate:/', $this->type)) {
866 $out.= $this->generateInputFieldEmailTemplate();
867 } elseif (preg_match('/category:/', $this->type)) {
868 $out.=$this->generateInputFieldCategories();
869 } elseif (preg_match('/thirdparty_type/', $this->type)) {
870 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
871 $formcompany = new FormCompany($this->db);
872 $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
873 } elseif ($this->type == 'securekey') {
874 $out.= $this->generateInputFieldSecureKey();
875 } elseif ($this->type == 'product') {
876 if (isModEnabled("product") || isModEnabled("service")) {
877 $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
878 $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
879 }
880 } else {
881 $out.= $this->generateInputFieldText();
882 }
883
884 return $out;
885 }
886
891 public function generateInputFieldText()
892 {
893 if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
894 return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
895 }
896
902 {
903 $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
904 $out.= dol_htmlentities($this->fieldValue);
905 $out.= "</textarea>\n";
906 return $out;
907 }
908
913 public function generateInputFieldHtml()
914 {
915 global $conf;
916 require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
917 $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
918 return $doleditor->Create(1);
919 }
920
926 {
927 global $conf;
928 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
929 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
930 $formother = new FormOther($this->db);
931
932 $tmp = explode(':', $this->type);
933 $out= img_picto('', 'category', 'class="pictofixedwidth"');
934 $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
935 return $out;
936 }
937
943 {
944 global $conf, $user;
945 $out = '';
946 if (preg_match('/emailtemplate:/', $this->type)) {
947 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
948 $formmail = new FormMail($this->db);
949
950 $tmp = explode(':', $this->type);
951 $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
952 $arrayOfMessageName = array();
953 if (is_array($formmail->lines_model)) {
954 foreach ($formmail->lines_model as $modelMail) {
955 $moreonlabel = '';
956 if (!empty($arrayOfMessageName[$modelMail->label])) {
957 $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
958 }
959 // The 'label' is the key that is unique if we exclude the language
960 $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
961 }
962 }
963 $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
964 }
965
966 return $out;
967 }
968
969
975 {
976 global $conf;
977 $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">';
978 if (!empty($conf->use_javascript_ajax)) {
979 $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
980 }
981
982 // Add button to autosuggest a key
983 include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
984 $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
985
986 return $out;
987 }
988
989
994 {
995 $TSelected = array();
996 if ($this->fieldValue) {
997 $TSelected = explode(',', $this->fieldValue);
998 }
999
1000 return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
1001 }
1002
1003
1008 {
1009 return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
1010 }
1011
1016 {
1017 return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1018 }
1019
1027 public function getType()
1028 {
1029 return $this->type;
1030 }
1031
1041 public function setTypeFromTypeString($type)
1042 {
1043 $this->type = $type;
1044
1045 return true;
1046 }
1047
1054 public function setErrors($errors)
1055 {
1056 if (is_array($errors)) {
1057 if (!empty($errors)) {
1058 foreach ($errors as $error) {
1059 $this->setErrors($error);
1060 }
1061 }
1062 } elseif (!empty($errors)) {
1063 $this->errors[] = $errors;
1064 }
1065 }
1066
1072 public function generateOutputField()
1073 {
1074 global $conf, $user, $langs;
1075
1076 if (!empty($this->fieldOverride)) {
1077 return $this->fieldOverride;
1078 }
1079
1080 if (!empty($this->fieldOutputOverride)) {
1081 return $this->fieldOutputOverride;
1082 }
1083
1084 $out = '';
1085
1086 if ($this->type == 'title') {
1087 // nothing to do
1088 } elseif ($this->type == 'textarea') {
1089 $out.= dol_nl2br($this->fieldValue);
1090 } elseif ($this->type == 'multiselect') {
1091 $out.= $this->generateOutputFieldMultiSelect();
1092 } elseif ($this->type == 'select') {
1093 $out.= $this->generateOutputFieldSelect();
1094 } elseif ($this->type == 'selectUser') {
1095 $out.= $this->generateOutputFieldSelectUser();
1096 } elseif ($this->type== 'html') {
1097 $out.= $this->fieldValue;
1098 } elseif ($this->type== 'color') {
1099 $out.= $this->generateOutputFieldColor();
1100 } elseif ($this->type == 'yesno') {
1101 if (!empty($conf->use_javascript_ajax)) {
1102 $out.= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1103 } else {
1104 if ($this->fieldValue == 1) {
1105 $out.= $langs->trans('yes');
1106 } else {
1107 $out.= $langs->trans('no');
1108 }
1109 }
1110 } elseif (preg_match('/emailtemplate:/', $this->type)) {
1111 if ($this->fieldValue > 0) {
1112 include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1113 $formmail = new FormMail($this->db);
1114
1115 $tmp = explode(':', $this->type);
1116
1117 $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1118 if (is_numeric($template) && $template < 0) {
1119 $this->setErrors($formmail->errors);
1120 }
1121 $out.= $this->langs->trans($template->label);
1122 }
1123 } elseif (preg_match('/category:/', $this->type)) {
1124 require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1125 $c = new Categorie($this->db);
1126 $result = $c->fetch($this->fieldValue);
1127 if ($result < 0) {
1128 $this->setErrors($c->errors);
1129 }
1130 $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1131 $toprint = array();
1132 foreach ($ways as $way) {
1133 $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1134 }
1135 $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1136 } elseif (preg_match('/thirdparty_type/', $this->type)) {
1137 if ($this->fieldValue==2) {
1138 $out.= $this->langs->trans("Prospect");
1139 } elseif ($this->fieldValue==3) {
1140 $out.= $this->langs->trans("ProspectCustomer");
1141 } elseif ($this->fieldValue==1) {
1142 $out.= $this->langs->trans("Customer");
1143 } elseif ($this->fieldValue==0) {
1144 $out.= $this->langs->trans("NorProspectNorCustomer");
1145 }
1146 } elseif ($this->type == 'product') {
1147 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1148
1149 $product = new Product($this->db);
1150 $resprod = $product->fetch($this->fieldValue);
1151 if ($resprod > 0) {
1152 $out.= $product->ref;
1153 } elseif ($resprod < 0) {
1154 $this->setErrors($product->errors);
1155 }
1156 } else {
1157 $out.= $this->fieldValue;
1158 }
1159
1160 return $out;
1161 }
1162
1163
1170 {
1171 $outPut = '';
1172 $TSelected = array();
1173 if (!empty($this->fieldValue)) {
1174 $TSelected = explode(',', $this->fieldValue);
1175 }
1176
1177 if (!empty($TSelected)) {
1178 foreach ($TSelected as $selected) {
1179 if (!empty($this->fieldOptions[$selected])) {
1180 $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1181 }
1182 }
1183 }
1184 return $outPut;
1185 }
1186
1193 {
1194 $this->fieldAttr['disabled']=null;
1195 return $this->generateInputField();
1196 }
1202 public function generateInputFieldColor()
1203 {
1204 $this->fieldAttr['type']= 'color';
1205 return $this->generateInputFieldText();
1206 }
1207
1214 {
1215 $outPut = '';
1216 if (!empty($this->fieldOptions[$this->fieldValue])) {
1217 $outPut = $this->fieldOptions[$this->fieldValue];
1218 }
1219
1220 return $outPut;
1221 }
1222
1229 {
1230 $outPut = '';
1231 $user = new User($this->db);
1232 $user->fetch($this->fieldValue);
1233 $outPut = $user->firstname . " " . $user->lastname;
1234 return $outPut;
1235 }
1236
1237 /*
1238 * METHODS FOR SETTING DISPLAY TYPE
1239 */
1240
1246 public function setAsString()
1247 {
1248 $this->type = 'string';
1249 return $this;
1250 }
1251
1257 public function setAsColor()
1258 {
1259 $this->type = 'color';
1260 return $this;
1261 }
1262
1268 public function setAsTextarea()
1269 {
1270 $this->type = 'textarea';
1271 return $this;
1272 }
1273
1279 public function setAsHtml()
1280 {
1281 $this->type = 'html';
1282 return $this;
1283 }
1284
1291 public function setAsEmailTemplate($templateType)
1292 {
1293 $this->type = 'emailtemplate:'.$templateType;
1294 return $this;
1295 }
1296
1302 public function setAsThirdpartyType()
1303 {
1304 $this->type = 'thirdparty_type';
1305 return $this;
1306 }
1307
1313 public function setAsYesNo()
1314 {
1315 $this->type = 'yesno';
1316 return $this;
1317 }
1318
1324 public function setAsSecureKey()
1325 {
1326 $this->type = 'securekey';
1327 return $this;
1328 }
1329
1335 public function setAsProduct()
1336 {
1337 $this->type = 'product';
1338 return $this;
1339 }
1340
1348 public function setAsCategory($catType)
1349 {
1350 $this->type = 'category:'.$catType;
1351 return $this;
1352 }
1353
1359 public function setAsTitle()
1360 {
1361 $this->type = 'title';
1362 return $this;
1363 }
1364
1365
1372 public function setAsMultiSelect($fieldOptions)
1373 {
1374 if (is_array($fieldOptions)) {
1375 $this->fieldOptions = $fieldOptions;
1376 }
1377
1378 $this->type = 'multiselect';
1379 return $this;
1380 }
1381
1388 public function setAsSelect($fieldOptions)
1389 {
1390 if (is_array($fieldOptions)) {
1391 $this->fieldOptions = $fieldOptions;
1392 }
1393
1394 $this->type = 'select';
1395 return $this;
1396 }
1397
1403 public function setAsSelectUser()
1404 {
1405 $this->type = 'selectUser';
1406 return $this;
1407 }
1408}
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
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
newItem($confKey, $targetItemKey=false, $insertAfterTarget=false)
Create a new item the tagret is useful with hooks : that allow externals modules to add setup items o...
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
__construct($db, $outputLangs=false)
Constructor.
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.
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
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:120
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Ouput javacript to autoset a generated password using default module into a HTML element.