diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Dashboard.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Dashboard.pm | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Dashboard.pm b/perllib/FixMyStreet/App/Controller/Dashboard.pm index 032c36e05..ffd8d76c1 100644 --- a/perllib/FixMyStreet/App/Controller/Dashboard.pm +++ b/perllib/FixMyStreet/App/Controller/Dashboard.pm @@ -103,7 +103,10 @@ sub index : Path : Args(0) { $c->stash->{bodies} = \@bodies; } - $c->stash->{start_date} = $c->get_param('start_date'); + my $days30 = DateTime->now(time_zone => FixMyStreet->time_zone || FixMyStreet->local_time_zone)->subtract(days => 30); + $days30->truncate( to => 'day' ); + + $c->stash->{start_date} = $c->get_param('start_date') || $days30->strftime('%Y-%m-%d'); $c->stash->{end_date} = $c->get_param('end_date'); $c->stash->{q_state} = $c->get_param('state') || ''; @@ -136,30 +139,14 @@ sub construct_rs_filter : Private { } my $dtf = $c->model('DB')->storage->datetime_parser; - my $date = DateTime->now( time_zone => FixMyStreet->local_time_zone )->subtract(days => 30); - $date->truncate( to => 'day' ); - - $where{'me.confirmed'} = { '>=', $dtf->format_datetime($date) }; - - my $start_date = $c->stash->{start_date}; - my $end_date = $c->stash->{end_date}; - if ($start_date or $end_date) { - my @parts; - if ($start_date) { - my $date = $dtf->parse_datetime($start_date); - push @parts, { '>=', $dtf->format_datetime( $date ) }; - } - if ($end_date) { - my $one_day = DateTime::Duration->new( days => 1 ); - my $date = $dtf->parse_datetime($end_date); - push @parts, { '<', $dtf->format_datetime( $date + $one_day ) }; - } - if (scalar @parts == 2) { - $where{'me.confirmed'} = [ -and => $parts[0], $parts[1] ]; - } else { - $where{'me.confirmed'} = $parts[0]; - } + my $start_date = $dtf->parse_datetime($c->stash->{start_date}); + $where{'me.confirmed'} = { '>=', $dtf->format_datetime($start_date) }; + + if (my $end_date = $c->stash->{end_date}) { + my $one_day = DateTime::Duration->new( days => 1 ); + $end_date = $dtf->parse_datetime($end_date) + $one_day; + $where{'me.confirmed'} = [ -and => $where{'me.confirmed'}, { '<', $dtf->format_datetime($end_date) } ]; } $c->stash->{params} = \%where; @@ -291,7 +278,7 @@ sub export_as_csv : Private { my $csv = $c->stash->{csv} = { problems => $c->stash->{problems_rs}->search_rs({}, { prefetch => 'comments', - order_by => 'me.confirmed' + order_by => ['me.confirmed', 'me.id'], }), headers => [ 'Report ID', @@ -346,6 +333,7 @@ sub export_as_csv : Private { } sort keys %where }, }; + $c->cobrand->call_hook("dashboard_export_add_columns"); $c->forward('generate_csv'); } @@ -358,6 +346,8 @@ Generates a CSV output, given a 'csv' stash hashref containing: * columns: an arrayref of the columns (looked up in the row's as_hashref, plus the following: user_name_display, acknowledged, fixed, closed, wards, local_coords_x, local_coords_y, url). +* extra_data: If present, a function that is passed the report and returns a +hashref of extra data to include that can be used by 'columns'. =cut @@ -371,21 +361,16 @@ sub generate_csv : Private { my $fixed_states = FixMyStreet::DB::Result::Problem->fixed_states; my $closed_states = FixMyStreet::DB::Result::Problem->closed_states; - my $wards = 0; - my $comments = 0; - foreach (@{$c->stash->{csv}->{columns}}) { - $wards = 1 if $_ eq 'wards'; - $comments = 1 if $_ eq 'acknowledged'; - } + my %asked_for = map { $_ => 1 } @{$c->stash->{csv}->{columns}}; my $problems = $c->stash->{csv}->{problems}; while ( my $report = $problems->next ) { - my $hashref = $report->as_hashref($c); + my $hashref = $report->as_hashref($c, \%asked_for); $hashref->{user_name_display} = $report->anonymous ? '(anonymous)' : $report->user->name; - if ($comments) { + if ($asked_for{acknowledged}) { for my $comment ($report->comments) { my $problem_state = $comment->problem_state or next; next unless $comment->state eq 'confirmed'; @@ -400,7 +385,7 @@ sub generate_csv : Private { } } - if ($wards) { + if ($asked_for{wards}) { $hashref->{wards} = join ', ', map { $c->stash->{children}->{$_}->{name} } grep {$c->stash->{children}->{$_} } @@ -411,6 +396,11 @@ sub generate_csv : Private { $report->local_coords; $hashref->{url} = join '', $c->cobrand->base_url_for_report($report), $report->url; + if (my $fn = $c->stash->{csv}->{extra_data}) { + my $extra = $fn->($report); + $hashref = { %$hashref, %$extra }; + } + $csv->combine( @{$hashref}{ @{$c->stash->{csv}->{columns}} |