diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 82e4f0c96dd..b26438fd9e9 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -133,7 +133,7 @@ class FormSetup */ public function generateOutput($editMode = false) { - global $hookmanager, $action; + global $hookmanager, $action, $langs; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; $parameters = array( @@ -177,6 +177,8 @@ class FormSetup $out .= '
'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes $out.= $this->htmlOutputMoreButton; $out .= ''; // Todo fix dolibarr style for
'; } @@ -283,8 +285,13 @@ class FormSetup $out = ''; if ($item->enabled==1) { + $trClass = 'oddeven'; + if ($item->getType() == 'title') { + $trClass = 'liste_titre'; + } + $this->setupNotEmpty++; - $out.= ''; + $out.= ''; $out.= ''; $out.= ''; @@ -579,6 +586,15 @@ class FormSetupItem /** @var int $rank */ public $rank = 0; + /** @var array set this var for options on select and multiselect items */ + public $fieldOptions = array(); + + /** @var callable $saveCallBack */ + public $saveCallBack; + + /** @var callable $setValueFromPostCallBack */ + public $setValueFromPostCallBack; + /** * @var string $errors */ @@ -636,6 +652,10 @@ class FormSetupItem */ public function saveConfValue() { + if (!empty($this->saveCallBack) && is_callable($this->saveCallBack)) { + return call_user_func($this->saveCallBack); + } + // Modify constant only if key was posted (avoid resetting key to the null value) if ($this->type != 'title') { $result = dolibarr_set_const($this->db, $this->confKey, $this->fieldValue, 'chaine', 0, '', $this->entity); @@ -647,6 +667,25 @@ class FormSetupItem } } + /** + * Set an override function for saving data + * @param callable $callBack a callable function + * @return void + */ + public function setSaveCallBack(callable $callBack) + { + $this->saveCallBack = $callBack; + } + + /** + * Set an override function for get data from post + * @param callable $callBack a callable function + * @return void + */ + public function setValueFromPostCallBack(callable $callBack) + { + $this->setValueFromPostCallBack = $callBack; + } /** * Save const value based on htdocs/core/actions_setmoduleoptions.inc.php @@ -654,6 +693,10 @@ class FormSetupItem */ public function setValueFromPost() { + if (!empty($this->setValueFromPostCallBack) && is_callable($this->setValueFromPostCallBack)) { + return call_user_func($this->setValueFromPostCallBack); + } + // Modify constant only if key was posted (avoid resetting key to the null value) if ($this->type != 'title') { if (preg_match('/category:/', $this->type)) { @@ -662,6 +705,13 @@ class FormSetupItem } else { $val_const = GETPOST($this->confKey, 'int'); } + } elseif ($this->type == 'multiselect') { + $val = GETPOST($this->confKey, 'array'); + if ($val && is_array($val)) { + $val_const = implode(',', $val); + } + } elseif ($this->type == 'html') { + $val_const = GETPOST($this->confKey, 'restricthtml'); } else { $val_const = GETPOST($this->confKey, 'alpha'); } @@ -719,6 +769,10 @@ class FormSetupItem if ($this->type == 'title') { $out.= $this->generateOutputField(); // title have no input + } elseif ($this->type == 'multiselect') { + $out.= $this->generateInputFieldMultiSelect(); + } elseif ($this->type == 'select') { + $out.= $this->generateInputFieldSelect(); } elseif ($this->type == 'textarea') { $out.= $this->generateInputFieldTextarea(); } elseif ($this->type== 'html') { @@ -851,6 +905,29 @@ class FormSetupItem return $out; } + + /** + * @return string + */ + public function generateInputFieldMultiSelect() + { + $TSelected = array(); + if ($this->fieldValue) { + $TSelected = explode(',', $this->fieldValue); + } + + return $this->form->multiselectarray($this->confKey, $this->fieldOptions, $TSelected, 0, 0, '', 0, 0, 'style="min-width:100px"'); + } + + + /** + * @return string + */ + public function generateInputFieldSelect() + { + return $this->form->selectarray($this->confKey, $this->fieldOptions, $this->fieldValue); + } + /** * get the type : used for old module builder setup conf style conversion and tests * because this two class will quickly evolve it's important to not set or get directly $this->type (will be protected) so this method exist @@ -916,6 +993,10 @@ class FormSetupItem // nothing to do } elseif ($this->type == 'textarea') { $out.= dol_nl2br($this->fieldValue); + } elseif ($this->type == 'multiselect') { + $out.= $this->generateOutputFieldMultiSelect(); + } elseif ($this->type == 'select') { + $out.= $this->generateOutputFieldSelect(); } elseif ($this->type== 'html') { $out.= $this->fieldValue; } elseif ($this->type == 'yesno') { @@ -969,6 +1050,41 @@ class FormSetupItem } + /** + * @return string + */ + public function generateOutputFieldMultiSelect() + { + $outPut = ''; + $TSelected = array(); + if (!empty($this->fieldValue)) { + $TSelected = explode(',', $this->fieldValue); + } + + if (!empty($TSelected)) { + foreach ($TSelected as $selected) { + if (!empty($this->fieldOptions[$selected])) { + $outPut.= dolGetBadge('', $this->fieldOptions[$selected], 'info').' '; + } + } + } + return $outPut; + } + + + /** + * @return string + */ + public function generateOutputFieldSelect() + { + $outPut = ''; + if (!empty($this->fieldOptions[$this->fieldValue])) { + $outPut = $this->fieldOptions[$this->fieldValue]; + } + + return $outPut; + } + /* * METHODS FOR SETTING DISPLAY TYPE */ @@ -1076,4 +1192,37 @@ class FormSetupItem $this->type = 'title'; return $this; } + + + /** + * Set type of input as a simple title + * no data to store + * @param array $fieldOptions A table of field options + * @return self + */ + public function setAsMultiSelect($fieldOptions) + { + if (is_array($fieldOptions)) { + $this->fieldOptions = $fieldOptions; + } + + $this->type = 'multiselect'; + return $this; + } + + /** + * Set type of input as a simple title + * no data to store + * @param array $fieldOptions A table of field options + * @return self + */ + public function setAsSelect($fieldOptions) + { + if (is_array($fieldOptions)) { + $this->fieldOptions = $fieldOptions; + } + + $this->type = 'select'; + return $this; + } }