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