-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategories.php
More file actions
141 lines (113 loc) · 3.9 KB
/
categories.php
File metadata and controls
141 lines (113 loc) · 3.9 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
<?php
require_once 'config.php';
// Check if user is logged in
if (!isLoggedIn()) {
sendResponse(false, 'Authentication required');
}
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
getAllCategories();
break;
case 'POST':
createCategory();
break;
case 'DELETE':
parse_str(file_get_contents("php://input"), $_DELETE);
deleteCategory($_DELETE['id'] ?? null);
break;
default:
sendResponse(false, 'Method not allowed');
}
function getAllCategories() {
global $pdo;
try {
// Get categories with image count
$stmt = $pdo->query("
SELECT c.id, c.name, c.created_at,
COUNT(g.id) as count
FROM categories c
LEFT JOIN gallery g ON c.id = g.category_id
GROUP BY c.id, c.name, c.created_at
ORDER BY c.name ASC
");
$categories = $stmt->fetchAll();
sendResponse(true, 'Categories retrieved successfully', $categories);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function createCategory() {
global $pdo;
$name = trim($_POST['name'] ?? '');
if (empty($name)) {
sendResponse(false, 'Category name is required');
}
// Validate name length
if (strlen($name) > 100) {
sendResponse(false, 'Category name is too long (max 100 characters)');
}
try {
// Check if category already exists
$stmt = $pdo->prepare("SELECT id FROM categories WHERE LOWER(name) = LOWER(?)");
$stmt->execute([$name]);
if ($stmt->fetch()) {
sendResponse(false, 'Category already exists');
}
// Create new category
$stmt = $pdo->prepare("INSERT INTO categories (name, created_at) VALUES (?, NOW())");
$stmt->execute([$name]);
$categoryId = $pdo->lastInsertId();
sendResponse(true, 'Category created successfully', [
'id' => $categoryId,
'name' => $name,
'created_at' => date('Y-m-d H:i:s'),
'count' => 0
]);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function deleteCategory($id) {
global $pdo;
if (!$id) {
sendResponse(false, 'Category ID is required');
}
try {
// Check if category exists
$stmt = $pdo->prepare("SELECT name FROM categories WHERE id = ?");
$stmt->execute([$id]);
$category = $stmt->fetch();
if (!$category) {
sendResponse(false, 'Category not found');
}
// Check if category has images
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM gallery WHERE category_id = ?");
$stmt->execute([$id]);
$imageCount = $stmt->fetch()['count'];
// Start transaction
$pdo->beginTransaction();
// If category has images, set their category_id to NULL
if ($imageCount > 0) {
$stmt = $pdo->prepare("UPDATE gallery SET category_id = NULL WHERE category_id = ?");
$stmt->execute([$id]);
}
// Delete the category
$stmt = $pdo->prepare("DELETE FROM categories WHERE id = ?");
$stmt->execute([$id]);
// Commit transaction
$pdo->commit();
$message = "Category '{$category['name']}' deleted successfully";
if ($imageCount > 0) {
$message .= ". {$imageCount} image(s) moved to uncategorized";
}
sendResponse(true, $message);
} catch(PDOException $e) {
// Rollback transaction on error
if ($pdo->inTransaction()) {
$pdo->rollback();
}
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
?>