3 if ($_POST['area_name']) {
4 set_last_selected("city_id", $_POST['city_id']);
5 $id = add_area($_POST['area_name'], $_POST['city_id']);
7 echo "<p>Added area.</p>\n";
8 $parameters = array($_POST['area_name'], $id);
11 else if ($_POST['city_id']) {
12 set_last_selected("city_id", $_POST['city_id']);
13 $city_id = $_POST['city_id'];
14 $city = get_city_by_id($city_id);
15 header(sprintf("Location: http%s://%s/%s/in/city/%s/%d", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, $city->getName(), $city_id));
18 else if ($_POST['update_area']) {
19 update_area_delivery_days($parameters[0]);
22 function show_areas($city_name = null, $city_id = null) {
23 list($first_page, $per_page) = pagination();
24 if (isset($city_name) || isset($city_id)) {
25 if (isset($city_id)) $city = get_city_by_id($city_id);
26 else if ($city_name) $city = get_city_by_name($city_name);
28 echo "<p>Areas in " . $city->getLink(get_city_displayname($city)) . ":";
30 $areas = $q->filterByCityId($city_id)->orderByName()->orderById()->paginate($first_page, $per_page);
33 foreach ($areas as $area) {
34 echo "<br>\nArea: " . $area->getStrongLink() . "\n";
35 $n = urlencode($area->getName());
37 echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
38 echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
39 echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
41 echo " " . $area->getDeleteLink();
44 show_pagination($areas);
49 else echo "<p>No such city!</p>\n";
53 function show_area_cities_form($city_id = null) {
56 if (! count($cities)) {
57 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
61 $candidates = array();
62 foreach ($cities as $city) {
63 if (! count(get_city_areas($city->getId()))) continue;
64 $candidates[] = $city;
66 if (! count($candidates)) return;
68 form("noprint standout");
69 echo "<p>Show areas in\n";
70 echo "<select name=\"city_id\">\n";
71 foreach ($candidates as $city) {
72 option("city_id", $city->getId(), get_city_displayname($city));
75 echo "<input type=\"submit\" value=\"Show\">\n";
79 function show_new_area_form($city_id = null) {
80 if (! check_admin(1)) return;
84 if (! count($cities)) {
85 echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
89 form("noprint standout");
90 echo "<p>Add a new area <input name=\"area_name\">\n";
91 echo "in <select name=\"city_id\">\n";
93 foreach ($cities as $city) {
94 option("city_id", $city->getId(), get_city_displayname($city));
97 echo "<input type=\"submit\" value=\"Add\">\n";
101 function add_area($name, $city_id) {
102 if (! check_admin(1, "add an area")) return;
104 $name = urldecode($name);
105 $area = get_area_by_name($name, false);
107 echo "<p>$name already exists!</p>\n";
112 $city = get_city_by_id($city_id);
114 echo "<p>Not a valid city!</p>\n";
119 $area->setName($name);
120 $area->setCityId($city_id);
125 catch (Exception $e) {
126 echo "<p>Error adding $name!</p>\n";
131 return $area->getId();
134 function show_area_delivery_days_form($days) {
136 echo "Delivery days:";
137 if (check_admin(1)) {
138 for ($i = 0; $i < count($week); $i++) {
139 echo " <input type=\"checkbox\" id=\"day_$i\" name=\"day_$i\"";
140 if ($days & (1 << $i)) echo " checked";
141 echo "><label for=\"day_$i\">$week[$i]</label>\n";
145 if (! $days) echo " none";
147 for ($i = 0; $i < count($week); $i++) {
148 if ($days & (1 << $i)) echo " $week[$i]";
154 function update_area_delivery_days($name) {
157 if (! check_admin(1, "edit an area")) return false;
160 for ($i = 0; $i < count($week); $i++) {
161 if ($_POST['day_' . $i] == "on") $days |= (1 << $i);
164 $area = get_area_by_name($name);
165 if (! $area) return false;
167 $area->setDays($days);
172 catch (Exception $e) {
173 echo "<p>Error updating area!</p>\n";
177 echo "<p>Updated area.</p>\n";
181 function confirm_delete_area($name, $id = null, &$city_id = null) {
182 if (! check_admin(1, "delete an area")) return;
184 if (isset($id)) $area = get_area_by_id($id);
185 else $area = get_area_by_name($name);
186 if (! $area) return false;
188 echo "<h3>Confirm deletion</h3>\n";
189 echo "<p>You must confirm deletion of area " . $area->getName() . ": " . $area->getDeleteLink(true) . "</p>\n";
192 function delete_area($name, $id = null, &$city_id = null) {
193 if (! check_admin(1, "delete an area")) return;
195 if (isset($id)) $area = get_area_by_id($id);
196 else $area = get_area_by_name($name);
197 if (! $area) return false;
199 /* Remember city ID for dropdown. */
200 $city_id = $area->getCityId();
204 echo "<p>Deleted area.</p>\n";
206 catch (Exception $e) {
207 echo "<p>Error deleting $name!</p>\n";
208 /* XXX: Why? Check for addresses in use... */
215 function show_area($name, &$city_id = null) {
216 $area = get_area_by_name($name);
220 echo "<p>Area: <span class=\"strong\">" . $area->getName() . "</span>";
221 $n = $area->getName();
223 echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
224 echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
225 echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
226 if (check_admin(1)) {
227 echo " " . $area->getDeleteLink();
229 $city = get_city_by_id($area->getCityId());
231 /* Remember city ID for dropdown. */
232 $city_id = $city->getId();
233 echo " in " . $city->getLink(get_city_displayname($city));
237 show_area_delivery_days_form($area->getDays());
238 if (check_admin(1)) {
239 echo "<input type=\"submit\" name=\"update_area\" value=\"Update\">\n";
245 /* /area/in/Cambridge/1 */
246 if (count($parameters)) {
247 if ($parameters[0] == "in") {
248 if ($parameters[1] == "city") {
249 $city_id = $parameters[3];
250 show_areas($parameters[2], $city_id);
251 show_new_area_form($city_id);
255 list($name, $id, $args) = parse_parameters($parameters);
256 //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
260 confirm_delete_area($name, $id, $city_id);
263 case "confirmdelete":
264 delete_area($name, $id, $city_id);
268 else if (isset($name)) show_area($name, $city_id);
270 show_area_cities_form($city_id);
271 show_new_area_form($city_id);
274 if (count($parameters))
275 show_area_cities_form($city_id);