Filter area and city searches.
[readifood.git] / lib / area.php
1 <?php
2
3   if ($_POST['area_name']) {
4     $id = add_area($_POST['area_name'], $_POST['city_id']);
5     if ($id !== false) {
6       echo "<p>Added area.</p>\n";
7       $parameters = array($_POST['area_name'], $id);
8     }
9   }
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']));
13     exit;
14   }
15   else if ($_POST['update_area']) {
16     update_area_delivery_days($parameters[0]);
17   }
18
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);
23       if ($city) {
24         echo "<p>Areas in " . $city->getLink(get_city_displayname($city)) . ":";
25         $q = new AreaQuery;
26         $areas = $q->filterByCityId($city_id)->find();
27
28         if (count($areas)) {
29           foreach ($areas as $area) {
30             echo "<br>\nArea: " . $area->getStrongLink() . "\n";
31             $n = urlencode($area->getName());
32             $i = $area->getId();
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);
36             if (check_admin(1)) {
37               echo " " . $area->getDeleteLink();
38             }
39           }
40         }
41         else echo " none";
42         echo "</p>\n";
43       }
44       else echo "<p>No such city!</p>\n";
45     }
46   }
47
48   function show_area_cities_form($city_id = null) {
49     $q = new CityQuery;
50     $cities = $q->find();
51     if (! count($cities)) {
52       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
53       return;
54     }
55
56     $candidates = array();
57     foreach ($cities as $city) {
58       if (! count(get_city_areas($city->getId()))) continue;
59       $candidates[] = $city;
60     }
61     if (! count($candidates)) return;
62
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));
68     }
69     echo "</select>\n";
70     echo "<input type=\"submit\" value=\"Show\">\n";
71     end_form();
72   }
73
74   function show_new_area_form($city_id = null) {
75     if (! check_admin(1)) return;
76
77     $q = new CityQuery;
78     $cities = $q->find();
79     if (! count($cities)) {
80       echo "<p>No <a href=\"/city\">cities</a>!</p>\n";
81       return;
82     }
83
84     form("noprint standout");
85     echo "<p>Add a new area <input name=\"area_name\">\n";
86     echo "in <select name=\"city_id\">\n";
87
88     foreach ($cities as $city) {
89       option("city_id", $city->getId(), get_city_displayname($city));
90     }
91     echo "</select>\n";
92     echo "<input type=\"submit\" value=\"Add\">\n";
93     end_form();
94   }
95
96   function add_area($name, $city_id) {
97     if (! check_admin(1, "add an area")) return;
98
99     $name = urldecode($name);
100     $area = get_area_by_name($name, false);
101     if ($area) {
102       echo "<p>$name already exists!</p>\n";
103       show_area($name);
104       return false;
105     }
106
107     $city = get_city_by_id($city_id);
108     if (! $city) {
109       echo "<p>Not a valid city!</p>\n";
110       return false;
111     }
112
113     $area = new Area;
114     $area->setName($name);
115     $area->setCityId($city_id);
116
117     try {
118       $area->save();
119     }
120     catch (Exception $e) {
121       echo "<p>Error adding $name!</p>\n";
122       /* XXX: Why? */
123       return false;
124     }
125
126     return $area->getId();
127   }
128
129   function delete_city($name) {
130     if (! check_admin(1, "delete a city")) return;
131
132     $city = get_city_by_name($name);
133     if (! $city) return false;
134
135     try {
136       $city->delete();
137     }
138     catch (Exception $e) {
139       echo "<p>Error deleting $name!</p>\n";
140       /* XXX: Why? Check for addresses in use... */
141       return false;
142     }
143
144     return true;
145   }
146
147   function show_area_delivery_days_form($days) {
148     global $week;
149     echo "Delivery days:";
150     if (check_admin(1)) {
151       for ($i = 0; $i < count($week); $i++) {
152         echo " <input type=\"checkbox\" name=\"day_$i\"";
153         if ($days & (1 << $i)) echo " checked";
154         echo ">$week[$i]\n";
155       }
156     }
157     else {
158       if (! $days) echo " none";
159       else {
160         for ($i = 0; $i < count($week); $i++) {
161           if ($days & (1 << $i)) echo " $week[$i]";
162         }
163       }
164     }
165   }
166
167   function update_area_delivery_days($name) {
168     global $week;
169
170     if (! check_admin(1, "edit an area")) return false;
171
172     $days = 0;
173     for ($i = 0; $i < count($week); $i++) {
174       if ($_POST['day_' . $i] == "on") $days |= (1 << $i);
175     }
176
177     $area = get_area_by_name($name);
178     if (! $area) return false;
179
180     $area->setDays($days);
181
182     try {
183       $area->save();
184     }
185     catch (Exception $e) {
186       echo "<p>Error updating area!</p>\n";
187       return false;
188     }
189
190     echo "<p>Updated area.</p>\n";
191     return true;
192   }
193
194   function delete_area($name, $id = null, &$city_id = null) {
195     if (! check_admin(1, "delete a area")) return;
196
197     if (isset($id)) $area = get_area_by_id($id);
198     else $area = get_area_by_name($name);
199     if (! $area) return false;
200
201     /* Remember city ID for dropdown. */
202     $city_id = $area->getCityId();
203
204     try {
205       $area->delete();
206       echo "<p>Deleted area.</p>\n";
207     }
208     catch (Exception $e) {
209       echo "<p>Error deleting $name!</p>\n";
210       /* XXX: Why? Check for addresses in use... */
211       return false;
212     }
213
214     return true;
215   }
216
217   function show_area($name, &$city_id = null) {
218     $area = get_area_by_name($name);
219     if (! $area) return;
220
221     form();
222     echo "<p>Area: <span class=\"strong\">" . $area->getName() . "</span>";
223     $n = $area->getName();
224     $i = $area->getId();
225     echo " " . get_small_link("Contacts", "/contact/in/area/%s/%d", $n, $i);
226     echo " " . get_small_link("Donations", "/donation/in/area/%s/%d", $n, $i);
227     echo " " . get_small_link("Orders", "/order/in/area/%s/%d", $n, $i);
228     if (check_admin(1)) {
229       echo " " . $area->getDeleteLink();
230     }
231     $city = get_city_by_id($area->getCityId());
232     if ($city) {
233       /* Remember city ID for dropdown. */
234       $city_id = $city->getId();
235       echo " in " . $city->getLink(get_city_displayname($city));
236     }
237     echo ": ";
238     echo "\n<br>";
239     show_area_delivery_days_form($area->getDays());
240     if (check_admin(1)) {
241       echo "<input type=\"submit\" name=\"update_area\" value=\"Update\">\n";
242     }
243     echo "</p>\n";
244     end_form();
245   }
246
247   /* /area/in/Cambridge/1 */
248   if (count($parameters)) {
249     if ($parameters[0] == "in") {
250       if ($parameters[1] == "city") {
251         $city_id = $parameters[3];
252         show_areas(0, 10, $parameters[2], $city_id);
253         show_new_area_form($city_id);
254       }
255     }
256   }
257   list($name, $id, $args) = parse_parameters($parameters);
258   //echo "<p>$name($id) " . print_r($args, true) . "</p>\n";
259   if (count($args)) {
260     switch ($args[0]) {
261       case "delete":
262         delete_area($name, $id, $city_id);
263       break;
264     }
265   }
266   else if (isset($name)) show_area($name, $city_id);
267   else {
268     show_area_cities_form($city_id);
269     show_new_area_form($city_id);
270   }
271
272   if (count($parameters))
273     show_area_cities_form($city_id);
274 ?>