dolibarr  16.0.5
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 {
24 
28  public $db;
29 
31  public $items = array();
32 
36  public $setupNotEmpty = 0;
37 
39  public $langs;
40 
42  public $form;
43 
45  protected $maxItemRank;
46 
51  public $htmlBeforeOutputForm = '';
52 
57  public $htmlAfterOutputForm = '';
58 
63  public $htmlOutputMoreButton = '';
64 
65 
70  public $formAttributes = array(
71  'action' => '', // set in __construct
72  'method' => 'POST'
73  );
74 
79  public $formHiddenInputs = array();
80 
81 
88  public function __construct($db, $outputLangs = false)
89  {
90  global $langs;
91  $this->db = $db;
92  $this->form = new Form($this->db);
93  $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
94 
95  $this->formHiddenInputs['token'] = newToken();
96  $this->formHiddenInputs['action'] = 'update';
97 
98 
99  if ($outputLangs) {
100  $this->langs = $outputLangs;
101  } else {
102  $this->langs = $langs;
103  }
104  }
105 
112  static public function generateAttributesStringFromArray($attributes)
113  {
114  $Aattr = array();
115  if (is_array($attributes)) {
116  foreach ($attributes as $attribute => $value) {
117  if (is_array($value) || is_object($value)) {
118  continue;
119  }
120  $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
121  }
122  }
123 
124  return !empty($Aattr)?implode(' ', $Aattr):'';
125  }
126 
127 
134  public function generateOutput($editMode = false)
135  {
136  global $hookmanager, $action, $langs;
137  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
138 
139  $parameters = array(
140  'editMode' => $editMode
141  );
142  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
143  if ($reshook < 0) {
144  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
145  }
146 
147  if ($reshook > 0) {
148  return $hookmanager->resPrint;
149  } else {
150  $out = '<!-- Start generateOutput from FormSetup class -->';
151  $out.= $this->htmlBeforeOutputForm;
152 
153  if ($editMode) {
154  $out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
155 
156  // generate hidden values from $this->formHiddenInputs
157  if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
158  foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
159  $out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
160  }
161  }
162  }
163 
164  // generate output table
165  $out .= $this->generateTableOutput($editMode);
166 
167 
168  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
169  if ($reshook < 0) {
170  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
171  }
172 
173  if ($reshook > 0) {
174  return $hookmanager->resPrint;
175  } elseif ($editMode) {
176  $out .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
177  $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
178  $out.= $this->htmlOutputMoreButton;
179  $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
180  $out .= ' &nbsp;&nbsp; ';
181  $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$langs->trans('Cancel').'</a>';
182  $out .= '</div>';
183  }
184 
185  if ($editMode) {
186  $out .= '</form>';
187  }
188 
189  $out.= $this->htmlAfterOutputForm;
190 
191  return $out;
192  }
193  }
194 
201  public function generateTableOutput($editMode = false)
202  {
203  global $hookmanager, $action;
204  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
205 
206  $parameters = array(
207  'editMode' => $editMode
208  );
209  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
210  if ($reshook < 0) {
211  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
212  }
213 
214  if ($reshook > 0) {
215  return $hookmanager->resPrint;
216  } else {
217  $out = '<table class="noborder centpercent">';
218  $out .= '<thead>';
219  $out .= '<tr class="liste_titre">';
220  $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
221  $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
222  $out .= '</tr>';
223  $out .= '</thead>';
224 
225  // Sort items before render
226  $this->sortingItems();
227 
228  $out .= '<tbody>';
229  foreach ($this->items as $item) {
230  $out .= $this->generateLineOutput($item, $editMode);
231  }
232  $out .= '</tbody>';
233 
234  $out .= '</table>';
235  return $out;
236  }
237  }
238 
245  public function saveConfFromPost($noMessageInUpdate = false)
246  {
247  global $hookmanager;
248 
249  $parameters = array();
250  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
251  if ($reshook < 0) {
252  $this->setErrors($hookmanager->errors);
253  return -1;
254  }
255 
256  if ($reshook > 0) {
257  return $reshook;
258  }
259 
260 
261  if (empty($this->items)) {
262  return null;
263  }
264 
265  $this->db->begin();
266  $error = 0;
267  foreach ($this->items as $item) {
268  $res = $item->setValueFromPost();
269  if ($res > 0) {
270  $item->saveConfValue();
271  } elseif ($res < 0) {
272  $error++;
273  break;
274  }
275  }
276 
277  if (!$error) {
278  $this->db->commit();
279  if (empty($noMessageInUpdate)) {
280  setEventMessages($this->langs->trans("SetupSaved"), null);
281  }
282  return 1;
283  } else {
284  $this->db->rollback();
285  if (empty($noMessageInUpdate)) {
286  setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
287  }
288  return -1;
289  }
290  }
291 
299  public function generateLineOutput($item, $editMode = false)
300  {
301 
302  $out = '';
303  if ($item->enabled==1) {
304  $trClass = 'oddeven';
305  if ($item->getType() == 'title') {
306  $trClass = 'liste_titre';
307  }
308 
309  $this->setupNotEmpty++;
310  $out.= '<tr class="'.$trClass.'">';
311 
312  $out.= '<td class="col-setup-title">';
313  $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
314  $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
315  $out.= '</span>';
316  $out.= '</td>';
317 
318  $out.= '<td>';
319 
320  if ($editMode) {
321  $out.= $item->generateInputField();
322  } else {
323  $out.= $item->generateOutputField();
324  }
325 
326  if (!empty($item->errors)) {
327  // TODO : move set event message in a methode to be called by cards not by this class
328  setEventMessages(null, $item->errors, 'errors');
329  }
330 
331  $out.= '</td>';
332  $out.= '</tr>';
333  }
334 
335  return $out;
336  }
337 
338 
345  public function addItemsFromParamsArray($params)
346  {
347  if (!is_array($params) || empty($params)) { return false; }
348  foreach ($params as $confKey => $param) {
349  $this->addItemFromParams($confKey, $param); // todo manage error
350  }
351  }
352 
353 
362  public function addItemFromParams($confKey, $params)
363  {
364  if (empty($confKey) || empty($params['type'])) { return false; }
365 
366  /*
367  * Exemple from old module builder setup page
368  * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
369  // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
370  //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
371  //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
372  //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
373  //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
374  //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
375  //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
376  */
377 
378  $item = new FormSetupItem($confKey);
379  // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage $item->setTypeFromTypeString($params['type']);
381 
382  if (!empty($params['enabled'])) {
383  $item->enabled = $params['enabled'];
384  }
385 
386  if (!empty($params['css'])) {
387  $item->cssClass = $params['css'];
388  }
389 
390  $this->items[$item->confKey] = $item;
391 
392  return true;
393  }
394 
401  public function exportItemsAsParamsArray()
402  {
403  $arrayofparameters = array();
404  foreach ($this->items as $item) {
405  $arrayofparameters[$item->confKey] = array(
406  'type' => $item->getType(),
407  'enabled' => $item->enabled
408  );
409  }
410 
411  return $arrayofparameters;
412  }
413 
420  public function reloadConfs()
421  {
422 
423  if (!array($this->items)) { return false; }
424  foreach ($this->items as $item) {
425  $item->loadValueFromConf();
426  }
427 
428  return true;
429  }
430 
431 
441  public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
442  {
443  $item = new FormSetupItem($confKey);
444 
445  // set item rank if not defined as last item
446  if (empty($item->rank)) {
447  $item->rank = $this->getCurentItemMaxRank() + 1;
448  $this->setItemMaxRank($item->rank); // set new max rank if needed
449  }
450 
451  // try to get rank from target column, this will override item->rank
452  if (!empty($targetItemKey)) {
453  if (isset($this->items[$targetItemKey])) {
454  $targetItem = $this->items[$targetItemKey];
455  $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
456  if ($targetItem->rank >= 0 && $insertAfterTarget) {
457  $item->rank++;
458  }
459  }
460 
461  // calc new rank for each item to make place for new item
462  foreach ($this->items as $fItem) {
463  if ($item->rank <= $fItem->rank) {
464  $fItem->rank = $fItem->rank + 1;
465  $this->setItemMaxRank($fItem->rank); // set new max rank if needed
466  }
467  }
468  }
469 
470  $this->items[$item->confKey] = $item;
471  return $this->items[$item->confKey];
472  }
473 
479  public function sortingItems()
480  {
481  // Sorting
482  return uasort($this->items, array($this, 'itemSort'));
483  }
484 
491  public function getCurentItemMaxRank($cache = true)
492  {
493  if (empty($this->items)) {
494  return 0;
495  }
496 
497  if ($cache && $this->maxItemRank > 0) {
498  return $this->maxItemRank;
499  }
500 
501  $this->maxItemRank = 0;
502  foreach ($this->items as $item) {
503  $this->maxItemRank = max($this->maxItemRank, $item->rank);
504  }
505 
506  return $this->maxItemRank;
507  }
508 
509 
516  public function setItemMaxRank($rank)
517  {
518  $this->maxItemRank = max($this->maxItemRank, $rank);
519  }
520 
521 
528  public function getLineRank($itemKey)
529  {
530  if (!isset($this->items[$itemKey]->rank)) {
531  return -1;
532  }
533  return $this->items[$itemKey]->rank;
534  }
535 
536 
544  public function itemSort(FormSetupItem $a, FormSetupItem $b)
545  {
546  if (empty($a->rank)) {
547  $a->rank = 0;
548  }
549  if (empty($b->rank)) {
550  $b->rank = 0;
551  }
552  if ($a->rank == $b->rank) {
553  return 0;
554  }
555  return ($a->rank < $b->rank) ? -1 : 1;
556  }
557 }
558 
563 {
567  public $db;
568 
570  public $langs;
571 
573  public $entity;
574 
576  public $form;
577 
579  public $confKey;
580 
582  public $nameText = false;
583 
585  public $helpText = '';
586 
588  public $fieldValue;
589 
591  public $defaultFieldValue = null;
592 
594  public $fieldAttr = array();
595 
597  public $fieldOverride = false;
598 
600  public $fieldInputOverride = false;
601 
603  public $fieldOutputOverride = false;
604 
606  public $rank = 0;
607 
609  public $fieldOptions = array();
610 
613 
616 
620  public $errors = array();
621 
628  protected $type = 'string';
629 
630  public $enabled = 1;
631 
632  public $cssClass = '';
633 
639  public function __construct($confKey)
640  {
641  global $langs, $db, $conf, $form;
642  $this->db = $db;
643 
644  if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
645  $this->form = $form;
646  } else {
647  $this->form = new Form($this->db);
648  }
649 
650  $this->langs = $langs;
651  $this->entity = $conf->entity;
652 
653  $this->confKey = $confKey;
654  $this->loadValueFromConf();
655  }
656 
661  public function loadValueFromConf()
662  {
663  global $conf;
664  if (isset($conf->global->{$this->confKey})) {
665  $this->fieldValue = $conf->global->{$this->confKey};
666  return true;
667  } else {
668  $this->fieldValue = null;
669  return false;
670  }
671  }
672 
678  public function reloadValueFromConf()
679  {
680  return $this->loadValueFromConf();
681  }
682 
683 
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 
724  public function setSaveCallBack(callable $callBack)
725  {
726  $this->saveCallBack = $callBack;
727  }
728 
734  public function setValueFromPostCallBack(callable $callBack)
735  {
736  $this->setValueFromPostCallBack = $callBack;
737  }
738 
743  public function setValueFromPost()
744  {
745  if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
746  return call_user_func($this->setValueFromPostCallBack);
747  }
748 
749  // Modify constant only if key was posted (avoid resetting key to the null value)
750  if ($this->type != 'title') {
751  if (preg_match('/category:/', $this->type)) {
752  if (GETPOST($this->confKey, 'int') == '-1') {
753  $val_const = '';
754  } else {
755  $val_const = GETPOST($this->confKey, 'int');
756  }
757  } elseif ($this->type == 'multiselect') {
758  $val = GETPOST($this->confKey, 'array');
759  if ($val && is_array($val)) {
760  $val_const = implode(',', $val);
761  }
762  } elseif ($this->type == 'html') {
763  $val_const = GETPOST($this->confKey, 'restricthtml');
764  } else {
765  $val_const = GETPOST($this->confKey, 'alpha');
766  }
767 
768  // TODO add value check with class validate
769  $this->fieldValue = $val_const;
770 
771  return 1;
772  }
773 
774  return 0;
775  }
776 
781  public function getHelpText()
782  {
783  if (!empty($this->helpText)) { return $this->helpText; }
784  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
785  }
786 
791  public function getNameText()
792  {
793  if (!empty($this->nameText)) { return $this->nameText; }
794  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
795  }
796 
801  public function generateInputField()
802  {
803  global $conf;
804 
805  if (!empty($this->fieldOverride)) {
806  return $this->fieldOverride;
807  }
808 
809  if (!empty($this->fieldInputOverride)) {
810  return $this->fieldInputOverride;
811  }
812 
813  // Set default value
814  if (is_null($this->fieldValue)) {
815  $this->fieldValue = $this->defaultFieldValue;
816  }
817 
818 
819  $this->fieldAttr['name'] = $this->confKey;
820  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
821  $this->fieldAttr['value'] = $this->fieldValue;
822 
823  $out = '';
824 
825  if ($this->type == 'title') {
826  $out.= $this->generateOutputField(); // title have no input
827  } elseif ($this->type == 'multiselect') {
828  $out.= $this->generateInputFieldMultiSelect();
829  } elseif ($this->type == 'select') {
830  $out.= $this->generateInputFieldSelect();
831  } elseif ($this->type == 'textarea') {
832  $out.= $this->generateInputFieldTextarea();
833  } elseif ($this->type== 'html') {
834  $out.= $this->generateInputFieldHtml();
835  } elseif ($this->type== 'color') {
836  $out.= $this->generateInputFieldColor();
837  } elseif ($this->type == 'yesno') {
838  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
839  } elseif (preg_match('/emailtemplate:/', $this->type)) {
840  $out.= $this->generateInputFieldEmailTemplate();
841  } elseif (preg_match('/category:/', $this->type)) {
842  $out.=$this->generateInputFieldCategories();
843  } elseif (preg_match('/thirdparty_type/', $this->type)) {
844  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
845  $formcompany = new FormCompany($this->db);
846  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
847  } elseif ($this->type == 'securekey') {
848  $out.= $this->generateInputFieldSecureKey();
849  } elseif ($this->type == 'product') {
850  if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) {
851  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
852  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
853  }
854  } else {
855  $out.= $this->generateInputFieldText();
856  }
857 
858  return $out;
859  }
860 
865  public function generateInputFieldText()
866  {
867  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
868  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
869  }
870 
875  public function generateInputFieldTextarea()
876  {
877  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
878  $out.= dol_htmlentities($this->fieldValue);
879  $out.= "</textarea>\n";
880  return $out;
881  }
882 
887  public function generateInputFieldHtml()
888  {
889  global $conf;
890  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
891  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%');
892  return $doleditor->Create(1);
893  }
894 
900  {
901  global $conf;
902  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
903  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
904  $formother = new FormOther($this->db);
905 
906  $tmp = explode(':', $this->type);
907  $out= img_picto('', 'category', 'class="pictofixedwidth"');
908  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
909  return $out;
910  }
911 
917  {
918  global $conf, $user;
919  $out = '';
920  if (preg_match('/emailtemplate:/', $this->type)) {
921  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
922  $formmail = new FormMail($this->db);
923 
924  $tmp = explode(':', $this->type);
925  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
926  $arrayOfMessageName = array();
927  if (is_array($formmail->lines_model)) {
928  foreach ($formmail->lines_model as $modelMail) {
929  $moreonlabel = '';
930  if (!empty($arrayOfMessageName[$modelMail->label])) {
931  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
932  }
933  // The 'label' is the key that is unique if we exclude the language
934  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel;
935  }
936  }
937  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
938  }
939 
940  return $out;
941  }
942 
943 
948  public function generateInputFieldSecureKey()
949  {
950  global $conf;
951  $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">';
952  if (!empty($conf->use_javascript_ajax)) {
953  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
954  }
955  if (!empty($conf->use_javascript_ajax)) {
956  $out .= "\n" . '<script type="text/javascript">';
957  $out .= '$(document).ready(function () {
958  $("#generate_token' . $this->confKey . '").click(function() {
959  $.get( "' . DOL_URL_ROOT . '/core/ajax/security.php", {
960  action: \'getrandompassword\',
961  generic: true
962  },
963  function(token) {
964  $("#' . $this->confKey . '").val(token);
965  });
966  });
967  });';
968  $out .= '</script>';
969  }
970  return $out;
971  }
972 
973 
978  {
979  $TSelected = array();
980  if ($this->fieldValue) {
981  $TSelected = explode(',', $this->fieldValue);
982  }
983 
984  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
985  }
986 
987 
991  public function generateInputFieldSelect()
992  {
993  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
994  }
995 
1003  public function getType()
1004  {
1005  return $this->type;
1006  }
1007 
1016  public function setTypeFromTypeString($type)
1017  {
1018  $this->type = $type;
1019  return true;
1020  }
1021 
1027  public function setErrors($errors)
1028  {
1029  if (is_array($errors)) {
1030  if (!empty($errors)) {
1031  foreach ($errors as $error) {
1032  $this->setErrors($error);
1033  }
1034  }
1035  } elseif (!empty($errors)) {
1036  $this->errors[] = $errors;
1037  }
1038  }
1039 
1043  public function generateOutputField()
1044  {
1045  global $conf, $user;
1046 
1047  if (!empty($this->fieldOverride)) {
1048  return $this->fieldOverride;
1049  }
1050 
1051  if (!empty($this->fieldOutputOverride)) {
1052  return $this->fieldOutputOverride;
1053  }
1054 
1055  $out = '';
1056 
1057  if ($this->type == 'title') {
1058  // nothing to do
1059  } elseif ($this->type == 'textarea') {
1060  $out.= dol_nl2br($this->fieldValue);
1061  } elseif ($this->type == 'multiselect') {
1062  $out.= $this->generateOutputFieldMultiSelect();
1063  } elseif ($this->type == 'select') {
1064  $out.= $this->generateOutputFieldSelect();
1065  } elseif ($this->type== 'html') {
1066  $out.= $this->fieldValue;
1067  } elseif ($this->type== 'color') {
1068  $out.= $this->generateOutputFieldColor();
1069  } elseif ($this->type == 'yesno') {
1070  $out.= ajax_constantonoff($this->confKey);
1071  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1072  if ($this->fieldValue > 0) {
1073  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1074  $formmail = new FormMail($this->db);
1075 
1076  $tmp = explode(':', $this->type);
1077 
1078  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1079  if ($template<0) {
1080  $this->setErrors($formmail->errors);
1081  }
1082  $out.= $this->langs->trans($template->label);
1083  }
1084  } elseif (preg_match('/category:/', $this->type)) {
1085  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1086  $c = new Categorie($this->db);
1087  $result = $c->fetch($this->fieldValue);
1088  if ($result < 0) {
1089  $this->setErrors($c->errors);
1090  }
1091  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1092  $toprint = array();
1093  foreach ($ways as $way) {
1094  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1095  }
1096  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1097  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1098  if ($this->fieldValue==2) {
1099  $out.= $this->langs->trans("Prospect");
1100  } elseif ($this->fieldValue==3) {
1101  $out.= $this->langs->trans("ProspectCustomer");
1102  } elseif ($this->fieldValue==1) {
1103  $out.= $this->langs->trans("Customer");
1104  } elseif ($this->fieldValue==0) {
1105  $out.= $this->langs->trans("NorProspectNorCustomer");
1106  }
1107  } elseif ($this->type == 'product') {
1108  if (!class_exists("Product")) {
1109  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1110  }
1111  $product = new Product($this->db);
1112  $resprod = $product->fetch($this->fieldValue);
1113  if ($resprod > 0) {
1114  $out.= $product->ref;
1115  } elseif ($resprod < 0) {
1116  $this->setErrors($product->errors);
1117  }
1118  } else {
1119  $out.= $this->fieldValue;
1120  }
1121 
1122  return $out;
1123  }
1124 
1125 
1130  {
1131  $outPut = '';
1132  $TSelected = array();
1133  if (!empty($this->fieldValue)) {
1134  $TSelected = explode(',', $this->fieldValue);
1135  }
1136 
1137  if (!empty($TSelected)) {
1138  foreach ($TSelected as $selected) {
1139  if (!empty($this->fieldOptions[$selected])) {
1140  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1141  }
1142  }
1143  }
1144  return $outPut;
1145  }
1146 
1150  public function generateOutputFieldColor()
1151  {
1152  $this->fieldAttr['disabled']=null;
1153  return $this->generateInputField();
1154  }
1158  public function generateInputFieldColor()
1159  {
1160  $this->fieldAttr['type']= 'color';
1161  return $this->generateInputFieldText();
1162  }
1163 
1167  public function generateOutputFieldSelect()
1168  {
1169  $outPut = '';
1170  if (!empty($this->fieldOptions[$this->fieldValue])) {
1171  $outPut = $this->fieldOptions[$this->fieldValue];
1172  }
1173 
1174  return $outPut;
1175  }
1176 
1177  /*
1178  * METHODS FOR SETTING DISPLAY TYPE
1179  */
1180 
1185  public function setAsString()
1186  {
1187  $this->type = 'string';
1188  return $this;
1189  }
1190 
1195  public function setAsColor()
1196  {
1197  $this->type = 'color';
1198  return $this;
1199  }
1200 
1205  public function setAsTextarea()
1206  {
1207  $this->type = 'textarea';
1208  return $this;
1209  }
1210 
1215  public function setAsHtml()
1216  {
1217  $this->type = 'html';
1218  return $this;
1219  }
1220 
1226  public function setAsEmailTemplate($templateType)
1227  {
1228  $this->type = 'emailtemplate:'.$templateType;
1229  return $this;
1230  }
1231 
1236  public function setAsThirdpartyType()
1237  {
1238  $this->type = 'thirdparty_type';
1239  return $this;
1240  }
1241 
1246  public function setAsYesNo()
1247  {
1248  $this->type = 'yesno';
1249  return $this;
1250  }
1251 
1256  public function setAsSecureKey()
1257  {
1258  $this->type = 'securekey';
1259  return $this;
1260  }
1261 
1266  public function setAsProduct()
1267  {
1268  $this->type = 'product';
1269  return $this;
1270  }
1271 
1278  public function setAsCategory($catType)
1279  {
1280  $this->type = 'category:'.$catType;
1281  return $this;
1282  }
1283 
1289  public function setAsTitle()
1290  {
1291  $this->type = 'title';
1292  return $this;
1293  }
1294 
1295 
1302  public function setAsMultiSelect($fieldOptions)
1303  {
1304  if (is_array($fieldOptions)) {
1305  $this->fieldOptions = $fieldOptions;
1306  }
1307 
1308  $this->type = 'multiselect';
1309  return $this;
1310  }
1311 
1318  public function setAsSelect($fieldOptions)
1319  {
1320  if (is_array($fieldOptions)) {
1321  $this->fieldOptions = $fieldOptions;
1322  }
1323 
1324  $this->type = 'select';
1325  return $this;
1326  }
1327 }
FormSetup\sortingItems
sortingItems()
Sort items according to rank.
Definition: html.formsetup.class.php:479
FormSetupItem\generateOutputFieldSelect
generateOutputFieldSelect()
Definition: html.formsetup.class.php:1167
FormSetup\reloadConfs
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
Definition: html.formsetup.class.php:420
FormSetupItem\setAsMultiSelect
setAsMultiSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1302
db
$conf db
API class for accounts.
Definition: inc.php:41
FormSetupItem\$defaultFieldValue
$defaultFieldValue
Definition: html.formsetup.class.php:591
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:1468
FormSetupItem\setAsSecureKey
setAsSecureKey()
Set type of input as secure key.
Definition: html.formsetup.class.php:1256
FormSetupItem\setErrors
setErrors($errors)
Add error.
Definition: html.formsetup.class.php:1027
FormSetupItem\__construct
__construct($confKey)
Constructor.
Definition: html.formsetup.class.php:639
FormSetupItem\setAsThirdpartyType
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
Definition: html.formsetup.class.php:1236
FormSetupItem\generateInputFieldColor
generateInputFieldColor()
Definition: html.formsetup.class.php:1158
FormSetup\saveConfFromPost
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
Definition: html.formsetup.class.php:245
FormSetupItem\generateInputFieldText
generateInputFieldText()
generatec default input field
Definition: html.formsetup.class.php:865
FormSetupItem\setAsCategory
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
Definition: html.formsetup.class.php:1278
FormSetupItem\generateOutputFieldMultiSelect
generateOutputFieldMultiSelect()
Definition: html.formsetup.class.php:1129
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:484
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:6963
FormSetupItem\generateInputFieldSecureKey
generateInputFieldSecureKey()
generate input field for secure key
Definition: html.formsetup.class.php:948
FormSetupItem\setAsTitle
setAsTitle()
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1289
FormSetup\setItemMaxRank
setItemMaxRank($rank)
set new max rank if needed
Definition: html.formsetup.class.php:516
FormSetupItem\generateInputFieldTextarea
generateInputFieldTextarea()
generate input field for textarea
Definition: html.formsetup.class.php:875
FormSetupItem\$rank
$rank
Definition: html.formsetup.class.php:606
FormSetupItem\$helpText
$helpText
Definition: html.formsetup.class.php:585
FormSetupItem\setValueFromPostCallBack
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
Definition: html.formsetup.class.php:734
FormSetupItem\generateOutputField
generateOutputField()
Definition: html.formsetup.class.php:1043
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:112
FormSetupItem\getHelpText
getHelpText()
Get help text or generate it.
Definition: html.formsetup.class.php:781
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:3880
dolGetBadge
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
Definition: functions.lib.php:10274
FormSetupItem\generateInputFieldCategories
generateInputFieldCategories()
generate input field for categories
Definition: html.formsetup.class.php:899
FormSetupItem\getNameText
getNameText()
Get field name text or generate it.
Definition: html.formsetup.class.php:791
FormSetupItem\generateInputField
generateInputField()
generate input field
Definition: html.formsetup.class.php:801
FormSetupItem\$saveCallBack
$saveCallBack
Definition: html.formsetup.class.php:612
FormSetup\addItemFromParams
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:362
FormSetupItem\setAsEmailTemplate
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
Definition: html.formsetup.class.php:1226
FormSetup\__construct
__construct($db, $outputLangs=false)
Constructor.
Definition: html.formsetup.class.php:88
FormSetupItem\setSaveCallBack
setSaveCallBack(callable $callBack)
Set an override function for saving data.
Definition: html.formsetup.class.php:724
FormSetupItem
This class help to create item for class formSetup.
Definition: html.formsetup.class.php:562
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:977
FormSetup\getLineRank
getLineRank($itemKey)
get item position rank from item key
Definition: html.formsetup.class.php:528
FormSetupItem\setValueFromPost
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:743
FormSetup\getCurentItemMaxRank
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
Definition: html.formsetup.class.php:491
FormSetupItem\generateInputFieldHtml
generateInputFieldHtml()
generate input field for html
Definition: html.formsetup.class.php:887
FormSetupItem\setAsHtml
setAsHtml()
Set type of input as html editor.
Definition: html.formsetup.class.php:1215
FormSetupItem\loadValueFromConf
loadValueFromConf()
load conf value from databases
Definition: html.formsetup.class.php:661
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:1266
FormSetup\itemSort
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
Definition: html.formsetup.class.php:544
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:441
FormSetup\addItemsFromParamsArray
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:345
ajax_constantonoff
ajax_constantonoff($code, $input=array(), $entity=null, $revertonoff=0, $strict=0, $forcereload=0, $marginleftonlyshort=2, $forcenoajax=0, $setzeroinsteadofdel=0, $suffix='', $mode='')
On/off button for constant.
Definition: ajax.lib.php:573
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
FormSetupItem\$setValueFromPostCallBack
$setValueFromPostCallBack
Definition: html.formsetup.class.php:615
FormSetupItem\setAsYesNo
setAsYesNo()
Set type of input as Yes.
Definition: html.formsetup.class.php:1246
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:627
FormSetupItem\setAsColor
setAsColor()
Set type of input as color.
Definition: html.formsetup.class.php:1195
FormSetupItem\generateInputFieldEmailTemplate
generateInputFieldEmailTemplate()
generate input field for email template selector
Definition: html.formsetup.class.php:916
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:201
FormSetup\generateOutput
generateOutput($editMode=false)
generateOutput
Definition: html.formsetup.class.php:134
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:1003
FormSetupItem\setAsSelect
setAsSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1318
FormSetupItem\$fieldValue
$fieldValue
Definition: html.formsetup.class.php:588
FormSetupItem\setAsTextarea
setAsTextarea()
Set type of input as textarea.
Definition: html.formsetup.class.php:1205
FormSetupItem\generateInputFieldSelect
generateInputFieldSelect()
Definition: html.formsetup.class.php:991
FormSetupItem\$nameText
$nameText
Definition: html.formsetup.class.php:582
FormSetup\generateLineOutput
generateLineOutput($item, $editMode=false)
generateLineOutput
Definition: html.formsetup.class.php:299
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
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:1185
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:401
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
FormSetupItem\generateOutputFieldColor
generateOutputFieldColor()
Definition: html.formsetup.class.php:1150
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7075
FormSetupItem\reloadValueFromConf
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
Definition: html.formsetup.class.php:678