diff options
author | Struan Donald <struan@exo.org.uk> | 2011-05-18 15:48:00 +0100 |
---|---|---|
committer | Struan Donald <struan@exo.org.uk> | 2011-05-18 15:48:00 +0100 |
commit | c40da27da59b74c32db0a50a2a03473ec4cdba2c (patch) | |
tree | 0113a608b5e45edf690ce9504d6ca4c8445a24f5 | |
parent | 6b304e144051fe6d531ca475cd6fe43fd3705c7f (diff) |
initial problem update reporting. only does basic validation so far
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 110 | ||||
-rw-r--r-- | t/app/controller/report_updates.t | 29 | ||||
-rw-r--r-- | templates/web/default/report/display.html | 4 |
3 files changed, 141 insertions, 2 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm new file mode 100644 index 000000000..58eaaf0f2 --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -0,0 +1,110 @@ +package FixMyStreet::App::Controller::Report::Update; + +use Moose; +use namespace::autoclean; +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Report::Update + +=head1 DESCRIPTION + +Creates an update to a report + +=cut + +sub report_update : Path : Args(0) { + my ( $self, $c ) = @_; + +# my $q = shift; +# my @vars = qw(id name rznvy update fixed upload_fileid add_alert); +# my %input = map { $_ => $q->param($_) || '' } @vars; +# my @errors; +# my %field_errors; +# +# my $fh = $q->upload('photo'); +# if ($fh) { +# my $err = Page::check_photo($q, $fh); +# push @errors, $err if $err; +# } +# +# my $image; +# if ($fh) { +# try { +# $image = Page::process_photo($fh); +# } catch Error::Simple with { +# my $e = shift; +# push(@errors, sprintf(_("That image doesn't appear to have uploaded correctly (%s), please try again."), $e)); +# }; +# } +# +# if ($input{upload_fileid}) { +# open FP, mySociety::Config::get('UPLOAD_CACHE') . $input{upload_fileid}; +# $image = join('', <FP>); +# close FP; +# } +# +# return display_problem($q, \@errors, \%field_errors) if (@errors || scalar(keys(%field_errors))); +# my $cobrand = Page::get_cobrand($q); +# my $cobrand_data = Cobrand::extra_update_data($cobrand, $q); +# my $id = dbh()->selectrow_array("select nextval('comment_id_seq');"); +# Utils::workaround_pg_bytea("insert into comment +# (id, problem_id, name, email, website, text, state, mark_fixed, photo, lang, cobrand, cobrand_data) +# values (?, ?, ?, ?, '', ?, 'unconfirmed', ?, ?, ?, ?, ?)", 7, +# $id, $input{id}, $input{name}, $input{rznvy}, $input{update}, +# $input{fixed} ? 't' : 'f', $image, $mySociety::Locale::lang, $cobrand, $cobrand_data); +# +# my %h = (); +# $h{update} = $input{update}; +# $h{name} = $input{name} ? $input{name} : _("Anonymous"); +# my $base = Page::base_url_with_lang($q, undef, 1); +# $h{url} = $base . '/C/' . mySociety::AuthToken::store('update', { id => $id, add_alert => $input{add_alert} } ); +# dbh()->commit(); +# +# my $out = Page::send_confirmation_email($q, $input{rznvy}, $input{name}, 'update', %h); +# return $out; + + $c->forward( 'setup_page' ); + $c->forward( 'validate' ) || $c->forward( '/report/display', [ $c->req->param( 'id' ) ] ); + + # just go back to the report page for now + $c->go( '/report/display', [ $c->req->param( 'id' ) ] ); + return 1; +} + +sub setup_page : Private { + my ( $self, $c ) = @_; + + $c->stash->{problem} = $c->model( 'DB::Problem' )->find( + { id => $c->req->param('id') } + ); +} + +sub validate : Private { + my ( $self, $c ) = @_; + + my %field_errors = (); + + if ( $c->req->param( 'update' ) !~ /\S/ ) { + $field_errors{update} = _('Please enter a message'); + } + + if ($c->req->param('rznvy') !~ /\S/) { + $field_errors{email} = _('Please enter your email'); + } elsif (!mySociety::EmailUtil::is_valid_email($c->req->param('rznvy'))) { + $field_errors{email} = _('Please enter a valid email'); + } + + if ( scalar keys %field_errors ) { + $c->stash->{field_errors} = \%field_errors; + return; + } + + return 1; +} + + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/t/app/controller/report_updates.t b/t/app/controller/report_updates.t index a3a612c95..1ecdef96d 100644 --- a/t/app/controller/report_updates.t +++ b/t/app/controller/report_updates.t @@ -187,6 +187,35 @@ subtest "several updates shown in correct order" => sub { is $meta->[2], 'Posted anonymously at 08:12, Tuesday 15 March 2011, marked as fixed', 'third update'; }; +for my $test ( + { + fields => { + rznvy => '', + update => '', + name => '', + }, + field_errors => [ 'Please enter your email', 'Please enter a message' ] + }, + { + fields => { + rznvy => 'test', + update => '', + name => '', + }, + field_errors => [ 'Please enter a valid email', 'Please enter a message' ] + }, + ) +{ + subtest "submit an update" => sub { + $mech->get_ok("/report/$report_id"); + + $mech->submit_form_ok( { with_fields => $test->{fields} }, + 'submit update' ); + + is_deeply $mech->form_errors, $test->{field_errors}, 'field errors'; + }; +} + ok $comment->delete, 'deleted comment'; $mech->delete_user('commenter@example.com'); $mech->delete_user('test@example.com'); diff --git a/templates/web/default/report/display.html b/templates/web/default/report/display.html index ea3877b28..6200f7a16 100644 --- a/templates/web/default/report/display.html +++ b/templates/web/default/report/display.html @@ -69,10 +69,10 @@ [% INCLUDE 'errors.html' %] - <form method="post" action="{{ $form_action }}" name="updateForm" id="fieldset"[% IF allow_photo_upload %] enctype="multipart/form-data"[% END %]> + <form method="post" action="[% c.uri_for( '/report/update' ) %]" name="updateForm" id="fieldset"[% IF allow_photo_upload %] enctype="multipart/form-data"[% END %]> <input type="hidden" name="submit_update" value="1"> - <input type="hidden" name="id" value="{{ $input_h{id} }}"> + <input type="hidden" name="id" value="[% problem.id | html %]"> <div> <label for="form_name">[% loc( 'Name:') %]</label> |