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($address_ids) {
70 list($first_page, $per_page) = pagination();
72 $hubs = $q->filterByAddressId($address_ids)->orderByDisplayname()->orderById()->paginate($first_page, $per_page);
74 foreach ($hubs as $hub) show_hub_summary($hub);
75 show_pagination($hubs);
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);
85 $areas = $q->filterByCityId($city->getId())->find();
87 foreach ($areas as $area) $area_ids[] = $area->getId();
89 $q = new AddressQuery;
90 $addresses = $q->filterByAreaId($area_ids)->find();
91 $address_ids = array();
92 foreach ($addresses as $address) $address_ids[] = $address->getId();
94 echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
95 return show_hubs($address_ids);
97 else echo "<p>No such city!</p>\n";
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);
104 $q = new AddressQuery;
105 $addresses = $q->filterByAreaId($area->getId())->find();
106 $address_ids = array();
107 foreach ($addresses as $address) $address_ids[] = $address->getId();
109 echo "<p>Hubs in area " . $area->getLink() . ":";
110 return show_hubs($address_ids);
112 else echo "<p>No such area!</p>\n";
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";
122 $candidates = array();
123 foreach ($areas as $area) {
124 if (! count(get_area_hubs($area->getId()))) continue;
125 $candidates[] = $area;
127 if (! count($candidates)) return;
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));
135 submit("show_in_area", "Show");
138 function show_hub_cities_form($city_id = null) {
140 $cities = $q->orderByName()->find();
142 if (! count($cities)) {
143 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
147 $candidates = array();
148 foreach ($cities as $city) {
149 if (! count(get_city_hubs($city->getId()))) continue;
150 $candidates[] = $city;
152 if (! count($candidates)) return;
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);
160 submit("show_in_city", "Show");
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);
170 function show_hub_role_form($role) {
171 return show_role_form($role, $GLOBALS['hub_roles']);
174 function show_hub_form($hub = null, $area_id = null) {
175 if (! $hub) $hub = new Hub;
179 echo " <td>Role</td>\n";
180 echo " <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
185 echo " <td>Hub name</td>\n";
186 echo " <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
190 $address = get_hub_address($hub);
191 if (! $address) $address = new Address;
193 echo " <td>Address</td>\n";
194 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\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";
205 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
211 echo " <td>Telephone</td>\n";
212 echo " <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
215 echo " <td>Alternative telephone</td>\n";
216 echo " <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
221 echo " <td>Email</td>\n";
222 echo " <td>"; input("email", $hub->getEmail()); echo "</td>\n";
226 $areas = get_city_areas();
227 if (! isset($area_id)) $area_id = get_hub_area($hub);
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);
234 echo " </select></td>\n";
238 function show_new_hub_form($city_id = null) {
239 if (! check_admin(1)) return;
241 $areas = get_city_areas($city_id);
242 if (! count($areas)) {
243 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
248 echo "<p>Add a new hub:</p>\n";
254 echo " <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
260 function show_add_new_hub_form() {
261 if (! check_admin(1)) return;
264 $cities = $q->find();
265 if (! count($cities)) {
266 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
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));
276 submit("show_add_hub", "Proceed");
281 function update_hub(&$hub, $area_id, $new = false) {
285 for ($i = 0; $i < count($hub_roles); $i++) {
286 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
289 $displayname = $_POST['displayname'];
291 if (! $displayname) {
292 echo "<p>Must have a name!</p>\n";
297 $line = $_POST['address'];
298 $postcode = trim($_POST['postcode']);
300 $postcode = format_postcode($_POST['postcode'], true);
301 if (! $postcode) return false;
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. */
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.
317 catch (Exception $e) {
318 echo "<p>Error adding $line.</p>\n";
323 $telephone1 = $_POST['telephone1'];
324 $telephone2 = $_POST['telephone2'];
325 $email = $_POST['email'];
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());
337 catch (Exception $e) {
338 if ($new) echo "<p>Error adding $displayname.</p>\n";
339 else echo "<p>Error updating $displayname.</p>\n";
346 function add_hub(&$name) {
347 if (! check_admin(1, "add a hub")) return;
349 $area_id = $_POST['area_id'];
350 if (! is_numeric($area_id)) {
351 echo "<p>Invalid area!</p>\n";
355 $area = get_area_by_id($area_id);
357 echo "<p>No such area!</p>\n";
362 if (! update_hub($hub, $area_id, true)) return false;
363 return $hub->getId();
366 function confirm_delete_hub($name, $id = null) {
367 if (! check_admin(1, "delete a hub")) return;
369 if (isset($id)) $hub = get_hub_by_id($id);
370 else $hub = get_hub_by_name($name);
371 if (! $hub) return false;
373 echo "<h3>Confirm deletion</h3>\n";
374 echo "<p>You must confirm deletion of hub " . $hub->getDisplayname() . ": " . $hub->getDeleteLink(true) . "</p>\n";
377 function delete_hub($name, $id = null, &$city_id = null) {
378 if (! check_admin(1, "delete a hub")) return;
380 if (isset($id)) $hub = get_hub_by_id($id);
381 else $hub = get_hub_by_name($name);
382 if (! $hub) return false;
384 ///* Remember city ID for dropdown. */
385 //$city_id = $area->getCityId();
389 echo "<p>Deleted hub.</p>\n";
391 catch (Exception $e) {
392 echo "<p>Error deleting $name!</p>\n";
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);
405 show_hub_summary($hub, true);
412 if (check_admin(1)) {
414 echo " <td colspan=2>";
415 submit("update_hub", "Update");
424 /* /hub/in/area/Cambridge/1 */
425 if (count($parameters)) {
426 if ($parameters[0] == "in") {
427 switch ($parameters[1]) {
429 $area_id = $parameters[3];
430 $_POST['area_id'] = $area_id;
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);
439 $city_id = $parameters[3];
440 $_POST['city_id'] = $city_id;
442 $city = $q->findOneById($city_id);
443 show_city_hubs($parameters[2], $city_id);
448 list($name, $id, $args) = parse_parameters($parameters);
449 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
453 confirm_delete_hub($name, $id);
456 case "confirmdelete":
457 delete_hub($name, $id);
461 else if (isset($name)) show_hub($name, $id);
463 show_hub_forms($city_id);
464 show_add_new_hub_form($city_id);