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_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());
52 echo " " . $hub->getDeleteLink();
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));
60 function show_hubs($offset, $per_page, $address_ids) {
62 $hubs = $q->filterByAddressId($address_ids)->find();
64 foreach ($hubs as $hub) show_hub_summary($hub);
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);
74 $areas = $q->filterByCityId($city->getId())->find();
76 foreach ($areas as $area) $area_ids[] = $area->getId();
78 $q = new AddressQuery;
79 $addresses = $q->filterByAreaId($area_ids)->find();
80 $address_ids = array();
81 foreach ($addresses as $address) $address_ids[] = $address->getId();
83 echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
84 return show_hubs($offset, $per_page, $address_ids);
86 else echo "<p>No such city!</p>\n";
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);
93 $q = new AddressQuery;
94 $addresses = $q->filterByAreaId($area->getId())->find();
95 $address_ids = array();
96 foreach ($addresses as $address) $address_ids[] = $address->getId();
98 echo "<p>Hubs in area " . $area->getLink() . ":";
99 return show_hubs($offset, $per_page, $address_ids);
101 else echo "<p>No such area!</p>\n";
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";
111 $candidates = array();
112 foreach ($areas as $area) {
113 if (! count(get_area_hubs($area->getId()))) continue;
114 $candidates[] = $area;
116 if (! count($candidates)) return;
118 echo "<p>Show hubs in area\n";
119 echo "<select name=\"area_id\">\n";
120 foreach ($candidates as $area) {
121 option("area_id", $area->getId(), get_area_displayname($area));
124 echo "<input type=\"submit\" value=\"Show\">\n";
127 function show_hub_cities_form($city_id = null) {
129 $cities = $q->orderByName()->find();
131 if (! count($cities)) {
132 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
136 $candidates = array();
137 foreach ($cities as $city) {
138 if (! count(get_city_hubs($city->getId()))) continue;
139 $candidates[] = $city;
141 if (! count($candidates)) return;
143 echo "<p>Show hubs in city\n";
144 echo "<select name=\"city_id\">\n";
145 foreach ($candidates as $city) {
146 option("city_id", $city->getId(), get_city_displayname($city), $city_id);
149 echo "<input type=\"submit\" value=\"Show\">\n";
152 function show_hub_forms($city_id) {
153 form("noprint standout");
154 show_hub_areas_form($city_id);
155 show_hub_cities_form($city_id);
159 function show_hub_role_form($role) {
160 return show_role_form($role, $GLOBALS['hub_roles']);
163 function show_hub_form($hub = null, $area_id = null) {
164 if (! $hub) $hub = new Hub;
168 echo " <td>Role</td>\n";
169 echo " <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
174 echo " <td>Hub name</td>\n";
175 echo " <td>"; input("displayname", $hub->getDisplayname()); echo "</td>\n";
179 $address = get_hub_address($hub);
180 if (! $address) $address = new Address;
182 echo " <td>Address</td>\n";
183 echo " <td>"; textarea("address", $address->getLine()); echo "</td>\n";
188 echo " <td>Postcode</td>\n";
189 echo " <td>"; input("postcode", $address->getPostcode()); echo "</td>\n";
194 echo " <td>Telephone</td>\n";
195 echo " <td>"; input("telephone1", $hub->getTelephone1()); echo "</td>\n";
198 echo " <td>Alternative telephone</td>\n";
199 echo " <td>"; input("telephone2", $hub->getTelephone2()); echo "</td>\n";
204 echo " <td>Email</td>\n";
205 echo " <td>"; input("email", $hub->getEmail()); echo "</td>\n";
209 $areas = get_city_areas();
210 if (! isset($area_id)) $area_id = get_hub_area($hub);
212 echo " <td>Area</td>\n";
213 echo " <td><select name=\"area_id\">\n";
214 foreach ($areas as $area) {
215 option("area_id", $area->getId(), get_area_displayname($area), $area_id);
217 echo " </select></td>\n";
221 function show_new_hub_form($city_id = null) {
222 if (! check_admin(1)) return;
224 $areas = get_city_areas($city_id);
225 if (! count($areas)) {
226 echo "<p>No <a href=\"/area\">areas</a>!</p>\n";
231 echo "<p>Add a new hub:</p>\n";
237 echo " <td colspan=2>"; submit("add_hub", "Add"); echo "</td></tr>\n";
243 function show_add_new_hub_form() {
244 if (! check_admin(1)) return;
247 $cities = $q->find();
248 if (! count($cities)) {
249 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
253 form("noprint standout");
254 echo "<p>Add a new hub in <select name=\"city_id\">\n";
255 foreach ($cities as $city) {
256 option("city_id", $city->getId(), get_city_displayname($city));
259 submit("show_add_hub", "Proceed");
264 function update_hub(&$hub, $area_id, $new = false) {
268 for ($i = 0; $i < count($hub_roles); $i++) {
269 if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
272 $displayname = $_POST['displayname'];
274 if (! $displayname) {
275 echo "<p>Must have a name!</p>\n";
280 $line = $_POST['address'];
281 $postcode = $_POST['postcode'];
282 $q = new AddressQuery;
283 /* XXX: Finding by area properly? */
284 $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
285 if ($address->isNew()) {
286 /* Changing address. */
289 XXX: Check for other hubs at the old address.
290 Make this a new address if there are others, but
291 provide a link to update other hubs.
296 catch (Exception $e) {
297 echo "<p>Error adding $line.</p>\n";
302 $telephone1 = $_POST['telephone1'];
303 $telephone2 = $_POST['telephone2'];
304 $email = $_POST['email'];
306 $hub->setRole($role);
307 $hub->setDisplayname($displayname);
308 $hub->setTelephone1($telephone1);
309 $hub->setTelephone2($telephone2);
310 $hub->setEmail($email);
311 $hub->setAddressId($address->getId());
316 catch (Exception $e) {
317 if ($new) echo "<p>Error adding $displayname.</p>\n";
318 else echo "<p>Error updating $displayname.</p>\n";
325 function add_hub(&$name) {
326 if (! check_admin(1, "add a hub")) return;
328 $area_id = $_POST['area_id'];
329 if (! is_numeric($area_id)) {
330 echo "<p>Invalid area!</p>\n";
334 $area = get_area_by_id($area_id);
336 echo "<p>No such area!</p>\n";
341 if (! update_hub($hub, $area_id, true)) return false;
342 return $hub->getId();
345 function delete_hub($name, $id = null, &$city_id = null) {
346 if (! check_admin(1, "delete a hub")) return;
348 if (isset($id)) $hub = get_hub_by_id($id);
349 else $hub = get_hub_by_name($name);
350 if (! $hub) return false;
352 ///* Remember city ID for dropdown. */
353 //$city_id = $area->getCityId();
357 echo "<p>Deleted hub.</p>\n";
359 catch (Exception $e) {
360 echo "<p>Error deleting $name!</p>\n";
367 function show_hub($name, &$id = null) {
368 if (isset($id)) $hub = get_hub_by_id($id);
369 else $hub = get_hub_by_name($name);
373 show_hub_summary($hub, true);
380 if (check_admin(1)) {
382 echo " <td colspan=2>";
383 submit("update_hub", "Update");
392 /* /hub/in/area/Cambridge/1 */
393 if (count($parameters)) {
394 if ($parameters[0] == "in") {
395 switch ($parameters[1]) {
397 $area_id = $parameters[3];
398 $_POST['area_id'] = $area_id;
400 $area = $q->findOneById($area_id);
401 $city = get_area_city($area);
402 if ($city) $city_id = $city->getId();
403 show_area_hubs(0, 10, $parameters[2], $area_id);
407 $city_id = $parameters[3];
408 $_POST['city_id'] = $city_id;
410 $city = $q->findOneById($city_id);
411 show_city_hubs(0, 10, $parameters[2], $city_id);
416 list($name, $id, $args) = parse_parameters($parameters);
417 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
421 delete_hub($name, $id);
425 else if (isset($name)) show_hub($name, $id);
427 show_hub_forms($city_id);
428 show_add_new_hub_form($city_id);