aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Admin/ReportExtraFields.pm
blob: d5ec64698e36c789812fccc76bdfc74757bcf2ea (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
package FixMyStreet::App::Controller::Admin::ReportExtraFields;
use Moose;
use namespace::autoclean;
use List::MoreUtils qw(uniq);

BEGIN { extends 'Catalyst::Controller'; }


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

    $c->forward('/admin/begin');
}

sub index : Path : Args(0) {
    my ( $self, $c ) = @_;

    my @extras = $c->model('DB::ReportExtraFields')->search(
        undef,
        {
            order_by => 'name'
        }
    );

    $c->stash->{extra_fields} = \@extras;
}

sub edit : Path : Args(1) {
    my ( $self, $c, $extra_id ) = @_;

    my $extra;
    if ( $extra_id eq 'new' ) {
        $extra = $c->model('DB::ReportExtraFields')->new({});
    } else {
        $extra = $c->model('DB::ReportExtraFields')->find( $extra_id )
            or $c->detach( '/page_error_404_not_found' );
    }

    if ($c->req->method eq 'POST') {
        $c->forward('/auth/check_csrf_token');

        foreach (qw/name cobrand language/) {
            $extra->$_($c->get_param($_));
        }
        $c->forward('/admin/update_extra_fields', [ $extra ]);

        $extra->update_or_insert;
    }

    $c->forward('/auth/get_csrf_token');
    $c->forward('/admin/fetch_languages');

    my @cobrands = uniq sort map { $_->{moniker} } FixMyStreet::Cobrand->available_cobrand_classes;
    $c->stash->{cobrands} = \@cobrands;

    $c->stash->{extra} = $extra;
}

__PACKAGE__->meta->make_immutable;

1;