diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/Triage.pm | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Dashboard.pm | 120 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Reports.pm | 123 |
3 files changed, 182 insertions, 62 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/Dashboard.pm b/perllib/FixMyStreet/App/Controller/Dashboard.pm index 495e47c2a..f226e08de 100644 --- a/perllib/FixMyStreet/App/Controller/Dashboard.pm +++ b/perllib/FixMyStreet/App/Controller/Dashboard.pm @@ -70,15 +70,20 @@ sub check_page_allowed : Private { $c->detach( '/auth/redirect' ) unless $c->user_exists; - $c->detach( '/page_error_404_not_found' ) - unless $c->user->from_body || $c->user->is_superuser; + my $cobrand_body = $c->cobrand->can('council_area_id') ? $c->cobrand->body : undef; - my $body = $c->user->from_body; - if (!$body && $c->get_param('body')) { - # Must be a superuser, so allow query parameter if given - $body = $c->model('DB::Body')->find({ id => $c->get_param('body') }); + my $body; + if ($c->user->is_superuser) { + if ($c->get_param('body')) { + $body = $c->model('DB::Body')->find({ id => $c->get_param('body') }); + } else { + $body = $cobrand_body; + } + } elsif ($c->user->from_body && (!$cobrand_body || $cobrand_body->id == $c->user->from_body->id)) { + $body = $c->user->from_body; + } else { + $c->detach( '/page_error_404_not_found' ) } - return $body; } @@ -494,6 +499,107 @@ sub generate_csv : Private { } } +sub heatmap : Local : Args(0) { + my ($self, $c) = @_; + + my $body = $c->stash->{body} = $c->forward('check_page_allowed'); + $c->detach( '/page_error_404_not_found' ) + unless $body && $c->cobrand->feature('heatmap'); + + $c->stash->{page} = 'reports'; # So the map knows to make clickable pins + + my @wards = $c->get_param_list('wards', 1); + $c->forward('/reports/ward_check', [ @wards ]) if @wards; + $c->forward('/reports/stash_report_filter_status'); + $c->forward('/reports/stash_report_sort', [ $c->cobrand->reports_ordering ]); # Not actually used + my $parameters = $c->forward( '/reports/load_problems_parameters'); + + my $where = $parameters->{where}; + my $filter = $parameters->{filter}; + delete $filter->{rows}; + + $c->forward('heatmap_filters', [ $where ]); + + # Load the relevant stuff for the sidebar as well + my $problems = $c->cobrand->problems; + $problems = $problems->to_body($body); + $problems = $problems->search($where, $filter); + + $c->forward('heatmap_sidebar', [ $problems, $where ]); + + if ($c->get_param('ajax')) { + my @pins; + while ( my $problem = $problems->next ) { + push @pins, $problem->pin_data($c, 'reports'); + } + $c->stash->{pins} = \@pins; + $c->detach('/reports/ajax', [ 'dashboard/heatmap-list.html' ]); + } + + my $children = $c->stash->{body}->first_area_children; + $c->stash->{children} = $children; + $c->stash->{ward_hash} = { map { $_->{id} => 1 } @{$c->stash->{wards}} } if $c->stash->{wards}; + + $c->forward('/reports/setup_categories_and_map'); +} + +sub heatmap_filters :Private { + my ($self, $c, $where) = @_; + + # Wards + my @areas = @{$c->user->area_ids || []}; + # Want to get everything if nothing given in an ajax call + if (!$c->stash->{wards} && @areas) { + $c->stash->{wards} = [ map { { id => $_ } } @areas ]; + $where->{areas} = [ + map { { 'like', '%,' . $_ . ',%' } } @areas + ]; + } + + # Date range + my $start_default = DateTime->today(time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone)->subtract(months => 1); + $c->stash->{start_date} = $c->get_param('start_date') || $start_default->strftime('%Y-%m-%d'); + $c->stash->{end_date} = $c->get_param('end_date'); + + my $range = FixMyStreet::DateRange->new( + start_date => $c->stash->{start_date}, + start_default => $start_default, + end_date => $c->stash->{end_date}, + formatter => $c->model('DB')->storage->datetime_parser, + ); + $where->{'me.confirmed'} = $range->sql; +} + +sub heatmap_sidebar :Private { + my ($self, $c, $problems, $where) = @_; + + $c->stash->{five_newest} = [ $problems->search(undef, { + rows => 5, + order_by => { -desc => 'confirmed' }, + })->all ]; + + $c->stash->{ten_oldest} = [ $problems->search({ + 'me.state' => [ FixMyStreet::DB::Result::Problem->open_states() ], + }, { + rows => 10, + order_by => 'lastupdate', + })->all ]; + + my $params = { map { my $n = $_; s/me\./problem\./; $_ => $where->{$n} } keys %$where }; + my $body = $c->stash->{body}; + my @c = $c->model('DB::Comment')->to_body($body)->search({ + %$params, + 'me.user_id' => { -not_in => [ $c->user->id, $body->comment_user_id || () ] }, + 'me.state' => 'confirmed', + }, { + columns => 'problem_id', + group_by => 'problem_id', + order_by => { -desc => \'max(me.confirmed)' }, + rows => 5, + })->all; + $c->stash->{five_commented} = [ map { $_->problem } @c ]; +} + =head1 AUTHOR Matthew Somerville diff --git a/perllib/FixMyStreet/App/Controller/Reports.pm b/perllib/FixMyStreet/App/Controller/Reports.pm index 741cbb60f..85e647f65 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,51 @@ 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'); + my $body = $c->stash->{body}; # Might be undef my $page = $c->get_param('p') || 1; + + my $problems = $c->cobrand->problems; + my $where = $parameters->{where}; + my $filter = $parameters->{filter}; + + 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 +649,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')) { @@ -617,45 +661,14 @@ sub load_and_group_problems : Private { $where->{longitude} = { '>=', $min_lon, '<', $max_lon }; } - 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, - ); + $c->cobrand->call_hook('munge_load_and_group_problems', $where, $filter); - return 1; + return { + where => $where, + filter => $filter, + }; } - sub check_non_public_reports_permission : Private { my ($self, $c, $where) = @_; |