dolibarr  18.0.0-alpha
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 
22 class FormSetup
23 {
27  public $db;
28 
30  public $items = array();
31 
35  public $setupNotEmpty = 0;
36 
38  public $langs;
39 
41  public $form;
42 
44  protected $maxItemRank;
45 
50  public $htmlBeforeOutputForm = '';
51 
56  public $htmlAfterOutputForm = '';
57 
62  public $htmlOutputMoreButton = '';
63 
64 
69  public $formAttributes = array(
70  'action' => '', // set in __construct
71  'method' => 'POST'
72  );
73 
78  public $formHiddenInputs = array();
79 
80 
87  public function __construct($db, $outputLangs = false)
88  {
89  global $langs;
90  $this->db = $db;
91  $this->form = new Form($this->db);
92  $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
93 
94  $this->formHiddenInputs['token'] = newToken();
95  $this->formHiddenInputs['action'] = 'update';
96 
97 
98  if ($outputLangs) {
99  $this->langs = $outputLangs;
100  } else {
101  $this->langs = $langs;
102  }
103  }
104 
111  static public function generateAttributesStringFromArray($attributes)
112  {
113  $Aattr = array();
114  if (is_array($attributes)) {
115  foreach ($attributes as $attribute => $value) {
116  if (is_array($value) || is_object($value)) {
117  continue;
118  }
119  $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
120  }
121  }
122 
123  return !empty($Aattr)?implode(' ', $Aattr):'';
124  }
125 
126 
133  public function generateOutput($editMode = false)
134  {
135  global $hookmanager, $action, $langs;
136  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
137 
138  $parameters = array(
139  'editMode' => $editMode
140  );
141  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
142  if ($reshook < 0) {
143  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
144  }
145 
146  if ($reshook > 0) {
147  return $hookmanager->resPrint;
148  } else {
149  $out = '<!-- Start generateOutput from FormSetup class -->';
150  $out.= $this->htmlBeforeOutputForm;
151 
152  if ($editMode) {
153  $out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
154 
155  // generate hidden values from $this->formHiddenInputs
156  if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
157  foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
158  $out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
159  }
160  }
161  }
162 
163  // generate output table
164  $out .= $this->generateTableOutput($editMode);
165 
166 
167  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
168  if ($reshook < 0) {
169  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
170  }
171 
172  if ($reshook > 0) {
173  return $hookmanager->resPrint;
174  } elseif ($editMode) {
175  $out .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
176  $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
177  $out.= $this->htmlOutputMoreButton;
178  $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
179  $out .= ' &nbsp;&nbsp; ';
180  $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$langs->trans('Cancel').'</a>';
181  $out .= '</div>';
182  }
183 
184  if ($editMode) {
185  $out .= '</form>';
186  }
187 
188  $out.= $this->htmlAfterOutputForm;
189 
190  return $out;
191  }
192  }
193 
200  public function generateTableOutput($editMode = false)
201  {
202  global $hookmanager, $action;
203  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
204 
205  $parameters = array(
206  'editMode' => $editMode
207  );
208  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
209  if ($reshook < 0) {
210  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
211  }
212 
213  if ($reshook > 0) {
214  return $hookmanager->resPrint;
215  } else {
216  $out = '<table class="noborder centpercent">';
217  $out .= '<thead>';
218  $out .= '<tr class="liste_titre">';
219  $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
220  $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
221  $out .= '</tr>';
222  $out .= '</thead>';
223 
224  // Sort items before render
225  $this->sortingItems();
226 
227  $out .= '<tbody>';
228  foreach ($this->items as $item) {
229  $out .= $this->generateLineOutput($item, $editMode);
230  }
231  $out .= '</tbody>';
232 
233  $out .= '</table>';
234  return $out;
235  }
236  }
237 
244  public function saveConfFromPost($noMessageInUpdate = false)
245  {
246  global $hookmanager;
247 
248  $parameters = array();
249  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
250  if ($reshook < 0) {
251  $this->setErrors($hookmanager->errors);
252  return -1;
253  }
254 
255  if ($reshook > 0) {
256  return $reshook;
257  }
258 
259 
260  if (empty($this->items)) {
261  return null;
262  }
263 
264  $this->db->begin();
265  $error = 0;
266  foreach ($this->items as $item) {
267  $res = $item->setValueFromPost();
268  if ($res > 0) {
269  $item->saveConfValue();
270  } elseif ($res < 0) {
271  $error++;
272  break;
273  }
274  }
275 
276  if (!$error) {
277  $this->db->commit();
278  if (empty($noMessageInUpdate)) {
279  setEventMessages($this->langs->trans("SetupSaved"), null);
280  }
281  return 1;
282  } else {
283  $this->db->rollback();
284  if (empty($noMessageInUpdate)) {
285  setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
286  }
287  return -1;
288  }
289  }
290 
298  public function generateLineOutput($item, $editMode = false)
299  {
300 
301  $out = '';
302  if ($item->enabled==1) {
303  $trClass = 'oddeven';
304  if ($item->getType() == 'title') {
305  $trClass = 'liste_titre';
306  }
307 
308  $this->setupNotEmpty++;
309  $out.= '<tr class="'.$trClass.'">';
310 
311  $out.= '<td class="col-setup-title">';
312  $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
313  $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
314  $out.= '</span>';
315  $out.= '</td>';
316 
317  $out.= '<td>';
318 
319  if ($editMode) {
320  $out.= $item->generateInputField();
321  } else {
322  $out.= $item->generateOutputField();
323  }
324 
325  if (!empty($item->errors)) {
326  // TODO : move set event message in a methode to be called by cards not by this class
327  setEventMessages(null, $item->errors, 'errors');
328  }
329 
330  $out.= '</td>';
331  $out.= '</tr>';
332  }
333 
334  return $out;
335  }
336 
337 
344  public function addItemsFromParamsArray($params)
345  {
346  if (!is_array($params) || empty($params)) { return false; }
347  foreach ($params as $confKey => $param) {
348  $this->addItemFromParams($confKey, $param); // todo manage error
349  }
350  }
351 
352 
361  public function addItemFromParams($confKey, $params)
362  {
363  if (empty($confKey) || empty($params['type'])) { return false; }
364 
365  /*
366  * Exemple from old module builder setup page
367  * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
368  // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
369  //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
370  //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
371  //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
372  //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
373  //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
374  //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
375  */
376 
377  $item = new FormSetupItem($confKey);
378  // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage $item->setTypeFromTypeString($params['type']);
380 
381  if (!empty($params['enabled'])) {
382  $item->enabled = $params['enabled'];
383  }
384 
385  if (!empty($params['css'])) {
386  $item->cssClass = $params['css'];
387  }
388 
389  $this->items[$item->confKey] = $item;
390 
391  return true;
392  }
393 
400  public function exportItemsAsParamsArray()
401  {
402  $arrayofparameters = array();
403  foreach ($this->items as $item) {
404  $arrayofparameters[$item->confKey] = array(
405  'type' => $item->getType(),
406  'enabled' => $item->enabled
407  );
408  }
409 
410  return $arrayofparameters;
411  }
412 
419  public function reloadConfs()
420  {
421 
422  if (!array($this->items)) { return false; }
423  foreach ($this->items as $item) {
424  $item->loadValueFromConf();
425  }
426 
427  return true;
428  }
429 
430 
440  public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
441  {
442  $item = new FormSetupItem($confKey);
443 
444  // set item rank if not defined as last item
445  if (empty($item->rank)) {
446  $item->rank = $this->getCurentItemMaxRank() + 1;
447  $this->setItemMaxRank($item->rank); // set new max rank if needed
448  }
449 
450  // try to get rank from target column, this will override item->rank
451  if (!empty($targetItemKey)) {
452  if (isset($this->items[$targetItemKey])) {
453  $targetItem = $this->items[$targetItemKey];
454  $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
455  if ($targetItem->rank >= 0 && $insertAfterTarget) {
456  $item->rank++;
457  }
458  }
459 
460  // calc new rank for each item to make place for new item
461  foreach ($this->items as $fItem) {
462  if ($item->rank <= $fItem->rank) {
463  $fItem->rank = $fItem->rank + 1;
464  $this->setItemMaxRank($fItem->rank); // set new max rank if needed
465  }
466  }
467  }
468 
469  $this->items[$item->confKey] = $item;
470  return $this->items[$item->confKey];
471  }
472 
478  public function sortingItems()
479  {
480  // Sorting
481  return uasort($this->items, array($this, 'itemSort'));
482  }
483 
490  public function getCurentItemMaxRank($cache = true)
491  {
492  if (empty($this->items)) {
493  return 0;
494  }
495 
496  if ($cache && $this->maxItemRank > 0) {
497  return $this->maxItemRank;
498  }
499 
500  $this->maxItemRank = 0;
501  foreach ($this->items as $item) {
502  $this->maxItemRank = max($this->maxItemRank, $item->rank);
503  }
504 
505  return $this->maxItemRank;
506  }
507 
508 
515  public function setItemMaxRank($rank)
516  {
517  $this->maxItemRank = max($this->maxItemRank, $rank);
518  }
519 
520 
527  public function getLineRank($itemKey)
528  {
529  if (!isset($this->items[$itemKey]->rank)) {
530  return -1;
531  }
532  return $this->items[$itemKey]->rank;
533  }
534 
535 
543  public function itemSort(FormSetupItem $a, FormSetupItem $b)
544  {
545  if (empty($a->rank)) {
546  $a->rank = 0;
547  }
548  if (empty($b->rank)) {
549  $b->rank = 0;
550  }
551  if ($a->rank == $b->rank) {
552  return 0;
553  }
554  return ($a->rank < $b->rank) ? -1 : 1;
555  }
556 }
557 
562 {
566  public $db;
567 
569  public $langs;
570 
572  public $entity;
573 
575  public $form;
576 
578  public $confKey;
579 
581  public $nameText = false;
582 
584  public $helpText = '';
585 
587  public $fieldValue;
588 
590  public $defaultFieldValue = null;
591 
593  public $fieldAttr = array();
594 
596  public $fieldOverride = false;
597 
599  public $fieldInputOverride = false;
600 
602  public $fieldOutputOverride = false;
603 
605  public $rank = 0;
606 
608  public $fieldOptions = array();
609 
612 
615 
619  public $errors = array();
620 
627  protected $type = 'string';
628 
629  public $enabled = 1;
630 
631  public $cssClass = '';
632 
638  public function __construct($confKey)
639  {
640  global $langs, $db, $conf, $form;
641  $this->db = $db;
642 
643  if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
644  $this->form = $form;
645  } else {
646  $this->form = new Form($this->db);
647  }
648 
649  $this->langs = $langs;
650  $this->entity = $conf->entity;
651 
652  $this->confKey = $confKey;
653  $this->loadValueFromConf();
654  }
655 
660  public function loadValueFromConf()
661  {
662  global $conf;
663  if (isset($conf->global->{$this->confKey})) {
664  $this->fieldValue = getDolGlobalString($this->confKey);
665  return true;
666  } else {
667  $this->fieldValue = null;
668  return false;
669  }
670  }
671 
677  public function reloadValueFromConf()
678  {
679  return $this->loadValueFromConf();
680  }
681 
682 
688  public function saveConfValue()
689  {
690  global $hookmanager;
691 
692  $parameters = array();
693  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
694  if ($reshook < 0) {
695  $this->setErrors($hookmanager->errors);
696  return -1;
697  }
698 
699  if ($reshook > 0) {
700  return $reshook;
701  }
702 
703 
704  if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
705  return call_user_func($this->saveCallBack, $this);
706  }
707 
708  // Modify constant only if key was posted (avoid resetting key to the null value)
709  if ($this->type != 'title') {
710  $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
711  if ($result < 0) {
712  return -1;
713  } else {
714  return 1;
715  }
716  }
717 
718  return 0;
719  }
720 
727  public function setSaveCallBack(callable $callBack)
728  {
729  $this->saveCallBack = $callBack;
730  }
731 
737  public function setValueFromPostCallBack(callable $callBack)
738  {
739  $this->setValueFromPostCallBack = $callBack;
740  }
741 
746  public function setValueFromPost()
747  {
748  if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
749  return call_user_func($this->setValueFromPostCallBack);
750  }
751 
752  // Modify constant only if key was posted (avoid resetting key to the null value)
753  if ($this->type != 'title') {
754  if (preg_match('/category:/', $this->type)) {
755  if (GETPOST($this->confKey, 'int') == '-1') {
756  $val_const = '';
757  } else {
758  $val_const = GETPOST($this->confKey, 'int');
759  }
760  } elseif ($this->type == 'multiselect') {
761  $val = GETPOST($this->confKey, 'array');
762  if ($val && is_array($val)) {
763  $val_const = implode(',', $val);
764  } else {
765  $val_const = '';
766  }
767  } elseif ($this->type == 'html') {
768  $val_const = GETPOST($this->confKey, 'restricthtml');
769  } else {
770  $val_const = GETPOST($this->confKey, 'alpha');
771  }
772 
773  // TODO add value check with class validate
774  $this->fieldValue = $val_const;
775 
776  return 1;
777  }
778 
779  return 0;
780  }
781 
786  public function getHelpText()
787  {
788  if (!empty($this->helpText)) { return $this->helpText; }
789  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
790  }
791 
796  public function getNameText()
797  {
798  if (!empty($this->nameText)) { return $this->nameText; }
799  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
800  }
801 
806  public function generateInputField()
807  {
808  global $conf;
809 
810  if (!empty($this->fieldOverride)) {
811  return $this->fieldOverride;
812  }
813 
814  if (!empty($this->fieldInputOverride)) {
815  return $this->fieldInputOverride;
816  }
817 
818  // Set default value
819  if (is_null($this->fieldValue)) {
820  $this->fieldValue = $this->defaultFieldValue;
821  }
822 
823 
824  $this->fieldAttr['name'] = $this->confKey;
825  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
826  $this->fieldAttr['value'] = $this->fieldValue;
827 
828  $out = '';
829 
830  if ($this->type == 'title') {
831  $out.= $this->generateOutputField(); // title have no input
832  } elseif ($this->type == 'multiselect') {
833  $out.= $this->generateInputFieldMultiSelect();
834  } elseif ($this->type == 'select') {
835  $out.= $this->generateInputFieldSelect();
836  } elseif ($this->type == 'textarea') {
837  $out.= $this->generateInputFieldTextarea();
838  } elseif ($this->type== 'html') {
839  $out.= $this->generateInputFieldHtml();
840  } elseif ($this->type== 'color') {
841  $out.= $this->generateInputFieldColor();
842  } elseif ($this->type == 'yesno') {
843  if (!empty($conf->use_javascript_ajax)) {
844  $out.= ajax_constantonoff($this->confKey);
845  } else {
846  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
847  }
848  } elseif (preg_match('/emailtemplate:/', $this->type)) {
849  $out.= $this->generateInputFieldEmailTemplate();
850  } elseif (preg_match('/category:/', $this->type)) {
851  $out.=$this->generateInputFieldCategories();
852  } elseif (preg_match('/thirdparty_type/', $this->type)) {
853  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
854  $formcompany = new FormCompany($this->db);
855  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
856  } elseif ($this->type == 'securekey') {
857  $out.= $this->generateInputFieldSecureKey();
858  } elseif ($this->type == 'product') {
859  if (isModEnabled("product") || isModEnabled("service")) {
860  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
861  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
862  }
863  } else {
864  $out.= $this->generateInputFieldText();
865  }
866 
867  return $out;
868  }
869 
874  public function generateInputFieldText()
875  {
876  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
877  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
878  }
879 
884  public function generateInputFieldTextarea()
885  {
886  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
887  $out.= dol_htmlentities($this->fieldValue);
888  $out.= "</textarea>\n";
889  return $out;
890  }
891 
896  public function generateInputFieldHtml()
897  {
898  global $conf;
899  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
900  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%');
901  return $doleditor->Create(1);
902  }
903 
909  {
910  global $conf;
911  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
912  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
913  $formother = new FormOther($this->db);
914 
915  $tmp = explode(':', $this->type);
916  $out= img_picto('', 'category', 'class="pictofixedwidth"');
917  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
918  return $out;
919  }
920 
926  {
927  global $conf, $user;
928  $out = '';
929  if (preg_match('/emailtemplate:/', $this->type)) {
930  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
931  $formmail = new FormMail($this->db);
932 
933  $tmp = explode(':', $this->type);
934  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
935  $arrayOfMessageName = array();
936  if (is_array($formmail->lines_model)) {
937  foreach ($formmail->lines_model as $modelMail) {
938  $moreonlabel = '';
939  if (!empty($arrayOfMessageName[$modelMail->label])) {
940  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
941  }
942  // The 'label' is the key that is unique if we exclude the language
943  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel;
944  }
945  }
946  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
947  }
948 
949  return $out;
950  }
951 
952 
957  public function generateInputFieldSecureKey()
958  {
959  global $conf;
960  $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">';
961  if (!empty($conf->use_javascript_ajax)) {
962  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
963  }
964 
965  // Add button to autosuggest a key
966  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
967  $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
968 
969  return $out;
970  }
971 
972 
977  {
978  $TSelected = array();
979  if ($this->fieldValue) {
980  $TSelected = explode(',', $this->fieldValue);
981  }
982 
983  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
984  }
985 
986 
990  public function generateInputFieldSelect()
991  {
992  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
993  }
994 
1002  public function getType()
1003  {
1004  return $this->type;
1005  }
1006 
1016  public function setTypeFromTypeString($type)
1017  {
1018  $this->type = $type;
1019 
1020  return true;
1021  }
1022 
1029  public function setErrors($errors)
1030  {
1031  if (is_array($errors)) {
1032  if (!empty($errors)) {
1033  foreach ($errors as $error) {
1034  $this->setErrors($error);
1035  }
1036  }
1037  } elseif (!empty($errors)) {
1038  $this->errors[] = $errors;
1039  }
1040  }
1041 
1047  public function generateOutputField()
1048  {
1049  global $conf, $user, $langs;
1050 
1051  if (!empty($this->fieldOverride)) {
1052  return $this->fieldOverride;
1053  }
1054 
1055  if (!empty($this->fieldOutputOverride)) {
1056  return $this->fieldOutputOverride;
1057  }
1058 
1059  $out = '';
1060 
1061  if ($this->type == 'title') {
1062  // nothing to do
1063  } elseif ($this->type == 'textarea') {
1064  $out.= dol_nl2br($this->fieldValue);
1065  } elseif ($this->type == 'multiselect') {
1066  $out.= $this->generateOutputFieldMultiSelect();
1067  } elseif ($this->type == 'select') {
1068  $out.= $this->generateOutputFieldSelect();
1069  } elseif ($this->type== 'html') {
1070  $out.= $this->fieldValue;
1071  } elseif ($this->type== 'color') {
1072  $out.= $this->generateOutputFieldColor();
1073  } elseif ($this->type == 'yesno') {
1074  if (!empty($conf->use_javascript_ajax)) {
1075  $out.= ajax_constantonoff($this->confKey);
1076  } else {
1077  if ($this->fieldValue == 1) {
1078  $out.= $langs->trans('yes');
1079  } else {
1080  $out.= $langs->trans('no');
1081  }
1082  }
1083  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1084  if ($this->fieldValue > 0) {
1085  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1086  $formmail = new FormMail($this->db);
1087 
1088  $tmp = explode(':', $this->type);
1089 
1090  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1091  if (is_numeric($template) && $template < 0) {
1092  $this->setErrors($formmail->errors);
1093  }
1094  $out.= $this->langs->trans($template->label);
1095  }
1096  } elseif (preg_match('/category:/', $this->type)) {
1097  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1098  $c = new Categorie($this->db);
1099  $result = $c->fetch($this->fieldValue);
1100  if ($result < 0) {
1101  $this->setErrors($c->errors);
1102  }
1103  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1104  $toprint = array();
1105  foreach ($ways as $way) {
1106  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1107  }
1108  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1109  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1110  if ($this->fieldValue==2) {
1111  $out.= $this->langs->trans("Prospect");
1112  } elseif ($this->fieldValue==3) {
1113  $out.= $this->langs->trans("ProspectCustomer");
1114  } elseif ($this->fieldValue==1) {
1115  $out.= $this->langs->trans("Customer");
1116  } elseif ($this->fieldValue==0) {
1117  $out.= $this->langs->trans("NorProspectNorCustomer");
1118  }
1119  } elseif ($this->type == 'product') {
1120  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1121 
1122  $product = new Product($this->db);
1123  $resprod = $product->fetch($this->fieldValue);
1124  if ($resprod > 0) {
1125  $out.= $product->ref;
1126  } elseif ($resprod < 0) {
1127  $this->setErrors($product->errors);
1128  }
1129  } else {
1130  $out.= $this->fieldValue;
1131  }
1132 
1133  return $out;
1134  }
1135 
1136 
1141  {
1142  $outPut = '';
1143  $TSelected = array();
1144  if (!empty($this->fieldValue)) {
1145  $TSelected = explode(',', $this->fieldValue);
1146  }
1147 
1148  if (!empty($TSelected)) {
1149  foreach ($TSelected as $selected) {
1150  if (!empty($this->fieldOptions[$selected])) {
1151  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1152  }
1153  }
1154  }
1155  return $outPut;
1156  }
1157 
1161  public function generateOutputFieldColor()
1162  {
1163  $this->fieldAttr['disabled']=null;
1164  return $this->generateInputField();
1165  }
1169  public function generateInputFieldColor()
1170  {
1171  $this->fieldAttr['type']= 'color';
1172  return $this->generateInputFieldText();
1173  }
1174 
1178  public function generateOutputFieldSelect()
1179  {
1180  $outPut = '';
1181  if (!empty($this->fieldOptions[$this->fieldValue])) {
1182  $outPut = $this->fieldOptions[$this->fieldValue];
1183  }
1184 
1185  return $outPut;
1186  }
1187 
1188  /*
1189  * METHODS FOR SETTING DISPLAY TYPE
1190  */
1191 
1196  public function setAsString()
1197  {
1198  $this->type = 'string';
1199  return $this;
1200  }
1201 
1206  public function setAsColor()
1207  {
1208  $this->type = 'color';
1209  return $this;
1210  }
1211 
1216  public function setAsTextarea()
1217  {
1218  $this->type = 'textarea';
1219  return $this;
1220  }
1221 
1226  public function setAsHtml()
1227  {
1228  $this->type = 'html';
1229  return $this;
1230  }
1231 
1237  public function setAsEmailTemplate($templateType)
1238  {
1239  $this->type = 'emailtemplate:'.$templateType;
1240  return $this;
1241  }
1242 
1247  public function setAsThirdpartyType()
1248  {
1249  $this->type = 'thirdparty_type';
1250  return $this;
1251  }
1252 
1257  public function setAsYesNo()
1258  {
1259  $this->type = 'yesno';
1260  return $this;
1261  }
1262 
1267  public function setAsSecureKey()
1268  {
1269  $this->type = 'securekey';
1270  return $this;
1271  }
1272 
1277  public function setAsProduct()
1278  {
1279  $this->type = 'product';
1280  return $this;
1281  }
1282 
1289  public function setAsCategory($catType)
1290  {
1291  $this->type = 'category:'.$catType;
1292  return $this;
1293  }
1294 
1300  public function setAsTitle()
1301  {
1302  $this->type = 'title';
1303  return $this;
1304  }
1305 
1306 
1313  public function setAsMultiSelect($fieldOptions)
1314  {
1315  if (is_array($fieldOptions)) {
1316  $this->fieldOptions = $fieldOptions;
1317  }
1318 
1319  $this->type = 'multiselect';
1320  return $this;
1321  }
1322 
1329  public function setAsSelect($fieldOptions)
1330  {
1331  if (is_array($fieldOptions)) {
1332  $this->fieldOptions = $fieldOptions;
1333  }
1334 
1335  $this->type = 'select';
1336  return $this;
1337  }
1338 }
FormSetup\sortingItems
sortingItems()
Sort items according to rank.
Definition: html.formsetup.class.php:478
FormSetupItem\generateOutputFieldSelect
generateOutputFieldSelect()
Definition: html.formsetup.class.php:1178
FormSetup\reloadConfs
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
Definition: html.formsetup.class.php:419
FormSetupItem\setAsMultiSelect
setAsMultiSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1313
db
$conf db
API class for accounts.
Definition: inc.php:41
FormSetupItem\$defaultFieldValue
$defaultFieldValue
Definition: html.formsetup.class.php:590
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1504
FormSetupItem\setAsSecureKey
setAsSecureKey()
Set type of input as secure key.
Definition: html.formsetup.class.php:1267
FormSetupItem\setErrors
setErrors($errors)
Add error.
Definition: html.formsetup.class.php:1029
FormSetupItem\__construct
__construct($confKey)
Constructor.
Definition: html.formsetup.class.php:638
FormSetupItem\setAsThirdpartyType
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
Definition: html.formsetup.class.php:1247
FormSetupItem\generateInputFieldColor
generateInputFieldColor()
Definition: html.formsetup.class.php:1169
FormSetup\saveConfFromPost
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
Definition: html.formsetup.class.php:244
FormSetupItem\generateInputFieldText
generateInputFieldText()
generatec default input field
Definition: html.formsetup.class.php:874
FormSetupItem\setAsCategory
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
Definition: html.formsetup.class.php:1289
FormSetupItem\generateOutputFieldMultiSelect
generateOutputFieldMultiSelect()
Definition: html.formsetup.class.php:1140
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:530
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:7201
FormSetupItem\generateInputFieldSecureKey
generateInputFieldSecureKey()
generate input field for secure key
Definition: html.formsetup.class.php:957
FormSetupItem\setAsTitle
setAsTitle()
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1300
FormSetup\setItemMaxRank
setItemMaxRank($rank)
set new max rank if needed
Definition: html.formsetup.class.php:515
FormSetupItem\generateInputFieldTextarea
generateInputFieldTextarea()
generate input field for textarea
Definition: html.formsetup.class.php:884
FormSetupItem\$rank
$rank
Definition: html.formsetup.class.php:605
FormSetupItem\$helpText
$helpText
Definition: html.formsetup.class.php:584
FormSetupItem\setValueFromPostCallBack
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
Definition: html.formsetup.class.php:737
FormSetupItem\generateOutputField
generateOutputField()
generateOutputField
Definition: html.formsetup.class.php:1047
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
Categorie
Class to manage categories.
Definition: categorie.class.php:47
FormSetup\generateAttributesStringFromArray
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
Definition: html.formsetup.class.php:111
FormSetupItem\getHelpText
getHelpText()
Get help text or generate it.
Definition: html.formsetup.class.php:786
FormCompany
Class to build HTML component for third parties management Only common components are here.
Definition: html.formcompany.class.php:40
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4025
dolGetBadge
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
Definition: functions.lib.php:10703
FormSetupItem\generateInputFieldCategories
generateInputFieldCategories()
generate input field for categories
Definition: html.formsetup.class.php:908
FormSetupItem\getNameText
getNameText()
Get field name text or generate it.
Definition: html.formsetup.class.php:796
FormSetupItem\generateInputField
generateInputField()
generate input field
Definition: html.formsetup.class.php:806
ajax_constantonoff
ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0, $forcereload=0, $marginleftonlyshort=2, $forcenoajax=0, $setzeroinsteadofdel=0, $suffix='', $mode='', $morecss='')
On/off button for constant.
Definition: ajax.lib.php:604
FormSetupItem\$saveCallBack
$saveCallBack
Definition: html.formsetup.class.php:611
FormSetup\addItemFromParams
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:361
FormSetupItem\setAsEmailTemplate
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
Definition: html.formsetup.class.php:1237
FormSetup\__construct
__construct($db, $outputLangs=false)
Constructor.
Definition: html.formsetup.class.php:87
FormSetupItem\setSaveCallBack
setSaveCallBack(callable $callBack)
Set an override function for saving data.
Definition: html.formsetup.class.php:727
FormSetupItem
This class help to create item for class formSetup.
Definition: html.formsetup.class.php:561
FormSetupItem\saveConfValue
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:688
FormSetupItem\generateInputFieldMultiSelect
generateInputFieldMultiSelect()
Definition: html.formsetup.class.php:976
FormSetup\getLineRank
getLineRank($itemKey)
get item position rank from item key
Definition: html.formsetup.class.php:527
FormSetupItem\setValueFromPost
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:746
FormSetup\getCurentItemMaxRank
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
Definition: html.formsetup.class.php:490
FormSetupItem\generateInputFieldHtml
generateInputFieldHtml()
generate input field for html
Definition: html.formsetup.class.php:896
FormSetupItem\setAsHtml
setAsHtml()
Set type of input as html editor.
Definition: html.formsetup.class.php:1226
FormSetupItem\loadValueFromConf
loadValueFromConf()
load conf value from databases
Definition: html.formsetup.class.php:660
FormSetup
This class help you create setup render.
Definition: html.formsetup.class.php:22
FormSetupItem\setAsProduct
setAsProduct()
Set type of input as product.
Definition: html.formsetup.class.php:1277
FormSetup\itemSort
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
Definition: html.formsetup.class.php:543
FormSetup\newItem
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...
Definition: html.formsetup.class.php:440
FormSetup\addItemsFromParamsArray
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:344
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:82
dolJSToSetRandomPassword
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Ouput javacript to autoset a generated password using default module into a HTML element.
Definition: security2.lib.php:568
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11317
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:147
FormSetupItem\$setValueFromPostCallBack
$setValueFromPostCallBack
Definition: html.formsetup.class.php:614
FormSetupItem\setAsYesNo
setAsYesNo()
Set type of input as Yes.
Definition: html.formsetup.class.php:1257
dolibarr_set_const
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).
Definition: admin.lib.php:633
FormSetupItem\setAsColor
setAsColor()
Set type of input as color.
Definition: html.formsetup.class.php:1206
FormSetupItem\generateInputFieldEmailTemplate
generateInputFieldEmailTemplate()
generate input field for email template selector
Definition: html.formsetup.class.php:925
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
FormSetup\generateTableOutput
generateTableOutput($editMode=false)
generateTableOutput
Definition: html.formsetup.class.php:200
FormSetup\generateOutput
generateOutput($editMode=false)
generateOutput
Definition: html.formsetup.class.php:133
FormSetupItem\setTypeFromTypeString
setTypeFromTypeString($type)
set the type from string : used for old module builder setup conf style conversion and tests because ...
Definition: html.formsetup.class.php:1016
FormSetupItem\getType
getType()
get the type : used for old module builder setup conf style conversion and tests because this two cla...
Definition: html.formsetup.class.php:1002
FormSetupItem\setAsSelect
setAsSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1329
FormSetupItem\$fieldValue
$fieldValue
Definition: html.formsetup.class.php:587
FormSetupItem\setAsTextarea
setAsTextarea()
Set type of input as textarea.
Definition: html.formsetup.class.php:1216
FormSetupItem\generateInputFieldSelect
generateInputFieldSelect()
Definition: html.formsetup.class.php:990
FormSetupItem\$nameText
$nameText
Definition: html.formsetup.class.php:581
FormSetup\generateLineOutput
generateLineOutput($item, $editMode=false)
generateLineOutput
Definition: html.formsetup.class.php:298
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8509
FormMail
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Definition: html.formmail.class.php:38
FormSetupItem\setAsString
setAsString()
Set type of input as string.
Definition: html.formsetup.class.php:1196
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
FormSetup\exportItemsAsParamsArray
exportItemsAsParamsArray()
Used to export param array for /core/actions_setmoduleoptions.inc.php template Method exists only for...
Definition: html.formsetup.class.php:400
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
FormSetupItem\generateOutputFieldColor
generateOutputFieldColor()
Definition: html.formsetup.class.php:1161
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7413
FormSetupItem\reloadValueFromConf
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
Definition: html.formsetup.class.php:677