-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy-categories.php
More file actions
307 lines (261 loc) · 12 KB
/
my-categories.php
File metadata and controls
307 lines (261 loc) · 12 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
<?php
declare(strict_types=1);
/**
* Stránka „Moje kategorie“ – správa vlastních štítků (přidání, úprava, smazání) + stránkování a hledání.
*/
$pageTitle = 'Beans – Moje kategorie';
$pageId = 'my-categories';
$pageHeading = 'Moje kategorie';
$pageIcon = 'icon-coffee.svg';
$showBackButton = true;
$backFallback = '/~krausiv4/beans/index';
require __DIR__ . '/php/bootstrap.php';
auth_require_login();
require_once __DIR__ . '/php/repos/categories_repo.php';
$errors = [];
$q = trim((string)($_GET['q'] ?? ''));
$page = max(1, (int)($_GET['page'] ?? 1));
$pageSize = 12;
$u = auth_user();
$userId = (int)($u['UserID'] ?? $u['id'] ?? 0);
if ($userId <= 0) {
auth_logout();
header('Location: /~krausiv4/beans/auth-login');
exit;
}
/**
* Sestaví URL pro stránku „Moje kategorie“ se zachováním podporovaných query parametrů (q, page).
*
* @param array<string, mixed> $overrides Parametry, které se mají přepsat (např. ['page' => 1]).
* @return string URL včetně query stringu.
*/
function categories_url(array $overrides = []): string
{
$base = '/~krausiv4/beans/my-categories';
// whitelist jen to, co tahle stránka reálně používá
$params = [
'q' => (string)($_GET['q'] ?? ''),
'page' => (string)($_GET['page'] ?? ''),
];
$params = array_merge($params, $overrides);
foreach ($params as $k => $v) {
if ($v === '' || $v === null) unset($params[$k]);
}
return $base . (count($params) ? ('?' . http_build_query($params, '', '&', PHP_QUERY_RFC3986)) : '');
}
/**
* Vrátí krátkou českou větu pro počet štítků (skloňování).
*
* @param int $n Počet štítků.
* @return string Text pro UI.
*/
function cz_tag_count_text(int $n): string
{
if ($n === 0) return 'Nemáte zatím žádné štítky.';
if ($n === 1) return 'Máte 1 štítek.';
if ($n >= 2 && $n <= 4) return "Máte {$n} štítky.";
return "Máte {$n} štítků.";
}
// pro re-render add inputu při chybě
$oldAddName = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
csrf_verify_post();
$action = (string)($_POST['action'] ?? '');
if ($action === 'add') {
$name = trim((string)($_POST['name'] ?? ''));
$oldAddName = $name;
if ($name === '' || mb_strlen($name) > 255) {
$errors['add'] = 'Zadej název štítku (1–255 znaků).';
} elseif (category_find_id_by_name_for_user($userId, $name) !== null) {
$errors['add'] = 'Takový štítek už existuje.';
}
if (!$errors) {
category_create_for_user($userId, $name);
header('Location: ' . categories_url(['page' => 1]));
exit;
}
} elseif ($action === 'edit') {
$id = (int)($_POST['categoryId'] ?? 0);
$name = trim((string)($_POST['name'] ?? ''));
if ($id <= 0 || !category_exists_for_user($userId, $id)) {
$errors['edit'] = 'Neplatný štítek.';
} elseif ($name === '' || mb_strlen($name) > 255) {
$errors['edit'] = 'Zadej název štítku (1–255 znaků).';
} elseif (category_find_id_by_name_for_user($userId, $name, $id) !== null) {
$errors['edit'] = 'Takový štítek už existuje.';
}
if (!$errors) {
category_update_for_user($userId, $id, $name);
header('Location: ' . categories_url());
exit;
}
} elseif ($action === 'delete') {
$id = (int)($_POST['categoryId'] ?? 0);
if ($id <= 0 || !category_exists_for_user($userId, $id)) {
$errors['delete'] = 'Neplatný štítek.';
} else {
$ok = category_delete_for_user($userId, $id);
if (!$ok) {
$used = category_usage_count_for_user($userId, $id);
$errors['delete'] = "Štítek nelze odstranit – používá se u {$used} káv.";
}
}
if (!$errors) {
header('Location: ' . categories_url());
exit;
}
}
}
$list = category_list_paged_for_user($userId, $q, $page, $pageSize);
$items = (array)($list['items'] ?? []);
$total = (int)($list['total'] ?? 0);
$page = (int)($list['page'] ?? $page);
$pages = (int)($list['pages'] ?? 1);
require __DIR__ . '/partials/header.php';
?>
<section class="page__section">
<div class="container">
<section class="my-categories">
<header class="my-categories__header">
<div class="my-categories__title">
<h1 class="heading heading--h2">Moje kategorie</h1>
<p class="my-categories__meta text-muted" data-categories-count>
<?= e(cz_tag_count_text($total)) ?>
</p>
</div>
</header>
<?php if (!empty($errors['delete'])): ?>
<div class="alert alert--error" role="alert"><?= e($errors['delete']) ?></div>
<?php endif; ?>
<?php if (!empty($errors['edit'])): ?>
<div class="alert alert--error" role="alert"><?= e($errors['edit']) ?></div>
<?php endif; ?>
<!-- Shared delete form (bez inline onclick) -->
<form method="post" action="<?= e(categories_url()) ?>" data-category-delete-form hidden>
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete">
<input type="hidden" name="categoryId" value="">
</form>
<!-- Přidání nové kategorie -->
<form class="my-categories__add" method="post" action="<?= e(categories_url(['page' => 1])) ?>"
data-category-form>
<?= csrf_field() ?>
<input type="hidden" name="action" value="add">
<div class="field field--full <?= !empty($errors['add']) ? 'field--invalid' : '' ?>">
<label class="my-categories__add-label field__label" for="category-name">
Přidat nový štítek
</label>
<div class="my-categories__add-row">
<input
id="category-name"
name="name"
type="text"
class="field__input"
placeholder="Např. filtr, espresso, decaf…"
value="<?= e($oldAddName) ?>"
required
maxlength="255"
aria-invalid="<?= !empty($errors['add']) ? 'true' : 'false' ?>"
<?= !empty($errors['add']) ? 'aria-describedby="category-add-error"' : '' ?>
data-category-input
>
<button type="submit" class="button button--accent-green">
Přidat
</button>
</div>
<?php if (!empty($errors['add'])): ?>
<div class="field__alert field__alert--error" id="category-add-error" role="alert">
<?= e($errors['add']) ?>
</div>
<?php endif; ?>
</div>
<p class="my-categories__hint text-muted">
Štítky můžeš pak používat u jednotlivých káv (např. způsob přípravy, zpracování, speciální edice…).
</p>
</form>
<ul class="my-categories__list" data-categories-list>
<?php foreach ($items as $cat): ?>
<?php
$id = (int)($cat['CategoryID'] ?? 0);
$name = (string)($cat['Name'] ?? '');
if ($id <= 0 || $name === '') continue;
?>
<li class="category-item" data-category-id="<?= e((string)$id) ?>"
data-category-name="<?= e($name) ?>">
<div class="category-item__view">
<span class="category-item__pill"><?= e($name) ?></span>
<div class="category-item__actions">
<button type="button" class="button button--secondary" data-category-edit>
Upravit
</button>
<button
type="button"
class="button button--error"
data-category-delete
data-category-id="<?= e((string)$id) ?>"
data-category-name="<?= e($name) ?>"
>
Odstranit
</button>
</div>
</div>
<form class="category-item__edit" method="post" action="<?= e(categories_url()) ?>"
data-category-edit-form hidden>
<?= csrf_field() ?>
<input type="hidden" name="action" value="edit">
<input type="hidden" name="categoryId" value="<?= e((string)$id) ?>">
<div class="category-item__edit-row">
<input
type="text"
name="name"
class="field__input category-item__input"
value="<?= e($name) ?>"
maxlength="255"
required
>
<div class="category-item__actions">
<button type="submit" class="button button--accent-green">Uložit</button>
<button type="button" class="button button--secondary" data-category-cancel>Zrušit
</button>
</div>
</div>
</form>
</li>
<?php endforeach; ?>
</ul>
<?php if ($pages > 0): ?>
<nav class="pagination" aria-label="Stránkování">
<?php $prev = max(1, $page - 1); ?>
<?php $next = min($pages, $page + 1); ?>
<a
class="pagination__button <?= $page <= 1 ? 'is-disabled' : '' ?>"
href="<?= e($page <= 1 ? '#' : categories_url(['page' => (string)$prev])) ?>"
aria-disabled="<?= $page <= 1 ? 'true' : 'false' ?>"
>
‹
</a>
<ol class="pagination__list">
<?php for ($p = 1; $p <= $pages; $p++): ?>
<li class="pagination__item">
<a
class="pagination__page <?= $p === $page ? 'pagination__page--current' : '' ?>"
href="<?= e(categories_url(['page' => (string)$p])) ?>"
>
<?= e((string)$p) ?>
</a>
</li>
<?php endfor; ?>
</ol>
<a
class="pagination__button <?= $page >= $pages ? 'is-disabled' : '' ?>"
href="<?= e($page >= $pages ? '#' : categories_url(['page' => (string)$next])) ?>"
aria-disabled="<?= $page >= $pages ? 'true' : 'false' ?>"
>
›
</a>
</nav>
<?php endif; ?>
</section>
</div>
</section>
<?php require __DIR__ . '/partials/footer.php'; ?>