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

Last change on this file since 183 was 183, checked in by nanardon, 15 years ago
  • output a pdf report using LaTeX
File size: 6.7 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
29sub index : Private {
30    my ( $self, $c ) = @_;
31
32    $c->res->redirect($c->uri_for('/'));
33}
34
35sub modify_poll : Private {
36    my ( $self, $c) = @_;
37    my $id = $c->stash->{voteid};
38
39    my $vote = $c->model('Vote');
40    my $poll = $c->model('Vote')->poll($id);
41    for ($vote->vote_status($id) || '') {
42    /^BEFORE$/ and do {
43        if ($c->req->param('addch')) {
44            $vote->vote_add_choice($id, $c->req->param('addch'))
45                and $vote->db->commit;
46        } elsif ($c->req->param('delch')) {
47            $vote->delete_choice($c->req->param('delch'))
48                and $vote->db->commit;
49        } elsif ($c->req->param('pollparam')) {
50            if ($c->req->param('dstart')) {
51                $c->req->param('start',
52                    $c->req->param('dstart') . ' ' . ($c->req->param('hstart') || '')
53                );
54            }
55            if ($c->req->param('dend')) {
56                $c->req->param('end',
57                    $c->req->param('dend') . ' ' . ($c->req->param('hend') || '')
58                );
59            }
60            $vote->vote_param(
61                $id,
62                map { $_ => ($c->req->param($_) || undef) }
63                grep { exists($c->req->params->{$_}) }
64                qw(label description start end choice_count free_choice)
65            ) and $vote->db->commit;
66        } elsif ($c->req->param('encrypted')) {
67            $poll->gen_poll_keys()
68                and $vote->db->commit;
69        } elsif ($c->req->param('notcrypted')) {
70            $poll->param(public_key => undef, private_key => undef)
71                and $vote->db->commit;
72        }
73    };
74
75    /^(BEFORE|RUNNING)$/ and do {
76        if (my ($upload) = $c->req->upload('votinglist')) {
77            $vote->voting_from_file(
78                $id,
79                $upload->fh,
80                $c->req->param('delete'),
81            ) and $vote->db->commit;
82        } elsif($c->req->param('delvoting')) {
83            $vote->delete_voting($c->req->param('delvoting'))
84                and $vote->db->commit;
85        } elsif ($c->req->param('mail')) {
86            $vote->addupd_voting($id, $c->req->param('mail'), $c->req->param('id'))
87                and $vote->db->commit;
88        } elsif($c->req->param('mailpasswd')) {
89            # TODO
90            foreach my $vkey ($poll->list_voting_no_passwd) {
91                my $voting = $poll->voting($vkey);
92                my $pass = $voting->gen_passwd;
93                $c->forward(
94                    q'Vote::View::Mail', 'render',
95                    [ 'voting_passwd.tt', {
96                        From => $poll->info->{owner},
97                        To => $voting->info->{mail},
98                        Subject => "Invitation à voter",
99                        mail => {
100                            voteid => $id,
101                            mail => $voting->info->{mail},
102                            passwd => $pass,
103                        }
104                    } ]
105                );
106            }
107        }
108    };
109
110    /^AFTER$/ and do {
111        if ($c->req->param('mapfrom') && $c->req->param('mapto')) {
112            $vote->vote_map_value(
113                $id,
114                $c->req->param('mapfrom'),
115                $c->req->param('mapto'),
116            );
117        }
118        foreach my $bid ($vote->list_vote_ballot_needvalid($id)) {
119            if (!$c->req->param($bid)) {
120                next;
121            } elsif($c->req->param($bid) eq 'invalid') {
122                $vote->mark_ballot_invalid($bid, 1);
123                $vote->db->commit;
124            } elsif($c->req->param($bid) eq 'valid') {
125                $vote->mark_ballot_invalid($bid, 0);
126                $vote->db->commit;
127            }
128        }
129        if ($c->req->param('decryptballot')) {
130            $c->model('Vote')->poll($c->stash->{voteid})->decrypted_ballots(
131                $c->session->{vpassword}
132            );
133        }   
134    };
135    }
136}
137
138sub auth : Private {
139    my ($self, $c) = @_;
140    my $vote = $c->model('Vote');
141    my $password = $c->session->{vpassword} || $c->req->param('vpassword');
142
143    if (!$c->model('Vote')->auth_poll($c->stash->{voteid}, $password)) {
144        $c->stash->{page}{title} = $vote->vote_info(
145            $c->stash->{voteid}
146        )->{label} . ': Login d\'administration';
147        $c->session->{vpassword} = undef;
148        $c->stash->{template} = 'admin/login.tt';
149        return;
150    }
151    $c->session->{vpassword} = $password;
152    return 1;
153}
154
155sub default : Private {
156    my ( $self, $c, undef, $id ) = @_;
157    $c->stash->{voteid} = $id;
158    my $vote = $c->model('Vote');
159
160    $vote->vote_info($id) or do {
161        $c->res->redirect($c->uri_for('/'));
162        return;
163    };
164
165    $c->forward('auth') or return;
166    $c->forward('modify_poll');
167    $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Administration';
168}
169
170sub voting: LocalRegex('^(\d+)/voting$') {
171    my ($self, $c, $id, @sub) = @_;
172    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
173    my $vote = $c->model('Vote');
174
175    $vote->vote_info($id) or do {
176        $c->res->redirect($c->uri_for('/'));
177        return;
178    };
179
180    $c->forward('auth') or return;
181    $c->forward('modify_poll');
182    $c->stash->{page}{title} = $c->model('Vote')->vote_info(
183        $c->stash->{voteid}
184    )->{label} . ': Administration, liste des electeurs';
185}
186
187sub ballot: LocalRegex('^(\d+)/ballot$') {
188    my ($self, $c, $id, @sub) = @_;
189    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
190    my $vote = $c->model('Vote');
191
192    $vote->vote_info($id) or do {
193        $c->res->redirect($c->uri_for('/'));
194        return;
195    };
196
197    $c->forward('auth') or return;
198    $c->forward('modify_poll');
199    $c->stash->{page}{title} = $c->model('Vote')->vote_info(
200        $c->stash->{voteid}
201    )->{label} . ': Administration, bulletin';
202}
203
204sub privatekey : LocalRegex('^(\d+)/privatekey$') {
205    my ($self, $c, $id, @sub) = @_;
206    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
207    my $vote = $c->model('Vote');
208
209    $vote->vote_info($id) or do {
210        $c->res->redirect($c->uri_for('/'));
211        return;
212    };
213    $c->response->body($vote->vote_info($c->stash->{voteid})->{private_key} || '');
214}
215
216sub end : Private {
217    my ($self, $c) = @_;
218    if ($c->res->body) { return }
219    elsif ($c->stash->{latex}) { $c->forward(qw/Vote::View::Latex/) }
220    else { $c->forward(qw/Vote::View::TT/) }
221}
222
223=head1 AUTHOR
224
225Thauvin Olivier
226
227=head1 LICENSE
228
229This library is free software, you can redistribute it and/or modify
230it under the same terms as Perl itself or CeCILL.
231
232=cut
233
2341;
Note: See TracBrowser for help on using the repository browser.