|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webperf plugin for Craft CMS 3.x |
|
4
|
|
|
* |
|
5
|
|
|
* Monitor the performance of your webpages through real-world user timing data |
|
6
|
|
|
* |
|
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace nystudio107\webperf\controllers; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\webperf\Webperf; |
|
14
|
|
|
use nystudio107\webperf\assetbundles\webperf\WebperfDashboardAsset; |
|
15
|
|
|
use nystudio107\webperf\helpers\MultiSite as MultiSiteHelper; |
|
16
|
|
|
use nystudio107\webperf\helpers\Permission as PermissionHelper; |
|
17
|
|
|
|
|
18
|
|
|
use Craft; |
|
|
|
|
|
|
19
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
|
|
20
|
|
|
use craft\web\Controller; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
|
|
23
|
|
|
use yii\web\NotFoundHttpException; |
|
|
|
|
|
|
24
|
|
|
use yii\web\Response; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
|
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
28
|
|
|
* @package Webperf |
|
|
|
|
|
|
29
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
|
class SectionsController extends Controller |
|
32
|
|
|
{ |
|
33
|
|
|
// Constants |
|
34
|
|
|
// ========================================================================= |
|
35
|
|
|
|
|
36
|
|
|
const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-webperf/'; |
|
37
|
|
|
|
|
38
|
|
|
// Protected Properties |
|
39
|
|
|
// ========================================================================= |
|
40
|
|
|
|
|
41
|
|
|
protected $allowAnonymous = []; |
|
42
|
|
|
|
|
43
|
|
|
// Public Methods |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
|
|
|
|
|
47
|
|
|
* @param string|null $siteHandle |
|
|
|
|
|
|
48
|
|
|
* @param bool $showWelcome |
|
|
|
|
|
|
49
|
|
|
* |
|
50
|
|
|
* @return Response |
|
51
|
|
|
* @throws NotFoundHttpException |
|
52
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response |
|
55
|
|
|
{ |
|
56
|
|
|
$variables = []; |
|
57
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
|
58
|
|
|
// Trim the statistics |
|
59
|
|
|
Webperf::$plugin->dataSamples->trimOrphanedSamples(1024); |
|
60
|
|
|
Webperf::$plugin->dataSamples->trimDataSamples(); |
|
61
|
|
|
// Get the site to edit |
|
62
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
|
63
|
|
|
$pluginName = Webperf::$settings->pluginName; |
|
64
|
|
|
$templateTitle = Craft::t('webperf', 'Dashboard'); |
|
65
|
|
|
$view = Craft::$app->getView(); |
|
66
|
|
|
// Asset bundle |
|
67
|
|
|
try { |
|
68
|
|
|
$view->registerAssetBundle(WebperfDashboardAsset::class); |
|
69
|
|
|
} catch (InvalidConfigException $e) { |
|
70
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
71
|
|
|
} |
|
72
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
|
73
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
|
74
|
|
|
true |
|
75
|
|
|
); |
|
76
|
|
|
// Enabled sites |
|
77
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
|
78
|
|
|
$variables['controllerHandle'] = 'dashboard'; |
|
79
|
|
|
|
|
80
|
|
|
// Basic variables |
|
81
|
|
|
$variables['fullPageForm'] = false; |
|
82
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
|
83
|
|
|
$variables['pluginName'] = $pluginName; |
|
84
|
|
|
$variables['title'] = $templateTitle; |
|
85
|
|
|
$variables['crumbs'] = [ |
|
86
|
|
|
[ |
|
87
|
|
|
'label' => $pluginName, |
|
88
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
|
89
|
|
|
], |
|
90
|
|
|
[ |
|
91
|
|
|
'label' => $templateTitle, |
|
92
|
|
|
'url' => UrlHelper::cpUrl('webperf/dashboard'), |
|
93
|
|
|
], |
|
94
|
|
|
]; |
|
95
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
|
96
|
|
|
$variables['selectedSubnavItem'] = 'dashboard'; |
|
97
|
|
|
$variables['showWelcome'] = $showWelcome; |
|
98
|
|
|
$variables['settings'] = Webperf::$settings; |
|
99
|
|
|
|
|
100
|
|
|
// Render the template |
|
101
|
|
|
return $this->renderTemplate('webperf/dashboard/index', $variables); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
|
|
|
|
|
105
|
|
|
* @param string|null $siteHandle |
|
|
|
|
|
|
106
|
|
|
* @param bool $showWelcome |
|
|
|
|
|
|
107
|
|
|
* |
|
108
|
|
|
* @return Response |
|
109
|
|
|
* @throws NotFoundHttpException |
|
110
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
111
|
|
|
*/ |
|
112
|
|
|
public function actionPagesIndex(string $siteHandle = null): Response |
|
113
|
|
|
{ |
|
114
|
|
|
$variables = []; |
|
115
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:pages'); |
|
116
|
|
|
// Trim the statistics |
|
117
|
|
|
Webperf::$plugin->dataSamples->trimOrphanedSamples(1024); |
|
118
|
|
|
Webperf::$plugin->dataSamples->trimDataSamples(); |
|
119
|
|
|
// Get the site to edit |
|
120
|
|
|
$siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle); |
|
121
|
|
|
$pluginName = Webperf::$settings->pluginName; |
|
122
|
|
|
$templateTitle = Craft::t('webperf', 'Pages'); |
|
123
|
|
|
$view = Craft::$app->getView(); |
|
124
|
|
|
// Asset bundle |
|
125
|
|
|
try { |
|
126
|
|
|
$view->registerAssetBundle(WebperfDashboardAsset::class); |
|
127
|
|
|
} catch (InvalidConfigException $e) { |
|
128
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
129
|
|
|
} |
|
130
|
|
|
$variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl( |
|
131
|
|
|
'@nystudio107/webperf/assetbundles/webperf/dist', |
|
132
|
|
|
true |
|
133
|
|
|
); |
|
134
|
|
|
// Enabled sites |
|
135
|
|
|
MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables); |
|
136
|
|
|
$variables['controllerHandle'] = 'dashboard'; |
|
137
|
|
|
|
|
138
|
|
|
// Basic variables |
|
139
|
|
|
$variables['fullPageForm'] = false; |
|
140
|
|
|
$variables['docsUrl'] = self::DOCUMENTATION_URL; |
|
141
|
|
|
$variables['pluginName'] = $pluginName; |
|
142
|
|
|
$variables['title'] = $templateTitle; |
|
143
|
|
|
$variables['crumbs'] = [ |
|
144
|
|
|
[ |
|
145
|
|
|
'label' => $pluginName, |
|
146
|
|
|
'url' => UrlHelper::cpUrl('webperf'), |
|
147
|
|
|
], |
|
148
|
|
|
[ |
|
149
|
|
|
'label' => $templateTitle, |
|
150
|
|
|
'url' => UrlHelper::cpUrl('webperf/pages'), |
|
151
|
|
|
], |
|
152
|
|
|
]; |
|
153
|
|
|
$variables['docTitle'] = "{$pluginName} - {$templateTitle}"; |
|
154
|
|
|
$variables['selectedSubnavItem'] = 'pages'; |
|
155
|
|
|
$variables['settings'] = Webperf::$settings; |
|
156
|
|
|
|
|
157
|
|
|
// Render the template |
|
158
|
|
|
return $this->renderTemplate('webperf/pages/index', $variables); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|