1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://dukt.net/analytics/ |
4
|
|
|
* @copyright Copyright (c) 2022, Dukt |
5
|
|
|
* @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dukt\analytics\services; |
9
|
|
|
|
10
|
|
|
use Craft; |
11
|
|
|
use dukt\analytics\errors\InvalidViewException; |
12
|
|
|
use dukt\analytics\models\SiteView; |
13
|
|
|
use dukt\analytics\models\View; |
14
|
|
|
use dukt\analytics\records\View as ViewRecord; |
15
|
|
|
use dukt\analytics\records\SiteView as SiteViewRecord; |
16
|
|
|
use yii\base\Component; |
17
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
class Views extends Component |
20
|
|
|
{ |
21
|
|
|
// Public Methods |
22
|
|
|
// ========================================================================= |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get all views. |
26
|
|
|
* |
27
|
|
|
* @return array|null |
28
|
|
|
*/ |
29
|
|
|
public function getViews() |
30
|
|
|
{ |
31
|
|
|
$results = ViewRecord::find()->all(); |
32
|
|
|
|
33
|
|
|
$views = []; |
34
|
|
|
|
35
|
|
|
foreach ($results as $result) { |
36
|
|
|
$views[] = new View($result->toArray([ |
37
|
|
|
'id', |
38
|
|
|
'name', |
39
|
|
|
'gaAccountId', |
40
|
|
|
'gaAccountName', |
41
|
|
|
'gaPropertyId', |
42
|
|
|
'gaPropertyName', |
43
|
|
|
'gaViewId', |
44
|
|
|
'gaViewName', |
45
|
|
|
])); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $views; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get view by ID. |
53
|
|
|
* |
54
|
|
|
* @param $id |
55
|
|
|
* |
56
|
|
|
* @return View|null |
57
|
|
|
*/ |
58
|
|
|
public function getViewById($id) |
59
|
|
|
{ |
60
|
|
|
$result = ViewRecord::findOne($id); |
61
|
|
|
|
62
|
|
|
if ($result) { |
|
|
|
|
63
|
|
|
return new View($result->toArray([ |
64
|
|
|
'id', |
65
|
|
|
'name', |
66
|
|
|
'gaAccountId', |
67
|
|
|
'gaAccountName', |
68
|
|
|
'gaPropertyId', |
69
|
|
|
'gaPropertyName', |
70
|
|
|
'gaViewCurrency', |
71
|
|
|
'gaViewId', |
72
|
|
|
'gaViewName', |
73
|
|
|
])); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get site views. |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function getSiteViews() |
85
|
|
|
{ |
86
|
|
|
$results = SiteViewRecord::find()->all(); |
87
|
|
|
|
88
|
|
|
$views = []; |
89
|
|
|
|
90
|
|
|
foreach ($results as $result) { |
91
|
|
|
$views[] = new SiteView($result->toArray([ |
92
|
|
|
'siteId', |
93
|
|
|
'viewId', |
94
|
|
|
])); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $views; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get ite view by site ID. |
102
|
|
|
* |
103
|
|
|
* @param $id |
104
|
|
|
* |
105
|
|
|
* @return SiteView|null |
106
|
|
|
*/ |
107
|
|
|
public function getSiteViewBySiteId($id) |
108
|
|
|
{ |
109
|
|
|
$result = SiteViewRecord::findOne([ |
110
|
|
|
'siteId' => $id |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
if ($result) { |
|
|
|
|
114
|
|
|
return new SiteView($result->toArray([ |
115
|
|
|
'id', |
116
|
|
|
'siteId', |
117
|
|
|
'viewId', |
118
|
|
|
])); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Saves a view. |
126
|
|
|
* |
127
|
|
|
* @param View $view The view to be saved |
128
|
|
|
* @param bool $runValidation Whether the view should be validated |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
* @throws InvalidViewException if $view->id is invalid |
132
|
|
|
* @throws Exception if reasons |
133
|
|
|
*/ |
134
|
|
|
public function saveView(View $view, bool $runValidation = true): bool |
135
|
|
|
{ |
136
|
|
|
if ($runValidation && !$view->validate()) { |
137
|
|
|
Craft::info('View not saved due to validation error.', __METHOD__); |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($view->id) { |
143
|
|
|
$viewRecord = ViewRecord::find() |
144
|
|
|
->where(['id' => $view->id]) |
145
|
|
|
->one(); |
146
|
|
|
|
147
|
|
|
if (!$viewRecord) { |
148
|
|
|
throw new InvalidViewException("No view exists with the ID '{$view->id}'"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$isNewView = false; |
152
|
|
|
} else { |
153
|
|
|
$viewRecord = new ViewRecord(); |
154
|
|
|
$isNewView = true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// Shared attributes |
158
|
|
|
$viewRecord->name = $view->name; |
|
|
|
|
159
|
|
|
$viewRecord->gaAccountId = $view->gaAccountId; |
|
|
|
|
160
|
|
|
$viewRecord->gaAccountName = $view->gaAccountName; |
|
|
|
|
161
|
|
|
$viewRecord->gaPropertyId = $view->gaPropertyId; |
|
|
|
|
162
|
|
|
$viewRecord->gaPropertyName = $view->gaPropertyName; |
|
|
|
|
163
|
|
|
$viewRecord->gaViewId = $view->gaViewId; |
|
|
|
|
164
|
|
|
$viewRecord->gaViewName = $view->gaViewName; |
|
|
|
|
165
|
|
|
$viewRecord->gaViewCurrency = $view->gaViewCurrency; |
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
$transaction = Craft::$app->getDb()->beginTransaction(); |
169
|
|
|
|
170
|
|
|
try { |
171
|
|
|
// Is the event giving us the go-ahead? |
172
|
|
|
$viewRecord->save(false); |
173
|
|
|
|
174
|
|
|
// Now that we have a view ID, save it on the model |
175
|
|
|
if ($isNewView) { |
176
|
|
|
$view->id = $viewRecord->id; |
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$transaction->commit(); |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
$transaction->rollBack(); |
182
|
|
|
|
183
|
|
|
throw $e; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Delete a view by ID. |
191
|
|
|
* |
192
|
|
|
* @param int $viewId |
193
|
|
|
* |
194
|
|
|
* @return bool |
195
|
|
|
* @throws \Throwable |
196
|
|
|
* @throws \yii\db\StaleObjectException |
197
|
|
|
*/ |
198
|
|
|
public function deleteViewById(int $viewId): bool |
199
|
|
|
{ |
200
|
|
|
$viewRecord = ViewRecord::findOne($viewId); |
201
|
|
|
|
202
|
|
|
if (!$viewRecord) { |
|
|
|
|
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$viewRecord->delete(); |
207
|
|
|
|
208
|
|
|
return true; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Save a site view. |
213
|
|
|
* |
214
|
|
|
* @param SiteView $siteView |
215
|
|
|
* @param bool $runValidation |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
* @throws \yii\db\Exception |
219
|
|
|
*/ |
220
|
|
|
public function saveSiteView(SiteView $siteView, bool $runValidation = true): bool |
221
|
|
|
{ |
222
|
|
|
if ($runValidation && !$siteView->validate()) { |
223
|
|
|
Craft::info('Site view not saved due to validation error.', __METHOD__); |
224
|
|
|
|
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if ($siteView->siteId) { |
229
|
|
|
$siteViewRecord = SiteViewRecord::find() |
230
|
|
|
->where(['siteId' => $siteView->siteId]) |
231
|
|
|
->one(); |
232
|
|
|
|
233
|
|
|
if (!$siteViewRecord) { |
234
|
|
|
$siteViewRecord = new SiteViewRecord(); |
235
|
|
|
$isNewSiteView = true; |
236
|
|
|
} else { |
237
|
|
|
$isNewSiteView = false; |
238
|
|
|
} |
239
|
|
|
} else { |
240
|
|
|
$siteViewRecord = new SiteViewRecord(); |
241
|
|
|
$isNewSiteView = true; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// Shared attributes |
245
|
|
|
$siteViewRecord->siteId = $siteView->siteId; |
|
|
|
|
246
|
|
|
$siteViewRecord->viewId = $siteView->viewId; |
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
$transaction = Craft::$app->getDb()->beginTransaction(); |
250
|
|
|
|
251
|
|
|
try { |
252
|
|
|
// Is the event giving us the go-ahead? |
253
|
|
|
$siteViewRecord->save(false); |
254
|
|
|
|
255
|
|
|
// Now that we have a view ID, save it on the model |
256
|
|
|
if ($isNewSiteView) { |
257
|
|
|
$siteView->id = $siteViewRecord->id; |
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$transaction->commit(); |
261
|
|
|
} catch (Exception $e) { |
262
|
|
|
$transaction->rollBack(); |
263
|
|
|
|
264
|
|
|
throw $e; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return true; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|