forked from anpel/restaurant-menu-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
288 lines (249 loc) · 8.66 KB
/
index.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
<?php
include 'includes/functions.php';
include 'includes/database.php'; // sets $dbh
// handle POST actions
if(!empty($_POST))
{
if($_POST['action'] == 'category_create')
{
if( trim($_POST['name_en']) == "" )
{
exit("Category name cannot be empty.");
}
$sql = "INSERT INTO categories(name_en, name_gr, name_it) "
. "VALUES(:name_en, :name_gr, :name_it);";
$stmt = $dbh->prepare($sql);
$stmt->execute([
":name_en" => $_POST['name_en'],
":name_gr" => $_POST['name_gr'],
":name_it" => $_POST['name_it']
]);
header("Location: /index.php?action=categories");
exit();
}
if($_POST['action'] == 'plate_create')
{
if( trim($_POST['name_en']) == "" || trim($_POST['price']) == "")
{
exit("Name and price are necessary to create a new plate");
}
$sql = "INSERT INTO plates "
. "(category_id, name_en, name_gr, name_it, price, available) "
. "VALUES(:category_id, :name_en, :name_gr, :name_it, :price, 1);";
$stmt = $dbh->prepare($sql);
$stmt->execute(array(
":category_id" => $_POST['category_id'],
":name_en" => $_POST['name_en'],
":name_gr" => $_POST['name_gr'],
":name_it" => $_POST['name_it'],
":price" => string2price($_POST['price'])
));
header("Location: /index.php");
exit();
}
if($_POST['action'] == 'plate_delete')
{
$sql = "DELETE FROM plates WHERE id = :id;";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$stmt->execute();
$count = $stmt->rowCount;
if($count > 0)
{
echo 'Plate '. $_POST['id']. ' deleted.';
} else {
echo 'Could not delete plate.';
}
}
if($_POST['action'] == 'category_update')
{
$sql = "UPDATE categories SET position = :position, name_en = :name_en,"
. " name_gr = :name_gr, name_it = :name_it WHERE id = :id;";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':position', $_POST['position'] , PDO::PARAM_INT);
$stmt->bindParam(':name_en', $_POST['name_en'] , PDO::PARAM_STR);
$stmt->bindParam(':name_gr', $_POST['name_gr'] , PDO::PARAM_STR);
$stmt->bindParam(':name_it', $_POST['name_it'] , PDO::PARAM_STR);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$rows = $stmt->execute();
echo 'Category '. $_POST['id']. ' updated.';
}
if($_POST['action'] == 'plate_update')
{
$sql = "UPDATE plates SET price = :price, name_en = :name_en, "
. "name_gr = :name_gr, name_it = :name_it WHERE id = :id;";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(
':price',
string2price($_POST['price']),
PDO::PARAM_BOOL
);
$stmt->bindParam(':name_en', $_POST['name_en'], PDO::PARAM_STR);
$stmt->bindParam(':name_gr', $_POST['name_gr'], PDO::PARAM_STR);
$stmt->bindParam(':name_it', $_POST['name_it'], PDO::PARAM_STR);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$rows = $stmt->execute();
echo 'Plate '. $_POST['id']. ' updated.';
}
if($_POST['action'] == 'plate_availability_update')
{
$sql = "UPDATE plates SET available = :available WHERE id = :id;";
$stmt = $dbh->prepare($sql);
$available = $_POST['available'] === 'true' ? 1 : 0;
$stmt->bindParam(':available', $available , PDO::PARAM_INT);
$stmt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$rows = $stmt->execute();
echo 'Plate '. $_POST['id']. ' updated.';
}
exit();
}
// Check GET variables
$language = 'en'; // default menu language
$allowed_languages = ['en', 'gr', 'it'];
if(isset($_GET['language']) && in_array($_GET['language'], $allowed_languages))
{
$language = $_GET['language'];
}
$action = 'plates'; // default action
$allowed_actions = [
'plates',
'ajax_plates_all',
'categories',
'ajax_categories_all',
'plate_create',
'category_create',
'get_print',
'menu'
];
if(isset($_GET['action']) && in_array($_GET['action'], $allowed_actions))
{
$action = $_GET['action'];
}
// Perform requested actions
if($action == 'plates') // loads empty page, actual data loaded later using AJAX
{
include 'templates/header.php';
include 'includes/table_setup_plates.php';
$table_id = 'table';
include 'templates/table.php';
include 'templates/footer.php';
}
if($action == 'ajax_plates_all')
{
try{
$avaialable_plates = $dbh->query("
SELECT plates.id, plates.name_en, plates.name_gr, plates.name_it,
plates.price, plates.available, categories.name_en AS category_name
FROM plates JOIN categories
ON plates.category_id = categories.id
ORDER BY categories.id;
")
->fetchAll(PDO::FETCH_ASSOC);
$previous_category = '';
foreach ($avaialable_plates as $key => $plate)
{
$avaialable_plates[$key]['price'] =
str_replace('.', ',', $avaialable_plates[$key]['price']);
$avaialable_plates[$key]['removalUrl'] = '<span class="glyphicon '
. 'glyphicon-remove" aria-hidden="true"></span><span '
. 'class="sr-only">Delete</span>';
$avaialable_plates[$key]['available'] =
(bool)$avaialable_plates[$key]['available'];
if($plate['category_name'] == $previous_category)
{
$avaialable_plates[$key]['category_name'] = '';
} else {
$previous_category = $plate['category_name'];
}
}
echo json_encode($avaialable_plates);
} catch (Exception $e) {
echo $e->getMessage();
}
}
if($action == 'categories') //loads empty page, actual data loaded later w/ AJAX
{
include 'templates/header.php';
include 'includes/table_setup_categories.php';
$table_id = 'cat_table';
include 'templates/table.php';
include 'templates/footer.php';
}
if($action == 'ajax_categories_all')
{
try{
$categories = $dbh->query("
SELECT *
FROM categories
ORDER BY position;
")
->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($categories);
} catch (Exception $e) {
echo $e->getMessage();
}
}
if($action == 'plate_create')
{
include 'templates/header.php';
try{
$categories = $dbh->query("
SELECT *
FROM categories
ORDER BY position;
")
->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
echo $e->getMessage();
}
include 'templates/add_plate.php';
include 'templates/footer.php';
}
if($action == 'category_create')
{
include 'templates/header.php';
include 'templates/add_category.php';
include 'templates/footer.php';
}
if($action == 'menu') // wireframe, data loaded later using AJAX
{
include 'templates/header.php';
include 'includes/table_setup_menu.php';
$table_id = 'menu';
include 'templates/table.php';
include 'templates/footer.php';
}
if($action == 'get_print')
{
$name_field = 'name_'.$language;
try{
$avaialable_plates = $dbh->query("
SELECT plates.id, plates.".$name_field." AS name, plates.price,
plates.available, categories.".$name_field." AS category_name
FROM plates JOIN categories
ON plates.category_id = categories.id
WHERE plates.available = 1
ORDER BY categories.position;
")
->fetchAll(PDO::FETCH_ASSOC);
$previous_category = '';
foreach ($avaialable_plates as $key => $plate)
{
$avaialable_plates[$key]['price'] = str_replace(
'.',
',',
$avaialable_plates[$key]['price']
);
$avaialable_plates[$key]['price'] .= ' €';
if($plate['category_name'] == $previous_category)
{
$avaialable_plates[$key]['category_name'] = '';
} else {
$previous_category = $plate['category_name'];
}
}
echo json_encode($avaialable_plates);
} catch (Exception $e) {
echo $e->getMessage();
}
}