-
Notifications
You must be signed in to change notification settings - Fork 58
/
Menu.php
139 lines (115 loc) · 6.28 KB
/
Menu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TagManager;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Menu\MenuTop;
use Piwik\Piwik;
use Piwik\Plugins\TagManager\Input\AccessValidator;
use Piwik\Plugins\TagManager\Model\Environment;
class Menu extends \Piwik\Plugin\Menu
{
/**
* @var AccessValidator
*/
private $accessValidator;
public function __construct(AccessValidator $accessValidator)
{
$this->accessValidator = $accessValidator;
}
public function configureTopMenu(MenuTop $menu)
{
// Check whether to show the MTM top menu. If not, simply return early
$idSite = \Piwik\Request::fromRequest()->getIntegerParameter('idSite', 0);
if (!StaticContainer::get(SystemSettings::class)->doesCurrentUserHaveTagManagerAccess($idSite)) {
return;
}
list($defaultAction, $defaultParams) = self::getDefaultAction();
if ($defaultAction) {
$menu->addItem('TagManager_TagManager', null, $this->urlForAction($defaultAction, $defaultParams), $orderId = 30);
}
}
public static function getDefaultAction()
{
$idSite = Common::getRequestVar('idSite', 0, 'string');
if (!$idSite || !Piwik::isUserHasViewAccess($idSite)) {
return array(null, null);
}
$defaultAction = 'manageContainers';
$defaultParams = array('idContainer' => false);
if ($idSite) {
// for better performance we go here directly on to the DAO and avoid formatting the containers as this
// makes initial pageview slower otherwise
$containers = StaticContainer::get('Piwik\Plugins\TagManager\Dao\ContainersDao')->getContainersForSite($idSite);
if (count($containers) == 1) {
$firstContainer = array_shift($containers);
$accessValidator = StaticContainer::get('Piwik\Plugins\TagManager\Input\AccessValidator');
if ($accessValidator->hasWriteCapability($idSite)) {
$defaultAction = 'dashboard';
} else {
$defaultAction = 'manageTags';
}
$defaultParams = array('idContainer' => $firstContainer['idcontainer']);
}
}
return array($defaultAction, $defaultParams);
}
public function configureTagManagerMenu(MenuTagManager $menu)
{
$idSite = Common::getRequestVar('idSite', 0, 'string');
if (!$idSite || !Piwik::isUserHasViewAccess($idSite)) {
return;
}
$manageContainers = Piwik::translate('TagManager_ManageX', Piwik::translate('TagManager_Containers'));
$paramsNoContainerId = array('idContainer' => null);// prevents eg error after deleting a container if idContainer is still set
$menu->addItem('TagManager_TagManager', $manageContainers, $this->urlForAction('manageContainers', $paramsNoContainerId), $orderId = 99);
$idContainer = Common::getRequestVar('idContainer', '', 'string');
if (!empty($idContainer)) {
$idContainer = trim($idContainer);
try {
$container = Request::processRequest('TagManager.getContainer', ['idSite' => $idSite, 'idContainer' => $idContainer]);
} catch (\Exception $e) {
$container = null; // the container might be deleted by now
}
if (!empty($container)) {
$params = array('idContainer' => $idContainer); // not needed as it is already present in url but we make sure the id is set
$menuCategory = strlen($container['name']) > 50 ? substr($container['name'], 0, 50) . '…' : $container['name'];
if ($this->accessValidator->hasWriteCapability($idSite)) {
$menu->addItem($menuCategory, 'Dashboard', $this->urlForAction('dashboard', $params), $orderId = 104);
}
$menu->addItem($menuCategory, 'TagManager_Tags', $this->urlForAction('manageTags', $params), $orderId = 105);
$menu->addItem($menuCategory, 'TagManager_Triggers', $this->urlForAction('manageTriggers', $params), $orderId = 110);
$menu->addItem($menuCategory, 'TagManager_Variables', $this->urlForAction('manageVariables', $params), $orderId = 115);
$previewEnabled = false;
foreach ($container['releases'] as $release) {
if ($release['environment'] === Environment::ENVIRONMENT_PREVIEW) {
$previewEnabled = true;
}
}
if ($this->accessValidator->hasWriteCapability($idSite)) {
$menu->addItem($menuCategory, 'TagManager_Versions', $this->urlForAction('manageVersions', $params), $orderId = 115);
if ($previewEnabled) {
$menu->addItem($menuCategory, 'TagManager_DisablePreview', array(), $orderId = 130, false, 'icon-bug', "tagManagerHelper.disablePreviewMode(" . json_encode($container['idcontainer']) . ")");
} else {
$menu->addItem($menuCategory, 'TagManager_EnablePreviewDebug', array(), $orderId = 130, false, 'icon-bug', "tagManagerHelper.enablePreviewMode(" . json_encode($container['idcontainer']) . ")");
}
if (
$this->accessValidator->hasUseCustomTemplatesCapability($idSite) ||
(
$this->accessValidator->hasWriteCapability($idSite) && $this->accessValidator->hasPublishLiveEnvironmentCapability($idSite)
)
) {
$menu->addItem($menuCategory, 'TagManager_Publish', array(), $orderId = 135, false, 'icon-rocket', "tagManagerHelper.editVersion(" . json_encode($container['idcontainer']) . ", 0, function () { window.location.reload(); })");
}
}
$menu->addItem($menuCategory, 'TagManager_InstallCode', $this->urlForAction('releases', $params), $orderId = 140, false, 'icon-embed', "tagManagerHelper.showInstallCode(" . json_encode($container['idcontainer']) . ")");
}
}
}
}