From d3d3a87ce9ca1ee8eb9328f87a367dd9c5625210 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sun, 24 Oct 2021 01:38:13 +0200 Subject: [PATCH 01/16] Add new class to help setup generation --- htdocs/core/class/html.formsetup.class.php | 403 ++++++++++++++++++ htdocs/modulebuilder/template/admin/setup.php | 157 +------ 2 files changed, 410 insertions(+), 150 deletions(-) create mode 100644 htdocs/core/class/html.formsetup.class.php diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php new file mode 100644 index 00000000000..095cf0d8887 --- /dev/null +++ b/htdocs/core/class/html.formsetup.class.php @@ -0,0 +1,403 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +/** + * This class help you create setup render + */ +class formSetup{ + + /** + * @var DoliDB Database handler. + */ + public $db; + + /** @var formSetupItem[] */ + public $arrayOfParameters = array(); + + public $setupNotEmpty = 0; + + /** @var Translate */ + public $langs; + + /** @var Form */ + public $form; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db, $outputLangs = false) + { + global $langs; + $this->db = $db; + $this->form = new Form($this->db); + + if($outputLangs){ + $this->langs = $outputLangs; + } + else{ + $this->langs = $langs; + } + } + + /** + * @return string + */ + public function generateOutput($edit = false){ + + require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; + + $out = ''; + $out.= ''; + $out.= ' '; + $out.= ' '; + $out.= ''; + + foreach ($this->arrayOfParameters as $const => $item) { + $out.= $this->generateLineOutput($item, $edit); + } + + $out.= '
'.$this->langs->trans("Parameter").''.$this->langs->trans("Value").'
'; + return $out; + } + + /** + * @param formSetupItem $item + * @param bool $edit + * @return string + */ + public function generateLineOutput($item, $edit = false){ + + $out = ''; + if ($item->enabled==1) { + $this->setupNotEmpty++; + $out.= ''; + + $out.= ''; + $out.= ''; + $out.= $this->form->textwithpicto($item->getNameText(), $item->getHelpText(), 1, 'info', '', 0, 3, 'tootips'.$item->confKey); + $out.= ''; + $out.= ''; + + $out.= ''; + + if($edit){ + $item->generateInputField(); + } + else{ + $item->generateOutputField(); + } + + if(!empty($item->errors)){ + // TODO : move set event message in a methode to be called by cards not by this class + setEventMessages(null, $item->errors, 'errors'); + } + + $out.= ''; + $out.= ''; + } + + return $out; + } + + + /** + * @param string $confKey + * @param array $params + * @ + */ + public function addItemsFromParamsArray($params){ + + if(!array($params)){ return false; } + + foreach ($params as $confKey => $param){ + $this->addItemFromParams($confKey, $param); // todo manage error + } + } + + + /** + * From old + * @param string $confKey + * @param array $params + */ + public function addItemFromParams($confKey, $params){ + + if(empty($confKey) || !empty($params['type'])){ return false; } + + /* + * Exemple from old module builder setup page + * // 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1), + // 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1), + //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1), + //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1), + //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1), + //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1), + //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1), + //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), + */ + + $item = new formSetupItem($this->db); + $item->type = $params['type']; + + if(!empty($params['enabled'])) { + $item->enabled = $params['enabled']; + } + + if(!empty($params['css'])){ + $item->enabled = $params['css']; + } + + $this->arrayOfParameters[$item->confKey] = $item; + + return true; + } + +} + + +class formSetupItem +{ + /** + * @var DoliDB Database handler. + */ + public $db; + + /** @var Translate */ + public $langs; + + /** @var Form */ + public $form; + + /** @var string $confKey the conf key used in database */ + public $confKey; + + /** @var string|false $nameText */ + public $nameText = false; + + /** @var string $helpText */ + public $helpText = ''; + + /** @var bool|string set this var to override field output */ + public $fieldOverride = false; + + /** + * @var string $errors + */ + public $errors = array(); + + /** + * @var string $type 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type' + */ + public $type; + + public $enabled = 0; + + public $cssClass = ''; + + /** + * Constructor + * + * @param $confKey + */ + public function __construct($confKey) + { + global $langs, $db; + $this->db = $db; + $this->form = new Form($this->db); + $this->langs = $langs; + + $this->confKey = $confKey; + } + + public function getHelpText(){ + if(!empty($this->helpText)){ return $this->helpText; } + return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : ''); + } + + public function getNameText(){ + if(!empty($this->nameText)){ return $this->nameText; } + return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : ''); + } + + public function generateInputField(){ + global $conf, $user; + + if(!empty($this->fieldOverride)){ + return $this->fieldOverride; + } + + $out = ''; + + if ($this->type == 'textarea') { + $out.= '\n"; + } elseif ($this->type== 'html') { + require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; + $doleditor = new DolEditor($this->confKey, $conf->global->{$this->confKey}, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); + $doleditor->Create(); + } elseif ($this->type == 'yesno') { + $out.= $this->form->selectyesno($this->confKey, $conf->global->{$this->confKey}, 1); + } elseif (preg_match('/emailtemplate:/', $this->type)) { + include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; + $formmail = new FormMail($this->db); + + $tmp = explode(':', $this->type); + $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang + //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, ''); + $arrayOfMessageName = array(); + if (is_array($formmail->lines_model)) { + foreach ($formmail->lines_model as $modelMail) { + //var_dump($modelmail); + $moreonlabel = ''; + if (!empty($arrayOfMessageName[$modelMail->label])) { + $moreonlabel = ' (' . $this->langs->trans("SeveralLangugeVariatFound") . ')'; + } + // The 'label' is the key that is unique if we exclude the language + $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel; + } + } + $out.= $this->form->selectarray($this->confKey, $arrayOfMessageName, $conf->global->{$this->confKey}, 'None', 0, 0, '', 0, 0, 0, '', '', 1); + } elseif (preg_match('/category:/', $this->type)) { + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; + $formother = new FormOther($this->db); + + $tmp = explode(':', $this->type); + $out.= img_picto('', 'category', 'class="pictofixedwidth"'); + $out.= $formother->select_categories($tmp[1], $conf->global->{$this->confKey}, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort')); + } elseif (preg_match('/thirdparty_type/', $this->type)) { + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; + $formcompany = new FormCompany($this->db); + $out.= $formcompany->selectProspectCustomerType($conf->global->{$this->confKey}, $this->confKey); + } elseif ($this->type == 'securekey') { + $out.= ''; + if (!empty($conf->use_javascript_ajax)) { + $out.= ' '.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"'); + } + if (!empty($conf->use_javascript_ajax)) { + $out.= "\n".''; + } + } elseif ($this->type == 'product') { + if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) { + $selected = (empty($conf->global->{$this->confKey}) ? '' : $conf->global->{$this->confKey}); + $this->form->select_produits($selected, $this->confKey, '', 0); + } + } else { + $out.= ''; + } + + return $out; + } + + + /** + * add error + * @param array|string $errors + */ + public function setErrors($errors){ + if(is_array($errors)){ + if(!empty($errors)){ + foreach ($errors as $error){ + $this->setErrors($error); + } + } + } + elseif(!empty($errors)){ + $this->errors[] = $errors; + } + } + + public function generateOutputField(){ + global $conf, $user; + + if(!empty($this->fieldOverride)){ + return $this->fieldOverride; + } + + $out = ''; + + if ($this->type == 'textarea') { + print dol_nl2br($conf->global->{$this->confKey}); + } elseif ($this->type== 'html') { + print $conf->global->{$this->confKey}; + } elseif ($this->type == 'yesno') { + print ajax_constantonoff($this->confKey); + } elseif (preg_match('/emailtemplate:/', $this->type)) { + include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; + $formmail = new FormMail($this->db); + + $tmp = explode(':', $this->type); + + $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $conf->global->{$this->confKey}); + if ($template<0) { + $this->setErrors($formmail->errors); + } + print $this->langs->trans($template->label); + } elseif (preg_match('/category:/', $this->type)) { + $c = new Categorie($this->db); + $result = $c->fetch($conf->global->{$this->confKey}); + if ($result < 0) { + $this->setErrors($c->errors); + } + $ways = $c->print_all_ways(' >> ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text + $toprint = array(); + foreach ($ways as $way) { + $toprint[] = '
  • color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '
  • '; + } + print '
      ' . implode(' ', $toprint) . '
    '; + } elseif (preg_match('/thirdparty_type/', $this->type)) { + if ($conf->global->{$this->confKey}==2) { + print $this->langs->trans("Prospect"); + } elseif ($conf->global->{$this->confKey}==3) { + print $this->langs->trans("ProspectCustomer"); + } elseif ($conf->global->{$this->confKey}==1) { + print $this->langs->trans("Customer"); + } elseif ($conf->global->{$this->confKey}==0) { + print $this->langs->trans("NorProspectNorCustomer"); + } + } elseif ($this->type == 'product') { + $product = new Product($this->db); + $resprod = $product->fetch($conf->global->{$this->confKey}); + if ($resprod > 0) { + print $product->ref; + } elseif ($resprod < 0) { + $this->setErrors($product->errors); + } + } else { + print $conf->global->{$this->confKey}; + } + + return $out; + } + +} diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 7c6c6b2c04e..390f45d955c 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -222,96 +222,17 @@ print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "mymo echo ''.$langs->trans("MyModuleSetupPage").'

    '; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; +$formSetup = new formSetup($db); + +$formSetup->addItemsFromParamsArray($arrayofparameters); + if ($action == 'edit') { print '
    '; print ''; print ''; - print ''; - print ''; - - foreach ($arrayofparameters as $constname => $val) { - if ($val['enabled']==1) { - $setupnotempty++; - print ''; - } - } - print '
    '.$langs->trans("Parameter").''.$langs->trans("Value").'
    '; - $tooltiphelp = (($langs->trans($constname . 'Tooltip') != $constname . 'Tooltip') ? $langs->trans($constname . 'Tooltip') : ''); - print ''.$form->textwithpicto($langs->trans($constname), $tooltiphelp, 1, 'info', '', 0, 3, 'tootips'.$constname).''; - print ''; - - if ($val['type'] == 'textarea') { - print '\n"; - } elseif ($val['type']== 'html') { - require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; - $doleditor = new DolEditor($constname, $conf->global->{$constname}, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); - $doleditor->Create(); - } elseif ($val['type'] == 'yesno') { - print $form->selectyesno($constname, $conf->global->{$constname}, 1); - } elseif (preg_match('/emailtemplate:/', $val['type'])) { - include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; - $formmail = new FormMail($db); - - $tmp = explode(':', $val['type']); - $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang - //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, ''); - $arrayofmessagename = array(); - if (is_array($formmail->lines_model)) { - foreach ($formmail->lines_model as $modelmail) { - //var_dump($modelmail); - $moreonlabel = ''; - if (!empty($arrayofmessagename[$modelmail->label])) { - $moreonlabel = ' (' . $langs->trans("SeveralLangugeVariatFound") . ')'; - } - // The 'label' is the key that is unique if we exclude the language - $arrayofmessagename[$modelmail->id] = $langs->trans(preg_replace('/\(|\)/', '', $modelmail->label)) . $moreonlabel; - } - } - print $form->selectarray($constname, $arrayofmessagename, $conf->global->{$constname}, 'None', 0, 0, '', 0, 0, 0, '', '', 1); - } elseif (preg_match('/category:/', $val['type'])) { - require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; - require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; - $formother = new FormOther($db); - - $tmp = explode(':', $val['type']); - print img_picto('', 'category', 'class="pictofixedwidth"'); - print $formother->select_categories($tmp[1], $conf->global->{$constname}, $constname, 0, $langs->trans('CustomersProspectsCategoriesShort')); - } elseif (preg_match('/thirdparty_type/', $val['type'])) { - require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; - $formcompany = new FormCompany($db); - print $formcompany->selectProspectCustomerType($conf->global->{$constname}, $constname); - } elseif ($val['type'] == 'securekey') { - print ''; - if (!empty($conf->use_javascript_ajax)) { - print ' '.img_picto($langs->trans('Generate'), 'refresh', 'id="generate_token'.$constname.'" class="linkobject"'); - } - if (!empty($conf->use_javascript_ajax)) { - print "\n".''; - } - } elseif ($val['type'] == 'product') { - if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) { - $selected = (empty($conf->global->$constname) ? '' : $conf->global->$constname); - $form->select_produits($selected, $constname, '', 0); - } - } else { - print ''; - } - print '
    '; + print $formSetup->generateOutput(true); print '
    '; print ''; @@ -321,72 +242,8 @@ if ($action == 'edit') { print '
    '; } else { if (!empty($arrayofparameters)) { - print ''; - print ''; - foreach ($arrayofparameters as $constname => $val) { - if ($val['enabled']==1) { - $setupnotempty++; - print ''; - } - } - - print '
    '.$langs->trans("Parameter").''.$langs->trans("Value").'
    '; - $tooltiphelp = (($langs->trans($constname . 'Tooltip') != $constname . 'Tooltip') ? $langs->trans($constname . 'Tooltip') : ''); - print $form->textwithpicto($langs->trans($constname), $tooltiphelp); - print ''; - - if ($val['type'] == 'textarea') { - print dol_nl2br($conf->global->{$constname}); - } elseif ($val['type']== 'html') { - print $conf->global->{$constname}; - } elseif ($val['type'] == 'yesno') { - print ajax_constantonoff($constname); - } elseif (preg_match('/emailtemplate:/', $val['type'])) { - include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; - $formmail = new FormMail($db); - - $tmp = explode(':', $val['type']); - - $template = $formmail->getEMailTemplate($db, $tmp[1], $user, $langs, $conf->global->{$constname}); - if ($template<0) { - setEventMessages(null, $formmail->errors, 'errors'); - } - print $langs->trans($template->label); - } elseif (preg_match('/category:/', $val['type'])) { - $c = new Categorie($db); - $result = $c->fetch($conf->global->{$constname}); - if ($result < 0) { - setEventMessages(null, $c->errors, 'errors'); - } - $ways = $c->print_all_ways(' >> ', 'none', 0, 1); // $ways[0] = "ccc2 >> ccc2a >> ccc2a1" with html formated text - $toprint = array(); - foreach ($ways as $way) { - $toprint[] = '
  • color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '
  • '; - } - print '
      ' . implode(' ', $toprint) . '
    '; - } elseif (preg_match('/thirdparty_type/', $val['type'])) { - if ($conf->global->{$constname}==2) { - print $langs->trans("Prospect"); - } elseif ($conf->global->{$constname}==3) { - print $langs->trans("ProspectCustomer"); - } elseif ($conf->global->{$constname}==1) { - print $langs->trans("Customer"); - } elseif ($conf->global->{$constname}==0) { - print $langs->trans("NorProspectNorCustomer"); - } - } elseif ($val['type'] == 'product') { - $product = new Product($db); - $resprod = $product->fetch($conf->global->{$constname}); - if ($resprod > 0) { - print $product->ref; - } elseif ($resprod < 0) { - setEventMessages(null, $object->errors, "errors"); - } - } else { - print $conf->global->{$constname}; - } - print '
    '; + print $formSetup->generateOutput(); print '
    '; print ''.$langs->trans("Modify").''; From fdcb9674d8470419daddb3c1b970113117f6a46b Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sun, 24 Oct 2021 02:09:49 +0200 Subject: [PATCH 02/16] Remove prints --- htdocs/core/class/html.formsetup.class.php | 38 +++++++++++----------- htdocs/langs/en_US/admin.lang | 1 + 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 095cf0d8887..27b0e3fb615 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -69,7 +69,7 @@ class formSetup{ $out.= ' '.$this->langs->trans("Value").''; $out.= ''; - foreach ($this->arrayOfParameters as $const => $item) { + foreach ($this->arrayOfParameters as $item) { $out.= $this->generateLineOutput($item, $edit); } @@ -98,10 +98,10 @@ class formSetup{ $out.= ''; if($edit){ - $item->generateInputField(); + $out.= $item->generateInputField(); } else{ - $item->generateOutputField(); + $out.= $item->generateOutputField(); } if(!empty($item->errors)){ @@ -125,7 +125,6 @@ class formSetup{ public function addItemsFromParamsArray($params){ if(!array($params)){ return false; } - foreach ($params as $confKey => $param){ $this->addItemFromParams($confKey, $param); // todo manage error } @@ -139,7 +138,7 @@ class formSetup{ */ public function addItemFromParams($confKey, $params){ - if(empty($confKey) || !empty($params['type'])){ return false; } + if(empty($confKey) || empty($params['type'])){ return false; } /* * Exemple from old module builder setup page @@ -155,13 +154,14 @@ class formSetup{ $item = new formSetupItem($this->db); $item->type = $params['type']; + $item->confKey = $confKey; if(!empty($params['enabled'])) { $item->enabled = $params['enabled']; } if(!empty($params['css'])){ - $item->enabled = $params['css']; + $item->cssClass = $params['css']; } $this->arrayOfParameters[$item->confKey] = $item; @@ -233,7 +233,7 @@ class formSetupItem public function getNameText(){ if(!empty($this->nameText)){ return $this->nameText; } - return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : ''); + return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey',$this->confKey)); } public function generateInputField(){ @@ -313,7 +313,7 @@ class formSetupItem $this->form->select_produits($selected, $this->confKey, '', 0); } } else { - $out.= ''; + $out.= ''; } return $out; @@ -347,11 +347,11 @@ class formSetupItem $out = ''; if ($this->type == 'textarea') { - print dol_nl2br($conf->global->{$this->confKey}); + $out.= dol_nl2br($conf->global->{$this->confKey}); } elseif ($this->type== 'html') { - print $conf->global->{$this->confKey}; + $out.= $conf->global->{$this->confKey}; } elseif ($this->type == 'yesno') { - print ajax_constantonoff($this->confKey); + $out.= ajax_constantonoff($this->confKey); } elseif (preg_match('/emailtemplate:/', $this->type)) { include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; $formmail = new FormMail($this->db); @@ -362,7 +362,7 @@ class formSetupItem if ($template<0) { $this->setErrors($formmail->errors); } - print $this->langs->trans($template->label); + $out.= $this->langs->trans($template->label); } elseif (preg_match('/category:/', $this->type)) { $c = new Categorie($this->db); $result = $c->fetch($conf->global->{$this->confKey}); @@ -374,27 +374,27 @@ class formSetupItem foreach ($ways as $way) { $toprint[] = '
  • color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '
  • '; } - print '
      ' . implode(' ', $toprint) . '
    '; + $out.= '
      ' . implode(' ', $toprint) . '
    '; } elseif (preg_match('/thirdparty_type/', $this->type)) { if ($conf->global->{$this->confKey}==2) { - print $this->langs->trans("Prospect"); + $out.= $this->langs->trans("Prospect"); } elseif ($conf->global->{$this->confKey}==3) { - print $this->langs->trans("ProspectCustomer"); + $out.= $this->langs->trans("ProspectCustomer"); } elseif ($conf->global->{$this->confKey}==1) { - print $this->langs->trans("Customer"); + $out.= $this->langs->trans("Customer"); } elseif ($conf->global->{$this->confKey}==0) { - print $this->langs->trans("NorProspectNorCustomer"); + $out.= $this->langs->trans("NorProspectNorCustomer"); } } elseif ($this->type == 'product') { $product = new Product($this->db); $resprod = $product->fetch($conf->global->{$this->confKey}); if ($resprod > 0) { - print $product->ref; + $out.= $product->ref; } elseif ($resprod < 0) { $this->setErrors($product->errors); } } else { - print $conf->global->{$this->confKey}; + $out.= $conf->global->{$this->confKey}; } return $out; diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index f75ba12abe5..96fba681373 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -2198,3 +2198,4 @@ SkinAndColors=Skin and colors IfYouUseASecondTaxYouMustSetYouUseTheMainTax=If you want to use a second tax, you must enable also the first sales tax IfYouUseAThirdTaxYouMustSetYouUseTheMainTax=If you want to use a third tax, you must enable also the first sales tax PDF_USE_1A=Generate PDF with PDF/A-1b format +MissingTranslationForConfKey = Missing translation for %s From 61576dad539cc363fa3efdfbb55e9f9512cb5cf7 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Sun, 24 Oct 2021 00:17:18 +0000 Subject: [PATCH 03/16] Fixing style errors. --- htdocs/core/class/html.formsetup.class.php | 73 ++++++++++++---------- 1 file changed, 39 insertions(+), 34 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 27b0e3fb615..70dd11e47ae 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -19,7 +19,8 @@ /** * This class help you create setup render */ -class formSetup{ +class formSetup +{ /** * @var DoliDB Database handler. @@ -48,10 +49,9 @@ class formSetup{ $this->db = $db; $this->form = new Form($this->db); - if($outputLangs){ + if ($outputLangs) { $this->langs = $outputLangs; - } - else{ + } else { $this->langs = $langs; } } @@ -59,7 +59,8 @@ class formSetup{ /** * @return string */ - public function generateOutput($edit = false){ + public function generateOutput($edit = false) + { require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; @@ -82,7 +83,8 @@ class formSetup{ * @param bool $edit * @return string */ - public function generateLineOutput($item, $edit = false){ + public function generateLineOutput($item, $edit = false) + { $out = ''; if ($item->enabled==1) { @@ -97,14 +99,13 @@ class formSetup{ $out.= ''; - if($edit){ + if ($edit) { $out.= $item->generateInputField(); - } - else{ + } else { $out.= $item->generateOutputField(); } - if(!empty($item->errors)){ + if (!empty($item->errors)) { // TODO : move set event message in a methode to be called by cards not by this class setEventMessages(null, $item->errors, 'errors'); } @@ -122,10 +123,11 @@ class formSetup{ * @param array $params * @ */ - public function addItemsFromParamsArray($params){ + public function addItemsFromParamsArray($params) + { - if(!array($params)){ return false; } - foreach ($params as $confKey => $param){ + if (!array($params)) { return false; } + foreach ($params as $confKey => $param) { $this->addItemFromParams($confKey, $param); // todo manage error } } @@ -136,9 +138,10 @@ class formSetup{ * @param string $confKey * @param array $params */ - public function addItemFromParams($confKey, $params){ + public function addItemFromParams($confKey, $params) + { - if(empty($confKey) || empty($params['type'])){ return false; } + if (empty($confKey) || empty($params['type'])) { return false; } /* * Exemple from old module builder setup page @@ -156,11 +159,11 @@ class formSetup{ $item->type = $params['type']; $item->confKey = $confKey; - if(!empty($params['enabled'])) { + if (!empty($params['enabled'])) { $item->enabled = $params['enabled']; } - if(!empty($params['css'])){ + if (!empty($params['css'])) { $item->cssClass = $params['css']; } @@ -168,7 +171,6 @@ class formSetup{ return true; } - } @@ -226,20 +228,23 @@ class formSetupItem $this->confKey = $confKey; } - public function getHelpText(){ - if(!empty($this->helpText)){ return $this->helpText; } + public function getHelpText() + { + if (!empty($this->helpText)) { return $this->helpText; } return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : ''); } - public function getNameText(){ - if(!empty($this->nameText)){ return $this->nameText; } - return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey',$this->confKey)); + public function getNameText() + { + if (!empty($this->nameText)) { return $this->nameText; } + return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey)); } - public function generateInputField(){ + public function generateInputField() + { global $conf, $user; - if(!empty($this->fieldOverride)){ + if (!empty($this->fieldOverride)) { return $this->fieldOverride; } @@ -324,23 +329,24 @@ class formSetupItem * add error * @param array|string $errors */ - public function setErrors($errors){ - if(is_array($errors)){ - if(!empty($errors)){ - foreach ($errors as $error){ + public function setErrors($errors) + { + if (is_array($errors)) { + if (!empty($errors)) { + foreach ($errors as $error) { $this->setErrors($error); } } - } - elseif(!empty($errors)){ + } elseif (!empty($errors)) { $this->errors[] = $errors; } } - public function generateOutputField(){ + public function generateOutputField() + { global $conf, $user; - if(!empty($this->fieldOverride)){ + if (!empty($this->fieldOverride)) { return $this->fieldOverride; } @@ -399,5 +405,4 @@ class formSetupItem return $out; } - } From 3a862212905ceebf3a1de29b33dd1abede9ae25f Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sat, 30 Oct 2021 12:54:13 +0200 Subject: [PATCH 04/16] add more customisation --- htdocs/core/class/html.formsetup.class.php | 327 +++++++++++++----- htdocs/modulebuilder/template/admin/setup.php | 28 +- 2 files changed, 272 insertions(+), 83 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 27b0e3fb615..f781afe0079 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -19,7 +19,8 @@ /** * This class help you create setup render */ -class formSetup{ +class formSetup +{ /** * @var DoliDB Database handler. @@ -27,7 +28,7 @@ class formSetup{ public $db; /** @var formSetupItem[] */ - public $arrayOfParameters = array(); + public $params = array(); public $setupNotEmpty = 0; @@ -37,10 +38,12 @@ class formSetup{ /** @var Form */ public $form; + /** * Constructor * - * @param DoliDB $db Database handler + * @param DoliDB $db Database handler + * @param Translate $outputLangs if needed can use another lang */ public function __construct($db, $outputLangs = false) { @@ -48,41 +51,53 @@ class formSetup{ $this->db = $db; $this->form = new Form($this->db); - if($outputLangs){ + if ($outputLangs) { $this->langs = $outputLangs; - } - else{ + } else { $this->langs = $langs; } } /** + * @param bool $editMode true will display output on edit mod * @return string */ - public function generateOutput($edit = false){ + public function generateOutput($editMode = false) + { + $out = ''; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; - $out = ''; + $out.= ''; + if ($editMode) { + $out .= ''; + } + + $out.= '
    '; + $out.= ''; $out.= ''; $out.= ' '; $out.= ' '; $out.= ''; + $out.= ''; - foreach ($this->arrayOfParameters as $item) { - $out.= $this->generateLineOutput($item, $edit); + $out.= ''; + foreach ($this->params as $item) { + $out.= $this->generateLineOutput($item, $editMode); } + $out.= ''; $out.= '
    '.$this->langs->trans("Parameter").''.$this->langs->trans("Value").'
    '; return $out; } /** - * @param formSetupItem $item - * @param bool $edit - * @return string + * @param formSetupItem $item the setup item + * @param bool $editMode Display as edit mod + * @return string the html output for an setup item */ - public function generateLineOutput($item, $edit = false){ + public function generateLineOutput($item, $editMode = false) + { $out = ''; if ($item->enabled==1) { @@ -97,14 +112,13 @@ class formSetup{ $out.= ''; - if($edit){ + if ($editMode) { $out.= $item->generateInputField(); - } - else{ + } else { $out.= $item->generateOutputField(); } - if(!empty($item->errors)){ + if (!empty($item->errors)) { // TODO : move set event message in a methode to be called by cards not by this class setEventMessages(null, $item->errors, 'errors'); } @@ -118,14 +132,14 @@ class formSetup{ /** - * @param string $confKey - * @param array $params - * @ + * @param array $params an array of arrays of params from old modulBuilder params + * @deprecated was used to test module builder convertion to this form usage + * @return null */ - public function addItemsFromParamsArray($params){ - - if(!array($params)){ return false; } - foreach ($params as $confKey => $param){ + public function addItemsFromParamsArray($params) + { + if (!array($params)) { return false; } + foreach ($params as $confKey => $param) { $this->addItemFromParams($confKey, $param); // todo manage error } } @@ -133,12 +147,14 @@ class formSetup{ /** * From old - * @param string $confKey - * @param array $params + * @param string $confKey the conf name to store + * @param array $params an array of params from old modulBuilder params + * @deprecated was used to test module builder convertion to this form usage + * @return bool */ - public function addItemFromParams($confKey, $params){ - - if(empty($confKey) || empty($params['type'])){ return false; } + public function addItemFromParams($confKey, $params) + { + if (empty($confKey) || empty($params['type'])) { return false; } /* * Exemple from old module builder setup page @@ -152,26 +168,56 @@ class formSetup{ //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), */ - $item = new formSetupItem($this->db); + $item = new formSetupItem($confKey); $item->type = $params['type']; - $item->confKey = $confKey; - if(!empty($params['enabled'])) { + if (!empty($params['enabled'])) { $item->enabled = $params['enabled']; } - if(!empty($params['css'])){ + if (!empty($params['css'])) { $item->cssClass = $params['css']; } - $this->arrayOfParameters[$item->confKey] = $item; + $this->params[$item->confKey] = $item; return true; } + /** + * Reload for each item default conf + * note: this will override custom configuration + * @return bool + */ + public function reloadConfs() + { + + if (!array($this->params)) { return false; } + foreach ($this->params as $item) { + $item->reloadConf(); + } + + return true; + } + + + + /** + * Create a new item + * @param $confKey the conf key used in database + * @return formSetupItem the new setup item created + */ + public function newItem($confKey) + { + $item = new formSetupItem($confKey); + $this->params[$item->confKey] = $item; + return $this->params[$item->confKey]; + } } - +/** + * This class help to create item for class formSetup + */ class formSetupItem { /** @@ -194,67 +240,108 @@ class formSetupItem /** @var string $helpText */ public $helpText = ''; - /** @var bool|string set this var to override field output */ + /** @var string $value */ + public $fieldValue; + + /** @var bool|string set this var to override field output will override $fieldInputOverride and $fieldOutputOverride too */ public $fieldOverride = false; + /** @var bool|string set this var to override field output */ + public $fieldInputOverride = false; + + /** @var bool|string set this var to override field output */ + public $fieldOutputOverride = false; + /** * @var string $errors */ public $errors = array(); /** + * TODO each type must have setAs{type} method to help configuration + * And set var as protected when its done configuration must be done by method * @var string $type 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type' */ - public $type; + public $type = 'string'; - public $enabled = 0; + public $enabled = 1; public $cssClass = ''; /** * Constructor * - * @param $confKey + * @param $confKey the conf key used in database */ public function __construct($confKey) { - global $langs, $db; + global $langs, $db, $conf; $this->db = $db; $this->form = new Form($this->db); $this->langs = $langs; $this->confKey = $confKey; + $this->fieldValue = $conf->global->{$this->confKey}; } - public function getHelpText(){ - if(!empty($this->helpText)){ return $this->helpText; } + /** + * reload conf value from databases + * @return null + */ + public function reloadConf() + { + global $conf; + $this->fieldValue = $conf->global->{$this->confKey}; + } + + /** + * Get help text or generate it + * @return int|string + */ + public function getHelpText() + { + if (!empty($this->helpText)) { return $this->helpText; } return (($this->langs->trans($this->confKey . 'Tooltip') != $this->confKey . 'Tooltip') ? $this->langs->trans($this->confKey . 'Tooltip') : ''); } - public function getNameText(){ - if(!empty($this->nameText)){ return $this->nameText; } - return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey',$this->confKey)); + /** + * Get field name text or generate it + * @return false|int|string + */ + public function getNameText() + { + if (!empty($this->nameText)) { return $this->nameText; } + return (($this->langs->trans($this->confKey) != $this->confKey) ? $this->langs->trans($this->confKey) : $this->langs->trans('MissingTranslationForConfKey', $this->confKey)); } - public function generateInputField(){ + /** + * generate input field + * @return bool|string + */ + public function generateInputField() + { global $conf, $user; - if(!empty($this->fieldOverride)){ + if (!empty($this->fieldOverride)) { return $this->fieldOverride; } + if (!empty($this->fieldInputOverride)) { + return $this->fieldInputOverride; + } + $out = ''; if ($this->type == 'textarea') { $out.= '\n"; } elseif ($this->type== 'html') { require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; - $doleditor = new DolEditor($this->confKey, $conf->global->{$this->confKey}, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); + $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); $doleditor->Create(); } elseif ($this->type == 'yesno') { - $out.= $this->form->selectyesno($this->confKey, $conf->global->{$this->confKey}, 1); + $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1); } elseif (preg_match('/emailtemplate:/', $this->type)) { include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; $formmail = new FormMail($this->db); @@ -274,7 +361,7 @@ class formSetupItem $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel; } } - $out.= $this->form->selectarray($this->confKey, $arrayOfMessageName, $conf->global->{$this->confKey}, 'None', 0, 0, '', 0, 0, 0, '', '', 1); + $out.= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1); } elseif (preg_match('/category:/', $this->type)) { require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; @@ -282,13 +369,13 @@ class formSetupItem $tmp = explode(':', $this->type); $out.= img_picto('', 'category', 'class="pictofixedwidth"'); - $out.= $formother->select_categories($tmp[1], $conf->global->{$this->confKey}, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort')); + $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort')); } elseif (preg_match('/thirdparty_type/', $this->type)) { require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; $formcompany = new FormCompany($this->db); - $out.= $formcompany->selectProspectCustomerType($conf->global->{$this->confKey}, $this->confKey); + $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey); } elseif ($this->type == 'securekey') { - $out.= ''; + $out.= ''; if (!empty($conf->use_javascript_ajax)) { $out.= ' '.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"'); } @@ -309,11 +396,11 @@ class formSetupItem } } elseif ($this->type == 'product') { if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) { - $selected = (empty($conf->global->{$this->confKey}) ? '' : $conf->global->{$this->confKey}); + $selected = (empty($this->fieldValue) ? '' : $this->fieldValue); $this->form->select_produits($selected, $this->confKey, '', 0); } } else { - $out.= ''; + $out.= ''; } return $out; @@ -321,35 +408,44 @@ class formSetupItem /** - * add error - * @param array|string $errors + * Add error + * @param array|string $errors the error text + * @return null */ - public function setErrors($errors){ - if(is_array($errors)){ - if(!empty($errors)){ - foreach ($errors as $error){ + public function setErrors($errors) + { + if (is_array($errors)) { + if (!empty($errors)) { + foreach ($errors as $error) { $this->setErrors($error); } } - } - elseif(!empty($errors)){ + } elseif (!empty($errors)) { $this->errors[] = $errors; } } - public function generateOutputField(){ + /** + * @return bool|string Generate the output html for this item + */ + public function generateOutputField() + { global $conf, $user; - if(!empty($this->fieldOverride)){ + if (!empty($this->fieldOverride)) { return $this->fieldOverride; } + if (!empty($this->fieldOutputOverride)) { + return $this->fieldOutputOverride; + } + $out = ''; if ($this->type == 'textarea') { - $out.= dol_nl2br($conf->global->{$this->confKey}); + $out.= dol_nl2br($this->fieldValue); } elseif ($this->type== 'html') { - $out.= $conf->global->{$this->confKey}; + $out.= $this->fieldValue; } elseif ($this->type == 'yesno') { $out.= ajax_constantonoff($this->confKey); } elseif (preg_match('/emailtemplate:/', $this->type)) { @@ -358,14 +454,14 @@ class formSetupItem $tmp = explode(':', $this->type); - $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $conf->global->{$this->confKey}); + $template = $formmail->getEMailTemplate($this->db, $tmp[1], $user, $this->langs, $this->fieldValue); if ($template<0) { $this->setErrors($formmail->errors); } $out.= $this->langs->trans($template->label); } elseif (preg_match('/category:/', $this->type)) { $c = new Categorie($this->db); - $result = $c->fetch($conf->global->{$this->confKey}); + $result = $c->fetch($this->fieldValue); if ($result < 0) { $this->setErrors($c->errors); } @@ -376,28 +472,105 @@ class formSetupItem } $out.= '
      ' . implode(' ', $toprint) . '
    '; } elseif (preg_match('/thirdparty_type/', $this->type)) { - if ($conf->global->{$this->confKey}==2) { + if ($this->fieldValue==2) { $out.= $this->langs->trans("Prospect"); - } elseif ($conf->global->{$this->confKey}==3) { + } elseif ($this->fieldValue==3) { $out.= $this->langs->trans("ProspectCustomer"); - } elseif ($conf->global->{$this->confKey}==1) { + } elseif ($this->fieldValue==1) { $out.= $this->langs->trans("Customer"); - } elseif ($conf->global->{$this->confKey}==0) { + } elseif ($this->fieldValue==0) { $out.= $this->langs->trans("NorProspectNorCustomer"); } } elseif ($this->type == 'product') { $product = new Product($this->db); - $resprod = $product->fetch($conf->global->{$this->confKey}); + $resprod = $product->fetch($this->fieldValue); if ($resprod > 0) { $out.= $product->ref; } elseif ($resprod < 0) { $this->setErrors($product->errors); } } else { - $out.= $conf->global->{$this->confKey}; + $out.= $this->fieldValue; } return $out; } + /* + * METHODS FOR SETTING DISPLAY TYPE + */ + + /** + * Set type of input as string + * @return null + */ + public function setAsString() + { + $this->type = 'string'; + } + + /** + * Set type of input as textarea + * @return null + */ + public function setAsTextarea() + { + $this->type = 'textarea'; + } + + /** + * Set type of input as html editor + * @return null + */ + public function setAsHtml() + { + $this->type = 'html'; + } + + /** + * Set type of input as emailtemplate selector + * @return null + */ + public function setAsEmailTemplate() + { + $this->type = 'emailtemplate'; + } + + /** + * Set type of input as thirdparty_type selector + * @return null + */ + public function setAsThirdpartyType() + { + $this->type = 'thirdparty_type'; + } + + /** + * Set type of input as Yes + * @return null + */ + public function setAsYesNo() + { + $this->type = 'yesno'; + } + + /** + * Set type of input as secure key + * @return null + */ + public function setAsSecureKey() + { + $this->type = 'securekey'; + } + + /** + * Set type of input as a category selector + * TODO add default value + * @param int $catType Type of category ('customer', 'supplier', 'contact', 'product', 'member'). Old mode (0, 1, 2, ...) is deprecated. + * @return null + */ + public function setAsCategory($catType) + { + $this->type = 'category:'.$catType; + } } diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 390f45d955c..e3e32cf6f6b 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -85,6 +85,27 @@ $arrayofparameters = array( //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), ); +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; +$formSetup = new formSetup($db); + +$formSetup->addItemsFromParamsArray($arrayofparameters); + +// Hôte +$item = $formSetup->newItem('GPC_HOST'); +$item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; + +// 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().' https://console.developers.google.com/apis/credentials'; + +// Clé pour API : Client Secret +$formSetup->newItem('GPC_GOOGLE_CLIENT_SECRET'); + + + $error = 0; $setupnotempty = 0; @@ -97,6 +118,7 @@ $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); if ((float) DOL_VERSION >= 6) { include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; + $formSetup->reloadConfs(); } if ($action == 'updateMask') { @@ -222,11 +244,6 @@ print dol_get_fiche_head($head, 'settings', $langs->trans($page_name), -1, "mymo echo ''.$langs->trans("MyModuleSetupPage").'

    '; -require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; -$formSetup = new formSetup($db); - -$formSetup->addItemsFromParamsArray($arrayofparameters); - if ($action == 'edit') { print ''; print ''; @@ -242,7 +259,6 @@ if ($action == 'edit') { print '
    '; } else { if (!empty($arrayofparameters)) { - print $formSetup->generateOutput(); print '
    '; From 5179f2c90b26fab6bbd0ed121ff68f3ff7d7b22f Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sat, 30 Oct 2021 13:03:23 +0200 Subject: [PATCH 05/16] Add demo data --- htdocs/modulebuilder/template/admin/setup.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index e3e32cf6f6b..f7f30c19228 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -90,8 +90,9 @@ $formSetup = new formSetup($db); $formSetup->addItemsFromParamsArray($arrayofparameters); + // Hôte -$item = $formSetup->newItem('GPC_HOST'); +$item = $formSetup->newItem('NO_PARAM_JUST_TEXT'); $item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; // Setup conf MYMODULE_MYPARAM1 as a simple string input @@ -99,12 +100,15 @@ $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().' https://console.developers.google.com/apis/credentials'; - -// Clé pour API : Client Secret -$formSetup->newItem('GPC_GOOGLE_CLIENT_SECRET'); +$item->nameText = $item->getNameText().' more html text '; +// Setup conf MYMODULE_MYPARAM3 +$item = $formSetup->newItem('MYMODULE_MYPARAM3'); +$item->setAsThirdpartyType(); + +// Setup conf MYMODULE_MYPARAM4 : quick define write style +$formSetup->newItem('MYMODULE_MYPARAM4')->setAsYesNo(); $error = 0; $setupnotempty = 0; From cdf1538ffacac651367a423cffce3b83d0c93ec9 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sat, 30 Oct 2021 16:36:51 +0200 Subject: [PATCH 06/16] fix save using template --- htdocs/core/class/html.formsetup.class.php | 59 +++++++++++++++---- htdocs/modulebuilder/template/admin/setup.php | 19 +++++- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index f229d12101d..eb5a9710da1 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -183,6 +183,24 @@ class formSetup return true; } + /** + * used to export param array for /core/actions_setmoduleoptions.inc.php template + * @return array $arrayofparameters for /core/actions_setmoduleoptions.inc.php + * @deprecated + */ + public function exportItemsAsParamsArray() + { + $arrayofparameters = array(); + foreach ($this->params as $key => $item) { + $arrayofparameters[$item->confKey] = array( + 'type' => $item->type, + 'enabled' => $item->enabled + ); + } + + return $arrayofparameters; + } + /** * Reload for each item default conf * note: this will override custom configuration @@ -396,7 +414,7 @@ class formSetupItem } elseif ($this->type == 'product') { if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) { $selected = (empty($this->fieldValue) ? '' : $this->fieldValue); - $this->form->select_produits($selected, $this->confKey, '', 0); + $out.= $this->form->select_produits($selected, $this->confKey, '', 0, 0, 1, 2, '', 0, array(), 0, '1', 0, $this->cssClass, 0, '', null, 1); } } else { $out.= ''; @@ -501,75 +519,94 @@ class formSetupItem /** * Set type of input as string - * @return null + * @return self */ public function setAsString() { $this->type = 'string'; + return $this; } /** * Set type of input as textarea - * @return null + * @return self */ public function setAsTextarea() { $this->type = 'textarea'; + return $this; } /** * Set type of input as html editor - * @return null + * @return self */ public function setAsHtml() { $this->type = 'html'; + return $this; } /** * Set type of input as emailtemplate selector - * @return null + * @param string $templateType email template type + * @return self */ - public function setAsEmailTemplate() + public function setAsEmailTemplate($templateType) { - $this->type = 'emailtemplate'; + $this->type = 'emailtemplate:'.$templateType; + return $this; } /** * Set type of input as thirdparty_type selector - * @return null + * @return self */ public function setAsThirdpartyType() { $this->type = 'thirdparty_type'; + return $this; } /** * Set type of input as Yes - * @return null + * @return self */ public function setAsYesNo() { $this->type = 'yesno'; + return $this; } /** * Set type of input as secure key - * @return null + * @return self */ public function setAsSecureKey() { $this->type = 'securekey'; + return $this; + } + + /** + * Set type of input as product + * @return self + */ + public function setAsProduct() + { + $this->type = 'product'; + return $this; } /** * Set type of input as a category selector * TODO add default value * @param int $catType Type of category ('customer', 'supplier', 'contact', 'product', 'member'). Old mode (0, 1, 2, ...) is deprecated. - * @return null + * @return self */ public function setAsCategory($catType) { $this->type = 'category:'.$catType; + return $this; } } diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index f7f30c19228..007e9d84b12 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -94,22 +94,33 @@ $formSetup->addItemsFromParamsArray($arrayofparameters); // 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 +// 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 : quick define write style +// 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(); + + $error = 0; $setupnotempty = 0; @@ -121,6 +132,8 @@ $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); */ if ((float) DOL_VERSION >= 6) { + // TODO Add save setup by formSetup + $arrayofparameters = $formSetup->exportItemsAsParamsArray(); include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; $formSetup->reloadConfs(); } From ca766c2e712a2e6e50b1e078541da8e0b70ee642 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sun, 31 Oct 2021 09:29:20 +0100 Subject: [PATCH 07/16] Factoring --- htdocs/core/class/html.formsetup.class.php | 73 +++++++++++++++++----- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index eb5a9710da1..219308bd5e6 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -63,11 +63,9 @@ class formSetup */ public function generateOutput($editMode = false) { - - $out = ''; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; - $out.= ''; + $out = ''; if ($editMode) { $out .= ''; } @@ -168,7 +166,7 @@ class formSetup */ $item = new formSetupItem($confKey); - $item->type = $params['type']; + $item->setTypeFromTypeString($params['type']); if (!empty($params['enabled'])) { $item->enabled = $params['enabled']; @@ -186,7 +184,7 @@ class formSetup /** * used to export param array for /core/actions_setmoduleoptions.inc.php template * @return array $arrayofparameters for /core/actions_setmoduleoptions.inc.php - * @deprecated + * @deprecated yes this method came deprecated because it exists only for manage setup convertion */ public function exportItemsAsParamsArray() { @@ -279,7 +277,7 @@ class formSetupItem * And set var as protected when its done configuration must be done by method * @var string $type 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type' */ - public $type = 'string'; + protected $type = 'string'; public $enabled = 1; @@ -349,14 +347,12 @@ class formSetupItem $out = ''; - if ($this->type == 'textarea') { - $out.= '\n"; + if ($this->type == 'title') { + $out.= $this->generateOutputField(); // title have no input + } elseif ($this->type == 'textarea') { + $out.= $this->generateInputFieldTextarea(); } elseif ($this->type== 'html') { - require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; - $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); - $doleditor->Create(); + $out.= $this->generateInputFieldHtml(); } elseif ($this->type == 'yesno') { $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1); } elseif (preg_match('/emailtemplate:/', $this->type)) { @@ -423,6 +419,42 @@ class formSetupItem return $out; } + /** + * generate input field for textarea + * @return string + */ + public function generateInputFieldTextarea() + { + $out = '\n"; + return $out; + } + /** + * generate input field for html + * @return string + */ + public function generateInputFieldHtml() + { + global $conf; + require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php'; + $doleditor = new DolEditor($this->confKey, $this->fieldValue, '', 160, 'dolibarr_notes', '', false, false, $conf->fckeditor->enabled, ROWS_5, '90%'); + return $doleditor->Create(1); + } + + /** + * set the type from string : used for old module builder setup conf style conversion and tests + * because this two class will quickly evolve it's important to not set directly $this->type (will be protected) so this method exist + * to be sure we can manage evolution easily + * @param string $type possible values based on old module builder setup : 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type' + * @deprecated yes this method came deprecated because it exists only for manage setup convertion + * @return bool + */ + public function setTypeFromTypeString($type) + { + $this->type = $type; + return true; + } /** * Add error @@ -459,7 +491,9 @@ class formSetupItem $out = ''; - if ($this->type == 'textarea') { + if ($this->type == 'title') { + // nothing to do + } elseif ($this->type == 'textarea') { $out.= dol_nl2br($this->fieldValue); } elseif ($this->type== 'html') { $out.= $this->fieldValue; @@ -609,4 +643,15 @@ class formSetupItem $this->type = 'category:'.$catType; return $this; } + + /** + * Set type of input as a simple title + * no data to store + * @return self + */ + public function setAsTitle() + { + $this->type = 'title'; + return $this; + } } From 4c016cc3be9fe59716d703c415648728711bc192 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Mon, 1 Nov 2021 10:11:43 +0100 Subject: [PATCH 08/16] integrate fix from V14 --- htdocs/core/class/html.formsetup.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 219308bd5e6..a7df249e44c 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -447,7 +447,7 @@ class formSetupItem * because this two class will quickly evolve it's important to not set directly $this->type (will be protected) so this method exist * to be sure we can manage evolution easily * @param string $type possible values based on old module builder setup : 'string', 'textarea', 'category:'.Categorie::TYPE_CUSTOMER', 'emailtemplate', 'thirdparty_type' - * @deprecated yes this method came deprecated because it exists only for manage setup convertion + * @deprecated yes this setTypeFromTypeString came deprecated because it exists only for manage setup convertion * @return bool */ public function setTypeFromTypeString($type) @@ -521,7 +521,7 @@ class formSetupItem foreach ($ways as $way) { $toprint[] = '
  • color ? ' style="background: #' . $c->color . ';"' : ' style="background: #bbb"') . '>' . $way . '
  • '; } - $out.= '
      ' . implode(' ', $toprint) . '
    '; + $out.='
      ' . implode(' ', $toprint) . '
    '; } elseif (preg_match('/thirdparty_type/', $this->type)) { if ($this->fieldValue==2) { $out.= $this->langs->trans("Prospect"); From ea641b707d17a0ca44c2aefc4838453e0e7eee9f Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Mon, 1 Nov 2021 10:32:37 +0100 Subject: [PATCH 09/16] Factoring --- htdocs/core/class/html.formsetup.class.php | 141 ++++++++++++++------- 1 file changed, 95 insertions(+), 46 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index a7df249e44c..df99b6cdbbb 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -191,7 +191,7 @@ class formSetup $arrayofparameters = array(); foreach ($this->params as $key => $item) { $arrayofparameters[$item->confKey] = array( - 'type' => $item->type, + 'type' => $item->getType(), 'enabled' => $item->enabled ); } @@ -356,57 +356,15 @@ class formSetupItem } elseif ($this->type == 'yesno') { $out.= $this->form->selectyesno($this->confKey, $this->fieldValue, 1); } elseif (preg_match('/emailtemplate:/', $this->type)) { - include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; - $formmail = new FormMail($this->db); - - $tmp = explode(':', $this->type); - $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang - //$arraydefaultmessage = $formmail->getEMailTemplate($db, $tmp[1], $user, null, 0, 1, ''); - $arrayOfMessageName = array(); - if (is_array($formmail->lines_model)) { - foreach ($formmail->lines_model as $modelMail) { - //var_dump($modelmail); - $moreonlabel = ''; - if (!empty($arrayOfMessageName[$modelMail->label])) { - $moreonlabel = ' (' . $this->langs->trans("SeveralLangugeVariatFound") . ')'; - } - // The 'label' is the key that is unique if we exclude the language - $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel; - } - } - $out.= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1); + $out.= $this->generateInputFieldEmailTemplate(); } elseif (preg_match('/category:/', $this->type)) { - require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; - require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; - $formother = new FormOther($this->db); - - $tmp = explode(':', $this->type); - $out.= img_picto('', 'category', 'class="pictofixedwidth"'); - $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort')); + $out.=$this->generateInputFieldCategories(); } elseif (preg_match('/thirdparty_type/', $this->type)) { require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; $formcompany = new FormCompany($this->db); $out.= $formcompany->selectProspectCustomerType($this->fieldValue, $this->confKey); } elseif ($this->type == 'securekey') { - $out.= ''; - if (!empty($conf->use_javascript_ajax)) { - $out.= ' '.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"'); - } - if (!empty($conf->use_javascript_ajax)) { - $out.= "\n".''; - } + $out.= $this->generateInputFieldSecureKey(); } elseif ($this->type == 'product') { if (!empty($conf->product->enabled) || !empty($conf->service->enabled)) { $selected = (empty($this->fieldValue) ? '' : $this->fieldValue); @@ -430,6 +388,7 @@ class formSetupItem $out.= "\n"; return $out; } + /** * generate input field for html * @return string @@ -442,6 +401,96 @@ class formSetupItem return $doleditor->Create(1); } + /** + * generate input field for categories + * @return string + */ + public function generateInputFieldCategories() + { + global $conf; + require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; + $formother = new FormOther($this->db); + + $tmp = explode(':', $this->type); + $out= img_picto('', 'category', 'class="pictofixedwidth"'); + $out.= $formother->select_categories($tmp[1], $this->fieldValue, $this->confKey, 0, $this->langs->trans('CustomersProspectsCategoriesShort')); + return $out; + } + + /** + * generate input field for email template selector + * @return string + */ + public function generateInputFieldEmailTemplate() + { + global $conf, $user; + $out = ''; + if (preg_match('/emailtemplate:/', $this->type)) { + include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; + $formmail = new FormMail($this->db); + + $tmp = explode(':', $this->type); + $nboftemplates = $formmail->fetchAllEMailTemplate($tmp[1], $user, null, 1); // We set lang=null to get in priority record with no lang + $arrayOfMessageName = array(); + if (is_array($formmail->lines_model)) { + foreach ($formmail->lines_model as $modelMail) { + $moreonlabel = ''; + if (!empty($arrayOfMessageName[$modelMail->label])) { + $moreonlabel = ' (' . $this->langs->trans("SeveralLangugeVariatFound") . ')'; + } + // The 'label' is the key that is unique if we exclude the language + $arrayOfMessageName[$modelMail->id] = $this->langs->trans(preg_replace('/\(|\)/', '', $modelMail->label)) . $moreonlabel; + } + } + $out .= $this->form->selectarray($this->confKey, $arrayOfMessageName, $this->fieldValue, 'None', 0, 0, '', 0, 0, 0, '', '', 1); + } + + return $out; + } + + + /** + * generate input field for secure key + * @return string + */ + public function generateInputFieldSecureKey() + { + global $conf; + $out = ''; + if (!empty($conf->use_javascript_ajax)) { + $out.= ' '.img_picto($this->langs->trans('Generate'), 'refresh', 'id="generate_token'.$this->confKey.'" class="linkobject"'); + } + if (!empty($conf->use_javascript_ajax)) { + $out .= "\n" . ''; + } + return $out; + } + + /** + * 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 + * to be sure we can manage evolution easily + * + * @return string + */ + public function getType() + { + return $this->type; + } + /** * set the type from string : used for old module builder setup conf style conversion and tests * because this two class will quickly evolve it's important to not set directly $this->type (will be protected) so this method exist From b98efd7e44085ff4f7bb0e9ac056139994bb7c4a Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Tue, 2 Nov 2021 13:55:12 +0100 Subject: [PATCH 10/16] Add rank item management and add Hook --- htdocs/core/class/html.formsetup.class.php | 258 ++++++++++++++++-- htdocs/modulebuilder/template/admin/setup.php | 27 +- 2 files changed, 248 insertions(+), 37 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index df99b6cdbbb..cbf8cbd37ba 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -30,6 +30,9 @@ class formSetup /** @var formSetupItem[] */ public $params = array(); + /** + * @var int + */ public $setupNotEmpty = 0; /** @var Translate */ @@ -38,6 +41,9 @@ class formSetup /** @var Form */ public $form; + /** @var int */ + protected $maxItemRank; + /** * Constructor * @@ -63,29 +69,81 @@ class formSetup */ public function generateOutput($editMode = false) { + global $hookmanager, $action; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; - $out = ''; - if ($editMode) { - $out .= ''; + $parameters = array( + 'editMode' => $editMode + ); + $reshook = $hookmanager->executeHooks('formSetupBeforeGenerateOutput', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks + if ($reshook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); } - $out.= ''; - $out.= ''; - $out.= ''; - $out.= ' '; - $out.= ' '; - $out.= ''; - $out.= ''; + if ($reshook > 0) { + return $hookmanager->resPrint; + } else { + $out = ''; + if ($editMode) { + $out .= ''; + } - $out.= ''; + $out .= '
    '.$this->langs->trans("Parameter").''.$this->langs->trans("Value").'
    '; + $out .= ''; + $out .= ''; + $out .= ' '; + $out .= ' '; + $out .= ''; + $out .= ''; + + // Sort items before render + $this->sortingItems(); + + $out .= ''; + foreach ($this->params as $item) { + $out .= $this->generateLineOutput($item, $editMode); + } + $out .= ''; + + $out .= '
    ' . $this->langs->trans("Parameter") . '' . $this->langs->trans("Value") . '
    '; + return $out; + } + } + + /** + * @param bool $noMessageInUpdate display event message on errors and success + * @return void|null + */ + public function saveConfFromPost($noMessageInUpdate = false) + { + + if (empty($this->params)) { + return null; + } + + $this->db->begin(); + $error = 0; foreach ($this->params as $item) { - $out.= $this->generateLineOutput($item, $editMode); + $res = $item->setValueFromPost(); + if ($res > 0) { + $item->saveConfValue(); + } elseif ($res < 0) { + $error++; + break; + } } - $out.= ''; - $out.= ''; - return $out; + if (!$error) { + $this->db->commit(); + if (empty($noMessageInUpdate)) { + setEventMessages($this->langs->trans("SetupSaved"), null); + } + } else { + $this->db->rollback(); + if (empty($noMessageInUpdate)) { + setEventMessages($this->langs->trans("SetupNotSaved"), null, 'errors'); + } + } } /** @@ -209,25 +267,133 @@ class formSetup if (!array($this->params)) { return false; } foreach ($this->params as $item) { - $item->reloadConf(); + $item->reloadValueFromConf(); } return true; } - /** * Create a new item + * the tagret is useful with hooks : that allow externals modules to add setup items on good place * @param $confKey the conf key used in database + * @param string $targetItemKey target item used to place the new item beside + * @param bool $insertAfterTarget insert before or after target item ? * @return formSetupItem the new setup item created */ - public function newItem($confKey) + public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false) { $item = new formSetupItem($confKey); + + // set item rank if not defined as last item + if (empty($item->rank)) { + $item->rank = $this->getCurentItemMaxRank() + 1; + $this->setItemMaxRank($item->rank); // set new max rank if needed + } + + // try to get rank from target column, this will override item->rank + if (!empty($targetItemKey)) { + if (isset($this->params[$targetItemKey])) { + $targetItem = $this->params[$targetItemKey]; + $item->rank = $targetItem->rank; // $targetItem->rank will be increase after + if ($targetItem->rank >= 0 && $insertAfterTarget) { + $item->rank++; + } + } + + // calc new rank for each item to make place for new item + foreach ($this->params as $fItem) { + if ($item->rank <= $fItem->rank) { + $fItem->rank = $fItem->rank + 1; + $this->setItemMaxRank($fItem->rank); // set new max rank if needed + } + } + } + $this->params[$item->confKey] = $item; return $this->params[$item->confKey]; } + + /** + * Sort items according to rank + * @return bool + */ + public function sortingItems() + { + // Sorting + return uasort($this->params, array($this, 'itemSort')); + } + + /** + * @param bool $cache To use cache or not + * @return int + */ + public function getCurentItemMaxRank($cache = true) + { + if (empty($this->params)) { + return 0; + } + + if ($cache && $this->maxItemRank > 0) { + return $this->maxItemRank; + } + + $this->maxItemRank = 0; + foreach ($this->params as $item) { + $this->maxItemRank = max($this->maxItemRank, $item->rank); + } + + return $this->maxItemRank; + } + + + /** + * set new max rank if needed + * @param int $rank the item rank + * @return int|void + */ + public function setItemMaxRank($rank) + { + $this->maxItemRank = max($this->maxItemRank, $rank); + } + + + /** + * get item position rank from item key + * + * @param string $itemKey the item key + * @return int rank on success and -1 on error + */ + public function getLineRank($itemKey) + { + if (!isset($this->params[$itemKey]->rank)) { + return -1; + } + return $this->params[$itemKey]->rank; + } + + + /** + * uasort callback function to Sort params items + * + * @param formSetupItem $a formSetup item + * @param formSetupItem $b formSetup item + * @return int Return compare result + */ + public function itemSort(formSetupItem $a, formSetupItem $b) + { + if (empty($a->rank)) { + $a->rank = 0; + } + if (empty($b->rank)) { + $b->rank = 0; + } + if ($a->rank == $b->rank) { + return 0; + } + return ($a->rank < $b->rank) ? -1 : 1; + } } /** @@ -243,6 +409,9 @@ class formSetupItem /** @var Translate */ public $langs; + /** @var int */ + public $entity; + /** @var Form */ public $form; @@ -267,6 +436,9 @@ class formSetupItem /** @var bool|string set this var to override field output */ public $fieldOutputOverride = false; + /** @var int $rank */ + public $rank = 0; + /** * @var string $errors */ @@ -294,6 +466,7 @@ class formSetupItem $this->db = $db; $this->form = new Form($this->db); $this->langs = $langs; + $this->entity = $conf->entity; $this->confKey = $confKey; $this->fieldValue = $conf->global->{$this->confKey}; @@ -303,12 +476,58 @@ class formSetupItem * reload conf value from databases * @return null */ - public function reloadConf() + public function reloadValueFromConf() { global $conf; $this->fieldValue = $conf->global->{$this->confKey}; } + + /** + * Save const value based on htdocs/core/actions_setmoduleoptions.inc.php + * @return int -1 if KO, 1 if OK + */ + public function saveConfValue() + { + // 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); + if ($result < 0) { + return -1; + } else { + return 1; + } + } + } + + + /** + * Save const value based on htdocs/core/actions_setmoduleoptions.inc.php + * @return int -1 if KO, 0 nothing to do , 1 if OK + */ + public function setValueFromPost() + { + // Modify constant only if key was posted (avoid resetting key to the null value) + if ($this->type != 'title') { + if (preg_match('/category:/', $this->type)) { + if (GETPOST($this->confKey, 'int') == '-1') { + $val_const = ''; + } else { + $val_const = GETPOST($this->confKey, 'int'); + } + } else { + $val_const = GETPOST($this->confKey, 'alpha'); + } + + // TODO add value check with class validate + $this->fieldValue = $val_const; + + return 1; + } + + return 0; + } + /** * Get help text or generate it * @return int|string @@ -596,6 +815,7 @@ class formSetupItem return $out; } + /* * METHODS FOR SETTING DISPLAY TYPE */ diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 007e9d84b12..527ea8eb32d 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -60,6 +60,9 @@ require_once '../lib/mymodule.lib.php'; // Translations $langs->loadLangs(array("admin", "mymodule@mymodule")); +// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context +$hookmanager->initHooks(array('mymodulesetup', 'globalsetup')); + // Access control if (!$user->admin) { accessforbidden(); @@ -74,22 +77,10 @@ $label = GETPOST('label', 'alpha'); $scandir = GETPOST('scan_dir', 'alpha'); $type = 'myobject'; -$arrayofparameters = array( - 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1), - 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1), - //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1), - //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1), - //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1), - //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1), - //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1), - //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), -); require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; $formSetup = new formSetup($db); -$formSetup->addItemsFromParamsArray($arrayofparameters); - // Hôte $item = $formSetup->newItem('NO_PARAM_JUST_TEXT'); @@ -131,11 +122,10 @@ $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); * Actions */ -if ((float) DOL_VERSION >= 6) { - // TODO Add save setup by formSetup - $arrayofparameters = $formSetup->exportItemsAsParamsArray(); - include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; - $formSetup->reloadConfs(); +if ((float) DOL_VERSION >= 15) { + if ($action == 'update') { + $formSetup->saveConfFromPost(); + } } if ($action == 'updateMask') { @@ -275,8 +265,9 @@ if ($action == 'edit') { print ''; print '
    '; } else { - if (!empty($arrayofparameters)) { + if (!empty($formSetup->params)) { print $formSetup->generateOutput(); + $setupnotempty++; print '
    '; print ''.$langs->trans("Modify").''; From bbe71c9c32d792a3fd0cfe56b37394d9af6f4942 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Tue, 2 Nov 2021 14:07:36 +0100 Subject: [PATCH 11/16] Rename var --- htdocs/core/class/html.formsetup.class.php | 36 +++++++++---------- htdocs/modulebuilder/template/admin/setup.php | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index cbf8cbd37ba..8f9d3af0528 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -28,7 +28,7 @@ class formSetup public $db; /** @var formSetupItem[] */ - public $params = array(); + public $items = array(); /** * @var int @@ -100,7 +100,7 @@ class formSetup $this->sortingItems(); $out .= ''; - foreach ($this->params as $item) { + foreach ($this->items as $item) { $out .= $this->generateLineOutput($item, $editMode); } $out .= ''; @@ -117,13 +117,13 @@ class formSetup public function saveConfFromPost($noMessageInUpdate = false) { - if (empty($this->params)) { + if (empty($this->items)) { return null; } $this->db->begin(); $error = 0; - foreach ($this->params as $item) { + foreach ($this->items as $item) { $res = $item->setValueFromPost(); if ($res > 0) { $item->saveConfValue(); @@ -234,7 +234,7 @@ class formSetup $item->cssClass = $params['css']; } - $this->params[$item->confKey] = $item; + $this->items[$item->confKey] = $item; return true; } @@ -247,7 +247,7 @@ class formSetup public function exportItemsAsParamsArray() { $arrayofparameters = array(); - foreach ($this->params as $key => $item) { + foreach ($this->items as $key => $item) { $arrayofparameters[$item->confKey] = array( 'type' => $item->getType(), 'enabled' => $item->enabled @@ -265,8 +265,8 @@ class formSetup public function reloadConfs() { - if (!array($this->params)) { return false; } - foreach ($this->params as $item) { + if (!array($this->items)) { return false; } + foreach ($this->items as $item) { $item->reloadValueFromConf(); } @@ -294,8 +294,8 @@ class formSetup // try to get rank from target column, this will override item->rank if (!empty($targetItemKey)) { - if (isset($this->params[$targetItemKey])) { - $targetItem = $this->params[$targetItemKey]; + if (isset($this->items[$targetItemKey])) { + $targetItem = $this->items[$targetItemKey]; $item->rank = $targetItem->rank; // $targetItem->rank will be increase after if ($targetItem->rank >= 0 && $insertAfterTarget) { $item->rank++; @@ -303,7 +303,7 @@ class formSetup } // calc new rank for each item to make place for new item - foreach ($this->params as $fItem) { + foreach ($this->items as $fItem) { if ($item->rank <= $fItem->rank) { $fItem->rank = $fItem->rank + 1; $this->setItemMaxRank($fItem->rank); // set new max rank if needed @@ -311,8 +311,8 @@ class formSetup } } - $this->params[$item->confKey] = $item; - return $this->params[$item->confKey]; + $this->items[$item->confKey] = $item; + return $this->items[$item->confKey]; } /** @@ -322,7 +322,7 @@ class formSetup public function sortingItems() { // Sorting - return uasort($this->params, array($this, 'itemSort')); + return uasort($this->items, array($this, 'itemSort')); } /** @@ -331,7 +331,7 @@ class formSetup */ public function getCurentItemMaxRank($cache = true) { - if (empty($this->params)) { + if (empty($this->items)) { return 0; } @@ -340,7 +340,7 @@ class formSetup } $this->maxItemRank = 0; - foreach ($this->params as $item) { + foreach ($this->items as $item) { $this->maxItemRank = max($this->maxItemRank, $item->rank); } @@ -367,10 +367,10 @@ class formSetup */ public function getLineRank($itemKey) { - if (!isset($this->params[$itemKey]->rank)) { + if (!isset($this->items[$itemKey]->rank)) { return -1; } - return $this->params[$itemKey]->rank; + return $this->items[$itemKey]->rank; } diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 527ea8eb32d..417c28f30a2 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -265,7 +265,7 @@ if ($action == 'edit') { print ''; print '
    '; } else { - if (!empty($formSetup->params)) { + if (!empty($formSetup->items)) { print $formSetup->generateOutput(); $setupnotempty++; From 12b046cb68a41ace480a519d366f3243b03144d6 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Tue, 2 Nov 2021 14:11:36 +0100 Subject: [PATCH 12/16] Fix class name --- htdocs/core/class/html.formsetup.class.php | 20 +++++++++---------- htdocs/modulebuilder/template/admin/setup.php | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/html.formsetup.class.php b/htdocs/core/class/html.formsetup.class.php index 8f9d3af0528..6f2ec2acfb6 100644 --- a/htdocs/core/class/html.formsetup.class.php +++ b/htdocs/core/class/html.formsetup.class.php @@ -19,7 +19,7 @@ /** * This class help you create setup render */ -class formSetup +class FormSetup { /** @@ -27,7 +27,7 @@ class formSetup */ public $db; - /** @var formSetupItem[] */ + /** @var FormSetupItem[] */ public $items = array(); /** @@ -147,7 +147,7 @@ class formSetup } /** - * @param formSetupItem $item the setup item + * @param FormSetupItem $item the setup item * @param bool $editMode Display as edit mod * @return string the html output for an setup item */ @@ -223,7 +223,7 @@ class formSetup //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), */ - $item = new formSetupItem($confKey); + $item = new FormSetupItem($confKey); $item->setTypeFromTypeString($params['type']); if (!empty($params['enabled'])) { @@ -280,11 +280,11 @@ class formSetup * @param $confKey the conf key used in database * @param string $targetItemKey target item used to place the new item beside * @param bool $insertAfterTarget insert before or after target item ? - * @return formSetupItem the new setup item created + * @return FormSetupItem the new setup item created */ public function newItem($confKey, $targetItemKey = false, $insertAfterTarget = false) { - $item = new formSetupItem($confKey); + $item = new FormSetupItem($confKey); // set item rank if not defined as last item if (empty($item->rank)) { @@ -377,11 +377,11 @@ class formSetup /** * uasort callback function to Sort params items * - * @param formSetupItem $a formSetup item - * @param formSetupItem $b formSetup item + * @param FormSetupItem $a formSetup item + * @param FormSetupItem $b formSetup item * @return int Return compare result */ - public function itemSort(formSetupItem $a, formSetupItem $b) + public function itemSort(FormSetupItem $a, FormSetupItem $b) { if (empty($a->rank)) { $a->rank = 0; @@ -399,7 +399,7 @@ class formSetup /** * This class help to create item for class formSetup */ -class formSetupItem +class FormSetupItem { /** * @var DoliDB Database handler. diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 417c28f30a2..206cca0ff52 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -79,7 +79,7 @@ $type = 'myobject'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; -$formSetup = new formSetup($db); +$formSetup = new FormSetup($db); // Hôte From 83cb6b67538bf4d41504ea7110e0f943dc19bd62 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 15 Nov 2021 11:54:08 +0100 Subject: [PATCH 13/16] Update setup.php --- htdocs/modulebuilder/template/admin/setup.php | 47 ++++++++++--------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 206cca0ff52..70d26b1753e 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -78,39 +78,40 @@ $scandir = GETPOST('scan_dir', 'alpha'); $type = 'myobject'; -require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; -$formSetup = new FormSetup($db); +if ((float) DOL_VERSION >= 15) { + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; + $formSetup = new FormSetup($db); -// Hôte -$item = $formSetup->newItem('NO_PARAM_JUST_TEXT'); -$item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; -$item->cssClass = 'minwidth500'; + // 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 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_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_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_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_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(); + // Setup conf MYMODULE_MYPARAM6 + $formSetup->newItem('MYMODULE_MYPARAM6')->setAsSecureKey()->enabled = 0; // disabled + // Setup conf MYMODULE_MYPARAM7 + $formSetup->newItem('MYMODULE_MYPARAM7')->setAsProduct(); +} $error = 0; $setupnotempty = 0; From 74e30f292c444ff6b2dd6c301d5131bc13de8004 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 15 Nov 2021 11:58:48 +0100 Subject: [PATCH 14/16] Update setup.php --- htdocs/modulebuilder/template/admin/setup.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 70d26b1753e..f9964a74f57 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -123,11 +123,7 @@ $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); * Actions */ -if ((float) DOL_VERSION >= 15) { - if ($action == 'update') { - $formSetup->saveConfFromPost(); - } -} +include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; if ($action == 'updateMask') { $maskconstorder = GETPOST('maskconstorder', 'alpha'); From f7d9975b660c5b450dfe0c4c5cf8f28fc46c6fe8 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 15 Nov 2021 12:00:37 +0100 Subject: [PATCH 15/16] Update setup.php --- htdocs/modulebuilder/template/admin/setup.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index f9964a74f57..c866d795b8a 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -79,16 +79,15 @@ $type = 'myobject'; if ((float) DOL_VERSION >= 15) { + // For Dolibarr v15+ compatibility require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; $formSetup = new FormSetup($db); - // 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'); @@ -111,7 +110,18 @@ if ((float) DOL_VERSION >= 15) { // Setup conf MYMODULE_MYPARAM7 $formSetup->newItem('MYMODULE_MYPARAM7')->setAsProduct(); -} +} else { + // For Dolibarr v6+ compatibility + $arrayofparameters = array( + 'MYMODULE_MYPARAM1'=>array('type'=>'string', 'css'=>'minwidth500' ,'enabled'=>1), + 'MYMODULE_MYPARAM2'=>array('type'=>'textarea','enabled'=>1), + //'MYMODULE_MYPARAM3'=>array('type'=>'category:'.Categorie::TYPE_CUSTOMER, 'enabled'=>1), + //'MYMODULE_MYPARAM4'=>array('type'=>'emailtemplate:thirdparty', 'enabled'=>1), + //'MYMODULE_MYPARAM5'=>array('type'=>'yesno', 'enabled'=>1), + //'MYMODULE_MYPARAM5'=>array('type'=>'thirdparty_type', 'enabled'=>1), + //'MYMODULE_MYPARAM6'=>array('type'=>'securekey', 'enabled'=>1), + //'MYMODULE_MYPARAM7'=>array('type'=>'product', 'enabled'=>1), +); $error = 0; $setupnotempty = 0; From dbdf34c7da428c70859e9508fc53a10e83bc1f77 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 15 Nov 2021 12:09:58 +0100 Subject: [PATCH 16/16] Update setup.php --- htdocs/modulebuilder/template/admin/setup.php | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index c866d795b8a..cc6d37f8abd 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -77,8 +77,9 @@ $label = GETPOST('label', 'alpha'); $scandir = GETPOST('scan_dir', 'alpha'); $type = 'myobject'; +$useFormSetup = 0; -if ((float) DOL_VERSION >= 15) { +if ($useFormSetup && (float) DOL_VERSION >= 15) { // For Dolibarr v15+ compatibility require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; $formSetup = new FormSetup($db); @@ -263,8 +264,11 @@ if ($action == 'edit') { print ''; print ''; - print $formSetup->generateOutput(true); - + if ($useFormSetup && (float) DOL_VERSION >= 15) { + print $formSetup->generateOutput(true); + } else { + // Add input fields here + } print '
    '; print ''; print '
    '; @@ -272,10 +276,18 @@ if ($action == 'edit') { print ''; print '
    '; } else { - if (!empty($formSetup->items)) { + if ($useFormSetup && (float) DOL_VERSION >= 15) { print $formSetup->generateOutput(); - $setupnotempty++; + $setupnotempty = count($formSetup->items); + } else { + // Show fields here + foreach ($arrayofparameters as $key => $val) { + $setupnotempty++; + + } + } + if ($setupnotempty) { print '
    '; print ''.$langs->trans("Modify").''; print '
    ';