dolibarr  18.0.6
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 $entity;
31 
33  public $items = array();
34 
38  public $setupNotEmpty = 0;
39 
41  public $langs;
42 
44  public $form;
45 
47  protected $maxItemRank;
48 
53  public $htmlBeforeOutputForm = '';
54 
59  public $htmlAfterOutputForm = '';
60 
65  public $htmlOutputMoreButton = '';
66 
67 
72  public $formAttributes = array(
73  'action' => '', // set in __construct
74  'method' => 'POST'
75  );
76 
81  public $formHiddenInputs = array();
82 
83 
90  public function __construct($db, $outputLangs = false)
91  {
92  global $conf, $langs;
93 
94  $this->db = $db;
95  $this->form = new Form($this->db);
96  $this->formAttributes['action'] = $_SERVER["PHP_SELF"];
97 
98  $this->formHiddenInputs['token'] = newToken();
99  $this->formHiddenInputs['action'] = 'update';
100 
101  $this->entity = (is_null($this->entity) ? $conf->entity : $this->entity);
102 
103  if ($outputLangs) {
104  $this->langs = $outputLangs;
105  } else {
106  $this->langs = $langs;
107  }
108  }
109 
116  static public function generateAttributesStringFromArray($attributes)
117  {
118  $Aattr = array();
119  if (is_array($attributes)) {
120  foreach ($attributes as $attribute => $value) {
121  if (is_array($value) || is_object($value)) {
122  continue;
123  }
124  $Aattr[] = $attribute.'="'.dol_escape_htmltag($value).'"';
125  }
126  }
127 
128  return !empty($Aattr)?implode(' ', $Aattr):'';
129  }
130 
131 
138  public function generateOutput($editMode = false)
139  {
140  global $hookmanager, $action, $langs;
141  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
142 
143  $parameters = array(
144  'editMode' => $editMode
145  );
146  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
147  if ($reshook < 0) {
148  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
149  }
150 
151  if ($reshook > 0) {
152  return $hookmanager->resPrint;
153  } else {
154  $out = '<!-- Start generateOutput from FormSetup class -->';
155  $out.= $this->htmlBeforeOutputForm;
156 
157  if ($editMode) {
158  $out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
159 
160  // generate hidden values from $this->formHiddenInputs
161  if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
162  foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
163  $out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
164  }
165  }
166  }
167 
168  // generate output table
169  $out .= $this->generateTableOutput($editMode);
170 
171 
172  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
173  if ($reshook < 0) {
174  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
175  }
176 
177  if ($reshook > 0) {
178  return $hookmanager->resPrint;
179  } elseif ($editMode) {
180  $out .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
181  $out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
182  $out.= $this->htmlOutputMoreButton;
183  $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
184  $out .= ' &nbsp;&nbsp; ';
185  $out .= '<a class="button button-cancel" type="submit" href="' . $this->formAttributes['action'] . '">'.$langs->trans('Cancel').'</a>';
186  $out .= '</div>';
187  }
188 
189  if ($editMode) {
190  $out .= '</form>';
191  }
192 
193  $out.= $this->htmlAfterOutputForm;
194 
195  return $out;
196  }
197  }
198 
205  public function generateTableOutput($editMode = false)
206  {
207  global $hookmanager, $action;
208  require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
209 
210  $parameters = array(
211  'editMode' => $editMode
212  );
213  $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
214  if ($reshook < 0) {
215  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
216  }
217 
218  if ($reshook > 0) {
219  return $hookmanager->resPrint;
220  } else {
221  $out = '<table class="noborder centpercent">';
222  $out .= '<thead>';
223  $out .= '<tr class="liste_titre">';
224  $out .= ' <td>' . $this->langs->trans("Parameter") . '</td>';
225  $out .= ' <td>' . $this->langs->trans("Value") . '</td>';
226  $out .= '</tr>';
227  $out .= '</thead>';
228 
229  // Sort items before render
230  $this->sortingItems();
231 
232  $out .= '<tbody>';
233  foreach ($this->items as $item) {
234  $out .= $this->generateLineOutput($item, $editMode);
235  }
236  $out .= '</tbody>';
237 
238  $out .= '</table>';
239  return $out;
240  }
241  }
242 
249  public function saveConfFromPost($noMessageInUpdate = false)
250  {
251  global $hookmanager, $conf;
252 
253  $parameters = array();
254  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfFromPost', $parameters, $this); // Note that $action and $object may have been modified by some hooks
255  if ($reshook < 0) {
256  $this->setErrors($hookmanager->errors);
257  return -1;
258  }
259 
260  if ($reshook > 0) {
261  return $reshook;
262  }
263 
264 
265  if (empty($this->items)) {
266  return null;
267  }
268 
269  $this->db->begin();
270  $error = 0;
271  foreach ($this->items as $item) {
272  if ($item->getType() == 'yesno' && !empty($conf->use_javascript_ajax)) {
273  continue;
274  }
275 
276  $res = $item->setValueFromPost();
277  if ($res > 0) {
278  $item->saveConfValue();
279  } elseif ($res < 0) {
280  $error++;
281  break;
282  }
283  }
284 
285  if (!$error) {
286  $this->db->commit();
287  if (empty($noMessageInUpdate)) {
288  setEventMessages($this->langs->trans("SetupSaved"), null);
289  }
290  return 1;
291  } else {
292  $this->db->rollback();
293  if (empty($noMessageInUpdate)) {
294  setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors');
295  }
296  return -1;
297  }
298  }
299 
307  public function generateLineOutput($item, $editMode = false)
308  {
309 
310  $out = '';
311  if ($item->enabled==1) {
312  $trClass = 'oddeven';
313  if ($item->getType() == 'title') {
314  $trClass = 'liste_titre';
315  }
316 
317  $this->setupNotEmpty++;
318  $out.= '<tr class="'.$trClass.'">';
319 
320  $out.= '<td class="col-setup-title">';
321  $out.= '<span id="helplink'.$item->confKey.'" class="spanforparamtooltip">';
322  $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey);
323  $out.= '</span>';
324  $out.= '</td>';
325 
326  $out.= '<td>';
327 
328  if ($editMode) {
329  $out.= $item->generateInputField();
330  } else {
331  $out.= $item->generateOutputField();
332  }
333 
334  if (!empty($item->errors)) {
335  // TODO : move set event message in a methode to be called by cards not by this class
336  setEventMessages(null, $item->errors, 'errors');
337  }
338 
339  $out.= '</td>';
340  $out.= '</tr>';
341  }
342 
343  return $out;
344  }
345 
346 
353  public function addItemsFromParamsArray($params)
354  {
355  if (!is_array($params) || empty($params)) { return false; }
356  foreach ($params as $confKey => $param) {
357  $this->addItemFromParams($confKey, $param); // todo manage error
358  }
359  return true;
360  }
361 
362 
371  public function addItemFromParams($confKey, $params)
372  {
373  if (empty($confKey) || empty($params['type'])) { return false; }
374 
375  /*
376  * Exemple from old module builder setup page
377  * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1),
378  // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1),
379  //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1),
380  //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1),
381  //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1),
382  //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1),
383  //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1),
384  //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1),
385  */
386 
387  $item = new FormSetupItem($confKey);
388  // need to be ignored from scrutinizer setTypeFromTypeString was created as deprecated to incite developper to use object oriented usage $item->setTypeFromTypeString($params['type']);
390 
391  if (!empty($params['enabled'])) {
392  $item->enabled = $params['enabled'];
393  }
394 
395  if (!empty($params['css'])) {
396  $item->cssClass = $params['css'];
397  }
398 
399  $this->items[$item->confKey] = $item;
400 
401  return true;
402  }
403 
410  public function exportItemsAsParamsArray()
411  {
412  $arrayofparameters = array();
413  foreach ($this->items as $item) {
414  $arrayofparameters[$item->confKey] = array(
415  'type' => $item->getType(),
416  'enabled' => $item->enabled
417  );
418  }
419 
420  return $arrayofparameters;
421  }
422 
429  public function reloadConfs()
430  {
431 
432  if (!array($this->items)) { return false; }
433  foreach ($this->items as $item) {
434  $item->loadValueFromConf();
435  }
436 
437  return true;
438  }
439 
440 
450  public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false)
451  {
452  $item = new FormSetupItem($confKey);
453 
454  $item->entity = $this->entity;
455 
456  // set item rank if not defined as last item
457  if (empty($item->rank)) {
458  $item->rank = $this->getCurentItemMaxRank() + 1;
459  $this->setItemMaxRank($item->rank); // set new max rank if needed
460  }
461 
462  // try to get rank from target column, this will override item->rank
463  if (!empty($targetItemKey)) {
464  if (isset($this->items[$targetItemKey])) {
465  $targetItem = $this->items[$targetItemKey];
466  $item->rank = $targetItem->rank; // $targetItem->rank will be increase after
467  if ($targetItem->rank >= 0 && $insertAfterTarget) {
468  $item->rank++;
469  }
470  }
471 
472  // calc new rank for each item to make place for new item
473  foreach ($this->items as $fItem) {
474  if ($item->rank <= $fItem->rank) {
475  $fItem->rank = $fItem->rank + 1;
476  $this->setItemMaxRank($fItem->rank); // set new max rank if needed
477  }
478  }
479  }
480 
481  $this->items[$item->confKey] = $item;
482  return $this->items[$item->confKey];
483  }
484 
490  public function sortingItems()
491  {
492  // Sorting
493  return uasort($this->items, array($this, 'itemSort'));
494  }
495 
502  public function getCurentItemMaxRank($cache = true)
503  {
504  if (empty($this->items)) {
505  return 0;
506  }
507 
508  if ($cache && $this->maxItemRank > 0) {
509  return $this->maxItemRank;
510  }
511 
512  $this->maxItemRank = 0;
513  foreach ($this->items as $item) {
514  $this->maxItemRank = max($this->maxItemRank, $item->rank);
515  }
516 
517  return $this->maxItemRank;
518  }
519 
520 
527  public function setItemMaxRank($rank)
528  {
529  $this->maxItemRank = max($this->maxItemRank, $rank);
530  }
531 
532 
539  public function getLineRank($itemKey)
540  {
541  if (!isset($this->items[$itemKey]->rank)) {
542  return -1;
543  }
544  return $this->items[$itemKey]->rank;
545  }
546 
547 
555  public function itemSort(FormSetupItem $a, FormSetupItem $b)
556  {
557  if (empty($a->rank)) {
558  $a->rank = 0;
559  }
560  if (empty($b->rank)) {
561  $b->rank = 0;
562  }
563  if ($a->rank == $b->rank) {
564  return 0;
565  }
566  return ($a->rank < $b->rank) ? -1 : 1;
567  }
568 }
569 
574 {
578  public $db;
579 
581  public $langs;
582 
584  public $entity;
585 
587  public $form;
588 
590  public $confKey;
591 
593  public $nameText = false;
594 
596  public $helpText = '';
597 
599  public $fieldValue;
600 
602  public $defaultFieldValue = null;
603 
605  public $fieldAttr = array();
606 
608  public $fieldOverride = false;
609 
611  public $fieldInputOverride = false;
612 
614  public $fieldOutputOverride = false;
615 
617  public $rank = 0;
618 
620  public $fieldOptions = array();
621 
623  public $saveCallBack;
624 
626  public $setValueFromPostCallBack;
627 
631  public $errors = array();
632 
639  protected $type = 'string';
640 
641  public $enabled = 1;
642 
643  public $cssClass = '';
644 
650  public function __construct($confKey)
651  {
652  global $langs, $db, $conf, $form;
653  $this->db = $db;
654 
655  if (!empty($form) && is_object($form) && get_class($form) == 'Form') { // the form class has a cache inside so I am using it to optimize
656  $this->form = $form;
657  } else {
658  $this->form = new Form($this->db);
659  }
660 
661  $this->langs = $langs;
662  $this->entity = (is_null($this->entity) ? $conf->entity : ((int) $this->entity));
663 
664  $this->confKey = $confKey;
665  $this->loadValueFromConf();
666  }
667 
672  public function loadValueFromConf()
673  {
674  global $conf;
675  if (isset($conf->global->{$this->confKey})) {
676  $this->fieldValue = getDolGlobalString($this->confKey);
677  return true;
678  } else {
679  $this->fieldValue = null;
680  return false;
681  }
682  }
683 
689  public function reloadValueFromConf()
690  {
691  return $this->loadValueFromConf();
692  }
693 
694 
700  public function saveConfValue()
701  {
702  global $hookmanager;
703 
704  $parameters = array();
705  $reshook = $hookmanager->executeHooks('formSetupBeforeSaveConfValue', $parameters, $this); // Note that $action and $object may have been modified by some hooks
706  if ($reshook < 0) {
707  $this->setErrors($hookmanager->errors);
708  return -1;
709  }
710 
711  if ($reshook > 0) {
712  return $reshook;
713  }
714 
715 
716  if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) {
717  return call_user_func($this->saveCallBack, $this);
718  }
719 
720  // Modify constant only if key was posted (avoid resetting key to the null value)
721  if ($this->type != 'title') {
722  $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity);
723  if ($result < 0) {
724  return -1;
725  } else {
726  return 1;
727  }
728  }
729 
730  return 0;
731  }
732 
739  public function setSaveCallBack(callable $callBack)
740  {
741  $this->saveCallBack = $callBack;
742  }
743 
749  public function setValueFromPostCallBack(callable $callBack)
750  {
751  $this->setValueFromPostCallBack = $callBack;
752  }
753 
758  public function setValueFromPost()
759  {
760  if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) {
761  return call_user_func($this->setValueFromPostCallBack);
762  }
763 
764  // Modify constant only if key was posted (avoid resetting key to the null value)
765  if ($this->type != 'title') {
766  if (preg_match('/category:/', $this->type)) {
767  if (GETPOST($this->confKey, 'int') == '-1') {
768  $val_const = '';
769  } else {
770  $val_const = GETPOST($this->confKey, 'int');
771  }
772  } elseif ($this->type == 'multiselect') {
773  $val = GETPOST($this->confKey, 'array');
774  if ($val && is_array($val)) {
775  $val_const = implode(',', $val);
776  } else {
777  $val_const = '';
778  }
779  } elseif ($this->type == 'html') {
780  $val_const = GETPOST($this->confKey, 'restricthtml');
781  } else {
782  $val_const = GETPOST($this->confKey, 'alpha');
783  }
784 
785  // TODO add value check with class validate
786  $this->fieldValue = $val_const;
787 
788  return 1;
789  }
790 
791  return 0;
792  }
793 
798  public function getHelpText()
799  {
800  if (!empty($this->helpText)) { return $this->helpText; }
801  return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : '');
802  }
803 
808  public function getNameText()
809  {
810  if (!empty($this->nameText)) { return $this->nameText; }
811  return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey));
812  }
813 
818  public function generateInputField()
819  {
820  global $conf;
821 
822  if (!empty($this->fieldOverride)) {
823  return $this->fieldOverride;
824  }
825 
826  if (!empty($this->fieldInputOverride)) {
827  return $this->fieldInputOverride;
828  }
829 
830  // Set default value
831  if (is_null($this->fieldValue)) {
832  $this->fieldValue = $this->defaultFieldValue;
833  }
834 
835 
836  $this->fieldAttr['name'] = $this->confKey;
837  $this->fieldAttr['id'] = 'setup-'.$this->confKey;
838  $this->fieldAttr['value'] = $this->fieldValue;
839 
840  $out = '';
841 
842  if ($this->type == 'title') {
843  $out.= $this->generateOutputField(); // title have no input
844  } elseif ($this->type == 'multiselect') {
845  $out.= $this->generateInputFieldMultiSelect();
846  } elseif ($this->type == 'select') {
847  $out.= $this->generateInputFieldSelect();
848  } elseif ($this->type == 'selectUser') {
849  $out.= $this->generateInputFieldSelectUser();
850  } elseif ($this->type == 'textarea') {
851  $out.= $this->generateInputFieldTextarea();
852  } elseif ($this->type== 'html') {
853  $out.= $this->generateInputFieldHtml();
854  } elseif ($this->type== 'color') {
855  $out.= $this->generateInputFieldColor();
856  } elseif ($this->type == 'yesno') {
857  if (!empty($conf->use_javascript_ajax)) {
858  $out.= ajax_constantonoff($this->confKey);
859  } else {
860  $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
861  }
862  } elseif (preg_match('/emailtemplate:/', $this->type)) {
863  $out.= $this->generateInputFieldEmailTemplate();
864  } elseif (preg_match('/category:/', $this->type)) {
865  $out.=$this->generateInputFieldCategories();
866  } elseif (preg_match('/thirdparty_type/', $this->type)) {
867  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
868  $formcompany = new FormCompany($this->db);
869  $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey);
870  } elseif ($this->type == 'securekey') {
871  $out.= $this->generateInputFieldSecureKey();
872  } elseif ($this->type == 'product') {
873  if (isModEnabled("product") || isModEnabled("service")) {
874  $selected = (empty($this->fieldValue) ? '' : $this->fieldValue);
875  $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
876  }
877  } else {
878  $out.= $this->generateInputFieldText();
879  }
880 
881  return $out;
882  }
883 
888  public function generateInputFieldText()
889  {
890  if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
891  return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
892  }
893 
898  public function generateInputFieldTextarea()
899  {
900  $out = '<textarea class="flat" name="'.$this->confKey.'" id="'.$this->confKey.'" cols="50" rows="5" wrap="soft">' . "\n";
901  $out.= dol_htmlentities($this->fieldValue);
902  $out.= "</textarea>\n";
903  return $out;
904  }
905 
910  public function generateInputFieldHtml()
911  {
912  global $conf;
913  require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
914  $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, isModEnabled('fckeditor'), ROWS_5, '90%');
915  return $doleditor->Create(1);
916  }
917 
923  {
924  global $conf;
925  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
926  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
927  $formother = new FormOther($this->db);
928 
929  $tmp = explode(':', $this->type);
930  $out= img_picto('', 'category', 'class="pictofixedwidth"');
931  $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort'));
932  return $out;
933  }
934 
940  {
941  global $conf, $user;
942  $out = '';
943  if (preg_match('/emailtemplate:/', $this->type)) {
944  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
945  $formmail = new FormMail($this->db);
946 
947  $tmp = explode(':', $this->type);
948  $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang
949  $arrayOfMessageName = array();
950  if (is_array($formmail->lines_model)) {
951  foreach ($formmail->lines_model as $modelMail) {
952  $moreonlabel = '';
953  if (!empty($arrayOfMessageName[$modelMail->label])) {
954  $moreonlabel = ' <span class="opacitymedium">(' . $this->langs->trans("SeveralLangugeVariatFound") . ')</span>';
955  }
956  // The 'label' is the key that is unique if we exclude the language
957  $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\‍(|\‍)/', '', $modelMail->label)) . $moreonlabel;
958  }
959  }
960  $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1);
961  }
962 
963  return $out;
964  }
965 
966 
971  public function generateInputFieldSecureKey()
972  {
973  global $conf;
974  $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">';
975  if (!empty($conf->use_javascript_ajax)) {
976  $out.= '&nbsp;'.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"');
977  }
978 
979  // Add button to autosuggest a key
980  include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
981  $out .= dolJSToSetRandomPassword($this->confKey, 'generate_token'.$this->confKey);
982 
983  return $out;
984  }
985 
986 
991  {
992  $TSelected = array();
993  if ($this->fieldValue) {
994  $TSelected = explode(',', $this->fieldValue);
995  }
996 
997  return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"');
998  }
999 
1000 
1004  public function generateInputFieldSelect()
1005  {
1006  return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue);
1007  }
1008 
1013  {
1014  return $this->form->select_dolusers($this->fieldValue, $this->confKey);
1015  }
1016 
1024  public function getType()
1025  {
1026  return $this->type;
1027  }
1028 
1038  public function setTypeFromTypeString($type)
1039  {
1040  $this->type = $type;
1041 
1042  return true;
1043  }
1044 
1051  public function setErrors($errors)
1052  {
1053  if (is_array($errors)) {
1054  if (!empty($errors)) {
1055  foreach ($errors as $error) {
1056  $this->setErrors($error);
1057  }
1058  }
1059  } elseif (!empty($errors)) {
1060  $this->errors[] = $errors;
1061  }
1062  }
1063 
1069  public function generateOutputField()
1070  {
1071  global $conf, $user, $langs;
1072 
1073  if (!empty($this->fieldOverride)) {
1074  return $this->fieldOverride;
1075  }
1076 
1077  if (!empty($this->fieldOutputOverride)) {
1078  return $this->fieldOutputOverride;
1079  }
1080 
1081  $out = '';
1082 
1083  if ($this->type == 'title') {
1084  // nothing to do
1085  } elseif ($this->type == 'textarea') {
1086  $out.= dol_nl2br($this->fieldValue);
1087  } elseif ($this->type == 'multiselect') {
1088  $out.= $this->generateOutputFieldMultiSelect();
1089  } elseif ($this->type == 'select') {
1090  $out.= $this->generateOutputFieldSelect();
1091  } elseif ($this->type == 'selectUser') {
1092  $out.= $this->generateOutputFieldSelectUser();
1093  } elseif ($this->type== 'html') {
1094  $out.= $this->fieldValue;
1095  } elseif ($this->type== 'color') {
1096  $out.= $this->generateOutputFieldColor();
1097  } elseif ($this->type == 'yesno') {
1098  if (!empty($conf->use_javascript_ajax)) {
1099  $out.= ajax_constantonoff($this->confKey, array(), $this->entity); // TODO possibility to add $input parameter
1100  } else {
1101  if ($this->fieldValue == 1) {
1102  $out.= $langs->trans('yes');
1103  } else {
1104  $out.= $langs->trans('no');
1105  }
1106  }
1107  } elseif (preg_match('/emailtemplate:/', $this->type)) {
1108  if ($this->fieldValue > 0) {
1109  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
1110  $formmail = new FormMail($this->db);
1111 
1112  $tmp = explode(':', $this->type);
1113 
1114  $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue);
1115  if (is_numeric($template) && $template < 0) {
1116  $this->setErrors($formmail->errors);
1117  }
1118  $out.= $this->langs->trans($template->label);
1119  }
1120  } elseif (preg_match('/category:/', $this->type)) {
1121  require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
1122  $c = new Categorie($this->db);
1123  $result = $c->fetch($this->fieldValue);
1124  if ($result < 0) {
1125  $this->setErrors($c->errors);
1126  }
1127  $ways = $c->print_all_ways(' &gt;&gt; ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text
1128  $toprint = array();
1129  foreach ($ways as $way) {
1130  $toprint[] = '<li class="select2-search-choice-dolibarr noborderoncategories"' . ($c->color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '</li>';
1131  }
1132  $out.='<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode(' ', $toprint) . '</ul></div>';
1133  } elseif (preg_match('/thirdparty_type/', $this->type)) {
1134  if ($this->fieldValue==2) {
1135  $out.= $this->langs->trans("Prospect");
1136  } elseif ($this->fieldValue==3) {
1137  $out.= $this->langs->trans("ProspectCustomer");
1138  } elseif ($this->fieldValue==1) {
1139  $out.= $this->langs->trans("Customer");
1140  } elseif ($this->fieldValue==0) {
1141  $out.= $this->langs->trans("NorProspectNorCustomer");
1142  }
1143  } elseif ($this->type == 'product') {
1144  require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
1145 
1146  $product = new Product($this->db);
1147  $resprod = $product->fetch($this->fieldValue);
1148  if ($resprod > 0) {
1149  $out.= $product->ref;
1150  } elseif ($resprod < 0) {
1151  $this->setErrors($product->errors);
1152  }
1153  } else {
1154  $out.= $this->fieldValue;
1155  }
1156 
1157  return $out;
1158  }
1159 
1160 
1167  {
1168  $outPut = '';
1169  $TSelected = array();
1170  if (!empty($this->fieldValue)) {
1171  $TSelected = explode(',', $this->fieldValue);
1172  }
1173 
1174  if (!empty($TSelected)) {
1175  foreach ($TSelected as $selected) {
1176  if (!empty($this->fieldOptions[$selected])) {
1177  $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' ';
1178  }
1179  }
1180  }
1181  return $outPut;
1182  }
1183 
1189  public function generateOutputFieldColor()
1190  {
1191  $this->fieldAttr['disabled']=null;
1192  return $this->generateInputField();
1193  }
1199  public function generateInputFieldColor()
1200  {
1201  $this->fieldAttr['type']= 'color';
1202  return $this->generateInputFieldText();
1203  }
1204 
1210  public function generateOutputFieldSelect()
1211  {
1212  $outPut = '';
1213  if (!empty($this->fieldOptions[$this->fieldValue])) {
1214  $outPut = $this->fieldOptions[$this->fieldValue];
1215  }
1216 
1217  return $outPut;
1218  }
1219 
1226  {
1227  $outPut = '';
1228  $user = new User($this->db);
1229  $user->fetch($this->fieldValue);
1230  $outPut = $user->firstname . " " . $user->lastname;
1231  return $outPut;
1232  }
1233 
1234  /*
1235  * METHODS FOR SETTING DISPLAY TYPE
1236  */
1237 
1243  public function setAsString()
1244  {
1245  $this->type = 'string';
1246  return $this;
1247  }
1248 
1254  public function setAsColor()
1255  {
1256  $this->type = 'color';
1257  return $this;
1258  }
1259 
1265  public function setAsTextarea()
1266  {
1267  $this->type = 'textarea';
1268  return $this;
1269  }
1270 
1276  public function setAsHtml()
1277  {
1278  $this->type = 'html';
1279  return $this;
1280  }
1281 
1288  public function setAsEmailTemplate($templateType)
1289  {
1290  $this->type = 'emailtemplate:'.$templateType;
1291  return $this;
1292  }
1293 
1299  public function setAsThirdpartyType()
1300  {
1301  $this->type = 'thirdparty_type';
1302  return $this;
1303  }
1304 
1310  public function setAsYesNo()
1311  {
1312  $this->type = 'yesno';
1313  return $this;
1314  }
1315 
1321  public function setAsSecureKey()
1322  {
1323  $this->type = 'securekey';
1324  return $this;
1325  }
1326 
1332  public function setAsProduct()
1333  {
1334  $this->type = 'product';
1335  return $this;
1336  }
1337 
1345  public function setAsCategory($catType)
1346  {
1347  $this->type = 'category:'.$catType;
1348  return $this;
1349  }
1350 
1356  public function setAsTitle()
1357  {
1358  $this->type = 'title';
1359  return $this;
1360  }
1361 
1362 
1369  public function setAsMultiSelect($fieldOptions)
1370  {
1371  if (is_array($fieldOptions)) {
1372  $this->fieldOptions = $fieldOptions;
1373  }
1374 
1375  $this->type = 'multiselect';
1376  return $this;
1377  }
1378 
1385  public function setAsSelect($fieldOptions)
1386  {
1387  if (is_array($fieldOptions)) {
1388  $this->fieldOptions = $fieldOptions;
1389  }
1390 
1391  $this->type = 'select';
1392  return $this;
1393  }
1394 
1400  public function setAsSelectUser()
1401  {
1402  $this->type = 'selectUser';
1403  return $this;
1404  }
1405 }
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
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
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.
generateOutputFieldColor()
generateOutputFieldColor
generateOutputField()
generateOutputField
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
generateOutputFieldMultiSelect()
generateOutputFieldMultiSelect
setAsColor()
Set type of input as color.
generateOutputFieldSelectUser()
generateOutputFieldSelectUser
generateOutputFieldSelect()
generateOutputFieldSelect
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.
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.
setAsSelectUser()
Set type of input as a simple title.
setValueFromPost()
Save const value based on htdocs/core/actions_setmoduleoptions.inc.php.
setAsMultiSelect($fieldOptions)
Set type of input as a simple title.
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 ...
generateInputFieldColor()
generateInputFieldColor
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.
Class to manage Dolibarr users.
Definition: user.class.php:48
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:120
dolJSToSetRandomPassword($htmlname, $htmlnameofbutton='generate_token', $generic=1)
Ouput javacript to autoset a generated password using default module into a HTML element.