diff options
author | Matthew Somerville <matthew@mysociety.org> | 2011-06-10 14:56:00 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2011-06-10 14:56:00 +0100 |
commit | 391ca1c469d93bb2c4798cc15e56fc495b5e80dd (patch) | |
tree | 6bc90fae589de824095e668fbf510ef259935729 /perllib/FixMyStreet/App/Controller/Report.pm | |
parent | 7c96f8ec61d6eddc211f3f0e71cdb276c6a5f773 (diff) | |
parent | 860383f0de3287b0666d64a3ffff3db3a0f087ae (diff) |
Merge branch 'migrate_to_catalyst' into reportemptyhomes
Diffstat (limited to 'perllib/FixMyStreet/App/Controller/Report.pm')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm new file mode 100644 index 000000000..497ec784a --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -0,0 +1,160 @@ +package FixMyStreet::App::Controller::Report; + +use Moose; +use namespace::autoclean; +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Report - display a report + +=head1 DESCRIPTION + +Show a report + +=head1 ACTIONS + +=head2 index + +Redirect to homepage unless C<id> parameter in query, in which case redirect to +'/report/$id'. + +=cut + +sub index : Path('') : Args(0) { + my ( $self, $c ) = @_; + + my $id = $c->req->param('id'); + + my $uri = + $id + ? $c->uri_for( '/report', $id ) + : $c->uri_for('/'); + + $c->res->redirect($uri); +} + +=head2 report_display + +Display a report. + +=cut + +sub display : Path('') : Args(1) { + my ( $self, $c, $id ) = @_; + + if ( + $id =~ m{ ^ 3D (\d+) $ }x # Some council with bad email software + || $id =~ m{ ^(\d+) \D .* $ }x # trailing garbage + ) + { + return $c->res->redirect( $c->uri_for($1), 301 ); + } + + $c->forward('load_problem_or_display_error', [ $id ] ); + $c->forward( 'load_updates' ); + $c->forward( 'format_problem_for_display' ); +} + +sub load_problem_or_display_error : Private { + my ( $self, $c, $id ) = @_; + + # try to load a report if the id is a number + my $problem # + = $id =~ m{\D} # is id non-numeric? + ? undef # ...don't even search + : $c->cobrand->problems->find( { id => $id } ); + + # check that the problem is suitable to show. + if ( !$problem || $problem->state eq 'unconfirmed' ) { + $c->detach( '/page_error_404_not_found', [ _('Unknown problem ID') ] ); + } + elsif ( $problem->state eq 'hidden' ) { + $c->detach( + '/page_error_410_gone', + [ _('That report has been removed from FixMyStreet.') ] # + ); + } + + $c->stash->{problem} = $problem; + return 1; +} + +sub load_updates : Private { + my ( $self, $c ) = @_; + + my $updates = $c->model('DB::Comment')->search( + { problem_id => $c->stash->{problem}->id, state => 'confirmed' }, + { order_by => 'confirmed' } + ); + + $c->stash->{updates} = $updates; + + return 1; +} + +sub format_problem_for_display : Private { + my ( $self, $c ) = @_; + + my $problem = $c->stash->{problem}; + + $c->stash->{banner} = $c->cobrand->generate_problem_banner($problem); + + $c->stash->{cobrand_alert_fields} = $c->cobrand->form_elements('/alerts'); + $c->stash->{cobrand_update_fields} = + $c->cobrand->form_elements('/updateForm'); + + ( $c->stash->{short_latitude}, $c->stash->{short_longitude} ) = + map { Utils::truncate_coordinate($_) } + ( $problem->latitude, $problem->longitude ); + + $c->stash->{report_name} = $c->req->param('name'); + + if ( $c->req->param('submit_update') ) { + # we may have munged these previously in /report/update + # so only set if they're not already in the stash + $c->stash->{form_name} ||= $c->req->param('name'); + $c->stash->{update_text} ||= $c->req->param('update'); + $c->stash->{email} ||= $c->req->param('rznvy'); + $c->stash->{fixed} ||= $c->req->param('fixed') ? ' checked' : ''; + $c->stash->{add_alert_checked} ||= + ( $c->req->param('add_alert') ? ' checked' : '' ); + } + else { + if ( $c->user ) { + $c->stash->{form_name} = $c->user->name; + $c->stash->{email} = $c->user->email; + $c->stash->{may_show_name} = ' checked' if $c->user->name; + } + $c->stash->{add_alert_checked} = ' checked'; + } + + $c->forward('generate_map_tags'); + + return 1; +} + +sub generate_map_tags : Private { + my ( $self, $c ) = @_; + + my $problem = $c->stash->{problem}; + + FixMyStreet::Map::display_map( + $c, + latitude => $problem->latitude, + longitude => $problem->longitude, + pins => $problem->used_map + ? [ { + latitude => $problem->latitude, + longitude => $problem->longitude, + colour => 'blue', + } ] + : [], + ); + + return 1; +} + +__PACKAGE__->meta->make_immutable; + +1; |