From 89b56120eb97dbbf0396adf7dbabe20711a92e01 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 15 Jun 2024 14:53:36 +0200 Subject: [PATCH] Fix error when import module not compatible with PHP --- .../modules/import/import_csv.modules.php | 12 ++++++-- .../modules/import/import_xlsx.modules.php | 10 ++++++- htdocs/core/modules/import/modules_import.php | 29 ++++++++++++++----- htdocs/imports/import.php | 25 +++++++++------- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index 71df9de78b2..d59db6ad759 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -87,12 +87,12 @@ class ImportCsv extends ModeleImports */ public function __construct($db, $datatoimport) { - global $conf, $langs; + global $langs; parent::__construct(); $this->db = $db; - $this->separator = (GETPOST('separator') ? GETPOST('separator') : (!getDolGlobalString('IMPORT_CSV_SEPARATOR_TO_USE') ? ',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE)); + $this->separator = (GETPOST('separator') ? GETPOST('separator') : getDolGlobalString('IMPORT_CSV_SEPARATOR_TO_USE', ',')); $this->enclosure = '"'; $this->escape = '"'; @@ -102,6 +102,14 @@ class ImportCsv extends ModeleImports $this->extension = 'csv'; // Extension for generated file by this driver $this->picto = 'mime/other'; // Picto $this->version = '1.34'; // Driver version + $this->phpmin = array(7, 0); // Minimum version of PHP required by module + + require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; + if (versioncompare($this->phpmin, versionphparray()) > 0) { + dol_syslog("Module need a higher PHP version"); + $this->error = "Module need a higher PHP version"; + return; + } // If driver use an external library, put its name here $this->label_lib = 'Dolibarr'; diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php index 9546a02b1e5..4a187166dea 100644 --- a/htdocs/core/modules/import/import_xlsx.modules.php +++ b/htdocs/core/modules/import/import_xlsx.modules.php @@ -94,7 +94,7 @@ class ImportXlsx extends ModeleImports */ public function __construct($db, $datatoimport) { - global $conf, $langs; + global $langs; parent::__construct(); $this->db = $db; @@ -106,6 +106,14 @@ class ImportXlsx extends ModeleImports $this->extension = 'xlsx'; // Extension for generated file by this driver $this->picto = 'mime/xls'; // Picto (This is not used by the example file code as Mime type, too bad ...) $this->version = '1.0'; // Driver version + $this->phpmin = array(7, 1); // Minimum version of PHP required by module + + require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; + if (versioncompare($this->phpmin, versionphparray()) > 0) { + dol_syslog("Module need a higher PHP version"); + $this->error = "Module need a higher PHP version"; + return; + } // If driver use an external library, put its name here require_once DOL_DOCUMENT_ROOT.'/includes/phpoffice/phpspreadsheet/src/autoloader.php'; diff --git a/htdocs/core/modules/import/modules_import.php b/htdocs/core/modules/import/modules_import.php index 537284e238f..5dd5bd85700 100644 --- a/htdocs/core/modules/import/modules_import.php +++ b/htdocs/core/modules/import/modules_import.php @@ -71,9 +71,23 @@ class ModeleImports */ public $version = 'dolibarr'; - public $label_lib; // Label of external lib used by driver + /** + * PHP minimal version required by driver + * @var string + */ + public $phpmin = array(); - public $version_lib; // Version of external lib used by driver + /** + * Label of external lib used by driver + * @var string + */ + public $label_lib; + + /** + * Version of external lib used by driver + * @var string + */ + public $version_lib; // Array of all drivers public $driverlabel = array(); @@ -82,6 +96,8 @@ class ModeleImports public $driverversion = array(); + public $drivererror = array(); + public $liblabel = array(); public $libversion = array(); @@ -225,10 +241,10 @@ class ModeleImports $dir = DOL_DOCUMENT_ROOT."/core/modules/import/"; $handle = opendir($dir); - // Recherche des fichiers drivers imports disponibles - $i = 0; + // Search list ov drivers available and qualified if (is_resource($handle)) { while (($file = readdir($handle)) !== false) { + $reg = array(); if (preg_match("/^import_(.*)\.modules\.php/i", $file, $reg)) { $moduleid = $reg[1]; @@ -245,11 +261,10 @@ class ModeleImports $this->driverlabel[$module->id] = $module->getDriverLabel(''); $this->driverdesc[$module->id] = $module->getDriverDesc(''); $this->driverversion[$module->id] = $module->getDriverVersion(''); + $this->drivererror[$module->id] = $module->error ? $module->error : ''; // If use an external lib - $this->liblabel[$module->id] = $module->getLibLabel(''); + $this->liblabel[$module->id] = ($module->error ? ''.$module->error.'' : $module->getLibLabel('')); $this->libversion[$module->id] = $module->getLibVersion(''); - - $i++; } } } diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php index 6940de807fc..5402667a2a3 100644 --- a/htdocs/imports/import.php +++ b/htdocs/imports/import.php @@ -465,20 +465,25 @@ if ($step == 2 && $datatoimport) { foreach ($list as $key) { print ''; print ''.img_picto_common($key, $objmodelimport->getPictoForKey($key)).''; - $text = $objmodelimport->getDriverDescForKey($key); - // @phan-suppress-next-line PhanPluginSuspiciousParamPosition - print ''.$form->textwithpicto($objmodelimport->getDriverLabelForKey($key), $text).''; + $htmltext = $objmodelimport->getDriverDescForKey($key); + print ''.$form->textwithpicto($objmodelimport->getDriverLabelForKey($key), $htmltext).''; print ''; - $filename = $langs->transnoentitiesnoconv("ExampleOfImportFile").'_'.$datatoimport.'.'.$key; - print ''; - print img_picto('', 'download', 'class="paddingright opacitymedium"'); - print $langs->trans("DownloadEmptyExampleShort"); - print ''; - print $form->textwithpicto('', $langs->trans("DownloadEmptyExample").'.
'.$langs->trans("StarAreMandatory")); + if (empty($objmodelimport->drivererror[$key])) { + $filename = $langs->transnoentitiesnoconv("ExampleOfImportFile").'_'.$datatoimport.'.'.$key; + print ''; + print img_picto('', 'download', 'class="paddingright opacitymedium"'); + print $langs->trans("DownloadEmptyExampleShort"); + print ''; + print $form->textwithpicto('', $langs->trans("DownloadEmptyExample").'.
'.$langs->trans("StarAreMandatory")); + } else { + print dolPrintHtml($objmodelimport->drivererror[$key]); + } print ''; // Action button print ''; - print ''.img_picto($langs->trans("SelectFormat"), 'next', 'class="fa-15"').''; + if (empty($objmodelimport->drivererror[$key])) { + print ''.img_picto($langs->trans("SelectFormat"), 'next', 'class="fa-15"').''; + } print ''; print ''; }