X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=lib%2Ffunctions.php;h=0f2e0e782662a9881b523a5763382524d697c45a;hb=446bce55325fc63d0583b838111d387764be498f;hp=7a02b2af054808cd98dd7b2343ab35354c7c5c49;hpb=a646a2cfbd63d70043e60d88afff0add11ad7e81;p=readifood.git diff --git a/lib/functions.php b/lib/functions.php index 7a02b2a..0f2e0e7 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -317,6 +317,13 @@ return $q->findOneById($address->getAreaId()); } + function get_address_map_link($address) { + $postcode = trim($address->getPostcode()); + if ($postcode) { + return " " . get_small_link("Map", "http://maps.google.co.uk/maps?q=" . urlencode($postcode)); + } + } + function get_contact_address($contact) { $q = new AddressQuery; return $q->findOneById($contact->getAddressId()); @@ -507,6 +514,52 @@ echo "Day: "; } + function validate_postcode($postcode, &$outward = null, &$inward = null) { + /* + Valid postcode formats (BS766): + + AN NLL + ABN NLL + ANN NLL + ABNN NLL + ABND NLL + ANC NLL + + Where N is a number; A is a letter not including Q, V, X; + B is a letter not including I, J, Z; C is a letter from the set + ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY; + L is a letter from the set ABDEFGHJLNPQRSTUWXYZ. + + The postcode GIR 0AA is also valid. + */ + $outward = $inward = null; + + /* Treat blank as valid for convenience. */ + $postcode = trim($postcode); + if (! $postcode) return true; + + $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]'; + $B = '[ABCDEFGHKLMNOPQRSTUVWXY]'; + $C = '[ABCDEFGHJKSTUW]'; + $D = '[ABEHMNPRVWXY]'; + $L = '[ABDEFGHJLNPQRSTUWXYZ]'; + $N = '\d'; + 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; + if ($m[1] == "GIR" && $m[2] != "0AA") return false; + list($ignored, $outward, $inward) = $m; + return true; + } + + function format_postcode($postcode, $complain = true) { + if (validate_postcode($postcode, $outward, $inward)) { + return "$outward $inward"; + } + if ($complain) { + echo "

Invalid postcode!

\n"; + return null; + } + } + function get_small_link() { /* Args are , , [ ...] */ $args = func_get_args();