diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2016-11-01 16:56:08 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2016-11-04 17:24:46 +0000 |
commit | 051093f803444d99c48d130d59dcfe2ba9759c90 (patch) | |
tree | 7407d9616442dc5bc9c81f29532b9a5b7704b6f5 /perllib/FixMyStreet/DB | |
parent | b3bb51dab4f620463c551e7bbe6814d415ebf227 (diff) |
Add sort order options to list pages.
Includes newest, oldest, least/most recently updated, and most comments.
The default remains the same, which is last updated on /reports, and
newest on /my and /around (the latter plus not-in-view
sorted-by-distance ones).
Diffstat (limited to 'perllib/FixMyStreet/DB')
-rw-r--r-- | perllib/FixMyStreet/DB/ResultSet/Problem.pm | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/perllib/FixMyStreet/DB/ResultSet/Problem.pm b/perllib/FixMyStreet/DB/ResultSet/Problem.pm index 723a6e7c2..f1ed50721 100644 --- a/perllib/FixMyStreet/DB/ResultSet/Problem.pm +++ b/perllib/FixMyStreet/DB/ResultSet/Problem.pm @@ -140,27 +140,27 @@ sub _recent { # Problems around a location sub around_map { - my ( $rs, $min_lat, $max_lat, $min_lon, $max_lon, $interval, $limit, $categories, $states ) = @_; + my ( $rs, $limit, %p) = @_; my $attr = { - order_by => { -desc => 'created' }, + order_by => $p{order}, }; $attr->{rows} = $limit if $limit; - unless ( $states ) { - $states = FixMyStreet::DB::Result::Problem->visible_states(); + unless ( $p{states} ) { + $p{states} = FixMyStreet::DB::Result::Problem->visible_states(); } my $q = { non_public => 0, - state => [ keys %$states ], - latitude => { '>=', $min_lat, '<', $max_lat }, - longitude => { '>=', $min_lon, '<', $max_lon }, + state => [ keys %{$p{states}} ], + latitude => { '>=', $p{min_lat}, '<', $p{max_lat} }, + longitude => { '>=', $p{min_lon}, '<', $p{max_lon} }, }; - $q->{'current_timestamp - lastupdate'} = { '<', \"'$interval'::interval" } - if $interval; - $q->{category} = $categories if $categories && @$categories; + $q->{'current_timestamp - lastupdate'} = { '<', \"'$p{interval}'::interval" } + if $p{interval}; + $q->{category} = $p{categories} if $p{categories} && @{$p{categories}}; - my @problems = mySociety::Locale::in_gb_locale { $rs->search( $q, $attr )->all }; + my @problems = mySociety::Locale::in_gb_locale { $rs->search( $q, $attr )->include_comment_counts->all }; return \@problems; } @@ -238,4 +238,16 @@ sub send_reports { return FixMyStreet::Script::Reports::send($site_override); } +sub include_comment_counts { + my $rs = shift; + my $order_by = $rs->{attrs}{order_by}; + return $rs unless ref $order_by eq 'HASH' && $order_by->{-desc} eq 'comment_count'; + $rs->search({}, { + '+select' => [ { + "" => \'(select count(*) from comment where problem_id=me.id and state=\'confirmed\')', + -as => 'comment_count' + } ] + }); +} + 1; |