dolibarr  x.y.z
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 = getDolGlobalString($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  } else {
762  $val_const = '';
763  }
764  } elseif ($this->type == 'html') {
765  $val_const = GETPOST($this->confKey, 'restricthtml');
766  } else {
767  $val_const = GETPOST($this->confKey, 'alpha');
768  }
769 
770  // TODO add value check with class validate
771  $this->fieldValue = $val_const;
772 
773  return 1;
774  }
775 
776  return 0;
777  }
778 
783  public function getHelpText()
784  {
785  if (!empty($this->helpText)) { return $this->helpText; }
786  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
787  }
788 
793  public function getNameText()
794  {
795  if (!empty($this->nameText)) { return $this->nameText; }
796  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
797  }
798 
803  public function generateInputField()
804  {
805  global $conf;
806 
807  if (!empty($this->fieldOverride)) {
808  return $this->fieldOverride;
809  }
810 
811  if (!empty($this->fieldInputOverride)) {
812  return $this->fieldInputOverride;
813  }
814 
815  // Set default value
816  if (is_null($this->fieldValue)) {
817  $this->fieldValue = $this->defaultFieldValue;
818  }
819 
820 
821  $this->fieldAttr['name'] = $this->confKey;
822  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
823  $this->fieldAttr['value'] = $this->fieldValue;
824 
825  $out = '';
826 
827  if ($this->type == 'title') {
828  $out.= $this->generateOutputField(); // title have no input
829  } elseif ($this->type == 'multiselect') {
830  $out.= $this->generateInputFieldMultiSelect();
831  } elseif ($this->type == 'select') {
832  $out.= $this->generateInputFieldSelect();
833  } elseif ($this->type == 'textarea') {
834  $out.= $this->generateInputFieldTextarea();
835  } elseif ($this->type== 'html') {
836  $out.= $this->generateInputFieldHtml();
837  } elseif ($this->type== 'color') {
838  $out.= $this->generateInputFieldColor();
839  } elseif ($this->type == 'yesno') {
840  if (!empty($conf->use_javascript_ajax)) {
841  $out.= ajax_constantonoff($this->confKey);
842  } else {
843  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
844  }
845  } elseif (preg_match('/emailtemplate:/', $this->type)) {
846  $out.= $this->generateInputFieldEmailTemplate();
847  } elseif (preg_match('/category:/', $this->type)) {
848  $out.=$this->generateInputFieldCategories();
849  } elseif (preg_match('/thirdparty_type/', $this->type)) {
850  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
851  $formcompany = new FormCompany($this->db);
852  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
853  } elseif ($this->type == 'securekey') {
854  $out.= $this->generateInputFieldSecureKey();
855  } elseif ($this->type == 'product') {
856  if (isModEnabled("product") || isModEnabled("service")) {
857  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
858  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
859  }
860  } else {
861  $out.= $this->generateInputFieldText();
862  }
863 
864  return $out;
865  }
866 
871  public function generateInputFieldText()
872  {
873  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
874  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
875  }
876 
881  public function generateInputFieldTextarea()
882  {
883  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
884  $out.= dol_htmlentities($this->fieldValue);
885  $out.= "</textarea>\n";
886  return $out;
887  }
888 
893  public function generateInputFieldHtml()
894  {
895  global $conf;
896  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
897  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%');
898  return $doleditor->Create(1);
899  }
900 
906  {
907  global $conf;
908  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
909  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
910  $formother = new FormOther($this->db);
911 
912  $tmp = explode(':', $this->type);
913  $out= img_picto('', 'category', 'class="pictofixedwidth"');
914  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
915  return $out;
916  }
917 
923  {
924  global $conf, $user;
925  $out = '';
926  if (preg_match('/emailtemplate:/', $this->type)) {
927  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
928  $formmail = new FormMail($this->db);
929 
930  $tmp = explode(':', $this->type);
931  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
932  $arrayOfMessageName = array();
933  if (is_array($formmail->lines_model)) {
934  foreach ($formmail->lines_model as $modelMail) {
935  $moreonlabel = '';
936  if (!empty($arrayOfMessageName[$modelMail->label])) {
937  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
938  }
939  // The 'label' is the key that is unique if we exclude the language
940  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
941  }
942  }
943  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
944  }
945 
946  return $out;
947  }
948 
949 
954  public function generateInputFieldSecureKey()
955  {
956  global $conf;
957  $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">';
958  if (!empty($conf->use_javascript_ajax)) {
959  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
960  }
961 
962  // Add button to autosuggest a key
963  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
964  $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
965 
966  return $out;
967  }
968 
969 
974  {
975  $TSelected = array();
976  if ($this->fieldValue) {
977  $TSelected = explode(',', $this->fieldValue);
978  }
979 
980  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
981  }
982 
983 
987  public function generateInputFieldSelect()
988  {
989  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
990  }
991 
999  public function getType()
1000  {
1001  return $this->type;
1002  }
1003 
1012  public function setTypeFromTypeString($type)
1013  {
1014  $this->type = $type;
1015  return true;
1016  }
1017 
1023  public function setErrors($errors)
1024  {
1025  if (is_array($errors)) {
1026  if (!empty($errors)) {
1027  foreach ($errors as $error) {
1028  $this->setErrors($error);
1029  }
1030  }
1031  } elseif (!empty($errors)) {
1032  $this->errors[] = $errors;
1033  }
1034  }
1035 
1039  public function generateOutputField()
1040  {
1041  global $conf, $user, $langs;
1042 
1043  if (!empty($this->fieldOverride)) {
1044  return $this->fieldOverride;
1045  }
1046 
1047  if (!empty($this->fieldOutputOverride)) {
1048  return $this->fieldOutputOverride;
1049  }
1050 
1051  $out = '';
1052 
1053  if ($this->type == 'title') {
1054  // nothing to do
1055  } elseif ($this->type == 'textarea') {
1056  $out.= dol_nl2br($this->fieldValue);
1057  } elseif ($this->type == 'multiselect') {
1058  $out.= $this->generateOutputFieldMultiSelect();
1059  } elseif ($this->type == 'select') {
1060  $out.= $this->generateOutputFieldSelect();
1061  } elseif ($this->type== 'html') {
1062  $out.= $this->fieldValue;
1063  } elseif ($this->type== 'color') {
1064  $out.= $this->generateOutputFieldColor();
1065  } elseif ($this->type == 'yesno') {
1066  if (!empty($conf->use_javascript_ajax)) {
1067  $out.= ajax_constantonoff($this->confKey);
1068  } else {
1069  if ($this->fieldValue == 1) {
1070  $out.= $langs->trans('yes');
1071  } else {
1072  $out.= $langs->trans('no');
1073  }
1074  }
1075  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1076  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1077  $formmail = new FormMail($this->db);
1078 
1079  $tmp = explode(':', $this->type);
1080 
1081  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1082  if (is_numeric($template) && $template < 0) {
1083  $this->setErrors($formmail->errors);
1084  }
1085  $out.= $this->langs->trans($template->label);
1086  } elseif (preg_match('/category:/', $this->type)) {
1087  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1088  $c = new Categorie($this->db);
1089  $result = $c->fetch($this->fieldValue);
1090  if ($result < 0) {
1091  $this->setErrors($c->errors);
1092  }
1093  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1094  $toprint = array();
1095  foreach ($ways as $way) {
1096  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1097  }
1098  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1099  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1100  if ($this->fieldValue==2) {
1101  $out.= $this->langs->trans("Prospect");
1102  } elseif ($this->fieldValue==3) {
1103  $out.= $this->langs->trans("ProspectCustomer");
1104  } elseif ($this->fieldValue==1) {
1105  $out.= $this->langs->trans("Customer");
1106  } elseif ($this->fieldValue==0) {
1107  $out.= $this->langs->trans("NorProspectNorCustomer");
1108  }
1109  } elseif ($this->type == 'product') {
1110  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1111  $product = new Product($this->db);
1112  $resprod = $product->fetch($this->fieldValue);
1113  if ($resprod > 0) {
1114  $out.= $product->ref;
1115  } elseif ($resprod < 0) {
1116  $this->setErrors($product->errors);
1117  }
1118  } else {
1119  $out.= $this->fieldValue;
1120  }
1121 
1122  return $out;
1123  }
1124 
1125 
1130  {
1131  $outPut = '';
1132  $TSelected = array();
1133  if (!empty($this->fieldValue)) {
1134  $TSelected = explode(',', $this->fieldValue);
1135  }
1136 
1137  if (!empty($TSelected)) {
1138  foreach ($TSelected as $selected) {
1139  if (!empty($this->fieldOptions[$selected])) {
1140  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1141  }
1142  }
1143  }
1144  return $outPut;
1145  }
1146 
1150  public function generateOutputFieldColor()
1151  {
1152  $this->fieldAttr['disabled']=null;
1153  return $this->generateInputField();
1154  }
1158  public function generateInputFieldColor()
1159  {
1160  $this->fieldAttr['type']= 'color';
1161  return $this->generateInputFieldText();
1162  }
1163 
1167  public function generateOutputFieldSelect()
1168  {
1169  $outPut = '';
1170  if (!empty($this->fieldOptions[$this->fieldValue])) {
1171  $outPut = $this->fieldOptions[$this->fieldValue];
1172  }
1173 
1174  return $outPut;
1175  }
1176 
1177  /*
1178  * METHODS FOR SETTING DISPLAY TYPE
1179  */
1180 
1185  public function setAsString()
1186  {
1187  $this->type = 'string';
1188  return $this;
1189  }
1190 
1195  public function setAsColor()
1196  {
1197  $this->type = 'color';
1198  return $this;
1199  }
1200 
1205  public function setAsTextarea()
1206  {
1207  $this->type = 'textarea';
1208  return $this;
1209  }
1210 
1215  public function setAsHtml()
1216  {
1217  $this->type = 'html';
1218  return $this;
1219  }
1220 
1226  public function setAsEmailTemplate($templateType)
1227  {
1228  $this->type = 'emailtemplate:'.$templateType;
1229  return $this;
1230  }
1231 
1236  public function setAsThirdpartyType()
1237  {
1238  $this->type = 'thirdparty_type';
1239  return $this;
1240  }
1241 
1246  public function setAsYesNo()
1247  {
1248  $this->type = 'yesno';
1249  return $this;
1250  }
1251 
1256  public function setAsSecureKey()
1257  {
1258  $this->type = 'securekey';
1259  return $this;
1260  }
1261 
1266  public function setAsProduct()
1267  {
1268  $this->type = 'product';
1269  return $this;
1270  }
1271 
1278  public function setAsCategory($catType)
1279  {
1280  $this->type = 'category:'.$catType;
1281  return $this;
1282  }
1283 
1289  public function setAsTitle()
1290  {
1291  $this->type = 'title';
1292  return $this;
1293  }
1294 
1295 
1302  public function setAsMultiSelect($fieldOptions)
1303  {
1304  if (is_array($fieldOptions)) {
1305  $this->fieldOptions = $fieldOptions;
1306  }
1307 
1308  $this->type = 'multiselect';
1309  return $this;
1310  }
1311 
1318  public function setAsSelect($fieldOptions)
1319  {
1320  if (is_array($fieldOptions)) {
1321  $this->fieldOptions = $fieldOptions;
1322  }
1323 
1324  $this->type = 'select';
1325  return $this;
1326  }
1327 }
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
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:601
Class to manage categories.
Class to manage a WYSIWYG editor.
Class to build HTML component for third parties management Only common components are here.
Class to manage generation of HTML components Only common components must be here.
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Classe permettant la generation de composants html autre Only common components are here.
This class help you create setup render.
sortingItems()
Sort items according to rank.
saveConfFromPost($noMessageInUpdate=false)
saveConfFromPost
itemSort(FormSetupItem $a, FormSetupItem $b)
uasort callback function to Sort params items
setItemMaxRank($rank)
set new max rank if needed
exportItemsAsParamsArray()
Used to export param array for /core/actions_setmoduleoptions.inc.php template Method exists only for...
getLineRank($itemKey)
get item position rank from item key
addItemsFromParamsArray($params)
Method used to test module builder convertion to this form usage.
addItemFromParams($confKey, $params)
From old Method was used to test module builder convertion to this form usage.
generateOutput($editMode=false)
generateOutput
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...
static generateAttributesStringFromArray($attributes)
Generate an attributes string form an input array.
__construct($db, $outputLangs=false)
Constructor.
reloadConfs()
Reload for each item default conf note: this will override custom configuration.
generateLineOutput($item, $editMode=false)
generateLineOutput
getCurentItemMaxRank($cache=true)
getCurentItemMaxRank
generateTableOutput($editMode=false)
generateTableOutput
This class help to create item for class formSetup.
reloadValueFromConf()
reload conf value from databases is an aliase of loadValueFromConf
setSaveCallBack(callable $callBack)
Set an override function for saving data.
generateInputFieldTextarea()
generate input field for textarea
setAsString()
Set type of input as string.
setValueFromPostCallBack(callable $callBack)
Set an override function for get data from post.
setAsSecureKey()
Set type of input as secure key.
saveConfValue()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
loadValueFromConf()
load conf value from databases
setAsHtml()
Set type of input as html editor.
generateInputField()
generate input field
setAsColor()
Set type of input as color.
setErrors($errors)
Add error.
getType()
get the type : used for old module builder setup conf style conversion and tests because this two cla...
setAsTitle()
Set type of input as a simple title no data to store.
generateInputFieldText()
generatec default input field
setAsCategory($catType)
Set type of input as a category selector TODO add default value.
setAsSelect($fieldOptions)
Set type of input as a simple title no data to store.
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
setAsMultiSelect($fieldOptions)
Set type of input as a simple title no data to store.
generateInputFieldCategories()
generate input field for categories
setAsProduct()
Set type of input as product.
setAsThirdpartyType()
Set type of input as thirdparty_type selector.
generateInputFieldEmailTemplate()
generate input field for email template selector
setTypeFromTypeString($type)
set the type from string : used for old module builder setup conf style conversion and tests because ...
generateInputFieldHtml()
generate input field for html
__construct($confKey)
Constructor.
getNameText()
Get field name text or generate it.
setAsYesNo()
Set type of input as Yes.
generateInputFieldSecureKey()
generate input field for secure key
setAsEmailTemplate($templateType)
Set type of input as emailtemplate selector.
getHelpText()
Get help text or generate it.
setAsTextarea()
Set type of input as textarea.
Class to manage products or services.
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.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
dolGetBadge($label, $html='', $type='primary', $mode='', $url='', $params=array())
Function dolGetBadge.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token')
Ouput javacript to autoset a generated password using default module into a HTML element.
$conf db
API class for accounts.
Definition: inc.php:41