Skip to content

Commit

Permalink
refs #5192 added generator for menus, updated more plugins to use men…
Browse files Browse the repository at this point in the history
…u class, fixed some bugs, improved design, fixed tests, ...
  • Loading branch information
tsteur committed May 20, 2014
1 parent 45ae10e commit 307ec31
Show file tree
Hide file tree
Showing 51 changed files with 635 additions and 294 deletions.
17 changes: 16 additions & 1 deletion core/Menu/MenuAbstract.php
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Menu;

use Piwik\Common;
use Piwik\Log;
use Piwik\Plugins\SitesManager\API;
use Piwik\Singleton;
use Piwik\Plugin\Manager as PluginManager;
Expand Down Expand Up @@ -84,7 +85,12 @@ private function findMenuInPlugin($pluginName)

$klassName = sprintf('Piwik\\Plugins\\%s\\Menu', $pluginName);

if (!class_exists($klassName) || !is_subclass_of($klassName, 'Piwik\\Plugin\\Menu')) {
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;
}

Expand Down Expand Up @@ -125,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 @@ -157,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 @@ -179,6 +193,7 @@ private function buildMenu()
* @param $subMenuOriginal
* @param $mainMenuRenamed
* @param $subMenuRenamed
* @api
*/
public function rename($mainMenuOriginal, $subMenuOriginal, $mainMenuRenamed, $subMenuRenamed)
{
Expand Down
58 changes: 58 additions & 0 deletions plugins/CoreConsole/Commands/GenerateMenu.php
@@ -0,0 +1,58 @@
<?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\Plugins\CoreConsole\Commands;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
*/
class GenerateMenu extends GeneratePluginBase
{
protected function configure()
{
$this->setName('generate:menu')
->setDescription('Adds a plugin menu class to an existing plugin')
->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin which does not have a menu defined yet');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$pluginName = $this->getPluginName($input, $output);

$exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin';
$replace = array('ExamplePlugin' => $pluginName);
$whitelistFiles = array('/Menu.php');

$this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);

$this->writeSuccessMessage($output, array(
sprintf('Menu.php for %s generated.', $pluginName),
'You can now start defining your plugin menu',
'Enjoy!'
));
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return array
* @throws \RunTimeException
*/
protected function getPluginName(InputInterface $input, OutputInterface $output)
{
$pluginNames = $this->getPluginNamesHavingNotSpecificFile('Menu.php');
$invalidName = 'You have to enter the name of an existing plugin which does not already have a menu defined';

return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
}

}
Expand Up @@ -7,10 +7,11 @@

/**
* Usage:
* <div piwik-menudropdown title="MyMenuItem">
* <a class="item" href="/url"></a>
* <a class="item active">test</a>
* <a class="item disabled">-------</a>
* <div piwik-menudropdown menu-title="MyMenuItem">
* <a class="item" href="/url">An Item</a>
* <a class="item disabled">Disabled</a>
* <a class="item active">Active item</a>
* <hr class="item separator"/>
* <a class="item" href="/url"></a>
* </div>
*/
Expand All @@ -21,7 +22,7 @@ angular.module('piwikApp').directive('piwikMenudropdown', function(){
replace: true,
restrict: 'A',
scope: {
title: '@'
menuTitle: '@'
},
templateUrl: 'plugins/CoreHome/angularjs/menudropdown/menudropdown.html?cb=' + piwik.cacheBuster
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreHome/angularjs/menudropdown/menudropdown.html
Expand Up @@ -2,7 +2,7 @@

<span class="title"
ng-click="view.showItems=!view.showItems"
>{{ title }}</span>
ng-bind-html="menuTitle"/>

<div class="items" ng-show="view.showItems" ng-transclude></div>

Expand Down
13 changes: 11 additions & 2 deletions plugins/CoreHome/angularjs/menudropdown/menudropdown.less
Expand Up @@ -28,8 +28,10 @@
.items {
z-index: 21;
position: absolute;
max-width: 200px;
border: 1px solid @color-silver-l80 !important;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
background: @theme-color-background-base;
max-height: 400px;
overflow-y: auto;
Expand All @@ -40,7 +42,7 @@
display: block;
color: @theme-color-text !important;
text-decoration: none !important;
padding: 1px 25px 1px 3px !important;
padding: 6px 25px 6px 6px !important;

&:hover {
background: @color-silver-l80;
Expand All @@ -51,6 +53,13 @@
cursor: default;
}

&.separator {
padding: 0px !important;
border-bottom: 0px;
margin: 0px;
}

&.separator,
&.disabled {
opacity: 0.5;
cursor: default;
Expand Down
5 changes: 5 additions & 0 deletions plugins/CoreHome/stylesheets/coreHome.less
Expand Up @@ -217,6 +217,11 @@ a.Piwik_Popover_Error_Back {
}
}

#userMenu .items {
margin-left: -105px;
width: 150px;
}

#updateCheckLinkContainer {
opacity: 0.7;
}
Expand Down
10 changes: 6 additions & 4 deletions plugins/CoreHome/templates/_topBarHelloMenu.twig
@@ -1,9 +1,9 @@
<div id="topRightBar">
{% set helloAlias %}
{% if userAlias is not empty %}
{{ userAlias }}
{{ userAlias|raw }}
{% else %}
{{ userLogin }}
{{ userLogin|raw }}
{% endif %}
{% endset %}

Expand All @@ -21,7 +21,9 @@
{% endmacro %}

<span class="topBarElem">
<div title="{{ helloAlias|trim|raw }}"
<div id="userMenu"
title="{{ 'General_HelloUser'|translate(helloAlias|trim)|raw }}"
menu-title="{{ helloAlias|trim }}"
piwik-menudropdown>

{% if userLogin != 'anonymous' %}
Expand All @@ -35,7 +37,7 @@
{% for lev1UserLabel,lev1UserMenu in userMenu if lev1UserLabel|slice(0,1) != '_' %}

{% if userLogin != 'anonymous' or not loop.first %}
<span class="item disabled">--------</span>
<hr class="item separator"/>
{% endif %}

{% if lev1UserMenu._hasSubmenu is defined and lev1UserMenu._hasSubmenu %}
Expand Down
2 changes: 1 addition & 1 deletion plugins/DevicesDetection/Menu.php
Expand Up @@ -26,7 +26,7 @@ public function configureAdminMenu(MenuAdmin $menu)
);
}

public function configureReporingMenu(MenuReporting $menu)
public function configureReportingMenu(MenuReporting $menu)
{
$menu->add('General_Visitors', 'DevicesDetection_submenu', array('module' => 'DevicesDetection', 'action' => 'index'));
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Events/Menu.php
Expand Up @@ -14,7 +14,7 @@
*/
class Menu extends \Piwik\Plugin\Menu
{
public function configureReporingMenu(MenuReporting $menu)
public function configureReportingMenu(MenuReporting $menu)
{
$menu->add('General_Actions', 'Events_Events', array('module' => 'Events', 'action' => 'index'), true, 30);
}
Expand Down
43 changes: 43 additions & 0 deletions plugins/ExamplePlugin/Menu.php
@@ -0,0 +1,43 @@
<?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\Plugins\ExamplePlugin;

use Piwik\Menu\MenuAdmin;
use Piwik\Menu\MenuReporting;
use Piwik\Menu\MenuTop;
use Piwik\Menu\MenuUser;

/**
* This class allows you to add, remove or rename menu items.
* To configure a menu (such as Admin Menu, Reporting Menu, User Menu...) simply call the corresponding methods as
* described in the API-Reference http://developer.piwik.org/api-reference/Piwik/Menu/MenuAbstract
*/
class Menu extends \Piwik\Plugin\Menu
{
public function configureReportingMenu(MenuReporting $menu)
{
// $menu->add('UI Framework', '', array('module' => 'ExamplePlugin', 'action' => ''), true, $orderId = 30);
// $menu->add('UI Framework', 'Report 1', array('module' => 'ExamplePlugin', 'action' => 'report1'), true, $orderId = 30);
}

public function configureAdminMenu(MenuAdmin $menu)
{
// $menu->add('General_Settings', 'My Admin Item', array('module' => 'ExamplePlugin', 'action' => ''), true, $orderId = 30);
}

public function configureTopMenu(MenuTop $menu)
{
// $menu->add('My Top Item', null, array('module' => 'ExamplePlugin', 'action' => ''), true, $orderId = 30);
}

public function configureUserMenu(MenuUser $menu)
{
// $menu->add('CoreAdminHome_MenuManage', 'My User Item', array('module' => 'ExamplePlugin', 'action' => ''), true, $orderId = 30);
}
}
29 changes: 9 additions & 20 deletions plugins/ExampleUI/ExampleUI.php → plugins/ExampleUI/Menu.php
Expand Up @@ -6,28 +6,17 @@
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/

namespace Piwik\Plugins\ExampleUI;
use Piwik\Menu\MenuAbstract;
use Piwik\Menu\MenuMain;

use Piwik\Menu\MenuReporting;
use Piwik\Menu\MenuTop;
use Piwik\Plugin\Manager as PluginManager;

/**
*/
class ExampleUI extends \Piwik\Plugin
class Menu extends \Piwik\Plugin\Menu
{
/**
* @see Piwik\Plugin::getListHooksRegistered
*/
public function getListHooksRegistered()
{
return array(
'Menu.Reporting.addItems' => 'addReportingMenuItems',
'Menu.Top.addItems' => 'addTopMenuItems',
);
}

function addReportingMenuItems(MenuAbstract $menu)
public function configureReportingMenu(MenuReporting $menu)
{
$menu->add('UI Framework', '', array('module' => 'ExampleUI', 'action' => 'dataTables'), true, 30);

Expand All @@ -38,18 +27,18 @@ function addReportingMenuItems(MenuAbstract $menu)
$this->addSubMenu($menu, 'Sparklines', 'sparklines', 5);
$this->addSubMenu($menu, 'Evolution Graph', 'evolutionGraph', 6);

if (\Piwik\Plugin\Manager::getInstance()->isPluginActivated('TreemapVisualization')) {
if (PluginManager::getInstance()->isPluginActivated('TreemapVisualization')) {
$this->addSubMenu($menu, 'Treemap', 'treemap', 7);
}
}

function addTopMenuItems(MenuTop $menu)
public function configureTopMenu(MenuTop $menu)
{
$urlParams = array('module' => 'ExampleUI', 'action' => 'notifications');
$menu->addEntry('UI Notifications', null, $urlParams, $displayedForCurrentUser = true, $order = 3);
$menu->add('UI Notifications', null, $urlParams, $displayedForCurrentUser = true, $order = 3);
}

private function addSubMenu(MenuAbstract $menu, $subMenu, $action, $order)
private function addSubMenu(MenuReporting $menu, $subMenu, $action, $order)
{
$menu->add('UI Framework', $subMenu, array('module' => 'ExampleUI', 'action' => $action), true, $order);
}
Expand Down
2 changes: 0 additions & 2 deletions plugins/Feedback/Menu.php
Expand Up @@ -11,8 +11,6 @@
use Piwik\Menu\MenuUser;
use Piwik\Piwik;

/**
*/
class Menu extends \Piwik\Plugin\Menu
{
public function configureUserMenu(MenuUser $menu)
Expand Down

0 comments on commit 307ec31

Please sign in to comment.