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