Added area.
\n"; $parameters = array($_POST['area_name'], $id); } } else if ($_POST['city_id']) { set_last_selected("city_id", $_POST['city_id']); $city_id = $_POST['city_id']; $city = get_city_by_id($city_id); header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $city->getName(), $city_id)); exit; } else if ($_POST['update_area']) { update_area_delivery_days($parameters[0]); } function show_areas($city_name = null, $city_id = null) { list($first_page, $per_page) = pagination(); 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)->orderByName()->orderById()->paginate($first_page, $per_page);
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();
}
}
show_pagination($areas);
}
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; } $candidates = array(); foreach ($cities as $city) { if (! count(get_city_areas($city->getId()))) continue; $candidates[] = $city; } if (! count($candidates)) return; form("noprint standout"); echo "Show areas in\n"; echo "\n"; echo "\n"; end_form(); } 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; } form("noprint standout"); echo "Add a new area \n"; echo "in \n"; echo "\n"; end_form(); } 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 show_area_delivery_days_form($days) { global $week; echo "Delivery days:"; if (check_admin(1)) { for ($i = 0; $i < count($week); $i++) { echo " \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 confirm_delete_area($name, $id = null, &$city_id = null) { if (! check_admin(1, "delete an area")) return; if (isset($id)) $area = get_area_by_id($id); else $area = get_area_by_name($name); if (! $area) return false; echo "You must confirm deletion of area " . $area->getName() . ": " . $area->getDeleteLink(true) . "
\n"; } function delete_area($name, $id = null, &$city_id = null) { if (! check_admin(1, "delete an 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; form(); echo "Area: " . $area->getName() . "";
$n = $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();
}
$city = get_city_by_id($area->getCityId());
if ($city) {
/* Remember city ID for dropdown. */
$city_id = $city->getId();
echo " in " . $city->getLink(get_city_displayname($city));
}
echo ": ";
echo "\n
";
show_area_delivery_days_form($area->getDays());
if (check_admin(1)) {
echo "\n";
}
echo "
$name($id) " . print_r($args, true) . "
\n"; if (count($args)) { switch ($args[0]) { case "delete": confirm_delete_area($name, $id, $city_id); break; case "confirmdelete": 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); ?>