aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--perllib/Cobrand.pm42
-rw-r--r--perllib/Page.pm21
-rwxr-xr-xt/Cobrand.t38
-rw-r--r--t/Cobrands/Mysite/Util.pm8
-rwxr-xr-xt/Page.t21
-rwxr-xr-xweb/alert.cgi4
-rwxr-xr-xweb/index.cgi11
-rwxr-xr-xweb/rss.cgi4
8 files changed, 121 insertions, 28 deletions
diff --git a/perllib/Cobrand.pm b/perllib/Cobrand.pm
index 3151d82ec..2441aff73 100644
--- a/perllib/Cobrand.pm
+++ b/perllib/Cobrand.pm
@@ -7,7 +7,7 @@
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: Cobrand.pm,v 1.15 2009-09-15 14:01:14 louise Exp $
+# $Id: Cobrand.pm,v 1.16 2009-09-15 17:42:43 louise Exp $
package Cobrand;
use strict;
@@ -212,6 +212,46 @@ sub enter_postcode_text {
}
}
+=item disambiguate_location COBRAND S Q
+
+Given a string representing a location, return a string that includes any disambiguating
+information available
+
+=cut
+
+sub disambiguate_location {
+ my ($cobrand, $s, $q) = @_;
+ my $handle;
+ if ($cobrand){
+ $handle = cobrand_handle($cobrand);
+ }
+ if ( !$cobrand || !$handle || !$handle->can('disambiguate_location')){
+ return $s;
+ } else{
+ return $handle->disambiguate_location($s, $q);
+ }
+}
+
+=item form_elements FORM_NAME Q
+
+Return HTML for any extra needed elements for FORM_NAME
+
+=cut
+
+sub form_elements {
+ my ($cobrand, $form_name, $q) = @_;
+ my $handle;
+ if ($cobrand){
+ $handle = cobrand_handle($cobrand);
+ }
+ if ( !$cobrand || !$handle || !$handle->can('form_elements')){
+ return '';
+ } else{
+ return $handle->form_elements($form_name, $q);
+ }
+
+}
+
1;
diff --git a/perllib/Page.pm b/perllib/Page.pm
index 21771eab3..1bb94bc58 100644
--- a/perllib/Page.pm
+++ b/perllib/Page.pm
@@ -6,7 +6,7 @@
# Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: Page.pm,v 1.177 2009-09-15 13:57:01 louise Exp $
+# $Id: Page.pm,v 1.178 2009-09-15 17:42:43 louise Exp $
#
package Page;
@@ -790,13 +790,14 @@ sub display_problem_updates {
return $out;
}
-# geocode STRING
+# geocode STRING QUERY
# Given a user-inputted string, try and convert it into co-ordinates using either
# MaPit if it's a postcode, or Google Maps API otherwise. Returns an array of
# data, including an error if there is one (which includes a location being in
-# Northern Ireland).
+# Northern Ireland). The information in the query may be used by cobranded versions
+# of the site to diambiguate locations.
sub geocode {
- my ($s) = @_;
+ my ($s, $q) = @_;
my ($x, $y, $easting, $northing, $error);
if (mySociety::PostcodeUtil::is_valid_postcode($s)) {
try {
@@ -821,17 +822,20 @@ sub geocode {
}
}
} else {
- ($x, $y, $easting, $northing, $error) = geocode_string($s);
+ ($x, $y, $easting, $northing, $error) = geocode_string($s, $q);
}
return ($x, $y, $easting, $northing, $error);
}
-# geocode_string STRING
+# geocode_string STRING QUERY
# Canonicalises, looks up on Google Maps API, and caches, a user-inputted location.
# Returns array of (TILE_X, TILE_Y, EASTING, NORTHING, ERROR), where ERROR is
-# either undef, a string, or an array of matches if there are more than one.
+# either undef, a string, or an array of matches if there are more than one. The
+# information in the query may be used to disambiguate the location in cobranded versions
+# of the site.
sub geocode_string {
- my $s = shift;
+ my ($s, $q) = @_;
+ $s = Cobrand::disambiguate_location(get_cobrand($q), $s, $q);
$s = lc($s);
$s =~ s/[^-&0-9a-z ']/ /g;
$s =~ s/\s+/ /g;
@@ -849,7 +853,6 @@ sub geocode_string {
$js = LWP::Simple::get($url);
File::Slurp::write_file($cache_file, $js) if $js && $js !~ /"code":6[12]0/;
}
-
if (!$js) {
$error = _('Sorry, we could not parse that location. Please try again.');
} elsif ($js !~ /"code" *: *200/) {
diff --git a/t/Cobrand.t b/t/Cobrand.t
index 13e9ef6b4..fe6d46f51 100755
--- a/t/Cobrand.t
+++ b/t/Cobrand.t
@@ -6,12 +6,12 @@
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: Cobrand.t,v 1.6 2009-09-10 08:54:33 louise Exp $
+# $Id: Cobrand.t,v 1.7 2009-09-15 17:42:43 louise Exp $
#
use strict;
use warnings;
-use Test::More tests => 14;
+use Test::More tests => 20;
use Test::Exception;
use FindBin;
@@ -22,7 +22,7 @@ use lib "$FindBin::Bin/../../perllib";
use Cobrand;
use MockQuery;
-sub test_site_restriction{
+sub test_site_restriction {
my $q = new MockQuery('mysite');
my ($site_restriction, $site_id) = Cobrand::set_site_restriction($q);
like($site_restriction, qr/ and council = 1 /, 'should return result of cobrand module site_restriction function');
@@ -34,7 +34,30 @@ sub test_site_restriction{
ok($site_id == 0, 'should return "" and zero if no module exists');
}
-sub test_cobrand_handle{
+sub test_form_elements {
+ my $q = new MockQuery('mysite');
+ my $element_html = Cobrand::form_elements('mysite', 'postcodeForm', $q);
+ ok($element_html eq 'Extra html', 'should return result of cobrand module element_html function') or diag("Got $element_html");
+
+ $element_html = Cobrand::form_elements('nosite', 'postcodeForm', $q);
+ ok($element_html eq '', 'should return an empty string if no cobrand module exists') or diag("Got $element_html");
+}
+
+sub test_disambiguate_location {
+ my $q = new MockQuery('mysite');
+ my $s = 'London Road';
+ $s = Cobrand::disambiguate_location('mysite', $s, $q);
+ ok($s eq 'Specific Location', 'should return result of cobrand module disambiguate_location function') or diag("Got $s");;
+
+ $q = new MockQuery('nosite');
+ $s = 'London Road';
+ $s = Cobrand::disambiguate_location('nosite', $s, $q);
+ ok($s eq 'London Road', 'should return location string as passed if no cobrand module exists') or diag("Got $s");
+
+}
+
+
+sub test_cobrand_handle {
my $cobrand = 'mysite';
my $handle = Cobrand::cobrand_handle($cobrand);
like($handle->site_name(), qr/mysite/, 'should get a module handle if Util module exists for cobrand');
@@ -44,7 +67,7 @@ sub test_cobrand_handle{
}
-sub test_cobrand_page{
+sub test_cobrand_page {
my $q = new MockQuery('mysite');
# should get the result of the page function in the cobrand module if one exists
my ($html, $params) = Cobrand::cobrand_page($q);
@@ -58,7 +81,7 @@ sub test_cobrand_page{
}
-sub test_base_url{
+sub test_base_url {
my $cobrand = 'mysite';
# should get the result of the page function in the cobrand module if one exists
@@ -76,4 +99,5 @@ ok(test_cobrand_handle() == 1, 'Ran all tests for the cobrand_handle function');
ok(test_cobrand_page() == 1, 'Ran all tests for the cobrand_page function');
ok(test_site_restriction() == 1, 'Ran all tests for the site_restriction function');
ok(test_base_url() == 1, 'Ran all tests for the base url');
-
+ok(test_disambiguate_location() == 1, 'Ran all tests for disambiguate location');
+ok(test_form_elements() == 1, 'Ran all tests for form_elements');
diff --git a/t/Cobrands/Mysite/Util.pm b/t/Cobrands/Mysite/Util.pm
index 59adb845d..6e7098b43 100644
--- a/t/Cobrands/Mysite/Util.pm
+++ b/t/Cobrands/Mysite/Util.pm
@@ -7,7 +7,7 @@
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: Util.pm,v 1.5 2009-09-14 16:09:40 louise Exp $
+# $Id: Util.pm,v 1.6 2009-09-15 17:42:43 louise Exp $
package Cobrands::Mysite::Util;
use Page;
@@ -37,5 +37,11 @@ sub base_url {
return 'http://mysite.example.com';
}
+sub disambiguate_location {
+ return 'Specific Location';
+}
+sub form_elements {
+ return "Extra html";
+}
1;
diff --git a/t/Page.t b/t/Page.t
index 4143908a2..ea68e7419 100755
--- a/t/Page.t
+++ b/t/Page.t
@@ -6,7 +6,7 @@
# Copyright (c) 2009 UK Citizens Online Democracy. All rights reserved.
# Email: louise@mysociety.org; WWW: http://www.mysociety.org/
#
-# $Id: Page.t,v 1.3 2009-09-10 08:54:33 louise Exp $
+# $Id: Page.t,v 1.4 2009-09-15 17:42:43 louise Exp $
#
use strict;
@@ -35,7 +35,23 @@ sub set_lang($) {
mySociety::Locale::change();
}
-sub test_header(){
+sub test_geocode_string() {
+ my %params = ();
+ my $q = new MockQuery('mysite', \%params);
+
+ # geocode a straightforward string, expect success
+ my ($x, $y, $easting, $northing, $error) = Page::geocode_string('Buckingham Palace', $q);
+ ok($x == 3279, 'example x coordinate generated');
+ ok($y == 1113, 'example y coordinate generated');
+ ok($easting == 529044, 'example easting generated');
+ ok($northing == 179619, 'example northing generated');
+
+ # expect a failure message for Northern Ireland
+ ($x, $y, $easting, $northing, $error) = Page::geocode_string('Falls Road, Belfast', $q);
+ ok($error eq "We do not cover Northern Ireland, I'm afraid, as our licence doesn't include any maps for the region.", 'error message produced for NI location');
+}
+
+sub test_header() {
my $q = mock_query();
my $html;
my %params = (title => 'test title');
@@ -77,3 +93,4 @@ sub test_base_url_with_lang {
ok(test_base_url_with_lang() == 1, 'Ran all tests for base_url_with_lang');
ok(test_footer() == 1, 'Ran all tests for the footer function');
ok(test_header() == 1, 'Ran all tests for the header function');
+ok(test_geocode_string() == 1, 'Ran all tests for the geocode_string function');
diff --git a/web/alert.cgi b/web/alert.cgi
index 302007dd0..3ba199ca5 100755
--- a/web/alert.cgi
+++ b/web/alert.cgi
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: alert.cgi,v 1.45 2009-09-09 08:32:41 louise Exp $
+# $Id: alert.cgi,v 1.46 2009-09-15 17:42:43 louise Exp $
use strict;
use Standard;
@@ -75,7 +75,7 @@ sub alert_list {
$n = Page::tile_to_os($input{y});
} else {
try {
- ($x, $y, $e, $n, $error) = Page::geocode($input{pc});
+ ($x, $y, $e, $n, $error) = Page::geocode($input{pc}, $q);
} catch Error::Simple with {
$error = shift;
};
diff --git a/web/index.cgi b/web/index.cgi
index 99614e364..b320f6bdb 100755
--- a/web/index.cgi
+++ b/web/index.cgi
@@ -6,7 +6,7 @@
# Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: index.cgi,v 1.281 2009-09-15 14:01:56 louise Exp $
+# $Id: index.cgi,v 1.282 2009-09-15 17:42:43 louise Exp $
use strict;
use Standard;
@@ -105,6 +105,8 @@ Page::do_fastcgi(\&main);
sub front_page {
my ($q, $error) = @_;
my $pc_h = ent($q->param('pc') || '');
+ my $cobrand = Page::get_cobrand($q);
+ my $cobrand_form_elements = Cobrand::form_elements(Page::get_cobrand($q), 'postcodeForm', $q);
my $out = '<p id="expl"><strong>' . _('Report, view, or discuss local problems') . '</strong>';
my $subhead = _('(like graffiti, fly tipping, broken paving slabs, or street lighting)');
$out .= '<br><small>' . $subhead . '</small>' if $subhead ne ' ';
@@ -142,6 +144,7 @@ EOF
<label for="pc">$question</label>
&nbsp;<input type="text" name="pc" value="$pc_h" id="pc" size="10" maxlength="200">
&nbsp;<input type="submit" value="$activate" id="submit">
+$cobrand_form_elements
</form>
<div id="front_intro">
@@ -447,7 +450,7 @@ sub display_form {
$easting = Page::tile_to_os($input{x});
$northing = Page::tile_to_os($input{y});
} else {
- my ($x, $y, $e, $n, $error) = Page::geocode($input{pc});
+ my ($x, $y, $e, $n, $error) = Page::geocode($input{pc}, $q);
$easting = $e; $northing = $n;
}
} elsif ($pin_x && $pin_y) {
@@ -463,7 +466,7 @@ sub display_form {
} elsif ($input{partial} && $input{pc} && !$input{easting} && !$input{northing}) {
my ($x, $y, $error);
try {
- ($x, $y, $easting, $northing, $error) = Page::geocode($input{pc});
+ ($x, $y, $easting, $northing, $error) = Page::geocode($input{pc}, $q);
} catch Error::Simple with {
$error = shift;
};
@@ -772,7 +775,7 @@ sub display_location {
return front_page($q, @errors) unless $x || $y || $input{pc};
if (!$x && !$y) {
try {
- ($x, $y, $easting, $northing, $error) = Page::geocode($input{pc});
+ ($x, $y, $easting, $northing, $error) = Page::geocode($input{pc}, $q);
} catch Error::Simple with {
$error = shift;
};
diff --git a/web/rss.cgi b/web/rss.cgi
index 5d6edded2..215a7421a 100755
--- a/web/rss.cgi
+++ b/web/rss.cgi
@@ -6,7 +6,7 @@
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
-# $Id: rss.cgi,v 1.27 2009-01-15 10:19:28 matthew Exp $
+# $Id: rss.cgi,v 1.28 2009-09-15 17:42:43 louise Exp $
use strict;
use Error qw(:try);
@@ -73,7 +73,7 @@ sub rss_local_problems {
} elsif ($pc) {
my $error;
try {
- ($x, $y, $e, $n, $error) = Page::geocode($pc);
+ ($x, $y, $e, $n, $error) = Page::geocode($pc, $q);
} catch Error::Simple with {
$error = shift;
};