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\helpers\Permission as PermissionHelper; |
14
|
|
|
|
15
|
|
|
use craft\db\Query; |
|
|
|
|
16
|
|
|
use craft\helpers\UrlHelper; |
|
|
|
|
17
|
|
|
use craft\web\Controller; |
|
|
|
|
18
|
|
|
|
19
|
|
|
use yii\web\ForbiddenHttpException; |
|
|
|
|
20
|
|
|
use yii\web\Response; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Webperf |
|
|
|
|
25
|
|
|
* @since 1.0.0 |
|
|
|
|
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
class TablesController extends Controller |
28
|
|
|
{ |
29
|
|
|
// Constants |
30
|
|
|
// ========================================================================= |
31
|
|
|
|
32
|
|
|
// Protected Properties |
33
|
|
|
// ========================================================================= |
34
|
|
|
|
35
|
|
|
/** |
|
|
|
|
36
|
|
|
* @var bool|array |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
protected $allowAnonymous = [ |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
// Public Methods |
42
|
|
|
// ========================================================================= |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handle requests for the dashboard statistics table |
46
|
|
|
* |
47
|
|
|
* @param string $sort |
|
|
|
|
48
|
|
|
* @param int $page |
|
|
|
|
49
|
|
|
* @param int $per_page |
|
|
|
|
50
|
|
|
* @param string $filter |
|
|
|
|
51
|
|
|
* @param null $siteId |
|
|
|
|
52
|
|
|
* |
53
|
|
|
* @return Response |
54
|
|
|
* @throws ForbiddenHttpException |
55
|
|
|
*/ |
56
|
|
|
public function actionDashboard( |
57
|
|
|
string $sort = 'hitCount|desc', |
58
|
|
|
int $page = 1, |
59
|
|
|
int $per_page = 20, |
60
|
|
|
$filter = '', |
61
|
|
|
$siteId = 0 |
62
|
|
|
): Response { |
63
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:dashboard'); |
64
|
|
|
$data = []; |
65
|
|
|
$sortField = 'hitCount'; |
66
|
|
|
$sortType = 'DESC'; |
67
|
|
|
// Figure out the sorting type |
68
|
|
|
if ($sort !== '') { |
69
|
|
|
if (strpos($sort, '|') === false) { |
70
|
|
|
$sortField = $sort; |
71
|
|
|
} else { |
72
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
// Query the db table |
76
|
|
|
$offset = ($page - 1) * $per_page; |
77
|
|
|
$query = (new Query()) |
78
|
|
|
->from(['{{%retour_stats}}']) |
79
|
|
|
->offset($offset) |
80
|
|
|
->limit($per_page) |
81
|
|
|
->orderBy("{$sortField} {$sortType}"); |
82
|
|
|
if ((int)$siteId !== 0) { |
83
|
|
|
$query->where(['siteId' => $siteId]); |
84
|
|
|
} |
85
|
|
|
if ($filter !== '') { |
86
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
87
|
|
|
$query->orWhere(['like', 'referrerUrl', $filter]); |
88
|
|
|
} |
89
|
|
|
$stats = $query->all(); |
90
|
|
|
if ($stats) { |
91
|
|
|
// Add in the `addLink` field |
92
|
|
|
foreach ($stats as &$stat) { |
93
|
|
|
$stat['addLink'] = ''; |
94
|
|
|
if (!$stat['handledByRetour']) { |
95
|
|
|
$encodedUrl = urlencode('/'.ltrim($stat['redirectSrcUrl'], '/')); |
96
|
|
|
$stat['addLink'] = UrlHelper::cpUrl('retour/add-redirect', [ |
|
|
|
|
97
|
|
|
'defaultUrl' => $encodedUrl |
98
|
|
|
]); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
// Format the data for the API |
102
|
|
|
$data['data'] = $stats; |
103
|
|
|
$query = (new Query()) |
104
|
|
|
->from(['{{%retour_stats}}']); |
105
|
|
|
if ($filter !== '') { |
106
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
107
|
|
|
$query->orWhere(['like', 'referrerUrl', $filter]); |
108
|
|
|
} |
109
|
|
|
$count = $query->count(); |
110
|
|
|
$data['links']['pagination'] = [ |
111
|
|
|
'total' => $count, |
112
|
|
|
'per_page' => $per_page, |
113
|
|
|
'current_page' => $page, |
114
|
|
|
'last_page' => ceil($count / $per_page), |
115
|
|
|
'next_page_url' => null, |
116
|
|
|
'prev_page_url' => null, |
117
|
|
|
'from' => $offset + 1, |
118
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
119
|
|
|
]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->asJson($data); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Handle requests for the dashboard redirects table |
127
|
|
|
* |
128
|
|
|
* @param string $sort |
|
|
|
|
129
|
|
|
* @param int $page |
|
|
|
|
130
|
|
|
* @param int $per_page |
|
|
|
|
131
|
|
|
* @param string $filter |
|
|
|
|
132
|
|
|
* @param null $siteId |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return Response |
135
|
|
|
* @throws ForbiddenHttpException |
136
|
|
|
*/ |
137
|
|
|
public function actionRedirects( |
138
|
|
|
string $sort = 'hitCount|desc', |
139
|
|
|
int $page = 1, |
140
|
|
|
int $per_page = 20, |
141
|
|
|
$filter = '', |
142
|
|
|
$siteId = 0 |
143
|
|
|
): Response { |
144
|
|
|
PermissionHelper::controllerPermissionCheck('webperf:redirects'); |
145
|
|
|
$data = []; |
146
|
|
|
$sortField = 'hitCount'; |
147
|
|
|
$sortType = 'DESC'; |
148
|
|
|
// Figure out the sorting type |
149
|
|
|
if ($sort !== '') { |
150
|
|
|
if (strpos($sort, '|') === false) { |
151
|
|
|
$sortField = $sort; |
152
|
|
|
} else { |
153
|
|
|
list($sortField, $sortType) = explode('|', $sort); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
// Query the db table |
157
|
|
|
$offset = ($page - 1) * $per_page; |
158
|
|
|
$query = (new Query()) |
159
|
|
|
->from(['{{%retour_static_redirects}}']) |
160
|
|
|
->offset($offset) |
161
|
|
|
->limit($per_page) |
162
|
|
|
->orderBy("{$sortField} {$sortType}"); |
163
|
|
|
if ((int)$siteId !== 0) { |
164
|
|
|
$query->where(['siteId' => $siteId]); |
165
|
|
|
} |
166
|
|
|
if ($filter !== '') { |
167
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
168
|
|
|
$query->orWhere(['like', 'redirectDestUrl', $filter]); |
169
|
|
|
} |
170
|
|
|
$redirects = $query->all(); |
171
|
|
|
// Add in the `deleteLink` field |
172
|
|
|
foreach ($redirects as &$redirect) { |
173
|
|
|
$redirect['deleteLink'] = UrlHelper::cpUrl('retour/delete-redirect/'.$redirect['id']); |
174
|
|
|
$redirect['editLink'] = UrlHelper::cpUrl('retour/edit-redirect/'.$redirect['id']); |
175
|
|
|
} |
176
|
|
|
// Format the data for the API |
177
|
|
|
if ($redirects) { |
178
|
|
|
$data['data'] = $redirects; |
179
|
|
|
$query = (new Query()) |
180
|
|
|
->from(['{{%retour_static_redirects}}']); |
181
|
|
|
if ($filter !== '') { |
182
|
|
|
$query->where(['like', 'redirectSrcUrl', $filter]); |
183
|
|
|
$query->orWhere(['like', 'redirectDestUrl', $filter]); |
184
|
|
|
} |
185
|
|
|
$count = $query->count(); |
186
|
|
|
$data['links']['pagination'] = [ |
187
|
|
|
'total' => $count, |
188
|
|
|
'per_page' => $per_page, |
189
|
|
|
'current_page' => $page, |
190
|
|
|
'last_page' => ceil($count / $per_page), |
191
|
|
|
'next_page_url' => null, |
192
|
|
|
'prev_page_url' => null, |
193
|
|
|
'from' => $offset + 1, |
194
|
|
|
'to' => $offset + ($count > $per_page ? $per_page : $count), |
195
|
|
|
]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this->asJson($data); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// Protected Methods |
202
|
|
|
// ========================================================================= |
203
|
|
|
} |
204
|
|
|
|