From 3659b38bf977596b894b03104b2950c6b3b43ede Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Mon, 13 Mar 2023 12:39:28 +0100 Subject: [PATCH 1/3] add function for listiong objects from directory --- htdocs/core/lib/modulebuilder.lib.php | 31 ++++++++++++ htdocs/modulebuilder/index.php | 69 ++------------------------- 2 files changed, 35 insertions(+), 65 deletions(-) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index a60e9a3a78a..3fd5a406d25 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -416,3 +416,34 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = ' return $error ? -1 : 1; } + +/** + * Get list of existing objects from directory + * @param string $destdir Directory + * @return Array|int <=0 if KO, array if OK + */ +function dolGetListOfObjectclasses($destdir) +{ + $objects = array(); + $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); + foreach ($listofobject as $fileobj) { + if (preg_match('/^api_/', $fileobj['name'])) { + continue; + } + if (preg_match('/^actions_/', $fileobj['name'])) { + continue; + } + + $tmpcontent = file_get_contents($fileobj['fullname']); + $reg = array(); + if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { + $objectnameloop = $reg[1]; + $objects[$fileobj['fullname']] = $objectnameloop; + } + } + if (count($objects)>0) { + return $objects; + } else { + return -1; + } +} diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 04d0f290732..1913954bb7b 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -405,23 +405,8 @@ if ($dirins && in_array($action, array('initapi', 'initphpunit', 'initpagecontac $destdir = $dirins.'/'.strtolower($module); // Get list of existing objects - $objects = array(); - $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); - foreach ($listofobject as $fileobj) { - if (preg_match('/^api_/', $fileobj['name'])) { - continue; - } - if (preg_match('/^actions_/', $fileobj['name'])) { - continue; - } + $objects = dolGetListOfObjectclasses($destdir); - $tmpcontent = file_get_contents($fileobj['fullname']); - $reg = array(); - if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { - $objectnameloop = $reg[1]; - $objects[$fileobj['fullname']] = $objectnameloop; - } - } if ($action == 'initapi') { if (file_exists($dirins.'/'.strtolower($module).'/class/api_'.strtolower($module).'.class.php')) { @@ -942,24 +927,8 @@ if ($dirins && $action == 'confirm_removefile' && !empty($module)) { $relativefilename = dol_sanitizePathName(GETPOST('file', 'restricthtml')); // Get list of existing objects - // TODO ALI This part of code is common at several places and is autonomous. So replace it with $objects = dolGetListOfObjectclasses($destdir); - $objects = array(); - $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); - foreach ($listofobject as $fileobj) { - if (preg_match('/^api_/', $fileobj['name'])) { - continue; - } - if (preg_match('/^actions_/', $fileobj['name'])) { - continue; - } + $objects = dolGetListOfObjectclasses($destdir); - $tmpcontent = file_get_contents($fileobj['fullname']); - $reg = array(); - if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { - $objectnameloop = $reg[1]; - $objects[$fileobj['fullname']] = $objectnameloop; - } - } // Now we delete the file if ($relativefilename) { @@ -2652,25 +2621,10 @@ if ($dirins && $action == 'addmenu' && empty($cancel)) { $error = 0; $dirins = $listofmodules[strtolower($module)]['moduledescriptorrootpath']; $destdir = $dirins.'/'.strtolower($module); - $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); // Get list of existing objets - $objects = array(); - foreach ($listofobject as $fileobj) { - if (preg_match('/^api_/', $fileobj['name'])) { - continue; - } - if (preg_match('/^actions_/', $fileobj['name'])) { - continue; - } + $objects = dolGetListOfObjectclasses($destdir); - $tmpcontent = file_get_contents($fileobj['fullname']); - $reg = array(); - if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { - $objectnameloop = $reg[1]; - $objects[$fileobj['fullname']] = $objectnameloop; - } - } // load class and check if right exist $pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath']; @@ -4614,25 +4568,10 @@ if ($module == 'initmodule') { $pathtofile = $listofmodules[strtolower($module)]['moduledescriptorrelpath']; $dirins = $listofmodules[strtolower($module)]['moduledescriptorrootpath']; $destdir = $dirins.'/'.strtolower($module); - $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$'); // Get list of existing objects - $objects = array(); - foreach ($listofobject as $fileobj) { - if (preg_match('/^api_/', $fileobj['name'])) { - continue; - } - if (preg_match('/^actions_/', $fileobj['name'])) { - continue; - } + $objects = dolGetListOfObjectclasses($destdir); - $tmpcontent = file_get_contents($fileobj['fullname']); - $reg = array(); - if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) { - $objectnameloop = $reg[1]; - $objects[$fileobj['fullname']] = $objectnameloop; - } - } $menus = $moduleobj->menu; if ($action != 'editfile' || empty($file)) { From f7c867f20887bc40055959ff875f943425d57c0d Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Mon, 13 Mar 2023 15:16:06 +0100 Subject: [PATCH 2/3] fix header s problemes --- htdocs/modulebuilder/index.php | 77 ++++++++++++++++------------------ 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index 1913954bb7b..4dcd25f2e15 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -988,20 +988,6 @@ if ($dirins && $action == 'confirm_removefile' && !empty($module)) { // Init an object 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); - if ($result) { - setEventMessages($result, null, 'errors'); - } else { - /* FIX ALI header must be after action. Always add an exit after a header. - header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module); - */ - setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); - } - } - $objectname = ucfirst($objectname); $dirins = $dirread = $listofmodules[strtolower($module)]['moduledescriptorrootpath']; @@ -1609,6 +1595,18 @@ if ($dirins && $action == 'initobject' && $module && $objectname) { } else { $tabobj = 'newobject'; } + + // 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); + if ($result) { + setEventMessages($result, null, 'errors'); + } + setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); + header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=objects&module='.$module); + exit; + } } // Add a dictionary @@ -1871,20 +1869,6 @@ if ($dirins && $action == 'confirm_deletemodule') { } if ($dirins && $action == 'confirm_deleteobject' && $objectname) { - // check if module is enabled (if it's disabled and send msg event) - 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); - if ($result) { - setEventMessages($result, null, 'errors'); - $error++; - } else { - /* TODO ALI Header redirect must be at end after actions. Also tab=pemrissions looks strange - header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module); - */ - setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); - } - } if (preg_match('/[^a-z0-9_]/i', $objectname)) { $error++; setEventMessages($langs->trans("SpaceOrSpecialCharAreNotAllowed"), null, 'errors'); @@ -1981,16 +1965,7 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) { "; $deleteright = dolReplaceInFile($moduledescriptorfile, array('/*'.strtoupper($objectname).'*/' => '', $rights => '', "/*END ".strtoupper($objectname).'*/'."\n\t\t" => "\n\t\t")); - if ($deleteright > 0) { - if (isModEnabled(strtolower($module))) { - $result = unActivateModule(strtolower($module)); - if ($result) { - setEventMessages($result, null, 'errors'); - } - setEventMessages($langs->trans("WarningModuleNeedRefrech", $langs->transnoentities($module)), null, 'warnings'); - header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?index.php?tab=description&module='.$module); - } - } + $resultko = 0; foreach ($filetodelete as $tmpfiletodelete) { $resulttmp = dol_delete_file($dir.'/'.$tmpfiletodelete, 0, 0, 1); @@ -2009,6 +1984,18 @@ if ($dirins && $action == 'confirm_deleteobject' && $objectname) { $action = ''; $tabobj = 'deleteobject'; + + // 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); + if ($result) { + setEventMessages($result, null, 'errors'); + } + setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); + header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=objects&module='.$module); + exit; + } } if ($dirins && $action == 'generatedoc') { @@ -2173,7 +2160,6 @@ if ($dirins && $action == 'addright' && !empty($module) && empty($cancel)) { if ($result) { setEventMessages($result, null, 'errors'); } - header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module); setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); } //prepare stirng to add @@ -2319,7 +2305,6 @@ if ($dirins && GETPOST('action') == 'update_right' && GETPOST('modifyright')&& e if ($result) { setEventMessages($result, null, 'errors'); } - header("Location: ".DOL_URL_ROOT.'/modulebuilder/index.php?tab=permissions&module='.$module); setEventMessages($langs->trans('WarningModuleNeedRefrech', $langs->transnoentities($module)), null, 'warnings'); } @@ -4574,6 +4559,18 @@ if ($module == 'initmodule') { $menus = $moduleobj->menu; + if ($action == 'deletemenu') { + $formconfirms = $form->formconfirm( + $_SERVER["PHP_SELF"].'?menukey='.urlencode(GETPOST('menukey', 'int')).'&tab='.urlencode($tab).'&module='.urlencode($module), + $langs->trans('Delete'), + $langs->trans('Confirm Delete Menu', GETPOST('menukey', 'int')), + 'confirm_deletemenu', + '', + 0, + 1 + ); + print $formconfirms; + } if ($action != 'editfile' || empty($file)) { print ''; $htmlhelp = $langs->trans("MenusDefDescTooltip", '{s1}'); From bbcdc3ad2091a63618744cb6d3b78285b25f77e4 Mon Sep 17 00:00:00 2001 From: Lamrani Abdel Date: Tue, 14 Mar 2023 14:48:48 +0100 Subject: [PATCH 3/3] rename a function --- htdocs/core/lib/modulebuilder.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/modulebuilder.lib.php b/htdocs/core/lib/modulebuilder.lib.php index 3fd5a406d25..506ee4697ed 100644 --- a/htdocs/core/lib/modulebuilder.lib.php +++ b/htdocs/core/lib/modulebuilder.lib.php @@ -422,7 +422,7 @@ function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = ' * @param string $destdir Directory * @return Array|int <=0 if KO, array if OK */ -function dolGetListOfObjectclasses($destdir) +function dolGetListOfObjectClasses($destdir) { $objects = array(); $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$');