From 72bfedaba15097d4e45f74f00cc79bc76fc0a75e Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Sat, 21 May 2022 00:33:28 +0200 Subject: [PATCH] Add form setup and backport as default setup builder --- htdocs/modulebuilder/index.php | 27 ++ htdocs/modulebuilder/template/admin/setup.php | 302 ++++-------------- 2 files changed, 95 insertions(+), 234 deletions(-) diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index e8e32f73d0f..0bf2036ec33 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -238,6 +238,33 @@ if ($dirins && $action == 'initmodule' && $modulename) { } } + // Copy last html.formsetup.class.php' to backport folder + $tryToCopyFromSetupClass = true; + $backportDest = $destdir .'/backport/v17/core/class'; + $backportFileSrc = DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; + $backportFileDest = $backportDest.'/html.formsetup.class.php'; + $result = dol_mkdir($backportDest); + + if ($result < 0) { + $error++; + $langs->load("errors"); + setEventMessages($langs->trans("ErrorFailToCreateDir", $backportDest), null, 'errors'); + $tryToCopyFromSetupClass = true; + } + + if ($tryToCopyFromSetupClass) { + $result = dol_copy($backportFileSrc, $backportFileDest); + if ($result <= 0) { + if ($result < 0) { + $error++; + $langs->load("errors"); + setEventMessages($langs->trans("ErrorFailToCopyFile", $backportFileSrc, $backportFileDest), null, 'errors'); + } else { + setEventMessages($langs->trans("AllFilesDidAlreadyExist", $backportFileDest), null, 'warnings'); + } + } + } + if (!empty($conf->global->MODULEBUILDER_USE_ABOUT)) { dol_delete_file($destdir.'/admin/about.php'); } diff --git a/htdocs/modulebuilder/template/admin/setup.php b/htdocs/modulebuilder/template/admin/setup.php index 84b394da808..c353ba420ed 100644 --- a/htdocs/modulebuilder/template/admin/setup.php +++ b/htdocs/modulebuilder/template/admin/setup.php @@ -78,65 +78,70 @@ $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), -); $error = 0; $setupnotempty = 0; // Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only -$useFormSetup = 0; -// Convert arrayofparameter into a formSetup object -if ($useFormSetup && (float) DOL_VERSION >= 15) { - require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php'; - $formSetup = new FormSetup($db); +$useFormSetup = 1; - // you can use the param convertor - $formSetup->addItemsFromParamsArray($arrayofparameters); - - // or use the new system see exemple as follow (or use both because you can ;-) ) - - /* - // Hôte - $item = $formSetup->newItem('NO_PARAM_JUST_TEXT'); - $item->fieldOverride = (empty($_SERVER['HTTPS']) ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; - $item->cssClass = 'minwidth500'; - - // Setup conf MYMODULE_MYPARAM1 as a simple string input - $item = $formSetup->newItem('MYMODULE_MYPARAM1'); - - // Setup conf MYMODULE_MYPARAM1 as a simple textarea input but we replace the text of field title - $item = $formSetup->newItem('MYMODULE_MYPARAM2'); - $item->nameText = $item->getNameText().' more html text '; - - // Setup conf MYMODULE_MYPARAM3 - $item = $formSetup->newItem('MYMODULE_MYPARAM3'); - $item->setAsThirdpartyType(); - - // Setup conf MYMODULE_MYPARAM4 : exemple of quick define write style - $formSetup->newItem('MYMODULE_MYPARAM4')->setAsYesNo(); - - // Setup conf MYMODULE_MYPARAM5 - $formSetup->newItem('MYMODULE_MYPARAM5')->setAsEmailTemplate('thirdparty'); - - // Setup conf MYMODULE_MYPARAM6 - $formSetup->newItem('MYMODULE_MYPARAM6')->setAsSecureKey()->enabled = 0; // disabled - - // Setup conf MYMODULE_MYPARAM7 - $formSetup->newItem('MYMODULE_MYPARAM7')->setAsProduct(); - */ - - $setupnotempty = count($formSetup->items); +if (!class_exists('FormSetup')) { + // For retrocompatibility Dolibarr < 15.0 + if (versioncompare(explode('.', DOL_VERSION), array(17)) < 0 && !class_exists('FormSetup')) { + require_once __DIR__.'/../backport/v17/core/class/html.formsetup.class.php'; + } else { + 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'); + +// Setup conf MYMODULE_MYPARAM1 as a simple textarea input but we replace the text of field title +$item = $formSetup->newItem('MYMODULE_MYPARAM2'); +$item->nameText = $item->getNameText().' more html text '; + +// Setup conf MYMODULE_MYPARAM3 +$item = $formSetup->newItem('MYMODULE_MYPARAM3'); +$item->setAsThirdpartyType(); + +// Setup conf MYMODULE_MYPARAM4 : exemple of quick define write style +$formSetup->newItem('MYMODULE_MYPARAM4')->setAsYesNo(); + +// Setup conf MYMODULE_MYPARAM5 +$formSetup->newItem('MYMODULE_MYPARAM5')->setAsEmailTemplate('thirdparty'); + +// Setup conf MYMODULE_MYPARAM6 +$formSetup->newItem('MYMODULE_MYPARAM6')->setAsSecureKey()->enabled = 0; // disabled + +// Setup conf MYMODULE_MYPARAM7 +$formSetup->newItem('MYMODULE_MYPARAM7')->setAsProduct(); + + +$formSetup->newItem('Title')->setAsTitle(); + +$item = $formSetup->newItem('MYMODULE_MYPARAM8'); +$TField = array( + 'test01' => $langs->trans('test01'), + 'test02' => $langs->trans('test02'), + 'test03' => $langs->trans('test03'), + 'test04' => $langs->trans('test04'), + 'test05' => $langs->trans('test05'), + 'test06' => $langs->trans('test06'), +); +$item->setAsMultiSelect($TField); +$item->helpText = $langs->transnoentities('MYMODULE_MYPARAM8'); + +$setupnotempty =+ count($formSetup->items); + $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); @@ -145,6 +150,11 @@ $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); * Actions */ +// For retrocompatibility Dolibarr < 15.0 +if ( versioncompare(explode('.', DOL_VERSION), array(15)) < 0 && $action == 'update' && !empty($user->admin)) { + $formSetup->saveConfFromPost(); +} + include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php'; if ($action == 'updateMask') { @@ -271,191 +281,15 @@ echo ''.$langs->trans("MyModuleSetupPage").'< if ($action == 'edit') { - if ($useFormSetup && (float) DOL_VERSION >= 15) { - print $formSetup->generateOutput(true); - } else { - 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 '
'; - print ''; - print '
'; - - print '
'; - } - + print $formSetup->generateOutput(true); print '
'; +} elseif (!empty($formSetup->items)) { + print $formSetup->generateOutput(); + print '
'; + print ''.$langs->trans("Modify").''; + print '
'; } else { - if ($useFormSetup && (float) DOL_VERSION >= 15) { - if (!empty($formSetup->items)) { - print $formSetup->generateOutput(); - } - } 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'); - } elseif ($result > 0 ) { - $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 '
    '; - } - } - - if ($setupnotempty) { - print '
    '; - print ''.$langs->trans("Modify").''; - print '
    '; - } else { - print '
    '.$langs->trans("NothingToSetup"); - } + print '
    '.$langs->trans("NothingToSetup"); }