8db2720fbcc1758ff1133fca5b59ece7faf8b533
[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_order_ids_by_state($state_mask) {
179     $order_ids = array();
180     $dbh = Propel::getConnection();
181     $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and state & $state_mask");
182     $sth->execute();
183     $order_states = OrderStatePeer::populateObjects($sth);
184     foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
185     return $order_ids;
186   }
187
188   function get_beneficiary_orders($contact, $state_mask = null) {
189     $q = new OrderQuery;
190     $q->filterByBeneficiaryId($contact->getId());
191     if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
192     return $q->orderByDate()->find();
193   }
194
195   function get_requester_orders($contact, $state_mask = null) {
196     $q = new OrderQuery;
197     $q->filterByRequesterId($contact->getId());
198     if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
199     return $q->orderByDate()->find();
200   }
201
202   function get_contact_orders($contact, $state_mask = null) {
203     $q = new OrderQuery;
204     $q->filterByBeneficiaryId($contact->getId())->_or()->filterByRequesterId($contact->getId());
205     if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
206     return $q->orderByDate()->find();
207   }
208
209   function get_user_by_contact_id($id, $verbose = true) {
210     $q = new UserQuery;
211     $user = $q->findOneByContactId($id);
212
213     if (! $q->count()) {
214       if ($verbose) echo "<p>No such user!</p>\n";
215       return null;
216     }
217
218     return $user;
219   }
220
221   function get_city_displayname($city) {
222     return $city->getName() . ", " . $city->getPostcodeArea();
223   }
224
225   function get_area_displayname($area) {
226     return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
227   }
228
229   function get_donation_displayname($donation) {
230     return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
231   }
232
233   function get_order_parcel_string($order) {
234     global $parcel_sizes, $parcel_contents;
235
236     $parcel_size = null;
237     for ($i = 0 ; $i < count($parcel_sizes); $i++) {
238       if ($order->getParcel() & (1 << $i)) {
239         $parcel_size = $parcel_sizes[$i];
240         break;
241       }
242     }
243
244     $selected = array();
245     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
246       if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
247     }
248
249     $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
250     $ret = preg_replace('/^: /', '', $ret);
251     $ret = preg_replace('/: $/', '', $ret);
252
253     return $ret;
254   }
255
256   function get_order_displayname($order) {
257     return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
258   }
259
260   function get_order_summary($order) {
261     $ret = "Order " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
262
263     if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
264
265     /* XXX: Should pull from query. */
266     $q = new ContactQuery;
267     $contact = $q->findOneById($order->getRequesterId());
268     if ($contact) {
269       $ret .= " referred by " . $contact->getLink();
270       $area = get_contact_area($contact);
271       if ($area) $ret .= " in " . $area->getLink();
272     }
273
274     $q = new ContactQuery;
275     $contact = $q->findOneById($order->getBeneficiaryId());
276     if ($contact) {
277       $ret .= " for " . $contact->getLink();
278       $area = get_contact_area($contact);
279       if ($area) $ret .= " in " . $area->getLink();
280     }
281
282     if ($order->getHubId()) {
283       $q = new HubQuery;
284       $hub = $q->findOneById($order->getHubId());
285       if ($hub) $ret .= " to hub " . $hub->getLink();
286       $area = get_hub_area($hub);
287       if ($area) $ret .= " in " . $area->getLink();
288     }
289
290     return $ret;
291   }
292
293   function get_address_area($address) {
294     $q = new AreaQuery;
295     return $q->findOneById($address->getAreaId());
296   }
297
298   function get_contact_address($contact) {
299     $q = new AddressQuery;
300     return $q->findOneById($contact->getAddressId());
301   }
302
303   function get_contact_area($contact) {
304     $address = get_contact_address($contact);
305     if (! $address) return null;
306
307     return get_address_area($address);
308   }
309
310   function get_contact_city($contact) {
311     $area = get_contact_area($contact);
312     if (! $area) return null;
313
314     return get_area_city($area);
315   }
316
317   /* Parcel strings are the same so this can work. */
318   function get_contact_parcel_string($contact) {
319     return get_order_parcel_string($contact);
320   }
321
322   /* Hub and Contact are similar enough that this can work. */
323   function get_hub_address($hub) {
324     return get_contact_address($hub);
325   }
326
327   /* Hub and Contact are similar enough that this can work. */
328   function get_hub_area($hub) {
329     return get_contact_area($hub);
330   }
331
332   /* Hub and Contact are similar enough that this can work. */
333   function get_hub_city($hub) {
334     return get_contact_city($hub);
335   }
336
337   function get_area_contacts($area_id = null, $role = null) {
338     $q = new ContactQuery;
339     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
340     if (isset($role)) $q->where("role & $role");
341     return $q->orderByDisplayname()->find();
342   }
343
344   function get_area_requesters($area_id = null) {
345     return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
346   }
347
348   function get_area_beneficiaries($area_id = null) {
349     return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
350   }
351
352   function get_area_donors($area_id = null) {
353     return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
354   }
355
356   function get_city_contacts($city_id = null, $role = null) {
357     /* XXX */
358     $area_ids = array();
359     $areas = get_city_areas($city_id);
360     foreach ($areas as $area) $area_ids[] = $area->getId();
361     return get_area_contacts($area_ids, $role);
362   }
363
364   function get_city_requesters($city_id = null) {
365     return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
366   }
367
368   function get_city_beneficiaries($city_id = null) {
369     return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
370   }
371
372   function get_city_donors($city_id = null) {
373     return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
374   }
375
376   function get_city_drivers($city_id = null) {
377     return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
378   }
379
380   function get_role_string($object, $roles) {
381     $role = $object->getRole();
382
383     $selected = array();
384
385     for ($i =0; $i < count($roles); $i++) {
386       if ($role & (1 << $i)) $selected[] = $roles[$i];
387     }
388
389     return implode(", ", $selected);
390   }
391
392   function get_contact_role_string($contact) {
393     return get_role_string($contact, $GLOBALS['contact_roles']);
394   }
395
396   function get_hub_role_string($hub) {
397     return get_role_string($hub, $GLOBALS['hub_roles']);
398   }
399
400   function show_role_form($role, $roles) {
401     for ($i = 0; $i < count($roles); $i++) {
402       echo " <input type=\"checkbox\" name=\"role_$i\"";
403       if ($role & (1 << $i)) echo " checked";
404       echo ">$roles[$i]\n";
405     }
406   }
407
408   function get_area_hubs($area_id = null) {
409     $q = new HubQuery;
410     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
411     return $q->orderByDisplayname()->find();
412   }
413
414   function get_city_areas($city_id = null) {
415     $q = new AreaQuery;
416     $q->join("City")->orderBy("City.Name");
417     if (isset($city_id)) $q->filterByCityId($city_id);
418     return $q->orderByName()->find();
419   }
420
421   function get_city_areas_with_contacts($city_id = null, $role = null) {
422     $q = new AreaQuery;
423     $q->join("City")->orderBy("City.Name");
424     if (isset($city_id)) $q->filterByCityId($city_id);
425     /* XXX */
426     if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
427     else $q->useAddressQuery()->join("Contact")->endUse();
428     return $q->orderByName()->distinct()->find();
429   }
430
431   function get_city_areas_with_hubs($city_id = null) {
432     $q = new AreaQuery;
433     $q->join("City")->orderBy("City.Name");
434     if (isset($city_id)) $q->filterByCityId($city_id);
435     $q->useAddressQuery()->join("Hub")->endUse();
436     return $q->orderByName()->distinct()->find();
437   }
438
439   function get_city_hubs($city_id = null) {
440     $q = new HubQuery;
441     if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
442     return $q->orderByDisplayname()->find();
443   }
444
445   function iso8601_to_ymd($iso8601) {
446     return split("-", $iso8601);
447   }
448
449   function ymd_to_iso8601($name) {
450     $y = $_POST[$name . "_y"];
451     if (! $y) return null;
452     $m = $_POST[$name . "_m"];
453     if (! $m) $m = 1;
454     $d = $_POST[$name . "_d"];
455     if (! $d) $d = 1;
456     return sprintf("%04d-%02d-%02d", $y, $m, $d);
457   }
458
459   function show_date_form($name, $date = null) {
460     $past = 60;
461     $future = 60;
462     echo "<select name=\"$name\">\n";
463     $now = date('Y-m-d', time());
464     list($y, $m, $d) = explode('-', $now);
465     $today = mktime(0, 0, 0, $m, $d, $y);
466     if (isset($date)) {
467       list($y, $m, $d) = explode('-', $date);
468       $then = mktime(0, 0, 0, $m, $d, $y);
469       if ($then < $today - 86400 * $past || $then > $today + 86400 * $future) {
470         option($name, $date, date('l j F Y', $then), $date);
471       }
472     }
473     else $date = $now;
474     for ($i = -$past; $i < $future; $i++) {
475       $then = $today + 86400 * $i;
476       option($name, date('Y-m-d', $then), date('l j F Y', $then), $date);
477     }
478     echo "</select>\n";
479     return;
480     if (! isset($date)) $date = date('Y-m-d');
481     list($y, $m, $d) = iso8601_to_ymd($date);
482
483     echo "Year: <input name=\"$name" . "_y\" value=\"$y\" size=4 maxlen=4> ";
484     echo "Month: <input name=\"$name" . "_m\" value=\"$m\" size=2 maxlen=2> ";
485     echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
486   }
487
488   function get_small_link() {
489     /* Args are <alt text>, <format>, [<stuff> ...] */
490     $args = func_get_args();
491     $html = htmlspecialchars(array_shift($args));
492     $url = array_shift($args);
493     return vsprintf("<a class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
494   }
495
496   function small_link() {
497     echo call_user_func_array("get_small_link", func_get_args());
498   }
499
500   include_once("$lib_root/admin.php");
501   include_once("$lib_root/forms.php");
502
503 ?>