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