diff options
author | Matthew Somerville <matthew@mysociety.org> | 2019-08-22 14:47:00 +0100 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-08-29 18:12:39 +0100 |
commit | 7a8dcd6b60877c37df6961abf6743981966dc30c (patch) | |
tree | ac41579c7437c47ee64992839d25f1d3fd3b6649 | |
parent | 7570b9d1d95ec5a5ee58df69feab487be78831aa (diff) |
Allow form-disabling per Open311 question answer.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 2 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report/New.pm | 23 | ||||
-rw-r--r-- | t/app/controller/report_new_open311.t | 10 | ||||
-rw-r--r-- | templates/web/base/admin/extra-metadata-form.html | 4 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/admin.js | 1 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/assets.js | 15 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/fixmystreet.js | 18 |
8 files changed, 63 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 295dd04ae..ce0db9630 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Admin improvements: - Add new roles system, to group permissions and apply to users. #2483 - Contact form emails now include user admin links. + - Allow categories/Open311 questions to disable the reporting form. #2599 - New features: - Categories can be listed under more than one group #2475 - OpenID Connect login support. #2523 diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index 22860430c..5373220a7 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -1207,9 +1207,11 @@ sub update_extra_fields : Private { foreach my $j (@vindices) { my $name = $c->get_param("metadata[$i].values[$j].name"); my $key = $c->get_param("metadata[$i].values[$j].key"); + my $disable = $c->get_param("metadata[$i].values[$j].disable"); push(@{$meta->{values}}, { name => $name, key => $key, + $disable ? (disable => 1) : (), }) if $name; } } diff --git a/perllib/FixMyStreet/App/Controller/Report/New.pm b/perllib/FixMyStreet/App/Controller/Report/New.pm index 5e4018e4d..c50093f23 100644 --- a/perllib/FixMyStreet/App/Controller/Report/New.pm +++ b/perllib/FixMyStreet/App/Controller/Report/New.pm @@ -311,13 +311,22 @@ sub by_category_ajax_data : Private { sub disable_form_message : Private { my ( $self, $c ) = @_; - my @descriptions = map { - $_->{description} - } grep { - $_->{disable_form} && $_->{disable_form} eq 'true' - } @{ $c->stash->{category_extras}->{$c->stash->{category}} }; - - return join " ", @descriptions; + my %out; + foreach (@{$c->stash->{category_extras}->{$c->stash->{category}}}) { + if ($_->{disable_form} && $_->{disable_form} eq 'true') { + $out{all} .= ' ' if $out{all}; + $out{all} .= $_->{description}; + } elsif (($_->{variable} || '') eq 'true' && @{$_->{values} || []}) { + foreach my $opt (@{$_->{values}}) { + if ($opt->{disable}) { + $out{message} = $_->{datatype_description}; + $out{code} = $_->{code}; + push @{$out{answers}}, $opt->{key}; + } + } + } + } + return \%out; } =head2 report_import diff --git a/t/app/controller/report_new_open311.t b/t/app/controller/report_new_open311.t index a0f546788..b52e0af18 100644 --- a/t/app/controller/report_new_open311.t +++ b/t/app/controller/report_new_open311.t @@ -293,6 +293,9 @@ subtest "Category extras includes form disabling string" => sub { MAPIT_URL => 'http://mapit.uk/', }, sub { $contact4->push_extra_fields({ description => 'Please ring us!', code => 'ring', variable => 'false', order => '0', disable_form => 'true' }); + $contact4->push_extra_fields({ datatype_description => 'Please please ring', description => 'Is it dangerous?', code => 'dangerous', + variable => 'true', order => '0', values => [ { name => 'Yes', key => 'yes', disable => 1 }, { name => 'No', key => 'no' } ] + }); $contact4->update; for ( { url => '/report/new/ajax?' }, @@ -300,7 +303,12 @@ subtest "Category extras includes form disabling string" => sub { ) { my $json = $mech->get_ok_json($_->{url} . '&latitude=55.952055&longitude=-3.189579'); my $output = $json->{by_category} ? $json->{by_category}{Pothole}{disable_form} : $json->{disable_form}; - is $output, 'Please ring us!'; + is_deeply $output, { + all => 'Please ring us!', + message => 'Please please ring', + code => 'dangerous', + answers => [ 'yes' ], + }; } }; }; diff --git a/templates/web/base/admin/extra-metadata-form.html b/templates/web/base/admin/extra-metadata-form.html index 00c592bee..b82eca966 100644 --- a/templates/web/base/admin/extra-metadata-form.html +++ b/templates/web/base/admin/extra-metadata-form.html @@ -87,6 +87,10 @@ [% loc('Name') %] <input class="js-metadata-option-name" name="metadata[[% outer_loop.index %]].values[[% loop.index %]].name" type="text" value="[% option.name | html %]"> </label> + <label> + [% loc('Disable form') %] + <input class="js-metadata-option-disable" name="metadata[[% outer_loop.index %]].values[[% loop.index %]].disable" type="checkbox"[% ' checked' IF option.disable %]> + </label> <button type="button" class="btn btn--small js-metadata-option-remove hidden-nojs">[% loc('Remove') %]</button> </li> [% END %] diff --git a/web/cobrands/fixmystreet/admin.js b/web/cobrands/fixmystreet/admin.js index 25c7651eb..4ed9b1866 100644 --- a/web/cobrands/fixmystreet/admin.js +++ b/web/cobrands/fixmystreet/admin.js @@ -216,6 +216,7 @@ $(function(){ var prefix = "metadata["+item_index+"].values["+i+"]"; $li.find(".js-metadata-option-key").attr("name", prefix+".key"); $li.find(".js-metadata-option-name").attr("name", prefix+".name"); + $li.find(".js-metadata-option-disable").attr("name", prefix+".disable"); }); } }); diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js index 0ce6c7624..87ceb142a 100644 --- a/web/cobrands/fixmystreet/assets.js +++ b/web/cobrands/fixmystreet/assets.js @@ -1112,7 +1112,20 @@ fixmystreet.message_controller = (function() { return false; } - return $('#form_category').val() == stopper.category; + var category = $('#form_category').val(); + if (category != stopper.category) { + return false; + } + + if (stopper.answers) { + var answer = $('#form_' + stopper.code).val(); + if (OpenLayers.Util.indexOf(stopper.answers, answer) > -1) { + return true; + } + return false; + } else { + return true; + } }); if (!matching.length) { diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js index 40b098973..8bcb71a3a 100644 --- a/web/cobrands/fixmystreet/fixmystreet.js +++ b/web/cobrands/fixmystreet/fixmystreet.js @@ -459,6 +459,12 @@ $.extend(fixmystreet.set_up, { $(".js-hide-if-public-category").hide(); } + if (fixmystreet.message_controller && data && data.disable_form && data.disable_form.answers) { + $('#form_' + data.disable_form.code).on('change.category', function() { + $(fixmystreet).trigger('report_new:category_change'); + }); + } + // remove existing validation rules validation_rules = fixmystreet.validator.settings.rules; $.each(validation_rules, function(rule) { @@ -1259,12 +1265,20 @@ fixmystreet.fetch_reporting_data = function() { if (fixmystreet.message_controller) { fixmystreet.message_controller.unregister_all_categories(); $.each(data.by_category, function(category, details) { - if (details.disable_form) { + if (!details.disable_form) { + return; + } + if (details.disable_form.all) { fixmystreet.message_controller.register_category({ category: category, - message: details.disable_form + message: details.disable_form.all }); } + if (details.disable_form.answers) { + details.disable_form.category = category; + details.disable_form.keep_category_extras = true; + fixmystreet.message_controller.register_category(details.disable_form); + } }); } |