3 if ($_POST['area_name']) {
4 $id = add_area($_POST['area_name'], $_POST['city_id']);
6 echo "<p>Added area.</p>\n";
7 $parameters = array($_POST['area_name'], $id);
10 else if ($_POST['city_id']) {
11 /* XXX: city_id is actually a string $city_name/$city_id */
12 header(sprintf("Location: http%s://%s/%s/in/city/%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $_POST['city_id']));
15 else if ($_POST['update_area']) {
16 update_area_delivery_days($parameters[0]);
19 function show_areas($offset, $per_page, $city_name = null, $city_id = null) {
20 if (isset($city_name) || isset($city_id)) {
21 if (isset($city_id)) $city = get_city_by_id($city_id);
22 else if ($city_name) $city = get_city_by_name($city_name);
24 echo "<p>Areas in " . $city->getLink(get_city_displayname($city)) . ":";
26 $areas = $q->filterByCityId($city_id)->find();
29 foreach ($areas as $area) {
30 echo "<br>\nArea: " . $area->getStrongLink() . "\n";
31 $n = urlencode($area->getName());
33 echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
34 echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
35 echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
37 echo " " . $area->getDeleteLink();
44 else echo "<p>No such city!</p>\n";
48 function show_area_cities_form($city_id = null) {
51 if (! count($cities)) {
52 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
56 $candidates = array();
57 foreach ($cities as $city) {
58 if (! count(get_city_areas($city->getId()))) continue;
59 $candidates[] = $city;
61 if (! count($candidates)) return;
63 form("noprint standout");
64 echo "<p>Show areas in\n";
65 echo "<select name=\"city_id\">\n";
66 foreach ($candidates as $city) {
67 option("city_id", sprintf("%s/%s", $city->getName(), $city->getId()), get_city_displayname($city));
70 echo "<input type=\"submit\" value=\"Show\">\n";
74 function show_new_area_form($city_id = null) {
75 if (! check_admin(1)) return;
79 if (! count($cities)) {
80 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
84 form("noprint standout");
85 echo "<p>Add a new area <input name=\"area_name\">\n";
86 echo "in <select name=\"city_id\">\n";
88 foreach ($cities as $city) {
89 option("city_id", $city->getId(), get_city_displayname($city));
92 echo "<input type=\"submit\" value=\"Add\">\n";
96 function add_area($name, $city_id) {
97 if (! check_admin(1, "add an area")) return;
99 $name = urldecode($name);
100 $area = get_area_by_name($name, false);
102 echo "<p>$name already exists!</p>\n";
107 $city = get_city_by_id($city_id);
109 echo "<p>Not a valid city!</p>\n";
114 $area->setName($name);
115 $area->setCityId($city_id);
120 catch (Exception $e) {
121 echo "<p>Error adding $name!</p>\n";
126 return $area->getId();
129 function show_area_delivery_days_form($days) {
131 echo "Delivery days:";
132 if (check_admin(1)) {
133 for ($i = 0; $i < count($week); $i++) {
134 echo " <input type=\"checkbox\" name=\"day_$i\"";
135 if ($days & (1 << $i)) echo " checked";
140 if (! $days) echo " none";
142 for ($i = 0; $i < count($week); $i++) {
143 if ($days & (1 << $i)) echo " $week[$i]";
149 function update_area_delivery_days($name) {
152 if (! check_admin(1, "edit an area")) return false;
155 for ($i = 0; $i < count($week); $i++) {
156 if ($_POST['day_' . $i] == "on") $days |= (1 << $i);
159 $area = get_area_by_name($name);
160 if (! $area) return false;
162 $area->setDays($days);
167 catch (Exception $e) {
168 echo "<p>Error updating area!</p>\n";
172 echo "<p>Updated area.</p>\n";
176 function confirm_delete_area($name, $id = null, &$city_id = null) {
177 if (! check_admin(1, "delete an area")) return;
179 if (isset($id)) $area = get_area_by_id($id);
180 else $area = get_area_by_name($name);
181 if (! $area) return false;
183 echo "<h3>Confirm deletion</h3>\n";
184 echo "<p>You must confirm deletion of area " . $area->getName() . ": " . $area->getDeleteLink(true) . "</p>\n";
187 function delete_area($name, $id = null, &$city_id = null) {
188 if (! check_admin(1, "delete an area")) return;
190 if (isset($id)) $area = get_area_by_id($id);
191 else $area = get_area_by_name($name);
192 if (! $area) return false;
194 /* Remember city ID for dropdown. */
195 $city_id = $area->getCityId();
199 echo "<p>Deleted area.</p>\n";
201 catch (Exception $e) {
202 echo "<p>Error deleting $name!</p>\n";
203 /* XXX: Why? Check for addresses in use... */
210 function show_area($name, &$city_id = null) {
211 $area = get_area_by_name($name);
215 echo "<p>Area: <span class=\"strong\">" . $area->getName() . "</span>";
216 $n = $area->getName();
218 echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
219 echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
220 echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
221 if (check_admin(1)) {
222 echo " " . $area->getDeleteLink();
224 $city = get_city_by_id($area->getCityId());
226 /* Remember city ID for dropdown. */
227 $city_id = $city->getId();
228 echo " in " . $city->getLink(get_city_displayname($city));
232 show_area_delivery_days_form($area->getDays());
233 if (check_admin(1)) {
234 echo "<input type=\"submit\" name=\"update_area\" value=\"Update\">\n";
240 /* /area/in/Cambridge/1 */
241 if (count($parameters)) {
242 if ($parameters[0] == "in") {
243 if ($parameters[1] == "city") {
244 $city_id = $parameters[3];
245 show_areas(0, 10, $parameters[2], $city_id);
246 show_new_area_form($city_id);
250 list($name, $id, $args) = parse_parameters($parameters);
251 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
255 confirm_delete_area($name, $id, $city_id);
258 case "confirmdelete":
259 delete_area($name, $id, $city_id);
263 else if (isset($name)) show_area($name, $city_id);
265 show_area_cities_form($city_id);
266 show_new_area_form($city_id);
269 if (count($parameters))
270 show_area_cities_form($city_id);