diff options
author | Matthew Somerville <matthew@mysociety.org> | 2019-11-05 17:13:27 +0000 |
---|---|---|
committer | Matthew Somerville <matthew@mysociety.org> | 2019-11-08 11:58:19 +0000 |
commit | 2dbcea98a4511b8f5830244a14faaceefb5e3c91 (patch) | |
tree | 55752949d7aa8a340b820ecf5630224e54a59774 | |
parent | f5fdcabddad82510fbaff75e13e1f63e0f8d01f8 (diff) |
Factor out admin template code to own controller.
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin.pm | 160 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Admin/Templates.pm | 176 | ||||
-rw-r--r-- | t/app/controller/admin/templates.t | 2 | ||||
-rw-r--r-- | templates/web/base/admin/templates/edit.html (renamed from templates/web/base/admin/template_edit.html) | 2 | ||||
-rw-r--r-- | templates/web/base/admin/templates/index.html (renamed from templates/web/base/admin/templates_index.html) | 2 | ||||
-rw-r--r-- | templates/web/base/admin/templates/view.html (renamed from templates/web/base/admin/templates.html) | 4 | ||||
-rw-r--r-- | templates/web/zurich/admin/templates/edit.html (renamed from templates/web/zurich/admin/template_edit.html) | 2 | ||||
-rw-r--r-- | templates/web/zurich/admin/templates/view.html (renamed from templates/web/zurich/admin/templates.html) | 4 |
8 files changed, 183 insertions, 169 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin.pm b/perllib/FixMyStreet/App/Controller/Admin.pm index b00c34777..04c41351e 100644 --- a/perllib/FixMyStreet/App/Controller/Admin.pm +++ b/perllib/FixMyStreet/App/Controller/Admin.pm @@ -694,166 +694,6 @@ sub alerts_for_report : Private { })->count(); } -sub templates : Path('templates') : Args(0) { - my ( $self, $c ) = @_; - - my $user = $c->user; - - if ($user->is_superuser) { - $c->forward('fetch_all_bodies'); - $c->stash->{template} = 'admin/templates_index.html'; - } elsif ( $user->from_body ) { - $c->forward('load_template_body', [ $user->from_body->id ]); - $c->res->redirect( $c->uri_for( 'templates', $c->stash->{body}->id ) ); - } else { - $c->detach( '/page_error_404_not_found', [] ); - } -} - -sub templates_view : Path('templates') : Args(1) { - my ($self, $c, $body_id) = @_; - - $c->forward('load_template_body', [ $body_id ]); - - my @templates = $c->stash->{body}->response_templates->search( - undef, - { - order_by => 'title' - } - ); - - $c->stash->{response_templates} = \@templates; - - $c->stash->{template} = 'admin/templates.html'; -} - -sub template_edit : Path('templates') : Args(2) { - my ( $self, $c, $body_id, $template_id ) = @_; - - $c->forward('load_template_body', [ $body_id ]); - - my $template; - if ($template_id eq 'new') { - $template = $c->stash->{body}->response_templates->new({}); - } - else { - $template = $c->stash->{body}->response_templates->find( $template_id ) - or $c->detach( '/page_error_404_not_found', [] ); - } - - $c->forward('fetch_contacts'); - my @contacts = $template->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_display, - active => $active_contacts{$_->id}, - email => $_->email, - } } @live_contacts; - $c->stash->{contacts} = \@all_contacts; - - # bare block to use 'last' if form is invalid. - if ($c->req->method eq 'POST') { { - if ($c->get_param('delete_template') && $c->get_param('delete_template') eq _("Delete template")) { - $template->contact_response_templates->delete_all; - $template->delete; - } else { - my @live_contact_ids = map { $_->id } @live_contacts; - my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids; - my %new_contacts = map { $_ => 1 } @new_contact_ids; - for my $contact (@all_contacts) { - $contact->{active} = $new_contacts{$contact->{id}}; - } - - $template->title( $c->get_param('title') ); - my $query = { title => $template->title }; - if ($template->in_storage) { - $query->{id} = { '!=', $template->id }; - } - if ($c->stash->{body}->response_templates->search($query)->count) { - $c->stash->{errors} ||= {}; - $c->stash->{errors}->{title} = _("There is already a template with that title."); - } - - $template->text( $c->get_param('text') ); - $template->state( $c->get_param('state') ); - $template->external_status_code( $c->get_param('external_status_code') ); - - if ( $template->state && $template->external_status_code ) { - $c->stash->{errors} ||= {}; - $c->stash->{errors}->{state} = _("State and external status code cannot be used simultaneously."); - $c->stash->{errors}->{external_status_code} = _("State and external status code cannot be used simultaneously."); - } - - $template->auto_response( $c->get_param('auto_response') && ( $template->state || $template->external_status_code ) ? 1 : 0 ); - if ($template->auto_response) { - my @check_contact_ids = @new_contact_ids; - # If the new template has not specific categories (i.e. it - # applies to all categories) then we need to check each of those - # category ids for existing auto-response templates. - if (!scalar @check_contact_ids) { - @check_contact_ids = @live_contact_ids; - } - my $query = { - 'auto_response' => 1, - 'contact.id' => [ @check_contact_ids, undef ], - -or => { - $template->state ? ('me.state' => $template->state) : (), - $template->external_status_code ? ('me.external_status_code' => $template->external_status_code) : (), - }, - }; - if ($template->in_storage) { - $query->{'me.id'} = { '!=', $template->id }; - } - if ($c->stash->{body}->response_templates->search($query, { - join => { 'contact_response_templates' => 'contact' }, - })->count) { - $c->stash->{errors} ||= {}; - $c->stash->{errors}->{auto_response} = _("There is already an auto-response template for this category/state."); - } - } - - last if $c->stash->{errors}; - - $template->update_or_insert; - $template->contact_response_templates->search({ - contact_id => { -not_in => \@new_contact_ids }, - })->delete; - foreach my $contact_id (@new_contact_ids) { - $template->contact_response_templates->find_or_create({ - contact_id => $contact_id, - }); - } - } - - $c->res->redirect( $c->uri_for( 'templates', $c->stash->{body}->id ) ); - } } - - $c->stash->{response_template} = $template; - - $c->stash->{template} = 'admin/template_edit.html'; -} - -sub load_template_body : Private { - my ($self, $c, $body_id) = @_; - - my $zurich_user = $c->user->from_body && $c->cobrand->moniker eq 'zurich'; - my $has_permission = $c->user->has_body_permission_to('template_edit', $body_id); - - unless ( $zurich_user || $has_permission ) { - $c->detach( '/page_error_404_not_found', [] ); - } - - # Regular users can only view their own body's templates - if ( !$c->user->is_superuser && $body_id ne $c->user->from_body->id ) { - $c->res->redirect( $c->uri_for( 'templates', $c->user->from_body->id ) ); - } - - $c->stash->{body} = $c->model('DB::Body')->find($body_id) - or $c->detach( '/page_error_404_not_found', [] ); -} - sub update_edit : Path('update_edit') : Args(1) { my ( $self, $c, $id ) = @_; diff --git a/perllib/FixMyStreet/App/Controller/Admin/Templates.pm b/perllib/FixMyStreet/App/Controller/Admin/Templates.pm new file mode 100644 index 000000000..1a7381c0b --- /dev/null +++ b/perllib/FixMyStreet/App/Controller/Admin/Templates.pm @@ -0,0 +1,176 @@ +package FixMyStreet::App::Controller::Admin::Templates; +use Moose; +use namespace::autoclean; + +BEGIN { extends 'Catalyst::Controller'; } + +=head1 NAME + +FixMyStreet::App::Controller::Admin::Templates - Catalyst Controller + +=head1 DESCRIPTION + +Admin pages for response templates + +=head1 METHODS + +=cut + +sub index : Path : Args(0) { + my ( $self, $c ) = @_; + + my $user = $c->user; + + if ($user->is_superuser) { + $c->forward('/admin/fetch_all_bodies'); + } elsif ( $user->from_body ) { + $c->forward('load_template_body', [ $user->from_body->id ]); + $c->res->redirect( $c->uri_for_action( '/admin/templates/view', $c->stash->{body}->id ) ); + } else { + $c->detach( '/page_error_404_not_found', [] ); + } +} + +sub view : Path : Args(1) { + my ($self, $c, $body_id) = @_; + + $c->forward('load_template_body', [ $body_id ]); + + my @templates = $c->stash->{body}->response_templates->search( + undef, + { + order_by => 'title' + } + ); + + $c->stash->{response_templates} = \@templates; +} + +sub edit : Path : Args(2) { + my ( $self, $c, $body_id, $template_id ) = @_; + + $c->forward('load_template_body', [ $body_id ]); + + my $template; + if ($template_id eq 'new') { + $template = $c->stash->{body}->response_templates->new({}); + } + else { + $template = $c->stash->{body}->response_templates->find( $template_id ) + or $c->detach( '/page_error_404_not_found', [] ); + } + + $c->forward('/admin/fetch_contacts'); + my @contacts = $template->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_display, + active => $active_contacts{$_->id}, + email => $_->email, + } } @live_contacts; + $c->stash->{contacts} = \@all_contacts; + + # bare block to use 'last' if form is invalid. + if ($c->req->method eq 'POST') { { + if ($c->get_param('delete_template') && $c->get_param('delete_template') eq _("Delete template")) { + $template->contact_response_templates->delete_all; + $template->delete; + } else { + my @live_contact_ids = map { $_->id } @live_contacts; + my @new_contact_ids = grep { $c->get_param("contacts[$_]") } @live_contact_ids; + my %new_contacts = map { $_ => 1 } @new_contact_ids; + for my $contact (@all_contacts) { + $contact->{active} = $new_contacts{$contact->{id}}; + } + + $template->title( $c->get_param('title') ); + my $query = { title => $template->title }; + if ($template->in_storage) { + $query->{id} = { '!=', $template->id }; + } + if ($c->stash->{body}->response_templates->search($query)->count) { + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{title} = _("There is already a template with that title."); + } + + $template->text( $c->get_param('text') ); + $template->state( $c->get_param('state') ); + $template->external_status_code( $c->get_param('external_status_code') ); + + if ( $template->state && $template->external_status_code ) { + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{state} = _("State and external status code cannot be used simultaneously."); + $c->stash->{errors}->{external_status_code} = _("State and external status code cannot be used simultaneously."); + } + + $template->auto_response( $c->get_param('auto_response') && ( $template->state || $template->external_status_code ) ? 1 : 0 ); + if ($template->auto_response) { + my @check_contact_ids = @new_contact_ids; + # If the new template has not specific categories (i.e. it + # applies to all categories) then we need to check each of those + # category ids for existing auto-response templates. + if (!scalar @check_contact_ids) { + @check_contact_ids = @live_contact_ids; + } + my $query = { + 'auto_response' => 1, + 'contact.id' => [ @check_contact_ids, undef ], + -or => { + $template->state ? ('me.state' => $template->state) : (), + $template->external_status_code ? ('me.external_status_code' => $template->external_status_code) : (), + }, + }; + if ($template->in_storage) { + $query->{'me.id'} = { '!=', $template->id }; + } + if ($c->stash->{body}->response_templates->search($query, { + join => { 'contact_response_templates' => 'contact' }, + })->count) { + $c->stash->{errors} ||= {}; + $c->stash->{errors}->{auto_response} = _("There is already an auto-response template for this category/state."); + } + } + + last if $c->stash->{errors}; + + $template->update_or_insert; + $template->contact_response_templates->search({ + contact_id => { -not_in => \@new_contact_ids }, + })->delete; + foreach my $contact_id (@new_contact_ids) { + $template->contact_response_templates->find_or_create({ + contact_id => $contact_id, + }); + } + } + + $c->res->redirect( $c->uri_for_action( '/admin/templates/view', $c->stash->{body}->id ) ); + } } + + $c->stash->{response_template} = $template; +} + +sub load_template_body : Private { + my ($self, $c, $body_id) = @_; + + my $zurich_user = $c->user->from_body && $c->cobrand->moniker eq 'zurich'; + my $has_permission = $c->user->has_body_permission_to('template_edit', $body_id); + + unless ( $zurich_user || $has_permission ) { + $c->detach( '/page_error_404_not_found', [] ); + } + + # Regular users can only view their own body's templates + if ( !$c->user->is_superuser && $body_id ne $c->user->from_body->id ) { + $c->res->redirect( $c->uri_for_action( '/admin/templates/view', $c->user->from_body->id ) ); + } + + $c->stash->{body} = $c->model('DB::Body')->find($body_id) + or $c->detach( '/page_error_404_not_found', [] ); +} + +__PACKAGE__->meta->make_immutable; + +1; diff --git a/t/app/controller/admin/templates.t b/t/app/controller/admin/templates.t index 6944f4b04..3bbd7bf85 100644 --- a/t/app/controller/admin/templates.t +++ b/t/app/controller/admin/templates.t @@ -45,8 +45,6 @@ my $report = FixMyStreet::App->model('DB::Problem')->find_or_create( } ); -$mech->log_in_ok( $superuser->email ); - my $report_id = $report->id; ok $report, "created test report - $report_id"; diff --git a/templates/web/base/admin/template_edit.html b/templates/web/base/admin/templates/edit.html index 87218c7dd..7ce67f96f 100644 --- a/templates/web/base/admin/template_edit.html +++ b/templates/web/base/admin/templates/edit.html @@ -4,7 +4,7 @@ [% UNLESS rt.id %]<h3>[% loc('New template') %]</h3>[% END %] <form method="post" - action="[% c.uri_for('templates', body.id, rt.id || 'new' ) %]" + action="[% c.uri_for_action('/admin/templates/edit', body.id, rt.id || 'new' ) %]" enctype="application/x-www-form-urlencoded" accept-charset="utf-8" class="validate"> diff --git a/templates/web/base/admin/templates_index.html b/templates/web/base/admin/templates/index.html index 40e1be300..9322c6ef6 100644 --- a/templates/web/base/admin/templates_index.html +++ b/templates/web/base/admin/templates/index.html @@ -3,7 +3,7 @@ <ul> [% FOR body IN bodies %] <li> - <a href="[% c.uri_for('templates', body.id) %]">[% body.name %]</a> + <a href="[% c.uri_for_action('/admin/templates/view', body.id) %]">[% body.name %]</a> </li> [% END %] </ul> diff --git a/templates/web/base/admin/templates.html b/templates/web/base/admin/templates/view.html index 21e4eea25..6a1dd9614 100644 --- a/templates/web/base/admin/templates.html +++ b/templates/web/base/admin/templates/view.html @@ -28,12 +28,12 @@ [% IF t.external_status_code %][% t.external_status_code | html %] (external)[% END %] </td> <td> [% IF t.auto_response %]X[% END %] </td> - <td> <a href="[% c.uri_for('templates', body.id, t.id) %]" class="btn">[% loc('Edit') %]</a> </td> + <td> <a href="[% c.uri_for_action('/admin/templates/edit', body.id, t.id) %]" class="btn">[% loc('Edit') %]</a> </td> </tr> [% END %] </tbody> </table> -<a href="[% c.uri_for('templates', body.id, 'new') %]" class="btn">[% loc('New template') %]</a> +<a href="[% c.uri_for_action('/admin/templates/index', body.id, 'new') %]" class="btn">[% loc('New template') %]</a> [% INCLUDE 'admin/footer.html' %] diff --git a/templates/web/zurich/admin/template_edit.html b/templates/web/zurich/admin/templates/edit.html index b6f68106c..8a95f2a04 100644 --- a/templates/web/zurich/admin/template_edit.html +++ b/templates/web/zurich/admin/templates/edit.html @@ -11,7 +11,7 @@ </h3> <form method="post" - action="[% c.uri_for('templates', body.id, rt.id || 'new' ) %]" + action="[% c.uri_for_action('/admin/templates/edit', body.id, rt.id || 'new' ) %]" enctype="application/x-www-form-urlencoded" accept-charset="utf-8" class="validate"> diff --git a/templates/web/zurich/admin/templates.html b/templates/web/zurich/admin/templates/view.html index 2db9e2e34..7b95c8a99 100644 --- a/templates/web/zurich/admin/templates.html +++ b/templates/web/zurich/admin/templates/view.html @@ -17,12 +17,12 @@ <td> [% t.title %] </td> <td> [% t.text %] </td> <td> [% t.created %] </td> - <td> <a href="[% c.uri_for('templates', body.id, t.id) %]" class="btn">[% loc('Edit') %]</a> </td> + <td> <a href="[% c.uri_for_action('/admin/templates/edit', body.id, t.id) %]" class="btn">[% loc('Edit') %]</a> </td> </tr> [% END %] </tbody> </table> -<a href="[% c.uri_for('templates', body.id, 'new') %]" class="btn">[% loc('New template') %]</a> +<a href="[% c.uri_for_action('/admin/templates/edit', body.id, 'new') %]" class="btn">[% loc('New template') %]</a> [% INCLUDE 'admin/footer.html' %] |