2
0
forked from Wavyzz/dolibarr

Better error management

This commit is contained in:
Laurent Destailleur
2024-03-13 20:17:26 +01:00
parent 3dd6b7a4c7
commit 45a68ba612
5 changed files with 49 additions and 25 deletions

View File

@@ -644,19 +644,17 @@ function dol_fileperm($pathoffile)
/**
* Make replacement of strings into a file.
*
* @param string $srcfile Source file (can't be a directory)
* @param string $srcfile Source file (can't be a directory)
* @param array<string,string> $arrayreplacement Array with strings to replace. Example: array('valuebefore'=>'valueafter', ...)
* @param string $destfile Destination file (can't be a directory). If empty, will be same than source file.
* @param string $newmask Mask for new file (0 by default means $conf->global->MAIN_UMASK). Example: '0666'
* @param int $indexdatabase 1=index new file into database.
* @param int $arrayreplacementisregex 1=Array of replacement is already an array with key that is a regex. Warning: the key must be escaped with preg_quote for '/'
* @return int Return integer <0 if error, 0 if nothing done (dest file already exists), >0 if OK
* @param string $destfile Destination file (can't be a directory). If empty, will be same than source file.
* @param string $newmask Mask for new file (0 by default means $conf->global->MAIN_UMASK). Example: '0666'
* @param int $indexdatabase 1=index new file into database.
* @param int $arrayreplacementisregex 1=Array of replacement is already an array with key that is a regex. Warning: the key must be escaped with preg_quote for '/'
* @return int Return integer <0 if error, 0 if nothing done (dest file already exists), >0 if OK
* @see dol_copy()
*/
function dolReplaceInFile($srcfile, $arrayreplacement, $destfile = '', $newmask = '0', $indexdatabase = 0, $arrayreplacementisregex = 0)
{
global $conf;
dol_syslog("files.lib.php::dolReplaceInFile srcfile=".$srcfile." destfile=".$destfile." newmask=".$newmask." indexdatabase=".$indexdatabase." arrayreplacementisregex=".$arrayreplacementisregex);
if (empty($srcfile)) {

View File

@@ -456,20 +456,37 @@ function dolGetListOfObjectClasses($destdir)
return -1;
}
/**
* function for check if comment begin an end exist in modMyModule class
* @param string $file filename or path
* @param int $number 0 = For Menus,1 = For permissions, 2 = For Dictionaries
* @return int 1 if OK , -1 if KO
* Function to check if comment begin an end exist in modMyModule class
*
* @param string $file Filename or path
* @param int $number 0 = For Menus,1 = For permissions, 2 = For Dictionaries
* @return int 1 if OK , -1 if KO
*/
function checkExistComment($file, $number)
{
if (!file_exists($file)) {
return -1;
}
$content = file_get_contents($file);
if ($number === 0) {
if (strpos($content, '/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !== false && strpos($content, '/* END MODULEBUILDER LEFTMENU MYOBJECT */') !== false) {
$ret = 0;
if (strpos($content, '/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !== false) {
$ret++;
}
if (strpos($content, '/* END MODULEBUILDER TOPMENU MYOBJECT */') !== false) {
$ret++;
}
if (strpos($content, '/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !== false) {
$ret++;
}
if (strpos($content, '/* END MODULEBUILDER LEFTMENU MYOBJECT */') !== false) {
$ret++;
}
if ($ret == 4) {
return 1;
}
} elseif ($number === 1) {

View File

@@ -374,7 +374,7 @@ WarningModuleNeedRefresh = Module <b>%s</b> has been disabled. Don't forget to e
WarningPermissionAlreadyExist=Existing permissions for this object
WarningGoOnAccountancySetupToAddAccounts=If this list is empty, go into menu %s - %s - %s to load or create accounts for your chart of account.
WarningCorrectedInvoiceNotFound=Corrected invoice not found
WarningCommentNotFound=Please check placement of start and end comments for <b>%s</b> section in file <b>%s</b> before submitting your action
WarningCommentNotFound=Warning: Can't find the start and/or end comments for the section <b>%s</b> into the file <b>%s</b>
WarningAlreadyReverse=Stock movement already reversed
SwissQrOnlyVIR = SwissQR invoice can only be added on invoices set to be paid with credit transfer payments.

View File

@@ -1017,6 +1017,8 @@ if ($dirins && $action == 'confirm_removefile' && !empty($module)) {
// Init an object
if ($dirins && $action == 'initobject' && $module && $objectname) {
$warning = 0;
$objectname = ucfirst($objectname);
$dirins = $dirread = $listofmodules[strtolower($module)]['moduledescriptorrootpath'];
@@ -1365,13 +1367,12 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
}
}
if (!$error) {
foreach ($filetogenerate as $srcfile => $destfile) {
$result = dol_copy($srcdir.'/'.$srcfile, $destdir.'/'.$destfile, $newmask, 0);
if ($result <= 0) {
if ($result < 0) {
$error++;
$warning++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorFailToCopyFile", $srcdir.'/'.$srcfile, $destdir.'/'.$destfile), null, 'errors');
} else {
@@ -1383,6 +1384,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
'/myobject\.class\.php/' => strtolower($objectname).'.class.php',
'/myobject\.lib\.php/' => strtolower($objectname).'.lib.php',
);
dolReplaceInFile($destdir.'/'.$destfile, $arrayreplacement, '', 0, 0, 1);
}
}
@@ -1529,8 +1531,8 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
if (!$counter) {
$checkComment = checkExistComment($moduledescriptorfile, 0);
if ($checkComment < 0) {
$error++;
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), "mod".$module."class.php"), null, 'warnings');
$warning++;
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), basename($moduledescriptorfile)), null, 'warnings');
} else {
$arrayofreplacement = array('/* END MODULEBUILDER LEFTMENU MYOBJECT */' => '/*LEFTMENU '.strtoupper($objectname).'*/'.$stringtoadd."\n\t\t".'/*END LEFTMENU '.strtoupper($objectname).'*/'."\n\t\t".'/* END MODULEBUILDER LEFTMENU MYOBJECT */');
dolReplaceInFile($moduledescriptorfile, $arrayofreplacement);
@@ -1580,7 +1582,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
if (is_numeric($object) && $object <= 0) {
$pathoffiletoeditsrc = $destdir.'/class/'.strtolower($objectname).'.class.php';
setEventMessages($langs->trans('ErrorFailToCreateFile', $pathoffiletoeditsrc), null, 'errors');
$error++;
$warning++;
}
// check if documentation was generate and add table of properties object
$file = $destdir.'/class/'.strtolower($objectname).'.class.php';
@@ -1610,7 +1612,7 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
// check if module is enabled
if (isModEnabled(strtolower($module))) {
$result = unActivateModule(strtolower($module));
dolibarr_set_const($db, "MAIN_IHM_PARAMS_REV", (int) $conf->global->MAIN_IHM_PARAMS_REV + 1, 'chaine', 0, '', $conf->entity);
dolibarr_set_const($db, "MAIN_IHM_PARAMS_REV", getDolGlobalInt('MAIN_IHM_PARAMS_REV') + 1, 'chaine', 0, '', $conf->entity);
if ($result) {
setEventMessages($result, null, 'errors');
}
@@ -1898,11 +1900,12 @@ if ($dirins && $action == 'confirm_deletemodule') {
} else {
$error++;
$langs->load("errors");
dol_print_error($db, $langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module));
exit;
setEventMessages($langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module), null, 'warnings');
}
$moduleobj->remove();
if ($moduleobj) {
$moduleobj->remove();
}
$result = dol_delete_dir_recursive($dir);

View File

@@ -28,6 +28,7 @@
*/
include_once DOL_DOCUMENT_ROOT.'/core/modules/DolibarrModules.class.php';
/**
* Description and activation class for module MyModule
*/
@@ -303,8 +304,10 @@ class modMyModule extends DolibarrModules
// Main menu entries to add
$this->menu = array();
$r = 0;
// Add here entries to declare new menus
/* BEGIN MODULEBUILDER TOPMENU */
/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */
$this->menu[$r++] = array(
'fk_menu'=>'', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
'type'=>'top', // This is a Top menu entry
@@ -320,7 +323,8 @@ class modMyModule extends DolibarrModules
'target'=>'',
'user'=>2, // 0=Menu for internal users, 1=external users, 2=both
);
/* END MODULEBUILDER TOPMENU */
/* END MODULEBUILDER TOPMENU MYOBJECT */
/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */
/*$this->menu[$r++]=array(
'fk_menu'=>'fk_mainmenu=mymodule', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
@@ -366,6 +370,8 @@ class modMyModule extends DolibarrModules
'user'=>2, // 0=Menu for internal users, 1=external users, 2=both
);*/
/* END MODULEBUILDER LEFTMENU MYOBJECT */
// Exports profiles provided by this module
$r = 1;
/* BEGIN MODULEBUILDER EXPORT MYOBJECT */