-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic-gallery.php
More file actions
165 lines (139 loc) · 5.11 KB
/
public-gallery.php
File metadata and controls
165 lines (139 loc) · 5.11 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
<?php
require_once 'config.php';
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
$method = $_SERVER['REQUEST_METHOD'];
// Only allow GET requests for public access
if ($method !== 'GET') {
sendResponse(false, 'Only GET requests are allowed for public gallery access');
exit;
}
// Debug: Check if PDO connection exists
if (!isset($pdo)) {
sendResponse(false, 'Database connection not available');
exit;
}
// Handle requests
if (isset($_GET['category_id'])) {
getImagesByCategory($_GET['category_id']);
} elseif (isset($_GET['categories'])) {
getAllCategories();
} else {
getAllPublicImages();
}
function getAllPublicImages() {
global $pdo;
try {
// Debug: Test basic connection first
$testQuery = $pdo->query("SELECT 1");
if (!$testQuery) {
sendResponse(false, 'Database connection test failed');
return;
}
// Debug: Check if tables exist
$galleryTableCheck = $pdo->query("SHOW TABLES LIKE 'gallery'");
$categoriesTableCheck = $pdo->query("SHOW TABLES LIKE 'categories'");
if ($galleryTableCheck->rowCount() === 0) {
sendResponse(false, 'Gallery table does not exist');
return;
}
// Get all images with category information
$stmt = $pdo->query("
SELECT g.id, g.name, g.url, g.uploaded_at, g.category_id,
c.name as category_name
FROM gallery g
LEFT JOIN categories c ON g.category_id = c.id
ORDER BY g.category_id, g.uploaded_at DESC
");
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get all categories with image counts
$categoriesStmt = $pdo->query("
SELECT c.id, c.name, COUNT(g.id) as image_count
FROM categories c
LEFT JOIN gallery g ON c.id = g.category_id
GROUP BY c.id, c.name
ORDER BY c.name
");
$categories = $categoriesStmt->fetchAll(PDO::FETCH_ASSOC);
// Group images by category
$imagesByCategory = [];
foreach ($images as $image) {
$categoryId = $image['category_id'] ?? 'uncategorized';
$categoryName = $image['category_name'] ?? 'Uncategorized';
if (!isset($imagesByCategory[$categoryId])) {
$imagesByCategory[$categoryId] = [
'category_id' => $categoryId,
'category_name' => $categoryName,
'images' => []
];
}
$imagesByCategory[$categoryId]['images'][] = $image;
}
// Send response
echo json_encode([
'success' => true,
'message' => 'Gallery data retrieved successfully',
'data' => [
'images_by_category' => array_values($imagesByCategory),
'categories' => $categories,
'total_images' => count($images)
],
'debug' => [
'images_count' => count($images),
'categories_count' => count($categories)
]
]);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ')');
} catch(Exception $e) {
sendResponse(false, 'General error: ' . $e->getMessage());
}
}
function getImagesByCategory($categoryId) {
global $pdo;
try {
if ($categoryId === 'all') {
getAllPublicImages();
return;
}
$stmt = $pdo->prepare("
SELECT g.id, g.name, g.url, g.uploaded_at, g.category_id,
c.name as category_name
FROM gallery g
LEFT JOIN categories c ON g.category_id = c.id
WHERE g.category_id = ?
ORDER BY g.uploaded_at DESC
");
$stmt->execute([$categoryId]);
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get category info
$categoryStmt = $pdo->prepare("SELECT id, name FROM categories WHERE id = ?");
$categoryStmt->execute([$categoryId]);
$category = $categoryStmt->fetch(PDO::FETCH_ASSOC);
sendResponse(true, 'Images retrieved successfully', [
'category' => $category,
'images' => $images,
'total_images' => count($images)
]);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
function getAllCategories() {
global $pdo;
try {
$stmt = $pdo->query("
SELECT c.id, c.name, COUNT(g.id) as image_count
FROM categories c
LEFT JOIN gallery g ON c.id = g.category_id
GROUP BY c.id, c.name
ORDER BY c.name
");
$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
sendResponse(true, 'Categories retrieved successfully', $categories);
} catch(PDOException $e) {
sendResponse(false, 'Database error: ' . $e->getMessage());
}
}
?>