|
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\helpers\Permission as PermissionHelper; |
|
15
|
|
|
|
|
16
|
|
|
use Craft; |
|
|
|
|
|
|
17
|
|
|
use craft\web\Controller; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use yii\web\Response; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
|
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
23
|
|
|
* @package Webperf |
|
|
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
class DataSamplesController extends Controller |
|
27
|
|
|
{ |
|
28
|
|
|
// Constants |
|
29
|
|
|
// ========================================================================= |
|
30
|
|
|
|
|
31
|
|
|
// Public Methods |
|
32
|
|
|
// ========================================================================= |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
|
|
|
|
|
35
|
|
|
* @param int $id |
|
|
|
|
|
|
36
|
|
|
* |
|
37
|
|
|
* @return Response |
|
38
|
|
|
* @throws \craft\errors\MissingComponentException |
|
39
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function actionDeleteSampleById(int $id): Response |
|
42
|
|
|
{ |
|
43
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples'); |
|
44
|
|
|
if (Webperf::$plugin->dataSamples->deleteSampleById($id)) { |
|
45
|
|
|
// Clear the caches and continue on |
|
46
|
|
|
Webperf::$plugin->clearAllCaches(); |
|
47
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('webperf', 'Data sample deleted.')); |
|
48
|
|
|
|
|
49
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
50
|
|
|
} |
|
51
|
|
|
Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data sample.")); |
|
52
|
|
|
|
|
53
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
|
|
|
|
|
57
|
|
|
* @param string $pageUrl |
|
|
|
|
|
|
58
|
|
|
* @param int|null $siteId |
|
|
|
|
|
|
59
|
|
|
* |
|
60
|
|
|
* @return Response |
|
61
|
|
|
* @throws \craft\errors\MissingComponentException |
|
62
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
63
|
|
|
*/ |
|
64
|
|
|
public function actionDeleteSamplesByUrl(string $pageUrl, int $siteId = null): Response |
|
65
|
|
|
{ |
|
66
|
|
|
// We may be passed 0 or other "empty" values, so coerce to null |
|
67
|
|
|
if (empty($siteId)) { |
|
68
|
|
|
$siteId = null; |
|
69
|
|
|
} |
|
70
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples'); |
|
71
|
|
|
if (Webperf::$plugin->dataSamples->deleteDataSamplesByUrl($pageUrl, $siteId)) { |
|
72
|
|
|
// Clear the caches and continue on |
|
73
|
|
|
Webperf::$plugin->clearAllCaches(); |
|
74
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('webperf', 'Data samples deleted.')); |
|
75
|
|
|
|
|
76
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
77
|
|
|
} |
|
78
|
|
|
Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data samples.")); |
|
79
|
|
|
|
|
80
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
|
|
|
|
|
84
|
|
|
* @param int|null $siteId |
|
|
|
|
|
|
85
|
|
|
* |
|
86
|
|
|
* @return Response |
|
87
|
|
|
* @throws \craft\errors\MissingComponentException |
|
88
|
|
|
* @throws \yii\web\ForbiddenHttpException |
|
89
|
|
|
*/ |
|
90
|
|
|
public function actionDeleteAllSamples(int $siteId = null): Response |
|
91
|
|
|
{ |
|
92
|
|
|
// We may be passed 0 or other "empty" values, so coerce to null |
|
93
|
|
|
if (empty($siteId)) { |
|
94
|
|
|
$siteId = null; |
|
95
|
|
|
} |
|
96
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples'); |
|
97
|
|
|
if (Webperf::$plugin->dataSamples->deleteAllDataSamples($siteId)) { |
|
98
|
|
|
// Clear the caches and continue on |
|
99
|
|
|
Webperf::$plugin->clearAllCaches(); |
|
100
|
|
|
Craft::$app->getSession()->setNotice(Craft::t('webperf', 'All data samples deleted.')); |
|
101
|
|
|
|
|
102
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
103
|
|
|
} |
|
104
|
|
|
Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data samples.")); |
|
105
|
|
|
|
|
106
|
|
|
return $this->redirect(Craft::$app->getRequest()->referrer); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|