diff options
Diffstat (limited to 'perllib/FixMyStreet/Cobrand/Zurich.pm')
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 178 |
1 files changed, 92 insertions, 86 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index b2a0e331d..817fc48a1 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -7,6 +7,7 @@ use RABX; use List::Util qw(min); use Scalar::Util 'blessed'; use DateTime::Format::Pg; +use Try::Tiny; use strict; use warnings; @@ -1172,95 +1173,100 @@ sub admin_stats { sub export_as_csv { my ($self, $c, $params) = @_; - $c->model('DB')->schema->storage->sql_maker->quote_char('"'); - my $csv = $c->stash->{csv} = { - problems => $c->model('DB::Problem')->search_rs( - $params, - { - join => ['admin_log_entries', 'user'], - distinct => 1, - columns => [ - 'id', 'created', - 'latitude', 'longitude', - 'cobrand', 'category', - 'state', 'user_id', - 'external_body', - 'title', 'detail', - 'photo', - 'whensent', 'lastupdate', - 'service', - 'extra', - { sum_time_spent => { sum => 'admin_log_entries.time_spent' } }, - 'name', 'user.id', 'user.email', 'user.phone', 'user.name', - ] - } - ), - headers => [ - 'Report ID', 'Created', 'Sent to Agency', 'Last Updated', - 'E', 'N', 'Category', 'Status', 'Closure Status', - 'UserID', 'User email', 'User phone', 'User name', - 'External Body', 'Time Spent', 'Title', 'Detail', - 'Media URL', 'Interface Used', 'Council Response', - 'Strasse', 'Mast-Nr.', 'Haus-Nr.', 'Hydranten-Nr.', - ], - columns => [ - 'id', 'created', 'whensent',' lastupdate', 'local_coords_x', - 'local_coords_y', 'category', 'state', 'closure_status', - 'user_id', 'user_email', 'user_phone', 'user_name', - 'body_name', 'sum_time_spent', 'title', 'detail', - 'media_url', 'service', 'public_response', - 'strasse', 'mast_nr',' haus_nr', 'hydranten_nr', - ], - extra_data => sub { - my $report = shift; - - my $body_name = ""; - if ( my $external_body = $report->body($c) ) { - $body_name = $external_body->name || '[Unknown body]'; - } + try { + $c->model('DB')->schema->storage->sql_maker->quote_char('"'); + my $csv = $c->stash->{csv} = { + problems => $c->model('DB::Problem')->search_rs( + $params, + { + join => ['admin_log_entries', 'user'], + distinct => 1, + columns => [ + 'id', 'created', + 'latitude', 'longitude', + 'cobrand', 'category', + 'state', 'user_id', + 'external_body', + 'title', 'detail', + 'photo', + 'whensent', 'lastupdate', + 'service', + 'extra', + { sum_time_spent => { sum => 'admin_log_entries.time_spent' } }, + 'name', 'user.id', 'user.email', 'user.phone', 'user.name', + ] + } + ), + headers => [ + 'Report ID', 'Created', 'Sent to Agency', 'Last Updated', + 'E', 'N', 'Category', 'Status', 'Closure Status', + 'UserID', 'User email', 'User phone', 'User name', + 'External Body', 'Time Spent', 'Title', 'Detail', + 'Media URL', 'Interface Used', 'Council Response', + 'Strasse', 'Mast-Nr.', 'Haus-Nr.', 'Hydranten-Nr.', + ], + columns => [ + 'id', 'created', 'whensent',' lastupdate', 'local_coords_x', + 'local_coords_y', 'category', 'state', 'closure_status', + 'user_id', 'user_email', 'user_phone', 'user_name', + 'body_name', 'sum_time_spent', 'title', 'detail', + 'media_url', 'service', 'public_response', + 'strasse', 'mast_nr',' haus_nr', 'hydranten_nr', + ], + extra_data => sub { + my $report = shift; + + my $body_name = ""; + if ( my $external_body = $report->body($c) ) { + $body_name = $external_body->name || '[Unknown body]'; + } - my $detail = $report->detail; - my $public_response = $report->get_extra_metadata('public_response') || ''; - my $metas = $report->get_extra_fields(); - my %extras; - foreach my $field (@$metas) { - $extras{$field->{name}} = $field->{value}; - } + my $detail = $report->detail; + my $public_response = $report->get_extra_metadata('public_response') || ''; + my $metas = $report->get_extra_fields(); + my %extras; + foreach my $field (@$metas) { + $extras{$field->{name}} = $field->{value}; + } - # replace newlines with HTML <br/> element - $detail =~ s{\r?\n}{ <br/> }g; - $public_response =~ s{\r?\n}{ <br/> }g if $public_response; - - # Assemble photo URL, if report has a photo - my $photo_to_display = $c->cobrand->allow_photo_display($report); - my $media_url = (@{$report->photos} && $photo_to_display) - ? $c->cobrand->base_url . $report->photos->[$photo_to_display-1]->{url} - : ''; - - return { - whensent => $report->whensent, - lastupdate => $report->lastupdate, - user_id => $report->user_id, - user_email => $report->user->email || '', - user_phone => $report->user->phone || '', - user_name => $report->name, - closure_status => $report->get_extra_metadata('closure_status') || '', - body_name => $body_name, - sum_time_spent => $report->get_column('sum_time_spent') || 0, - detail => $detail, - media_url => $media_url, - service => $report->service || 'Web interface', - public_response => $public_response, - strasse => $extras{'strasse'} || '', - mast_nr => $extras{'mast_nr'} || '', - haus_nr => $extras{'haus_nr'} || '', - hydranten_nr => $extras{'hydranten_nr'} || '' - }; - }, - filename => 'stats', + # replace newlines with HTML <br/> element + $detail =~ s{\r?\n}{ <br/> }g; + $public_response =~ s{\r?\n}{ <br/> }g if $public_response; + + # Assemble photo URL, if report has a photo + my $photo_to_display = $c->cobrand->allow_photo_display($report); + my $media_url = (@{$report->photos} && $photo_to_display) + ? $c->cobrand->base_url . $report->photos->[$photo_to_display-1]->{url} + : ''; + + return { + whensent => $report->whensent, + lastupdate => $report->lastupdate, + user_id => $report->user_id, + user_email => $report->user->email || '', + user_phone => $report->user->phone || '', + user_name => $report->name, + closure_status => $report->get_extra_metadata('closure_status') || '', + body_name => $body_name, + sum_time_spent => $report->get_column('sum_time_spent') || 0, + detail => $detail, + media_url => $media_url, + service => $report->service || 'Web interface', + public_response => $public_response, + strasse => $extras{'strasse'} || '', + mast_nr => $extras{'mast_nr'} || '', + haus_nr => $extras{'haus_nr'} || '', + hydranten_nr => $extras{'hydranten_nr'} || '' + }; + }, + filename => 'stats', + }; + $c->forward('/dashboard/generate_csv'); + } catch { + die $_; + } finally { + $c->model('DB')->schema->storage->sql_maker->quote_char(''); }; - $c->forward('/dashboard/generate_csv'); - $c->model('DB')->schema->storage->sql_maker->quote_char(''); } sub problem_confirm_email_extras { |