-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetRepository.php
More file actions
316 lines (272 loc) · 11.2 KB
/
WidgetRepository.php
File metadata and controls
316 lines (272 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
namespace App\Repositories;
use App\Models\Event;
use App\Models\Stream;
use App\Models\WidgetAlert;
use App\Models\WidgetCard;
use App\Models\WidgetDonation;
use Exception;
use PDO;
class WidgetRepository
{
public function __construct(
private PDO $pdo,
private string $prefix
) {}
public function selectDonationWidgetByGuid(?string $streamGuid, ?string $eventGuid): ?WidgetDonation
{
$stmt = $this->pdo->prepare('
SELECT *
FROM ' . $this->prefix . 'widget_donation_goal_bar
WHERE charity_stream_guid = ?
OR charity_event_guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, WidgetDonation::class);
$stmt->execute([$streamGuid, $eventGuid]);
return $stmt->fetch() ?: null;
}
public function selectAlertWidgetByGuid(string $guid): ?WidgetAlert
{
$stmt = $this->pdo->prepare('
SELECT *
FROM ' . $this->prefix . 'widget_alert_box
WHERE charity_stream_guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, WidgetAlert::class);
$stmt->execute([$guid]);
return $stmt->fetch() ?: null;
}
public function updateDonationWidget(?string $streamGuid, ?string $eventGuid, array $data): void
{
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_donation_goal_bar
SET text_color_main = ?, text_color_alt = ?, text_content = ?, bar_color = ?, background_color = ?, goal = ?
WHERE charity_stream_guid = ?
OR charity_event_guid = ?
');
$stmt->execute([
$data['text_color_main'],
$data['text_color_alt'],
$data['text_content'],
$data['bar_color'],
$data['background_color'],
$data['goal'],
$streamGuid,
$eventGuid
]);
}
public function updateAlertWidget(string $guid, array $postData, ?string $image = null, ?string $sound = null): void
{
$this->pdo->beginTransaction();
try {
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_alert_box
SET alert_duration = ?, message_template = ?, sound_volume = ?
WHERE charity_stream_guid = ?
');
$stmt->execute([
$postData['alert_duration'],
$postData['message_template'],
$postData['sound_volume'],
$guid
]);
if (isset($image)) {
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_alert_box
SET image = ?
WHERE charity_stream_guid = ?
');
$stmt->execute([
$image,
$guid
]);
}
if (isset($sound)) {
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_alert_box
SET sound = ?
WHERE charity_stream_guid = ?
');
$stmt->execute([
$sound,
$guid
]);
}
$this->pdo->commit();
} catch (Exception $e) {
$this->pdo->rollBack();
throw $e;
}
}
// ── Generic cache helpers ────────────────────────────────────
private function selectCacheData(string $table, string $column, string $guid): ?array
{
$stmt = $this->pdo->prepare(
'SELECT cache_data, cache_updated_at FROM ' . $this->prefix . $table . ' WHERE ' . $column . ' = ?'
);
$stmt->execute([$guid]);
$data = $stmt->fetch();
if (!$data || !$data['cache_data']) {
return null;
}
$result = json_decode($data['cache_data'], true);
if ($result !== null && isset($data['cache_updated_at'])) {
$result['_cache_updated_at'] = $data['cache_updated_at'];
}
return $result;
}
private function updateCacheData(string $table, string $column, string $guid, array $data): void
{
// Remove internal metadata before persisting
unset($data['_cache_updated_at']);
$stmt = $this->pdo->prepare(
'UPDATE ' . $this->prefix . $table . ' SET cache_data = ?, cache_updated_at = NOW(6) WHERE ' . $column . ' = ?'
);
$stmt->execute([json_encode($data), $guid]);
}
/**
* Vérifie si le cache est encore frais (non expiré) selon le TTL donné en secondes.
*/
public function isCacheFresh(?array $cacheData, int $ttlSeconds): bool
{
if ($cacheData === null || !isset($cacheData['_cache_updated_at'])) {
return false;
}
$updatedAt = new \DateTime($cacheData['_cache_updated_at']);
$now = new \DateTime();
$age = $now->getTimestamp() - $updatedAt->getTimestamp();
return $age < $ttlSeconds;
}
// ── Alert widget cache ────────────────────────────────────────
public function selectAlertWidgetCacheData(Stream $stream): ?array
{
return $this->selectCacheData('widget_alert_box', 'charity_stream_guid', $stream->guid);
}
public function updateAlertWidgetCacheData(string $streamGuid, array $data): void
{
$this->updateCacheData('widget_alert_box', 'charity_stream_guid', $streamGuid, $data);
}
// ── Donation widget cache ─────────────────────────────────────
public function selectStreamDonationWidgetCacheData(Stream $stream): ?array
{
return $this->selectCacheData('widget_donation_goal_bar', 'charity_stream_guid', $stream->guid);
}
public function updateStreamDonationWidgetCacheData(string $streamGuid, array $data): void
{
$this->updateCacheData('widget_donation_goal_bar', 'charity_stream_guid', $streamGuid, $data);
}
public function selectEventDonationWidgetCacheData(Event $event): ?array
{
return $this->selectCacheData('widget_donation_goal_bar', 'charity_event_guid', $event->guid);
}
public function updateEventDonationWidgetCacheData(string $eventGuid, array $data): void
{
$this->updateCacheData('widget_donation_goal_bar', 'charity_event_guid', $eventGuid, $data);
}
// ── Widget Card ──────────────────────────────────────────────
public function selectCardWidgetByGuid(?string $streamGuid, ?string $eventGuid): ?WidgetCard
{
try {
$stmt = $this->pdo->prepare('
SELECT *
FROM ' . $this->prefix . 'widget_card
WHERE charity_stream_guid = ?
OR charity_event_guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, WidgetCard::class);
$stmt->execute([$streamGuid, $eventGuid]);
return $stmt->fetch() ?: null;
} catch (Exception $e) {
// Table may not exist yet (migration not run)
return null;
}
}
public function insertCardWidget(?string $streamGuid, ?string $eventGuid): void
{
$stmt = $this->pdo->prepare('
INSERT INTO ' . $this->prefix . 'widget_card (charity_stream_guid, charity_event_guid, description)
VALUES (?, ?, "")
');
$stmt->execute([$streamGuid, $eventGuid]);
}
public function updateCardWidget(?string $streamGuid, ?string $eventGuid, array $data, ?string $image = null): void
{
$this->pdo->beginTransaction();
try {
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_card
SET tag = ?, title = ?, description = ?, goal = ?,
background_color = ?, bar_color = ?, bar_background_color = ?,
text_color = ?, tag_color = ?, tag_background_color = ?
WHERE charity_stream_guid = ?
OR charity_event_guid = ?
');
$stmt->execute([
$data['card_tag'] ?? '',
$data['card_title'] ?? '',
$data['card_description'] ?? '',
$data['card_goal'] ?? 1000,
$data['card_background_color'] ?? '#ffffff',
$data['card_bar_color'] ?? '#2563eb',
$data['card_bar_background_color'] ?? '#e5e7eb',
$data['card_text_color'] ?? '#1a1a1a',
$data['card_tag_color'] ?? '#166534',
$data['card_tag_background_color'] ?? '#dcfce7',
$streamGuid,
$eventGuid
]);
if ($image !== null) {
$stmt = $this->pdo->prepare('
UPDATE ' . $this->prefix . 'widget_card
SET image = ?
WHERE charity_stream_guid = ?
OR charity_event_guid = ?
');
$stmt->execute([$image, $streamGuid, $eventGuid]);
}
$this->pdo->commit();
} catch (Exception $e) {
$this->pdo->rollBack();
throw $e;
}
}
// ── Stream activity map ──────────────────────────────────────
/**
* Retourne une map guid => {amount, widget_last_update} pour tous les streams.
* Utilisé pour afficher un indicateur d'activité dans l'admin.
*/
public function selectStreamActivityMap(): array
{
$stmt = $this->pdo->query('
SELECT charity_stream_guid AS guid, cache_data, last_update AS widget_last_update
FROM ' . $this->prefix . 'widget_donation_goal_bar
WHERE charity_stream_guid IS NOT NULL
');
$map = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$cacheData = json_decode($row['cache_data'] ?? '', true);
$map[$row['guid']] = [
'amount' => $cacheData['amount'] ?? 0,
'widget_last_update' => $row['widget_last_update'],
];
}
return $map;
}
// ── Card widget cache ─────────────────────────────────────────
public function selectStreamCardWidgetCacheData(Stream $stream): ?array
{
return $this->selectCacheData('widget_card', 'charity_stream_guid', $stream->guid);
}
public function updateStreamCardWidgetCacheData(string $streamGuid, array $data): void
{
$this->updateCacheData('widget_card', 'charity_stream_guid', $streamGuid, $data);
}
public function selectEventCardWidgetCacheData(Event $event): ?array
{
return $this->selectCacheData('widget_card', 'charity_event_guid', $event->guid);
}
public function updateEventCardWidgetCacheData(string $eventGuid, array $data): void
{
$this->updateCacheData('widget_card', 'charity_event_guid', $eventGuid, $data);
}
}