diff options
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 18 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 5 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Barnet.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Southampton.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Nearby.pm | 33 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 9 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Nearby.pm | 50 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 23 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map.pm | 11 | ||||
-rw-r--r-- | perllib/FixMyStreet/Map/Tilma/Original.pm | 25 | ||||
-rw-r--r-- | perllib/Problems.pm | 66 | ||||
-rw-r--r-- | templates/web/default/around/around_map_list_items.html | 6 | ||||
-rw-r--r-- | templates/web/default/around/on_map_list_items.html | 2 |
14 files changed, 169 insertions, 104 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index 1e06beef1..370fe7b89 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -182,13 +182,17 @@ sub display_location : Private { # create a list of all the pins my @pins; unless ($c->req->param('no_pins')) { - @pins = map { { - latitude => $_->{latitude}, - longitude => $_->{longitude}, - colour => $_->{state} eq 'fixed' ? 'green' : 'red', - id => $_->{id}, - title => $_->{title}, - } } @$on_map_all, @$around_map; + @pins = map { + # Here we might have a DB::Problem or a DB::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::App::Model::DB::Nearby') ? $_->problem : $_; + { + latitude => $p->latitude, + longitude => $p->longitude, + colour => $p->state eq 'fixed' ? 'green' : 'red', + id => $p->id, + title => $p->title, + } + } @$on_map_all, @$around_map; } { # FIXME - ideally this indented code should be in the templates diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index c64bc9054..17ebc8b0c 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -253,14 +253,13 @@ sub load_problems : Private { } elsif ($c->stash->{council}) { $where->{areas} = { 'like', '%' . $c->stash->{council}->{id} . '%' }; } - my $current_timestamp = Problems::current_timestamp(); my $problems = $c->cobrand->problems->search( $where, { columns => [ 'id', 'title', 'detail', 'council', 'state', 'areas', - { duration => { extract => "epoch from $current_timestamp-lastupdate" } }, - { age => { extract => "epoch from $current_timestamp-confirmed" } }, + { duration => { extract => "epoch from current_timestamp-lastupdate" } }, + { age => { extract => "epoch from current_timestamp-confirmed" } }, ], order_by => { -desc => 'id' }, } diff --git a/perllib/FixMyStreet/Cobrand/Barnet.pm b/perllib/FixMyStreet/Cobrand/Barnet.pm index 6468fd934..460bd3be9 100644 --- a/perllib/FixMyStreet/Cobrand/Barnet.pm +++ b/perllib/FixMyStreet/Cobrand/Barnet.pm @@ -12,11 +12,13 @@ sub site_restriction { return ( "and council='2489'", 'barnet', { council => '2489' } ); } +sub problems_clause { + return { council => '2489' }; +} + sub problems { my $self = shift; - return $self->{c}->model('DB::Problem')->search( { - council => '2489' - } ); + return $self->{c}->model('DB::Problem')->search( $self->problems_clause ); } sub base_url { diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 208ac6643..949d875a3 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -69,6 +69,15 @@ sub path_to_web_templates { return FixMyStreet->path_to( 'templates/web', $self->moniker ); } +=head1 problems_clause + +Returns a hash for a query to be used by problems (and elsewhere in joined +queries) to restrict results for a cobrand. + +=cut + +sub problems_clause {} + =head1 problems Returns a ResultSet of Problems, restricted to a subset if we're on a cobrand diff --git a/perllib/FixMyStreet/Cobrand/Southampton.pm b/perllib/FixMyStreet/Cobrand/Southampton.pm index 77032baa8..99484181a 100644 --- a/perllib/FixMyStreet/Cobrand/Southampton.pm +++ b/perllib/FixMyStreet/Cobrand/Southampton.pm @@ -12,11 +12,13 @@ sub site_restriction { return ( "and council='2567'", 'southampton', { council => '2567' } ); } +sub problems_clause { + return { council => '2567' }; +} + sub problems { my $self = shift; - return $self->{c}->model('DB::Problem')->search( { - council => '2567' - } ); + return $self->{c}->model('DB::Problem')->search( $self->problems_clause ); } sub base_url { diff --git a/perllib/FixMyStreet/DB/Result/Nearby.pm b/perllib/FixMyStreet/DB/Result/Nearby.pm new file mode 100644 index 000000000..d3d228788 --- /dev/null +++ b/perllib/FixMyStreet/DB/Result/Nearby.pm @@ -0,0 +1,33 @@ +package FixMyStreet::DB::Result::Nearby; + +# Thanks to http://www.perlmonks.org/?node_id=633800 + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; +use Moose; +use namespace::clean -except => [ 'meta' ]; + +__PACKAGE__->table( 'NONE' ); +__PACKAGE__->add_columns( + "problem_id", + { data_type => "integer", is_nullable => 0 }, + "distance", + { data_type => "double precision", is_nullable => 0 }, +); +__PACKAGE__->belongs_to( + "problem", + "FixMyStreet::DB::Result::Problem", + { id => "problem_id" }, + { is_deferrable => 1 }, +); + +# Make a new ResultSource based on the User class +__PACKAGE__->result_source_instance + ->name( \'problem_find_nearby(?,?,?)' ); + +# we need the inline_constructor bit as we don't inherit from Moose +__PACKAGE__->meta->make_immutable( inline_constructor => 0 ); + +1; diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index f496fb062..f1665ccba 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -134,7 +134,7 @@ sub whensent_local { return $self->whensent ? $self->whensent->set_time_zone($tz) - : $self->confirmed; + : $self->whensent; } sub lastupdate_local { @@ -145,6 +145,13 @@ sub lastupdate_local { : $self->lastupdate; } +around service => sub { + my ( $orig, $self ) = ( shift, shift ); + my $s = $self->$orig(@_); + $s =~ s/_/ /g; + return $s; +}; + =head2 check_for_errors $error_hashref = $problem->check_for_errors(); diff --git a/perllib/FixMyStreet/DB/ResultSet/Nearby.pm b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm new file mode 100644 index 000000000..3b3a3d90b --- /dev/null +++ b/perllib/FixMyStreet/DB/ResultSet/Nearby.pm @@ -0,0 +1,50 @@ +package FixMyStreet::DB::ResultSet::Nearby; +use base 'DBIx::Class::ResultSet'; + +use strict; +use warnings; + +sub nearby { + my ( $rs, $c, $dist, $ids, $limit, $mid_lat, $mid_lon, $interval ) = @_; + + my $params = { + state => [ 'confirmed', 'fixed' ], + }; + $params->{'current_timestamp-lastupdate'} = { '<', \"'$interval'::interval" } + if $interval; + $params->{id} = { -not_in => $ids } + if $ids; + $params = { + %{ $c->cobrand->problems_clause }, + %$params + } if $c->cobrand->problems_clause; + + my $attrs = { + join => 'problem', + columns => [ + 'problem.id', 'problem.title', 'problem.latitude', + 'problem.longitude', 'distance', 'problem.state', + 'problem.confirmed' + ], + bind => [ $mid_lat, $mid_lon, $dist ], + order_by => [ 'distance', { -desc => 'created' } ], + rows => $limit, + }; + + my @problems = mySociety::Locale::in_gb_locale { $rs->search( $params, $attrs )->all }; + return \@problems; +} + +# XXX Not currently used, so not migrating at present. +#sub fixed_nearby { +# my ($dist, $mid_lat, $mid_lon) = @_; +# mySociety::Locale::in_gb_locale { select_all( +# "select id, title, latitude, longitude, distance +# from problem_find_nearby(?, ?, $dist) as nearby, problem +# where nearby.problem_id = problem.id and state='fixed' +# site_restriction +# order by lastupdate desc", $mid_lat, $mid_lon); +# } +#} + +1; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index af850ecd0..ea146cd54 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -63,6 +63,29 @@ sub recent { return $result; } +# Problems around a location + +sub around_map { + my ( $rs, $min_lat, $max_lat, $min_lon, $max_lon, $interval, $limit ) = @_; + my $attr = { + order_by => { -desc => 'created' }, + columns => [ + 'id', 'title' ,'latitude', 'longitude', 'state', 'confirmed' + ], + }; + $attr->{rows} = $limit if $limit; + + my $q = { + state => [ 'confirmed', 'fixed' ], + latitude => { '>=', $min_lat, '<', $max_lat }, + longitude => { '>=', $min_lon, '<', $max_lon }, + }; + $q->{'current_timestamp - lastupdate'} = { '<', \"'$interval'::interval" }; + + my @problems = mySociety::Locale::in_gb_locale { $rs->search( $q, $attr )->all }; + return \@problems; +} + # Admin functions sub timeline { diff --git a/perllib/FixMyStreet/Map.pm b/perllib/FixMyStreet/Map.pm index 6b41c8d0d..6a5a82fa9 100644 --- a/perllib/FixMyStreet/Map.pm +++ b/perllib/FixMyStreet/Map.pm @@ -80,8 +80,8 @@ sub map_features { my $around_limit = $c->cobrand->on_map_list_limit || undef; my @around_args = ( $min_lat, $max_lat, $min_lon, $max_lon, $interval ); - my $around_map_list = Problems::around_map( @around_args, $around_limit ); - my $around_map = Problems::around_map( @around_args, undef ); + my $around_map_list = $c->cobrand->problems->around_map( @around_args, $around_limit ); + my $around_map = $c->cobrand->problems->around_map( @around_args, undef ); my $dist; mySociety::Locale::in_gb_locale { @@ -92,9 +92,10 @@ sub map_features { $dist = int( $dist * 10 + 0.5 ) / 10; my $limit = 20; - my @ids = map { $_->{id} } @$around_map_list; - my $nearby = Problems::nearby( $dist, join( ',', @ids ), - $limit, $lat, $lon, $interval ); + my @ids = map { $_->id } @$around_map_list; + my $nearby = $c->model('DB::Nearby')->nearby( + $c, $dist, \@ids, $limit, $lat, $lon, $interval + ); return ( $around_map, $around_map_list, $nearby, $dist ); } diff --git a/perllib/FixMyStreet/Map/Tilma/Original.pm b/perllib/FixMyStreet/Map/Tilma/Original.pm index 486a7c739..6fa854cb2 100644 --- a/perllib/FixMyStreet/Map/Tilma/Original.pm +++ b/perllib/FixMyStreet/Map/Tilma/Original.pm @@ -107,21 +107,22 @@ sub map_pins { my $pins = ''; foreach (@$around_map) { - ( $_->{easting}, $_->{northing} ) = - _ll_to_en( $_->{latitude}, $_->{longitude} ); - my $px = os_to_px($_->{easting}, $sx); - my $py = os_to_px($_->{northing}, $sy, 1); - my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; - $pins .= display_pin($c, $px, $py, $col, $_->{id}, $_->{title}); + my ( $easting, $northing ) = + _ll_to_en( $_->latitude, $_->longitude ); + my $px = os_to_px($easting, $sx); + my $py = os_to_px($northing, $sy, 1); + my $col = $_->state eq 'fixed' ? 'green' : 'red'; + $pins .= display_pin($c, $px, $py, $col, $_->id, $_->title); } foreach (@$nearby) { - ( $_->{easting}, $_->{northing} ) = - _ll_to_en( $_->{latitude}, $_->{longitude} ); - my $px = os_to_px($_->{easting}, $sx); - my $py = os_to_px($_->{northing}, $sy, 1); - my $col = $_->{state} eq 'fixed' ? 'green' : 'red'; - $pins .= display_pin($c, $px, $py, $col, $_->{id}, $_->{title}); + my $p = $_->problem; + my ( $easting, $northing ) = + _ll_to_en( $p->latitude, $p->longitude ); + my $px = os_to_px($easting, $sx); + my $py = os_to_px($northing, $sy, 1); + my $col = $p->state eq 'fixed' ? 'green' : 'red'; + $pins .= display_pin($c, $px, $py, $col, $p->id, $p->title); } return ($pins, $around_map_list, $nearby, $dist); diff --git a/perllib/Problems.pm b/perllib/Problems.pm index a6eb0c519..5f38a12c4 100644 --- a/perllib/Problems.pm +++ b/perllib/Problems.pm @@ -22,11 +22,6 @@ use mySociety::MaPit; my $site_restriction = ''; my $site_key = 0; -sub current_timestamp { - my $current_timestamp = dbh()->selectrow_array('select ms_current_timestamp()'); - return "'$current_timestamp'::timestamp"; -} - sub number_comments { my $key = "number_comments:$site_key"; my $result = Memcached::get($key); @@ -85,69 +80,8 @@ sub recent_photos { return $out; } -# Problems around a location - -sub around_map { - my ($min_lat, $max_lat, $min_lon, $max_lon, $interval, $limit) = @_; - my $limit_clause = ''; - if ($limit) { - $limit_clause = " limit $limit"; - } - mySociety::Locale::in_gb_locale { select_all( - "select id,title,latitude,longitude,state, - extract(epoch from confirmed) as time - from problem - where state in ('confirmed', 'fixed') - and latitude>=? and latitude<? and longitude>=? and longitude<? " . - ($interval ? " and ms_current_timestamp()-lastupdate < '$interval'::interval" : '') . - " $site_restriction - order by created desc - $limit_clause", $min_lat, $max_lat, $min_lon, $max_lon); - }; -} - -sub nearby { - my ($dist, $ids, $limit, $mid_lat, $mid_lon, $interval) = @_; - mySociety::Locale::in_gb_locale { select_all( - "select id, title, latitude, longitude, distance, state, - extract(epoch from confirmed) as time - from problem_find_nearby(?, ?, $dist) as nearby, problem - where nearby.problem_id = problem.id " . - ($interval ? " and ms_current_timestamp()-lastupdate < '$interval'::interval" : '') . - " and state in ('confirmed', 'fixed')" . ($ids ? ' and id not in (' . $ids . ')' : '') . " - $site_restriction - order by distance, created desc limit $limit", $mid_lat, $mid_lon); - } -} - -sub fixed_nearby { - my ($dist, $mid_lat, $mid_lon) = @_; - mySociety::Locale::in_gb_locale { select_all( - "select id, title, latitude, longitude, distance - from problem_find_nearby(?, ?, $dist) as nearby, problem - where nearby.problem_id = problem.id and state='fixed' - $site_restriction - order by lastupdate desc", $mid_lat, $mid_lon); - } -} - # Fetch an individual problem -sub fetch_problem { - my $id = shift; - my $p = dbh()->selectrow_hashref( - "select id, latitude, longitude, council, category, title, detail, photo, - used_map, name, anonymous, extract(epoch from confirmed) as time, - state, extract(epoch from whensent-confirmed) as whensent, - extract(epoch from ms_current_timestamp()-lastupdate) as duration, - service, cobrand, cobrand_data, external_body - from problem where id=? and state in ('confirmed','fixed', 'hidden') - $site_restriction", {}, $id - ); - $p->{service} =~ s/_/ /g if $p && $p->{service}; - return $p; -} - # Report functions =item council_problems WARD COUNCIL diff --git a/templates/web/default/around/around_map_list_items.html b/templates/web/default/around/around_map_list_items.html index 87416f7a1..e13e9ccc9 100644 --- a/templates/web/default/around/around_map_list_items.html +++ b/templates/web/default/around/around_map_list_items.html @@ -4,9 +4,9 @@ [% dist = tprintf("%.1f", (p.distance || 0) ) %] <li> - <a href="[% c.uri_for('/report', p.id ) %]">[% p.title | html %]</a> - <small>([% prettify_epoch( p.time, 1 ) %], [% dist %]km)</small> - [% IF p.state == 'fixed' %] + <a href="[% c.uri_for('/report', p.problem.id ) %]">[% p.problem.title | html %]</a> + <small>([% prettify_epoch( p.problem.confirmed_local.epoch, 1 ) %], [% dist %]km)</small> + [% IF p.problem.state == 'fixed' %] <small>[% loc('(fixed)') %]</small> [% END %] </li> diff --git a/templates/web/default/around/on_map_list_items.html b/templates/web/default/around/on_map_list_items.html index 11a4e3138..9db3d9492 100644 --- a/templates/web/default/around/on_map_list_items.html +++ b/templates/web/default/around/on_map_list_items.html @@ -2,7 +2,7 @@ [% FOREACH p IN on_map %] <li> <a href="[% c.uri_for('/report', p.id ) %]">[% p.title | html %]</a> - <small>([% prettify_epoch( p.time, 1 ) %])</small> + <small>([% prettify_epoch( p.confirmed_local.epoch, 1 ) %])</small> [% IF p.state == 'fixed' %] <small>[% loc('(fixed)') %]</small> [% END %] |