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