3 function parse_parameters($parameters) {
8 if (count($parameters) > 0) {
9 $name = array_shift($parameters);
11 /* Recall that we shifted. */
12 if (count($parameters) > 0) {
13 if (is_numeric($parameters[0])) {
14 $id = array_shift($parameters);
21 return array($name, $id, $args);
24 function get_city_by_name($name, $postcode_area = null, $verbose = true) {
27 $m = $q->filterByName(urldecode($name));
28 if (isset($postcode_area)) {
29 $m->filterByPostcodeArea($postcode_area);
33 switch ($m->count()) {
35 if ($verbose) echo "<p>No such city!</p>\n";
42 if ($verbose) echo "<p>Can't identify city uniquely.</p>!\n";
47 function get_city_by_id($id, $verbose = true) {
49 $city = $q->findOneById($id);
52 if ($verbose) echo "<p>No such city!</p>\n";
59 function get_area_by_name($name, $verbose = true) {
61 $areas = $q->filterByName(urldecode($name))->find();
63 switch ($q->count()) {
65 if ($verbose) echo "<p>No such area!</p>\n";
72 if ($verbose) echo "<p>Can't identify area uniquely.</p>!\n";
77 function get_area_by_id($id, $verbose = true) {
79 $area = $q->findOneById($id);
82 if ($verbose) echo "<p>No such area!</p>\n";
89 function get_area_city($area) {
91 return $q->findOneById($area->getCityId());
94 function get_contact_by_name($name, $verbose = true) {
95 $q = new ContactQuery;
96 $contact = $q->filterByDisplayname(urldecode($name))->find();
98 switch ($q->count()) {
100 if ($verbose) echo "<p>No such contact!</p>\n";
107 if ($verbose) echo "<p>Can't identify contact uniquely.</p>!\n";
112 function get_contact_by_id($id, $verbose = true) {
113 $q = new ContactQuery;
114 $contact = $q->findOneById($id);
117 if ($verbose) echo "<p>No such contact!</p>\n";
124 function get_hub_by_name($name, $verbose = true) {
126 $hubs = $q->filterByDisplayname(urldecode($name))->find();
128 switch ($q->count()) {
130 if ($verbose) echo "<p>No such hub!</p>\n";
137 if ($verbose) echo "<p>Can't identify hub uniquely.</p>!\n";
142 function get_hub_by_id($id, $verbose = true) {
144 $hub = $q->findOneById($id);
147 if ($verbose) echo "<p>No such hub!</p>\n";
154 function get_donation_by_id($id, $verbose = true) {
155 $q = new DonationQuery;
156 $donation = $q->findOneById($id);
159 if ($verbose) echo "<p>No such donation!</p>\n";
166 function get_order_by_id($id, $verbose = true) {
168 $order = $q->findOneById($id);
171 if ($verbose) echo "<p>No such order!</p>\n";
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");
183 $order_states = OrderStatePeer::populateObjects($sth);
184 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
188 function get_beneficiary_orders($contact, $state_mask = null) {
190 $q->filterByBeneficiaryId($contact->getId());
191 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
192 return $q->orderByDate()->find();
195 function get_requester_orders($contact, $state_mask = null) {
197 $q->filterByRequesterId($contact->getId());
198 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
199 return $q->orderByDate()->find();
202 function get_contact_orders($contact, $state_mask = null) {
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();
209 function get_user_by_contact_id($id, $verbose = true) {
211 $user = $q->findOneByContactId($id);
214 if ($verbose) echo "<p>No such user!</p>\n";
221 function get_city_displayname($city) {
222 return $city->getName() . ", " . $city->getPostcodeArea();
225 function get_area_displayname($area) {
226 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
229 function get_donation_displayname($donation) {
230 return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
233 function get_order_parcel_string($order) {
234 global $parcel_sizes, $parcel_contents;
237 for ($i = 0 ; $i < count($parcel_sizes); $i++) {
238 if ($order->getParcel() & (1 << $i)) {
239 $parcel_size = $parcel_sizes[$i];
245 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
246 if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
249 $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
250 $ret = preg_replace('/^: /', '', $ret);
251 $ret = preg_replace('/: $/', '', $ret);
256 function get_order_displayname($order) {
257 return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
260 function get_order_state_string($order_state = null) {
263 if (is_null($order_state)) return null;
265 for ($i = 0; $i < count($states); $i++) {
266 if ($order_state->getState() & (1 << $i)) {
274 function get_order_state($order) {
275 $q = new OrderStateQuery();
276 return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
279 function get_order_summary($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);
285 if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
287 /* XXX: Should pull from query. */
288 $q = new ContactQuery;
289 $contact = $q->findOneById($order->getRequesterId());
291 $ret .= " referred by " . $contact->getLink();
292 $area = get_contact_area($contact);
293 if ($area) $ret .= " in " . $area->getLink();
296 $q = new ContactQuery;
297 $contact = $q->findOneById($order->getBeneficiaryId());
299 $ret .= " for " . $contact->getLink();
300 $area = get_contact_area($contact);
301 if ($area) $ret .= " in " . $area->getLink();
304 if ($order->getHubId()) {
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();
315 function get_address_area($address) {
317 return $q->findOneById($address->getAreaId());
320 function get_address_map_link($address) {
321 $postcode = trim($address->getPostcode());
323 # mrt=loc specifies a location search.
324 $map = "maps.google.co.uk/maps?q=" . urlencode($postcode) . "&mrt=loc";
325 $url = "http://$map";
326 # output=embed allows display in an iframe.
327 # iwloc=near hides the popup window for the embedded view.
328 $embed = $GLOBALS['http'] . "://$map&output=embed&iwloc=near";
330 $html .= get_small_link_with_id("map", "Map", $url);
331 $html .= "<script>\n $(function() {\n";
332 $html .= " var x = 0;\n";
333 $html .= " var y = 0;\n";
334 $html .= " var loaded = false;\n";
335 $html .= " $(\"#map\").hover(function(e) {\n";
336 $html .= " x = $(this).outerWidth();\n";
337 $html .= " y = $(this).outerHeight() / 2;\n";
338 $html .= " $(\"#popup\").css(\"left\", e.pageX + x).css(\"top\", e.pageY + y);;\n";
339 $html .= " $(\"#popup\").show();\n";
340 $html .= " if (! loaded) {\n";
341 $html .= " $(\"#popup\").html(\"<iframe width='100%' height='100%' src='$embed'></iframe>\");\n";
342 $html .= " loaded = true;\n";
344 $html .= " },function() {\n";
345 $html .= " $(\"#popup\").hide();\n";
347 $html .= " });</script>";
352 function get_contact_address($contact) {
353 $q = new AddressQuery;
354 return $q->findOneById($contact->getAddressId());
357 function get_contact_area($contact) {
358 $address = get_contact_address($contact);
359 if (! $address) return null;
361 return get_address_area($address);
364 function get_contact_city($contact) {
365 $area = get_contact_area($contact);
366 if (! $area) return null;
368 return get_area_city($area);
371 /* Parcel strings are the same so this can work. */
372 function get_contact_parcel_string($contact) {
373 return get_order_parcel_string($contact);
376 /* Hub and Contact are similar enough that this can work. */
377 function get_hub_address($hub) {
378 return get_contact_address($hub);
381 /* Hub and Contact are similar enough that this can work. */
382 function get_hub_area($hub) {
383 return get_contact_area($hub);
386 /* Hub and Contact are similar enough that this can work. */
387 function get_hub_city($hub) {
388 return get_contact_city($hub);
391 function get_area_contacts($area_id = null, $role = null) {
392 $q = new ContactQuery;
393 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
394 if (isset($role)) $q->where("role & $role");
395 return $q->orderByDisplayname()->find();
398 function get_area_requesters($area_id = null) {
399 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
402 function get_area_beneficiaries($area_id = null) {
403 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
406 function get_area_donors($area_id = null) {
407 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
410 function get_city_contacts($city_id = null, $role = null) {
413 $areas = get_city_areas($city_id);
414 foreach ($areas as $area) $area_ids[] = $area->getId();
415 return get_area_contacts($area_ids, $role);
418 function get_city_requesters($city_id = null) {
419 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
422 function get_city_beneficiaries($city_id = null) {
423 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
426 function get_city_donors($city_id = null) {
427 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
430 function get_city_drivers($city_id = null) {
431 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
434 function get_role_string($object, $roles) {
435 $role = $object->getRole();
439 for ($i =0; $i < count($roles); $i++) {
440 if ($role & (1 << $i)) $selected[] = $roles[$i];
443 return implode(", ", $selected);
446 function get_contact_role_string($contact) {
447 return get_role_string($contact, $GLOBALS['contact_roles']);
450 function get_hub_role_string($hub) {
451 return get_role_string($hub, $GLOBALS['hub_roles']);
454 function show_role_form($role, $roles) {
455 for ($i = 0; $i < count($roles); $i++) {
456 echo " <input type=\"checkbox\" id=\"role_$i\" name=\"role_$i\"";
457 if ($role & (1 << $i)) echo " checked";
458 echo "><label for=\"role_$i\">$roles[$i]</label>\n";
462 function get_area_hubs($area_id = null) {
464 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
465 return $q->orderByDisplayname()->find();
468 function get_city_areas($city_id = null) {
470 $q->join("City")->orderBy("City.Name");
471 if (isset($city_id)) $q->filterByCityId($city_id);
472 return $q->orderByName()->find();
475 function get_city_areas_with_contacts($city_id = null, $role = null) {
477 $q->join("City")->orderBy("City.Name");
478 if (isset($city_id)) $q->filterByCityId($city_id);
480 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
481 else $q->useAddressQuery()->join("Contact")->endUse();
482 return $q->orderByName()->distinct()->find();
485 function get_city_areas_with_hubs($city_id = null) {
487 $q->join("City")->orderBy("City.Name");
488 if (isset($city_id)) $q->filterByCityId($city_id);
489 $q->useAddressQuery()->join("Hub")->endUse();
490 return $q->orderByName()->distinct()->find();
493 function get_city_hubs($city_id = null) {
495 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
496 return $q->orderByDisplayname()->find();
499 function iso8601_to_ymd($iso8601) {
500 return split("-", $iso8601);
503 function ymd_to_iso8601($name) {
504 $y = $_POST[$name . "_y"];
505 if (! $y) return null;
506 $m = $_POST[$name . "_m"];
508 $d = $_POST[$name . "_d"];
510 return sprintf("%04d-%02d-%02d", $y, $m, $d);
513 function show_date_form($name, $date = null) {
514 if (! isset($date)) $date = date('Y-m-d', time());
515 datepicker($name, $date);
518 function validate_postcode($postcode, &$outward = null, &$inward = null) {
520 Valid postcode formats (BS7666):
529 Where N is a number; A is a letter not including Q, V, X;
530 B is a letter not including I, J, Z; C is a letter from the set
531 ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
532 L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
534 The postcode GIR 0AA is also valid.
536 $outward = $inward = null;
538 /* Treat blank as valid for convenience. */
539 $postcode = trim($postcode);
540 if (! $postcode) return true;
542 $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
543 $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
544 $C = '[ABCDEFGHJKSTUW]';
545 $D = '[ABEHMNPRVWXY]';
546 $L = '[ABDEFGHJLNPQRSTUWXYZ]';
548 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;
549 if ($m[1] == "GIR" && $m[2] != "0AA") return false;
550 list($ignored, $outward, $inward) = $m;
554 function format_postcode($postcode, $complain = true) {
555 if (validate_postcode($postcode, $outward, $inward)) {
556 return "$outward $inward";
559 echo "<p>Invalid postcode!</p>\n";
564 function get_small_link_with_id() {
565 /* Args are <id>, <alt text>, <format>, [<stuff> ...] */
566 $args = func_get_args();
567 $id = array_shift($args);
568 if (isset($id)) $id = " id=\"$id\"";
569 $html = htmlspecialchars(array_shift($args));
570 $url = array_shift($args);
571 return vsprintf("<a$id class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
574 function get_small_link() {
575 /* Args are <alt text>, <format>, [<stuff> ...] */
576 $args = func_get_args();
577 array_unshift($args, null);
578 return call_user_func_array("get_small_link_with_id", $args);
581 function small_link() {
582 echo call_user_func_array("get_small_link", func_get_args());
585 include_once("$lib_root/admin.php");
586 include_once("$lib_root/forms.php");