Merge pull request #20950 from atm-john/new_form_setup_color

Fix backport for 16 and ADD color, default value ,
This commit is contained in:
Laurent Destailleur 2022-05-21 17:27:14 +02:00 committed by GitHub
commit d5344cf2e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 90 additions and 12 deletions

View File

@ -422,7 +422,7 @@ class FormSetup
if (!array($this->items)) { return false; } if (!array($this->items)) { return false; }
foreach ($this->items as $item) { foreach ($this->items as $item) {
$item->reloadValueFromConf(); $item->loadValueFromConf();
} }
return true; return true;
@ -587,8 +587,11 @@ class FormSetupItem
/** @var string $fieldValue */ /** @var string $fieldValue */
public $fieldValue; public $fieldValue;
/** @var string $defaultFieldValue */
public $defaultFieldValue = null;
/** @var array $fieldAttr fields attribute only for compatible fields like input text */ /** @var array $fieldAttr fields attribute only for compatible fields like input text */
public $fieldAttr; public $fieldAttr = array();
/** @var bool|string set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too */ /** @var bool|string set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too */
public $fieldOverride = false; public $fieldOverride = false;
@ -648,17 +651,33 @@ class FormSetupItem
$this->entity = $conf->entity; $this->entity = $conf->entity;
$this->confKey = $confKey; $this->confKey = $confKey;
$this->fieldValue = $conf->global->{$this->confKey}; $this->loadValueFromConf();
} }
/** /**
* reload conf value from databases * load conf value from databases
* @return null * @return bool
*/
public function loadValueFromConf()
{
global $conf;
if (isset($conf->global->{$this->confKey})) {
$this->fieldValue = $conf->global->{$this->confKey};
return true;
} else {
$this->fieldValue = null;
return false;
}
}
/**
* reload conf value from databases is an aliase of loadValueFromConf
* @deprecated
* @return bool
*/ */
public function reloadValueFromConf() public function reloadValueFromConf()
{ {
global $conf; return $this->loadValueFromConf();
$this->fieldValue = $conf->global->{$this->confKey};
} }
@ -791,6 +810,12 @@ class FormSetupItem
return $this->fieldInputOverride; return $this->fieldInputOverride;
} }
// Set default value
if (is_null($this->fieldValue)) {
$this->fieldValue = $this->defaultFieldValue;
}
$this->fieldAttr['name'] = $this->confKey; $this->fieldAttr['name'] = $this->confKey;
$this->fieldAttr['id'] = 'setup-'.$this->confKey; $this->fieldAttr['id'] = 'setup-'.$this->confKey;
$this->fieldAttr['value'] = $this->fieldValue; $this->fieldAttr['value'] = $this->fieldValue;
@ -807,6 +832,8 @@ class FormSetupItem
$out.= $this->generateInputFieldTextarea(); $out.= $this->generateInputFieldTextarea();
} elseif ($this->type== 'html') { } elseif ($this->type== 'html') {
$out.= $this->generateInputFieldHtml(); $out.= $this->generateInputFieldHtml();
} elseif ($this->type== 'color') {
$out.= $this->generateInputFieldColor();
} elseif ($this->type == 'yesno') { } elseif ($this->type == 'yesno') {
$out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1); $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1);
} elseif (preg_match('/emailtemplate:/', $this->type)) { } elseif (preg_match('/emailtemplate:/', $this->type)) {
@ -825,14 +852,22 @@ class FormSetupItem
$out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1); $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1);
} }
} else { } else {
if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); } $out.= $this->generateInputFieldText();
$out.= '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
} }
return $out; return $out;
} }
/**
* generatec default input field
* @return string
*/
public function generateInputFieldText()
{
if (empty($this->fieldAttr)) { $this->fieldAttr['class'] = 'flat '.(empty($this->cssClass) ? 'minwidth200' : $this->cssClass); }
return '<input '.FormSetup::generateAttributesStringFromArray($this->fieldAttr).' />';
}
/** /**
* generate input field for textarea * generate input field for textarea
* @return string * @return string
@ -1029,6 +1064,8 @@ class FormSetupItem
$out.= $this->generateOutputFieldSelect(); $out.= $this->generateOutputFieldSelect();
} elseif ($this->type== 'html') { } elseif ($this->type== 'html') {
$out.= $this->fieldValue; $out.= $this->fieldValue;
} elseif ($this->type== 'color') {
$out.= $this->generateOutputFieldColor();
} elseif ($this->type == 'yesno') { } elseif ($this->type == 'yesno') {
$out.= ajax_constantonoff($this->confKey); $out.= ajax_constantonoff($this->confKey);
} elseif (preg_match('/emailtemplate:/', $this->type)) { } elseif (preg_match('/emailtemplate:/', $this->type)) {
@ -1102,6 +1139,22 @@ class FormSetupItem
return $outPut; return $outPut;
} }
/**
* @return string
*/
public function generateOutputFieldColor()
{
$this->fieldAttr['disabled']=null;
return $this->generateInputField();
}
/**
* @return string
*/
public function generateInputFieldColor()
{
$this->fieldAttr['type']= 'color';
return $this->generateInputFieldText();
}
/** /**
* @return string * @return string
@ -1130,6 +1183,16 @@ class FormSetupItem
return $this; return $this;
} }
/**
* Set type of input as color
* @return self
*/
public function setAsColor()
{
$this->type = 'color';
return $this;
}
/** /**
* Set type of input as textarea * Set type of input as textarea
* @return self * @return self

View File

@ -240,7 +240,7 @@ if ($dirins && $action == 'initmodule' && $modulename) {
// Copy last html.formsetup.class.php' to backport folder // Copy last html.formsetup.class.php' to backport folder
$tryToCopyFromSetupClass = true; $tryToCopyFromSetupClass = true;
$backportDest = $destdir .'/backport/v17/core/class'; $backportDest = $destdir .'/backport/v16/core/class';
$backportFileSrc = DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; $backportFileSrc = DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
$backportFileDest = $backportDest.'/html.formsetup.class.php'; $backportFileDest = $backportDest.'/html.formsetup.class.php';
$result = dol_mkdir($backportDest); $result = dol_mkdir($backportDest);

View File

@ -88,7 +88,7 @@ $useFormSetup = 1;
if (!class_exists('FormSetup')) { if (!class_exists('FormSetup')) {
// For retrocompatibility Dolibarr < 16.0 // For retrocompatibility Dolibarr < 16.0
if (floatval(DOL_VERSION) < 16.0 && !class_exists('FormSetup')) { if (floatval(DOL_VERSION) < 16.0 && !class_exists('FormSetup')) {
require_once __DIR__.'/../backport/v17/core/class/html.formsetup.class.php'; require_once __DIR__.'/../backport/v16/core/class/html.formsetup.class.php';
} else { } else {
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
} }
@ -104,6 +104,7 @@ $item->cssClass = 'minwidth500';
// Setup conf MYMODULE_MYPARAM1 as a simple string input // Setup conf MYMODULE_MYPARAM1 as a simple string input
$item = $formSetup->newItem('MYMODULE_MYPARAM1'); $item = $formSetup->newItem('MYMODULE_MYPARAM1');
$item->defaultFieldValue = 'default value';
// Setup conf MYMODULE_MYPARAM1 as a simple textarea input but we replace the text of field title // Setup conf MYMODULE_MYPARAM1 as a simple textarea input but we replace the text of field title
$item = $formSetup->newItem('MYMODULE_MYPARAM2'); $item = $formSetup->newItem('MYMODULE_MYPARAM2');
@ -145,6 +146,20 @@ $item->helpText = $langs->transnoentities('MYMODULE_MYPARAM8');
$formSetup->newItem('MYMODULE_MYPARAM9')->setAsSelect($TField); $formSetup->newItem('MYMODULE_MYPARAM9')->setAsSelect($TField);
// Setup conf MYMODULE_MYPARAM10
$item = $formSetup->newItem('MYMODULE_MYPARAM10');
$item->setAsColor();
$item->defaultFieldValue = '#FF0000';
$item->nameText = $item->getNameText().' more html text ';
$item->fieldInputOverride = '';
$item->helpText = $langs->transnoentities('AnHelpMessage');
//$item->fieldValue = '';
//$item->fieldAttr = array() ; // fields attribute only for compatible fields like input text
//$item->fieldOverride = false; // set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too
//$item->fieldInputOverride = false; // set this var to override field input
//$item->fieldOutputOverride = false; // set this var to override field output
$setupnotempty =+ count($formSetup->items); $setupnotempty =+ count($formSetup->items);