diff options
author | Zarino Zappia <mail@zarino.co.uk> | 2016-08-03 16:15:19 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2016-08-10 09:33:39 +0100 |
commit | e0308e6af55a95f8c6e26e331434ea61259b18b9 (patch) | |
tree | 86d97dd8a4f9a87557db7f518fb291aac30ae652 | |
parent | 0c31945416238d7eb06543c107214adfd4b83734 (diff) |
Feature to create report as body/other user.
This allows users who have the correct permissions to add reports on
behalf of the body or another user.
We enable editing of the email box by default, so that if JavaScript
isn't available, someone can still change the email for the 'another
user' option.
For mysociety/fixmystreetforcouncils#10 and
mysociety/fixmystreetforcouncils#11
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 46 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/Update.pm | 39 | ||||
-rw-r--r-- | perllib/FixMyStreet/DB/Result/User.pm | 8 | ||||
-rw-r--r-- | perllib/FixMyStreet/TestMech.pm | 1 | ||||
-rw-r--r-- | t/app/controller/report_as_other.t | 194 | ||||
-rw-r--r-- | templates/email/default/other-reported.html | 30 | ||||
-rw-r--r-- | templates/email/default/other-reported.txt | 27 | ||||
-rw-r--r-- | templates/email/default/other-updated.html | 26 | ||||
-rw-r--r-- | templates/email/default/other-updated.txt | 16 | ||||
-rw-r--r-- | templates/web/base/report/new/form_user_loggedin.html | 30 | ||||
-rw-r--r-- | templates/web/base/report/update/form_name.html | 18 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/fixmystreet.js | 28 |
12 files changed, 448 insertions, 15 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index bbd27c666..8eabf64c1 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -718,16 +718,31 @@ sub process_user : Private { } } - # The user is already signed in - if ( $c->user_exists ) { + # The user is already signed in. Extra bare block for 'last'. + if ( $c->user_exists ) { { my $user = $c->user->obj; + + if ($c->stash->{contributing_as_another_user} = $user->contributing_as('another_user', $c, $c->stash->{bodies})) { + # Act as if not logged in (and it will be auto-confirmed later on) + $report->user(undef); + last; + } + $user->name( Utils::trim_text( $params{name} ) ) if $params{name}; $user->phone( Utils::trim_text( $params{phone} ) ); $user->title( $user_title ) if $user_title; $report->user( $user ); - $report->name( $user->name ); + + if ($c->stash->{contributing_as_body} = $user->contributing_as('body', $c, $c->stash->{bodies})) { + $report->name($user->from_body->name); + $user->name($user->from_body->name) unless $user->name; + $c->stash->{no_reporter_alert} = 1; + } else { + $report->name($user->name); + } + return 1; - } + } } # cleanup the email address my $email = $params{email} ? lc $params{email} : ''; @@ -796,7 +811,11 @@ sub process_report : Private { $report->send_questionnaire( $c->cobrand->send_questionnaires() ); # set some simple bool values (note they get inverted) - $report->anonymous( $params{may_show_name} ? 0 : 1 ); + if ($c->stash->{contributing_as_body}) { + $report->anonymous(0); + } else { + $report->anonymous( $params{may_show_name} ? 0 : 1 ); + } # clean up text before setting $report->title( Utils::cleanup_text( $params{title} ) ); @@ -1075,7 +1094,10 @@ sub save_user_and_report : Private { $report->user->insert(); } $report->confirm(); - + } elsif ( $c->forward('created_as_someone_else', [ $c->stash->{bodies} ]) ) { + # If created on behalf of someone else, we automatically confirm it, + # but we don't want to update the user account + $report->confirm(); } elsif ( !$report->user->in_storage ) { # User does not exist. $c->forward('tokenize_user', [ $report ]); @@ -1111,6 +1133,11 @@ sub save_user_and_report : Private { return 1; } +sub created_as_someone_else : Private { + my ($self, $c, $bodies) = @_; + return $c->stash->{contributing_as_another_user} || $c->stash->{contributing_as_body}; +} + =head2 generate_map Add the html needed to for the map to the stash. @@ -1166,6 +1193,11 @@ sub redirect_or_confirm_creation : Private { if ( $report->confirmed ) { # Subscribe problem reporter to email updates $c->forward( 'create_reporter_alert' ); + if ($c->stash->{contributing_as_another_user}) { + $c->send_email( 'other-reported.txt', { + to => [ [ $report->user->email, $report->name ] ], + } ); + } $c->log->info($report->user->id . ' was logged in, showing confirmation page for ' . $report->id); $c->stash->{created_report} = 'loggedin'; $c->stash->{template} = 'tokens/confirm_problem.html'; @@ -1184,6 +1216,8 @@ sub redirect_or_confirm_creation : Private { sub create_reporter_alert : Private { my ( $self, $c ) = @_; + return if $c->stash->{no_reporter_alert}; + my $problem = $c->stash->{report}; my $alert = $c->model('DB::Alert')->find_or_create( { user => $problem->user, diff --git a/perllib/FixMyStreet/App/Controller/Report/Update.pm b/perllib/FixMyStreet/App/Controller/Report/Update.pm index d03797f56..705e6ee99 100644 --- a/perllib/FixMyStreet/App/Controller/Report/Update.pm +++ b/perllib/FixMyStreet/App/Controller/Report/Update.pm @@ -109,8 +109,15 @@ sub process_user : Private { my $update = $c->stash->{update}; - if ( $c->user_exists ) { + # Extra block to use 'last' + if ( $c->user_exists ) { { my $user = $c->user->obj; + + if ($c->stash->{contributing_as_another_user} = $user->contributing_as('another_user', $c, $update->problem->bodies_str)) { + # Act as if not logged in (and it will be auto-confirmed later on) + last; + } + my $name = $c->get_param('name'); $user->name( Utils::trim_text( $name ) ) if $name; my $title = $c->get_param('fms_extra_title'); @@ -119,8 +126,14 @@ sub process_user : Private { $user->title( Utils::trim_text( $title ) ); } $update->user( $user ); + + # Just in case, make sure the user will have a name + if ($c->stash->{contributing_as_body}) { + $user->name($user->from_body->name) unless $user->name; + } + return 1; - } + } } # Extract all the params to a hash to make them easier to work with my %params = map { $_ => $c->get_param($_) } @@ -254,16 +267,23 @@ sub process_update : Private { Utils::cleanup_text( $params{update}, { allow_multiline => 1 } ); my $name = Utils::trim_text( $params{name} ); - my $anonymous = $c->get_param('may_show_name') ? 0 : 1; $params{reopen} = 0 unless $c->user && $c->user->id == $c->stash->{problem}->user->id; my $update = $c->stash->{update}; $update->text($params{update}); - $update->name($name); + $update->mark_fixed($params{fixed} ? 1 : 0); $update->mark_open($params{reopen} ? 1 : 0); - $update->anonymous($anonymous); + + $c->stash->{contributing_as_body} = $c->user_exists && $c->user->contributing_as('body', $c, $update->problem->bodies_str); + if ($c->stash->{contributing_as_body}) { + $update->name($c->user->from_body->name); + $update->anonymous(0); + } else { + $update->name($name); + $update->anonymous($c->get_param('may_show_name') ? 0 : 1); + } if ( $params{state} ) { $params{state} = 'fixed - council' @@ -431,6 +451,10 @@ sub save_update : Private { $update->user->insert(); } $update->confirm(); + } elsif ( $c->forward('/report/new/created_as_someone_else', [ $update->problem->bodies_str ]) ) { + # If created on behalf of someone else, we automatically confirm it, + # but we don't want to update the user account + $update->confirm(); } elsif ( !$update->user->in_storage ) { # User does not exist. $c->forward('tokenize_user', [ $update ]); @@ -474,6 +498,11 @@ sub redirect_or_confirm_creation : Private { if ( $update->confirmed ) { $c->forward( 'update_problem' ); $c->forward( 'signup_for_alerts' ); + if ($c->stash->{contributing_as_another_user}) { + $c->send_email( 'other-updated.txt', { + to => [ [ $update->user->email, $update->name ] ], + } ); + } $c->stash->{template} = 'tokens/confirm_update.html'; return 1; } diff --git a/perllib/FixMyStreet/DB/Result/User.pm b/perllib/FixMyStreet/DB/Result/User.pm index 65dd1dab1..7d1785c4b 100644 --- a/perllib/FixMyStreet/DB/Result/User.pm +++ b/perllib/FixMyStreet/DB/Result/User.pm @@ -243,6 +243,14 @@ sub has_permission_to { return $permission ? 1 : undef; } +sub contributing_as { + my ($self, $other, $c, $bodies) = @_; + $bodies = join(',', keys %$bodies) if ref $bodies eq 'HASH'; + $c->log->error("Bad data $bodies passed to contributing_as") if ref $bodies; + my $form_as = $c->get_param('form_as') || ''; + return 1 if $form_as eq $other && $self->has_permission_to("contribute_as_$other", $bodies); +} + sub adopt { my ($self, $other) = @_; diff --git a/perllib/FixMyStreet/TestMech.pm b/perllib/FixMyStreet/TestMech.pm index 5f4a6ceed..45f32f46a 100644 --- a/perllib/FixMyStreet/TestMech.pm +++ b/perllib/FixMyStreet/TestMech.pm @@ -165,6 +165,7 @@ sub delete_user { } $_->delete for $user->comments; $_->delete for $user->admin_logs; + $_->delete for $user->user_body_permissions; $user->delete; return 1; diff --git a/t/app/controller/report_as_other.t b/t/app/controller/report_as_other.t new file mode 100644 index 000000000..505a1bf6b --- /dev/null +++ b/t/app/controller/report_as_other.t @@ -0,0 +1,194 @@ +use strict; +use warnings; +use Test::More; +use LWP::Protocol::PSGI; + +use t::Mock::MapIt; +use FixMyStreet::TestMech; +use FixMyStreet::App; + +# disable info logs for this test run +FixMyStreet::App->log->disable('info'); +END { FixMyStreet::App->log->enable('info'); } + +my $mech = FixMyStreet::TestMech->new; + +my $body = $mech->create_body_ok(2245, 'Wiltshire Council'); +my $contact1 = $mech->create_contact_ok( body_id => $body->id, category => 'Street lighting', email => 'highways@example.com' ); +my $contact2 = $mech->create_contact_ok( body_id => $body->id, category => 'Potholes', email => 'potholes@example.com' ); + +my $test_email = 'body-user@example.net'; +my $user = $mech->log_in_ok($test_email); +$user->update({ from_body => $body->id, name => 'Body User' }); + +my ($report_to_update) = $mech->create_problems_for_body(1, $body->id, 'Title'); + +subtest "Body user, no permissions, no special reporting tools shown" => sub { + start_report(); + dropdown_shown(0); + start_update(); + dropdown_shown(0, 'updateForm'); +}; + +subtest "Body user, has permission to add report as council" => sub { + my $report = add_report( + 'contribute_as_body', + form_as => 'body', + title => "Test Report", + detail => 'Test report details.', + category => 'Street lighting', + ); + is $report->name, 'Wiltshire Council', 'report name is body'; + is $report->user->name, 'Body User', 'user name unchanged'; + is $report->user->id, $user->id, 'user matches'; + is $report->anonymous, 0, 'report not anonymous'; +}; + +my @users; +subtest "Body user, has permission to add report as another user" => sub { + my $report = add_report( + 'contribute_as_another_user', + form_as => 'another_user', + title => "Test Report", + detail => 'Test report details.', + category => 'Potholes', + name => 'Another User', + email => 'another@example.net', + ); + is $report->name, 'Another User', 'report name is given name'; + is $report->user->name, 'Another User', 'user name matches'; + is $report->user->email, 'another@example.net', 'user email correct'; + isnt $report->user->id, $user->id, 'user does not match'; + like $mech->get_text_body_from_email, qr/Your report to Wiltshire Council has been logged/; + push @users, $report->user; +}; + +subtest "Body user, has permission to add report as another (existing) user" => sub { + $mech->create_user_ok('existing@example.net', name => 'Existing User'); + my $report = add_report( + 'contribute_as_another_user', + form_as => 'another_user', + title => "Test Report", + detail => 'Test report details.', + category => 'Potholes', + name => 'Existing Yooser', + email => 'existing@example.net', + ); + is $report->name, 'Existing Yooser', 'report name is given name'; + is $report->user->name, 'Existing User', 'user name remains same'; + is $report->user->email, 'existing@example.net', 'user email correct'; + isnt $report->user->id, $user->id, 'user does not match'; + like $mech->get_text_body_from_email, qr/Your report to Wiltshire Council has been logged/; + push @users, $report->user; +}; + +subtest "Body user, has permission to add update as council" => sub { + my $update = add_update( + 'contribute_as_body', + form_as => 'body', + update => 'Test Update', + ); + is $update->name, 'Wiltshire Council', 'update name is body'; + is $update->user->name, 'Body User', 'user name unchanged'; + is $update->user->id, $user->id, 'user matches'; + is $update->anonymous, 0, 'update not anonymous'; +}; + +subtest "Body user, has permission to add update as another user" => sub { + my $update = add_update( + 'contribute_as_another_user', + form_as => 'another_user', + update => 'Test Update', + name => 'Another User', + rznvy => 'another2@example.net', + ); + is $update->name, 'Another User', 'update name is given name'; + is $update->user->name, 'Another User', 'user name matches'; + is $update->user->email, 'another2@example.net', 'user email correct'; + isnt $update->user->id, $user->id, 'user does not match'; + like $mech->get_text_body_from_email, qr/Your update has been logged/; + push @users, $update->user; +}; + +subtest "Body user, has permission to add update as another (existing) user" => sub { + my $update = add_update( + 'contribute_as_another_user', + form_as => 'another_user', + update => 'Test Update', + name => 'Existing Yooser', + rznvy => 'existing@example.net', + ); + is $update->name, 'Existing Yooser', 'update name is given name'; + is $update->user->name, 'Existing User', 'user name remains same'; + is $update->user->email, 'existing@example.net', 'user email correct'; + isnt $update->user->id, $user->id, 'user does not match'; + like $mech->get_text_body_from_email, qr/Your update has been logged/; +}; + +done_testing(); + +END { + $mech->delete_body($body); + $mech->delete_user($_) for @users; +} + +sub start_report { + my $permission = shift; + LWP::Protocol::PSGI->register(t::Mock::MapIt->run_if_script, host => 'mapit.uk'); + $_->delete for $user->user_body_permissions; + $user->user_body_permissions->create({ body => $body, permission_type => $permission }) + if $permission; + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/report/new?latitude=51.7549262252&longitude=-1.25617899435'); + }; +} + +sub add_report { + my ($permission, %fields) = @_; + start_report($permission); + FixMyStreet::override_config { + ALLOWED_COBRANDS => [ 'fixmystreet' ], + MAPIT_URL => 'http://mapit.uk/', + }, sub { + dropdown_shown(1); + $mech->submit_form_ok({ + with_fields => \%fields, + }, "submit details"); + }; + $mech->content_contains('Thank you for reporting this issue'); + my $report = FixMyStreet::DB->resultset("Problem")->search(undef, { order_by => { -desc => 'id' } })->first; + ok $report, "Found the report"; + is $report->state, 'confirmed', "report is now confirmed"; + return $report; +} + +sub start_update { + my $permission = shift; + $_->delete for $user->user_body_permissions; + $user->user_body_permissions->create({ body => $body, permission_type => $permission }) + if $permission; + $mech->get_ok('/report/' . $report_to_update->id); +} + +sub add_update { + my ($permission, %fields) = @_; + start_update($permission); + dropdown_shown(1, 'updateForm'); + $mech->submit_form_ok({ + with_fields => \%fields, + }, "submit details"); + $mech->content_contains('Thank you for updating this issue'); + my $update = FixMyStreet::DB->resultset("Comment")->search(undef, { order_by => { -desc => 'id' } })->first; + ok $update, "Found the update"; + is $update->state, 'confirmed', "update is now confirmed"; + return $update; +} + +sub dropdown_shown { + my ($shown, $name) = @_; + is grep({ $_ eq 'form_as' } keys %{$mech->visible_form_values($name)}), $shown, "Dropdown shown = $shown"; +} + diff --git a/templates/email/default/other-reported.html b/templates/email/default/other-reported.html new file mode 100644 index 000000000..5435e735a --- /dev/null +++ b/templates/email/default/other-reported.html @@ -0,0 +1,30 @@ +[% + +email_summary = "Thanks for logging your report"; +email_columns = 2; + +PROCESS '_email_settings.html'; +INCLUDE '_email_top.html'; + +%] + +<th style="[% td_style %][% primary_column_style %]" id="primary_column"> + [% start_padded_box %] + <h1 style="[% h1_style %]">Your report has been logged</h1> + <p style="[% p_style %]">Your report to [% report.body %] has been logged on [% site_name %]. +[% IF c.cobrand.is_council && !c.cobrand.owns_problem( report ) %] +Please note that [% c.cobrand.council_name %] is not responsible for this type +of report, so it will instead be sent to [% report.body %]. +[% END %] + </p> + <p style="margin: 20px auto; text-align: center"> + <a style="[% button_style %]" href="[% cobrand.base_url_for_report(report) %][% report.url %]">View my report</a> + </p> + [% end_padded_box %] +</th> +[% WRAPPER '_email_sidebar.html' object = report %] + <h2 style="[% h2_style %]">[% report.title | html %]</h2> + <p style="[% secondary_p_style %]">[% report.detail | html %]</p> +[% END %] + +[% INCLUDE '_email_bottom.html' %] diff --git a/templates/email/default/other-reported.txt b/templates/email/default/other-reported.txt new file mode 100644 index 000000000..b626e56d9 --- /dev/null +++ b/templates/email/default/other-reported.txt @@ -0,0 +1,27 @@ +Subject: Your report has been logged: [% report.title %] + +Hello [% report.name %], + +Your report to [% report.body %] has been logged on [% site_name %]. + +[% IF c.cobrand.is_council && !c.cobrand.owns_problem( report ) %] +Please note that [% c.cobrand.council_name %] is not responsible for this type +of report, so it will instead be sent to [% report.body %]. +[% END %] + +It is available to view at: + +[% cobrand.base_url_for_report(report) %][% report.url %] + +Your report has the title: + +[% report.title %] + +And details: + +[% report.detail %] + +[% INCLUDE 'signature.txt' %] + +This email was sent automatically, from an unmonitored email account - so +please do not reply to it. diff --git a/templates/email/default/other-updated.html b/templates/email/default/other-updated.html new file mode 100644 index 000000000..fbae37268 --- /dev/null +++ b/templates/email/default/other-updated.html @@ -0,0 +1,26 @@ +[% + +email_summary = "Thanks for logging your update"; +email_columns = 2; + +PROCESS '_email_settings.html'; +INCLUDE '_email_top.html'; + +%] + +<th style="[% td_style %][% primary_column_style %]" id="primary_column"> + [% start_padded_box %] + <h1 style="[% h1_style %]">Your update has been logged</h1> + <p style="[% p_style %]">Your update has been logged on [% site_name %]:</p> + <p style="margin: 20px auto; text-align: center"> + <a style="[% button_style %]" href="[% cobrand.base_url_for_report(problem) %][% problem.url %]#update_[% update.id %]">View my update</a> + </p> + [% end_padded_box %] +</th> +[% WRAPPER '_email_sidebar.html' + object = update + report = problem %] + <p style="[% secondary_p_style %]">[% update.text | html %]</p> +[% END %] + +[% INCLUDE '_email_bottom.html' %] diff --git a/templates/email/default/other-updated.txt b/templates/email/default/other-updated.txt new file mode 100644 index 000000000..4900f6c29 --- /dev/null +++ b/templates/email/default/other-updated.txt @@ -0,0 +1,16 @@ +Subject: Your update has been logged + +Hello [% update.name %], + +Your update has been logged on [% site_name %]: + +[% cobrand.base_url_for_report(problem) %][% problem.url %]#update_[% update.id %] + +Your update reads: + +[% update.text %] + +[% INCLUDE 'signature.txt' %] + +This email was sent automatically, from an unmonitored email account - so +please do not reply to it. diff --git a/templates/web/base/report/new/form_user_loggedin.html b/templates/web/base/report/new/form_user_loggedin.html index 75ff76204..79b8d866e 100644 --- a/templates/web/base/report/new/form_user_loggedin.html +++ b/templates/web/base/report/new/form_user_loggedin.html @@ -1,7 +1,29 @@ <div class="form-box" id="form-box--logged-in-name"> - <label for="form_email">[% loc('Your email') %]</label> - <input disabled type="text" value="[% c.user.email | html %]"> + [% can_contribute_as_another_user = c.user.has_permission_to("contribute_as_another_user", bodies.keys.join(",")) %] + [% can_contribute_as_body = c.user.from_body AND c.user.has_permission_to("contribute_as_body", bodies.keys.join(",")) %] + + [% IF can_contribute_as_another_user OR can_contribute_as_body %] + <label for="form_as">[% loc('Report as') %]</label> + <select id="form_as" class="js-contribute-as" name="form_as"> + <option value="myself" selected>[% loc('Yourself') %]</option> + [% IF can_contribute_as_another_user %] + <option value="another_user">[% loc('Another user') %]</option> + [% END %] + [% IF can_contribute_as_body %] + <option value="body">[% c.user.from_body.name %]</option> + [% END %] + </select> + [% END %] + + <label for="form_email">[% loc('Email address') %]</label> + <input id="form_email" + [%- IF can_contribute_as_another_user OR can_contribute_as_body -%] + name="email" + [%- ELSE -%] + disabled + [%- END -%] + type="text" value="[% c.user.email | html %]"> [% INCLUDE 'report/new/extra_name.html' %] [% PROCESS 'user/_anonymity.html' anonymous = report.anonymous %] @@ -15,7 +37,7 @@ [% IF field_errors.name %] <p class='form-error'>[% field_errors.name %]</p> [% END %] - <input type="text" class="validName" value="[% report.name | html %]" name="name" id="form_name" placeholder="[% loc('Your name') %]"> + <input type="text" class="validName" value="[% report.name | html %]" name="name" id="form_name"> [%# if there is nothing in the name field then set check box as default on form %] <div class="checkbox-group"> @@ -24,7 +46,7 @@ </div> <label for="form_phone">[% loc('Phone number (optional)') %]</label> - <input class="" type="text" value="[% report.user.phone | html %]" name="phone" id="form_phone" placeholder="[% loc('Your phone number') %]"> + <input class="" type="text" value="[% report.user.phone | html %]" name="phone" id="form_phone"> <div class="general-notes"> <p>[% loc('We never show your email address or phone number.') %]</p> diff --git a/templates/web/base/report/update/form_name.html b/templates/web/base/report/update/form_name.html index 4cb3e516c..e65ab1790 100644 --- a/templates/web/base/report/update/form_name.html +++ b/templates/web/base/report/update/form_name.html @@ -2,6 +2,24 @@ [% PROCESS 'user/_anonymity.html' anonymous = update.anonymous %] + [% can_contribute_as_another_user = c.user.has_permission_to("contribute_as_another_user", problem.bodies_str) %] + [% can_contribute_as_body = c.user.from_body AND c.user.has_permission_to("contribute_as_body", problem.bodies_str) %] + + [% IF can_contribute_as_another_user OR can_contribute_as_body %] + <label for="form_as">[% loc('Provide update as') %]</label> + <select id="form_as" class="js-contribute-as" name="form_as"> + <option value="myself" selected>[% loc('Yourself') %]</option> + [% IF can_contribute_as_another_user %] + <option value="another_user">[% loc('Another user') %]</option> + [% END %] + [% IF can_contribute_as_body %] + <option value="body">[% c.user.from_body.name %]</option> + [% END %] + </select> + <label for="form_email">[% loc('Email address') %]</label> + <input name="rznvy" id="form_email" type="text" value="[% c.user.email | html %]"> + [% END %] + <label for="form_name">[% loc('Name') %]</label> [% IF field_errors.name %] <p class='form-error'>[% field_errors.name %]</p> diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js index acdfc869d..52f098b07 100644 --- a/web/cobrands/fixmystreet/fixmystreet.js +++ b/web/cobrands/fixmystreet/fixmystreet.js @@ -397,6 +397,34 @@ $.extend(fixmystreet.set_up, { $('#mapForm').removeAttr('novalidate'); }, + contribute_as: function() { + $('.js-contribute-as').on('change', function(){ + var opt = this.options[this.selectedIndex], + val = opt.value, + txt = opt.text; + var $emailInput = $('#form_email'); + var $nameInput = $('#form_name'); + var $showNameCheckbox = $('#form_may_show_name'); + var $addAlertCheckbox = $('#form_add_alert'); + if (val === 'myself') { + $emailInput.val($emailInput.prop('defaultValue')).prop('disabled', true); + $nameInput.val($nameInput.prop('defaultValue')).prop('disabled', false); + $showNameCheckbox.prop('checked', false).prop('disabled', false); + $addAlertCheckbox.prop('checked', true).prop('disabled', false); + } else if (val === 'another_user') { + $emailInput.val('').prop('disabled', false); + $nameInput.val('').prop('disabled', false); + $showNameCheckbox.prop('checked', false).prop('disabled', false); + $addAlertCheckbox.prop('checked', true).prop('disabled', false); + } else if (val === 'body') { + $emailInput.val('-').prop('disabled', true); + $nameInput.val(txt).prop('disabled', true); + $showNameCheckbox.prop('checked', true).prop('disabled', true); + $addAlertCheckbox.prop('checked', false).prop('disabled', true); + } + }).change(); + }, + on_resize: function() { var last_type; $(window).on('resize', function() { |