diff options
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Contact.pm | 15 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 21 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Barnet.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Default.pm | 4 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Southampton.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/Problem.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 53 | ||||
-rw-r--r-- | perllib/Problems.pm | 95 | ||||
-rw-r--r-- | templates/web/default/alert/index.html | 5 | ||||
-rw-r--r-- | templates/web/default/alert/list.html | 5 | ||||
-rw-r--r-- | templates/web/default/index.html | 5 |
12 files changed, 85 insertions, 138 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Contact.pm b/perllib/FixMyStreet/App/Controller/Contact.pm index bd7d415e7..047de7691 100644 --- a/perllib/FixMyStreet/App/Controller/Contact.pm +++ b/perllib/FixMyStreet/App/Controller/Contact.pm @@ -63,16 +63,8 @@ sub determine_contact_type : Private { $update_id = undef unless $update_id && $update_id =~ /^[1-9]\d*$/; if ($id) { - my $problem = $c->cobrand->problems->find( - { id => $id }, - { - 'select' => [ - 'title', 'detail', 'name', - 'anonymous', 'id', - 'user_id', 'confirmed', - ] - } - ); + + $c->forward( '/report/load_problem_or_display_error', [ $id ] ); if ($update_id) { my $update = $c->model('DB::Comment')->find( @@ -81,9 +73,6 @@ sub determine_contact_type : Private { $c->stash->{update} = $update; } - elsif ($problem) { - $c->stash->{problem} = $problem; - } } return 1; diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index 0fe1834e7..497ec784a 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -52,6 +52,7 @@ sub display : Path('') : Args(1) { } $c->forward('load_problem_or_display_error', [ $id ] ); + $c->forward( 'load_updates' ); $c->forward( 'format_problem_for_display' ); } @@ -76,9 +77,14 @@ sub load_problem_or_display_error : Private { } $c->stash->{problem} = $problem; + return 1; +} + +sub load_updates : Private { + my ( $self, $c ) = @_; my $updates = $c->model('DB::Comment')->search( - { problem_id => $problem->id, state => 'confirmed' }, + { problem_id => $c->stash->{problem}->id, state => 'confirmed' }, { order_by => 'confirmed' } ); diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 2ca9245ad..2f1d88d08 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -24,7 +24,7 @@ sub report_update : Path : Args(0) { $c->detach( '/page_error_404_not_found', [ _('Unknown problem ID') ] ) unless $c->req->param('id'); - $c->forward('setup_page') + $c->forward( '/report/load_problem_or_display_error', [ $c->req->param('id') ] ) && $c->forward('process_user') && $c->forward('process_update') && $c->forward('/report/new/process_photo') @@ -89,25 +89,6 @@ sub display_confirmation : Private { return 1; } -=head2 setup_page - -Setup things we need for later. - -=cut - -sub setup_page : Private { - my ( $self, $c ) = @_; - - my $problem = - $c->cobrand->problems->find( { id => $c->req->param('id') } ); - - return unless $problem; - - $c->stash->{problem} = $problem; - - return 1; -} - =head2 process_user Load user from the database or prepare a new one. diff --git a/perllib/FixMyStreet/Cobrand/Barnet.pm b/perllib/FixMyStreet/Cobrand/Barnet.pm index 460bd3be9..e7bc67c5c 100644 --- a/perllib/FixMyStreet/Cobrand/Barnet.pm +++ b/perllib/FixMyStreet/Cobrand/Barnet.pm @@ -82,7 +82,7 @@ sub disambiguate_location { sub recent_photos { my ( $self, $num, $lat, $lon, $dist ) = @_; $num = 2 if $num == 3; - return Problems::recent_photos( $num, $lat, $lon, $dist ); + return $self->problems->recent_photos( $num, $lat, $lon, $dist ); } sub tilma_mid_point { diff --git a/perllib/FixMyStreet/Cobrand/Default.pm b/perllib/FixMyStreet/Cobrand/Default.pm index 949d875a3..fd03f235f 100644 --- a/perllib/FixMyStreet/Cobrand/Default.pm +++ b/perllib/FixMyStreet/Cobrand/Default.pm @@ -219,7 +219,7 @@ EASTING and NORTHING. sub recent_photos { my $self = shift; - return Problems::recent_photos(@_); + return $self->problems->recent_photos(@_); } =head2 recent @@ -258,7 +258,7 @@ sub front_stats_data { my $shorter_recency = '3 days'; my $fixed = $self->problems->recent_fixed(); - my $updates = Problems::number_comments(); + my $updates = $self->problems->number_comments(); my $new = $self->problems->recent_new( $recency ); if ( $new > $fixed && $self->shorten_recency_if_new_greater_than_fixed ) { diff --git a/perllib/FixMyStreet/Cobrand/Southampton.pm b/perllib/FixMyStreet/Cobrand/Southampton.pm index 99484181a..d70d818be 100644 --- a/perllib/FixMyStreet/Cobrand/Southampton.pm +++ b/perllib/FixMyStreet/Cobrand/Southampton.pm @@ -81,7 +81,7 @@ sub disambiguate_location { sub recent_photos { my ($self, $num, $lat, $lon, $dist) = @_; $num = 2 if $num == 3; - return Problems::recent_photos( $num, $lat, $lon, $dist ); + return $self->problems->recent_photos( $num, $lat, $lon, $dist ); } sub tilma_mid_point { diff --git a/perllib/FixMyStreet/DB/Result/Problem.pm b/perllib/FixMyStreet/DB/Result/Problem.pm index f1665ccba..ab05c3157 100644 --- a/perllib/FixMyStreet/DB/Result/Problem.pm +++ b/perllib/FixMyStreet/DB/Result/Problem.pm @@ -103,6 +103,14 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07010 @ 2011-05-24 15:32:43 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:U3aYCRwE4etekKaHdhEkIw +# Add fake relationship to stored procedure table +__PACKAGE__->has_many( + "nearby", + "FixMyStreet::DB::Result::Nearby", + { "foreign.problem_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + use DateTime::TimeZone; use Image::Size; use Moose; diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index ea146cd54..1f06acf5a 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -29,6 +29,20 @@ sub recent_fixed { return $result; } +sub number_comments { + my $rs = shift; + my $key = "number_comments:$site_key"; + my $result = Memcached::get($key); + unless ($result) { + $result = $rs->search( + { 'comments.state' => 'confirmed' }, + { join => 'comments' } + )->count; + Memcached::set($key, $result, 3600); + } + return $result; +} + sub recent_new { my ( $rs, $interval ) = @_; (my $key = $interval) =~ s/\s+//g; @@ -51,18 +65,53 @@ sub recent { my $key = "recent:$site_key"; my $result = Memcached::get($key); unless ($result) { - $result = $rs->search( { + $result = [ $rs->search( { state => [ 'confirmed', 'fixed' ] }, { columns => [ 'id', 'title' ], order_by => { -desc => 'confirmed' }, rows => 5, - } )->count; + } )->all ]; Memcached::set($key, $result, 3600); } return $result; } +sub recent_photos { + my ( $rs, $num, $lat, $lon, $dist ) = @_; + my $probs; + my $query = { + state => [ 'confirmed', 'fixed' ], + photo => { '!=', undef }, + }; + my $attrs = { + columns => [ 'id', 'title' ], + order_by => { -desc => 'confirmed' }, + rows => $num, + }; + if (defined $lat) { + my $dist2 = $dist; # Create a copy of the variable to stop it being stringified into a locale in the next line! + my $key = "recent_photos:$site_key:$num:$lat:$lon:$dist2"; + $probs = Memcached::get($key); + unless ($probs) { + $attrs->{bind} = [ $lat, $lon, $dist ]; + $attrs->{join} = 'nearby'; + $probs = [ mySociety::Locale::in_gb_locale { + $rs->search( $query, $attrs )->all; + } ]; + Memcached::set($key, $probs, 3600); + } + } else { + my $key = "recent_photos:$site_key:$num"; + $probs = Memcached::get($key); + unless ($probs) { + $probs = [ $rs->search( $query, $attrs )->all ]; + Memcached::set($key, $probs, 3600); + } + } + return $probs; +} + # Problems around a location sub around_map { diff --git a/perllib/Problems.pm b/perllib/Problems.pm index 5f38a12c4..987820d0c 100644 --- a/perllib/Problems.pm +++ b/perllib/Problems.pm @@ -22,101 +22,6 @@ use mySociety::MaPit; my $site_restriction = ''; my $site_key = 0; -sub number_comments { - my $key = "number_comments:$site_key"; - my $result = Memcached::get($key); - unless ($result) { - if ($site_restriction) { - $result = dbh()->selectrow_array("select count(*) from comment, problem - where comment.problem_id=problem.id and comment.state='confirmed' - $site_restriction"); - } else { - $result = dbh()->selectrow_array("select count(*) from comment - where state='confirmed'"); - } - Memcached::set($key, $result, 3600); - } - return $result; -} - -# Front page recent lists - -sub recent_photos { - my ($num, $lat, $lon, $dist) = @_; - my $probs; - if (defined $lat) { - my $dist2 = $dist; # Create a copy of the variable to stop it being stringified into a locale in the next line! - my $key = "recent_photos:$site_key:$num:$lat:$lon:$dist2"; - $probs = Memcached::get($key); - unless ($probs) { - $probs = mySociety::Locale::in_gb_locale { - select_all("select id, title - from problem_find_nearby(?, ?, ?) as nearby, problem - where nearby.problem_id = problem.id - and state in ('confirmed', 'fixed') and photo is not null - $site_restriction - order by confirmed desc limit $num", $lat, $lon, $dist); - }; - Memcached::set($key, $probs, 3600); - } - } else { - my $key = "recent_photos:$site_key:$num"; - $probs = Memcached::get($key); - unless ($probs) { - $probs = select_all("select id, title from problem - where state in ('confirmed', 'fixed') and photo is not null - $site_restriction - order by confirmed desc limit $num"); - Memcached::set($key, $probs, 3600); - } - } - my $out = ''; - foreach (@$probs) { - my $title = ent($_->{title}); - $out .= '<a href="/report/' . $_->{id} . - '"><img border="0" height="100" src="/photo?tn=1;id=' . $_->{id} . - '" alt="' . $title . '" title="' . $title . '"></a>'; - } - return $out; -} - -# Fetch an individual problem - -# Report functions - -=item council_problems WARD COUNCIL - -Returns a list of problems for a summary page. If WARD or COUNCIL area ids are given, -will only return problems for that area. Uses any site restriction defined by the -cobrand. - -=cut - -sub council_problems { - my ($ward, $one_council) = @_; - my @params; - my $where_extra = ''; - if ($ward) { - push @params, $ward; - $where_extra = "and areas like '%,'||?||',%'"; - } elsif ($one_council) { - push @params, $one_council; - $where_extra = "and areas like '%,'||?||',%'"; - } - my $current_timestamp = current_timestamp(); - my $problems = select_all( - "select id, title, detail, council, state, areas, - extract(epoch from $current_timestamp-lastupdate) as duration, - extract(epoch from $current_timestamp-confirmed) as age - from problem - where state in ('confirmed', 'fixed') - $where_extra - $site_restriction - order by id desc - ", @params); - return $problems; -} - # Admin view functions =item problem_search SEARCH diff --git a/templates/web/default/alert/index.html b/templates/web/default/alert/index.html index 7e03be4ae..3c01c44d4 100644 --- a/templates/web/default/alert/index.html +++ b/templates/web/default/alert/index.html @@ -29,7 +29,10 @@ postcode or street name and area:' ) %] [% IF photos %] <div id="alert_recent"> <h2>[% loc('Some photos of recent reports') %]</h2> - [% photos %] + [% FOREACH p IN photos %] + <a href="/report/[% p.id %]"><img border="0" height="100" + src="/photo?tn=1;id=[% p.id %]" alt="[% p.title | html %]" title="[% p.title | html %]"></a> + [% END %] </div> [% END %] [%# FIXME - put in blurb here %] diff --git a/templates/web/default/alert/list.html b/templates/web/default/alert/list.html index 1682b5a66..32c531247 100644 --- a/templates/web/default/alert/list.html +++ b/templates/web/default/alert/list.html @@ -27,7 +27,10 @@ [% IF photos %] <div id="alert_photos"> <h2>[% loc('Photos of recent nearby reports') %]</h2> - [% photos %] + [% FOREACH p IN photos %] + <a href="/report/[% p.id %]"><img border="0" height="100" + src="/photo?tn=1;id=[% p.id %]" alt="[% p.title | html %]" title="[% p.title | html %]"></a> + [% END %] </div> [% END %] diff --git a/templates/web/default/index.html b/templates/web/default/index.html index 4007e82a7..9c786459c 100644 --- a/templates/web/default/index.html +++ b/templates/web/default/index.html @@ -76,7 +76,10 @@ <div id="front_recent"> [% IF recent_photos %] <h2>[% loc('Photos of recent reports') %]</h2> - [% recent_photos %] + [% FOREACH p IN recent_photos %] + <a href="/report/[% p.id %]"><img border="0" height="100" + src="/photo?tn=1;id=[% p.id %]" alt="[% p.title | html %]" title="[% p.title | html %]"></a> + [% END %] [% END %] [% IF probs.size %] |