diff options
Diffstat (limited to 'perllib/FixMyStreet/App')
5 files changed, 130 insertions, 78 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm b/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm index 5077fe78f..5e2908290 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm @@ -4,98 +4,94 @@ use namespace::autoclean; BEGIN { extends 'Catalyst::Controller'; } +use FixMyStreet::App::Form::ResponsePriority; -sub index : Path : Args(0) { - my ( $self, $c ) = @_; +sub auto :Private { + my ($self, $c) = @_; my $user = $c->user; - if ($user->is_superuser) { - $c->forward('/admin/fetch_all_bodies'); - } elsif ( $user->from_body ) { - $c->forward('load_user_body', [ $user->from_body->id ]); - $c->res->redirect( $c->uri_for( '', $c->stash->{body}->id ) ); - } else { - $c->detach( '/page_error_404_not_found' ); + $c->stash(rs => $c->model('DB::ResponsePriority')->search_rs(undef, { + prefetch => 'body', + order_by => ['body.name', 'me.name'] + })); + } elsif ($user->from_body) { + $c->stash(rs => $user->from_body->response_priorities->search_rs(undef, { + order_by => 'name' + })); } } -sub list : Path : Args(1) { - my ($self, $c, $body_id) = @_; - - $c->forward('load_user_body', [ $body_id ]); +sub index : Path : Args(0) { + my ( $self, $c ) = @_; - my @priorities = $c->stash->{body}->response_priorities->search( - undef, - { - order_by => 'name' - } + if (my $body_id = $c->get_param('body_id')) { + $c->res->redirect($c->uri_for($self->action_for('create'), [ $body_id ])); + $c->detach; + } + if ($c->user->is_superuser) { + $c->forward('/admin/fetch_all_bodies'); + } + $c->stash( + response_priorities => [ $c->stash->{rs}->all ], ); - - $c->stash->{response_priorities} = \@priorities; } -sub edit : Path : Args(2) { - my ( $self, $c, $body_id, $priority_id ) = @_; - - $c->forward('load_user_body', [ $body_id ]); +sub body :PathPart('admin/responsepriorities') :Chained :CaptureArgs(1) { + my ($self, $c, $body_id) = @_; - my $priority; - if ($priority_id eq 'new') { - $priority = $c->stash->{body}->response_priorities->new({}); - } - else { - $priority = $c->stash->{body}->response_priorities->find( $priority_id ) - or $c->detach( '/page_error_404_not_found' ); + my $user = $c->user; + if ($user->is_superuser) { + $c->stash->{body} = $c->model('DB::Body')->find($body_id); + } elsif ($user->from_body && $user->from_body->id == $body_id) { + $c->stash->{body} = $user->from_body; } - $c->forward('/admin/fetch_contacts'); - my @contacts = $priority->contacts->all; - my @live_contacts = $c->stash->{live_contacts}->all; - my %active_contacts = map { $_->id => 1 } @contacts; - my @all_contacts = map { { - id => $_->id, - category => $_->category, - active => $active_contacts{$_->id}, - } } @live_contacts; - $c->stash->{contacts} = \@all_contacts; - - if ($c->req->method eq 'POST') { - $priority->deleted( $c->get_param('deleted') ? 1 : 0 ); - $priority->name( $c->get_param('name') ); - $priority->description( $c->get_param('description') ); - $priority->external_id( $c->get_param('external_id') ); - $priority->is_default( $c->get_param('is_default') ? 1 : 0 ); - $priority->update_or_insert; - - my @live_contact_ids = map { $_->id } @live_contacts; - my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids; - $priority->contact_response_priorities->search({ - contact_id => { -not_in => \@new_contact_ids }, - })->delete; - foreach my $contact_id (@new_contact_ids) { - $priority->contact_response_priorities->find_or_create({ - contact_id => $contact_id, - }); - } - - $c->res->redirect( $c->uri_for( '', $c->stash->{body}->id ) ); - } + $c->detach( '/page_error_404_not_found' ) unless $c->stash->{body}; +} - $c->stash->{response_priority} = $priority; +sub create :Chained('body') :Args(0) { + my ($self, $c) = @_; + + my $priority = $c->stash->{rs}->new_result({ body => $c->stash->{body} }); + return $self->form($c, $priority); } -sub load_user_body : Private { - my ($self, $c, $body_id) = @_; +sub item :PathPart('') :Chained('body') :CaptureArgs(1) { + my ($self, $c, $id) = @_; - my $has_permission = $c->user->has_body_permission_to('responsepriority_edit', $body_id); + my $obj = $c->stash->{rs}->find($id) + or $c->detach('/page_error_404_not_found', []); + $c->stash(obj => $obj); +} - unless ( $has_permission ) { - $c->detach( '/page_error_404_not_found' ); - } +sub edit :PathPart('') :Chained('item') :Args(0) { + my ($self, $c) = @_; + return $self->form($c, $c->stash->{obj}); +} + +sub form { + my ($self, $c, $priority) = @_; - $c->stash->{body} = $c->model('DB::Body')->find($body_id) - or $c->detach( '/page_error_404_not_found' ); + # Otherwise, the form includes contacts for *all* bodies + $c->forward('/admin/fetch_contacts'); + my @all_contacts = map { + { value => $_->id, label => $_->category } + } $c->stash->{live_contacts}->all; + + my $opts = { + field_list => [ + '+contacts' => { options => \@all_contacts }, + ], + body_id => $c->stash->{body}->id, + }; + + my $form = FixMyStreet::App::Form::ResponsePriority->new(%$opts); + $c->stash(template => 'admin/responsepriorities/edit.html', form => $form); + $form->process(item => $priority, params => $c->req->params); + return unless $form->validated; + + $c->response->redirect($c->uri_for($self->action_for('index'))); } __PACKAGE__->meta->make_immutable; diff --git a/perllib/FixMyStreet/App/Controller/Admin/Roles.pm b/perllib/FixMyStreet/App/Controller/Admin/Roles.pm index 15c96a4ed..902ed6255 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/Roles.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/Roles.pm @@ -11,7 +11,10 @@ sub auto :Private { my $user = $c->user; if ($user->is_superuser) { - $c->stash(rs => $c->model('DB::Role')->search_rs({}, { join => 'body', order_by => ['body.name', 'me.name'] })); + $c->stash(rs => $c->model('DB::Role')->search_rs({}, { + prefetch => 'body', + order_by => ['body.name', 'me.name'] + })); } elsif ($user->from_body) { $c->stash(rs => $user->from_body->roles->search_rs({}, { order_by => 'name' })); } @@ -36,7 +39,7 @@ sub index :Path :Args(0) { } sub create :Local :Args(0) { - my ($self, $c, $id) = @_; + my ($self, $c) = @_; my $role = $c->stash->{rs}->new_result({}); return $self->form($c, $role); @@ -60,7 +63,7 @@ sub form { if ($c->get_param('delete_role')) { $role->delete; - $c->response->redirect($c->uri_for($self->action_for('list'))); + $c->response->redirect($c->uri_for($self->action_for('index'))); $c->detach; } @@ -90,7 +93,7 @@ sub form { $form->process(item => $role, params => $c->req->params); return unless $form->validated; - $c->response->redirect($c->uri_for($self->action_for('list'))); + $c->response->redirect($c->uri_for($self->action_for('index'))); } 1; diff --git a/perllib/FixMyStreet/App/Form/ResponsePriority.pm b/perllib/FixMyStreet/App/Form/ResponsePriority.pm new file mode 100644 index 000000000..9182bd7a1 --- /dev/null +++ b/perllib/FixMyStreet/App/Form/ResponsePriority.pm @@ -0,0 +1,50 @@ +package FixMyStreet::App::Form::ResponsePriority; + +use HTML::FormHandler::Moose; +use FixMyStreet::App::Form::I18N; +extends 'HTML::FormHandler::Model::DBIC'; +use namespace::autoclean; + +has 'body_id' => ( isa => 'Int', is => 'ro' ); + +has '+widget_name_space' => ( default => sub { ['FixMyStreet::App::Form::Widget'] } ); +has '+widget_tags' => ( default => sub { { wrapper_tag => 'p' } } ); +has '+item_class' => ( default => 'ResponsePriority' ); +has_field 'name' => ( required => 1 ); +has_field 'description'; +has_field 'external_id' => ( label => 'External ID' ); +has_field 'is_default' => ( + type => 'Checkbox', + option_label => 'Default priority', + do_label => 0, +); +has_field 'deleted' => ( + type => 'Checkbox', + option_label => 'Flag as deleted', + do_label => 0, +); +has_field 'contacts' => ( + type => 'Multiple', + widget => 'CheckboxGroup', + ul_class => 'no-bullets no-margin', + do_label => 0, + do_wrapper => 0, + tags => { inline => 1 }, +); + +before 'update_model' => sub { + my $self = shift; + $self->item->body_id($self->body_id); +}; + +sub _build_language_handle { FixMyStreet::App::Form::I18N->new } + +has '+unique_messages' => ( + default => sub { + { response_priorities_body_id_name_key => "Names must be unique" }; + } +); + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/perllib/FixMyStreet/App/Form/Role.pm b/perllib/FixMyStreet/App/Form/Role.pm index f0711af15..0b0d20703 100644 --- a/perllib/FixMyStreet/App/Form/Role.pm +++ b/perllib/FixMyStreet/App/Form/Role.pm @@ -15,6 +15,7 @@ has_field 'body' => ( type => 'Select', empty_select => 'Select a body', require has_field 'permissions' => ( type => 'Multiple', widget => 'CheckboxGroup', + ul_class => 'permissions-checkboxes', tags => { inline => 1, wrapper_tag => 'fieldset', }, ); diff --git a/perllib/FixMyStreet/App/Form/Widget/Field/CheckboxGroup.pm b/perllib/FixMyStreet/App/Form/Widget/Field/CheckboxGroup.pm index 1dc55e49b..e755f1c11 100644 --- a/perllib/FixMyStreet/App/Form/Widget/Field/CheckboxGroup.pm +++ b/perllib/FixMyStreet/App/Form/Widget/Field/CheckboxGroup.pm @@ -4,11 +4,13 @@ use Moose::Role; with 'HTML::FormHandler::Widget::Field::CheckboxGroup'; use namespace::autoclean; +has ul_class => ( is => 'ro' ); + sub render_element { my ( $self, $result ) = @_; $result ||= $self->result; - my $output = '<ul class="permissions-checkboxes">'; + my $output = '<ul class="' . ($self->ul_class || '') . '">'; foreach my $option ( @{ $self->{options} } ) { if ( my $label = $option->{group} ) { $label = $self->_localize( $label ) if $self->localize_labels; @@ -23,7 +25,7 @@ sub render_element { $output .= qq{</ul>\n</li>}; } else { - $output .= $self->render_option( $option, $result ); + $output .= '<li>' . $self->render_option( $option, $result ) . '</li>'; } } $output .= '</ul>'; |