Merge pull request #19508 from atm-john/fix_missing_formsetup_elements

Fix missing formsetup elements
This commit is contained in:
Laurent Destailleur 2021-12-16 18:42:55 +01:00 committed by GitHub
commit fdf7749a54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 173 additions and 41 deletions

View File

@ -44,6 +44,41 @@ 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();
/**
* Constructor
*
@ -55,6 +90,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 +103,27 @@ class FormSetup
}
}
/**
* Generate 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.'="'.dol_escape_htmltag($value).'"';
}
}
return !empty($Aattr)?implode(' ', $Aattr):'';
}
/**
* @param bool $editMode true will display output on edit mod
* @return string
@ -83,12 +144,70 @@ class FormSetup
if ($reshook > 0) {
return $hookmanager->resPrint;
} else {
$out = '<input type="hidden" name="token" value="' . newToken() . '">';
$out = '<!-- Start generateOutput from FormSetup class -->';
$out.= $this->htmlBeforeOutputForm;
if ($editMode) {
$out .= '<input type="hidden" name="action" value="update">';
$out.= '<form ' . self::generateAttributesStringFromArray($this->formAttributes) . ' >';
// generate hidden values from $this->formHiddenInputs
if (!empty($this->formHiddenInputs) && is_array($this->formHiddenInputs)) {
foreach ($this->formHiddenInputs as $hiddenKey => $hiddenValue) {
$out.= '<input type="hidden" name="'.dol_escape_htmltag($hiddenKey).'" value="' . dol_escape_htmltag($hiddenValue) . '">';
}
}
}
$out .= '<table class="noborder centpercent">';
// 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 .= '<br>'; // Todo : remove this <br/> by adding style to form-setup-button-container css class in all themes
$out .= '<div class="form-setup-button-container center">'; // Todo : remove .center by adding style to form-setup-button-container css class in all themes
$out.= $this->htmlOutputMoreButton;
$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
$out .= '</div>';
}
if ($editMode) {
$out .= '</form>';
}
$out.= $this->htmlAfterOutputForm;
return $out;
}
}
/**
* @param bool $editMode true will display output on edit mod
* @return string
*/
public function generateTableOutput($editMode = false)
{
global $hookmanager, $action;
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
$parameters = array(
'editMode' => $editMode
);
$reshook = $hookmanager->executeHooks('formSetupBeforeGenerateTableOutput', $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;
} else {
$out = '<table class="noborder centpercent">';
$out .= '<thead>';
$out .= '<tr class="liste_titre">';
$out .= ' <td class="titlefield">' . $this->langs->trans("Parameter") . '</td>';
@ -247,7 +366,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
@ -554,7 +673,7 @@ class FormSetupItem
*/
public function generateInputField()
{
global $conf, $user;
global $conf;
if (!empty($this->fieldOverride)) {
return $this->fieldOverride;

View File

@ -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 '<span class="opacitymedium">'.$langs->trans("MyModuleSetupPage").'</span><
if ($action == 'edit') {
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
if ($useFormSetup && (float) DOL_VERSION >= 15) {
print $formSetup->generateOutput(true);
} else {
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
print '<table class="noborder centpercent">';
print '<tr class="liste_titre"><td class="titlefield">'.$langs->trans("Parameter").'</td><td>'.$langs->trans("Value").'</td></tr>';
@ -350,18 +362,19 @@ if ($action == 'edit') {
}
}
print '</table>';
}
print '<br><div class="center">';
print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
print '</div>';
print '</form>';
print '<br><div class="center">';
print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
print '</div>';
print '</form>';
}
print '<br>';
} else {
if ($useFormSetup && (float) DOL_VERSION >= 15) {
if (!empty($formSetup->items)) {
print $formSetup->generateOutput();
$setupnotempty = count($formSetup->items);
}
} else {
if (!empty($arrayofparameters)) {