From 20638f430a9db2e5ba8d9f77ca9ed3ca5619d399 Mon Sep 17 00:00:00 2001 From: Gabriel Pedro Date: Mon, 28 Nov 2022 13:05:08 -0400 Subject: [PATCH] feat: add hook admin menu (#208) * feat: add hook admin menu * fix: removing menu sort * feat: menu order property * fix: menu order value * feat: add main menu order --- admin/template/menus.php | 70 ++++++++++++++++++++++++---------------- system/hooks.php | 3 +- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/admin/template/menus.php b/admin/template/menus.php index 12ffb85c..80ed3b48 100644 --- a/admin/template/menus.php +++ b/admin/template/menus.php @@ -1,48 +1,64 @@ 'Dashboard', 'icon' => 'tachometer-alt', 'link' => 'dashboard'], - ['name' => 'News', 'icon' => 'newspaper', 'link' => +$menus = [ + ['name' => 'Dashboard', 'icon' => 'tachometer-alt', 'order' => 10, 'link' => 'dashboard'], + ['name' => 'News', 'icon' => 'newspaper', 'order' => 20, 'link' => [ - ['name' => 'View', 'link' => 'news'], - ['name' => 'Add news', 'link' => 'news&action=new&type=1'], - ['name' => 'Add ticker', 'link' => 'news&action=new&type=2'], - ['name' => 'Add article', 'link' => 'news&action=new&type=3'], + ['name' => 'View', 'link' => 'news', 'order' => 10], + ['name' => 'Add news', 'link' => 'news&action=new&type=1', 'order' => 20], + ['name' => 'Add ticker', 'link' => 'news&action=new&type=2', 'order' => 30], + ['name' => 'Add article', 'link' => 'news&action=new&type=3', 'order' => 40], ], ], - ['name' => 'Changelogs', 'icon' => 'newspaper', 'link' => + ['name' => 'Changelogs', 'icon' => 'newspaper', 'order' => 30, 'link' => [ - ['name' => 'View', 'link' => 'changelog'], - ['name' => 'Add', 'link' => 'changelog&action=new'], + ['name' => 'View', 'link' => 'changelog', 'order' => 10], + ['name' => 'Add', 'link' => 'changelog&action=new', 'order' => 20], ], ], - ['name' => 'Mailer', 'icon' => 'envelope', 'link' => 'mailer', 'disabled' => !config('mail_enabled')], - ['name' => 'Pages', 'icon' => 'book', 'link' => + ['name' => 'Mailer', 'icon' => 'envelope', 'order' => 40, 'link' => 'mailer', 'disabled' => !config('mail_enabled')], + ['name' => 'Pages', 'icon' => 'book', 'order' => 50, 'link' => [ - ['name' => 'View', 'link' => 'pages'], - ['name' => 'Add', 'link' => 'pages&action=new'], + ['name' => 'View', 'link' => 'pages', 'order' => 10], + ['name' => 'Add', 'link' => 'pages&action=new', 'order' => 20], ], ], - ['name' => 'Menus', 'icon' => 'list', 'link' => 'menus'], - ['name' => 'Plugins', 'icon' => 'plug', 'link' => 'plugins'], - ['name' => 'Server Data', 'icon' => 'gavel', 'link' => 'data'], - ['name' => 'Editor', 'icon' => 'edit', 'link' => + ['name' => 'Menus', 'icon' => 'list', 'order' => 60, 'link' => 'menus'], + ['name' => 'Plugins', 'icon' => 'plug', 'order' => 70, 'link' => 'plugins'], + ['name' => 'Server Data', 'icon' => 'gavel', 'order' => 80, 'link' => 'data'], + ['name' => 'Editor', 'icon' => 'edit', 'order' => 90, 'link' => [ - ['name' => 'Accounts', 'link' => 'accounts'], - ['name' => 'Players', 'link' => 'players'], + ['name' => 'Accounts', 'link' => 'accounts', 'order' => 10], + ['name' => 'Players', 'link' => 'players', 'order' => 20], ], ], - ['name' => 'Tools', 'icon' => 'tools', 'link' => + ['name' => 'Tools', 'icon' => 'tools', 'order' => 100, 'link' => [ - ['name' => 'Notepad', 'link' => 'notepad'], - ['name' => 'phpinfo', 'link' => 'phpinfo'], + ['name' => 'Notepad', 'link' => 'notepad', 'order' => 10], + ['name' => 'phpinfo', 'link' => 'phpinfo', 'order' => 20], ], ], - ['name' => 'Logs', 'icon' => 'bug', 'link' => + ['name' => 'Logs', 'icon' => 'bug', 'order' => 110, 'link' => [ - ['name' => 'Logs', 'link' => 'logs'], - ['name' => 'Reports', 'link' => 'reports'], - ['name' => 'Visitors', 'icon' => 'user', 'link' => 'visitors'], + ['name' => 'Logs', 'link' => 'logs', 'order' => 10], + ['name' => 'Reports', 'link' => 'reports', 'order' => 20], + ['name' => 'Visitors', 'icon' => 'user', 'link' => 'visitors', 'order' => 30], ], ], ]; + +$hooks->trigger(HOOK_ADMIN_MENU); + +usort($menus, function ($a, $b) { + return $a['order'] - $b['order']; +}); + +foreach ($menus as $i => $menu) { + if (isset($menu['link']) && is_array($menu['link'])) { + usort($menus[$i]['link'], function ($a, $b) { + return $a['order'] - $b['order']; + }); + } +} + +return $menus; diff --git a/system/hooks.php b/system/hooks.php index 089af581..ae9333c5 100644 --- a/system/hooks.php +++ b/system/hooks.php @@ -49,8 +49,9 @@ define('HOOK_ACCOUNT_CREATE_AFTER_TOWNS', ++$i); define('HOOK_ACCOUNT_CREATE_BEFORE_SUBMIT_BUTTON', ++$i); define('HOOK_ACCOUNT_CREATE_AFTER_FORM', ++$i); define('HOOK_ACCOUNT_CREATE_AFTER_SUBMIT', ++$i); +define('HOOK_ADMIN_MENU', ++$i); define('HOOK_FIRST', HOOK_STARTUP); -define('HOOK_LAST', HOOK_ACCOUNT_CREATE_AFTER_SUBMIT); +define('HOOK_LAST', HOOK_ADMIN_MENU); require_once LIBS . 'plugins.php'; class Hook