1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS 3.x |
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 nystudio107\retour\Retour; |
15
|
|
|
use nystudio107\retour\assetbundles\retour\RetourDashboardAsset; |
16
|
|
|
use nystudio107\retour\helpers\MultiSite as MultiSiteHelper; |
17
|
|
|
use nystudio107\retour\helpers\Permission as PermissionHelper; |
18
|
|
|
|
19
|
|
|
use Craft; |
20
|
|
|
use craft\web\Controller; |
21
|
|
|
use craft\errors\MissingComponentException; |
22
|
|
|
use craft\helpers\UrlHelper; |
23
|
|
|
|
24
|
|
|
use yii\base\InvalidConfigException; |
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
|
|
|
const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/'; |
39
|
|
|
|
40
|
|
|
// Protected Properties |
41
|
|
|
// ========================================================================= |
42
|
|
|
|
43
|
|
|
protected $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 \yii\web\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/assetbundles/retour/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 \yii\web\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 \yii\web\ForbiddenHttpException |
136
|
|
|
*/ |
137
|
|
|
public function actionDeleteStatistics(): Response |
138
|
|
|
{ |
139
|
|
|
PermissionHelper::controllerPermissionCheck('retour:dashboard'); |
140
|
|
|
$request = Craft::$app->getRequest(); |
141
|
|
|
$statisticIds = $request->getRequiredBodyParam('statisticIds'); |
142
|
|
|
$stickyError = false; |
143
|
|
|
foreach ($statisticIds as $statisticId) { |
144
|
|
|
if (!Retour::$plugin->statistics->deleteStatisticById($statisticId)) { |
145
|
|
|
$stickyError = true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
Craft::info( |
149
|
|
|
Craft::t( |
150
|
|
|
'retour', |
151
|
|
|
'Retour statistics deleted: {error}', |
152
|
|
|
['error' => $stickyError] |
153
|
|
|
), |
154
|
|
|
__METHOD__ |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
Retour::$plugin->clearAllCaches(); |
158
|
|
|
// Handle any cumulative errors |
159
|
|
|
if (!$stickyError) { |
160
|
|
|
// Clear the caches and continue on |
161
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('retour', 'Retour statistics deleted.')); |
162
|
|
|
|
163
|
|
|
return $this->redirect('retour/dashboard'); |
164
|
|
|
} |
165
|
|
|
Craft::$app->getSession()->setError(Craft::t('retour', "Couldn't delete statistic.")); |
166
|
|
|
|
167
|
|
|
return null; |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|