aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/App/Controller/Admin/ManifestTheme.pm
blob: 9e3bdc33e326b29760c0c8865cea1265c5b92ee1 (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::ManifestTheme;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller'; }

use FixMyStreet::App::Form::ManifestTheme;

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

    if ( $c->cobrand->moniker eq 'fixmystreet' ) {
        $c->stash(rs => $c->model('DB::ManifestTheme')->search_rs({}), show_all => 1);
    } else {
        $c->stash(rs => $c->model('DB::ManifestTheme')->search_rs({ cobrand => $c->cobrand->moniker }));
    }
}

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

    unless ( $c->stash->{show_all} ) {
        if ( $c->stash->{rs}->count ) {
            $c->res->redirect($c->uri_for($self->action_for('edit'), [ $c->stash->{rs}->first->cobrand ]));
        } else {
            $c->res->redirect($c->uri_for($self->action_for('create')));
        }
        $c->detach;
    }
}

sub item :PathPart('admin/manifesttheme') :Chained :CaptureArgs(1) {
    my ($self, $c, $cobrand) = @_;

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

sub edit :PathPart('') :Chained('item') :Args(0) {
    my ($self, $c) = @_;

    my $form = $self->form($c, $c->stash->{obj});

    # We need to do this after form processing, in case a form POST has deleted
    # an icon.
    $c->stash->{editing_manifest_theme} = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]);

    return $form;
}


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

    unless ( $c->stash->{show_all} || $c->stash->{rs}->count == 0) {
        $c->res->redirect($c->uri_for($self->action_for('edit'), [ $c->stash->{rs}->first->cobrand ]));
        $c->detach;
    }

    my $theme = $c->stash->{rs}->new_result({});
    return $self->form($c, $theme);
}

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

    if ($c->get_param('delete_theme')) {
        $c->forward('_delete_all_manifest_icons');
        $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]);
        $theme->delete;
        $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', 'delete' ]);
        $c->response->redirect($c->uri_for($self->action_for('index')));
        $c->detach;
    }

    my $action = $theme->in_storage ? 'edit' : 'add';
    my $form = FixMyStreet::App::Form::ManifestTheme->new( cobrand => $c->cobrand->moniker );
    $c->stash(template => 'admin/manifesttheme/form.html', form => $form);
    my $params = $c->req->params;
    $params->{icon} = $c->req->upload('icon') if $params->{icon};
    $form->process(item => $theme, params => $params);
    return unless $form->validated;

    $c->forward('/admin/log_edit', [ $theme->id, 'manifesttheme', $action ]);
    $c->forward('/offline/_clear_manifest_theme_cache', [ $theme->cobrand ]);
    $c->response->redirect($c->uri_for($self->action_for('index')));
}

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

    my $theme = $c->forward('/offline/_find_manifest_theme', [ $c->stash->{obj}->cobrand, 1 ]);
    foreach my $icon ( @{ $theme->{icons} } ) {
        unlink FixMyStreet->path_to('web', $icon->{src});
    }
}

1;