aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Admin/ResponsePriorities.pm
blob: 5e2908290df4ba9088075510589161581384b31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package FixMyStreet::App::Controller::Admin::ResponsePriorities;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

use FixMyStreet::App::Form::ResponsePriority;

sub auto :Private {
    my ($self, $c) = @_;

    my $user = $c->user;
    if ($user->is_superuser) {
        $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 index : Path : Args(0) {
    my ( $self, $c ) = @_;

    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 ],
    );
}

sub body :PathPart('admin/responsepriorities') :Chained :CaptureArgs(1) {
    my ($self, $c, $body_id) = @_;

    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->detach( '/page_error_404_not_found' ) unless $c->stash->{body};
}

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 item :PathPart('') :Chained('body') :CaptureArgs(1) {
    my ($self, $c, $id) = @_;

    my $obj = $c->stash->{rs}->find($id)
        or $c->detach('/page_error_404_not_found', []);
    $c->stash(obj => $obj);
}

sub edit :PathPart('') :Chained('item') :Args(0) {
    my ($self, $c) = @_;
    return $self->form($c, $c->stash->{obj});
}

sub form {
    my ($self, $c, $priority) = @_;

    # 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;

1;