diff options
author | Matthew Somerville <matthew@mysociety.org> | 2019-10-08 10:24:20 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-10-14 14:36:50 +0100 |
commit | ca2df0362def3476e9bf50a09513fc0a1fe2786a (patch) | |
tree | 23c775c7888911aa6c3798ecad17542ad550dfcb | |
parent | f61ec9231b8562f27248ae2ba564bd809c54099a (diff) |
Refactor Reports controller a bit.
Split load_and_group_problems in two, split out category list/map setup,
and factor out sort stash setting. No actual changes.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/Triage.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 128 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 1 |
3 files changed, 76 insertions, 54 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/Triage.pm b/perllib/FixMyStreet/App/Controller/Admin/Triage.pm index 385248cd1..50d1b1437 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/Triage.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/Triage.pm @@ -45,6 +45,7 @@ sub index : Path : Args(0) { } $c->stash->{body} = $c->forward('/reports/body_find', [ $c->cobrand->council_area ]); $c->forward( 'stash_report_filter_status' ); + $c->forward('/reports/stash_report_sort', [ $c->cobrand->reports_ordering ]); $c->forward( '/reports/load_and_group_problems' ); $c->stash->{page} = 'reports'; # So the map knows to make clickable pins diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 741cbb60f..771d829f0 100644 --- a/perllib/FixMyStreet/App/Controller/Reports.pm +++ b/perllib/FixMyStreet/App/Controller/Reports.pm @@ -151,6 +151,7 @@ sub ward : Path : Args(2) { if @wards; $c->forward( 'check_canonical_url', [ $body ] ); $c->forward( 'stash_report_filter_status' ); + $c->forward('stash_report_sort', [ $c->cobrand->reports_ordering ]); $c->forward( 'load_and_group_problems' ); if ($c->get_param('ajax')) { @@ -164,6 +165,25 @@ sub ward : Path : Args(2) { $c->stash->{stats} = $c->cobrand->get_report_stats(); + $c->forward('setup_categories_and_map'); + + # List of wards + if ( !$c->stash->{wards} && $c->stash->{body}->id && $c->stash->{body}->body_areas->first ) { + my $children = $c->stash->{body}->first_area_children; + unless ($children->{error}) { + foreach (values %$children) { + $_->{url} = $c->uri_for( $c->stash->{body_url} + . '/' . $c->cobrand->short_name( $_ ) + ); + } + $c->stash->{children} = $children; + } + } +} + +sub setup_categories_and_map :Private { + my ($self, $c) = @_; + my @categories = $c->stash->{body}->contacts->not_deleted->search( undef, { columns => [ 'id', 'category', 'extra', 'body_id', 'send_method' ], distinct => 1, @@ -188,19 +208,6 @@ sub ward : Path : Args(2) { ); $c->cobrand->tweak_all_reports_map( $c ); - - # List of wards - if ( !$c->stash->{wards} && $c->stash->{body}->id && $c->stash->{body}->body_areas->first ) { - my $children = $c->stash->{body}->first_area_children; - unless ($children->{error}) { - foreach (values %$children) { - $_->{url} = $c->uri_for( $c->stash->{body_url} - . '/' . $c->cobrand->short_name( $_ ) - ); - } - $c->stash->{children} = $children; - } - } } sub rss_area : Path('/rss/area') : Args(1) { @@ -551,9 +558,57 @@ sub load_dashboard_data : Private { sub load_and_group_problems : Private { my ( $self, $c ) = @_; - $c->forward('stash_report_sort', [ $c->cobrand->reports_ordering ]); + my $parameters = $c->forward('load_problems_parameters'); + + # JS will request the same (or more) data client side + return if $c->get_param('js'); + my $body = $c->stash->{body}; # Might be undef my $page = $c->get_param('p') || 1; + + my $problems = $parameters->{problems}; + unless ($problems) { + my $where = $parameters->{where}; + my $filter = $parameters->{filter}; + + $problems = $c->cobrand->problems; + if ($where->{areas} || $body) { + $problems = $problems->to_body($body); + } + + $problems = $problems->search( + $where, + $filter + )->include_comment_counts->page( $page ); + + $c->stash->{pager} = $problems->pager; + } + + my ( %problems, @pins ); + while ( my $problem = $problems->next ) { + if ( !$body ) { + add_row( $c, $problem, 0, \%problems, \@pins ); + next; + } + # Add to bodies it was sent to + my $bodies = $problem->bodies_str_ids; + foreach ( @$bodies ) { + next if $_ != $body->id; + add_row( $c, $problem, $_, \%problems, \@pins ); + } + } + + $c->stash( + problems => \%problems, + pins => \@pins, + ); + + return 1; +} + +sub load_problems_parameters : Private { + my ($self, $c) = @_; + my $category = [ $c->get_param_list('filter_category', 1) ]; my $states = $c->stash->{filter_problem_states}; @@ -600,15 +655,10 @@ sub load_and_group_problems : Private { $where->{category} = $category; } - my $problems = $c->cobrand->problems; - if ($c->stash->{wards}) { $where->{areas} = [ map { { 'like', '%,' . $_->{id} . ',%' } } @{$c->stash->{wards}} ]; - $problems = $problems->to_body($body); - } elsif ($body) { - $problems = $problems->to_body($body); } if (my $bbox = $c->get_param('bbox')) { @@ -619,43 +669,13 @@ sub load_and_group_problems : Private { my $cobrand_problems = $c->cobrand->call_hook('munge_load_and_group_problems', $where, $filter); - # JS will request the same (or more) data client side - return if $c->get_param('js'); - - if ($cobrand_problems) { - $problems = $cobrand_problems; - } else { - $problems = $problems->search( - $where, - $filter - )->include_comment_counts->page( $page ); - - $c->stash->{pager} = $problems->pager; - } - - my ( %problems, @pins ); - while ( my $problem = $problems->next ) { - if ( !$body ) { - add_row( $c, $problem, 0, \%problems, \@pins ); - next; - } - # Add to bodies it was sent to - my $bodies = $problem->bodies_str_ids; - foreach ( @$bodies ) { - next if $_ != $body->id; - add_row( $c, $problem, $_, \%problems, \@pins ); - } - } - - $c->stash( - problems => \%problems, - pins => \@pins, - ); - - return 1; + return { + problems => $cobrand_problems, + where => $where, + filter => $filter, + }; } - sub check_non_public_reports_permission : Private { my ($self, $c, $where) = @_; diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 9cf1030f0..6cbaa285b 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -324,6 +324,7 @@ sub report_page_data { $c->stash->{page} = 'reports'; $c->forward( 'stash_report_filter_status' ); + $c->forward('stash_report_sort', [ $c->cobrand->reports_ordering ]); $c->forward( 'load_and_group_problems' ); $c->stash->{body} = { id => 0 }; # So template can fetch the list |