From 98e61f99be442523fdfb38e95d9a8c2132ea0405 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Mon, 1 Apr 2019 10:47:11 +0100 Subject: Factor to common FixMyStreet::MapIt call. --- perllib/FixMyStreet/App/Controller/Admin/Bodies.pm | 11 ++++------- perllib/FixMyStreet/App/Controller/Admin/Users.pm | 3 ++- perllib/FixMyStreet/App/Controller/Council.pm | 13 ++++--------- perllib/FixMyStreet/App/Controller/Open311.pm | 8 ++------ perllib/FixMyStreet/App/Controller/Report/New.pm | 1 - perllib/FixMyStreet/App/Controller/Reports.pm | 18 +++++++----------- perllib/FixMyStreet/App/Controller/Rss.pm | 4 ++-- perllib/FixMyStreet/Cobrand/Default.pm | 1 - perllib/FixMyStreet/DB/Factories.pm | 4 ++-- perllib/FixMyStreet/DB/Result/Body.pm | 8 ++++---- perllib/FixMyStreet/MapIt.pm | 18 ++++++++++++++++++ perllib/FixMyStreet/Script/Alerts.pm | 6 +++--- perllib/FixMyStreet/Script/Reports.pm | 1 - perllib/Open311/GetServiceRequests.pm | 8 ++------ 14 files changed, 50 insertions(+), 54 deletions(-) create mode 100644 perllib/FixMyStreet/MapIt.pm diff --git a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm index 1f8840bcb..0e47d2238 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/Bodies.pm @@ -6,6 +6,7 @@ BEGIN { extends 'Catalyst::Controller'; } use POSIX qw(strcoll); use mySociety::EmailUtil qw(is_valid_email_list); +use FixMyStreet::MapIt; use FixMyStreet::SendReport; =head1 NAME @@ -104,7 +105,7 @@ sub body : Chained('/') : PathPart('admin/body') : CaptureArgs(1) { $c->stash->{body} = $body; if ($body->body_areas->first) { - my $example_postcode = mySociety::MaPit::call('area/example_postcode', $body->body_areas->first->area_id); + my $example_postcode = FixMyStreet::MapIt::call('area/example_postcode', $body->body_areas->first->area_id); if ($example_postcode && ! ref $example_postcode) { $c->stash->{example_pc} = $example_postcode; } @@ -187,14 +188,10 @@ sub body_form_dropdowns : Private { my $areas; my $whitelist = $c->config->{MAPIT_ID_WHITELIST}; - my %params; - $params{generation} = $c->config->{MAPIT_GENERATION} - if $c->config->{MAPIT_GENERATION}; - if ( $whitelist && ref $whitelist eq 'ARRAY' && @$whitelist ) { - $areas = mySociety::MaPit::call('areas', $whitelist, %params); + $areas = FixMyStreet::MapIt::call('areas', $whitelist); } else { - $areas = mySociety::MaPit::call('areas', $c->cobrand->area_types, %params); + $areas = FixMyStreet::MapIt::call('areas', $c->cobrand->area_types); } # Some cobrands may want to add extra areas at runtime beyond those diff --git a/perllib/FixMyStreet/App/Controller/Admin/Users.pm b/perllib/FixMyStreet/App/Controller/Admin/Users.pm index 11a6d9962..bcbc808ed 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/Users.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/Users.pm @@ -8,6 +8,7 @@ use POSIX qw(strcoll); use mySociety::EmailUtil qw(is_valid_email); use Text::CSV; +use FixMyStreet::MapIt; use FixMyStreet::SMS; use Utils; @@ -450,7 +451,7 @@ sub user_alert_details : Private { } if (@wards) { - $c->stash->{alert_areas} = mySociety::MaPit::call('areas', join(',', @wards) ); + $c->stash->{alert_areas} = FixMyStreet::MapIt::call('areas', join(',', @wards) ); } my %body_names = map { $_->{id} => $_->{name} } @{ $c->stash->{bodies} }; diff --git a/perllib/FixMyStreet/App/Controller/Council.pm b/perllib/FixMyStreet/App/Controller/Council.pm index 2e2dce0f7..4acaba903 100644 --- a/perllib/FixMyStreet/App/Controller/Council.pm +++ b/perllib/FixMyStreet/App/Controller/Council.pm @@ -2,6 +2,8 @@ package FixMyStreet::App::Controller::Council; use Moose; use namespace::autoclean; +use FixMyStreet::MapIt; + BEGIN {extends 'Catalyst::Controller'; } =head1 NAME @@ -59,10 +61,6 @@ sub load_and_check_areas : Private { my $all_areas; - my %params; - $params{generation} = $c->config->{MAPIT_GENERATION} - if $c->config->{MAPIT_GENERATION}; - if ($prefetched_all_areas) { $all_areas = { map { $_ => { id => $_ } } @@ -71,8 +69,7 @@ sub load_and_check_areas : Private { } elsif ( $c->stash->{fetch_all_areas} ) { my %area_types = map { $_ => 1 } @$area_types; $all_areas = - mySociety::MaPit::call( 'point', - "4326/$longitude,$latitude", %params ); + FixMyStreet::MapIt::call('point', "4326/$longitude,$latitude"); $c->stash->{all_areas_mapit} = $all_areas; $all_areas = { map { $_ => $all_areas->{$_} } @@ -81,9 +78,7 @@ sub load_and_check_areas : Private { }; } else { $all_areas = - mySociety::MaPit::call( 'point', - "4326/$longitude,$latitude", %params, - type => $area_types ); + FixMyStreet::MapIt::call('point', "4326/$longitude,$latitude", type => $area_types); } if ($all_areas->{error}) { $c->stash->{location_error_mapit_error} = 1; diff --git a/perllib/FixMyStreet/App/Controller/Open311.pm b/perllib/FixMyStreet/App/Controller/Open311.pm index 5d7ec63d6..97e1a782a 100644 --- a/perllib/FixMyStreet/App/Controller/Open311.pm +++ b/perllib/FixMyStreet/App/Controller/Open311.pm @@ -7,6 +7,7 @@ use namespace::autoclean; use JSON::MaybeXS; use XML::Simple; use DateTime::Format::W3CDTF; +use FixMyStreet::MapIt; BEGIN { extends 'Catalyst::Controller'; } @@ -164,12 +165,7 @@ sub get_services : Private { if ($lat || $lon) { my $area_types = $c->cobrand->area_types; - my %params = ( type => $area_types ); - $params{generation} = $c->config->{MAPIT_GENERATION} - if $c->config->{MAPIT_GENERATION}; - my $all_areas = mySociety::MaPit::call('point', - "4326/$lon,$lat", - %params); + my $all_areas = FixMyStreet::MapIt::call('point', "4326/$lon,$lat", type => $area_types); $categories = $categories->search( { 'body_areas.area_id' => [ keys %$all_areas ], }, { join => { 'body' => 'body_areas' } } ); diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index b6292facb..5f039a074 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -9,7 +9,6 @@ use List::MoreUtils qw(uniq); use List::Util 'first'; use POSIX 'strcoll'; use HTML::Entities; -use mySociety::MaPit; use Path::Class; use Utils; use mySociety::EmailUtil; diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 751870bd4..49bdce379 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -6,7 +6,7 @@ use JSON::MaybeXS; use List::MoreUtils qw(any); use Path::Tiny; use RABX; -use mySociety::MaPit; +use FixMyStreet::MapIt; BEGIN { extends 'Catalyst::Controller'; } @@ -219,12 +219,10 @@ sub rss_area_ward : Path('/rss/area') : Args(2) { # We're checking an area here, but this function is currently doing that. return if $c->cobrand->reports_body_check( $c, $area ); - my %params = ( type => $c->cobrand->area_types ); - $params{generation} = $c->config->{MAPIT_GENERATION} - if $c->config->{MAPIT_GENERATION}; - # We must now have a string to check on mapit - my $areas = mySociety::MaPit::call( 'areas', $area, %params ); + my $areas = FixMyStreet::MapIt::call( 'areas', $area, + type => $c->cobrand->area_types, + ); if (keys %$areas == 1) { ($c->stash->{area}) = values %$areas; @@ -393,11 +391,9 @@ sub ward_check : Private { $parent_id = $c->stash->{area}->{id}; } - my %params = ( type => $c->cobrand->area_types_children ); - $params{generation} = $c->config->{MAPIT_GENERATION} - if $c->config->{MAPIT_GENERATION}; - - my $qw = mySociety::MaPit::call('area/children', [ $parent_id ], %params); + my $qw = FixMyStreet::MapIt::call('area/children', [ $parent_id ], + type => $c->cobrand->area_types_children, + ); my %names = map { $c->cobrand->short_name({ name => $_ }) => 1 } @wards; my @areas; foreach my $area (sort { $a->{name} cmp $b->{name} } values %$qw) { diff --git a/perllib/FixMyStreet/App/Controller/Rss.pm b/perllib/FixMyStreet/App/Controller/Rss.pm index e1da4445d..443e45b93 100755 --- a/perllib/FixMyStreet/App/Controller/Rss.pm +++ b/perllib/FixMyStreet/App/Controller/Rss.pm @@ -11,7 +11,7 @@ use FixMyStreet::App::Model::PhotoSet; use FixMyStreet::Gaze; use mySociety::Locale; -use mySociety::MaPit; +use FixMyStreet::MapIt; use Lingua::EN::Inflect qw(ORD); BEGIN { extends 'Catalyst::Controller'; } @@ -66,7 +66,7 @@ sub reports_in_area : LocalRegex('^area/(\d+)$') { my ( $self, $c ) = @_; my $id = $c->req->captures->[0]; - my $area = mySociety::MaPit::call('area', $id); + my $area = FixMyStreet::MapIt::call('area', $id); $c->stash->{type} = 'area_problems'; $c->stash->{qs} = '/' . $id; $c->stash->{db_params} = [ $id ]; diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index ed15dbf05..a8146128e 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -13,7 +13,6 @@ use URI; use Digest::MD5 qw(md5_hex); use Carp; -use mySociety::MaPit; use mySociety::PostcodeUtil; =head1 The default cobrand diff --git a/perllib/FixMyStreet/DB/Factories.pm b/perllib/FixMyStreet/DB/Factories.pm index 4cbab4753..5af9ed38f 100644 --- a/perllib/FixMyStreet/DB/Factories.pm +++ b/perllib/FixMyStreet/DB/Factories.pm @@ -199,7 +199,7 @@ sub create_problem { package FixMyStreet::DB::Factory::Body; use parent -norequire, "FixMyStreet::DB::Factory::Base"; -use mySociety::MaPit; +use FixMyStreet::MapIt; __PACKAGE__->resultset(FixMyStreet::DB->resultset("Body")); @@ -208,7 +208,7 @@ __PACKAGE__->exclude(['area_id', 'categories']); __PACKAGE__->fields({ name => __PACKAGE__->callback(sub { my $area_id = shift->get('area_id'); - my $area = mySociety::MaPit::call('area', $area_id); + my $area = FixMyStreet::MapIt::call('area', $area_id); $area->{name}; }), body_areas => __PACKAGE__->callback(sub { diff --git a/perllib/FixMyStreet/DB/Result/Body.pm b/perllib/FixMyStreet/DB/Result/Body.pm index c04c117ae..37164c990 100644 --- a/perllib/FixMyStreet/DB/Result/Body.pm +++ b/perllib/FixMyStreet/DB/Result/Body.pm @@ -134,6 +134,7 @@ __PACKAGE__->rabx_column('extra'); use Moo; use namespace::clean; +use FixMyStreet::MapIt; with 'FixMyStreet::Roles::Translatable', 'FixMyStreet::Roles::Extra'; @@ -182,10 +183,9 @@ sub first_area_children { my $cobrand = $self->result_source->schema->cobrand; - my %params = ( type => $cobrand->area_types_children ); - $params{generation} = FixMyStreet->config('MAPIT_GENERATION') - if FixMyStreet->config('MAPIT_GENERATION'); - my $children = mySociety::MaPit::call('area/children', $body_area->area_id, %params); + my $children = FixMyStreet::MapIt::call('area/children', $body_area->area_id, + type => $cobrand->area_types_children, + ); return $children; } diff --git a/perllib/FixMyStreet/MapIt.pm b/perllib/FixMyStreet/MapIt.pm new file mode 100644 index 000000000..d0a5f4760 --- /dev/null +++ b/perllib/FixMyStreet/MapIt.pm @@ -0,0 +1,18 @@ +package FixMyStreet::MapIt; + +use FixMyStreet; +use mySociety::MaPit; + +sub call { + my ($url, $params, %opts) = @_; + + # 'area' always returns the ID you provide, no matter its generation, so no + # point in specifying it for that. 'areas' similarly if given IDs, but we + # might be looking up types or names, so might as well specify it then. + $opts{generation} = FixMyStreet->config('MAPIT_GENERATION') + if $url ne 'area' && FixMyStreet->config('MAPIT_GENERATION'); + + return mySociety::MaPit::call($url, $params, %opts); +} + +1; diff --git a/perllib/FixMyStreet/Script/Alerts.pm b/perllib/FixMyStreet/Script/Alerts.pm index a0c25a8fc..55f4b3db5 100644 --- a/perllib/FixMyStreet/Script/Alerts.pm +++ b/perllib/FixMyStreet/Script/Alerts.pm @@ -8,7 +8,7 @@ use IO::String; use FixMyStreet::Gaze; use mySociety::Locale; -use mySociety::MaPit; +use FixMyStreet::MapIt; use RABX; use FixMyStreet::Cobrand; @@ -186,14 +186,14 @@ sub send() { $data{report} = $schema->resultset('Problem')->find({ id => $row->{id} }); } if ($ref eq 'area_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter}); + my $va_info = FixMyStreet::MapIt::call('area', $row->{alert_parameter}); $data{area_name} = $va_info->{name}; } elsif ($ref eq 'council_problems' || $ref eq 'ward_problems') { my $body = FixMyStreet::DB->resultset('Body')->find({ id => $row->{alert_parameter} }); $data{area_name} = $body->name; } if ($ref eq 'ward_problems') { - my $va_info = mySociety::MaPit::call('area', $row->{alert_parameter2}); + my $va_info = FixMyStreet::MapIt::call('area', $row->{alert_parameter2}); $data{ward_name} = $va_info->{name}; } } diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index 1fef6fe08..ecd461cd9 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -8,7 +8,6 @@ use DateTime::Format::Pg; use Utils; use Utils::OpenStreetMap; -use mySociety::MaPit; use FixMyStreet; use FixMyStreet::Cobrand; diff --git a/perllib/Open311/GetServiceRequests.pm b/perllib/Open311/GetServiceRequests.pm index 8a48cc6f4..215361a10 100644 --- a/perllib/Open311/GetServiceRequests.pm +++ b/perllib/Open311/GetServiceRequests.pm @@ -3,6 +3,7 @@ package Open311::GetServiceRequests; use Moo; use Open311; use FixMyStreet::DB; +use FixMyStreet::MapIt; use FixMyStreet::App::Model::PhotoSet; use DateTime::Format::W3CDTF; @@ -89,18 +90,13 @@ sub create_problems { } my $request_id = $request->{service_request_id}; - my %params; - $params{generation} = mySociety::Config::get('MAPIT_GENERATION') - if mySociety::Config::get('MAPIT_GENERATION'); - my ($latitude, $longitude) = ( $request->{lat}, $request->{long} ); ($latitude, $longitude) = Utils::convert_en_to_latlon_truncated( $longitude, $latitude ) if $self->convert_latlong; my $all_areas = - mySociety::MaPit::call( 'point', - "4326/$longitude,$latitude", %params ); + FixMyStreet::MapIt::call('point', "4326/$longitude,$latitude"); # skip if it doesn't look like it's for this body my @areas = grep { $all_areas->{$_->area_id} } $body->body_areas; -- cgit v1.2.3