diff options
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 62 |
1 files changed, 50 insertions, 12 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index 629408401..bac3c5b17 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -72,9 +72,8 @@ sub report_update : Path : Args(0) { or $c->go( '/report/display', [ $c->req->param('id') ] ); $c->forward('save_update'); + $c->forward('redirect_or_confirm_creation'); - # just go back to the report page for now - $c->go( '/report/display', [ $c->req->param('id') ] ); return 1; } @@ -136,14 +135,18 @@ sub process_update : Private { my ( $self, $c ) = @_; my %params = # - map { $_ => scalar $c->req->param($_) } ( 'update', 'name' ); + map { $_ => scalar $c->req->param($_) } ( 'update', 'name', 'fixed' ); + use Data::Dumper; + $c->log->debug( 'params: ' . Dumper( %params ) ); my $update = $c->model('DB::Comment')->new( { text => $params{update}, name => _trim_text( $params{name} ), problem => $c->stash->{problem}, - user => $c->stash->{update_user} + user => $c->stash->{update_user}, + state => 'unconfirmed', + mark_fixed => $params{fixed} ? 't' : 'f', } ); @@ -179,7 +182,8 @@ sub check_for_errors : Private { %{ $c->stash->{update}->check_for_errors }, ); - $c->log->debug( join ', ', keys %field_errors ); + # we don't care if there are errors with this... + delete $field_errors{name}; # all good if no errors return 1 unless scalar keys %field_errors; @@ -198,19 +202,53 @@ Save the update and the user as appropriate. sub save_update : Private { my ( $self, $c ) = @_; - if ( $c->stash->{update_user}->in_storage ) { - $c->stash->{update_user}->update_user; - } else { - $c->stash->{update_user}->insert; + my $user = $c->stash->{update_user}; + my $update = $c->stash->{update}; + + if ( !$user->in_storage ) { + $user->insert; + } elsif ( $c->user && $c->user->id == $user->id ) { + $user->update; + $update->confirm; } - if ( $c->stash->{update}->in_storage ) { - $c->stash->{update}->update; + if ( $update->in_storage ) { + $update->update; } else { - $c->stash->{update}->insert; + $update->insert; } } +=head2 redirect_or_confirm_creation + +Now that the update has been created either redirect the user to problem page if it +has been confirmed or email them a token if it has not been. + +=cut + +sub redirect_or_confirm_creation : Private { + my ( $self, $c ) = @_; + my $update = $c->stash->{update}; + + # If confirmed send the user straight there. + if ( $update->confirmed ) { + my $report_uri = $c->uri_for( '/report', $update->problem_id ); + $c->res->redirect($report_uri); + $c->detach; + } + + # otherwise create a confirm token and email it to them. + my $token = + $c->model("DB::Token") + ->create( { scope => 'comment', data => $update->id } ); + $c->stash->{token_url} = $c->uri_for_email( '/C', $token->token ); + $c->send_email( 'update-confirm.txt', { to => $update->user->email } ); + + # tell user that they've been sent an email + $c->stash->{template} = 'email_sent.html'; + $c->stash->{email_type} = 'update'; +} + __PACKAGE__->meta->make_immutable; 1; |