source: trunk/lib/Vote/Controller/Admin.pm @ 171

Last change on this file since 171 was 169, checked in by nanardon, 15 years ago
  • split ballot admin page into subsection
File size: 5.1 KB
Line 
1package Vote::Controller::Admin;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Vote::Controller::Admin - Catalyst Controller
10
11=head1 DESCRIPTION
12
13Catalyst Controller.
14
15=head1 METHODS
16
17=cut
18
19
20=head2 index
21
22=cut
23
24sub begin : Private {
25    my ( $self, $c, @args ) = @_;
26    $c->model('Vote')->db->rollback;
27
28    warn join('-', @args);
29}
30
31sub index : Private {
32    my ( $self, $c ) = @_;
33
34    $c->res->redirect($c->uri_for('/'));
35}
36
37sub modify_poll : Private {
38    my ( $self, $c) = @_;
39    my $id = $c->stash->{voteid};
40
41    my $vote = $c->model('Vote');
42    for ($vote->vote_status($id) || '') {
43    /^BEFORE$/ and do {
44        if ($c->req->param('addch')) {
45            $vote->vote_add_choice($id, $c->req->param('addch'))
46                and $vote->db->commit;
47        } elsif ($c->req->param('delch')) {
48            $vote->delete_choice($c->req->param('delch'))
49                and $vote->db->commit;
50        } elsif ($c->req->param('pollparam')) {
51            if ($c->req->param('dstart')) {
52                $c->req->param('start',
53                    $c->req->param('dstart') . ' ' . ($c->req->param('hstart') || '')
54                );
55            }
56            if ($c->req->param('dend')) {
57                $c->req->param('end',
58                    $c->req->param('dend') . ' ' . ($c->req->param('hend') || '')
59                );
60            }
61            $vote->vote_param(
62                $id,
63                map { $_ => ($c->req->param($_) || undef) }
64                grep { exists($c->req->params->{$_}) }
65                qw(label description start end choice_count free_choice)
66            ) and $vote->db->commit;
67        }
68    };
69
70    /^(BEFORE|RUNNING)$/ and do {
71        if (my ($upload) = $c->req->upload('votinglist')) {
72            $vote->voting_from_file(
73                $id,
74                $upload->fh,
75                $c->req->param('delete'),
76            ) and $vote->db->commit;
77        } elsif($c->req->param('delvoting')) {
78            $vote->delete_voting($c->req->param('delvoting'))
79                and $vote->db->commit;
80        } elsif ($c->req->param('mail')) {
81            $vote->addupd_voting($id, $c->req->param('mail'), $c->req->param('id'))
82                and $vote->db->commit;
83        } elsif($c->req->param('mailpasswd')) {
84            $vote->mail_passwd_ifnul($id, {
85                voteurl => $c->uri_for('/ballot', $id),
86            });
87        }
88    };
89
90    /^AFTER$/ and do {
91        if ($c->req->param('mapfrom') && $c->req->param('mapto')) {
92            $vote->vote_map_value(
93                $id,
94                $c->req->param('mapfrom'),
95                $c->req->param('mapto'),
96            );
97        }
98        foreach my $bid ($vote->list_vote_ballot_needvalid($id)) {
99            if (!$c->req->param($bid)) {
100                next;
101            } elsif($c->req->param($bid) eq 'invalid') {
102                $vote->mark_ballot_invalid($bid, 1);
103                $vote->db->commit;
104            } elsif($c->req->param($bid) eq 'valid') {
105                $vote->mark_ballot_invalid($bid, 0);
106                $vote->db->commit;
107            }
108        }
109    };
110    }
111}
112
113sub auth : Private {
114    my ($self, $c) = @_;
115    my $vote = $c->model('Vote');
116    my $password = $c->session->{vpassword} || $c->req->param('vpassword');
117
118    if (!$c->model('Vote')->auth_poll($c->stash->{voteid}, $password)) {
119        $c->stash->{page}{title} = $vote->vote_info(
120            $c->stash->{voteid}
121        )->{label} . ': Login d\'administration';
122        $c->session->{vpassword} = undef;
123        $c->stash->{template} = 'admin/login.tt';
124        return;
125    }
126    $c->session->{vpassword} = $password;
127    return 1;
128}
129
130sub default : Private {
131    my ( $self, $c, undef, $id ) = @_;
132    $c->stash->{voteid} = $id;
133    my $vote = $c->model('Vote');
134
135    $vote->vote_info($id) or do {
136        $c->res->redirect($c->uri_for('/'));
137        return;
138    };
139
140    $c->forward('auth') or return;
141    $c->forward('modify_poll');
142    $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Administration';
143}
144
145sub voting: LocalRegex('^(\d+)/voting$') {
146    my ($self, $c, $id, @sub) = @_;
147    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
148    my $vote = $c->model('Vote');
149
150    $vote->vote_info($id) or do {
151        $c->res->redirect($c->uri_for('/'));
152        return;
153    };
154
155    $c->forward('auth') or return;
156    $c->forward('modify_poll');
157    $c->stash->{page}{title} = $c->model('Vote')->vote_info(
158        $c->stash->{voteid}
159    )->{label} . ': Administration, liste des electeurs';
160}
161
162sub ballot: LocalRegex('^(\d+)/ballot$') {
163    my ($self, $c, $id, @sub) = @_;
164    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
165    my $vote = $c->model('Vote');
166
167    $vote->vote_info($id) or do {
168        $c->res->redirect($c->uri_for('/'));
169        return;
170    };
171
172    $c->forward('auth') or return;
173    $c->forward('modify_poll');
174    $c->stash->{page}{title} = $c->model('Vote')->vote_info(
175        $c->stash->{voteid}
176    )->{label} . ': Administration, bulletin';
177}
178
179=head1 AUTHOR
180
181Thauvin Olivier
182
183=head1 LICENSE
184
185This library is free software, you can redistribute it and/or modify
186it under the same terms as Perl itself or CeCILL.
187
188=cut
189
1901;
Note: See TracBrowser for help on using the repository browser.