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 pagination() {
26 $per_page = $GLOBALS['default_page_size'];
28 parse_str($_SERVER['QUERY_STRING'], $params);
29 if (array_key_exists('page', $params)) if (is_numeric($params['page'])) $offset = $params['page'] - 1;
30 if (array_key_exists('size', $params)) if (is_numeric($params['size'])) $per_page = $params['size'];
32 return array($offset, $per_page);
35 function page_link($alt, $n, $cur, $max, $size) {
37 if ($n < 1 || $n == $cur || $n > $max) return $alt;
38 $params = array('page' => $n);
39 if ($size != $GLOBALS['default_page_size']) $params['size'] = $size;
40 $url = http_build_query($params);
41 return "<a href=\"?$url\">$alt</a> ";
44 function show_pagination($pager, $n = 5) {
45 if (! $pager->haveToPaginate()) return;
47 list($offset, $per_page) = pagination();
52 $pages = ceil($pager->getNbResults() / $per_page);
55 $links[] = page_link('First', 1, $offset, $pages, $per_page);
56 $links[] = page_link('Previous', $offset - 1, $offset, $pages, $per_page);
57 foreach ($pager->getLinks($n) as $link) $links[] = page_link($link, $link, $offset, $pages, $per_page);
58 $links[] = page_link('Next', $offset + 1, $offset, $pages, $per_page);
59 $links[] = page_link('Last', $pages, $offset, $pages, $per_page);
62 echo implode(' / ', $links);
66 function get_city_by_name($name, $postcode_area = null, $verbose = true) {
69 $m = $q->filterByName(urldecode($name));
70 if (isset($postcode_area)) {
71 $m->filterByPostcodeArea($postcode_area);
75 switch ($m->count()) {
77 if ($verbose) echo "<p>No such city!</p>\n";
84 if ($verbose) echo "<p>Can't identify city uniquely.</p>!\n";
89 function get_city_by_id($id, $verbose = true) {
91 $city = $q->findOneById($id);
94 if ($verbose) echo "<p>No such city!</p>\n";
101 function get_area_by_name($name, $verbose = true) {
103 $areas = $q->filterByName(urldecode($name))->find();
105 switch ($q->count()) {
107 if ($verbose) echo "<p>No such area!</p>\n";
114 if ($verbose) echo "<p>Can't identify area uniquely.</p>!\n";
119 function get_area_by_id($id, $verbose = true) {
121 $area = $q->findOneById($id);
124 if ($verbose) echo "<p>No such area!</p>\n";
131 function get_area_city($area) {
133 return $q->findOneById($area->getCityId());
136 function get_contact_by_name($name, $verbose = true) {
137 $q = new ContactQuery;
138 $contact = $q->filterByDisplayname(urldecode($name))->find();
140 switch ($q->count()) {
142 if ($verbose) echo "<p>No such contact!</p>\n";
149 if ($verbose) echo "<p>Can't identify contact uniquely.</p>!\n";
154 function get_contact_by_id($id, $verbose = true) {
155 $q = new ContactQuery;
156 $contact = $q->findOneById($id);
159 if ($verbose) echo "<p>No such contact!</p>\n";
166 function get_hub_by_name($name, $verbose = true) {
168 $hubs = $q->filterByDisplayname(urldecode($name))->find();
170 switch ($q->count()) {
172 if ($verbose) echo "<p>No such hub!</p>\n";
179 if ($verbose) echo "<p>Can't identify hub uniquely.</p>!\n";
184 function get_hub_by_id($id, $verbose = true) {
186 $hub = $q->findOneById($id);
189 if ($verbose) echo "<p>No such hub!</p>\n";
196 function get_donation_by_id($id, $verbose = true) {
197 $q = new DonationQuery;
198 $donation = $q->findOneById($id);
201 if ($verbose) echo "<p>No such donation!</p>\n";
208 function get_order_by_id($id, $verbose = true) {
210 $order = $q->findOneById($id);
213 if ($verbose) echo "<p>No such order!</p>\n";
220 function get_order_ids_by_state($state_mask) {
221 $order_ids = array();
222 $dbh = Propel::getConnection();
223 $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and state & $state_mask");
225 $order_states = OrderStatePeer::populateObjects($sth);
226 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
230 function get_beneficiary_orders($contact, $state_mask = null) {
232 $q->filterByBeneficiaryId($contact->getId());
233 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
234 return $q->orderByDate()->find();
237 function get_requester_orders($contact, $state_mask = null) {
239 $q->filterByRequesterId($contact->getId());
240 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
241 return $q->orderByDate()->find();
244 function get_contact_orders($contact, $state_mask = null) {
246 $q->filterByBeneficiaryId($contact->getId())->_or()->filterByRequesterId($contact->getId());
247 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
248 return $q->orderByDate()->find();
251 function get_user_by_contact_id($id, $verbose = true) {
253 $user = $q->findOneByContactId($id);
256 if ($verbose) echo "<p>No such user!</p>\n";
263 function get_city_displayname($city) {
264 return $city->getName() . ", " . $city->getPostcodeArea();
267 function get_area_displayname($area) {
268 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
271 function get_donation_displayname($donation) {
272 return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
275 function get_order_parcel_string($order) {
276 global $parcel_sizes, $parcel_contents;
279 for ($i = 0 ; $i < count($parcel_sizes); $i++) {
280 if ($order->getParcel() & (1 << $i)) {
281 $parcel_size = $parcel_sizes[$i];
287 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
288 if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
291 $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
292 $ret = preg_replace('/^: /', '', $ret);
293 $ret = preg_replace('/: $/', '', $ret);
298 function get_order_displayname($order) {
299 return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
302 function get_order_state_string($order_state = null) {
305 if (is_null($order_state)) return null;
307 for ($i = 0; $i < count($states); $i++) {
308 if ($order_state->getState() & (1 << $i)) {
316 function get_order_state($order) {
317 $q = new OrderStateQuery();
318 return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
321 function get_order_summary($order) {
323 $order_state = get_order_state($order);
324 if ($order_state) $ret = "<strong>" . ucfirst(get_order_state_string($order_state)) . "</strong> order ";
325 $ret .= $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
327 if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
329 /* XXX: Should pull from query. */
330 $q = new ContactQuery;
331 $contact = $q->findOneById($order->getRequesterId());
333 $ret .= " referred by " . $contact->getLink();
334 $area = get_contact_area($contact);
335 if ($area) $ret .= " in " . $area->getLink();
338 $q = new ContactQuery;
339 $contact = $q->findOneById($order->getBeneficiaryId());
341 $ret .= " for " . $contact->getLink();
342 $area = get_contact_area($contact);
343 if ($area) $ret .= " in " . $area->getLink();
346 if ($order->getHubId()) {
348 $hub = $q->findOneById($order->getHubId());
349 if ($hub) $ret .= " to hub " . $hub->getLink();
350 $area = get_hub_area($hub);
351 if ($area) $ret .= " in " . $area->getLink();
357 function get_address_area($address) {
359 return $q->findOneById($address->getAreaId());
362 function get_address_map_link($address) {
363 $postcode = trim($address->getPostcode());
365 # mrt=loc specifies a location search.
366 $map = "maps.google.co.uk/maps?q=" . urlencode($postcode) . "&mrt=loc";
367 $url = "http://$map";
368 # output=embed allows display in an iframe.
369 # iwloc=near hides the popup window for the embedded view.
370 $embed = $GLOBALS['http'] . "://$map&output=embed&iwloc=near";
372 $html .= get_small_link_with_id("map", "Map", $url);
373 $html .= "<script>\n $(function() {\n";
374 $html .= " var x = 0;\n";
375 $html .= " var y = 0;\n";
376 $html .= " var loaded = false;\n";
377 $html .= " $(\"#map\").hover(function(e) {\n";
378 $html .= " x = $(this).outerWidth();\n";
379 $html .= " y = $(this).outerHeight() / 2;\n";
380 $html .= " $(\"#popup\").css(\"left\", e.pageX + x).css(\"top\", e.pageY + y);;\n";
381 $html .= " $(\"#popup\").show();\n";
382 $html .= " if (! loaded) {\n";
383 $html .= " $(\"#popup\").html(\"<iframe width='100%' height='100%' src='$embed'></iframe>\");\n";
384 $html .= " loaded = true;\n";
386 $html .= " },function() {\n";
387 $html .= " $(\"#popup\").hide();\n";
389 $html .= " });</script>";
394 function get_contact_address($contact) {
395 $q = new AddressQuery;
396 return $q->findOneById($contact->getAddressId());
399 function get_contact_area($contact) {
400 $address = get_contact_address($contact);
401 if (! $address) return null;
403 return get_address_area($address);
406 function get_contact_city($contact) {
407 $area = get_contact_area($contact);
408 if (! $area) return null;
410 return get_area_city($area);
413 /* Parcel strings are the same so this can work. */
414 function get_contact_parcel_string($contact) {
415 return get_order_parcel_string($contact);
418 /* Hub and Contact are similar enough that this can work. */
419 function get_hub_address($hub) {
420 return get_contact_address($hub);
423 /* Hub and Contact are similar enough that this can work. */
424 function get_hub_area($hub) {
425 return get_contact_area($hub);
428 /* Hub and Contact are similar enough that this can work. */
429 function get_hub_city($hub) {
430 return get_contact_city($hub);
433 function get_area_contacts($area_id = null, $role = null) {
434 $q = new ContactQuery;
435 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
436 if (isset($role)) $q->where("role & $role");
437 return $q->orderByDisplayname()->find();
440 function get_area_requesters($area_id = null) {
441 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
444 function get_area_beneficiaries($area_id = null) {
445 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
448 function get_area_donors($area_id = null) {
449 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
452 function get_city_contacts($city_id = null, $role = null) {
455 $areas = get_city_areas($city_id);
456 foreach ($areas as $area) $area_ids[] = $area->getId();
457 return get_area_contacts($area_ids, $role);
460 function get_city_requesters($city_id = null) {
461 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
464 function get_city_beneficiaries($city_id = null) {
465 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
468 function get_city_donors($city_id = null) {
469 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
472 function get_city_drivers($city_id = null) {
473 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
476 function get_role_string($object, $roles) {
477 $role = $object->getRole();
481 for ($i =0; $i < count($roles); $i++) {
482 if ($role & (1 << $i)) $selected[] = $roles[$i];
485 return implode(", ", $selected);
488 function get_contact_role_string($contact) {
489 return get_role_string($contact, $GLOBALS['contact_roles']);
492 function get_hub_role_string($hub) {
493 return get_role_string($hub, $GLOBALS['hub_roles']);
496 function show_role_form($role, $roles) {
497 for ($i = 0; $i < count($roles); $i++) {
498 echo " <input type=\"checkbox\" id=\"role_$i\" name=\"role_$i\"";
499 if ($role & (1 << $i)) echo " checked";
500 echo "><label for=\"role_$i\">$roles[$i]</label>\n";
504 function get_area_hubs($area_id = null) {
506 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
507 return $q->orderByDisplayname()->find();
510 function get_city_areas($city_id = null) {
512 $q->join("City")->orderBy("City.Name");
513 if (isset($city_id)) $q->filterByCityId($city_id);
514 return $q->orderByName()->find();
517 function get_city_areas_with_contacts($city_id = null, $role = null) {
519 $q->join("City")->orderBy("City.Name");
520 if (isset($city_id)) $q->filterByCityId($city_id);
522 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
523 else $q->useAddressQuery()->join("Contact")->endUse();
524 return $q->orderByName()->distinct()->find();
527 function get_city_areas_with_hubs($city_id = null) {
529 $q->join("City")->orderBy("City.Name");
530 if (isset($city_id)) $q->filterByCityId($city_id);
531 $q->useAddressQuery()->join("Hub")->endUse();
532 return $q->orderByName()->distinct()->find();
535 function get_city_hubs($city_id = null) {
537 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
538 return $q->orderByDisplayname()->find();
541 function iso8601_to_ymd($iso8601) {
542 return split("-", $iso8601);
545 function ymd_to_iso8601($name) {
546 $y = $_POST[$name . "_y"];
547 if (! $y) return null;
548 $m = $_POST[$name . "_m"];
550 $d = $_POST[$name . "_d"];
552 return sprintf("%04d-%02d-%02d", $y, $m, $d);
555 function show_date_form($name, $date = null) {
556 if (! isset($date)) $date = date('Y-m-d', time());
557 datepicker($name, $date);
560 function validate_postcode($postcode, &$outward = null, &$inward = null) {
562 Valid postcode formats (BS7666):
571 Where N is a number; A is a letter not including Q, V, X;
572 B is a letter not including I, J, Z; C is a letter from the set
573 ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
574 L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
576 The postcode GIR 0AA is also valid.
578 $outward = $inward = null;
580 /* Treat blank as valid for convenience. */
581 $postcode = trim($postcode);
582 if (! $postcode) return true;
584 $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
585 $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
586 $C = '[ABCDEFGHJKSTUW]';
587 $D = '[ABEHMNPRVWXY]';
588 $L = '[ABDEFGHJLNPQRSTUWXYZ]';
590 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;
591 if ($m[1] == "GIR" && $m[2] != "0AA") return false;
592 list($ignored, $outward, $inward) = $m;
596 function format_postcode($postcode, $complain = true) {
597 if (validate_postcode($postcode, $outward, $inward)) {
598 return "$outward $inward";
601 echo "<p>Invalid postcode!</p>\n";
606 function get_small_link_with_id() {
607 /* Args are <id>, <alt text>, <format>, [<stuff> ...] */
608 $args = func_get_args();
609 $id = array_shift($args);
610 if (isset($id)) $id = " id=\"$id\"";
611 $html = htmlspecialchars(array_shift($args));
612 $url = array_shift($args);
613 return vsprintf("<a$id class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
616 function get_small_link() {
617 /* Args are <alt text>, <format>, [<stuff> ...] */
618 $args = func_get_args();
619 array_unshift($args, null);
620 return call_user_func_array("get_small_link_with_id", $args);
623 function small_link() {
624 echo call_user_func_array("get_small_link", func_get_args());
627 include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "admin.php")));
628 include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "forms.php")));