From b2cb622bf1ea754a73ce06e165a083e3305aa807 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Tue, 20 May 2014 01:05:41 +0200 Subject: refs #5192 continued menu refactoring. not sure yet how good this idea is but seems to be already better than before --- core/Menu/MenuAbstract.php | 44 +++++++++++++++++++++ core/Menu/MenuAdmin.php | 5 +++ core/Menu/MenuMain.php | 65 +++--------------------------- core/Menu/MenuReporting.php | 96 +++++++++++++++++++++++++++++++++++++++++++++ core/Menu/MenuTop.php | 6 ++- core/Menu/MenuUser.php | 52 ++++++++++++++++++++++++ 6 files changed, 207 insertions(+), 61 deletions(-) create mode 100644 core/Menu/MenuReporting.php create mode 100755 core/Menu/MenuUser.php (limited to 'core/Menu') diff --git a/core/Menu/MenuAbstract.php b/core/Menu/MenuAbstract.php index 781aed7b81..02d76b2ce5 100644 --- a/core/Menu/MenuAbstract.php +++ b/core/Menu/MenuAbstract.php @@ -11,6 +11,7 @@ namespace Piwik\Menu; use Piwik\Common; use Piwik\Plugins\SitesManager\API; use Piwik\Singleton; +use Piwik\Plugin\Manager as PluginManager; /** * Base class for classes that manage one of Piwik's menus. @@ -30,6 +31,7 @@ abstract class MenuAbstract extends Singleton protected $edits = array(); protected $renames = array(); protected $orderingApplied = false; + protected static $menus = array(); /** * Builds the menu, applies edits, renames @@ -47,6 +49,48 @@ abstract class MenuAbstract extends Singleton return $this->menu; } + /** + * Returns a list of available plugin menu instances. + * + * @return \Piwik\Plugin\Menu[] + */ + protected function getAvailableMenus() + { + if (!empty(self::$menus)) { + return self::$menus; + } + + $pluginNames = PluginManager::getInstance()->getLoadedPluginsName(); + + self::$menus = array(); + foreach ($pluginNames as $pluginName) { + $menu = $this->findMenuInPlugin($pluginName); + + if (!empty($menu)) { + self::$menus[] = $menu; + } + } + + return self::$menus; + } + + private function findMenuInPlugin($pluginName) + { + $menuFile = PIWIK_INCLUDE_PATH . '/plugins/' . $pluginName . '/Menu.php'; + + if (!file_exists($menuFile)) { + return; + } + + $klassName = sprintf('Piwik\\Plugins\\%s\\Menu', $pluginName); + + if (!class_exists($klassName) || !is_subclass_of($klassName, 'Piwik\\Plugin\\Menu')) { + return; + } + + return new $klassName; + } + /** * Adds a new entry to the menu. * diff --git a/core/Menu/MenuAdmin.php b/core/Menu/MenuAdmin.php index b187d18eba..0b41bb33c5 100644 --- a/core/Menu/MenuAdmin.php +++ b/core/Menu/MenuAdmin.php @@ -85,7 +85,12 @@ class MenuAdmin extends MenuAbstract * } */ Piwik::postEvent('Menu.Admin.addItems', array($this)); + + foreach ($this->getAvailableMenus() as $menu) { + $menu->configureAdminMenu($this); + } } + return parent::getMenu(); } diff --git a/core/Menu/MenuMain.php b/core/Menu/MenuMain.php index 0421a33785..c078e2166d 100644 --- a/core/Menu/MenuMain.php +++ b/core/Menu/MenuMain.php @@ -7,15 +7,15 @@ * */ namespace Piwik\Menu; -use Piwik\Piwik; + /** * Contains menu entries for the Main menu (the menu displayed under the Piwik logo). * Plugins can subscribe to the {@hook Menu.Reporting.addItems} event to add new pages to * the main menu. - * + * * **Example** - * + * * // add a new page in an observer to Menu.Admin.addItems * public function addMainMenuItem() * { @@ -27,65 +27,10 @@ use Piwik\Piwik; * $order = 2 * ); * } - * + * * @api * @method static \Piwik\Menu\MenuMain getInstance() */ -class MenuMain extends MenuAbstract +class MenuMain extends MenuReporting { - /** - * Returns if the URL was found in the menu. - * - * @param string $url - * @return boolean - */ - public function isUrlFound($url) - { - $menu = MenuMain::getInstance()->getMenu(); - - foreach ($menu as $subMenus) { - foreach ($subMenus as $subMenuName => $menuUrl) { - if (strpos($subMenuName, '_') !== 0 && $menuUrl['_url'] == $url) { - return true; - } - } - } - return false; - } - - /** - * Triggers the Menu.Reporting.addItems hook and returns the menu. - * - * @return Array - */ - public function getMenu() - { - // We trigger the Event only once! - if (!$this->menu) { - - /** - * Triggered when collecting all available reporting menu items. Subscribe to this event if you - * want to add one or more items to the Piwik reporting menu. - * - * Menu items should be added via the {@link add()} method. - * - * **Example** - * - * use Piwik\Menu\MenuMain; - * - * public function addMenuItems() - * { - * MenuMain::getInstance()->add( - * 'CustomMenuName', - * 'CustomSubmenuName', - * array('module' => 'MyPlugin', 'action' => 'index'), - * $showOnlyIf = Piwik::hasUserSuperUserAccess(), - * $order = 6 - * ); - * } - */ - Piwik::postEvent('Menu.Reporting.addItems', array($this)); - } - return parent::getMenu(); - } } diff --git a/core/Menu/MenuReporting.php b/core/Menu/MenuReporting.php new file mode 100644 index 0000000000..fa9e8f4b46 --- /dev/null +++ b/core/Menu/MenuReporting.php @@ -0,0 +1,96 @@ +add( + * 'MyPlugin_MyTranslatedMenuCategory', + * 'MyPlugin_MyTranslatedMenuName', + * array('module' => 'MyPlugin', 'action' => 'index'), + * Piwik::isUserHasSomeAdminAccess(), + * $order = 2 + * ); + * } + * + * @api + * @method static \Piwik\Menu\MenuMain getInstance() + */ +class MenuReporting extends MenuAbstract +{ + /** + * Returns if the URL was found in the menu. + * + * @param string $url + * @return boolean + */ + public function isUrlFound($url) + { + $menu = MenuMain::getInstance()->getMenu(); + + foreach ($menu as $subMenus) { + foreach ($subMenus as $subMenuName => $menuUrl) { + if (strpos($subMenuName, '_') !== 0 && $menuUrl['_url'] == $url) { + return true; + } + } + } + return false; + } + + /** + * Triggers the Menu.Reporting.addItems hook and returns the menu. + * + * @return Array + */ + public function getMenu() + { + // We trigger the Event only once! + if (!$this->menu) { + + /** + * Triggered when collecting all available reporting menu items. Subscribe to this event if you + * want to add one or more items to the Piwik reporting menu. + * + * Menu items should be added via the {@link add()} method. + * + * **Example** + * + * use Piwik\Menu\MenuMain; + * + * public function addMenuItems() + * { + * MenuMain::getInstance()->add( + * 'CustomMenuName', + * 'CustomSubmenuName', + * array('module' => 'MyPlugin', 'action' => 'index'), + * $showOnlyIf = Piwik::hasUserSuperUserAccess(), + * $order = 6 + * ); + * } + */ + Piwik::postEvent('Menu.Reporting.addItems', array($this)); + + foreach ($this->getAvailableMenus() as $menu) { + $menu->configureReportingMenu($this); + } + } + + return parent::getMenu(); + } +} diff --git a/core/Menu/MenuTop.php b/core/Menu/MenuTop.php index dcec860ac4..307132a2fd 100644 --- a/core/Menu/MenuTop.php +++ b/core/Menu/MenuTop.php @@ -65,7 +65,6 @@ class MenuTop extends MenuAbstract MenuTop::getInstance()->remove($menuName, $subMenuName); } - /** * Directly adds a menu entry containing html. * @@ -119,7 +118,12 @@ class MenuTop extends MenuAbstract * } */ Piwik::postEvent('Menu.Top.addItems', array($this)); + + foreach ($this->getAvailableMenus() as $menu) { + $menu->configureTopMenu($this); + } } + return parent::getMenu(); } } diff --git a/core/Menu/MenuUser.php b/core/Menu/MenuUser.php new file mode 100755 index 0000000000..dd27fa4aef --- /dev/null +++ b/core/Menu/MenuUser.php @@ -0,0 +1,52 @@ +add( + * 'MyPlugin_MyTranslatedMenuCategory', + * 'MyPlugin_MyTranslatedMenuName', + * array('module' => 'MyPlugin', 'action' => 'index'), + * Piwik::isUserHasSomeAdminAccess(), + * $order = 2 + * ); + * } + * + * @method static \Piwik\Menu\MenuUser getInstance() + */ +class MenuUser extends MenuTop +{ + + /** + * Triggers the Menu.User.addItems hook and returns the menu. + * + * @return Array + */ + public function getMenu() + { + if (!$this->menu) { + foreach ($this->getAvailableMenus() as $menu) { + $menu->configureUserMenu($this); + } + } + + return parent::getMenu(); + } +} -- cgit v1.2.3