forked from matomo-org/tag-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.php
241 lines (196 loc) · 7.96 KB
/
Controller.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?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\DataTable\Filter\SafeDecodeLabel;
use Piwik\Filechecks;
use Piwik\Menu\MenuTop;
use Piwik\Notification;
use Piwik\Piwik;
use Piwik\Plugins\TagManager\API\PreviewCookie;
use Piwik\Plugins\TagManager\Input\AccessValidator;
use Piwik\Plugins\TagManager\Model\Container;
use Piwik\Plugins\TagManager\Model\Environment;
use Piwik\Url;
use Piwik\View;
use Piwik\Notification\Manager as NotificationManager;
class Controller extends \Piwik\Plugin\Controller
{
/**
* @var AccessValidator
*/
private $accessValidator;
/**
* @var Container
*/
private $container;
public function __construct(AccessValidator $accessValidator, Container $container)
{
parent::__construct();
$this->accessValidator = $accessValidator;
$this->container = $container;
}
public function getDefaultAction()
{
return 'manageContainers';
}
public function debug()
{
Piwik::checkUserHasSomeWriteAccess();
$output = $this->renderTemplate('debug.twig');
return $output;
}
public function manageContainers()
{
$this->accessValidator->checkViewPermission($this->idSite);
$idContainer = Common::getRequestVar('idContainer', '', 'string');
$container = null;
if (!empty($idContainer)) {
try {
$container = Request::processRequest('TagManager.getContainer', ['idSite' => $this->idSite, 'idContainer' => $idContainer]);
} catch (\Exception $e) {
// we ignore this error, it is totally fine if this container doesn't exist as we only need it to pre-select the current container name
}
}
return $this->renderTemplate('manageContainers', array(
'container' => $container
));
}
public function dashboard()
{
$this->accessValidator->checkWriteCapability($this->idSite);
return $this->renderManageContainerTemplate('dashboard');
}
public function gettingStarted()
{
Piwik::checkUserHasSomeViewAccess();
$canEdit = $this->accessValidator->hasWriteCapability($this->idSite);
return $this->renderTemplate('gettingStarted', array(
'canEdit' => $canEdit
));
}
public function manageTags()
{
return $this->renderManageContainerTemplate('manageTags');
}
public function manageTriggers()
{
return $this->renderManageContainerTemplate('manageTriggers');
}
public function manageVariables()
{
return $this->renderManageContainerTemplate('manageVariables');
}
public function manageVersions()
{
$path = TagManager::getAbsolutePathToContainerDirectory();
Filechecks::dieIfDirectoriesNotWritable(array($path));
return $this->renderManageContainerTemplate('manageVersions');
}
public function releases()
{
return $this->renderManageContainerTemplate('releases');
}
private function renderManageContainerTemplate($template, $variables = array())
{
$this->accessValidator->checkViewPermission($this->idSite);
$idContainer = Common::getRequestVar('idContainer', null, 'string');
try {
$this->container->checkContainerExists($this->idSite, $idContainer);
} catch (\Exception $e) {
// use case when switching within tag management from one site to another eg on page "manage tags".
// the other container won't be found
$notification = new Notification($e->getMessage());
$notification->context = Notification::CONTEXT_WARNING;
Notification\Manager::notify('TagManager_TagDoesNotExist', $notification);
if (Piwik::getAction() !== 'manageContainers') {
$this->redirectToIndex('TagManager', 'manageContainers', $this->idSite);
}
return;
}
if (empty($variables['container'])) {
$variables['container'] = Request::processRequest('TagManager.getContainer', ['idSite' => $this->idSite, 'idContainer' => $idContainer]);
}
$variables['idcontainerversion'] = null;
if (!empty($variables['container']['draft']['idcontainerversion'])) {
$variables['idcontainerversion'] = $variables['container']['draft']['idcontainerversion'];
}
foreach ($variables['container']['releases'] as $release) {
if ($release['environment'] === Environment::ENVIRONMENT_PREVIEW) {
$version = '';
if (!empty($release['version_name'])) {
$version = ' (' . Piwik::translate('TagManager_VersionX', $release['version_name']) . ')';
}
$mtmPreviewId = PreviewCookie::COOKIE_NAME . '=' .$idContainer;
$mtmPreviewId = SafeDecodeLabel::decodeLabelSafe($mtmPreviewId);
$previewCookie = new PreviewCookie();
$debugSiteUrl = $previewCookie->getDebugSiteUrl();
$previewUrl = '';
if (!empty($debugSiteUrl)) {
$previewUrl = $debugSiteUrl . (stripos($debugSiteUrl, '?') !== False ? '&' : '?') . $mtmPreviewId;
}
$notificationMessage = $this->renderTemplate('previewDebugNotification.twig', array(
'idcontainer' => $release['idcontainer'],
'debugSiteUrl' => $debugSiteUrl,
'version' => $version,
'mtmPreviewId' => $mtmPreviewId,
'previewUrl' => $previewUrl
)
);
$notification = new Notification($notificationMessage);
$notification->context = Notification::CONTEXT_INFO;
$notification->raw = true;
NotificationManager::notify('previewDebugMode', $notification);
}
}
return $this->renderTemplate($template, $variables);
}
public function exportContainerVersion()
{
$this->checkSitePermission();
$jsonCallback = Common::getRequestVar('callback', false);
if (!$jsonCallback) {
$jsonCallback = Common::getRequestVar('jsoncallback', false);
}
if ($jsonCallback) {
throw new \Exception('JSON callback not possible');
}
$result = Request::processRequest('TagManager.exportContainerVersion', array('format' => 'JSON', 'idSite' => $this->idSite));
if (!empty($_SERVER['SERVER_NAME']) && Url::getCorsHostsFromConfig()) {
// we make sure to send a custom cors header
Common::sendHeader('Access-Control-Allow-Origin: ' . $_SERVER['SERVER_NAME']);
}
return $result;
}
protected function renderTemplate($template, array $variables = array())
{
if (false === strpos($template, '@') || false === strpos($template, '/')) {
$template = '@' . $this->pluginName . '/' . $template;
}
$view = new View($template);
if (empty($this->site) || empty($this->idSite)) {
$this->setBasicVariablesView($view);
} else {
$this->setGeneralVariablesView($view);
}
$view->topMenu = MenuTop::getInstance()->getMenu();
$view->tagManagerMenu = MenuTagManager::getInstance()->getMenu();
list($defaultAction, $defaultParameters) = Menu::getDefaultAction();
$view->tagAction = $defaultAction;
foreach ($variables as $key => $value) {
$view->$key = $value;
}
$notifications = $view->notifications;
if (empty($notifications)) {
$view->notifications = NotificationManager::getAllNotificationsToDisplay();
NotificationManager::cancelAllNonPersistent();
}
return $view->render();
}
}