diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 6f2ec2acfb6..a8ed08587ed 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -44,6 +44,47 @@ class FormSetup /** @var int */ protected $maxItemRank; + /** + * this is an html string display before output form + * @var string + */ + public $htmlBeforeOutputForm = ''; + + /** + * this is an html string display after output form + * @var string + */ + public $htmlAfterOutputForm = ''; + + /** + * this is an html string display on buttons zone + * @var string + */ + public $htmlOutputMoreButton = ''; + + + /** + * + * @var array + */ + public $formAttributes = array( + 'action' => '', // set in __construct + 'method' => 'POST' + ); + + /** + * an list of hidden inputs used only in edit mode + * @var array + */ + public $formHiddenInputs = array(); + + + /** + * the value of action attribute of form + * @var string + */ + public $formAction; + /** * Constructor * @@ -55,6 +96,11 @@ class FormSetup global $langs; $this->db = $db; $this->form = new Form($this->db); + $this->formAttributes['action'] = $_SERVER["PHP_SELF"]; + + $this->formHiddenInputs['token'] = newToken(); + $this->formHiddenInputs['action'] = 'update'; + if ($outputLangs) { $this->langs = $outputLangs; @@ -63,6 +109,38 @@ class FormSetup } } + /** + * a quick method to sanitize html attributes + * @param string $var the string to sanitize + * @return string + */ + static public function sanitizeHtmlAttribute($var) + { + $var = preg_replace("/\r|\n/", "", $var); + return htmlspecialchars($var, ENT_QUOTES); + } + + /** + * Generae an attributes string form an input array + * @param array $attributes an array of attributes keys and values, + * @return string + */ + static public function generateAttributesStringFromArray($attributes) + { + $Aattr = array(); + if (is_array($attributes)) { + foreach ($attributes as $attribute => $value) { + if (is_array($value) || is_object($value)) { + continue; + } + $Aattr[] = $attribute.'="'.self::sanitizeHtmlAttribute($value).'"'; + } + } + + return !empty($Aattr)?implode(' ', $Aattr):''; + } + + /** * @param bool $editMode true will display output on edit mod * @return string @@ -83,12 +161,70 @@ class FormSetup if ($reshook > 0) { return $hookmanager->resPrint; } else { - $out = ''; + $out = ''; + $out.= $this->htmlBeforeOutputForm; + if ($editMode) { - $out .= ''; + $out.= '
formAttributes) . ' >'; + + // generate hidden values from $this->formHiddenInputs + if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) { + foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) { + $out.= ''; + } + } } - $out .= ''; + // generate output table + $out .= $this->generateTableOutput($editMode); + + + $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutputButton', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks + if ($reshook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); + } + + if ($reshook > 0) { + return $hookmanager->resPrint; + } elseif ($editMode) { + $out .= '
'; // Todo : remove this
by adding style to form-setup-button-container css class in all themes + $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
'; $out .= ''; $out .= ''; $out .= ' '; @@ -247,7 +383,7 @@ class FormSetup public function exportItemsAsParamsArray() { $arrayofparameters = array(); - foreach ($this->items as $key => $item) { + foreach ($this->items as $item) { $arrayofparameters[$item->confKey] = array( 'type' => $item->getType(), 'enabled' => $item->enabled diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 5690d18678b..6836585285c 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -88,6 +88,9 @@ $arrayofparameters = array( //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), ); +$error = 0; +$setupnotempty = 0; + // Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only $useFormSetup = 0; // Convert arrayofparameter into a formSetup object @@ -95,35 +98,44 @@ if (!empty($arrayofparameters) && $useFormSetup && (float) DOL_VERSION >= 15) { require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; $formSetup = new FormSetup($db); - foreach ($arrayofparameters as $key => $val) { - if ($val['enabled']) { - $item = $formSetup->newItem($key); + // you can use the param convertor + $formSetup->addItemsFromParamsArray($arrayofparameters); - if ($val['type'] == 'string') { - $item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; - $item->cssClass = $val['css']; - } - if ($val['type'] == 'thirdparty_type') { - $item->setAsThirdpartyType(); - } - if ($val['type'] == 'yesno') { - $formSetup->newItem($key)->setAsYesNo(); - } - if ($val['type'] == 'emailtemplate:thirdparty') { - $formSetup->newItem($key)->setAsEmailTemplate('thirdparty'); - } - if ($val['type'] == 'securekey') { - $formSetup->newItem($key)->setAsSecureKey()->enabled = 0; // disabled - } - if ($val['type'] == 'product') { - $formSetup->newItem($key)->setAsProduct(); - } - } - } + // or use the new system see exemple as follow (or use both because you can ;-) ) + + /* + // Hôte + $item = $formSetup->newItem('NO_PARAM_JUST_TEXT'); + $item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; + $item->cssClass = 'minwidth500'; + + // Setup conf MYMODULE_MYPARAM1 as a simple string input + $item = $formSetup->newItem('MYMODULE_MYPARAM1'); + + // Setup conf MYMODULE_MYPARAM1 as a simple textarea input but we replace the text of field title + $item = $formSetup->newItem('MYMODULE_MYPARAM2'); + $item->nameText = $item->getNameText().' more html text '; + + // Setup conf MYMODULE_MYPARAM3 + $item = $formSetup->newItem('MYMODULE_MYPARAM3'); + $item->setAsThirdpartyType(); + + // Setup conf MYMODULE_MYPARAM4 : exemple of quick define write style + $formSetup->newItem('MYMODULE_MYPARAM4')->setAsYesNo(); + + // Setup conf MYMODULE_MYPARAM5 + $formSetup->newItem('MYMODULE_MYPARAM5')->setAsEmailTemplate('thirdparty'); + + // Setup conf MYMODULE_MYPARAM6 + $formSetup->newItem('MYMODULE_MYPARAM6')->setAsSecureKey()->enabled = 0; // disabled + + // Setup conf MYMODULE_MYPARAM7 + $formSetup->newItem('MYMODULE_MYPARAM7')->setAsProduct(); + */ + + $setupnotempty = count($formSetup->items); } -$error = 0; -$setupnotempty = 0; $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); @@ -258,13 +270,13 @@ echo ''.$langs->trans("MyModuleSetupPage").'< if ($action == 'edit') { - print ''; - print ''; - print ''; - if ($useFormSetup && (float) DOL_VERSION >= 15) { print $formSetup->generateOutput(true); } else { + print ''; + print ''; + print ''; + print '
' . $this->langs->trans("Parameter") . '
'; print ''; @@ -350,18 +362,19 @@ if ($action == 'edit') { } } print '
'.$langs->trans("Parameter").''.$langs->trans("Value").'
'; - } - print '
'; - print ''; - print '
'; - print '
'; + print '
'; + print ''; + print '
'; + + print ''; + } + print '
'; } else { if ($useFormSetup && (float) DOL_VERSION >= 15) { if (!empty($formSetup->items)) { print $formSetup->generateOutput(); - $setupnotempty = count($formSetup->items); } } else { if (!empty($arrayofparameters)) {