forked from jtpedersen/poodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
316 lines (252 loc) · 8.68 KB
/
functions.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
<?php
setlocale(LC_TIME, "da_DK.utf8");
function get_poodle_uuid() {
$salt = "4d9de2624eb26";
$raw_id = $salt . uniqid();
return sha1($raw_id);
}
function cell($s) {
echo "<td>" . $s . "</td>\n";
}
function cell_h($s) {
echo "<th>" . $s . "</th>\n";
}
function template_header($title, $pid) {
return "
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Poodle</title>
<link rel='stylesheet' href='poodle.css'/>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link href='fonts/bender-black.css' rel='stylesheet' type='text/css'>
<link rel='shortcut icon' type='image/x-icon' href='favicon.ico'>
<script type='text/javascript'> var pid='$pid';</script>
<script type='text/javascript' src='./js/jq.js' ></script>
<style type='text/css'>import 'js/jquery.countdown.css';</style>
<script type='text/javascript' src='js/jquery.countdown.js'></script>
<script type='text/javascript' src='./js/hello.js' ></script>
</head>
<body>
<canvas id='canvas' > <h1>no canvas for you</h1> </canvas>
<div id='wrapper'>
<div id='header'>
<div id='top'>
<div id='msg'> </div>
<div id='countdown'> </div>
</div>
<img src='images/poodle-logo.png'><h1>Poodle</h1>
</div>
<div id='content'>
";
}
function template_footer() {
return "
</div>
</div>
</body>
</html>
";
}
function create_poodle() {
return '<img src="./images/poodle.png" alt="no such poodle" />';
}
function create_unicornpoodle() {
return '<img src="./images/unicorn.jpg" alt="no such poodle" />';
}
function get_dbconnection() {
$tmp = pg_connect("dbname=poodle user=poodle") or die("Could not connect");
return $tmp;
}
function pizza_adder($orderid) {
$str=<<<EOT
<form name="input" action="index.php?id=$orderid" method="post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="username"/></td><td> </td>
</tr>
<tr>
<td>Pizza:</td>
<td><input type="text" name="pizza_id"/></td><td>fx 12a</td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" value="60" /> </td><td></td>
</tr>
<tr>
<td>Comment:</td>
<td><input type="text" name="comment"/></td><td>fx "please draw a unicorn on the box"</td>
</tr>
<tr><td></td><td class='submit'><input type="submit" name="ADD" value="Go Fetch!"/></td><td></td></tr>
</table>
</form>
EOT;
return $str;
}
function clean_str($s) {
return htmlspecialchars($s);
}
function edit_pizza($row, $poodle_id, $i) {
$checked = $row['paid']=='t' ? "checked" : "";
$str=<<<EOT
<tr>
<form name="input" action="index.php?id=$poodle_id" method="post">
<input type="hidden" name="pid" value="$row[id]" />
<td>$i</td>
<td><input type="text" name="username" value="$row[username]"/> </td>
<td><input type="text" name="pizza_id" value="$row[pizza_id]"/> </td>
<td><input type="text" name="comment" value="$row[comment]"/> </td>
<td><input type="text" name="price" value="$row[price]"/> </td>
<!--- TODO onclick submit --->
<td><input type="checkbox" name="paid" $checked /> </td>
<td> <input type="submit" name="EDIT" value="edit" /> </td>
<td> <input type="submit" name="DELETE" value="delete" /> </td>
</form>
</tr>
EOT;
return $str;
}
function check_error($res) {
if (!$res) {
echo "errorhandling";
echo create_unicornpoodle();
}
}
function create_form() {
$conn = get_dbconnection();
$res = pg_prepare($conn, "create", "SELECT name, id FROM pizza_place ORDER BY id");
check_error($res);
$res = pg_execute($conn, "create", array());
$str = '<form name="input" action="create.php" method="post">
pizza place: <select name="pizza_place">';
while ($row = pg_fetch_assoc($res)) {
$val = $row['id'];
$name = $row['name'];
$str .= "<option value=\"$val\">$name</option>";
}
$str .= "</select> <br />";
$str .= '
Order title: <input type="text" name="order_title" />
<br />
collector: <input type="text" name="collector" />
<br />
driver: <input type="text" name="driver" />
<br />
<input type="submit" value="Submit" />
</form>';
pg_close($conn);
return $str;
}
function pizza_place($conn, $id) {
$res = pg_prepare($conn, "place", "SELECT * FROM pizza_place WHERE id=$1");
check_error($res);
$res = pg_execute($conn, "place", array($id));
check_error($res);
$row = pg_fetch_assoc($res);
check_error($row);
$str = "<p>We are ordering food from ";
$str .= '<a target="_blank" href="' . $row['url'] . '" >' . $row['name'] . '</a>. ';
$str .= 'See the <a target="_blank" href="' . $row['catalog_url'] . '" >menu</a></p>';
//TODO use a logo image some how
$str .= '<p>Call them at ' . $row['phone_1'];
$phone_2 = $row['phone_2'];
if ($phone_2 != NULL && $phone_2 != 0)
$str .= " or $phone_2";
$str .= "</p>";
return $str;
}
function get_paid($conn, $order_id) {
$res = pg_prepare($conn, "paid", "SELECT SUM(price) FROM pizza WHERE order_id=$1 AND paid");
check_error($res);
$res = pg_execute($conn, "paid", array($order_id));
check_error($res);
$row = pg_fetch_row($res);
check_error($row);
return $row[0];
}
function get_total($conn, $order_id) {
$res = pg_prepare($conn, "total", "SELECT SUM(price) FROM pizza WHERE order_id=$1");
check_error($res);
$res = pg_execute($conn, "total", array($order_id));
check_error($res);
$row = pg_fetch_row($res);
check_error($row);
return $row[0];
}
function get_timestamp($str) {
if( $str == "")
return NULL;
$tmp = date("Y m d ") . $str;
return $tmp;
}
function edit_order($row) {
$poodle_id = $row['admin_uuid'];
$pickup_time = isset($row['pickup_time']) ? date("H:i", strtotime($row['pickup_time'])) : "";
$order_time = isset($row['order_time']) ? date("H:i", strtotime($row['order_time'])) : "";
$str=<<<EOT
<table>
<tr>
<form name="input" action="index.php?id=$poodle_id" method="post">
<td>Collector:<input type="text" name="collector" value="$row[collector]"/> </td>
<td>Driver:<input type="text" name="driver" value="$row[driver]"/> </td>
<td>pickup time<input type="text" name="pickup_time" value="$pickup_time"/> </td>
<td>order time<input type="text" name="order_time" value="$order_time"/> </td>
<td> <input type="submit" name="EDIT_ORDER" value="edit" /> </td>
</form>
</tr>
EOT;
return $str;
}
function show_poodle_list($heading, $res, $conn) {
echo "<h2>$heading</h2>";
echo "<table class='ordertable'>";
echo "<tr>";
cell_h(" ");
cell_h("Driver");
cell_h("Collector");
cell_h("Place");
cell_h("Created");
cell_h("Access");
echo "</tr>";
while ($row = pg_fetch_assoc($res)) {
echo "<tr>";
$order_title = $row['order_title'];
$driver = $row['driver'];
$collector = $row['collector'];
$uuid = $row['user_uuid'];
$name_res = pg_query_params($conn, "SELECT name FROM pizza_place WHERE id=$1", array($row['pizza_place']));
check_error($name_res);
$name_row = pg_fetch_row($name_res);
check_error($name_row);
$pizza_place = $name_row ? $name_row[0] : "unknown pizzapplace";
$created = $row['created'];
cell($order_title);
cell($driver);
cell($collector);
cell($pizza_place);
cell(strftime("%A den %e. %B kl %R", strtotime($created)));
cell("<a href=\"index.php?id=$uuid\" >Show poodle </a>");
echo "</tr>";
}
echo "</table>";
}
function show_poodles() {
$conn = get_dbconnection();
$res = pg_prepare($conn, "live", "SELECT * FROM pizza_order WHERE pickup_time IS NULL
AND date_part('epoch', age(order_time)) < 36*24*60
ORDER BY created ASC");
check_error($res);
$res = pg_execute($conn, "live", array());
show_poodle_list("Active poodles", $res, $conn);
//dead poodles
$res = pg_prepare($conn, "dead", "SELECT * FROM pizza_order
WHERE NOT pickup_time IS NULL
ORDER BY created DESC
LIMIT 5");
check_error($res);
$res = pg_execute($conn, "dead", array());
show_poodle_list("Recently deceased poodles", $res, $conn);
pg_close($conn);
}
?>