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 if ($role & $GLOBALS['ROLE_COLLECTION']) printf(" <a class=\"small\" href=\"/donation/to/hub/%s/%d\">Donations</a>", urlencode($hub->getName()), $hub->getId());
54 if ($role & $GLOBALS['ROLE_DISTRIBUTION']) printf(" <a class=\"small\" href=\"/order/to/hub/%s/%d\">Orders</a>", urlencode($hub->getName()), $hub->getId());
56 echo " " . $hub->getDeleteLink();
58 $area = get_hub_area($hub);
59 if ($area) echo " in " . $area->getLink();
60 $city = get_hub_city($hub);
61 if ($city) echo ", " . $city->getLink(get_city_displayname($city));
64 function show_hubs($offset, $per_page, $address_ids) {
66 $hubs = $q->filterByAddressId($address_ids)->find();
68 foreach ($hubs as $hub) show_hub_summary($hub);
73 function show_city_hubs($offset, $per_page, $city_name, $city_id = null) {
74 if (isset($city_id)) $city = get_city_by_id($city_id);
75 else if ($city_name) $city = get_city_by_name($city_name);
78 $areas = $q->filterByCityId($city->getId())->find();
80 foreach ($areas as $area) $area_ids[] = $area->getId();
82 $q = new AddressQuery;
83 $addresses = $q->filterByAreaId($area_ids)->find();
84 $address_ids = array();
85 foreach ($addresses as $address) $address_ids[] = $address->getId();
87 echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
88 return show_hubs($offset, $per_page, $address_ids);
90 else echo "<p>No such city!</p>\n";
93 function show_area_hubs($offset, $per_page, $area_name, $area_id = null) {
94 if (isset($area_id)) $area = get_area_by_id($area_id);
95 else if ($area_name) $area = get_area_by_name($area_name);
97 $q = new AddressQuery;
98 $addresses = $q->filterByAreaId($area->getId())->find();
99 $address_ids = array();
100 foreach ($addresses as $address) $address_ids[] = $address->getId();
102 echo "<p>Hubs in area " . $area->getLink() . ":";
103 return show_hubs($offset, $per_page, $address_ids);
105 else echo "<p>No such area!</p>\n";
108 function show_hub_areas_form($city_id = null) {
109 $areas = get_city_areas($city_id);
110 if (! count($areas)) {
111 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
115 $candidates = array();
116 foreach ($areas as $area) {
117 if (! count(get_area_hubs($area->getId()))) continue;
118 $candidates[] = $area;
120 if (! count($candidates)) return;
122 echo "<p>Show hubs in area\n";
123 echo "<select name=\"area_id\">\n";
124 foreach ($candidates as $area) {
125 option("area_id", $area->getId(), get_area_displayname($area));
128 submit("show_in_area", "Show");
131 function show_hub_cities_form($city_id = null) {
133 $cities = $q->orderByName()->find();
135 if (! count($cities)) {
136 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
140 $candidates = array();
141 foreach ($cities as $city) {
142 if (! count(get_city_hubs($city->getId()))) continue;
143 $candidates[] = $city;
145 if (! count($candidates)) return;
147 echo "<p>Show hubs in city\n";
148 echo "<select name=\"city_id\">\n";
149 foreach ($candidates as $city) {
150 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
153 submit("show_in_city", "Show");
156 function show_hub_forms($city_id) {
157 form("noprint standout");
158 show_hub_areas_form($city_id);
159 show_hub_cities_form($city_id);
163 function show_hub_role_form($role) {
164 return show_role_form($role, $GLOBALS['hub_roles']);
167 function show_hub_form($hub = null, $area_id = null) {
168 if (! $hub) $hub = new Hub;
172 echo " <td>Role</td>\n";
173 echo " <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
178 echo " <td>Hub name</td>\n";
179 echo " <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
183 $address = get_hub_address($hub);
184 if (! $address) $address = new Address;
186 echo " <td>Address</td>\n";
187 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
192 echo " <td>Postcode</td>\n";
193 $postcode = $address->getPostcode();
194 if (validate_postcode($postcode)) {
195 echo " <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
198 echo " <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
204 echo " <td>Telephone</td>\n";
205 echo " <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
208 echo " <td>Alternative telephone</td>\n";
209 echo " <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
214 echo " <td>Email</td>\n";
215 echo " <td>"; input("email", $hub->getEmail()); echo "</td>\n";
219 $areas = get_city_areas();
220 if (! isset($area_id)) $area_id = get_hub_area($hub);
222 echo " <td>Area</td>\n";
223 echo " <td><select name=\"area_id\">\n";
224 foreach ($areas as $area) {
225 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
227 echo " </select></td>\n";
231 function show_new_hub_form($city_id = null) {
232 if (! check_admin(1)) return;
234 $areas = get_city_areas($city_id);
235 if (! count($areas)) {
236 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
241 echo "<p>Add a new hub:</p>\n";
247 echo " <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
253 function show_add_new_hub_form() {
254 if (! check_admin(1)) return;
257 $cities = $q->find();
258 if (! count($cities)) {
259 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
263 form("noprint standout");
264 echo "<p>Add a new hub in <select name=\"city_id\">\n";
265 foreach ($cities as $city) {
266 option("city_id", $city->getId(), get_city_displayname($city));
269 submit("show_add_hub", "Proceed");
274 function update_hub(&$hub, $area_id, $new = false) {
278 for ($i = 0; $i < count($hub_roles); $i++) {
279 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
282 $displayname = $_POST['displayname'];
284 if (! $displayname) {
285 echo "<p>Must have a name!</p>\n";
290 $line = $_POST['address'];
291 $postcode = trim($_POST['postcode']);
293 $postcode = format_postcode($_POST['postcode'], true);
294 if (! $postcode) return false;
296 $q = new AddressQuery;
297 /* XXX: Finding by area properly? */
298 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
299 if ($address->isNew()) {
300 /* Changing address. */
303 XXX: Check for other hubs at the old address.
304 Make this a new address if there are others, but
305 provide a link to update other hubs.
310 catch (Exception $e) {
311 echo "<p>Error adding $line.</p>\n";
316 $telephone1 = $_POST['telephone1'];
317 $telephone2 = $_POST['telephone2'];
318 $email = $_POST['email'];
320 $hub->setRole($role);
321 $hub->setDisplayname($displayname);
322 $hub->setTelephone1($telephone1);
323 $hub->setTelephone2($telephone2);
324 $hub->setEmail($email);
325 $hub->setAddressId($address->getId());
330 catch (Exception $e) {
331 if ($new) echo "<p>Error adding $displayname.</p>\n";
332 else echo "<p>Error updating $displayname.</p>\n";
339 function add_hub(&$name) {
340 if (! check_admin(1, "add a hub")) return;
342 $area_id = $_POST['area_id'];
343 if (! is_numeric($area_id)) {
344 echo "<p>Invalid area!</p>\n";
348 $area = get_area_by_id($area_id);
350 echo "<p>No such area!</p>\n";
355 if (! update_hub($hub, $area_id, true)) return false;
356 return $hub->getId();
359 function confirm_delete_hub($name, $id = null) {
360 if (! check_admin(1, "delete a hub")) return;
362 if (isset($id)) $hub = get_hub_by_id($id);
363 else $hub = get_hub_by_name($name);
364 if (! $hub) return false;
366 echo "<h3>Confirm deletion</h3>\n";
367 echo "<p>You must confirm deletion of hub " . $hub->getDisplayname() . ": " . $hub->getDeleteLink(true) . "</p>\n";
370 function delete_hub($name, $id = null, &$city_id = null) {
371 if (! check_admin(1, "delete a hub")) return;
373 if (isset($id)) $hub = get_hub_by_id($id);
374 else $hub = get_hub_by_name($name);
375 if (! $hub) return false;
377 ///* Remember city ID for dropdown. */
378 //$city_id = $area->getCityId();
382 echo "<p>Deleted hub.</p>\n";
384 catch (Exception $e) {
385 echo "<p>Error deleting $name!</p>\n";
392 function show_hub($name, &$id = null) {
393 if (isset($id)) $hub = get_hub_by_id($id);
394 else $hub = get_hub_by_name($name);
398 show_hub_summary($hub, true);
405 if (check_admin(1)) {
407 echo " <td colspan=2>";
408 submit("update_hub", "Update");
417 /* /hub/in/area/Cambridge/1 */
418 if (count($parameters)) {
419 if ($parameters[0] == "in") {
420 switch ($parameters[1]) {
422 $area_id = $parameters[3];
423 $_POST['area_id'] = $area_id;
425 $area = $q->findOneById($area_id);
426 $city = get_area_city($area);
427 if ($city) $city_id = $city->getId();
428 show_area_hubs(0, 10, $parameters[2], $area_id);
432 $city_id = $parameters[3];
433 $_POST['city_id'] = $city_id;
435 $city = $q->findOneById($city_id);
436 show_city_hubs(0, 10, $parameters[2], $city_id);
441 list($name, $id, $args) = parse_parameters($parameters);
442 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
446 confirm_delete_hub($name, $id);
449 case "confirmdelete":
450 delete_hub($name, $id);
454 else if (isset($name)) show_hub($name, $id);
456 show_hub_forms($city_id);
457 show_add_new_hub_form($city_id);