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