dolibarr  17.0.3
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  }
355 
356 
365  public function addItemFromParams($confKey, $params)
366  {
367  if (empty($confKey) || empty($params['type'])) { return false; }
368 
369  /*
370  * Exemple from old module builder setup page
371  * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
372  // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
373  //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
374  //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
375  //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
376  //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
377  //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
378  //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
379  */
380 
381  $item = new FormSetupItem($confKey);
382  // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage $item->setTypeFromTypeString($params['type']);
384 
385  if (!empty($params['enabled'])) {
386  $item->enabled = $params['enabled'];
387  }
388 
389  if (!empty($params['css'])) {
390  $item->cssClass = $params['css'];
391  }
392 
393  $this->items[$item->confKey] = $item;
394 
395  return true;
396  }
397 
404  public function exportItemsAsParamsArray()
405  {
406  $arrayofparameters = array();
407  foreach ($this->items as $item) {
408  $arrayofparameters[$item->confKey] = array(
409  'type' => $item->getType(),
410  'enabled' => $item->enabled
411  );
412  }
413 
414  return $arrayofparameters;
415  }
416 
423  public function reloadConfs()
424  {
425 
426  if (!array($this->items)) { return false; }
427  foreach ($this->items as $item) {
428  $item->loadValueFromConf();
429  }
430 
431  return true;
432  }
433 
434 
444  public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
445  {
446  $item = new FormSetupItem($confKey);
447 
448  // set item rank if not defined as last item
449  if (empty($item->rank)) {
450  $item->rank = $this->getCurentItemMaxRank() + 1;
451  $this->setItemMaxRank($item->rank); // set new max rank if needed
452  }
453 
454  // try to get rank from target column, this will override item->rank
455  if (!empty($targetItemKey)) {
456  if (isset($this->items[$targetItemKey])) {
457  $targetItem = $this->items[$targetItemKey];
458  $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
459  if ($targetItem->rank >= 0 && $insertAfterTarget) {
460  $item->rank++;
461  }
462  }
463 
464  // calc new rank for each item to make place for new item
465  foreach ($this->items as $fItem) {
466  if ($item->rank <= $fItem->rank) {
467  $fItem->rank = $fItem->rank + 1;
468  $this->setItemMaxRank($fItem->rank); // set new max rank if needed
469  }
470  }
471  }
472 
473  $this->items[$item->confKey] = $item;
474  return $this->items[$item->confKey];
475  }
476 
482  public function sortingItems()
483  {
484  // Sorting
485  return uasort($this->items, array($this, 'itemSort'));
486  }
487 
494  public function getCurentItemMaxRank($cache = true)
495  {
496  if (empty($this->items)) {
497  return 0;
498  }
499 
500  if ($cache && $this->maxItemRank > 0) {
501  return $this->maxItemRank;
502  }
503 
504  $this->maxItemRank = 0;
505  foreach ($this->items as $item) {
506  $this->maxItemRank = max($this->maxItemRank, $item->rank);
507  }
508 
509  return $this->maxItemRank;
510  }
511 
512 
519  public function setItemMaxRank($rank)
520  {
521  $this->maxItemRank = max($this->maxItemRank, $rank);
522  }
523 
524 
531  public function getLineRank($itemKey)
532  {
533  if (!isset($this->items[$itemKey]->rank)) {
534  return -1;
535  }
536  return $this->items[$itemKey]->rank;
537  }
538 
539 
547  public function itemSort(FormSetupItem $a, FormSetupItem $b)
548  {
549  if (empty($a->rank)) {
550  $a->rank = 0;
551  }
552  if (empty($b->rank)) {
553  $b->rank = 0;
554  }
555  if ($a->rank == $b->rank) {
556  return 0;
557  }
558  return ($a->rank < $b->rank) ? -1 : 1;
559  }
560 }
561 
566 {
570  public $db;
571 
573  public $langs;
574 
576  public $entity;
577 
579  public $form;
580 
582  public $confKey;
583 
585  public $nameText = false;
586 
588  public $helpText = '';
589 
591  public $fieldValue;
592 
594  public $defaultFieldValue = null;
595 
597  public $fieldAttr = array();
598 
600  public $fieldOverride = false;
601 
603  public $fieldInputOverride = false;
604 
606  public $fieldOutputOverride = false;
607 
609  public $rank = 0;
610 
612  public $fieldOptions = array();
613 
616 
619 
623  public $errors = array();
624 
631  protected $type = 'string';
632 
633  public $enabled = 1;
634 
635  public $cssClass = '';
636 
642  public function __construct($confKey)
643  {
644  global $langs, $db, $conf, $form;
645  $this->db = $db;
646 
647  if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
648  $this->form = $form;
649  } else {
650  $this->form = new Form($this->db);
651  }
652 
653  $this->langs = $langs;
654  $this->entity = $conf->entity;
655 
656  $this->confKey = $confKey;
657  $this->loadValueFromConf();
658  }
659 
664  public function loadValueFromConf()
665  {
666  global $conf;
667  if (isset($conf->global->{$this->confKey})) {
668  $this->fieldValue = $conf->global->{$this->confKey};
669  return true;
670  } else {
671  $this->fieldValue = null;
672  return false;
673  }
674  }
675 
681  public function reloadValueFromConf()
682  {
683  return $this->loadValueFromConf();
684  }
685 
686 
691  public function saveConfValue()
692  {
693  global $hookmanager;
694 
695  $parameters = array();
696  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
697  if ($reshook < 0) {
698  $this->setErrors($hookmanager->errors);
699  return -1;
700  }
701 
702  if ($reshook > 0) {
703  return $reshook;
704  }
705 
706 
707  if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
708  return call_user_func($this->saveCallBack, $this);
709  }
710 
711  // Modify constant only if key was posted (avoid resetting key to the null value)
712  if ($this->type != 'title') {
713  $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
714  if ($result < 0) {
715  return -1;
716  } else {
717  return 1;
718  }
719  }
720  }
721 
727  public function setSaveCallBack(callable $callBack)
728  {
729  $this->saveCallBack = $callBack;
730  }
731 
737  public function setValueFromPostCallBack(callable $callBack)
738  {
739  $this->setValueFromPostCallBack = $callBack;
740  }
741 
746  public function setValueFromPost()
747  {
748  if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
749  return call_user_func($this->setValueFromPostCallBack);
750  }
751 
752  // Modify constant only if key was posted (avoid resetting key to the null value)
753  if ($this->type != 'title') {
754  if (preg_match('/category:/', $this->type)) {
755  if (GETPOST($this->confKey, 'int') == '-1') {
756  $val_const = '';
757  } else {
758  $val_const = GETPOST($this->confKey, 'int');
759  }
760  } elseif ($this->type == 'multiselect') {
761  $val = GETPOST($this->confKey, 'array');
762  if ($val && is_array($val)) {
763  $val_const = implode(',', $val);
764  } else {
765  $val_const = '';
766  }
767  } elseif ($this->type == 'html') {
768  $val_const = GETPOST($this->confKey, 'restricthtml');
769  } else {
770  $val_const = GETPOST($this->confKey, 'alpha');
771  }
772 
773  // TODO add value check with class validate
774  $this->fieldValue = $val_const;
775 
776  return 1;
777  }
778 
779  return 0;
780  }
781 
786  public function getHelpText()
787  {
788  if (!empty($this->helpText)) { return $this->helpText; }
789  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
790  }
791 
796  public function getNameText()
797  {
798  if (!empty($this->nameText)) { return $this->nameText; }
799  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
800  }
801 
806  public function generateInputField()
807  {
808  global $conf;
809 
810  if (!empty($this->fieldOverride)) {
811  return $this->fieldOverride;
812  }
813 
814  if (!empty($this->fieldInputOverride)) {
815  return $this->fieldInputOverride;
816  }
817 
818  // Set default value
819  if (is_null($this->fieldValue)) {
820  $this->fieldValue = $this->defaultFieldValue;
821  }
822 
823 
824  $this->fieldAttr['name'] = $this->confKey;
825  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
826  $this->fieldAttr['value'] = $this->fieldValue;
827 
828  $out = '';
829 
830  if ($this->type == 'title') {
831  $out.= $this->generateOutputField(); // title have no input
832  } elseif ($this->type == 'multiselect') {
833  $out.= $this->generateInputFieldMultiSelect();
834  } elseif ($this->type == 'select') {
835  $out.= $this->generateInputFieldSelect();
836  } elseif ($this->type == 'textarea') {
837  $out.= $this->generateInputFieldTextarea();
838  } elseif ($this->type== 'html') {
839  $out.= $this->generateInputFieldHtml();
840  } elseif ($this->type== 'color') {
841  $out.= $this->generateInputFieldColor();
842  } elseif ($this->type == 'yesno') {
843  if (!empty($conf->use_javascript_ajax)) {
844  $out.= ajax_constantonoff($this->confKey);
845  } else {
846  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
847  }
848  } elseif (preg_match('/emailtemplate:/', $this->type)) {
849  $out.= $this->generateInputFieldEmailTemplate();
850  } elseif (preg_match('/category:/', $this->type)) {
851  $out.=$this->generateInputFieldCategories();
852  } elseif (preg_match('/thirdparty_type/', $this->type)) {
853  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
854  $formcompany = new FormCompany($this->db);
855  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
856  } elseif ($this->type == 'securekey') {
857  $out.= $this->generateInputFieldSecureKey();
858  } elseif ($this->type == 'product') {
859  if (isModEnabled("product") || isModEnabled("service")) {
860  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
861  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
862  }
863  } else {
864  $out.= $this->generateInputFieldText();
865  }
866 
867  return $out;
868  }
869 
874  public function generateInputFieldText()
875  {
876  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
877  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
878  }
879 
884  public function generateInputFieldTextarea()
885  {
886  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
887  $out.= dol_htmlentities($this->fieldValue);
888  $out.= "</textarea>\n";
889  return $out;
890  }
891 
896  public function generateInputFieldHtml()
897  {
898  global $conf;
899  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
900  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%');
901  return $doleditor->Create(1);
902  }
903 
909  {
910  global $conf;
911  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
912  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
913  $formother = new FormOther($this->db);
914 
915  $tmp = explode(':', $this->type);
916  $out= img_picto('', 'category', 'class="pictofixedwidth"');
917  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
918  return $out;
919  }
920 
926  {
927  global $conf, $user;
928  $out = '';
929  if (preg_match('/emailtemplate:/', $this->type)) {
930  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
931  $formmail = new FormMail($this->db);
932 
933  $tmp = explode(':', $this->type);
934  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
935  $arrayOfMessageName = array();
936  if (is_array($formmail->lines_model)) {
937  foreach ($formmail->lines_model as $modelMail) {
938  $moreonlabel = '';
939  if (!empty($arrayOfMessageName[$modelMail->label])) {
940  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
941  }
942  // The 'label' is the key that is unique if we exclude the language
943  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel;
944  }
945  }
946  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
947  }
948 
949  return $out;
950  }
951 
952 
957  public function generateInputFieldSecureKey()
958  {
959  global $conf;
960  $out = '<input required="required" type="text" class="flat" id="'.$this->confKey.'" name="'.$this->confKey.'" value="'.(GETPOST($this->confKey, 'alpha') ?GETPOST($this->confKey, 'alpha') : $this->fieldValue).'" size="40">';
961  if (!empty($conf->use_javascript_ajax)) {
962  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
963  }
964 
965  // Add button to autosuggest a key
966  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
967  $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
968 
969  return $out;
970  }
971 
972 
977  {
978  $TSelected = array();
979  if ($this->fieldValue) {
980  $TSelected = explode(',', $this->fieldValue);
981  }
982 
983  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
984  }
985 
986 
990  public function generateInputFieldSelect()
991  {
992  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
993  }
994 
1002  public function getType()
1003  {
1004  return $this->type;
1005  }
1006 
1015  public function setTypeFromTypeString($type)
1016  {
1017  $this->type = $type;
1018  return true;
1019  }
1020 
1026  public function setErrors($errors)
1027  {
1028  if (is_array($errors)) {
1029  if (!empty($errors)) {
1030  foreach ($errors as $error) {
1031  $this->setErrors($error);
1032  }
1033  }
1034  } elseif (!empty($errors)) {
1035  $this->errors[] = $errors;
1036  }
1037  }
1038 
1042  public function generateOutputField()
1043  {
1044  global $conf, $user, $langs;
1045 
1046  if (!empty($this->fieldOverride)) {
1047  return $this->fieldOverride;
1048  }
1049 
1050  if (!empty($this->fieldOutputOverride)) {
1051  return $this->fieldOutputOverride;
1052  }
1053 
1054  $out = '';
1055 
1056  if ($this->type == 'title') {
1057  // nothing to do
1058  } elseif ($this->type == 'textarea') {
1059  $out.= dol_nl2br($this->fieldValue);
1060  } elseif ($this->type == 'multiselect') {
1061  $out.= $this->generateOutputFieldMultiSelect();
1062  } elseif ($this->type == 'select') {
1063  $out.= $this->generateOutputFieldSelect();
1064  } elseif ($this->type== 'html') {
1065  $out.= $this->fieldValue;
1066  } elseif ($this->type== 'color') {
1067  $out.= $this->generateOutputFieldColor();
1068  } elseif ($this->type == 'yesno') {
1069  if (!empty($conf->use_javascript_ajax)) {
1070  $out.= ajax_constantonoff($this->confKey);
1071  } else {
1072  if ($this->fieldValue == 1) {
1073  $out.= $langs->trans('yes');
1074  } else {
1075  $out.= $langs->trans('no');
1076  }
1077  }
1078  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1079  if ($this->fieldValue > 0) {
1080  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1081  $formmail = new FormMail($this->db);
1082 
1083  $tmp = explode(':', $this->type);
1084 
1085  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1086  if (is_numeric($template) && $template < 0) {
1087  $this->setErrors($formmail->errors);
1088  }
1089  $out.= $this->langs->trans($template->label);
1090  }
1091  } elseif (preg_match('/category:/', $this->type)) {
1092  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1093  $c = new Categorie($this->db);
1094  $result = $c->fetch($this->fieldValue);
1095  if ($result < 0) {
1096  $this->setErrors($c->errors);
1097  }
1098  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1099  $toprint = array();
1100  foreach ($ways as $way) {
1101  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1102  }
1103  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1104  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1105  if ($this->fieldValue==2) {
1106  $out.= $this->langs->trans("Prospect");
1107  } elseif ($this->fieldValue==3) {
1108  $out.= $this->langs->trans("ProspectCustomer");
1109  } elseif ($this->fieldValue==1) {
1110  $out.= $this->langs->trans("Customer");
1111  } elseif ($this->fieldValue==0) {
1112  $out.= $this->langs->trans("NorProspectNorCustomer");
1113  }
1114  } elseif ($this->type == 'product') {
1115  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1116 
1117  $product = new Product($this->db);
1118  $resprod = $product->fetch($this->fieldValue);
1119  if ($resprod > 0) {
1120  $out.= $product->ref;
1121  } elseif ($resprod < 0) {
1122  $this->setErrors($product->errors);
1123  }
1124  } else {
1125  $out.= $this->fieldValue;
1126  }
1127 
1128  return $out;
1129  }
1130 
1131 
1136  {
1137  $outPut = '';
1138  $TSelected = array();
1139  if (!empty($this->fieldValue)) {
1140  $TSelected = explode(',', $this->fieldValue);
1141  }
1142 
1143  if (!empty($TSelected)) {
1144  foreach ($TSelected as $selected) {
1145  if (!empty($this->fieldOptions[$selected])) {
1146  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1147  }
1148  }
1149  }
1150  return $outPut;
1151  }
1152 
1156  public function generateOutputFieldColor()
1157  {
1158  $this->fieldAttr['disabled']=null;
1159  return $this->generateInputField();
1160  }
1164  public function generateInputFieldColor()
1165  {
1166  $this->fieldAttr['type']= 'color';
1167  return $this->generateInputFieldText();
1168  }
1169 
1173  public function generateOutputFieldSelect()
1174  {
1175  $outPut = '';
1176  if (!empty($this->fieldOptions[$this->fieldValue])) {
1177  $outPut = $this->fieldOptions[$this->fieldValue];
1178  }
1179 
1180  return $outPut;
1181  }
1182 
1183  /*
1184  * METHODS FOR SETTING DISPLAY TYPE
1185  */
1186 
1191  public function setAsString()
1192  {
1193  $this->type = 'string';
1194  return $this;
1195  }
1196 
1201  public function setAsColor()
1202  {
1203  $this->type = 'color';
1204  return $this;
1205  }
1206 
1211  public function setAsTextarea()
1212  {
1213  $this->type = 'textarea';
1214  return $this;
1215  }
1216 
1221  public function setAsHtml()
1222  {
1223  $this->type = 'html';
1224  return $this;
1225  }
1226 
1232  public function setAsEmailTemplate($templateType)
1233  {
1234  $this->type = 'emailtemplate:'.$templateType;
1235  return $this;
1236  }
1237 
1242  public function setAsThirdpartyType()
1243  {
1244  $this->type = 'thirdparty_type';
1245  return $this;
1246  }
1247 
1252  public function setAsYesNo()
1253  {
1254  $this->type = 'yesno';
1255  return $this;
1256  }
1257 
1262  public function setAsSecureKey()
1263  {
1264  $this->type = 'securekey';
1265  return $this;
1266  }
1267 
1272  public function setAsProduct()
1273  {
1274  $this->type = 'product';
1275  return $this;
1276  }
1277 
1284  public function setAsCategory($catType)
1285  {
1286  $this->type = 'category:'.$catType;
1287  return $this;
1288  }
1289 
1295  public function setAsTitle()
1296  {
1297  $this->type = 'title';
1298  return $this;
1299  }
1300 
1301 
1308  public function setAsMultiSelect($fieldOptions)
1309  {
1310  if (is_array($fieldOptions)) {
1311  $this->fieldOptions = $fieldOptions;
1312  }
1313 
1314  $this->type = 'multiselect';
1315  return $this;
1316  }
1317 
1324  public function setAsSelect($fieldOptions)
1325  {
1326  if (is_array($fieldOptions)) {
1327  $this->fieldOptions = $fieldOptions;
1328  }
1329 
1330  $this->type = 'select';
1331  return $this;
1332  }
1333 }
FormSetup\sortingItems
sortingItems()
Sort items according to rank.
Definition: html.formsetup.class.php:482
FormSetupItem\generateOutputFieldSelect
generateOutputFieldSelect()
Definition: html.formsetup.class.php:1173
FormSetup\reloadConfs
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
Definition: html.formsetup.class.php:423
FormSetupItem\setAsMultiSelect
setAsMultiSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1308
db
$conf db
API class for accounts.
Definition: inc.php:41
FormSetupItem\$defaultFieldValue
$defaultFieldValue
Definition: html.formsetup.class.php:594
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:1493
FormSetupItem\setAsSecureKey
setAsSecureKey()
Set type of input as secure key.
Definition: html.formsetup.class.php:1262
FormSetupItem\setErrors
setErrors($errors)
Add error.
Definition: html.formsetup.class.php:1026
FormSetupItem\__construct
__construct($confKey)
Constructor.
Definition: html.formsetup.class.php:642
FormSetupItem\setAsThirdpartyType
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
Definition: html.formsetup.class.php:1242
FormSetupItem\generateInputFieldColor
generateInputFieldColor()
Definition: html.formsetup.class.php:1164
FormSetup\saveConfFromPost
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
Definition: html.formsetup.class.php:244
FormSetupItem\generateInputFieldText
generateInputFieldText()
generatec default input field
Definition: html.formsetup.class.php:874
FormSetupItem\setAsCategory
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
Definition: html.formsetup.class.php:1284
FormSetupItem\generateOutputFieldMultiSelect
generateOutputFieldMultiSelect()
Definition: html.formsetup.class.php:1135
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:520
dol_nl2br
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
Definition: functions.lib.php:7160
FormSetupItem\generateInputFieldSecureKey
generateInputFieldSecureKey()
generate input field for secure key
Definition: html.formsetup.class.php:957
FormSetupItem\setAsTitle
setAsTitle()
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1295
FormSetup\setItemMaxRank
setItemMaxRank($rank)
set new max rank if needed
Definition: html.formsetup.class.php:519
FormSetupItem\generateInputFieldTextarea
generateInputFieldTextarea()
generate input field for textarea
Definition: html.formsetup.class.php:884
FormSetupItem\$rank
$rank
Definition: html.formsetup.class.php:609
FormSetupItem\$helpText
$helpText
Definition: html.formsetup.class.php:588
FormSetupItem\setValueFromPostCallBack
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
Definition: html.formsetup.class.php:737
FormSetupItem\generateOutputField
generateOutputField()
Definition: html.formsetup.class.php:1042
FormOther
Classe permettant la generation de composants html autre Only common components are here.
Definition: html.formother.class.php:39
Categorie
Class to manage categories.
Definition: categorie.class.php:47
FormSetup\generateAttributesStringFromArray
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
Definition: html.formsetup.class.php:111
FormSetupItem\getHelpText
getHelpText()
Get help text or generate it.
Definition: html.formsetup.class.php:786
FormCompany
Class to build HTML component for third parties management Only common components are here.
Definition: html.formcompany.class.php:40
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4024
dolGetBadge
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
Definition: functions.lib.php:10681
FormSetupItem\generateInputFieldCategories
generateInputFieldCategories()
generate input field for categories
Definition: html.formsetup.class.php:908
FormSetupItem\getNameText
getNameText()
Get field name text or generate it.
Definition: html.formsetup.class.php:796
FormSetupItem\generateInputField
generateInputField()
generate input field
Definition: html.formsetup.class.php:806
FormSetupItem\$saveCallBack
$saveCallBack
Definition: html.formsetup.class.php:615
FormSetup\addItemFromParams
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:365
FormSetupItem\setAsEmailTemplate
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
Definition: html.formsetup.class.php:1232
FormSetup\__construct
__construct($db, $outputLangs=false)
Constructor.
Definition: html.formsetup.class.php:87
FormSetupItem\setSaveCallBack
setSaveCallBack(callable $callBack)
Set an override function for saving data.
Definition: html.formsetup.class.php:727
FormSetupItem
This class help to create item for class formSetup.
Definition: html.formsetup.class.php:565
FormSetupItem\saveConfValue
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:691
FormSetupItem\generateInputFieldMultiSelect
generateInputFieldMultiSelect()
Definition: html.formsetup.class.php:976
FormSetup\getLineRank
getLineRank($itemKey)
get item position rank from item key
Definition: html.formsetup.class.php:531
FormSetupItem\setValueFromPost
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
Definition: html.formsetup.class.php:746
FormSetup\getCurentItemMaxRank
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
Definition: html.formsetup.class.php:494
FormSetupItem\generateInputFieldHtml
generateInputFieldHtml()
generate input field for html
Definition: html.formsetup.class.php:896
FormSetupItem\setAsHtml
setAsHtml()
Set type of input as html editor.
Definition: html.formsetup.class.php:1221
FormSetupItem\loadValueFromConf
loadValueFromConf()
load conf value from databases
Definition: html.formsetup.class.php:664
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:1272
FormSetup\itemSort
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
Definition: html.formsetup.class.php:547
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:444
FormSetup\addItemsFromParamsArray
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
Definition: html.formsetup.class.php:348
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:600
dolJSToSetRandomPassword
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Ouput javacript to autoset a generated password using default module into a HTML element.
Definition: security2.lib.php:568
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:11295
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:137
FormSetupItem\$setValueFromPostCallBack
$setValueFromPostCallBack
Definition: html.formsetup.class.php:618
FormSetupItem\setAsYesNo
setAsYesNo()
Set type of input as Yes.
Definition: html.formsetup.class.php:1252
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:632
FormSetupItem\setAsColor
setAsColor()
Set type of input as color.
Definition: html.formsetup.class.php:1201
FormSetupItem\generateInputFieldEmailTemplate
generateInputFieldEmailTemplate()
generate input field for email template selector
Definition: html.formsetup.class.php:925
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php: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:1015
FormSetupItem\getType
getType()
get the type : used for old module builder setup conf style conversion and tests because this two cla...
Definition: html.formsetup.class.php:1002
FormSetupItem\setAsSelect
setAsSelect($fieldOptions)
Set type of input as a simple title no data to store.
Definition: html.formsetup.class.php:1324
FormSetupItem\$fieldValue
$fieldValue
Definition: html.formsetup.class.php:591
FormSetupItem\setAsTextarea
setAsTextarea()
Set type of input as textarea.
Definition: html.formsetup.class.php:1211
FormSetupItem\generateInputFieldSelect
generateInputFieldSelect()
Definition: html.formsetup.class.php:990
FormSetupItem\$nameText
$nameText
Definition: html.formsetup.class.php:585
FormSetup\generateLineOutput
generateLineOutput($item, $editMode=false)
generateLineOutput
Definition: html.formsetup.class.php:302
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8466
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:1191
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:404
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
FormSetupItem\generateOutputFieldColor
generateOutputFieldColor()
Definition: html.formsetup.class.php:1156
dol_htmlentities
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
Definition: functions.lib.php:7365
FormSetupItem\reloadValueFromConf
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
Definition: html.formsetup.class.php:681