From 0a9c2ea426e8477ad302582e41756730965ea8c8 Mon Sep 17 00:00:00 2001 From: Dave Arter Date: Wed, 19 Oct 2016 17:46:54 +0100 Subject: Add UI for assigning categories to a user in admin - A user can be assigned to any number of its body's categories - The category ids are stored as a list in the user's extra field --- perllib/FixMyStreet/App/Controller/Admin.pm | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'perllib/FixMyStreet/App/Controller/Admin.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index c4bd5c293..ca2eeb52b 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -1330,6 +1330,17 @@ sub user_edit : Path('user_edit') : Args(1) { } $c->stash->{field_errors} = {}; + + # Update the categories this user operates in + if ( $user->from_body ) { + $c->stash->{body} = $user->from_body; + $c->forward('fetch_contacts'); + my @live_contacts = $c->stash->{live_contacts}->all; + my @live_contact_ids = map { $_->id } @live_contacts; + my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids; + $user->set_extra_metadata('categories', \@new_contact_ids); + } + unless ($user->email) { $c->stash->{field_errors}->{email} = _('Please enter a valid email'); } @@ -1354,6 +1365,22 @@ sub user_edit : Path('user_edit') : Args(1) { '

' . _('Updated!') . '

'; } + if ( $user->from_body ) { + unless ( $c->stash->{body} && $user->from_body->id eq $c->stash->{body}->id ) { + $c->stash->{body} = $user->from_body; + $c->forward('fetch_contacts'); + } + my @contacts = @{$user->get_extra_metadata('categories') || []}; + my %active_contacts = map { $_ => 1 } @contacts; + my @live_contacts = $c->stash->{live_contacts}->all; + my @all_contacts = map { { + id => $_->id, + category => $_->category, + active => $active_contacts{$_->id}, + } } @live_contacts; + $c->stash->{contacts} = \@all_contacts; + } + return 1; } -- cgit v1.2.3