Added area.
\n"; $parameters = array($_POST['area_name'], $id); } } else if ($_POST['city_id']) { /* XXX: city_id is actually a string $city_name/$city_id */ header(sprintf("Location: http%s://%s/%s/in/city/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['city_id'])); exit; } else if ($_POST['update_area']) { update_area_delivery_days($parameters[0]); } function show_areas($offset, $per_page, $city_name = null, $city_id = null) { if (isset($city_name) || isset($city_id)) { if (isset($city_id)) $city = get_city_by_id($city_id); else if ($city_name) $city = get_city_by_name($city_name); if ($city) { echo "Areas in " . $city->getLink(get_city_displayname($city)) . ":";
$q = new AreaQuery;
$areas = $q->filterByCityId($city_id)->find();
if (count($areas)) {
foreach ($areas as $area) {
echo "
\nArea: " . $area->getStrongLink() . "\n";
$n = urlencode($area->getName());
$i = $area->getId();
echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
if (check_admin(1)) {
echo " " . $area->getDeleteLink();
}
}
}
else echo " none";
echo "
No such city!
\n"; } } function show_area_cities_form($city_id = null) { $q = new CityQuery; $cities = $q->find(); if (! count($cities)) { echo "No cities!
\n"; return; } echo "\n"; } function show_new_area_form($city_id = null) { if (! check_admin(1)) return; $q = new CityQuery; $cities = $q->find(); if (! count($cities)) { echo "No cities!
\n"; return; } echo "\n"; } function add_area($name, $city_id) { if (! check_admin(1, "add an area")) return; $name = urldecode($name); $area = get_area_by_name($name, false); if ($area) { echo "$name already exists!
\n"; show_area($name); return false; } $city = get_city_by_id($city_id); if (! $city) { echo "Not a valid city!
\n"; return false; } $area = new Area; $area->setName($name); $area->setCityId($city_id); try { $area->save(); } catch (Exception $e) { echo "Error adding $name!
\n"; /* XXX: Why? */ return false; } return $area->getId(); } function delete_city($name) { if (! check_admin(1, "delete a city")) return; $city = get_city_by_name($name); if (! $city) return false; try { $city->delete(); } catch (Exception $e) { echo "Error deleting $name!
\n"; /* XXX: Why? Check for addresses in use... */ return false; } return true; } function show_area_delivery_days_form($days) { global $week; echo "Delivery days:"; if (check_admin(1)) { for ($i = 0; $i < count($week); $i++) { echo " $week[$i]\n"; } } else { if (! $days) echo " none"; else { for ($i = 0; $i < count($week); $i++) { if ($days & (1 << $i)) echo " $week[$i]"; } } } } function update_area_delivery_days($name) { global $week; if (! check_admin(1, "edit an area")) return false; $days = 0; for ($i = 0; $i < count($week); $i++) { if ($_POST['day_' . $i] == "on") $days |= (1 << $i); } $area = get_area_by_name($name); if (! $area) return false; $area->setDays($days); try { $area->save(); } catch (Exception $e) { echo "Error updating area!
\n"; return false; } echo "Updated area.
\n"; return true; } function delete_area($name, $id = null, &$city_id = null) { if (! check_admin(1, "delete a area")) return; if (isset($id)) $area = get_area_by_id($id); else $area = get_area_by_name($name); if (! $area) return false; /* Remember city ID for dropdown. */ $city_id = $area->getCityId(); try { $area->delete(); echo "Deleted area.
\n"; } catch (Exception $e) { echo "Error deleting $name!
\n"; /* XXX: Why? Check for addresses in use... */ return false; } return true; } function show_area($name, &$city_id = null) { $area = get_area_by_name($name); if (! $area) return; echo "\n"; } /* /area/in/Cambridge/1 */ if (count($parameters)) { if ($parameters[0] == "in") { if ($parameters[1] == "city") { $city_id = $parameters[3]; show_areas(0, 10, $parameters[2], $city_id); show_new_area_form($city_id); } } } list($name, $id, $args) = parse_parameters($parameters); //echo "$name($id) " . print_r($args, true) . "
\n"; if (count($args)) { switch ($args[0]) { case "delete": delete_area($name, $id, $city_id); break; } } else if (isset($name)) show_area($name, $city_id); else { show_area_cities_form($city_id); show_new_area_form($city_id); } if (count($parameters)) show_area_cities_form($city_id); ?>