Make textareas dynamically resizable.
[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_state_string($order_state = null) {
261     global $states;
262
263     if (is_null($order_state)) return null;
264
265     for ($i = 0; $i < count($states); $i++) {
266       if ($order_state->getState() & (1 << $i)) {
267         return $states[$i];
268       }
269     }
270
271     return "unknown";
272   }
273
274   function get_order_state($order) {
275     $q = new OrderStateQuery();
276     return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
277   }
278
279   function get_order_summary($order) {
280     $ret = "Order ";
281     $order_state = get_order_state($order);
282     if ($order_state) $ret = "<strong>" . ucfirst(get_order_state_string($order_state)) . "</strong> order ";
283     $ret .= $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
284
285     if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
286
287     /* XXX: Should pull from query. */
288     $q = new ContactQuery;
289     $contact = $q->findOneById($order->getRequesterId());
290     if ($contact) {
291       $ret .= " referred by " . $contact->getLink();
292       $area = get_contact_area($contact);
293       if ($area) $ret .= " in " . $area->getLink();
294     }
295
296     $q = new ContactQuery;
297     $contact = $q->findOneById($order->getBeneficiaryId());
298     if ($contact) {
299       $ret .= " for " . $contact->getLink();
300       $area = get_contact_area($contact);
301       if ($area) $ret .= " in " . $area->getLink();
302     }
303
304     if ($order->getHubId()) {
305       $q = new HubQuery;
306       $hub = $q->findOneById($order->getHubId());
307       if ($hub) $ret .= " to hub " . $hub->getLink();
308       $area = get_hub_area($hub);
309       if ($area) $ret .= " in " . $area->getLink();
310     }
311
312     return $ret;
313   }
314
315   function get_address_area($address) {
316     $q = new AreaQuery;
317     return $q->findOneById($address->getAreaId());
318   }
319
320   function get_address_map_link($address) {
321     $postcode = trim($address->getPostcode());
322     if ($postcode) {
323       return " " . get_small_link("Map", "http://maps.google.co.uk/maps?q=" . urlencode($postcode));
324     }
325   }
326
327   function get_contact_address($contact) {
328     $q = new AddressQuery;
329     return $q->findOneById($contact->getAddressId());
330   }
331
332   function get_contact_area($contact) {
333     $address = get_contact_address($contact);
334     if (! $address) return null;
335
336     return get_address_area($address);
337   }
338
339   function get_contact_city($contact) {
340     $area = get_contact_area($contact);
341     if (! $area) return null;
342
343     return get_area_city($area);
344   }
345
346   /* Parcel strings are the same so this can work. */
347   function get_contact_parcel_string($contact) {
348     return get_order_parcel_string($contact);
349   }
350
351   /* Hub and Contact are similar enough that this can work. */
352   function get_hub_address($hub) {
353     return get_contact_address($hub);
354   }
355
356   /* Hub and Contact are similar enough that this can work. */
357   function get_hub_area($hub) {
358     return get_contact_area($hub);
359   }
360
361   /* Hub and Contact are similar enough that this can work. */
362   function get_hub_city($hub) {
363     return get_contact_city($hub);
364   }
365
366   function get_area_contacts($area_id = null, $role = null) {
367     $q = new ContactQuery;
368     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
369     if (isset($role)) $q->where("role & $role");
370     return $q->orderByDisplayname()->find();
371   }
372
373   function get_area_requesters($area_id = null) {
374     return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
375   }
376
377   function get_area_beneficiaries($area_id = null) {
378     return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
379   }
380
381   function get_area_donors($area_id = null) {
382     return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
383   }
384
385   function get_city_contacts($city_id = null, $role = null) {
386     /* XXX */
387     $area_ids = array();
388     $areas = get_city_areas($city_id);
389     foreach ($areas as $area) $area_ids[] = $area->getId();
390     return get_area_contacts($area_ids, $role);
391   }
392
393   function get_city_requesters($city_id = null) {
394     return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
395   }
396
397   function get_city_beneficiaries($city_id = null) {
398     return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
399   }
400
401   function get_city_donors($city_id = null) {
402     return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
403   }
404
405   function get_city_drivers($city_id = null) {
406     return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
407   }
408
409   function get_role_string($object, $roles) {
410     $role = $object->getRole();
411
412     $selected = array();
413
414     for ($i =0; $i < count($roles); $i++) {
415       if ($role & (1 << $i)) $selected[] = $roles[$i];
416     }
417
418     return implode(", ", $selected);
419   }
420
421   function get_contact_role_string($contact) {
422     return get_role_string($contact, $GLOBALS['contact_roles']);
423   }
424
425   function get_hub_role_string($hub) {
426     return get_role_string($hub, $GLOBALS['hub_roles']);
427   }
428
429   function show_role_form($role, $roles) {
430     for ($i = 0; $i < count($roles); $i++) {
431       echo " <input type=\"checkbox\" id=\"role_$i\" name=\"role_$i\"";
432       if ($role & (1 << $i)) echo " checked";
433       echo "><label for=\"role_$i\">$roles[$i]</label>\n";
434     }
435   }
436
437   function get_area_hubs($area_id = null) {
438     $q = new HubQuery;
439     if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
440     return $q->orderByDisplayname()->find();
441   }
442
443   function get_city_areas($city_id = null) {
444     $q = new AreaQuery;
445     $q->join("City")->orderBy("City.Name");
446     if (isset($city_id)) $q->filterByCityId($city_id);
447     return $q->orderByName()->find();
448   }
449
450   function get_city_areas_with_contacts($city_id = null, $role = null) {
451     $q = new AreaQuery;
452     $q->join("City")->orderBy("City.Name");
453     if (isset($city_id)) $q->filterByCityId($city_id);
454     /* XXX */
455     if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
456     else $q->useAddressQuery()->join("Contact")->endUse();
457     return $q->orderByName()->distinct()->find();
458   }
459
460   function get_city_areas_with_hubs($city_id = null) {
461     $q = new AreaQuery;
462     $q->join("City")->orderBy("City.Name");
463     if (isset($city_id)) $q->filterByCityId($city_id);
464     $q->useAddressQuery()->join("Hub")->endUse();
465     return $q->orderByName()->distinct()->find();
466   }
467
468   function get_city_hubs($city_id = null) {
469     $q = new HubQuery;
470     if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
471     return $q->orderByDisplayname()->find();
472   }
473
474   function iso8601_to_ymd($iso8601) {
475     return split("-", $iso8601);
476   }
477
478   function ymd_to_iso8601($name) {
479     $y = $_POST[$name . "_y"];
480     if (! $y) return null;
481     $m = $_POST[$name . "_m"];
482     if (! $m) $m = 1;
483     $d = $_POST[$name . "_d"];
484     if (! $d) $d = 1;
485     return sprintf("%04d-%02d-%02d", $y, $m, $d);
486   }
487
488   function show_date_form($name, $date = null) {
489     if (! isset($date)) $date = date('Y-m-d', time());
490     datepicker($name, $date);
491   }
492
493   function validate_postcode($postcode, &$outward = null, &$inward = null) {
494     /*
495       Valid postcode formats (BS7666):
496
497         AN NLL
498         ABN NLL
499         ANN NLL
500         ABNN NLL
501         ABND NLL
502         ANC NLL
503
504       Where N is a number; A is a letter not including Q, V, X;
505       B is a letter not including I, J, Z; C is a letter from the set
506       ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
507       L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
508
509       The postcode GIR 0AA is also valid.
510     */
511     $outward = $inward = null;
512
513     /* Treat blank as valid for convenience. */
514     $postcode = trim($postcode);
515     if (! $postcode) return true;
516
517     $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
518     $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
519     $C = '[ABCDEFGHJKSTUW]';
520     $D = '[ABEHMNPRVWXY]';
521     $L = '[ABDEFGHJLNPQRSTUWXYZ]';
522     $N = '\d';
523     if (! preg_match("/^($A$N|$A$B$N|$A$N$N|$A$B$N$N|$A$B$N$D|$A$N$C|GIR)\s*($N$L$L)$/", $postcode, $m)) return false;
524     if ($m[1] == "GIR" && $m[2] != "0AA") return false;
525     list($ignored, $outward, $inward) = $m;
526     return true;
527   }
528
529   function format_postcode($postcode, $complain = true) {
530     if (validate_postcode($postcode, $outward, $inward)) {
531       return "$outward $inward";
532     }
533     if ($complain) {
534       echo "<p>Invalid postcode!</p>\n";
535       return null;
536     }
537   }
538
539   function get_small_link() {
540     /* Args are <alt text>, <format>, [<stuff> ...] */
541     $args = func_get_args();
542     $html = htmlspecialchars(array_shift($args));
543     $url = array_shift($args);
544     return vsprintf("<a class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
545   }
546
547   function small_link() {
548     echo call_user_func_array("get_small_link", func_get_args());
549   }
550
551   include_once("$lib_root/admin.php");
552   include_once("$lib_root/forms.php");
553
554 ?>