From 650bb54051a9c516c4b25b7350d9faaffadcd4b7 Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Tue, 8 Sep 2020 10:07:34 +0200 Subject: [PATCH] add common list function for module overview --- htdocs/admin/system/modules.php | 344 +++++++++++++++++++++++++++----- 1 file changed, 293 insertions(+), 51 deletions(-) diff --git a/htdocs/admin/system/modules.php b/htdocs/admin/system/modules.php index 1e5de3b9a46..bba227f9acc 100644 --- a/htdocs/admin/system/modules.php +++ b/htdocs/admin/system/modules.php @@ -25,32 +25,64 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; -// Load translation files required by the page -$langs->loadLangs(array("install", "other", "admin")); - if (!$user->admin) accessforbidden(); +// Load translation files required by the page +$langs->loadLangs(array("install", "other", "admin")); + +$optioncss = GETPOST('optioncss', 'alpha'); +$contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'moduleoverview'; + +$search_name = GETPOST("search_name", 'alpha'); +$search_id = GETPOST("search_id", 'alpha'); +$search_version = GETPOST("search_version", 'alpha'); +$search_permission = GETPOST("search_permission", 'alpha'); + +$sortfield = GETPOST("sortfield", 'alpha'); +$sortorder = GETPOST("sortorder", 'alpha'); + +if (!$sortfield) $sortfield = "id"; +if (!$sortorder) $sortorder = "asc"; + +// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array of hooks +$hookmanager->initHooks(array('moduleoverview')); +$form = new Form($db); +$object = new stdClass(); + +// Definition of fields for lists +$arrayfields = array( + 'name'=>array('label'=>$langs->trans("Modules"), 'checked'=>1, 'position'=>10), + 'version'=>array('label'=>$langs->trans("Version"), 'checked'=>1, 'position'=>20), + 'id'=>array('label'=>$langs->trans("IdModule"), 'checked'=>1, 'position'=>30), + 'permission'=>array('label'=>$langs->trans("IdPermissions"), 'checked'=>1, 'position'=>40) +); + +$arrayfields = dol_sort_array($arrayfields, 'position'); /* - * View + * Actions */ -llxHeader(); +$parameters = array(); +$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks +if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); -print load_fiche_titre($langs->trans("AvailableModules"), '', 'title_setup'); +if (empty($reshook)) +{ + // Selection of new fields + include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php'; +} -print ''.$langs->trans("ToActivateModule").'
'; -print "
\n"; +// Load list of modules +$moduleList = array(); $modules = array(); -$modules_names = array(); $modules_files = array(); $modules_fullpath = array(); $modulesdir = dolGetModulesDirs(); +$rights_ids = array(); -// Load list of modules -$i = 0; foreach ($modulesdir as $dir) { $handle = @opendir(dol_osencode($dir)); @@ -69,7 +101,9 @@ foreach ($modulesdir as $dir) { // File duplicate print "Warning duplicate file found : ".$file." (Found ".$dir.$file.", already found ".$modules_fullpath[$file].")
"; - } else { + } + else + { // File to load $res = include_once $dir.$file; if (class_exists($modName)) @@ -78,15 +112,16 @@ foreach ($modulesdir as $dir) $objMod = new $modName($db); $modules[$objMod->numero] = $objMod; - $modules_names[$objMod->numero] = $objMod->name; $modules_files[$objMod->numero] = $file; $modules_fullpath[$file] = $dir.$file; - $picto[$objMod->numero] = (isset($objMod->picto) && $objMod->picto) ? $objMod->picto : 'generic'; - } catch (Exception $e) + } + catch (Exception $e) { dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR); } - } else { + } + else + { print "Warning bad descriptor file : ".$dir.$file." (Class ".$modName." not found into file)
"; } } @@ -97,60 +132,267 @@ foreach ($modulesdir as $dir) } } -print '
'; -print ''; -print ''; -print ''; -print ''; -print ''; -print ''; -print ''; -$var = false; -$sortorder = $modules_names; -ksort($sortorder); -$rights_ids = array(); -foreach ($sortorder as $numero=>$name) +// create pre-filtered list for modules +foreach ($modules as $key=>$module) { - $idperms = ""; - // Module - print '"; - // Version - print ''; - // Id - print ''; - // Permissions - if ($modules[$numero]->rights) + else { - foreach ($modules[$numero]->rights as $rights) + $newModule->picto = img_object($alt, 'generic', 'width="14px"'); + } + + $permission = array(); + if ($module->rights) + { + foreach ($module->rights as $rights) { - $idperms .= ($idperms ? ", " : "").$rights[0]; + if (empty($rights[0])) + { + continue; + } + + $permission[] = $rights[0]; + array_push($rights_ids, $rights[0]); } } - print ''; - print "\n"; + + $newModule->permission = $permission; + + // pre-filter list + if($search_name && !stristr($newModule->name, $search_name)) continue; + if($search_version && !stristr($newModule->version, $search_version)) continue; + if($search_id && !stristr($newModule->id, $search_id)) continue; + + if($search_permission) + { + $found = false; + + foreach($newModule->permission as $permission) + { + if(stristr($permission, $search_permission)) + { + $found = true; + break; + } + } + + if(!$found) continue; + } + + $moduleList[] = $newModule; } + + + +/* + * View + */ + +llxHeader(); + +print ''; +if ($optioncss != '') print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +print_barre_liste($langs->trans("AvailableModules"), $page, $_SERVER["PHP_SELF"], '', $sortfield, $sortorder, $massactionbutton, -1, '', 'title_setup', 0, '', '', 0, 1, 1); + +print ''.$langs->trans("ToActivateModule").''; +print '
'; +print '
'; + +$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage; +$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields + +print '
'; +print '
'.$langs->trans("Modules").''.$langs->trans("Version").''.$langs->trans("IdModule").''.$langs->trans("IdPermissions").'
'; - $alt = $name.' - '.$modules_files[$numero]; - if (!empty($picto[$numero])) + $newModule = new stdClass(); + + $newModule->name = $module->getName(); + $newModule->version = $module->getVersion(); + $newModule->id = $key; + + $alt = $module->name.' - '.$modules_files[$key]; + + if (!empty($module->picto)) { - if (preg_match('/^\//', $picto[$numero])) print img_picto($alt, $picto[$numero], 'width="14px"', 1); - else print img_object($alt, $picto[$numero], 'width="14px"'); - } else { - print img_object($alt, $picto[$numero], 'width="14px"'); + if (preg_match('/^\//', $module->picto)) $newModule->picto = img_picto($alt, $module->picto, 'width="14px"', 1); + else $newModule->picto = img_object($alt, $module->picto, 'width="14px"'); } - print ' '.$modules[$numero]->getName(); - print "'.$modules[$numero]->getVersion().''.$numero.''.($idperms ? $idperms : " ").'
'; + +// Lines with input filters +print ''; + +if($arrayfields['name']['checked']) +{ + print ''; +} +if($arrayfields['version']['checked']) +{ + print ''; +} +if($arrayfields['id']['checked']) +{ + print ''; +} +if($arrayfields['permission']['checked']) +{ + print ''; +} + +print ''; + +print ''; + +print ''; + +if($arrayfields['name']['checked']) +{ + print_liste_field_titre($arrayfields['name']['label'], $_SERVER["PHP_SELF"], "name", "", "", "", $sortfield, $sortorder); +} +if($arrayfields['version']['checked']) +{ + print_liste_field_titre($arrayfields['version']['label'], $_SERVER["PHP_SELF"], "version", "", "", "", $sortfield, $sortorder); +} +if($arrayfields['id']['checked']) +{ + print_liste_field_titre($arrayfields['id']['label'], $_SERVER["PHP_SELF"], "id", "", "", "", $sortfield, $sortorder); +} +if($arrayfields['permission']['checked']) +{ + print_liste_field_titre($arrayfields['permission']['label'], $_SERVER["PHP_SELF"], "permission", "", "", "", $sortfield, $sortorder); +} + +// Fields from hook +$parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder); +$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook +print $hookmanager->resPrint; + +print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch '); +print ''; + +// sort list +if($sortfield == "name" && $sortorder == "asc") usort($moduleList, function(stdClass $a, stdClass $b) { return strcasecmp($a->name, $b->name); }); +if($sortfield == "name" && $sortorder == "desc") usort($moduleList, function(stdClass $a, stdClass $b) { return strcasecmp($b->name, $a->name); }); +if($sortfield == "version" && $sortorder == "asc") usort($moduleList, function (stdClass $a, stdClass $b) { return strcasecmp($a->version, $b->version); }); +if($sortfield == "version" && $sortorder == "desc") usort($moduleList, function(stdClass $a, stdClass $b) { return strcasecmp($b->version, $a->version); }); +if($sortfield == "id" && $sortorder == "asc") usort($moduleList, "sortIdAsc"); +if($sortfield == "id" && $sortorder == "desc") usort($moduleList, "sortIdDesc"); +if($sortfield == "permission" && $sortorder == "asc") usort($moduleList, "sortPermissionIdsAsc"); +if($sortfield == "permission" && $sortorder == "desc") usort($moduleList, "sortPermissionIdsDesc"); + +foreach ($moduleList as $module) +{ + print ''; + + if($arrayfields['name']['checked']) + { + print '"; + } + + if($arrayfields['version']['checked']) + { + print ''; + } + + if($arrayfields['id']['checked']) + { + print ''; + } + + if($arrayfields['permission']['checked']) + { + $idperms = ''; + foreach ($module->permission as $permission) + { + $idperms .= ($idperms ? ", " : "").$permission; + + $translationKey = "Permission".$permission; + if(empty($langs->tab_translate[$translationKey])) + { + $tooltip = 'Missing translation (key '.$translation.' not found in admin.lang)'; + $idperms .= ' Warning'; + } + } + + print ''; + } + + print ''; + print ''; +} + print '
'; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; +$searchpicto = $form->showFilterButtons(); +print $searchpicto; +print '
'; + print $module->picto; + print ' '.$module->name; + print "'.$module->version.''.$module->id.''.($idperms ? $idperms : " ").'
'; print '
'; +print ''; print '
'; + sort($rights_ids); $old = ''; + foreach ($rights_ids as $right_id) { - if ($old == $right_id) print "Warning duplicate id on permission : ".$right_id."
"; + if ($old == $right_id) + { + print "Warning duplicate id on permission : ".$right_id."
"; + } + $old = $right_id; } // End of page llxFooter(); $db->close(); + + +/* + * user-defined sort functions + */ + +function sortIdAsc(stdClass $a, stdClass $b) +{ + return $a->id > $b->id ? -1 : 1; +} + +function sortIdDesc(stdClass $a, stdClass $b) +{ + return $b->id > $a->id ? -1 : 1; +} + +function sortPermissionIdsAsc(stdClass $a, stdClass $b) +{ + if (empty($a->permission) && empty($b->permission)) return sortIdAsc($a, $b); + + if (empty($a->permission)) return 1; + if (empty($b->permission)) return -1; + + return $a->permission[0] > $b->permission[0] ? -1 : 1; +} + +function sortPermissionIdsDesc(stdClass $a, stdClass $b) +{ + if (empty($a->permission) && empty($b->permission)) return sortIdDesc($a, $b); + + if (empty($a->permission)) return -1; + if (empty($b->permission)) return 1; + + return $a->permission[0] > $b->permission[0] ? 1 : -1; +}