-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategories.php
395 lines (281 loc) · 11.5 KB
/
categories.php
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
<?php
include 'inc/__settings_and_functions.php';
include 'inc/_validation.php';
$html = "<h1>Categories</h1>";
$page_title = "Categories";
if (!isset($_GET['mode']) && ($_SERVER['REQUEST_METHOD'] != 'POST')) {
// ####################################################################################################################################################################################
// ADMIN TABLE
// ####################################################################################################################################################################################
// ##############################################
// count categories
// ##############################################
$sql_pages = "SELECT count(*) total_category_count FROM diary_categories c";
$stmt = $pdo->prepare($sql_pages);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_category_count = number_format($row['total_category_count']);
$snip = NULL;
$sql = "SELECT fld_id
, fld_cat
FROM diary_categories
ORDER BY fld_cat";
$stmt = $pdo->prepare($sql);
$stmt->execute();
// loop through results, building a table row for each category
foreach ($stmt as $row) {
$data = true;
$fld_id = htmlspecialchars($row['fld_id']);
$fld_cat = htmlspecialchars($row['fld_cat']);
$snip .= "<tr><td><a href='categories.php?fld_id=$fld_id&mode=edit'>$fld_cat</a></td>";
// ##############################################
// count child posts start
// ##############################################
$sql_pages = "SELECT count(*) ct
FROM diary_days d
, diary_categories c
WHERE d.fld_cat_id = c.fld_id
AND c.fld_id = :fld_id";
$stmt = $pdo->prepare($sql_pages);
$stmt->bindParam(':fld_id', $fld_id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$ct = number_format(htmlspecialchars($row['ct']));
$snip .= "
<td>$ct</td>
<td><a href='categories.php?fld_id=$fld_id&mode=edit'>Edit</a></td>
";
if ($ct > 0) {
$snip .= "<td><span class='link-secondary'>Cannot delete active category</td>";
} elseif ($total_category_count == 1) {
$snip .= "<td>Cannot delete only existing category</td>";
} else {
$snip .= "<td><a href='categories.php?fld_id=$fld_id&mode=delete'>Delete</a></td>";
}
if ($ct > 0) {
$snip .= "
<td>
<form action='search.php' method='post'>
<input type='hidden' name='method' value='search'>
<input type='hidden' name='cat' value='$fld_id'>
<button type='submit' class='btn btn-primary btn-sm'><i class='fa-solid fa-magnifying-glass'></i> Find diary entries</button>
</form>
</td>
";
}
$snip .= "</tr>";
}
// if category data found, built HTML for table to display categories
if (isset($data)) {
$html .= "
<nav aria-label='breadcrumb'>
<ol class='breadcrumb'>
<li class='breadcrumb-item'><a href='categories.php'>Categories Home</a></li>
</ol>
</nav>
<div class='table-responsive'>
<table class='table table-light table-striped table-hover table-bordered'>
<tr>
<thead>
<th>Category</th>
<th>Entry Count</th>
<th>Edit</th>
<th>Delete</th>
<th>Lookup</th>
</thead>
</tr>
$snip
</table>
</div>
";
} else {
$html .= "<p>No Categories found. <a href='categories.php'>Return to Categories home</a>.</p>";
}
$html .= "<a href='categories.php?mode=new' class='btn btn-success'><i class='fa fa-plus'></i> New Category</a>";
}
// ####################################################################################################################################################################################
// QUERYSTRING
// ####################################################################################################################################################################################
if (isset($_GET['mode'])) {
// if mode is populated in querystring
$mode = $_GET['mode'];
// if in edit mode, search for category to edit
if ($mode == 'edit') {
$sql = "SELECT c.fld_id
, c.fld_cat
FROM diary_categories c
WHERE c.fld_id = :cat_id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cat_id', $_GET['fld_id']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$row) {
$html .= "<p>No category found to edit. <a href='categories.php'>Return to Categories home</a>.</p>";
} else {
$cat_title = htmlspecialchars($row['fld_cat']);
$cat_id = htmlspecialchars($row['fld_id']);
$page_title = "Edit: $cat_title / Categories";
$html = "
<h1>Edit Category</h1>
<nav aria-label='breadcrumb'>
<ol class='breadcrumb'>
<li class='breadcrumb-item'><a href='categories.php'>Categories Home</a></li>
<li class='breadcrumb-item active' aria-current='page'>Edit: <span class='text-primary-emphasis'>$cat_title</span></li>
</ol>
</nav>
<form method='post' class='alert alert-secondary' action='categories.php'>
<input type='hidden' name='mode' value='editprocess'>
<input type='hidden' name='cat_id' value='$cat_id'>
<div class='mb-3'>
<label for='cat_title'>Title</label>
<input type='text' class='form-control' id='cat_title' name='cat_title' value='$cat_title' placeholder='✏️ Category' required>
</div>
<div class='mb-3'>
<button type='submit' class='btn btn-success'><i class='fa-solid fa-floppy-disk'></i> Save Changes</button>
<a href='categories.php' class='btn btn-primary'><i class='fa fa-times'></i> Cancel</a>
</div>
</form>
";
}
} // end edit mode
// new record mode
if ($mode == 'new') {
// HTML for new category form
$html = "
<h1>New Category</h1>
<nav aria-label='breadcrumb'>
<ol class='breadcrumb'>
<li class='breadcrumb-item'><a href='categories.php'>Categories Home</a></li>
<li class='breadcrumb-item active' aria-current='page'>New</li>
</ol>
</nav>
<form method='post' class='alert alert-secondary' action='categories.php'>
<input type='hidden' name='mode' value='newprocess'>
<div class='mb-3'>
<label for='cat_title'>Title</label>
<input type='text' class='form-control' id='cat_title' name='cat_title' placeholder='✏️ Category' required>
</div>
<div class='mb-3'>
<button type='submit' class='btn btn-success'><i class='fa-solid fa-check'></i> Create Category</button> <a href='categories.php' class='btn btn-primary'><i class='fa fa-times'></i> Cancel</a>
</div>
</form>
";
$page_title = "New / Categories";
} // end new mode
// delete mode
if ($mode == 'delete') {
$fld_id = $_GET['fld_id'];
if (!isset($fld_id)) {
header('Location:categories.php#delete-id-not-set');
exit;
}
// ##############################################
// get count of categories from database
// ##############################################
$sql_pages = "SELECT count(*) total_category_count
FROM diary_categories c";
$stmt = $pdo->prepare($sql_pages);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$total_category_count = number_format(htmlspecialchars($row['total_category_count']));
// Get Category Details ##################################################################
$sql = "SELECT c.fld_id
, c.fld_cat
FROM diary_categories c
WHERE c.fld_id = :cat_id";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cat_id', $_GET['fld_id']);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if(!$row) {
$html .= "<p>No record found to delete. <a href='categories.php'>Return to Categories home</a>.</p>";
} else {
$cat_title = htmlspecialchars($row['fld_cat']);
$cat_id = htmlspecialchars($row['fld_id']);
// List Posts ##################################################################################
$sql_pages = "SELECT count(*) ct
FROM diary_days d
, diary_categories c
WHERE d.fld_cat_id = c.fld_id
AND c.fld_id = :cat_id";
$stmt = $pdo->prepare($sql_pages);
$stmt->bindParam(':cat_id', $cat_id);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$ct = htmlspecialchars($row['ct']);
if ($ct > 0) {
$html .= "<p>Cannot delete category as it contains <code>$ct</code> records. <a href='categories.php'>Return to Categories home</a>.</p>";
} elseif ($total_category_count == 1) {
$html .= "<p>Cannot delete only existing category. <a href='categories.php'>Return to Categories home</a>.</p>";
} else {
$sql = "DELETE FROM diary_categories WHERE fld_id = :fld_id LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':fld_id', $_GET['fld_id']);
$stmt->execute();
header("Location:categories.php#deleted");
}
}
} // end delete mode
} else {
$page_title = "New / Categories";
}
// ####################################################################################################################################################################################
// PROCESS FORM DATA
// ####################################################################################################################################################################################
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$mode = $_POST['mode'];
// process edit mode
if ($mode == 'editprocess') {
$cat_id = $_POST['cat_id'];
/* 404 for no results */
if (!isset($cat_id)) {
$html = "<p>No category found. <a href='categories.php'>Return to Categories home</a>.</p>";
} else {
$cat_title = $_POST['cat_title'];
// if category not provided in form alert user
if(!$cat_title){
$html = "<p>Category value not provided. <a href='categories.php'>Return to Categories home</a>.</p>";
} else { // otherwise update category in database
$sql = "UPDATE diary_categories SET fld_cat = :cat_title
, fld_update_date = now()
WHERE fld_id = :cat_id
LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cat_title', $cat_title);
$stmt->bindParam(':cat_id', $cat_id);
$stmt->execute();
header("Location:categories.php#editprocess");
} // end checking to see if category provided
} // end if for no record exists in database
} // end edit mode
// new record mode
if ($mode == 'newprocess') {
$cat_title = $_POST['cat_title'];
// if category not provided in form alert user
if(!$cat_title){
$html = "<p>Category value not provided. <a href='categories.php'>Return to Categories home</a>.</p>";
} else { // otherwise insert record into database
$sql = "INSERT INTO diary_categories (fld_cat
, fld_creation_date)
VALUES (:cat_title
, now())";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':cat_title', $cat_title);
$stmt->execute();
header("Location:categories.php#newprocess");
} // end checking to see if category provided
} // end new record mode
}
include 'inc/_inc1.php';
?>
<title><?php echo htmlspecialchars($page_title) ?></title>
<?php include 'inc/_inc2.php';?>
<?php include 'inc/_inc_nav.php';?>
<div class="<?php echo htmlspecialchars($container); ?>">
<?php
echo $html;
?>
</div>
<?php include 'inc/_inc3.php';?>
</body>
</html>