aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller
diff options
context:
space:
mode:
authorDave Arter <davea@mysociety.org>2016-09-06 15:37:42 +0100
committerDave Arter <davea@mysociety.org>2016-09-09 14:14:37 +0100
commit54a2b63fac54d01914fd2bb456da483e6982ee21 (patch)
tree574fe83a0dcf2cba219d32f440f44cde1dbc3132 /perllib/FixMyStreet/App/Controller
parentf8f870be0f9f648b48896cb6411446b7f9e049ce (diff)
Refactor problem response priority into its own model
This moves the response priority values from a cobrand-specific method to a full DB model, and includes management screens in the admin for administering them. For mysociety/fixmystreetforcouncils#66
Diffstat (limited to 'perllib/FixMyStreet/App/Controller')
-rw-r--r--perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm107
-rw-r--r--perllib/FixMyStreet/App/Controller/Report.pm15
2 files changed, 110 insertions, 12 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm b/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm
new file mode 100644
index 000000000..f1110fbf0
--- /dev/null
+++ b/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm
@@ -0,0 +1,107 @@
+package FixMyStreet::App::Controller::Admin::ResponsePriorities;
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'Catalyst::Controller'; }
+
+
+sub begin : Private {
+ my ( $self, $c ) = @_;
+
+ $c->forward('/admin/begin');
+}
+
+sub index : Path : Args(0) {
+ my ( $self, $c ) = @_;
+
+ my $user = $c->user;
+
+ if ($user->is_superuser) {
+ $c->forward('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' );
+ }
+}
+
+sub list : Path : Args(1) {
+ my ($self, $c, $body_id) = @_;
+
+ $c->forward('load_user_body', [ $body_id ]);
+
+ my @priorities = $c->stash->{body}->response_priorities->search(
+ undef,
+ {
+ order_by => 'name'
+ }
+ );
+
+ $c->stash->{response_priorities} = \@priorities;
+}
+
+sub edit : Path : Args(2) {
+ my ( $self, $c, $body_id, $priority_id ) = @_;
+
+ $c->forward('load_user_body', [ $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' );
+ }
+
+ $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->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 => { '!=' => \@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->stash->{response_priority} = $priority;
+}
+
+sub load_user_body : Private {
+ my ($self, $c, $body_id) = @_;
+
+ my $has_permission = $c->user->has_body_permission_to('responsepriority_edit') &&
+ $c->user->from_body->id eq $body_id;
+
+ unless ( $c->user->is_superuser || $has_permission ) {
+ $c->detach( '/page_error_404_not_found' );
+ }
+
+ $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/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm
index 7f1132117..6a7a14b5c 100644
--- a/perllib/FixMyStreet/App/Controller/Report.pm
+++ b/perllib/FixMyStreet/App/Controller/Report.pm
@@ -307,21 +307,10 @@ sub inspect : Private {
$c->stash->{categories} = $c->forward('/admin/categories_for_point');
- # The available priorities for this problem are dependent on the cobrand it
- # was reported to, not necessarily the active cobrand (e.g. inspecting a
- # report on fms.com that was sent to Oxfordshire), so make sure the correct
- # priorities are available for selection.
- if ( $c->cobrand->can('get_body_handler_for_problem') ) {
- my $handler = $c->cobrand->get_body_handler_for_problem($c->stash->{problem});
- if ( $handler->can('problem_response_priorities') ) {
- $c->stash->{priorities} = $handler->problem_response_priorities;
- }
- }
-
if ( $c->get_param('save') || $c->get_param('save_inspected') ) {
$c->forward('/auth/check_csrf_token');
- foreach (qw/priority detailed_location detailed_information traffic_information/) {
+ foreach (qw/detailed_location detailed_information traffic_information/) {
$problem->set_extra_metadata( $_ => $c->get_param($_) );
}
@@ -342,6 +331,8 @@ sub inspect : Private {
$c->forward( '/admin/log_edit', [ $id, 'problem', 'state_change' ] );
}
+ $problem->response_priority( $problem->response_priorities->find({ id => $c->get_param('priority') }) );
+
my $valid = 1;
if ( !$c->forward( '/admin/report_edit_location', [ $problem ] ) ) {
# New lat/lon isn't valid, show an error