dolibarr  16.0.1
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 
612  public $saveCallBack;
613 
615  public $setValueFromPostCallBack;
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  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1073  $formmail = new FormMail($this->db);
1074 
1075  $tmp = explode(':', $this->type);
1076 
1077  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1078  if ($template<0) {
1079  $this->setErrors($formmail->errors);
1080  }
1081  $out.= $this->langs->trans($template->label);
1082  } elseif (preg_match('/category:/', $this->type)) {
1083  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1084  $c = new Categorie($this->db);
1085  $result = $c->fetch($this->fieldValue);
1086  if ($result < 0) {
1087  $this->setErrors($c->errors);
1088  }
1089  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1090  $toprint = array();
1091  foreach ($ways as $way) {
1092  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1093  }
1094  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1095  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1096  if ($this->fieldValue==2) {
1097  $out.= $this->langs->trans("Prospect");
1098  } elseif ($this->fieldValue==3) {
1099  $out.= $this->langs->trans("ProspectCustomer");
1100  } elseif ($this->fieldValue==1) {
1101  $out.= $this->langs->trans("Customer");
1102  } elseif ($this->fieldValue==0) {
1103  $out.= $this->langs->trans("NorProspectNorCustomer");
1104  }
1105  } elseif ($this->type == 'product') {
1106  $product = new Product($this->db);
1107  $resprod = $product->fetch($this->fieldValue);
1108  if ($resprod > 0) {
1109  $out.= $product->ref;
1110  } elseif ($resprod < 0) {
1111  $this->setErrors($product->errors);
1112  }
1113  } else {
1114  $out.= $this->fieldValue;
1115  }
1116 
1117  return $out;
1118  }
1119 
1120 
1125  {
1126  $outPut = '';
1127  $TSelected = array();
1128  if (!empty($this->fieldValue)) {
1129  $TSelected = explode(',', $this->fieldValue);
1130  }
1131 
1132  if (!empty($TSelected)) {
1133  foreach ($TSelected as $selected) {
1134  if (!empty($this->fieldOptions[$selected])) {
1135  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1136  }
1137  }
1138  }
1139  return $outPut;
1140  }
1141 
1145  public function generateOutputFieldColor()
1146  {
1147  $this->fieldAttr['disabled']=null;
1148  return $this->generateInputField();
1149  }
1153  public function generateInputFieldColor()
1154  {
1155  $this->fieldAttr['type']= 'color';
1156  return $this->generateInputFieldText();
1157  }
1158 
1162  public function generateOutputFieldSelect()
1163  {
1164  $outPut = '';
1165  if (!empty($this->fieldOptions[$this->fieldValue])) {
1166  $outPut = $this->fieldOptions[$this->fieldValue];
1167  }
1168 
1169  return $outPut;
1170  }
1171 
1172  /*
1173  * METHODS FOR SETTING DISPLAY TYPE
1174  */
1175 
1180  public function setAsString()
1181  {
1182  $this->type = 'string';
1183  return $this;
1184  }
1185 
1190  public function setAsColor()
1191  {
1192  $this->type = 'color';
1193  return $this;
1194  }
1195 
1200  public function setAsTextarea()
1201  {
1202  $this->type = 'textarea';
1203  return $this;
1204  }
1205 
1210  public function setAsHtml()
1211  {
1212  $this->type = 'html';
1213  return $this;
1214  }
1215 
1221  public function setAsEmailTemplate($templateType)
1222  {
1223  $this->type = 'emailtemplate:'.$templateType;
1224  return $this;
1225  }
1226 
1231  public function setAsThirdpartyType()
1232  {
1233  $this->type = 'thirdparty_type';
1234  return $this;
1235  }
1236 
1241  public function setAsYesNo()
1242  {
1243  $this->type = 'yesno';
1244  return $this;
1245  }
1246 
1251  public function setAsSecureKey()
1252  {
1253  $this->type = 'securekey';
1254  return $this;
1255  }
1256 
1261  public function setAsProduct()
1262  {
1263  $this->type = 'product';
1264  return $this;
1265  }
1266 
1273  public function setAsCategory($catType)
1274  {
1275  $this->type = 'category:'.$catType;
1276  return $this;
1277  }
1278 
1284  public function setAsTitle()
1285  {
1286  $this->type = 'title';
1287  return $this;
1288  }
1289 
1290 
1297  public function setAsMultiSelect($fieldOptions)
1298  {
1299  if (is_array($fieldOptions)) {
1300  $this->fieldOptions = $fieldOptions;
1301  }
1302 
1303  $this->type = 'multiselect';
1304  return $this;
1305  }
1306 
1313  public function setAsSelect($fieldOptions)
1314  {
1315  if (is_array($fieldOptions)) {
1316  $this->fieldOptions = $fieldOptions;
1317  }
1318 
1319  $this->type = 'select';
1320  return $this;
1321  }
1322 }
generateLineOutput($item, $editMode=false)
generateLineOutput
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
generateInputField()
generate input field
setAsHtml()
Set type of input as html editor.
loadValueFromConf()
load conf value from databases
Classe permettant la generation du formulaire html d&#39;envoi de mail unitaire Usage: $formail = new For...
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
generateInputFieldEmailTemplate()
generate input field for email template selector
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding= 'UTF-8', $double_encode=false)
Replace htmlentities functions.
$conf db
API class for accounts.
Definition: inc.php:41
getType()
get the type : used for old module builder setup conf style conversion and tests because this two cla...
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Class to manage products or services.
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
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
setAsColor()
Set type of input as color.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
This class help to create item for class formSetup.
setItemMaxRank($rank)
set new max rank if needed
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...
setAsYesNo()
Set type of input as Yes.
setTypeFromTypeString($type)
set the type from string : used for old module builder setup conf style conversion and tests because ...
setAsTextarea()
Set type of input as textarea.
getLineRank($itemKey)
get item position rank from item key
setErrors($errors)
Add error.
setAsSecureKey()
Set type of input as secure key.
Class to build HTML component for third parties management Only common components are here...
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
setAsSelect($fieldOptions)
Set type of input as a simple title no data to store.
Class to manage generation of HTML components Only common components must be here.
Class to manage categories.
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
setAsString()
Set type of input as string.
generateInputFieldText()
generatec default input field
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
setAsMultiSelect($fieldOptions)
Set type of input as a simple title no data to store.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
Classe permettant la generation de composants html autre Only common components are here...
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
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...
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
generateInputFieldTextarea()
generate input field for textarea
__construct($db, $outputLangs=false)
Constructor.
getHelpText()
Get help text or generate it.
generateInputFieldSecureKey()
generate input field for secure key
__construct($confKey)
Constructor.
setAsTitle()
Set type of input as a simple title no data to store.
exportItemsAsParamsArray()
Used to export param array for /core/actions_setmoduleoptions.inc.php template Method exists only for...
getNameText()
Get field name text or generate it.
dolGetBadge($label, $html= '', $type= 'primary', $mode= '', $url= '', $params=array())
Function dolGetBadge.
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
Class to manage a WYSIWYG editor.
generateTableOutput($editMode=false)
generateTableOutput
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:564
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
generateInputFieldHtml()
generate input field for html
This class help you create setup render.
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
sortingItems()
Sort items according to rank.
generateOutput($editMode=false)
generateOutput
generateInputFieldCategories()
generate input field for categories
setAsProduct()
Set type of input as product.
setSaveCallBack(callable $callBack)
Set an override function for saving data.