Skip to content

Commit

Permalink
Merge pull request #291 from piwik/5192_menu
Browse files Browse the repository at this point in the history
refs #5192 Simplify / improve top menu
  • Loading branch information
tsteur committed May 26, 2014
2 parents 6871bbc + af0e0c6 commit e985743
Show file tree
Hide file tree
Showing 95 changed files with 1,682 additions and 883 deletions.
62 changes: 61 additions & 1 deletion core/Menu/MenuAbstract.php
Expand Up @@ -9,8 +9,10 @@
namespace Piwik\Menu;

use Piwik\Common;
use Piwik\Log;
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.
Expand All @@ -30,6 +32,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
Expand All @@ -47,6 +50,53 @@ public function getMenu()
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)) {
return;
}

if (!is_subclass_of($klassName, 'Piwik\\Plugin\\Menu')) {
Log::warning(sprintf('Cannot use menu for plugin %s, class %s does not extend Piwik\Plugin\Menu', $pluginName, $klassName));
return;
}

return new $klassName;
}

/**
* Adds a new entry to the menu.
*
Expand All @@ -57,7 +107,7 @@ public function getMenu()
* @param boolean $displayedForCurrentUser Whether this menu entry should be displayed for the
* current user. If false, the entry will not be added.
* @param int $order The order hint.
* @param false|string $tooltip An optional tooltip to display.
* @param bool|string $tooltip An optional tooltip to display or false to display the tooltip.
* @api
*/
public function add($menuName, $subMenuName, $url, $displayedForCurrentUser = true, $order = 50, $tooltip = false)
Expand All @@ -81,6 +131,13 @@ public function add($menuName, $subMenuName, $url, $displayedForCurrentUser = tr
);
}

/**
* Removes an existing entry from the menu.
*
* @param string $menuName The menu's category name. Can be a translation token.
* @param bool|string $subMenuName The menu item's name. Can be a translation token.
* @api
*/
public function remove($menuName, $subMenuName = false)
{
$this->menuEntriesToRemove[] = array(
Expand Down Expand Up @@ -113,6 +170,7 @@ private function buildMenuItem($menuName, $subMenuName, $url, $order = 50, $tool
$this->menu[$menuName][$subMenuName]['_url'] = $url;
$this->menu[$menuName][$subMenuName]['_order'] = $order;
$this->menu[$menuName][$subMenuName]['_name'] = $subMenuName;
$this->menu[$menuName][$subMenuName]['_tooltip'] = $tooltip;
$this->menu[$menuName]['_hasSubmenu'] = true;
$this->menu[$menuName]['_tooltip'] = $tooltip;
}
Expand All @@ -135,6 +193,7 @@ private function buildMenu()
* @param $subMenuOriginal
* @param $mainMenuRenamed
* @param $subMenuRenamed
* @api
*/
public function rename($mainMenuOriginal, $subMenuOriginal, $mainMenuRenamed, $subMenuRenamed)
{
Expand All @@ -148,6 +207,7 @@ public function rename($mainMenuOriginal, $subMenuOriginal, $mainMenuRenamed, $s
* @param $mainMenuToEdit
* @param $subMenuToEdit
* @param $newUrl
* @api
*/
public function editUrl($mainMenuToEdit, $subMenuToEdit, $newUrl)
{
Expand Down
32 changes: 11 additions & 21 deletions core/Menu/MenuAdmin.php
Expand Up @@ -41,7 +41,7 @@ class MenuAdmin extends MenuAbstract
* @param boolean $displayedForCurrentUser Whether this menu entry should be displayed for the
* current user. If false, the entry will not be added.
* @param int $order The order hint.
* @api
* @deprecated since version 2.4.0. See {@link Piwik\Plugin\Menu} for new implementation.
*/
public static function addEntry($adminMenuName, $url, $displayedForCurrentUser = true, $order = 20)
{
Expand All @@ -58,28 +58,15 @@ public function getMenu()
if (!$this->menu) {

/**
* Triggered when collecting all available admin menu items. Subscribe to this event if you want
* to add one or more items to the Piwik admin menu.
*
* Menu items should be added via the {@link add()} method.
*
* **Example**
*
* use Piwik\Menu\MenuAdmin;
*
* public function addMenuItems()
* {
* MenuAdmin::getInstance()->add(
* 'MenuName',
* 'SubmenuName',
* array('module' => 'MyPlugin', 'action' => 'index'),
* $showOnlyIf = Piwik::hasUserSuperUserAccess(),
* $order = 6
* );
* }
* @ignore
*/
Piwik::postEvent('Menu.Admin.addItems');
Piwik::postEvent('Menu.Admin.addItems', array());

foreach ($this->getAvailableMenus() as $menu) {
$menu->configureAdminMenu($this);
}
}

return parent::getMenu();
}

Expand All @@ -106,6 +93,9 @@ public function getCurrentAdminMenuName()
return false;
}

/**
* @deprecated since version 2.4.0. See {@link Piwik\Plugin\Menu} for new implementation.
*/
public static function removeEntry($menuName, $subMenuName = false)
{
MenuAdmin::getInstance()->remove($menuName, $subMenuName);
Expand Down
80 changes: 4 additions & 76 deletions core/Menu/MenuMain.php
Expand Up @@ -7,85 +7,13 @@
*
*/
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()
* {
* MenuMain::getInstance()->add(
* 'MyPlugin_MyTranslatedMenuCategory',
* 'MyPlugin_MyTranslatedMenuName',
* array('module' => 'MyPlugin', 'action' => 'index'),
* Piwik::isUserHasSomeAdminAccess(),
* $order = 2
* );
* }
*
* @api
* @deprecated since 2.4.0
* @see MenuReporting
* @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');
}
return parent::getMenu();
}
}
77 changes: 77 additions & 0 deletions core/Menu/MenuReporting.php
@@ -0,0 +1,77 @@
<?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 Reporting menu (the menu displayed under the Piwik logo).
* Plugins can subscribe to the {@hook Menu.Reporting.addItems} event to add new pages to
* the reporting menu.
*
* **Example**
*
* // add a new page in an observer to Menu.Admin.addItems
* public function addReportingMenuItem()
* {
* MenuReporting::getInstance()->add(
* 'MyPlugin_MyTranslatedMenuCategory',
* 'MyPlugin_MyTranslatedMenuName',
* array('module' => 'MyPlugin', 'action' => 'index'),
* Piwik::isUserHasSomeAdminAccess(),
* $order = 2
* );
* }
*
* @api
* @method static \Piwik\Menu\MenuReporting getInstance()
*/
class MenuReporting extends MenuAbstract
{
/**
* Returns if the URL was found in the menu.
*
* @param string $url
* @return boolean
*/
public function isUrlFound($url)
{
$menu = $this->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()
{
if (!$this->menu) {

/**
* @ignore
*/
Piwik::postEvent('Menu.Reporting.addItems', array());

foreach ($this->getAvailableMenus() as $menu) {
$menu->configureReportingMenu($this);
}
}

return parent::getMenu();
}
}

0 comments on commit e985743

Please sign in to comment.