Go live.
[readifood.git] / lib / functions.php
1 <?php
2
3   function parse_parameters($parameters) {
4     $name = null;
5     $id = null;
6     $args = array();
7
8     if (count($parameters) > 0) {
9       $name = array_shift($parameters);
10
11       /* Recall that we shifted. */
12       if (count($parameters) > 0) {
13         if (is_numeric($parameters[0])) {
14           $id = array_shift($parameters);
15         }
16       }
17
18       $args = $parameters;
19     }
20
21     return array($name, $id, $args);
22   }
23
24   function get_city_by_name($name, $postcode_area = null, $verbose = true) {
25     $q = new CityQuery;
26
27     $m = $q->filterByName(urldecode($name));
28     if (isset($postcode_area)) {
29       $m->filterByPostcodeArea($postcode_area);
30     }
31     $cities = $m->find();
32
33     switch ($m->count()) {
34       case 0:
35         if ($verbose) echo "<p>No such city!</p>\n";
36         return null;
37
38       case 1:
39         return $cities[0];
40
41       default:
42         if ($verbose) echo "<p>Can't identify city uniquely.</p>!\n";
43         return null;
44     }
45   }
46
47   function get_city_by_id($id, $verbose = true) {
48     $q = new CityQuery;
49     $city = $q->findOneById($id);
50
51     if (! $q->count()) {
52       if ($verbose) echo "<p>No such city!</p>\n";
53       return null;
54     }
55
56     return $city;
57   }
58
59   function get_area_by_name($name, $verbose = true) {
60     $q = new AreaQuery;
61     $areas = $q->filterByName(urldecode($name))->find();
62
63     switch ($q->count()) {
64       case 0:
65         if ($verbose) echo "<p>No such area!</p>\n";
66         return null;
67
68       case 1:
69         return $areas[0];
70
71       default:
72         if ($verbose) echo "<p>Can't identify area uniquely.</p>!\n";
73         return null;
74     }
75   }
76
77   function get_area_by_id($id, $verbose = true) {
78     $q = new AreaQuery;
79     $area = $q->findOneById($id);
80
81     if (! $q->count()) {
82       if ($verbose) echo "<p>No such area!</p>\n";
83       return null;
84     }
85
86     return $area;
87   }
88
89   function get_area_city($area) {
90     $q = new CityQuery;
91     return $q->findOneById($area->getCityId());
92   }
93
94   function get_contact_by_name($name, $verbose = true) {
95     $q = new ContactQuery;
96     $contact = $q->filterByDisplayname(urldecode($name))->find();
97
98     switch ($q->count()) {
99       case 0:
100         if ($verbose) echo "<p>No such contact!</p>\n";
101         return null;
102
103       case 1:
104         return $contacts[0];
105
106       default:
107         if ($verbose) echo "<p>Can't identify contact uniquely.</p>!\n";
108         return null;
109     }
110   }
111
112   function get_contact_by_id($id, $verbose = true) {
113     $q = new ContactQuery;
114     $contact = $q->findOneById($id);
115
116     if (! $q->count()) {
117       if ($verbose) echo "<p>No such contact!</p>\n";
118       return null;
119     }
120
121     return $contact;
122   }
123
124   function get_hub_by_name($name, $verbose = true) {
125     $q = new HubQuery;
126     $hubs = $q->filterByDisplayname(urldecode($name))->find();
127
128     switch ($q->count()) {
129       case 0:
130         if ($verbose) echo "<p>No such hub!</p>\n";
131         return null;
132
133       case 1:
134         return $hubs[0];
135
136       default:
137         if ($verbose) echo "<p>Can't identify hub uniquely.</p>!\n";
138         return null;
139     }
140   }
141
142   function get_hub_by_id($id, $verbose = true) {
143     $q = new HubQuery;
144     $hub = $q->findOneById($id);
145
146     if (! $q->count()) {
147       if ($verbose) echo "<p>No such hub!</p>\n";
148       return null;
149     }
150
151     return $hub;
152   }
153
154   function get_donation_by_id($id, $verbose = true) {
155     $q = new DonationQuery;
156     $donation = $q->findOneById($id);
157
158     if (! $q->count()) {
159       if ($verbose) echo "<p>No such donation!</p>\n";
160       return null;
161     }
162
163     return $donation;
164   }
165
166   function get_order_by_id($id, $verbose = true) {
167     $q = new OrderQuery;
168     $order = $q->findOneById($id);
169
170     if (! $q->count()) {
171       if ($verbose) echo "<p>No such order!</p>\n";
172       return null;
173     }
174
175     return $order;
176   }
177
178   function get_user_by_contact_id($id, $verbose = true) {
179     $q = new UserQuery;
180     $user = $q->findOneByContactId($id);
181
182     if (! $q->count()) {
183       if ($verbose) echo "<p>No such user!</p>\n";
184       return null;
185     }
186
187     return $user;
188   }
189
190   function get_city_displayname($city) {
191     return $city->getName() . ", " . $city->getPostcodeArea();
192   }
193
194   function get_area_displayname($area) {
195     return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
196   }
197
198   function get_donation_displayname($donation) {
199     return $donation->getQuantity() . "kg on " . $donation->getDate();
200   }
201
202   function get_order_displayname($order) {
203     return $order->getQuantity() . "kg on " . $order->getDate();
204   }
205
206   function get_address_area($address) {
207     $q = new AreaQuery;
208     return $q->findOneById($address->getAreaId());
209   }
210
211   function get_contact_address($contact) {
212     $q = new AddressQuery;
213     return $q->findOneById($contact->getAddressId());
214   }
215
216   function get_contact_area($contact) {
217     $address = get_contact_address($contact);
218     if (! $address) return null;
219
220     return get_address_area($address);
221   }
222
223   function get_contact_city($contact) {
224     $area = get_contact_area($contact);
225     if (! $area) return null;
226
227     return get_area_city($area);
228   }
229
230   /* Hub and Contact are similar enough that this can work. */
231   function get_hub_address($hub) {
232     return get_contact_address($hub);
233   }
234
235   /* Hub and Contact are similar enough that this can work. */
236   function get_hub_area($hub) {
237     return get_contact_area($hub);
238   }
239
240   /* Hub and Contact are similar enough that this can work. */
241   function get_hub_city($hub) {
242     return get_contact_city($hub);
243   }
244
245   function get_area_contacts($area_id = null, $role = null) {
246     $q = new ContactQuery;
247     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
248     if (isset($role)) $q->where("role & $role");
249     return $q->orderByDisplayname()->find();
250   }
251
252   function get_area_requesters($area_id = null) {
253     return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
254   }
255
256   function get_area_beneficiaries($area_id = null) {
257     return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
258   }
259
260   function get_area_donors($area_id = null) {
261     return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
262   }
263
264   function get_city_contacts($city_id = null, $role = null) {
265     /* XXX */
266     $area_ids = array();
267     $areas = get_city_areas($city_id);
268     foreach ($areas as $area) $area_ids[] = $area->getId();
269     return get_area_contacts($area_ids, $role);
270   }
271
272   function get_city_requesters($city_id = null) {
273     return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
274   }
275
276   function get_city_beneficiaries($city_id = null) {
277     return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
278   }
279
280   function get_city_donors($city_id = null) {
281     return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
282   }
283
284   function get_city_drivers($city_id = null) {
285     return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
286   }
287
288   function get_contact_role_string($contact) {
289     global $roles;
290
291     $role = $contact->getRole();
292
293     $selected = array();
294
295     for ($i =0; $i < count($roles); $i++) {
296       if ($role & (1 << $i)) $selected[] = $roles[$i];
297     }
298
299     return implode(", ", $selected);
300   }
301
302   function get_area_hubs($area_id = null) {
303     $q = new HubQuery;
304     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
305     return $q->orderByDisplayname()->find();
306   }
307
308   function get_city_areas($city_id = null) {
309     $q = new AreaQuery;
310     $q->join("City")->orderBy("City.Name");
311     if (isset($city_id)) $q->filterByCityId($city_id);
312     return $q->orderByName()->find();
313   }
314
315   function get_city_areas_with_contacts($city_id = null, $role = null) {
316     $q = new AreaQuery;
317     $q->join("City")->orderBy("City.Name");
318     if (isset($city_id)) $q->filterByCityId($city_id);
319     /* XXX */
320     if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
321     else $q->useAddressQuery()->join("Contact")->endUse();
322     return $q->orderByName()->distinct()->find();
323   }
324
325   function get_city_areas_with_hubs($city_id = null) {
326     $q = new AreaQuery;
327     $q->join("City")->orderBy("City.Name");
328     if (isset($city_id)) $q->filterByCityId($city_id);
329     $q->useAddressQuery()->join("Hub")->endUse();
330     return $q->orderByName()->distinct()->find();
331   }
332
333   function get_city_hubs($city_id = null) {
334     $q = new HubQuery;
335     if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
336     return $q->orderByDisplayname()->find();
337   }
338
339   function iso8601_to_ymd($iso8601) {
340     return split("-", $iso8601);
341   }
342
343   function ymd_to_iso8601($name) {
344     $y = $_POST[$name . "_y"];
345     if (! $y) return null;
346     $m = $_POST[$name . "_m"];
347     if (! $m) $m = 1;
348     $d = $_POST[$name . "_d"];
349     if (! $d) $d = 1;
350     return sprintf("%04d-%02d-%02d", $y, $m, $d);
351   }
352
353   function show_date_form($name, $date = null) {
354     echo "<select name=\"$name\">\n";
355     $now = time();
356     if (isset($date)) {
357       list($y, $m, $d) = explode('-', $date);
358       $then = mktime(0, 0, 0, $m, $d, $y);
359       option($name, $date, date('l j F Y', $then), $date);
360     }
361     for ($i = 0; $i < 60; $i++) {
362       $then = $now + 86400 * $i;
363       option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
364     }
365     echo "</select>\n";
366     return;
367     if (! isset($date)) $date = date('Y-m-d');
368     list($y, $m, $d) = iso8601_to_ymd($date);
369
370     echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
371     echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
372     echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
373   }
374
375   include_once("$lib_root/admin.php");
376   include_once("$lib_root/forms.php");
377
378 ?>