Repository navigation
Expand file tree
/
Copy pathfeature_admin.php
More file actions
620 lines (533 loc) · 20.3 KB
/
Copy pathfeature_admin.php
File metadata and controls
620 lines (533 loc) · 20.3 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/functions.php';
require_admin_user();
$pageTitle = 'Feature Updates Admin';
$currentPage = 'features_admin';
$message = '';
$error = '';
$featureUpdates = [];
$editingFeatureId = isset($_GET['edit']) ? (int)$_GET['edit'] : 0;
$editingFeature = null;
function admin_feature_type_options(): array
{
return [
'feature' => 'New Feature',
'improvement' => 'Improvement',
'fix' => 'Bug Fix',
'beta' => 'Beta',
'roadmap' => 'Coming Soon',
'performance' => 'Performance',
'ui' => 'UI Update',
'security' => 'Security',
];
}
function admin_feature_status_options(): array
{
return [
'live' => 'Live',
'beta' => 'Beta',
'planned' => 'Planned',
];
}
function admin_feature_visibility_options(): array
{
return [
'public' => 'Public',
'beta' => 'Beta',
'internal' => 'Internal',
];
}
function admin_get_feature_update_by_id(int $featureId): ?array
{
$stmt = db()->prepare("
SELECT *
FROM feature_updates
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'id' => $featureId,
]);
$feature = $stmt->fetch(PDO::FETCH_ASSOC);
return $feature ?: null;
}
function admin_get_feature_updates(int $limit = 100): array
{
$stmt = db()->prepare("
SELECT *
FROM feature_updates
ORDER BY
COALESCE(release_date, DATE(created_at)) DESC,
id DESC
LIMIT :limit_count
");
$stmt->bindValue('limit_count', $limit, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
function admin_save_feature_update(?int $featureId, array $data): void
{
if ($featureId && $featureId > 0) {
$stmt = db()->prepare("
UPDATE feature_updates
SET
title = :title,
summary = :summary,
details = :details,
feature_type = :feature_type,
status = :status,
release_date = :release_date,
visibility = :visibility,
release_version = :release_version,
is_highlighted = :is_highlighted,
created_at = :created_at
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'title' => $data['title'],
'summary' => $data['summary'],
'details' => $data['details'],
'feature_type' => $data['feature_type'],
'status' => $data['status'],
'release_date' => $data['release_date'],
'visibility' => $data['visibility'],
'release_version' => $data['release_version'],
'is_highlighted' => $data['is_highlighted'],
'created_at' => $data['created_at'],
'id' => $featureId,
]);
return;
}
$stmt = db()->prepare("
INSERT INTO feature_updates (
title,
summary,
details,
feature_type,
status,
release_date,
visibility,
release_version,
is_highlighted,
created_at
)
VALUES (
:title,
:summary,
:details,
:feature_type,
:status,
:release_date,
:visibility,
:release_version,
:is_highlighted,
:created_at
)
");
$stmt->execute([
'title' => $data['title'],
'summary' => $data['summary'],
'details' => $data['details'],
'feature_type' => $data['feature_type'],
'status' => $data['status'],
'release_date' => $data['release_date'],
'visibility' => $data['visibility'],
'release_version' => $data['release_version'],
'is_highlighted' => $data['is_highlighted'],
'created_at' => $data['created_at'],
]);
}
function admin_delete_feature_update(int $featureId): void
{
$stmt = db()->prepare("
DELETE FROM feature_updates
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'id' => $featureId,
]);
}
function admin_toggle_feature_highlight(int $featureId, bool $isHighlighted): void
{
$stmt = db()->prepare("
UPDATE feature_updates
SET is_highlighted = :is_highlighted
WHERE id = :id
LIMIT 1
");
$stmt->execute([
'is_highlighted' => $isHighlighted ? 1 : 0,
'id' => $featureId,
]);
}
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf_token();
$action = (string)($_POST['action'] ?? '');
if ($action === 'create_feature_update' || $action === 'update_feature_update') {
$featureId = $action === 'update_feature_update'
? (int)($_POST['feature_id'] ?? 0)
: null;
$title = trim((string)($_POST['title'] ?? ''));
$summary = trim((string)($_POST['summary'] ?? ''));
$details = trim((string)($_POST['details'] ?? ''));
$featureType = trim((string)($_POST['feature_type'] ?? 'feature'));
$status = trim((string)($_POST['status'] ?? 'live'));
$releaseDate = trim((string)($_POST['release_date'] ?? ''));
$visibility = trim((string)($_POST['visibility'] ?? 'public'));
$releaseVersion = trim((string)($_POST['release_version'] ?? ''));
$isHighlighted = !empty($_POST['is_highlighted']) ? 1 : 0;
$createdAtInput = trim((string)($_POST['created_at'] ?? ''));
if ($title === '') {
throw new RuntimeException('Title is required.');
}
if ($summary === '') {
throw new RuntimeException('Summary is required.');
}
if (!array_key_exists($featureType, admin_feature_type_options())) {
throw new RuntimeException('Invalid feature type.');
}
if (!array_key_exists($status, admin_feature_status_options())) {
throw new RuntimeException('Invalid status.');
}
if (!array_key_exists($visibility, admin_feature_visibility_options())) {
throw new RuntimeException('Invalid visibility.');
}
$createdAt = $createdAtInput !== ''
? date('Y-m-d H:i:s', strtotime($createdAtInput))
: date('Y-m-d H:i:s');
admin_save_feature_update($featureId, [
'title' => $title,
'summary' => $summary,
'details' => $details !== '' ? $details : null,
'feature_type' => $featureType,
'status' => $status,
'release_date' => $releaseDate !== '' ? $releaseDate : null,
'visibility' => $visibility,
'release_version' => $releaseVersion !== '' ? $releaseVersion : null,
'is_highlighted' => $isHighlighted,
'created_at' => $createdAt,
]);
header('Location: feature_admin.php?msg=' . ($featureId ? 'updated' : 'created'));
exit;
}
if ($action === 'delete_feature_update') {
admin_delete_feature_update((int)($_POST['feature_id'] ?? 0));
header('Location: feature_admin.php?msg=deleted');
exit;
}
if ($action === 'toggle_highlight') {
$featureId = (int)($_POST['feature_id'] ?? 0);
$isHighlighted = (int)($_POST['is_highlighted'] ?? 0) === 1;
admin_toggle_feature_highlight($featureId, !$isHighlighted);
header('Location: feature_admin.php');
exit;
}
}
$statusMessage = (string)($_GET['msg'] ?? '');
if ($statusMessage === 'created') {
$message = 'Feature update added successfully.';
} elseif ($statusMessage === 'updated') {
$message = 'Feature update updated successfully.';
} elseif ($statusMessage === 'deleted') {
$message = 'Feature update deleted successfully.';
}
if ($editingFeatureId > 0) {
$editingFeature = admin_get_feature_update_by_id($editingFeatureId);
if (!$editingFeature) {
$editingFeatureId = 0;
}
}
$featureUpdates = admin_get_feature_updates(100);
} catch (Throwable $e) {
$error = $e->getMessage();
}
require_once __DIR__ . '/includes/header.php';
$createdAtValue = '';
if (!empty($editingFeature['created_at'])) {
$createdAtValue = date('Y-m-d\TH:i', strtotime((string)$editingFeature['created_at']));
}
$releaseDateValue = !empty($editingFeature['release_date'])
? date('Y-m-d', strtotime((string)$editingFeature['release_date']))
: date('Y-m-d');
$featureTypeValue = (string)($editingFeature['feature_type'] ?? 'feature');
$statusValue = (string)($editingFeature['status'] ?? 'live');
$visibilityValue = (string)($editingFeature['visibility'] ?? 'public');
?>
<h1 class="page-title brand-title-font">Feature Updates Admin</h1>
<?php if ($message !== ''): ?>
<div class="msg ok"><?= h($message) ?></div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="msg err"><?= h($error) ?></div>
<?php endif; ?>
<div class="games-layout">
<div class="card">
<h2><?= $editingFeature ? 'Edit Feature Update' : 'Add Feature Update' ?></h2>
<form method="post" action="feature_admin.php<?= $editingFeature ? '?edit=' . (int)$editingFeature['id'] : '' ?>">
<?= csrf_field() ?>
<input type="hidden" name="action" value="<?= $editingFeature ? 'update_feature_update' : 'create_feature_update' ?>">
<?php if ($editingFeature): ?>
<input type="hidden" name="feature_id" value="<?= (int)$editingFeature['id'] ?>">
<?php endif; ?>
<div class="form-field">
<label for="title">Title</label>
<input
type="text"
id="title"
name="title"
required
maxlength="150"
value="<?= h((string)($editingFeature['title'] ?? '')) ?>"
placeholder="Manual Lineup Autosave"
>
</div>
<div class="form-field">
<label for="summary">Summary</label>
<input
type="text"
id="summary"
name="summary"
maxlength="255"
required
value="<?= h((string)($editingFeature['summary'] ?? '')) ?>"
placeholder="Manual lineup edits now autosave while coaches work."
>
</div>
<div class="form-field">
<label for="details">Details</label>
<textarea
id="details"
name="details"
rows="6"
placeholder="Explain what changed, why it matters, and where coaches can use it."
><?= h((string)($editingFeature['details'] ?? '')) ?></textarea>
</div>
<div class="game-meta-grid">
<div class="form-field">
<label for="feature_type">Feature Type</label>
<select id="feature_type" name="feature_type">
<?php foreach (admin_feature_type_options() as $value => $label): ?>
<option value="<?= h($value) ?>" <?= $featureTypeValue === $value ? 'selected' : '' ?>>
<?= h($label) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-field">
<label for="status">Status</label>
<select id="status" name="status">
<?php foreach (admin_feature_status_options() as $value => $label): ?>
<option value="<?= h($value) ?>" <?= $statusValue === $value ? 'selected' : '' ?>>
<?= h($label) ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="game-meta-grid">
<div class="form-field">
<label for="release_date">Release Date</label>
<input
type="date"
id="release_date"
name="release_date"
value="<?= h($releaseDateValue) ?>"
>
</div>
<div class="form-field">
<label for="release_version">Release Version</label>
<input
type="text"
id="release_version"
name="release_version"
maxlength="30"
value="<?= h((string)($editingFeature['release_version'] ?? '')) ?>"
placeholder="Beta 1.6"
>
</div>
</div>
<div class="game-meta-grid">
<div class="form-field">
<label for="visibility">Visibility</label>
<select id="visibility" name="visibility">
<?php foreach (admin_feature_visibility_options() as $value => $label): ?>
<option value="<?= h($value) ?>" <?= $visibilityValue === $value ? 'selected' : '' ?>>
<?= h($label) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="form-field">
<label for="created_at">Admin Created Date</label>
<input
type="datetime-local"
id="created_at"
name="created_at"
value="<?= h($createdAtValue) ?>"
>
</div>
</div>
<div class="form-field checkbox-field" style="margin-top:16px;">
<label class="ios-toggle-wrap" for="is_highlighted">
<span class="ios-toggle-label">Highlight this update</span>
<span class="ios-toggle">
<input
type="checkbox"
id="is_highlighted"
name="is_highlighted"
value="1"
<?= !empty($editingFeature['is_highlighted']) ? 'checked' : '' ?>
>
<span class="ios-toggle-slider"></span>
</span>
</label>
<div class="muted" style="margin-top:6px;">
Highlighted updates can be shown more prominently on What’s New or inside Coach Helper nudges.
</div>
</div>
<div class="actions-row" style="margin-top:20px;">
<button type="submit">
<?= $editingFeature ? 'Update Feature Update' : 'Add Feature Update' ?>
</button>
<?php if ($editingFeature): ?>
<a class="btn btn-secondary" href="feature_admin.php">Cancel Edit</a>
<?php endif; ?>
<a class="btn btn-secondary" href="features.php">View What’s New</a>
</div>
</form>
</div>
<div class="card">
<h2>Recent Feature History</h2>
<?php if (empty($featureUpdates)): ?>
<p class="muted">No feature updates added yet.</p>
<?php else: ?>
<div class="table-wrap desktop-table">
<table>
<thead>
<tr>
<th>Title</th>
<th>Summary</th>
<th>Version</th>
<th>Type</th>
<th>Status</th>
<th>Visibility</th>
<th>Release Date</th>
<th>Highlighted</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($featureUpdates as $feature): ?>
<tr>
<td><?= h((string)$feature['title']) ?></td>
<td><?= h((string)($feature['summary'] ?? '')) ?></td>
<td><?= h((string)($feature['release_version'] ?? '')) ?></td>
<td><?= h(admin_feature_type_options()[(string)($feature['feature_type'] ?? 'feature')] ?? 'Update') ?></td>
<td><?= h(ucwords((string)($feature['status'] ?? ''))) ?></td>
<td><?= h(ucwords((string)($feature['visibility'] ?? 'public'))) ?></td>
<td>
<?= !empty($feature['release_date'])
? h(date('M j, Y', strtotime((string)$feature['release_date'])))
: h(date('M j, Y', strtotime((string)$feature['created_at'])))
?>
</td>
<td>
<form method="post" style="display:inline;">
<?= csrf_field() ?>
<input type="hidden" name="action" value="toggle_highlight">
<input type="hidden" name="feature_id" value="<?= (int)$feature['id'] ?>">
<input type="hidden" name="is_highlighted" value="<?= (int)$feature['is_highlighted'] ?>">
<button type="submit" class="btn btn-secondary" style="padding:4px 8px;">
<?= !empty($feature['is_highlighted']) ? 'Unhighlight' : 'Highlight' ?>
</button>
</form>
</td>
<td>
<div class="inline-actions">
<a class="btn-sm" href="feature_admin.php?edit=<?= (int)$feature['id'] ?>">Edit</a>
<form method="post" style="display:inline;" onsubmit="return confirm('Delete this feature update?');">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete_feature_update">
<input type="hidden" name="feature_id" value="<?= (int)$feature['id'] ?>">
<button type="submit" class="btn-sm btn-danger">Delete</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<div class="mobile-cards">
<?php foreach ($featureUpdates as $feature): ?>
<div class="mobile-card">
<div class="mobile-card-title"><?= h((string)$feature['title']) ?></div>
<div class="mobile-card-row">
<span class="mobile-card-label">Summary</span>
<?= h((string)($feature['summary'] ?? '')) ?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Version</span>
<?= h((string)($feature['release_version'] ?? '')) ?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Type</span>
<?= h(admin_feature_type_options()[(string)($feature['feature_type'] ?? 'feature')] ?? 'Update') ?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Status</span>
<?= h(ucwords((string)($feature['status'] ?? ''))) ?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Visibility</span>
<?= h(ucwords((string)($feature['visibility'] ?? 'public'))) ?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Release</span>
<?= !empty($feature['release_date'])
? h(date('M j, Y', strtotime((string)$feature['release_date'])))
: h(date('M j, Y', strtotime((string)$feature['created_at'])))
?>
</div>
<div class="mobile-card-row">
<span class="mobile-card-label">Highlighted</span>
<?= !empty($feature['is_highlighted']) ? 'Yes' : 'No' ?>
</div>
<div class="stack-actions" style="margin-top:12px;">
<a class="btn" href="feature_admin.php?edit=<?= (int)$feature['id'] ?>">Edit</a>
<form method="post">
<?= csrf_field() ?>
<input type="hidden" name="action" value="toggle_highlight">
<input type="hidden" name="feature_id" value="<?= (int)$feature['id'] ?>">
<input type="hidden" name="is_highlighted" value="<?= (int)$feature['is_highlighted'] ?>">
<button type="submit" class="btn btn-secondary">
<?= !empty($feature['is_highlighted']) ? 'Unhighlight' : 'Highlight' ?>
</button>
</form>
<form method="post" onsubmit="return confirm('Delete this feature update?');">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete_feature_update">
<input type="hidden" name="feature_id" value="<?= (int)$feature['id'] ?>">
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
</div>
<button
type="button"
class="btn btn-secondary"
style="margin-top:16px;"
onclick="localStorage.removeItem('coach_helper_dismissed_feature_id'); location.reload();"
>
Reset Feature Nudge
</button>
<?php require_once __DIR__ . '/includes/footer.php'; ?>