1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\retour\controllers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use craft\errors\MissingComponentException; |
16
|
|
|
use craft\helpers\UrlHelper; |
17
|
|
|
use craft\web\Controller; |
18
|
|
|
use nystudio107\retour\assetbundles\retour\RetourDashboardAsset; |
19
|
|
|
use nystudio107\retour\helpers\MultiSite as MultiSiteHelper; |
20
|
|
|
use nystudio107\retour\helpers\Permission as PermissionHelper; |
21
|
|
|
use nystudio107\retour\Retour; |
22
|
|
|
use yii\base\InvalidConfigException; |
23
|
|
|
use yii\web\BadRequestHttpException; |
24
|
|
|
use yii\web\ForbiddenHttpException; |
25
|
|
|
use yii\web\NotFoundHttpException; |
26
|
|
|
use yii\web\Response; |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @author nystudio107 |
|
|
|
|
30
|
|
|
* @package Retour |
|
|
|
|
31
|
|
|
* @since 3.0.0 |
|
|
|
|
32
|
|
|
*/ |
|
|
|
|
33
|
|
|
class StatisticsController extends Controller |
34
|
|
|
{ |
35
|
|
|
// Constants |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
protected const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/'; |
39
|
|
|
|
40
|
|
|
// Protected Properties |
41
|
|
|
// ========================================================================= |
42
|
|
|
|
43
|
|
|
protected array|bool|int $allowAnonymous = []; |
44
|
|
|
|
45
|
|
|
// Public Methods |
46
|
|
|
// ========================================================================= |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @param string|null $siteHandle |
|
|
|
|
51
|
|
|
* @param bool $showWelcome |
|
|
|
|
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
* @throws NotFoundHttpException |
55
|
|
|
* @throws ForbiddenHttpException |
56
|
|
|
*/ |
57
|
|
|
public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response |
58
|
|
|
{ |
59
|
|
|
$variables = []; |
60
|
|
|
PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
61
|
|
|
// Trim the statistics |
62
|
|
|
Retour::$plugin->statistics->trimStatistics(); |
|
|
|
|
63
|
|
|
// Get the site to edit |
64
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
65
|
|
|
$pluginName = Retour::$settings->pluginName; |
66
|
|
|
$templateTitle = Craft::t('retour', 'Dashboard'); |
67
|
|
|
$view = Craft::$app->getView(); |
68
|
|
|
// Asset bundle |
69
|
|
|
try { |
70
|
|
|
$view->registerAssetBundle(RetourDashboardAsset::class); |
71
|
|
|
} catch (InvalidConfigException $e) { |
72
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
73
|
|
|
} |
74
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
75
|
|
|
'@nystudio107/retour/web/assets/dist', |
76
|
|
|
true |
|
|
|
|
77
|
|
|
); |
78
|
|
|
// Enabled sites |
79
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
80
|
|
|
$variables['controllerHandle'] = 'dashboard'; |
81
|
|
|
|
82
|
|
|
// Basic variables |
83
|
|
|
$variables['fullPageForm'] = false; |
84
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
85
|
|
|
$variables['pluginName'] = $pluginName; |
86
|
|
|
$variables['title'] = $templateTitle; |
87
|
|
|
$siteHandleUri = Craft::$app->isMultiSite ? '/' . $siteHandle : ''; |
88
|
|
|
$variables['crumbs'] = [ |
89
|
|
|
[ |
90
|
|
|
'label' => $pluginName, |
91
|
|
|
'url' => UrlHelper::cpUrl('retour'), |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
'label' => $templateTitle, |
95
|
|
|
'url' => UrlHelper::cpUrl('retour/dashboard' . $siteHandleUri), |
96
|
|
|
], |
97
|
|
|
]; |
98
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
99
|
|
|
$variables['selectedSubnavItem'] = 'dashboard'; |
100
|
|
|
$variables['showWelcome'] = $showWelcome; |
101
|
|
|
$variables['settings'] = Retour::$settings; |
102
|
|
|
|
103
|
|
|
// Render the template |
104
|
|
|
return $this->renderTemplate('retour/dashboard/index', $variables); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
|
|
|
|
108
|
|
|
* @return Response |
109
|
|
|
* @throws ForbiddenHttpException |
110
|
|
|
*/ |
111
|
|
|
public function actionClearStatistics(): Response |
112
|
|
|
{ |
113
|
|
|
PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
114
|
|
|
$error = Retour::$plugin->statistics->clearStatistics(); |
115
|
|
|
Craft::info( |
116
|
|
|
Craft::t( |
117
|
|
|
'retour', |
118
|
|
|
'Retour statistics cleared: {error}', |
119
|
|
|
['error' => $error] |
120
|
|
|
), |
121
|
|
|
__METHOD__ |
122
|
|
|
); |
123
|
|
|
Retour::$plugin->clearAllCaches(); |
|
|
|
|
124
|
|
|
try { |
125
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('retour', 'Retour statistics cleared.')); |
126
|
|
|
} catch (MissingComponentException $e) { |
127
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->redirect('retour/dashboard'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
|
|
|
|
134
|
|
|
* @return ?Response |
135
|
|
|
* @throws MissingComponentException |
136
|
|
|
* @throws BadRequestHttpException |
137
|
|
|
* @throws ForbiddenHttpException |
138
|
|
|
*/ |
139
|
|
|
public function actionDeleteStatistics(): ?Response |
140
|
|
|
{ |
141
|
|
|
PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
142
|
|
|
$request = Craft::$app->getRequest(); |
143
|
|
|
$statisticIds = $request->getRequiredBodyParam('statisticIds'); |
144
|
|
|
$stickyError = false; |
145
|
|
|
foreach ($statisticIds as $statisticId) { |
146
|
|
|
if (Retour::$plugin->statistics->deleteStatisticById($statisticId) === 0) { |
147
|
|
|
$stickyError = true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
Craft::info( |
151
|
|
|
Craft::t( |
152
|
|
|
'retour', |
153
|
|
|
'Retour statistics deleted: {error}', |
154
|
|
|
['error' => $stickyError] |
155
|
|
|
), |
156
|
|
|
__METHOD__ |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
Retour::$plugin->clearAllCaches(); |
160
|
|
|
// Handle any cumulative errors |
161
|
|
|
if (!$stickyError) { |
162
|
|
|
// Clear the caches and continue on |
163
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('retour', 'Retour statistics deleted.')); |
164
|
|
|
|
165
|
|
|
return $this->redirect('retour/dashboard'); |
166
|
|
|
} |
167
|
|
|
Craft::$app->getSession()->setError(Craft::t('retour', "Couldn't delete statistic.")); |
168
|
|
|
|
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|