Fixed pagination of orders.
[readifood.git] / lib / hub.php
1 <?php
2
3   if (isset($_POST['show_add_hub'])) {
4     set_last_selected("city_id", $_POST['city_id']);
5     $city_id = $_POST['city_id'];
6     show_new_hub_form($city_id);
7   }
8   else if (isset($_POST['add_hub'])) {
9     set_last_selected("area_id", $_POST['area_id']);
10     $id = add_hub($displayname);
11     if ($id !== false) {
12       echo "<p>Added hub.</p>\n";
13       $parameters = array($displayname, $id);
14     }
15   }
16   else if (isset($_POST['update_hub'])) {
17     list($name, $id, $args) = parse_parameters($parameters);
18     $q = new HubQuery;
19     $hub = $q->findOneById($id);
20     if ($hub) {
21       $area = get_hub_area($hub);
22       if ($area) $area_id = $area->getId();
23       if (update_hub($hub, $area_id) !== false) {
24         echo "<p>Updated hub.</p>\n";
25         $parameters = array($hub->getDisplayname(), $hub->getId());
26       }
27     }
28     else {
29       echo "<p>No such hub!</p>\n";
30     }
31   }
32   else if ($_POST['show_in_area']) {
33     set_last_selected("area_id", $_POST['area_id']);
34     $q = new AreaQuery;
35     $area = $q->findOneById($_POST['area_id']);
36     header(sprintf("Location: http%s://%s/%s/in/area/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id']));
37     exit;
38   }
39   else if ($_POST['show_in_city']) {
40     set_last_selected("city_id", $_POST['city_id']);
41     $q = new CityQuery;
42     $city = $q->findOneById($_POST['city_id']);
43     header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id']));
44     exit;
45   }
46
47   function show_hub_summary(&$hub, $editing = false) {
48     if ($editing) echo "<p>Hub: <span class=\"strong\">" . htmlspecialchars($hub->getName()) . "</span>";
49     else echo "<br>\nHub " . $hub->getStrongLink();
50     $role = $hub->getRole();
51     $role_string = get_hub_role_string($hub);
52     if ($role_string) echo " $role_string";
53     $d = urlencode($hub->getName());
54     $i = $hub->getId();
55     if ($role & $GLOBALS['ROLE_COLLECTION']) {
56       printf(" <a class=\"small\" href=\"/donation/to/hub/%s/%d\">Donations</a>", $d, $i);
57       echo " " . get_small_link("Record", "/donation/record/to/hub/%s/%d", $d, $i);
58     }
59     if ($role & $GLOBALS['ROLE_DISTRIBUTION']) printf(" <a class=\"small\" href=\"/order/to/hub/%s/%d\">Orders</a>", $d, $i);
60     if (check_admin(1)) {
61       echo " " . $hub->getDeleteLink();
62     }
63     $area = get_hub_area($hub);
64     if ($area) echo " in " . $area->getLink();
65     $city = get_hub_city($hub);
66     if ($city) echo ", " . $city->getLink(get_city_displayname($city));
67   }
68
69   function show_hubs($offset, $per_page, $address_ids) {
70     $q = new HubQuery;
71     $hubs = $q->filterByAddressId($address_ids)->find();
72     if (count($hubs)) {
73       foreach ($hubs as $hub) show_hub_summary($hub);
74     }
75     else echo " none";
76   }
77
78   function show_city_hubs($offset, $per_page, $city_name, $city_id = null) {
79     if (isset($city_id)) $city = get_city_by_id($city_id);
80     else if ($city_name) $city = get_city_by_name($city_name);
81     if ($city) {
82       $q = new AreaQuery;
83       $areas = $q->filterByCityId($city->getId())->find();
84       $area_ids = array();
85       foreach ($areas as $area) $area_ids[] = $area->getId();
86
87       $q = new AddressQuery;
88       $addresses = $q->filterByAreaId($area_ids)->find();
89       $address_ids = array();
90       foreach ($addresses as $address) $address_ids[] = $address->getId();
91
92       echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
93       return show_hubs($offset, $per_page, $address_ids);
94     }
95     else echo "<p>No such city!</p>\n";
96   }
97
98   function show_area_hubs($offset, $per_page, $area_name, $area_id = null) {
99     if (isset($area_id)) $area = get_area_by_id($area_id);
100     else if ($area_name) $area = get_area_by_name($area_name);
101     if ($area) {
102       $q = new AddressQuery;
103       $addresses = $q->filterByAreaId($area->getId())->find();
104       $address_ids = array();
105       foreach ($addresses as $address) $address_ids[] = $address->getId();
106
107       echo "<p>Hubs in area " . $area->getLink() . ":";
108       return show_hubs($offset, $per_page, $address_ids);
109     }
110     else echo "<p>No such area!</p>\n";
111   }
112
113   function show_hub_areas_form($city_id = null) {
114     $areas = get_city_areas($city_id);
115     if (! count($areas)) {
116       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
117       return;
118     }
119
120     $candidates = array();
121     foreach ($areas as $area) {
122       if (! count(get_area_hubs($area->getId()))) continue;
123       $candidates[] = $area;
124     }
125     if (! count($candidates)) return;
126
127     echo "<p>Show hubs in area\n";
128     echo "<select name=\"area_id\">\n";
129     foreach ($candidates as $area) {
130       option("area_id", $area->getId(), get_area_displayname($area));
131     }
132     echo "</select>\n";
133     submit("show_in_area", "Show");
134   }
135
136   function show_hub_cities_form($city_id = null) {
137     $q = new CityQuery;
138     $cities = $q->orderByName()->find();
139
140     if (! count($cities)) {
141       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
142       return;
143     }
144
145     $candidates = array();
146     foreach ($cities as $city) {
147       if (! count(get_city_hubs($city->getId()))) continue;
148       $candidates[] = $city;
149     }
150     if (! count($candidates)) return;
151
152     echo "<p>Show hubs in city\n";
153     echo "<select name=\"city_id\">\n";
154     foreach ($candidates as $city) {
155       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
156     }
157     echo "</select>\n";
158     submit("show_in_city", "Show");
159   }
160
161   function show_hub_forms($city_id) {
162     form("noprint standout");
163     show_hub_areas_form($city_id);
164     show_hub_cities_form($city_id);
165     end_form();
166   }
167
168   function show_hub_role_form($role) {
169     return show_role_form($role, $GLOBALS['hub_roles']);
170   }
171
172   function show_hub_form($hub = null, $area_id = null) {
173     if (! $hub) $hub = new Hub;
174
175     /* Role. */
176     echo "<tr>\n";
177     echo "  <td>Role</td>\n";
178     echo "  <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
179     echo "</tr>\n";
180
181     /* Display name. */
182     echo "<tr>\n";
183     echo "  <td>Hub name</td>\n";
184     echo "  <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
185     echo "</tr>\n";
186
187     /* Address. */
188     $address = get_hub_address($hub);
189     if (! $address) $address = new Address;
190     echo "<tr>\n";
191     echo "  <td>Address</td>\n";
192     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
193     echo "</tr>\n";
194
195     /* Postcode. */
196     echo "<tr>\n";
197     echo "  <td>Postcode</td>\n";
198     $postcode = $address->getPostcode();
199     if (validate_postcode($postcode)) {
200       echo "  <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
201     }
202     else {
203       echo "  <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
204     }
205     echo "</tr>\n";
206
207     /* Telephone. */
208     echo "<tr>\n";
209     echo "  <td>Telephone</td>\n";
210     echo "  <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
211     echo "</tr>\n";
212     echo "<tr>\n";
213     echo "  <td>Alternative telephone</td>\n";
214     echo "  <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
215     echo "</tr>\n";
216
217     /* Email. */
218     echo "<tr>\n";
219     echo "  <td>Email</td>\n";
220     echo "  <td>"; input("email", $hub->getEmail()); echo "</td>\n";
221     echo "</tr>\n";
222
223     /* Area. */
224     $areas = get_city_areas();
225     if (! isset($area_id)) $area_id = get_hub_area($hub);
226     echo "<tr>\n";
227     echo "  <td>Area</td>\n";
228     echo "  <td><select name=\"area_id\">\n";
229     foreach ($areas as $area) {
230       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
231     }
232     echo "  </select></td>\n";
233     echo "</tr>\n";
234   }
235
236   function show_new_hub_form($city_id = null) {
237     if (! check_admin(1)) return;
238
239     $areas = get_city_areas($city_id);
240     if (! count($areas)) {
241       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
242       return;
243     }
244
245     form("noprint");
246     echo "<p>Add a new hub:</p>\n";
247
248     echo "<table>\n";
249     show_hub_form($hub);
250
251     echo "<tr>\n";
252     echo "  <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
253     echo "</tr>\n";
254     echo "</table>\n";
255     end_form();
256   }
257
258   function show_add_new_hub_form() {
259     if (! check_admin(1)) return;
260
261     $q = new CityQuery;
262     $cities = $q->find();
263     if (! count($cities)) {
264       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
265       return;
266     }
267
268     form("noprint standout");
269     echo "<p>Add a new hub in <select name=\"city_id\">\n";
270     foreach ($cities as $city) {
271       option("city_id", $city->getId(), get_city_displayname($city));
272     }
273     echo "</select>";
274     submit("show_add_hub", "Proceed");
275     echo "</p>\n";
276     end_form();
277   }
278
279   function update_hub(&$hub, $area_id, $new = false) {
280     global $hub_roles;
281
282     $role = 0;
283     for ($i = 0; $i < count($hub_roles); $i++) {
284       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
285     }
286
287     $displayname = $_POST['displayname'];
288
289     if (! $displayname) {
290       echo "<p>Must have a name!</p>\n";
291       return false;
292     }
293
294     /* Get address. */
295     $line = $_POST['address'];
296     $postcode = trim($_POST['postcode']);
297     if ($postcode) {
298       $postcode = format_postcode($_POST['postcode'], true);
299       if (! $postcode) return false;
300     }
301     $q = new AddressQuery;
302     /* XXX: Finding by area properly? */
303     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
304     if ($address->isNew()) {
305       /* Changing address. */
306       //if (! $new)
307       /*
308         XXX: Check for other hubs at the old address.
309         Make this a new address if there are others, but
310         provide a link to update other hubs.
311       */
312       try {
313         $address->save();
314       }
315       catch (Exception $e) {
316         echo "<p>Error adding $line.</p>\n";
317         return false;
318       }
319     }
320
321     $telephone1 = $_POST['telephone1'];
322     $telephone2 = $_POST['telephone2'];
323     $email = $_POST['email'];
324
325     $hub->setRole($role);
326     $hub->setDisplayname($displayname);
327     $hub->setTelephone1($telephone1);
328     $hub->setTelephone2($telephone2);
329     $hub->setEmail($email);
330     $hub->setAddressId($address->getId());
331
332     try {
333       $hub->save();
334     }
335     catch (Exception $e) {
336       if ($new) echo "<p>Error adding $displayname.</p>\n";
337       else echo "<p>Error updating $displayname.</p>\n";
338       return false;
339     }
340
341     return true;
342   }
343
344   function add_hub(&$name) {
345     if (! check_admin(1, "add a hub")) return;
346
347     $area_id = $_POST['area_id'];
348     if (! is_numeric($area_id)) {
349       echo "<p>Invalid area!</p>\n";
350       return false;
351     }
352
353     $area = get_area_by_id($area_id);
354     if (! $area) {
355       echo "<p>No such area!</p>\n";
356       return false;
357     }
358
359     $hub = new Hub;
360     if (! update_hub($hub, $area_id, true)) return false;
361     return $hub->getId();
362   }
363
364   function confirm_delete_hub($name, $id = null) {
365     if (! check_admin(1, "delete a hub")) return;
366
367     if (isset($id)) $hub = get_hub_by_id($id);
368     else $hub = get_hub_by_name($name);
369     if (! $hub) return false;
370
371     echo "<h3>Confirm deletion</h3>\n";
372     echo "<p>You must confirm deletion of hub " . $hub->getDisplayname() . ": " . $hub->getDeleteLink(true) . "</p>\n";
373   }
374
375   function delete_hub($name, $id = null, &$city_id = null) {
376     if (! check_admin(1, "delete a hub")) return;
377
378     if (isset($id)) $hub = get_hub_by_id($id);
379     else $hub = get_hub_by_name($name);
380     if (! $hub) return false;
381
382     ///* Remember city ID for dropdown. */
383     //$city_id = $area->getCityId();
384
385     try {
386       $hub->delete();
387       echo "<p>Deleted hub.</p>\n";
388     }
389     catch (Exception $e) {
390       echo "<p>Error deleting $name!</p>\n";
391       return false;
392     }
393
394     return true;
395   }
396
397   function show_hub($name, &$id = null) {
398     if (isset($id)) $hub = get_hub_by_id($id);
399     else $hub = get_hub_by_name($name);
400     if (! $hub) return;
401
402     form();
403     show_hub_summary($hub, true);
404     echo ": ";
405     echo "\n</p>";
406
407     echo "<table>\n";
408     show_hub_form($hub);
409
410     if (check_admin(1)) {
411       echo "<tr>\n";
412       echo "  <td colspan=2>";
413       submit("update_hub", "Update");
414       echo "</td>\n";
415       echo "</tr>\n";
416     }
417
418     echo "</table>\n";
419     end_form();
420   }
421
422   /* /hub/in/area/Cambridge/1 */
423   if (count($parameters)) {
424     if ($parameters[0] == "in") {
425       switch ($parameters[1]) {
426         case "area":
427           $area_id = $parameters[3];
428           $_POST['area_id'] = $area_id;
429           $q = new AreaQuery;
430           $area = $q->findOneById($area_id);
431           $city = get_area_city($area);
432           if ($city) $city_id = $city->getId();
433           show_area_hubs(0, 10, $parameters[2], $area_id);
434         break;
435
436         case "city":
437           $city_id = $parameters[3];
438           $_POST['city_id'] = $city_id;
439           $q = new CityQuery;
440           $city = $q->findOneById($city_id);
441           show_city_hubs(0, 10, $parameters[2], $city_id);
442         break;
443       }
444     }
445   }
446   list($name, $id, $args) = parse_parameters($parameters);
447   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
448   if (count($args)) {
449     switch ($args[0]) {
450       case "delete":
451         confirm_delete_hub($name, $id);
452       break;
453
454       case "confirmdelete":
455         delete_hub($name, $id);
456       break;
457     }
458   }
459   else if (isset($name)) show_hub($name, $id);
460
461   show_hub_forms($city_id);
462   show_add_new_hub_form($city_id);
463
464 ?>