Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core/Menu
diff options
context:
space:
mode:
authorThomas Steur <thomas.steur@googlemail.com>2014-05-20 03:05:41 +0400
committerThomas Steur <thomas.steur@googlemail.com>2014-05-20 03:05:41 +0400
commitb2cb622bf1ea754a73ce06e165a083e3305aa807 (patch)
tree43bf1134201759cf73b88d53274fa87fdffebd0d /core/Menu
parent009a6f21177e8cb6366577d8f54024e896bb0d8e (diff)
refs #5192 continued menu refactoring. not sure yet how good this idea is but seems to be already better than before
Diffstat (limited to 'core/Menu')
-rw-r--r--core/Menu/MenuAbstract.php44
-rw-r--r--core/Menu/MenuAdmin.php5
-rw-r--r--core/Menu/MenuMain.php65
-rw-r--r--core/Menu/MenuReporting.php96
-rw-r--r--core/Menu/MenuTop.php6
-rwxr-xr-xcore/Menu/MenuUser.php52
6 files changed, 207 insertions, 61 deletions
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
@@ -48,6 +50,48 @@ abstract class MenuAbstract extends Singleton
}
/**
+ * 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.
*
* @param string $menuName The menu's category name. Can be a translation token.
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 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+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()
+ * {
+ * MenuReporting::getInstance()->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 @@
+<?php
+/**
+ * Piwik - Open source web analytics
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Menu;
+
+use Piwik\Piwik;
+
+/**
+ * Contains menu entries for the User menu (the menu at the very top of the page).
+ * Plugins can subscribe to the {@hook Menu.User.addItems} event to add new pages to
+ * the user menu.
+ *
+ * **Example**
+ *
+ * // add a new page in an observer to Menu.User.addItems
+ * public function addUserMenuItem()
+ * {
+ * MenuUser::getInstance()->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();
+ }
+}