dolibarr  18.0.0
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, $conf;
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  if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
268  continue;
269  }
270 
271  $res = $item->setValueFromPost();
272  if ($res > 0) {
273  $item->saveConfValue();
274  } elseif ($res < 0) {
275  $error++;
276  break;
277  }
278  }
279 
280  if (!$error) {
281  $this->db->commit();
282  if (empty($noMessageInUpdate)) {
283  setEventMessages($this->langs->trans("SetupSaved"), null);
284  }
285  return 1;
286  } else {
287  $this->db->rollback();
288  if (empty($noMessageInUpdate)) {
289  setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
290  }
291  return -1;
292  }
293  }
294 
302  public function generateLineOutput($item, $editMode = false)
303  {
304 
305  $out = '';
306  if ($item->enabled==1) {
307  $trClass = 'oddeven';
308  if ($item->getType() == 'title') {
309  $trClass = 'liste_titre';
310  }
311 
312  $this->setupNotEmpty++;
313  $out.= '<tr class="'.$trClass.'">';
314 
315  $out.= '<td class="col-setup-title">';
316  $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
317  $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
318  $out.= '</span>';
319  $out.= '</td>';
320 
321  $out.= '<td>';
322 
323  if ($editMode) {
324  $out.= $item->generateInputField();
325  } else {
326  $out.= $item->generateOutputField();
327  }
328 
329  if (!empty($item->errors)) {
330  // TODO : move set event message in a methode to be called by cards not by this class
331  setEventMessages(null, $item->errors, 'errors');
332  }
333 
334  $out.= '</td>';
335  $out.= '</tr>';
336  }
337 
338  return $out;
339  }
340 
341 
348  public function addItemsFromParamsArray($params)
349  {
350  if (!is_array($params) || empty($params)) { return false; }
351  foreach ($params as $confKey => $param) {
352  $this->addItemFromParams($confKey, $param); // todo manage error
353  }
354  return true;
355  }
356 
357 
366  public function addItemFromParams($confKey, $params)
367  {
368  if (empty($confKey) || empty($params['type'])) { return false; }
369 
370  /*
371  * Exemple from old module builder setup page
372  * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
373  // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
374  //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
375  //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
376  //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
377  //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
378  //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
379  //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
380  */
381 
382  $item = new FormSetupItem($confKey);
383  // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage $item->setTypeFromTypeString($params['type']);
385 
386  if (!empty($params['enabled'])) {
387  $item->enabled = $params['enabled'];
388  }
389 
390  if (!empty($params['css'])) {
391  $item->cssClass = $params['css'];
392  }
393 
394  $this->items[$item->confKey] = $item;
395 
396  return true;
397  }
398 
405  public function exportItemsAsParamsArray()
406  {
407  $arrayofparameters = array();
408  foreach ($this->items as $item) {
409  $arrayofparameters[$item->confKey] = array(
410  'type' => $item->getType(),
411  'enabled' => $item->enabled
412  );
413  }
414 
415  return $arrayofparameters;
416  }
417 
424  public function reloadConfs()
425  {
426 
427  if (!array($this->items)) { return false; }
428  foreach ($this->items as $item) {
429  $item->loadValueFromConf();
430  }
431 
432  return true;
433  }
434 
435 
445  public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
446  {
447  $item = new FormSetupItem($confKey);
448 
449  // set item rank if not defined as last item
450  if (empty($item->rank)) {
451  $item->rank = $this->getCurentItemMaxRank() + 1;
452  $this->setItemMaxRank($item->rank); // set new max rank if needed
453  }
454 
455  // try to get rank from target column, this will override item->rank
456  if (!empty($targetItemKey)) {
457  if (isset($this->items[$targetItemKey])) {
458  $targetItem = $this->items[$targetItemKey];
459  $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
460  if ($targetItem->rank >= 0 && $insertAfterTarget) {
461  $item->rank++;
462  }
463  }
464 
465  // calc new rank for each item to make place for new item
466  foreach ($this->items as $fItem) {
467  if ($item->rank <= $fItem->rank) {
468  $fItem->rank = $fItem->rank + 1;
469  $this->setItemMaxRank($fItem->rank); // set new max rank if needed
470  }
471  }
472  }
473 
474  $this->items[$item->confKey] = $item;
475  return $this->items[$item->confKey];
476  }
477 
483  public function sortingItems()
484  {
485  // Sorting
486  return uasort($this->items, array($this, 'itemSort'));
487  }
488 
495  public function getCurentItemMaxRank($cache = true)
496  {
497  if (empty($this->items)) {
498  return 0;
499  }
500 
501  if ($cache && $this->maxItemRank > 0) {
502  return $this->maxItemRank;
503  }
504 
505  $this->maxItemRank = 0;
506  foreach ($this->items as $item) {
507  $this->maxItemRank = max($this->maxItemRank, $item->rank);
508  }
509 
510  return $this->maxItemRank;
511  }
512 
513 
520  public function setItemMaxRank($rank)
521  {
522  $this->maxItemRank = max($this->maxItemRank, $rank);
523  }
524 
525 
532  public function getLineRank($itemKey)
533  {
534  if (!isset($this->items[$itemKey]->rank)) {
535  return -1;
536  }
537  return $this->items[$itemKey]->rank;
538  }
539 
540 
548  public function itemSort(FormSetupItem $a, FormSetupItem $b)
549  {
550  if (empty($a->rank)) {
551  $a->rank = 0;
552  }
553  if (empty($b->rank)) {
554  $b->rank = 0;
555  }
556  if ($a->rank == $b->rank) {
557  return 0;
558  }
559  return ($a->rank < $b->rank) ? -1 : 1;
560  }
561 }
562 
567 {
571  public $db;
572 
574  public $langs;
575 
577  public $entity;
578 
580  public $form;
581 
583  public $confKey;
584 
586  public $nameText = false;
587 
589  public $helpText = '';
590 
592  public $fieldValue;
593 
595  public $defaultFieldValue = null;
596 
598  public $fieldAttr = array();
599 
601  public $fieldOverride = false;
602 
604  public $fieldInputOverride = false;
605 
607  public $fieldOutputOverride = false;
608 
610  public $rank = 0;
611 
613  public $fieldOptions = array();
614 
617 
620 
624  public $errors = array();
625 
632  protected $type = 'string';
633 
634  public $enabled = 1;
635 
636  public $cssClass = '';
637 
643  public function __construct($confKey)
644  {
645  global $langs, $db, $conf, $form;
646  $this->db = $db;
647 
648  if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
649  $this->form = $form;
650  } else {
651  $this->form = new Form($this->db);
652  }
653 
654  $this->langs = $langs;
655  $this->entity = $conf->entity;
656 
657  $this->confKey = $confKey;
658  $this->loadValueFromConf();
659  }
660 
665  public function loadValueFromConf()
666  {
667  global $conf;
668  if (isset($conf->global->{$this->confKey})) {
669  $this->fieldValue = getDolGlobalString($this->confKey);
670  return true;
671  } else {
672  $this->fieldValue = null;
673  return false;
674  }
675  }
676 
682  public function reloadValueFromConf()
683  {
684  return $this->loadValueFromConf();
685  }
686 
687 
693  public function saveConfValue()
694  {
695  global $hookmanager;
696 
697  $parameters = array();
698  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
699  if ($reshook < 0) {
700  $this->setErrors($hookmanager->errors);
701  return -1;
702  }
703 
704  if ($reshook > 0) {
705  return $reshook;
706  }
707 
708 
709  if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
710  return call_user_func($this->saveCallBack, $this);
711  }
712 
713  // Modify constant only if key was posted (avoid resetting key to the null value)
714  if ($this->type != 'title') {
715  $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
716  if ($result < 0) {
717  return -1;
718  } else {
719  return 1;
720  }
721  }
722 
723  return 0;
724  }
725 
732  public function setSaveCallBack(callable $callBack)
733  {
734  $this->saveCallBack = $callBack;
735  }
736 
742  public function setValueFromPostCallBack(callable $callBack)
743  {
744  $this->setValueFromPostCallBack = $callBack;
745  }
746 
751  public function setValueFromPost()
752  {
753  if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
754  return call_user_func($this->setValueFromPostCallBack);
755  }
756 
757  // Modify constant only if key was posted (avoid resetting key to the null value)
758  if ($this->type != 'title') {
759  if (preg_match('/category:/', $this->type)) {
760  if (GETPOST($this->confKey, 'int') == '-1') {
761  $val_const = '';
762  } else {
763  $val_const = GETPOST($this->confKey, 'int');
764  }
765  } elseif ($this->type == 'multiselect') {
766  $val = GETPOST($this->confKey, 'array');
767  if ($val && is_array($val)) {
768  $val_const = implode(',', $val);
769  } else {
770  $val_const = '';
771  }
772  } elseif ($this->type == 'html') {
773  $val_const = GETPOST($this->confKey, 'restricthtml');
774  } else {
775  $val_const = GETPOST($this->confKey, 'alpha');
776  }
777 
778  // TODO add value check with class validate
779  $this->fieldValue = $val_const;
780 
781  return 1;
782  }
783 
784  return 0;
785  }
786 
791  public function getHelpText()
792  {
793  if (!empty($this->helpText)) { return $this->helpText; }
794  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
795  }
796 
801  public function getNameText()
802  {
803  if (!empty($this->nameText)) { return $this->nameText; }
804  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
805  }
806 
811  public function generateInputField()
812  {
813  global $conf;
814 
815  if (!empty($this->fieldOverride)) {
816  return $this->fieldOverride;
817  }
818 
819  if (!empty($this->fieldInputOverride)) {
820  return $this->fieldInputOverride;
821  }
822 
823  // Set default value
824  if (is_null($this->fieldValue)) {
825  $this->fieldValue = $this->defaultFieldValue;
826  }
827 
828 
829  $this->fieldAttr['name'] = $this->confKey;
830  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
831  $this->fieldAttr['value'] = $this->fieldValue;
832 
833  $out = '';
834 
835  if ($this->type == 'title') {
836  $out.= $this->generateOutputField(); // title have no input
837  } elseif ($this->type == 'multiselect') {
838  $out.= $this->generateInputFieldMultiSelect();
839  } elseif ($this->type == 'select') {
840  $out.= $this->generateInputFieldSelect();
841  } elseif ($this->type == 'selectUser') {
842  $out.= $this->generateInputFieldSelectUser();
843  } elseif ($this->type == 'textarea') {
844  $out.= $this->generateInputFieldTextarea();
845  } elseif ($this->type== 'html') {
846  $out.= $this->generateInputFieldHtml();
847  } elseif ($this->type== 'color') {
848  $out.= $this->generateInputFieldColor();
849  } elseif ($this->type == 'yesno') {
850  if (!empty($conf->use_javascript_ajax)) {
851  $out.= ajax_constantonoff($this->confKey);
852  } else {
853  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
854  }
855  } elseif (preg_match('/emailtemplate:/', $this->type)) {
856  $out.= $this->generateInputFieldEmailTemplate();
857  } elseif (preg_match('/category:/', $this->type)) {
858  $out.=$this->generateInputFieldCategories();
859  } elseif (preg_match('/thirdparty_type/', $this->type)) {
860  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
861  $formcompany = new FormCompany($this->db);
862  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
863  } elseif ($this->type == 'securekey') {
864  $out.= $this->generateInputFieldSecureKey();
865  } elseif ($this->type == 'product') {
866  if (isModEnabled("product") || isModEnabled("service")) {
867  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
868  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
869  }
870  } else {
871  $out.= $this->generateInputFieldText();
872  }
873 
874  return $out;
875  }
876 
881  public function generateInputFieldText()
882  {
883  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
884  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
885  }
886 
891  public function generateInputFieldTextarea()
892  {
893  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
894  $out.= dol_htmlentities($this->fieldValue);
895  $out.= "</textarea>\n";
896  return $out;
897  }
898 
903  public function generateInputFieldHtml()
904  {
905  global $conf;
906  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
907  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
908  return $doleditor->Create(1);
909  }
910 
916  {
917  global $conf;
918  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
919  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
920  $formother = new FormOther($this->db);
921 
922  $tmp = explode(':', $this->type);
923  $out= img_picto('', 'category', 'class="pictofixedwidth"');
924  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
925  return $out;
926  }
927 
933  {
934  global $conf, $user;
935  $out = '';
936  if (preg_match('/emailtemplate:/', $this->type)) {
937  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
938  $formmail = new FormMail($this->db);
939 
940  $tmp = explode(':', $this->type);
941  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
942  $arrayOfMessageName = array();
943  if (is_array($formmail->lines_model)) {
944  foreach ($formmail->lines_model as $modelMail) {
945  $moreonlabel = '';
946  if (!empty($arrayOfMessageName[$modelMail->label])) {
947  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
948  }
949  // The 'label' is the key that is unique if we exclude the language
950  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel;
951  }
952  }
953  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
954  }
955 
956  return $out;
957  }
958 
959 
964  public function generateInputFieldSecureKey()
965  {
966  global $conf;
967  $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">';
968  if (!empty($conf->use_javascript_ajax)) {
969  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
970  }
971 
972  // Add button to autosuggest a key
973  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
974  $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
975 
976  return $out;
977  }
978 
979 
984  {
985  $TSelected = array();
986  if ($this->fieldValue) {
987  $TSelected = explode(',', $this->fieldValue);
988  }
989 
990  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
991  }
992 
993 
997  public function generateInputFieldSelect()
998  {
999  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
1000  }
1001 
1006  {
1007  return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1008  }
1009 
1017  public function getType()
1018  {
1019  return $this->type;
1020  }
1021 
1031  public function setTypeFromTypeString($type)
1032  {
1033  $this->type = $type;
1034 
1035  return true;
1036  }
1037 
1044  public function setErrors($errors)
1045  {
1046  if (is_array($errors)) {
1047  if (!empty($errors)) {
1048  foreach ($errors as $error) {
1049  $this->setErrors($error);
1050  }
1051  }
1052  } elseif (!empty($errors)) {
1053  $this->errors[] = $errors;
1054  }
1055  }
1056 
1062  public function generateOutputField()
1063  {
1064  global $conf, $user, $langs;
1065 
1066  if (!empty($this->fieldOverride)) {
1067  return $this->fieldOverride;
1068  }
1069 
1070  if (!empty($this->fieldOutputOverride)) {
1071  return $this->fieldOutputOverride;
1072  }
1073 
1074  $out = '';
1075 
1076  if ($this->type == 'title') {
1077  // nothing to do
1078  } elseif ($this->type == 'textarea') {
1079  $out.= dol_nl2br($this->fieldValue);
1080  } elseif ($this->type == 'multiselect') {
1081  $out.= $this->generateOutputFieldMultiSelect();
1082  } elseif ($this->type == 'select') {
1083  $out.= $this->generateOutputFieldSelect();
1084  } elseif ($this->type == 'selectUser') {
1085  $out.= $this->generateOutputFieldSelectUser();
1086  } elseif ($this->type== 'html') {
1087  $out.= $this->fieldValue;
1088  } elseif ($this->type== 'color') {
1089  $out.= $this->generateOutputFieldColor();
1090  } elseif ($this->type == 'yesno') {
1091  if (!empty($conf->use_javascript_ajax)) {
1092  $out.= ajax_constantonoff($this->confKey);
1093  } else {
1094  if ($this->fieldValue == 1) {
1095  $out.= $langs->trans('yes');
1096  } else {
1097  $out.= $langs->trans('no');
1098  }
1099  }
1100  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1101  if ($this->fieldValue > 0) {
1102  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1103  $formmail = new FormMail($this->db);
1104 
1105  $tmp = explode(':', $this->type);
1106 
1107  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1108  if (is_numeric($template) && $template < 0) {
1109  $this->setErrors($formmail->errors);
1110  }
1111  $out.= $this->langs->trans($template->label);
1112  }
1113  } elseif (preg_match('/category:/', $this->type)) {
1114  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1115  $c = new Categorie($this->db);
1116  $result = $c->fetch($this->fieldValue);
1117  if ($result < 0) {
1118  $this->setErrors($c->errors);
1119  }
1120  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1121  $toprint = array();
1122  foreach ($ways as $way) {
1123  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1124  }
1125  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1126  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1127  if ($this->fieldValue==2) {
1128  $out.= $this->langs->trans("Prospect");
1129  } elseif ($this->fieldValue==3) {
1130  $out.= $this->langs->trans("ProspectCustomer");
1131  } elseif ($this->fieldValue==1) {
1132  $out.= $this->langs->trans("Customer");
1133  } elseif ($this->fieldValue==0) {
1134  $out.= $this->langs->trans("NorProspectNorCustomer");
1135  }
1136  } elseif ($this->type == 'product') {
1137  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1138 
1139  $product = new Product($this->db);
1140  $resprod = $product->fetch($this->fieldValue);
1141  if ($resprod > 0) {
1142  $out.= $product->ref;
1143  } elseif ($resprod < 0) {
1144  $this->setErrors($product->errors);
1145  }
1146  } else {
1147  $out.= $this->fieldValue;
1148  }
1149 
1150  return $out;
1151  }
1152 
1153 
1160  {
1161  $outPut = '';
1162  $TSelected = array();
1163  if (!empty($this->fieldValue)) {
1164  $TSelected = explode(',', $this->fieldValue);
1165  }
1166 
1167  if (!empty($TSelected)) {
1168  foreach ($TSelected as $selected) {
1169  if (!empty($this->fieldOptions[$selected])) {
1170  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1171  }
1172  }
1173  }
1174  return $outPut;
1175  }
1176 
1182  public function generateOutputFieldColor()
1183  {
1184  $this->fieldAttr['disabled']=null;
1185  return $this->generateInputField();
1186  }
1192  public function generateInputFieldColor()
1193  {
1194  $this->fieldAttr['type']= 'color';
1195  return $this->generateInputFieldText();
1196  }
1197 
1203  public function generateOutputFieldSelect()
1204  {
1205  $outPut = '';
1206  if (!empty($this->fieldOptions[$this->fieldValue])) {
1207  $outPut = $this->fieldOptions[$this->fieldValue];
1208  }
1209 
1210  return $outPut;
1211  }
1212 
1219  {
1220  $outPut = '';
1221  $user = new User($this->db);
1222  $user->fetch($this->fieldValue);
1223  $outPut = $user->firstname . " " . $user->lastname;
1224  return $outPut;
1225  }
1226 
1227  /*
1228  * METHODS FOR SETTING DISPLAY TYPE
1229  */
1230 
1236  public function setAsString()
1237  {
1238  $this->type = 'string';
1239  return $this;
1240  }
1241 
1247  public function setAsColor()
1248  {
1249  $this->type = 'color';
1250  return $this;
1251  }
1252 
1258  public function setAsTextarea()
1259  {
1260  $this->type = 'textarea';
1261  return $this;
1262  }
1263 
1269  public function setAsHtml()
1270  {
1271  $this->type = 'html';
1272  return $this;
1273  }
1274 
1281  public function setAsEmailTemplate($templateType)
1282  {
1283  $this->type = 'emailtemplate:'.$templateType;
1284  return $this;
1285  }
1286 
1292  public function setAsThirdpartyType()
1293  {
1294  $this->type = 'thirdparty_type';
1295  return $this;
1296  }
1297 
1303  public function setAsYesNo()
1304  {
1305  $this->type = 'yesno';
1306  return $this;
1307  }
1308 
1314  public function setAsSecureKey()
1315  {
1316  $this->type = 'securekey';
1317  return $this;
1318  }
1319 
1325  public function setAsProduct()
1326  {
1327  $this->type = 'product';
1328  return $this;
1329  }
1330 
1338  public function setAsCategory($catType)
1339  {
1340  $this->type = 'category:'.$catType;
1341  return $this;
1342  }
1343 
1349  public function setAsTitle()
1350  {
1351  $this->type = 'title';
1352  return $this;
1353  }
1354 
1355 
1362  public function setAsMultiSelect($fieldOptions)
1363  {
1364  if (is_array($fieldOptions)) {
1365  $this->fieldOptions = $fieldOptions;
1366  }
1367 
1368  $this->type = 'multiselect';
1369  return $this;
1370  }
1371 
1378  public function setAsSelect($fieldOptions)
1379  {
1380  if (is_array($fieldOptions)) {
1381  $this->fieldOptions = $fieldOptions;
1382  }
1383 
1384  $this->type = 'select';
1385  return $this;
1386  }
1387 
1393  public function setAsSelectUser()
1394  {
1395  $this->type = 'selectUser';
1396  return $this;
1397  }
1398 }
FormSetup\sortingItems
sortingItems()
Sort items according to rank.
Definition: html.formsetup.class.php:483
FormSetupItem\generateOutputFieldSelect
generateOutputFieldSelect()
generateOutputFieldSelect
Definition: html.formsetup.class.php:1203
FormSetup\reloadConfs
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
Definition: html.formsetup.class.php:424
FormSetupItem\setAsMultiSelect
setAsMultiSelect($fieldOptions)
Set type of input as a simple title.
Definition: html.formsetup.class.php:1362
FormSetupItem\$defaultFieldValue
$defaultFieldValue
Definition: html.formsetup.class.php:595
FormSetupItem\setAsSecureKey
setAsSecureKey()
Set type of input as secure key.
Definition: html.formsetup.class.php:1314
FormSetupItem\setErrors
setErrors($errors)
Add error.
Definition: html.formsetup.class.php:1044
FormSetupItem\setAsSelectUser
setAsSelectUser()
Set type of input as a simple title.
Definition: html.formsetup.class.php:1393
FormSetupItem\__construct
__construct($confKey)
Constructor.
Definition: html.formsetup.class.php:643
FormSetupItem\setAsThirdpartyType
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
Definition: html.formsetup.class.php:1292
FormSetupItem\generateInputFieldColor
generateInputFieldColor()
generateInputFieldColor
Definition: html.formsetup.class.php:1192
FormSetup\saveConfFromPost
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
Definition: html.formsetup.class.php:244
FormSetupItem\generateInputFieldText
generateInputFieldText()
generatec default input field
Definition: html.formsetup.class.php:881
FormSetupItem\setAsCategory
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
Definition: html.formsetup.class.php:1338
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
Definition: functions.lib.php:1600
FormSetupItem\generateOutputFieldMultiSelect
generateOutputFieldMultiSelect()
generateOutputFieldMultiSelect
Definition: html.formsetup.class.php:1159
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:609
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:7339
FormSetupItem\generateOutputFieldSelectUser
generateOutputFieldSelectUser()
generateOutputFieldSelectUser
Definition: html.formsetup.class.php:1218
FormSetupItem\generateInputFieldSecureKey
generateInputFieldSecureKey()
generate input field for secure key
Definition: html.formsetup.class.php:964
FormSetupItem\setAsTitle
setAsTitle()
Set type of input as a simple title.
Definition: html.formsetup.class.php:1349
FormSetup\setItemMaxRank
setItemMaxRank($rank)
set new max rank if needed
Definition: html.formsetup.class.php:520
FormSetupItem\generateInputFieldTextarea
generateInputFieldTextarea()
generate input field for textarea
Definition: html.formsetup.class.php:891
FormSetupItem\$rank
$rank
Definition: html.formsetup.class.php:610
FormSetupItem\$helpText
$helpText
Definition: html.formsetup.class.php:589
FormSetupItem\setValueFromPostCallBack
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
Definition: html.formsetup.class.php:742
FormSetupItem\generateOutputField
generateOutputField()
generateOutputField
Definition: html.formsetup.class.php:1062
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
FormSetupItem\generateInputFieldSelectUser
generateInputFieldSelectUser()
Definition: html.formsetup.class.php:1005
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:791
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:4135
dolGetBadge
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
Definition: functions.lib.php:10903
FormSetupItem\generateInputFieldCategories
generateInputFieldCategories()
generate input field for categories
Definition: html.formsetup.class.php:915
FormSetupItem\getNameText
getNameText()
Get field name text or generate it.
Definition: html.formsetup.class.php:801
FormSetupItem\generateInputField
generateInputField()
generate input field
Definition: html.formsetup.class.php:811
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:630
FormSetupItem\$saveCallBack
$saveCallBack
Definition: html.formsetup.class.php:616
FormSetup\addItemFromParams
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:366
FormSetupItem\setAsEmailTemplate
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
Definition: html.formsetup.class.php:1281
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:732
FormSetupItem
This class help to create item for class formSetup.
Definition: html.formsetup.class.php:566
FormSetupItem\saveConfValue
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:693
FormSetupItem\generateInputFieldMultiSelect
generateInputFieldMultiSelect()
Definition: html.formsetup.class.php:983
FormSetup\getLineRank
getLineRank($itemKey)
get item position rank from item key
Definition: html.formsetup.class.php:532
FormSetupItem\setValueFromPost
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:751
FormSetup\getCurentItemMaxRank
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
Definition: html.formsetup.class.php:495
FormSetupItem\generateInputFieldHtml
generateInputFieldHtml()
generate input field for html
Definition: html.formsetup.class.php:903
FormSetupItem\setAsHtml
setAsHtml()
Set type of input as html editor.
Definition: html.formsetup.class.php:1269
FormSetupItem\loadValueFromConf
loadValueFromConf()
load conf value from databases
Definition: html.formsetup.class.php:665
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:1325
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
Definition: functions.lib.php:8673
FormSetup\itemSort
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
Definition: html.formsetup.class.php:548
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:445
FormSetup\addItemsFromParamsArray
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:348
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:573
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11654
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:207
FormSetupItem\$setValueFromPostCallBack
$setValueFromPostCallBack
Definition: html.formsetup.class.php:619
FormSetupItem\setAsYesNo
setAsYesNo()
Set type of input as Yes.
Definition: html.formsetup.class.php:1303
getDolGlobalString
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:142
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:638
User
Class to manage Dolibarr users.
Definition: user.class.php:47
FormSetupItem\setAsColor
setAsColor()
Set type of input as color.
Definition: html.formsetup.class.php:1247
FormSetupItem\generateInputFieldEmailTemplate
generateInputFieldEmailTemplate()
generate input field for email template selector
Definition: html.formsetup.class.php:932
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:53
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:1031
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:1017
FormSetupItem\setAsSelect
setAsSelect($fieldOptions)
Set type of input as a simple title.
Definition: html.formsetup.class.php:1378
FormSetupItem\$fieldValue
$fieldValue
Definition: html.formsetup.class.php:592
FormSetupItem\setAsTextarea
setAsTextarea()
Set type of input as textarea.
Definition: html.formsetup.class.php:1258
FormSetupItem\generateInputFieldSelect
generateInputFieldSelect()
Definition: html.formsetup.class.php:997
FormSetupItem\$nameText
$nameText
Definition: html.formsetup.class.php:586
FormSetup\generateLineOutput
generateLineOutput($item, $editMode=false)
generateLineOutput
Definition: html.formsetup.class.php:302
FormMail
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Definition: html.formmail.class.php:40
FormSetupItem\setAsString
setAsString()
Set type of input as string.
Definition: html.formsetup.class.php:1236
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
FormSetup\exportItemsAsParamsArray
exportItemsAsParamsArray()
Used to export param array for /core/actions_setmoduleoptions.inc.php template Method exists only for...
Definition: html.formsetup.class.php:405
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
FormSetupItem\generateOutputFieldColor
generateOutputFieldColor()
generateOutputFieldColor
Definition: html.formsetup.class.php:1182
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7557
FormSetupItem\reloadValueFromConf
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
Definition: html.formsetup.class.php:682