Typo.
[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['area_id']) {
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['city_id']) {
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     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
112     echo "<p>Show hubs in area\n";
113     echo "<select name=\"area_id\">\n";
114     foreach ($areas as $area) {
115       option("area_id", $area->getId(), get_area_displayname($area));
116     }
117     echo "</select>\n";
118     echo "<input type=\"submit\" value=\"Show\">\n";
119     echo "</form>\n";
120   }
121
122   function show_hub_cities_form($city_id = null) {
123     $q = new CityQuery;
124     $cities = $q->orderByName()->find();
125
126     if (! count($cities)) {
127       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
128       return;
129     }
130
131     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
132     echo "<p>Show hubs in city\n";
133     echo "<select name=\"city_id\">\n";
134     foreach ($cities as $city) {
135       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
136     }
137     echo "</select>\n";
138     echo "<input type=\"submit\" value=\"Show\">\n";
139     echo "</form>\n";
140   }
141
142   function show_hub_forms($city_id) {
143     show_hub_areas_form($city_id);
144     show_hub_cities_form($city_id);
145   }
146
147   function show_hub_role_form($role) {
148     return show_role_form($role, $GLOBALS['hub_roles']);
149   }
150
151   function show_hub_form($hub = null, $area_id = null) {
152     if (! $hub) $hub = new Hub;
153
154     /* Role. */
155     echo "<tr>\n";
156     echo "  <td>Role</td>\n";
157     echo "  <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
158     echo "</tr>\n";
159
160     /* Display name. */
161     echo "<tr>\n";
162     echo "  <td>Hub name</td>\n";
163     echo "  <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
164     echo "</tr>\n";
165
166     /* Address. */
167     $address = get_hub_address($hub);
168     if (! $address) $address = new Address;
169     echo "<tr>\n";
170     echo "  <td>Address</td>\n";
171     echo "  <td>"; textarea("address", $address->getLine()); echo "</td>\n";
172     echo "</tr>\n";
173
174     /* Postcode. */
175     echo "<tr>\n";
176     echo "  <td>Postcode</td>\n";
177     echo "  <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
178     echo "</tr>\n";
179
180     /* Telephone. */
181     echo "<tr>\n";
182     echo "  <td>Telephone</td>\n";
183     echo "  <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
184     echo "</tr>\n";
185     echo "<tr>\n";
186     echo "  <td>Alternative telephone</td>\n";
187     echo "  <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
188     echo "</tr>\n";
189
190     /* Email. */
191     echo "<tr>\n";
192     echo "  <td>Email</td>\n";
193     echo "  <td>"; input("email", $hub->getEmail()); echo "</td>\n";
194     echo "</tr>\n";
195
196     /* Area. */
197     $areas = get_city_areas();
198     if (! isset($area_id)) $area_id = get_hub_area($hub);
199     echo "<tr>\n";
200     echo "  <td>Area</td>\n";
201     echo "  <td><select name=\"area_id\">\n";
202     foreach ($areas as $area) {
203       option("area_id", $area->getId(), get_area_displayname($area), $area_id);
204     }
205     echo "  </select></td>\n";
206     echo "</tr>\n";
207   }
208
209   function show_new_hub_form($city_id = null) {
210     if (! check_admin(1)) return;
211
212     $areas = get_city_areas($city_id);
213     if (! count($areas)) {
214       echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
215       return;
216     }
217
218     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
219     echo "<p>Add a new hub:</p>\n";
220
221     echo "<table>\n";
222     show_hub_form($hub);
223
224     echo "<tr>\n";
225     echo "  <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
226     echo "</tr>\n";
227     echo "</table>\n";
228     echo "</form>\n";
229   }
230
231   function show_add_new_hub_form() {
232     if (! check_admin(1)) return;
233
234     $q = new CityQuery;
235     $cities = $q->find();
236     if (! count($cities)) {
237       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
238       return;
239     }
240
241     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
242     echo "<p>Add a new hub in <select name=\"city_id\">\n";
243     foreach ($cities as $city) {
244       option("city_id", $city->getId(), get_city_displayname($city));
245     }
246     echo "</select>";
247     submit("show_add_hub", "Proceed");
248     echo "</p>\n";
249     echo "</form>\n";
250   }
251
252   function update_hub(&$hub, $area_id, $new = false) {
253     global $hub_roles;
254
255     $role = 0;
256     for ($i = 0; $i < count($hub_roles); $i++) {
257       if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
258     }
259
260     $displayname = $_POST['displayname'];
261
262     if (! $displayname) {
263       echo "<p>Must have a name!</p>\n";
264       return false;
265     }
266
267     /* Get address. */
268     $line = $_POST['address'];
269     $postcode = $_POST['postcode'];
270     $q = new AddressQuery;
271     /* XXX: Finding by area properly? */
272     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
273     if ($address->isNew()) {
274       /* Changing address. */
275       //if (! $new)
276       /*
277         XXX: Check for other hubs at the old address.
278         Make this a new address if there are others, but
279         provide a link to update other hubs.
280       */
281       try {
282         $address->save();
283       }
284       catch (Exception $e) {
285         echo "<p>Error adding $line.</p>\n";
286         return false;
287       }
288     }
289
290     $telephone1 = $_POST['telephone1'];
291     $telephone2 = $_POST['telephone2'];
292     $email = $_POST['email'];
293
294     $hub->setRole($role);
295     $hub->setDisplayname($displayname);
296     $hub->setTelephone1($telephone1);
297     $hub->setTelephone2($telephone2);
298     $hub->setEmail($email);
299     $hub->setAddressId($address->getId());
300
301     try {
302       $hub->save();
303     }
304     catch (Exception $e) {
305       if ($new) echo "<p>Error adding $displayname.</p>\n";
306       else echo "<p>Error updating $displayname.</p>\n";
307       return false;
308     }
309
310     return true;
311   }
312
313   function add_hub(&$name) {
314     if (! check_admin(1, "add a hub")) return;
315
316     $area_id = $_POST['area_id'];
317     if (! is_numeric($area_id)) {
318       echo "<p>Invalid area!</p>\n";
319       return false;
320     }
321
322     $area = get_area_by_id($area_id);
323     if (! $area) {
324       echo "<p>No such area!</p>\n";
325       return false;
326     }
327
328     $hub = new Hub;
329     if (! update_hub($hub, $area_id, true)) return false;
330     return $hub->getId();
331   }
332
333   function delete_hub($name, $id = null, &$city_id = null) {
334     if (! check_admin(1, "delete a hub")) return;
335
336     if (isset($id)) $hub = get_hub_by_id($id);
337     else $hub = get_hub_by_name($name);
338     if (! $hub) return false;
339
340     ///* Remember city ID for dropdown. */
341     //$city_id = $area->getCityId();
342
343     try {
344       $hub->delete();
345       echo "<p>Deleted hub.</p>\n";
346     }
347     catch (Exception $e) {
348       echo "<p>Error deleting $name!</p>\n";
349       return false;
350     }
351
352     return true;
353   }
354
355   function show_hub($name, &$id = null) {
356     if (isset($id)) $hub = get_hub_by_id($id);
357     else $hub = get_hub_by_name($name);
358     if (! $hub) return;
359
360     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
361     show_hub_summary($hub, true);
362     echo ": ";
363     echo "\n</p>";
364
365     echo "<table>\n";
366     show_hub_form($hub);
367
368     if (check_admin(1)) {
369       echo "<tr>\n";
370       echo "  <td colspan=2>";
371       submit("update_hub", "Update");
372       echo "</td>\n";
373       echo "</tr>\n";
374     }
375
376     echo "</table>\n";
377     echo "</form>\n";
378   }
379
380   /* /hub/in/area/Cambridge/1 */
381   if (count($parameters)) {
382     if ($parameters[0] == "in") {
383       switch ($parameters[1]) {
384         case "area":
385           $area_id = $parameters[3];
386           $_POST['area_id'] = $area_id;
387           $q = new AreaQuery;
388           $area = $q->findOneById($area_id);
389           $city = get_area_city($area);
390           if ($city) $city_id = $city->getId();
391           show_area_hubs(0, 10, $parameters[2], $area_id);
392         break;
393
394         case "city":
395           $city_id = $parameters[3];
396           $_POST['city_id'] = $city_id;
397           $q = new CityQuery;
398           $city = $q->findOneById($city_id);
399           show_city_hubs(0, 10, $parameters[2], $city_id);
400         break;
401       }
402
403       show_new_hub_form($city_id);
404     }
405   }
406   list($name, $id, $args) = parse_parameters($parameters);
407   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
408   if (count($args)) {
409     switch ($args[0]) {
410       case "delete":
411         delete_hub($name, $id);
412       break;
413     }
414   }
415   else if (isset($name)) show_hub($name, $id);
416   else {
417     /* XXX: Shown after adding. */
418     show_hub_forms($city_id);
419     show_add_new_hub_form($city_id);
420   }
421
422   if (count($parameters)) {
423     show_hub_forms($city_id);
424   }
425
426 ?>