diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-05-02 10:33:36 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-05-02 16:05:17 +0100 |
commit | 1f83e1aec0128a2cc52d220e5eae2c2b2fad5122 (patch) | |
tree | dfb84334cdc163d95e0112ff19ab39739b81bfbc /perllib/FixMyStreet/Cobrand/Default.pm | |
parent | 6e739457cb294f7c99ce0a5b1132292c8fc352ac (diff) |
Tidy up find_closest* functions.
Allow find_closest to be called multiple times with only one lookup,
and to return just its data, not a compiled string.
Diffstat (limited to 'perllib/FixMyStreet/Cobrand/Default.pm')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 52 |
1 files changed, 24 insertions, 28 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index ac70fff08..ad84bd496 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -509,27 +509,35 @@ sub geocoded_string_check { return 1; } =head2 find_closest -Used by send-reports to attach nearest things to the bottom of the report +Used by send-reports and similar to attach nearest things to the bottom of the +report. =cut sub find_closest { - my ( $self, $latitude, $longitude, $problem ) = @_; - my $str = ''; + my ( $self, $problem, $as_data ) = @_; - if ( my $j = FixMyStreet::Geocode::Bing::reverse( $latitude, $longitude, disambiguate_location()->{bing_culture} ) ) { + my $j = $problem->geocode; + if (!$j) { + $j = FixMyStreet::Geocode::Bing::reverse( $problem->latitude, $problem->longitude, + disambiguate_location()->{bing_culture} ); # cache the bing results for use in alerts - if ( $problem ) { - $problem->geocode( $j ); - $problem->update; - } - if ($j->{resourceSets}[0]{resources}[0]{name}) { - $str .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"), - $j->{resourceSets}[0]{resources}[0]{name}) . "\n\n"; + $problem->geocode( $j ); + $problem->update; + } + + my $data = $as_data ? {} : ''; + if ($j && $j->{resourceSets}[0]{resources}[0]{name}) { + my $str = $j->{resourceSets}[0]{resources}[0]{name}; + if ($as_data) { + $data->{road} = $str; + } else { + $data .= sprintf(_("Nearest road to the pin placed on the map (automatically generated by Bing Maps): %s"), + $str) . "\n\n"; } } - return $str; + return $data; } =head2 find_closest_address_for_rss @@ -539,26 +547,14 @@ Used by rss feeds to provide a bit more context =cut sub find_closest_address_for_rss { - my ( $self, $latitude, $longitude, $problem ) = @_; - my $str = ''; + my ( $self, $problem ) = @_; - my $j; - if ( $problem && ref($problem) =~ /FixMyStreet/ && $problem->can( 'geocode' ) ) { - $j = $problem->geocode; - } else { + if (ref($problem) eq 'HASH') { $problem = FixMyStreet::App->model('DB::Problem')->find( { id => $problem->{id} } ); - $j = $problem->geocode; } + my $j = $problem->geocode; - # if we've not cached it then we don't want to look it up in order to avoid - # hammering the bing api - # if ( !$j ) { - # $j = FixMyStreet::Geocode::Bing::reverse( $latitude, $longitude, disambiguate_location()->{bing_culture}, 1 ); - - # $problem->geocode( $j ); - # $problem->update; - # } - + my $str = ''; if ($j && $j->{resourceSets}[0]{resources}[0]{name}) { my $address = $j->{resourceSets}[0]{resources}[0]{address}; my @address; |