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);
8 else if (isset($_POST['add_hub'])) {
9 set_last_selected("area_id", $_POST['area_id']);
10 $id = add_hub($displayname);
12 echo "<p>Added hub.</p>\n";
13 $parameters = array($displayname, $id);
16 else if (isset($_POST['update_hub'])) {
17 list($name, $id, $args) = parse_parameters($parameters);
19 $hub = $q->findOneById($id);
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());
29 echo "<p>No such hub!</p>\n";
32 else if ($_POST['show_in_area']) {
33 set_last_selected("area_id", $_POST['area_id']);
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']));
39 else if ($_POST['show_in_city']) {
40 set_last_selected("city_id", $_POST['city_id']);
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']));
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());
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);
59 if ($role & $GLOBALS['ROLE_DISTRIBUTION']) printf(" <a class=\"small\" href=\"/order/to/hub/%s/%d\">Orders</a>", $d, $i);
61 echo " " . $hub->getDeleteLink();
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));
69 function show_hubs($offset, $per_page, $address_ids) {
71 $hubs = $q->filterByAddressId($address_ids)->find();
73 foreach ($hubs as $hub) show_hub_summary($hub);
78 function show_city_hubs($offset, $per_page, $city_name, $city_id = null) {
79 if (isset($city_id)) $city = get_city_by_id($city_id);
80 else if ($city_name) $city = get_city_by_name($city_name);
83 $areas = $q->filterByCityId($city->getId())->find();
85 foreach ($areas as $area) $area_ids[] = $area->getId();
87 $q = new AddressQuery;
88 $addresses = $q->filterByAreaId($area_ids)->find();
89 $address_ids = array();
90 foreach ($addresses as $address) $address_ids[] = $address->getId();
92 echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
93 return show_hubs($offset, $per_page, $address_ids);
95 else echo "<p>No such city!</p>\n";
98 function show_area_hubs($offset, $per_page, $area_name, $area_id = null) {
99 if (isset($area_id)) $area = get_area_by_id($area_id);
100 else if ($area_name) $area = get_area_by_name($area_name);
102 $q = new AddressQuery;
103 $addresses = $q->filterByAreaId($area->getId())->find();
104 $address_ids = array();
105 foreach ($addresses as $address) $address_ids[] = $address->getId();
107 echo "<p>Hubs in area " . $area->getLink() . ":";
108 return show_hubs($offset, $per_page, $address_ids);
110 else echo "<p>No such area!</p>\n";
113 function show_hub_areas_form($city_id = null) {
114 $areas = get_city_areas($city_id);
115 if (! count($areas)) {
116 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
120 $candidates = array();
121 foreach ($areas as $area) {
122 if (! count(get_area_hubs($area->getId()))) continue;
123 $candidates[] = $area;
125 if (! count($candidates)) return;
127 echo "<p>Show hubs in area\n";
128 echo "<select name=\"area_id\">\n";
129 foreach ($candidates as $area) {
130 option("area_id", $area->getId(), get_area_displayname($area));
133 submit("show_in_area", "Show");
136 function show_hub_cities_form($city_id = null) {
138 $cities = $q->orderByName()->find();
140 if (! count($cities)) {
141 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
145 $candidates = array();
146 foreach ($cities as $city) {
147 if (! count(get_city_hubs($city->getId()))) continue;
148 $candidates[] = $city;
150 if (! count($candidates)) return;
152 echo "<p>Show hubs in city\n";
153 echo "<select name=\"city_id\">\n";
154 foreach ($candidates as $city) {
155 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
158 submit("show_in_city", "Show");
161 function show_hub_forms($city_id) {
162 form("noprint standout");
163 show_hub_areas_form($city_id);
164 show_hub_cities_form($city_id);
168 function show_hub_role_form($role) {
169 return show_role_form($role, $GLOBALS['hub_roles']);
172 function show_hub_form($hub = null, $area_id = null) {
173 if (! $hub) $hub = new Hub;
177 echo " <td>Role</td>\n";
178 echo " <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
183 echo " <td>Hub name</td>\n";
184 echo " <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
188 $address = get_hub_address($hub);
189 if (! $address) $address = new Address;
191 echo " <td>Address</td>\n";
192 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
197 echo " <td>Postcode</td>\n";
198 $postcode = $address->getPostcode();
199 if (validate_postcode($postcode)) {
200 echo " <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
203 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
209 echo " <td>Telephone</td>\n";
210 echo " <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
213 echo " <td>Alternative telephone</td>\n";
214 echo " <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
219 echo " <td>Email</td>\n";
220 echo " <td>"; input("email", $hub->getEmail()); echo "</td>\n";
224 $areas = get_city_areas();
225 if (! isset($area_id)) $area_id = get_hub_area($hub);
227 echo " <td>Area</td>\n";
228 echo " <td><select name=\"area_id\">\n";
229 foreach ($areas as $area) {
230 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
232 echo " </select></td>\n";
236 function show_new_hub_form($city_id = null) {
237 if (! check_admin(1)) return;
239 $areas = get_city_areas($city_id);
240 if (! count($areas)) {
241 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
246 echo "<p>Add a new hub:</p>\n";
252 echo " <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
258 function show_add_new_hub_form() {
259 if (! check_admin(1)) return;
262 $cities = $q->find();
263 if (! count($cities)) {
264 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
268 form("noprint standout");
269 echo "<p>Add a new hub in <select name=\"city_id\">\n";
270 foreach ($cities as $city) {
271 option("city_id", $city->getId(), get_city_displayname($city));
274 submit("show_add_hub", "Proceed");
279 function update_hub(&$hub, $area_id, $new = false) {
283 for ($i = 0; $i < count($hub_roles); $i++) {
284 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
287 $displayname = $_POST['displayname'];
289 if (! $displayname) {
290 echo "<p>Must have a name!</p>\n";
295 $line = $_POST['address'];
296 $postcode = trim($_POST['postcode']);
298 $postcode = format_postcode($_POST['postcode'], true);
299 if (! $postcode) return false;
301 $q = new AddressQuery;
302 /* XXX: Finding by area properly? */
303 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
304 if ($address->isNew()) {
305 /* Changing address. */
308 XXX: Check for other hubs at the old address.
309 Make this a new address if there are others, but
310 provide a link to update other hubs.
315 catch (Exception $e) {
316 echo "<p>Error adding $line.</p>\n";
321 $telephone1 = $_POST['telephone1'];
322 $telephone2 = $_POST['telephone2'];
323 $email = $_POST['email'];
325 $hub->setRole($role);
326 $hub->setDisplayname($displayname);
327 $hub->setTelephone1($telephone1);
328 $hub->setTelephone2($telephone2);
329 $hub->setEmail($email);
330 $hub->setAddressId($address->getId());
335 catch (Exception $e) {
336 if ($new) echo "<p>Error adding $displayname.</p>\n";
337 else echo "<p>Error updating $displayname.</p>\n";
344 function add_hub(&$name) {
345 if (! check_admin(1, "add a hub")) return;
347 $area_id = $_POST['area_id'];
348 if (! is_numeric($area_id)) {
349 echo "<p>Invalid area!</p>\n";
353 $area = get_area_by_id($area_id);
355 echo "<p>No such area!</p>\n";
360 if (! update_hub($hub, $area_id, true)) return false;
361 return $hub->getId();
364 function confirm_delete_hub($name, $id = null) {
365 if (! check_admin(1, "delete a hub")) return;
367 if (isset($id)) $hub = get_hub_by_id($id);
368 else $hub = get_hub_by_name($name);
369 if (! $hub) return false;
371 echo "<h3>Confirm deletion</h3>\n";
372 echo "<p>You must confirm deletion of hub " . $hub->getDisplayname() . ": " . $hub->getDeleteLink(true) . "</p>\n";
375 function delete_hub($name, $id = null, &$city_id = null) {
376 if (! check_admin(1, "delete a hub")) return;
378 if (isset($id)) $hub = get_hub_by_id($id);
379 else $hub = get_hub_by_name($name);
380 if (! $hub) return false;
382 ///* Remember city ID for dropdown. */
383 //$city_id = $area->getCityId();
387 echo "<p>Deleted hub.</p>\n";
389 catch (Exception $e) {
390 echo "<p>Error deleting $name!</p>\n";
397 function show_hub($name, &$id = null) {
398 if (isset($id)) $hub = get_hub_by_id($id);
399 else $hub = get_hub_by_name($name);
403 show_hub_summary($hub, true);
410 if (check_admin(1)) {
412 echo " <td colspan=2>";
413 submit("update_hub", "Update");
422 /* /hub/in/area/Cambridge/1 */
423 if (count($parameters)) {
424 if ($parameters[0] == "in") {
425 switch ($parameters[1]) {
427 $area_id = $parameters[3];
428 $_POST['area_id'] = $area_id;
430 $area = $q->findOneById($area_id);
431 $city = get_area_city($area);
432 if ($city) $city_id = $city->getId();
433 show_area_hubs(0, 10, $parameters[2], $area_id);
437 $city_id = $parameters[3];
438 $_POST['city_id'] = $city_id;
440 $city = $q->findOneById($city_id);
441 show_city_hubs(0, 10, $parameters[2], $city_id);
446 list($name, $id, $args) = parse_parameters($parameters);
447 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
451 confirm_delete_hub($name, $id);
454 case "confirmdelete":
455 delete_hub($name, $id);
459 else if (isset($name)) show_hub($name, $id);
461 show_hub_forms($city_id);
462 show_add_new_hub_form($city_id);