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'])) $first_page = $params['page'];
30 if (array_key_exists('size', $params)) if (is_numeric($params['size'])) $per_page = $params['size'];
32 return array($first_page, $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($first_page, $per_page) = pagination();
49 $pages = ceil($pager->getNbResults() / $per_page);
51 /* Highlight the fact we skipped some pages. */
52 $linked_pages = $pager->getLinks($n);
53 $first_link = $linked_pages[0];
54 $last_link = end($linked_pages);
57 $links[] = page_link('First', 1, $first_page, $pages, $per_page);
58 $links[] = page_link('Previous', $first_page - 1, $first_page, $pages, $per_page);
59 if ($first_link > 1) $links[] = page_link('...', $first_page, $pages, $per_page);
60 foreach ($linked_pages as $link) $links[] = page_link($link, $link, $first_page, $pages, $per_page);
61 if ($last_link < $pages) $links[] = page_link('...', $first_page, $pages, $per_page);
62 $links[] = page_link('Next', $first_page + 1, $first_page, $pages, $per_page);
63 $links[] = page_link('Last', $pages, $first_page, $pages, $per_page);
66 echo implode(' / ', $links);
70 function get_city_by_name($name, $postcode_area = null, $verbose = true) {
73 $m = $q->filterByName(urldecode($name));
74 if (isset($postcode_area)) {
75 $m->filterByPostcodeArea($postcode_area);
79 switch ($m->count()) {
81 if ($verbose) echo "<p>No such city!</p>\n";
88 if ($verbose) echo "<p>Can't identify city uniquely.</p>!\n";
93 function get_city_by_id($id, $verbose = true) {
95 $city = $q->findOneById($id);
98 if ($verbose) echo "<p>No such city!</p>\n";
105 function get_area_by_name($name, $verbose = true) {
107 $areas = $q->filterByName(urldecode($name))->find();
109 switch ($q->count()) {
111 if ($verbose) echo "<p>No such area!</p>\n";
118 if ($verbose) echo "<p>Can't identify area uniquely.</p>!\n";
123 function get_area_by_id($id, $verbose = true) {
125 $area = $q->findOneById($id);
128 if ($verbose) echo "<p>No such area!</p>\n";
135 function get_area_city($area) {
137 return $q->findOneById($area->getCityId());
140 function get_contact_by_name($name, $verbose = true) {
141 $q = new ContactQuery;
142 $contact = $q->filterByDisplayname(urldecode($name))->find();
144 switch ($q->count()) {
146 if ($verbose) echo "<p>No such contact!</p>\n";
153 if ($verbose) echo "<p>Can't identify contact uniquely.</p>!\n";
158 function get_contact_by_id($id, $verbose = true) {
159 $q = new ContactQuery;
160 $contact = $q->findOneById($id);
163 if ($verbose) echo "<p>No such contact!</p>\n";
170 function get_hub_by_name($name, $verbose = true) {
172 $hubs = $q->filterByDisplayname(urldecode($name))->find();
174 switch ($q->count()) {
176 if ($verbose) echo "<p>No such hub!</p>\n";
183 if ($verbose) echo "<p>Can't identify hub uniquely.</p>!\n";
188 function get_hub_by_id($id, $verbose = true) {
190 $hub = $q->findOneById($id);
193 if ($verbose) echo "<p>No such hub!</p>\n";
200 function get_donation_by_id($id, $verbose = true) {
201 $q = new DonationQuery;
202 $donation = $q->findOneById($id);
205 if ($verbose) echo "<p>No such donation!</p>\n";
212 function get_order_by_id($id, $verbose = true) {
214 $order = $q->findOneById($id);
217 if ($verbose) echo "<p>No such order!</p>\n";
224 function get_order_ids_by_state($state_mask) {
225 $order_ids = array();
226 $dbh = Propel::getConnection();
227 $sth = $dbh->prepare("select * from OrderState o where updated=(select max(updated) from OrderState where order_id=o.order_id) and state & $state_mask");
229 $order_states = OrderStatePeer::populateObjects($sth);
230 foreach ($order_states as $order_state) $order_ids[] = $order_state->getOrderId();
234 function get_beneficiary_orders($contact, $state_mask = null) {
236 $q->filterByBeneficiaryId($contact->getId());
237 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
238 return $q->orderByDate()->find();
241 function get_requester_orders($contact, $state_mask = null) {
243 $q->filterByRequesterId($contact->getId());
244 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
245 return $q->orderByDate()->find();
248 function get_contact_orders($contact, $state_mask = null) {
250 $q->filterByBeneficiaryId($contact->getId())->_or()->filterByRequesterId($contact->getId());
251 if ($state_mask) $q->filterById(get_order_ids_by_state($state_mask));
252 return $q->orderByDate()->find();
255 function get_user_by_contact_id($id, $verbose = true) {
257 $user = $q->findOneByContactId($id);
260 if ($verbose) echo "<p>No such user!</p>\n";
267 function get_city_displayname($city) {
268 return $city->getName() . ", " . $city->getPostcodeArea();
271 function get_area_displayname($area) {
272 return $area->getName() . " in " . get_city_displayname(CityQuery::create()->findOneById($area->getCityId()));
275 function get_donation_displayname($donation) {
276 return sprintf("%0.2fkg on %s", $donation->getQuantity() / 1000, $donation->getDate());
279 function get_order_parcel_string($order) {
280 global $parcel_sizes, $parcel_contents;
283 for ($i = 0 ; $i < count($parcel_sizes); $i++) {
284 if ($order->getParcel() & (1 << $i)) {
285 $parcel_size = $parcel_sizes[$i];
291 for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
292 if ($order->getParcel() & (1 << $i)) $selected[] = $parcel_contents[$i];
295 $ret = implode(": ", array($parcel_size, implode(", ", $selected)));
296 $ret = preg_replace('/^: /', '', $ret);
297 $ret = preg_replace('/: $/', '', $ret);
302 function get_order_displayname($order) {
303 return sprintf("<span class=\"small\">%s</span> on %s", get_order_parcel_string($order), $order->getDate());
306 function get_order_state_string($order_state = null) {
309 if (is_null($order_state)) return null;
311 for ($i = 0; $i < count($states); $i++) {
312 if ($order_state->getState() & (1 << $i)) {
320 function get_order_state($order) {
321 $q = new OrderStateQuery();
322 return $q->filterByOrderId($order->getId())->orderByUpdated('desc')->findOne();
325 function get_order_summary($order) {
327 $order_state = get_order_state($order);
328 if ($order_state) $ret = "<strong>" . ucfirst(get_order_state_string($order_state)) . "</strong> order ";
329 $ret .= $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
331 if (check_admin(1)) $ret .= " " . $order->getDeleteLink();
333 /* XXX: Should pull from query. */
334 $q = new ContactQuery;
335 $contact = $q->findOneById($order->getRequesterId());
337 $ret .= " referred by " . $contact->getLink();
338 $area = get_contact_area($contact);
339 if ($area) $ret .= " in " . $area->getLink();
342 $q = new ContactQuery;
343 $contact = $q->findOneById($order->getBeneficiaryId());
345 $ret .= " for " . $contact->getLink();
346 $area = get_contact_area($contact);
347 if ($area) $ret .= " in " . $area->getLink();
350 if ($order->getHubId()) {
352 $hub = $q->findOneById($order->getHubId());
353 if ($hub) $ret .= " to hub " . $hub->getLink();
354 $area = get_hub_area($hub);
355 if ($area) $ret .= " in " . $area->getLink();
361 function get_address_area($address) {
363 return $q->findOneById($address->getAreaId());
366 function get_address_map_link($address) {
367 $postcode = trim($address->getPostcode());
369 # mrt=loc specifies a location search.
370 $map = "maps.google.co.uk/maps?q=" . urlencode($postcode) . "&mrt=loc";
371 $url = "http://$map";
372 # output=embed allows display in an iframe.
373 # iwloc=near hides the popup window for the embedded view.
374 $embed = $GLOBALS['http'] . "://$map&output=embed&iwloc=near";
376 $html .= get_small_link_with_id("map", "Map", $url);
377 $html .= "<script>\n $(function() {\n";
378 $html .= " var x = 0;\n";
379 $html .= " var y = 0;\n";
380 $html .= " var loaded = false;\n";
381 $html .= " $(\"#map\").hover(function(e) {\n";
382 $html .= " x = $(this).outerWidth();\n";
383 $html .= " y = $(this).outerHeight() / 2;\n";
384 $html .= " $(\"#popup\").css(\"left\", e.pageX + x).css(\"top\", e.pageY + y);;\n";
385 $html .= " $(\"#popup\").show();\n";
386 $html .= " if (! loaded) {\n";
387 $html .= " $(\"#popup\").html(\"<iframe width='100%' height='100%' src='$embed'></iframe>\");\n";
388 $html .= " loaded = true;\n";
390 $html .= " },function() {\n";
391 $html .= " $(\"#popup\").hide();\n";
393 $html .= " });</script>";
398 function get_contact_address($contact) {
399 $q = new AddressQuery;
400 return $q->findOneById($contact->getAddressId());
403 function get_contact_area($contact) {
404 $address = get_contact_address($contact);
405 if (! $address) return null;
407 return get_address_area($address);
410 function get_contact_city($contact) {
411 $area = get_contact_area($contact);
412 if (! $area) return null;
414 return get_area_city($area);
417 /* Parcel strings are the same so this can work. */
418 function get_contact_parcel_string($contact) {
419 return get_order_parcel_string($contact);
422 /* Hub and Contact are similar enough that this can work. */
423 function get_hub_address($hub) {
424 return get_contact_address($hub);
427 /* Hub and Contact are similar enough that this can work. */
428 function get_hub_area($hub) {
429 return get_contact_area($hub);
432 /* Hub and Contact are similar enough that this can work. */
433 function get_hub_city($hub) {
434 return get_contact_city($hub);
437 function get_area_contacts($area_id = null, $role = null) {
438 $q = new ContactQuery;
439 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
440 if (isset($role)) $q->where("role & $role");
441 return $q->orderByDisplayname()->find();
444 function get_area_requesters($area_id = null) {
445 return get_area_contacts($area_id, $GLOBALS['ROLE_REQUESTER']);
448 function get_area_beneficiaries($area_id = null) {
449 return get_area_contacts($area_id, $GLOBALS['ROLE_BENEFICIARY']);
452 function get_area_donors($area_id = null) {
453 return get_area_contacts($area_id, $GLOBALS['ROLE_DONOR']);
456 function get_city_contacts($city_id = null, $role = null) {
459 $areas = get_city_areas($city_id);
460 foreach ($areas as $area) $area_ids[] = $area->getId();
461 return get_area_contacts($area_ids, $role);
464 function get_city_requesters($city_id = null) {
465 return get_city_contacts($city_id, $GLOBALS['ROLE_REQUESTER']);
468 function get_city_beneficiaries($city_id = null) {
469 return get_city_contacts($city_id, $GLOBALS['ROLE_BENEFICIARY']);
472 function get_city_donors($city_id = null) {
473 return get_city_contacts($city_id, $GLOBALS['ROLE_DONOR']);
476 function get_city_drivers($city_id = null) {
477 return get_city_contacts($city_id, $GLOBALS['ROLE_DRIVER']);
480 function get_role_string($object, $roles) {
481 $role = $object->getRole();
485 for ($i =0; $i < count($roles); $i++) {
486 if ($role & (1 << $i)) $selected[] = $roles[$i];
489 return implode(", ", $selected);
492 function get_contact_role_string($contact) {
493 return get_role_string($contact, $GLOBALS['contact_roles']);
496 function get_hub_role_string($hub) {
497 return get_role_string($hub, $GLOBALS['hub_roles']);
500 function show_role_form($role, $roles) {
501 for ($i = 0; $i < count($roles); $i++) {
502 echo " <input type=\"checkbox\" id=\"role_$i\" name=\"role_$i\"";
503 if ($role & (1 << $i)) echo " checked";
504 echo "><label for=\"role_$i\">$roles[$i]</label>\n";
508 function get_area_hubs($area_id = null) {
510 if (isset($area_id)) $q->useAddressQuery()->filterByAreaId($area_id)->endUse();
511 return $q->orderByDisplayname()->find();
514 function get_city_areas($city_id = null) {
516 $q->join("City")->orderBy("City.Name");
517 if (isset($city_id)) $q->filterByCityId($city_id);
518 return $q->orderByName()->find();
521 function get_city_areas_with_contacts($city_id = null, $role = null) {
523 $q->join("City")->orderBy("City.Name");
524 if (isset($city_id)) $q->filterByCityId($city_id);
526 if (isset($role)) $q->useAddressQuery()->join("Contact")->useContactQuery()->where("role & $role")->endUse()->endUse();
527 else $q->useAddressQuery()->join("Contact")->endUse();
528 return $q->orderByName()->distinct()->find();
531 function get_city_areas_with_hubs($city_id = null) {
533 $q->join("City")->orderBy("City.Name");
534 if (isset($city_id)) $q->filterByCityId($city_id);
535 $q->useAddressQuery()->join("Hub")->endUse();
536 return $q->orderByName()->distinct()->find();
539 function get_city_hubs($city_id = null) {
541 if (isset($city_id)) $q->useAddressQuery()->useAreaQuery()->filterByCityId($city_id)->endUse()->endUse();
542 return $q->orderByDisplayname()->find();
545 function iso8601_to_ymd($iso8601) {
546 return split("-", $iso8601);
549 function ymd_to_iso8601($name) {
550 $y = $_POST[$name . "_y"];
551 if (! $y) return null;
552 $m = $_POST[$name . "_m"];
554 $d = $_POST[$name . "_d"];
556 return sprintf("%04d-%02d-%02d", $y, $m, $d);
559 function show_date_form($name, $date = null) {
560 if (! isset($date)) $date = date('Y-m-d', time());
561 datepicker($name, $date);
564 function validate_postcode($postcode, &$outward = null, &$inward = null) {
566 Valid postcode formats (BS7666):
575 Where N is a number; A is a letter not including Q, V, X;
576 B is a letter not including I, J, Z; C is a letter from the set
577 ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
578 L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
580 The postcode GIR 0AA is also valid.
582 $outward = $inward = null;
584 /* Treat blank as valid for convenience. */
585 $postcode = trim($postcode);
586 if (! $postcode) return true;
588 $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
589 $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
590 $C = '[ABCDEFGHJKSTUW]';
591 $D = '[ABEHMNPRVWXY]';
592 $L = '[ABDEFGHJLNPQRSTUWXYZ]';
594 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;
595 if ($m[1] == "GIR" && $m[2] != "0AA") return false;
596 list($ignored, $outward, $inward) = $m;
600 function format_postcode($postcode, $complain = true) {
601 if (validate_postcode($postcode, $outward, $inward)) {
602 return "$outward $inward";
605 echo "<p>Invalid postcode!</p>\n";
610 function get_small_link_with_id() {
611 /* Args are <id>, <alt text>, <format>, [<stuff> ...] */
612 $args = func_get_args();
613 $id = array_shift($args);
614 if (isset($id)) $id = " id=\"$id\"";
615 $html = htmlspecialchars(array_shift($args));
616 $url = array_shift($args);
617 return vsprintf("<a$id class=\"small noprint\" href=\"$url\">$html</a>\n", $args);
620 function get_small_link() {
621 /* Args are <alt text>, <format>, [<stuff> ...] */
622 $args = func_get_args();
623 array_unshift($args, null);
624 return call_user_func_array("get_small_link_with_id", $args);
627 function small_link() {
628 echo call_user_func_array("get_small_link", func_get_args());
631 include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "admin.php")));
632 include_once(join(DIRECTORY_SEPARATOR, array($lib_root, "forms.php")));