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