Merge pull request #25805 from lamrani002/checkBeforereWrite

NEW functionality for Checkin comments begin and end before each actions
This commit is contained in:
Laurent Destailleur
2023-09-05 23:39:58 +02:00
committed by GitHub
4 changed files with 198 additions and 111 deletions

View File

@@ -448,7 +448,34 @@ 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 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) {
return 1;
}
} elseif ($number === 1) {
if (strpos($content, '/* BEGIN MODULEBUILDER PERMISSIONS */') !== false && strpos($content, '/* END MODULEBUILDER PERMISSIONS */') !== false) {
return 1;
}
} elseif ($number == 2) {
if (strpos($content, '/* BEGIN MODULEBUILDER DICTIONARIES */') !== false && strpos($content, '/* END MODULEBUILDER DICTIONARIES */') !== false) {
return 1;
}
}
return -1;
}
/**
* Delete all permissions
*

View File

@@ -363,6 +363,7 @@ WarningModuleNeedRefrech = 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
SwissQrOnlyVIR = SwissQR invoice can only be added on invoices set to be paid with credit transfer payments.
SwissQrCreditorAddressInvalid = Creditor address is invalid (are ZIP and city set? (%s)

View File

@@ -363,6 +363,7 @@ WarningModuleNeedRefrech = Le module <b>%s</b> a été désactivé. N'oubliez pa
WarningPermissionAlreadyExist=Autorisations existantes pour cet objet
WarningGoOnAccountancySetupToAddAccounts=Si cette liste est vide, allez dans le menu %s - %s - %s pour charger ou créer des comptes pour votre plan comptable.
WarningCorrectedInvoiceNotFound=Facture corrigée introuvable
WarningCommentNotFound=Verifier l'emplacement des commentaires debut et fin pour la section <b>%s</b> dans le fichier <b>%s</b> avant de soumettre votre action
SwissQrOnlyVIR = La facture SwissQR ne peut être ajoutée que sur les factures définies pour être payées avec des paiements par virement.
SwissQrCreditorAddressInvalid = L'adresse du créancier n'est pas valide (le code postal et la ville sont-ils définis ? (%s)

View File

@@ -1290,12 +1290,16 @@ if ($dirins && $action == 'initobject' && $module && $objectname) {
}
$rights = $moduleobj->rights;
$moduledescriptorfile = $destdir.'/core/modules/mod'.$module.'.class.php';
$checkComment=checkExistComment($moduledescriptorfile, 1);
if ($checkComment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Permissions"), "mod".$module."class.php"), null, 'warnings');
} else {
$generatePerms = reWriteAllPermissions($moduledescriptorfile, $rights, null, null, $objectname, $module, -2);
if ($generatePerms < 0) {
setEventMessages($langs->trans("WarningPermissionAlreadyExist", $langs->transnoentities($objectname)), null, 'warnings');
}
}
}
if (!$error) {
@@ -1459,8 +1463,14 @@ 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');
} else {
dolReplaceInFile($moduledescriptorfile, array('/* END MODULEBUILDER LEFTMENU MYOBJECT */' => '/*LEFTMENU '.strtoupper($objectname).'*/'.$stringtoadd."\n\t\t".'/*END LEFTMENU '.strtoupper($objectname).'*/'."\n\t\t".'/* END MODULEBUILDER LEFTMENU MYOBJECT */'));
}
}
// Add module descriptor to list of files to replace "MyObject' string with real name of object.
$filetogenerate[] = 'core/modules/mod'.$module.'.class.php';
}
@@ -1585,6 +1595,10 @@ if ($dirins && $action == 'initdic' && $module && $dicname) {
exit;
}
$dictionaries = $moduleobj->dictionaries;
$checkComment = checkExistComment($moduledescriptorfile, 2);
if ($checkComment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Dictionaries"), "mod".$module."class.php"), null, 'warnings');
} else {
createNewDictionnary($module, $moduledescriptorfile, $newdicname, $dictionaries);
if (function_exists('opcache_invalidate')) {
opcache_reset(); // remove the include cache hell !
@@ -1594,6 +1608,7 @@ if ($dirins && $action == 'initdic' && $module && $dicname) {
exit;
}
}
}
// Delete a SQL table
if ($dirins && ($action == 'droptable' || $action == 'droptableextrafields') && !empty($module) && !empty($tabobj)) {
@@ -1895,17 +1910,26 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) {
// delete menus linked to the object
$menus = $moduleobj->menu;
$rewriteMenu = checkExistComment($moduledescriptorfile, 0);
if ($rewriteMenu < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllMenus($moduledescriptorfile, $menus, $objectname, null, -1);
}
// regenerate permissions and delete them
$permissions = $moduleobj->rights;
$rewritePerms = checkExistComment($moduledescriptorfile, 1);
if ($rewritePerms < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Permissions"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllPermissions($moduledescriptorfile, $permissions, null, null, $objectname, '', -1);
}
if ($rewritePerms && $rewriteMenu) {
// check if documentation has been generated
$file_doc = $dirins.'/'.strtolower($module).'/doc/Documentation.asciidoc';
if (file_exists($file_doc)) {
deletePropsAndPermsFromDoc($file_doc, $objectname);
}
clearstatcache(true);
if (function_exists('opcache_invalidate')) {
@@ -1926,6 +1950,7 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) {
setEventMessages($langs->trans("ErrorSomeFilesCouldNotBeDeleted"), null, 'warnings');
}
}
}
$action = '';
$tabobj = 'deleteobject';
@@ -1978,6 +2003,11 @@ if (($dirins && $action == 'confirm_deletedictionary' && $dicname) || ($dirins &
}
$dicts = $moduleobj->dictionaries;
$checkComment = checkExistComment($moduledescriptorfile, 2);
if ($checkComment < 0) {
$error++;
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Dictionaries"), "mod".$module."class.php"), null, 'warnings');
}
if (!empty(GETPOST('dictionnarykey'))) {
$newdicname = $dicts['tabname'][GETPOST('dictionnarykey')-1];
@@ -2021,7 +2051,9 @@ if (($dirins && $action == 'confirm_deletedictionary' && $dicname) || ($dirins &
$result = updateDictionaryInFile($module, $moduledescriptorfile, $dicts);
if ($result > 0) {
setEventMessages($langs->trans("DictionaryDeleted", ucfirst(substr($newdicname, 2))), null);
}
} elseif (!$result) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Dictionaries"), "mod".$module."class.php"), null, 'warnings');
} else {
if (function_exists('opcache_invalidate')) {
opcache_reset(); // remove the include cache hell !
}
@@ -2030,6 +2062,7 @@ if (($dirins && $action == 'confirm_deletedictionary' && $dicname) || ($dirins &
exit;
}
}
}
if ($dirins && $action == 'updatedictionary' && GETPOST('dictionnarykey')) {
$keydict = GETPOST('dictionnarykey') - 1 ;
@@ -2056,6 +2089,10 @@ if ($dirins && $action == 'updatedictionary' && GETPOST('dictionnarykey')) {
$dicts = $moduleobj->dictionaries;
if (!empty(GETPOST('tablib')) && GETPOST('tablib') !== $dicts['tablib'][$keydict]) {
$dicts['tablib'][$keydict] = ucfirst(strtolower(GETPOST('tablib')));
$checkComment = checkExistComment($moduledescriptorfile, 2);
if ($checkComment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Dictionaries"), "mod".$module."class.php"), null, 'warnings');
} else {
$updateDict = updateDictionaryInFile($module, $moduledescriptorfile, $dicts);
if ($updateDict > 0) {
setEventMessages($langs->trans("DictionaryNameUpdated", ucfirst(GETPOST('tablib'))), null);
@@ -2067,6 +2104,7 @@ if ($dirins && $action == 'updatedictionary' && GETPOST('dictionnarykey')) {
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=dictionaries&module='.$module.($forceddirread ? '@'.$dirread : ''));
exit;
}
}
//var_dump(GETPOST('tablib'));exit;
}
if ($dirins && $action == 'generatedoc') {
@@ -2215,11 +2253,6 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
5=>$crud
];
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
//rewriting all permissions after add a right
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, '', '', 1);
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
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);
@@ -2229,15 +2262,23 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) {
setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings');
}
}
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
//rewriting all permissions after add a right
$rewrite = checkExistComment($moduledescriptorfile, 1);
if ($rewrite < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Permissions"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightToAdd, '', '', 1);
setEventMessages($langs->trans('PermissionAddedSuccesfuly'), null);
clearstatcache(true);
if (function_exists('opcache_invalidate')) {
opcache_reset(); // remove the include cache hell !
}
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
exit;
}
}
// Update permission
@@ -2330,19 +2371,21 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
// rewriting all permissions after update permission needed
$rewrite = checkExistComment($moduledescriptorfile, 1);
if ($rewrite < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Permissions"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, $rightUpdated, '', '', 2);
setEventMessages($langs->trans('PermissionUpdatedSuccesfuly'), null);
clearstatcache(true);
if (function_exists('opcache_invalidate')) {
opcache_reset(); // remove the include cache hell !
}
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
exit;
}
}
}
// Delete permission
if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('permskey', 'int')) {
$error = 0;
@@ -2363,11 +2406,6 @@ if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('p
$key = (int) GETPOST('permskey', 'int')-1;
if (!$error) {
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
// rewriting all permissions
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, null, '', '', 0);
// check if module is enabled
if (isModEnabled(strtolower($module))) {
$result = unActivateModule(strtolower($module));
@@ -2375,12 +2413,18 @@ if ($dirins && $action == 'confirm_deleteright' && !empty($module) && GETPOST('p
if ($result) {
setEventMessages($result, null, 'errors');
}
setEventMessages($langs->trans('PermissionDeletedSuccesfuly'), null);
setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings');
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module);
exit;
}
// rewriting all permissions
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
$rewrite = checkExistComment($moduledescriptorfile, 1);
if ($rewrite < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Permissions"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllPermissions($moduledescriptorfile, $permissions, $key, null, '', '', 0);
setEventMessages($langs->trans('PermissionDeletedSuccesfuly'), null);
clearstatcache(true);
@@ -2538,6 +2582,10 @@ if ($dirins && $action == 'confirm_deletemenu' && GETPOST('menukey', 'int')) {
$key = (int) GETPOST('menukey', 'int');
$moduledescriptorfile = $dirins.'/'.strtolower($module).'/core/modules/mod'.$module.'.class.php';
$checkcomment = checkExistComment($moduledescriptorfile, 0);
if ($checkcomment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), "mod".$module."class.php"), null, 'warnings');
} else {
if ($menus[$key]['fk_menu'] === 'fk_mainmenu='.strtolower($module)) {
if (in_array(strtolower($menus[$key]['leftmenu']), $result)) {
reWriteAllMenus($moduledescriptorfile, $menus, $menus[$key]['leftmenu'], $key, -1);
@@ -2557,6 +2605,7 @@ if ($dirins && $action == 'confirm_deletemenu' && GETPOST('menukey', 'int')) {
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=menus&module='.$module);
exit;
}
}
// Add menu in module without initial object
if ($dirins && $action == 'addmenu' && empty($cancel)) {
@@ -2684,8 +2733,11 @@ if ($dirins && $action == 'addmenu' && empty($cancel)) {
$menuToAdd['perms'] = '1';
}
$result = reWriteAllMenus($moduledescriptorfile, $menus, $menuToAdd, null, 1);
$checkcomment = checkExistComment($moduledescriptorfile, 0);
if ($checkcomment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), "mod".$module."class.php"), null, 'warnings');
} else {
reWriteAllMenus($moduledescriptorfile, $menus, $menuToAdd, null, 1);
clearstatcache(true);
if (function_exists('opcache_invalidate')) {
opcache_reset();
@@ -2695,6 +2747,7 @@ if ($dirins && $action == 'addmenu' && empty($cancel)) {
exit;
}
}
}
// modify a menu
if ($dirins && $action == "modify_menu" && GETPOST('menukey', 'int') && GETPOST('tabobj')) {
@@ -2771,6 +2824,10 @@ if ($dirins && $action == "modify_menu" && GETPOST('menukey', 'int') && GETPOST(
}
if (!$error) {
//update menu
$checkComment = checkExistComment($moduledescriptorfile, 0);
if ($checkComment < 0) {
setEventMessages($langs->trans("WarningCommentNotFound", $langs->trans("Menus"), "mod".$module."class.php"), null, 'warnings');
} else {
$result = reWriteAllMenus($moduledescriptorfile, $menus, $menuModify, $key, 2);
clearstatcache(true);
@@ -2786,6 +2843,7 @@ if ($dirins && $action == "modify_menu" && GETPOST('menukey', 'int') && GETPOST(
header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=menus&module='.$module);
exit;
}
}
} else {
$_POST['type'] = '';
$_POST['titre'] = '';