3 if (isset($_POST['show_add_hub'])) {
4 $city_id = $_POST['city_id'];
5 show_new_hub_form($city_id);
7 else if (isset($_POST['add_hub'])) {
8 $id = add_hub($displayname);
10 echo "<p>Added hub.</p>\n";
11 $parameters = array($displayname, $id);
14 else if (isset($_POST['update_hub'])) {
15 list($name, $id, $args) = parse_parameters($parameters);
17 $hub = $q->findOneById($id);
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());
27 echo "<p>No such hub!</p>\n";
30 else if ($_POST['area_id']) {
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']));
36 else if ($_POST['city_id']) {
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']));
43 function show_hubs($offset, $per_page, $address_ids) {
45 $hubs = $q->filterByAddressId($address_ids)->find();
47 foreach ($hubs as $hub) {
48 echo "<br>\nhub " . $hub->getLink();
50 echo " " . $hub->getDeleteLink();
52 $area = get_hub_area($hub);
53 echo " in " . $area->getLink();
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);
64 $areas = $q->filterByCityId($city->getId())->find();
66 foreach ($areas as $area) $area_ids[] = $area->getId();
68 $q = new AddressQuery;
69 $addresses = $q->filterByAreaId($area_ids)->find();
70 $address_ids = array();
71 foreach ($addresses as $address) $address_ids[] = $address->getId();
73 echo "<p>hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
74 return show_hubs($offset, $per_page, $address_ids);
76 else echo "<p>No such city!</p>\n";
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);
83 $q = new AddressQuery;
84 $addresses = $q->filterByAreaId($area->getId())->find();
85 $address_ids = array();
86 foreach ($addresses as $address) $address_ids[] = $address->getId();
88 echo "<p>Hubs in area " . $area->getLink() . ":";
89 return show_hubs($offset, $per_page, $address_ids);
91 else echo "<p>No such area!</p>\n";
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";
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));
108 echo "<input type=\"submit\" value=\"Show\">\n";
112 function show_hub_cities_form($city_id = null) {
114 $cities = $q->orderByName()->find();
116 if (! count($cities)) {
117 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
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);
128 echo "<input type=\"submit\" value=\"Show\">\n";
132 function show_hub_forms($city_id) {
133 show_hub_areas_form($city_id);
134 show_hub_cities_form($city_id);
137 function show_hub_form($hub = null, $area_id = null) {
138 if (! $hub) $hub = new Hub;
142 echo " <td>Hub name</td>\n";
143 echo " <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
147 $address = get_hub_address($hub);
148 if (! $address) $address = new Address;
150 echo " <td>Address</td>\n";
151 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
156 echo " <td>Postcode</td>\n";
157 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
162 echo " <td>Telephone</td>\n";
163 echo " <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
166 echo " <td>Alternative telephone</td>\n";
167 echo " <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
172 echo " <td>Email</td>\n";
173 echo " <td>"; input("email", $hub->getEmail()); echo "</td>\n";
177 $areas = get_city_areas();
178 if (! isset($area_id)) $area_id = get_hub_area($hub);
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);
185 echo " </select></td>\n";
189 function show_new_hub_form($city_id = null) {
190 if (! check_admin(1)) return;
192 $areas = get_city_areas($city_id);
193 if (! count($areas)) {
194 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
198 echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
199 echo "<p>Add a new hub:</p>\n";
205 echo " <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
211 function show_add_new_hub_form() {
212 if (! check_admin(1)) return;
215 $cities = $q->find();
216 if (! count($cities)) {
217 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
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));
227 submit("show_add_hub", "Proceed");
232 function update_hub(&$hub, $area_id, $new = false) {
233 $displayname = $_POST['displayname'];
235 if (! $displayname) {
236 echo "<p>Must have a name!</p>\n";
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. */
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.
257 catch (Exception $e) {
258 echo "<p>Error adding $line.</p>\n";
263 $telephone1 = $_POST['telephone1'];
264 $telephone2 = $_POST['telephone2'];
265 $email = $_POST['email'];
267 $hub->setDisplayname($displayname);
268 $hub->setTelephone1($telephone1);
269 $hub->setTelephone2($telephone2);
270 $hub->setEmail($email);
271 $hub->setAddressId($address->getId());
276 catch (Exception $e) {
277 if ($new) echo "<p>Error adding $displayname.</p>\n";
278 else echo "<p>Error updating $displayname.</p>\n";
285 function add_hub(&$name) {
286 if (! check_admin(1, "add a hub")) return;
288 $area_id = $_POST['area_id'];
289 if (! is_numeric($area_id)) {
290 echo "<p>Invalid area!</p>\n";
294 $area = get_area_by_id($area_id);
296 echo "<p>No such area!</p>\n";
301 if (! update_hub($hub, $area_id, true)) return false;
302 return $hub->getId();
305 function delete_hub($name, $id = null, &$city_id = null) {
306 if (! check_admin(1, "delete a hub")) return;
308 if (isset($id)) $hub = get_hub_by_id($id);
309 else $hub = get_hub_by_name($name);
310 if (! $hub) return false;
312 ///* Remember city ID for dropdown. */
313 //$city_id = $area->getCityId();
317 echo "<p>Deleted hub.</p>\n";
319 catch (Exception $e) {
320 echo "<p>Error deleting $name!</p>\n";
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);
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();
337 $city = get_hub_city($hub);
338 if ($city) echo " in " . $city->getLink(get_city_displayname($city));
345 if (check_admin(1)) {
347 echo " <td colspan=2>";
348 submit("update_hub", "Update");
357 /* /hub/in/area/Cambridge/1 */
358 if (count($parameters)) {
359 if ($parameters[0] == "in") {
360 switch ($parameters[1]) {
362 $area_id = $parameters[3];
363 $_POST['area_id'] = $area_id;
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);
372 $city_id = $parameters[3];
373 $_POST['city_id'] = $city_id;
375 $city = $q->findOneById($city_id);
376 show_city_hubs(0, 10, $parameters[2], $city_id);
380 show_new_hub_form($city_id);
383 list($name, $id, $args) = parse_parameters($parameters);
384 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
388 delete_hub($name, $id);
392 else if (isset($name)) show_hub($name, $id);
394 /* XXX: Shown after adding. */
395 show_hub_forms($city_id);
396 show_add_new_hub_form($city_id);
399 if (count($parameters)) {
400 show_hub_forms($city_id);