update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -8,17 +8,17 @@
*/
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.
*
*
* There are three menus in Piwik, the main menu, the top menu and the admin menu.
* Each menu has a class that manages the menu's content. Each class invokes
* a different event to allow plugins to add new menu items.
*
*
* @static \Piwik\Menu\MenuAbstract getInstance()
*/
abstract class MenuAbstract extends Singleton
@ -30,6 +30,8 @@ abstract class MenuAbstract extends Singleton
protected $edits = array();
protected $renames = array();
protected $orderingApplied = false;
protected $menuIcons = array();
protected static $menus = array();
/**
* Builds the menu, applies edits, renames
@ -47,6 +49,43 @@ abstract class MenuAbstract extends Singleton
return $this->menu;
}
/**
* Let's you register a menu icon for a certain menu category to replace the default arrow icon.
*
* @param string $menuName The translation key of a main menu category, eg 'Dashboard_Dashboard'
* @param string $iconCssClass The css class name of an icon, eg 'icon-user'
*/
public function registerMenuIcon($menuName, $iconCssClass)
{
$this->menuIcons[$menuName] = $iconCssClass;
}
/**
* Returns a list of available plugin menu instances.
*
* @return \Piwik\Plugin\Menu[]
*/
protected function getAllMenus()
{
if (!empty(self::$menus)) {
return self::$menus;
}
self::$menus = PluginManager::getInstance()->findComponents('Menu', 'Piwik\\Plugin\\Menu');
return self::$menus;
}
/**
* To use only for tests.
*
* @deprecated The whole $menus cache should be replaced by a real transient cache
*/
public static function clearMenus()
{
self::$menus = array();
}
/**
* Adds a new entry to the menu.
*
@ -57,8 +96,9 @@ abstract class MenuAbstract extends Singleton
* @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.
* @api
* @param bool|string $tooltip An optional tooltip to display or false to display the tooltip.
*
* @deprecated since 2.7.0 Use {@link addItem() instead}. Method will be removed in Piwik 3.0
*/
public function add($menuName, $subMenuName, $url, $displayedForCurrentUser = true, $order = 50, $tooltip = false)
{
@ -66,8 +106,25 @@ abstract class MenuAbstract extends Singleton
return;
}
$this->addItem($menuName, $subMenuName, $url, $order, $tooltip);
}
/**
* Adds a new entry to the menu.
*
* @param string $menuName The menu's category name. Can be a translation token.
* @param string $subMenuName The menu item's name. Can be a translation token.
* @param string|array $url The URL the admin menu entry should link to, or an array of query parameters
* that can be used to build the URL.
* @param int $order The order hint.
* @param bool|string $tooltip An optional tooltip to display or false to display the tooltip.
* @since 2.7.0
* @api
*/
public function addItem($menuName, $subMenuName, $url, $order = 50, $tooltip = false)
{
// make sure the idSite value used is numeric (hack-y fix for #3426)
if (!is_numeric(Common::getRequestVar('idSite', false))) {
if (isset($url['idSite']) && !is_numeric($url['idSite'])) {
$idSites = API::getInstance()->getSitesIdWithAtLeastViewAccess();
$url['idSite'] = reset($idSites);
}
@ -81,6 +138,13 @@ abstract class MenuAbstract extends Singleton
);
}
/**
* 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(
@ -100,21 +164,34 @@ abstract class MenuAbstract extends Singleton
*/
private function buildMenuItem($menuName, $subMenuName, $url, $order = 50, $tooltip = false)
{
if (!isset($this->menu[$menuName]) || empty($subMenuName)) {
$this->menu[$menuName]['_url'] = $url;
if (empty($subMenuName)) {
$this->menu[$menuName]['_order'] = $order;
}
$this->menu[$menuName]['_name'] = $menuName;
$this->menu[$menuName]['_hasSubmenu'] = false;
if (!isset($this->menu[$menuName])) {
$this->menu[$menuName] = array(
'_hasSubmenu' => false,
'_order' => $order
);
}
if (empty($subMenuName)) {
$this->menu[$menuName]['_url'] = $url;
$this->menu[$menuName]['_order'] = $order;
$this->menu[$menuName]['_name'] = $menuName;
$this->menu[$menuName]['_tooltip'] = $tooltip;
if (!empty($this->menuIcons[$menuName])) {
$this->menu[$menuName]['_icon'] = $this->menuIcons[$menuName];
} else {
$this->menu[$menuName]['_icon'] = '';
}
}
if (!empty($subMenuName)) {
$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;
if (!array_key_exists('_tooltip', $this->menu[$menuName])) {
$this->menu[$menuName]['_tooltip'] = $tooltip;
}
}
}
@ -135,6 +212,7 @@ abstract class MenuAbstract extends Singleton
* @param $subMenuOriginal
* @param $mainMenuRenamed
* @param $subMenuRenamed
* @api
*/
public function rename($mainMenuOriginal, $subMenuOriginal, $mainMenuRenamed, $subMenuRenamed)
{
@ -148,6 +226,7 @@ abstract class MenuAbstract extends Singleton
* @param $mainMenuToEdit
* @param $subMenuToEdit
* @param $newUrl
* @api
*/
public function editUrl($mainMenuToEdit, $subMenuToEdit, $newUrl)
{
@ -161,13 +240,21 @@ abstract class MenuAbstract extends Singleton
{
foreach ($this->edits as $edit) {
$mainMenuToEdit = $edit[0];
$subMenuToEdit = $edit[1];
$newUrl = $edit[2];
$subMenuToEdit = $edit[1];
$newUrl = $edit[2];
if ($subMenuToEdit === null) {
$menuDataToEdit = @$this->menu[$mainMenuToEdit];
if (isset($this->menu[$mainMenuToEdit])) {
$menuDataToEdit = &$this->menu[$mainMenuToEdit];
} else {
$menuDataToEdit = null;
}
} else {
$menuDataToEdit = @$this->menu[$mainMenuToEdit][$subMenuToEdit];
if (isset($this->menu[$mainMenuToEdit][$subMenuToEdit])) {
$menuDataToEdit = &$this->menu[$mainMenuToEdit][$subMenuToEdit];
} else {
$menuDataToEdit = null;
}
}
if (empty($menuDataToEdit)) {
@ -180,16 +267,15 @@ abstract class MenuAbstract extends Singleton
private function applyRemoves()
{
foreach($this->menuEntriesToRemove as $menuToDelete) {
if(empty($menuToDelete[1])) {
foreach ($this->menuEntriesToRemove as $menuToDelete) {
if (empty($menuToDelete[1])) {
// Delete Main Menu
if(isset($this->menu[$menuToDelete[0]])) {
if (isset($this->menu[$menuToDelete[0]])) {
unset($this->menu[$menuToDelete[0]]);
}
} else {
// Delete Sub Menu
if(isset($this->menu[$menuToDelete[0]][$menuToDelete[1]])) {
if (isset($this->menu[$menuToDelete[0]][$menuToDelete[1]])) {
unset($this->menu[$menuToDelete[0]][$menuToDelete[1]]);
}
}
@ -202,9 +288,10 @@ abstract class MenuAbstract extends Singleton
{
foreach ($this->renames as $rename) {
$mainMenuOriginal = $rename[0];
$subMenuOriginal = $rename[1];
$mainMenuRenamed = $rename[2];
$subMenuRenamed = $rename[3];
$subMenuOriginal = $rename[1];
$mainMenuRenamed = $rename[2];
$subMenuRenamed = $rename[3];
// Are we changing a submenu?
if (!empty($subMenuOriginal)) {
if (isset($this->menu[$mainMenuOriginal][$subMenuOriginal])) {
@ -214,7 +301,7 @@ abstract class MenuAbstract extends Singleton
$this->menu[$mainMenuRenamed][$subMenuRenamed] = $save;
}
} // Changing a first-level element
else if (isset($this->menu[$mainMenuOriginal])) {
elseif (isset($this->menu[$mainMenuOriginal])) {
$save = $this->menu[$mainMenuOriginal];
$save['_name'] = $mainMenuRenamed;
unset($this->menu[$mainMenuOriginal]);
@ -238,7 +325,7 @@ abstract class MenuAbstract extends Singleton
foreach ($this->menu as $key => &$element) {
if (is_null($element)) {
unset($this->menu[$key]);
} else if ($element['_hasSubmenu']) {
} elseif ($element['_hasSubmenu']) {
uasort($element, array($this, 'menuCompare'));
}
}
@ -255,15 +342,36 @@ abstract class MenuAbstract extends Singleton
*/
protected function menuCompare($itemOne, $itemTwo)
{
if (!is_array($itemOne) || !is_array($itemTwo)
|| !isset($itemOne['_order']) || !isset($itemTwo['_order'])
) {
if (!is_array($itemOne) && !is_array($itemTwo)) {
return 0;
}
if ($itemOne['_order'] == $itemTwo['_order']) {
return strcmp($itemOne['_name'], $itemTwo['_name']);
if (!is_array($itemOne) && is_array($itemTwo)) {
return -1;
}
if (is_array($itemOne) && !is_array($itemTwo)) {
return 1;
}
if (!isset($itemOne['_order']) && !isset($itemTwo['_order'])) {
return 0;
}
if (!isset($itemOne['_order']) && isset($itemTwo['_order'])) {
return -1;
}
if (isset($itemOne['_order']) && !isset($itemTwo['_order'])) {
return 1;
}
if ($itemOne['_order'] == $itemTwo['_order']) {
return strcmp(
@$itemOne['_name'],
@$itemTwo['_name']);
}
return ($itemOne['_order'] < $itemTwo['_order']) ? -1 : 1;
}
}