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