diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-02-15 13:32:20 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-02-18 13:14:50 +0000 |
commit | fa33cd37ec6e478356ade8ef3554d01df77287f8 (patch) | |
tree | fce7ac414bd9bb33751700627f52577e784475c4 | |
parent | f0eab21555efca2218d3cc49cf435ea09d03ac4b (diff) |
[Bromley] Allow subcategory attachment to users.
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bromley.pm | 56 | ||||
-rw-r--r-- | t/cobrand/bromley.t | 19 | ||||
-rw-r--r-- | templates/web/bromley/admin/category-checkboxes.html | 18 |
3 files changed, 92 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 9e3919901..3ae8ef140 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -244,5 +244,61 @@ sub open311_contact_meta_override { @$meta = grep { !$ignore{$_->{code}} } @$meta; } +# If any subcategories ticked in user edit admin, make sure they're saved. +sub admin_user_edit_extra_data { + my $self = shift; + my $c = $self->{c}; + my $user = $c->stash->{user}; + + return unless $c->get_param('submit') && $user && $user->from_body; + + $c->stash->{body} = $user->from_body; + my %subcats = $self->subcategories; + my @subcat_ids = map { $_->{key} } map { @$_ } values %subcats; + my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @subcat_ids; + $user->set_extra_metadata('subcategories', \@new_contact_ids); +} + +# Returns a hash of contact ID => list of subcategories +# (which are stored as Open311 attribute questions) +sub subcategories { + my $self = shift; + + my @c = $self->body->contacts->not_deleted->all; + my %subcategories; + foreach my $contact (@c) { + my @fields = @{$contact->get_extra_fields}; + my ($field) = grep { $_->{code} eq 'service_sub_code' } @fields; + $subcategories{$contact->id} = $field->{values} || []; + } + return %subcategories; +} + +# Returns the list of categories, with Bromley subcategories added, +# for the user edit admin interface +sub add_admin_subcategories { + my $self = shift; + my $c = $self->{c}; + + my $user = $c->stash->{user}; + my @subcategories = @{$user->get_extra_metadata('subcategories') || []}; + my %active_contacts = map { $_ => 1 } @subcategories; + + my %subcats = $self->subcategories; + my $contacts = $c->stash->{contacts}; + my @new_contacts; + foreach (@$contacts) { + push @new_contacts, $_; + foreach (@{$subcats{$_->{id}}}) { + push @new_contacts, { + id => $_->{key}, + category => (" " x 4) . $_->{name}, + active => $active_contacts{$_->{key}}, + }; + } + } + return \@new_contacts; +} + 1; diff --git a/t/cobrand/bromley.t b/t/cobrand/bromley.t index 6ee4be282..05581bdfe 100644 --- a/t/cobrand/bromley.t +++ b/t/cobrand/bromley.t @@ -4,7 +4,7 @@ use FixMyStreet::Script::Reports; my $mech = FixMyStreet::TestMech->new; # Create test data -my $user = $mech->create_user_ok( 'bromley@example.com' ); +my $user = $mech->create_user_ok( 'bromley@example.com', name => 'Bromley' ); my $body = $mech->create_body_ok( 2482, 'Bromley Council'); my $contact = $mech->create_contact_ok( body_id => $body->id, @@ -16,6 +16,7 @@ $contact->set_extra_fields( { code => 'easting', datatype => 'number', }, { code => 'northing', datatype => 'number', }, { code => 'service_request_id_ext', datatype => 'number', }, + { code => 'service_sub_code', values => [ { key => 'RED', name => 'Red' }, { key => 'BLUE', name => 'Blue' } ], }, ); $contact->update; my $tfl = $mech->create_body_ok( 2482, 'TfL'); @@ -211,4 +212,20 @@ subtest 'check geolocation overrides' => sub { } }; +subtest 'check special subcategories in admin' => sub { + $mech->create_user_ok('superuser@example.com', is_superuser => 1); + $mech->log_in_ok('superuser@example.com'); + $user->update({ from_body => $body->id }); + FixMyStreet::override_config { + ALLOWED_COBRANDS => 'bromley', + MAPIT_URL => 'http://mapit.uk/', + }, sub { + $mech->get_ok('/admin/user_edit/' . $user->id); + $mech->submit_form_ok({ with_fields => { 'contacts['.$contact->id.']' => 1, 'contacts[BLUE]' => 1 } }); + }; + $user->discard_changes; + is_deeply $user->get_extra_metadata('categories'), [ $contact->id ]; + is_deeply $user->get_extra_metadata('subcategories'), [ 'BLUE' ]; +}; + done_testing(); diff --git a/templates/web/bromley/admin/category-checkboxes.html b/templates/web/bromley/admin/category-checkboxes.html new file mode 100644 index 000000000..ce4b489e8 --- /dev/null +++ b/templates/web/bromley/admin/category-checkboxes.html @@ -0,0 +1,18 @@ + <p> + <strong>[% loc('Categories:') %]</strong> + </p> + <ul> + <li> + [% loc('Select:') %] + <a href="#" data-select-all>[% loc('all') %]</a> / + <a href="#" data-select-none>[% loc('none') %]</a> + </li> + [% FOR contact IN c.cobrand.add_admin_subcategories # Bromley-specific function %] + <li> + <label title="[% contact.email | html %]"> + <input type="checkbox" name="contacts[[% contact.id %]]" [% 'checked' IF contact.active %]/> + [% contact.category %] + </label> + </li> + [% END %] + </ul> |