source: trunk/lib/Epoll/Controller/Admin.pm @ 373

Last change on this file since 373 was 373, checked in by nanardon, 14 years ago
  • fix syntax
File size: 12.7 KB
Line 
1package Epoll::Controller::Admin;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Epoll::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')->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 $poll = $c->model('Vote')->poll($id);
40    for ($poll->status || '') {
41    /^BEFORE$/ and do {
42        if ($c->req->param('addch')) {
43            $poll->add_choice($c->req->param('addch'))
44                and $poll->commit;
45        } elsif ($c->req->param('delch')) {
46            $poll->delete_choice($c->req->param('delch'))
47                and $poll->commit;
48        } elsif ($c->req->param('polldate')) {
49            if ($c->req->param('dstart')) {
50                $c->req->param('start',
51                    $c->req->param('dstart') . ' ' . ($c->req->param('hstart') || '')
52                );
53            }
54            if ($c->req->param('dend')) {
55                $c->req->param('end',
56                    $c->req->param('dend') . ' ' . ($c->req->param('hend') || '')
57                );
58            }
59            if ($c->req->param('end') && $c->req->param('start')) {
60                if ($poll->check_date_max($c->req->param('start'))) {
61                    if (! $poll->check_date_max($c->req->param('end'), $c->req->param('start'))) {
62                        $c->stash->{dateerror} = $c->localize('The vote finish before starting');
63                        next;
64                    }
65                } else {
66                    $c->stash->{dateerror} = $c->('The start of the vote must be in the future');
67                    next;
68                }
69            } elsif ($c->req->param('end') || $c->req->param('start')) {
70                $c->stash->{dateerror} =  $c->localize('You must define a start and a end date');
71                next;
72            }
73            $poll->param(
74                map { $_ => ($c->req->param($_) || undef) }
75                grep { exists($c->req->params->{$_}) }
76                qw(start end)
77            ) and $poll->commit;
78        } elsif ($c->req->param('pollparam')) {
79            $poll->param(
80                map { $_ => ($c->req->param($_) || undef) }
81                qw(label description procedure
82                no_mail_confirm_vote no_ballot_id_voting no_mail_ballot_id
83                )
84            ) and $poll->commit;
85        } elsif ($c->req->param('pollballot')) {
86            $poll->param(
87                map { $_ => ($c->req->param($_) || undef) }
88                qw(choice_count free_choice elected_count elected_choice
89                )
90            ) and $poll->commit;
91        } elsif ($c->req->param('encrypted')) {
92            my $passphrase = $c->req->param('passphrase') ||
93                $c->session->{'vpass' . $c->stash->{voteid}};
94            $poll->gen_poll_keys($passphrase)
95                and $poll->commit;
96        } elsif ($c->req->param('notcrypted')) {
97            $poll->param(public_key => undef, private_key => undef)
98                and $poll->commit;
99        }
100    };
101
102    /^AFTER$/ and do {
103        if ($c->req->param('mapfrom')) {
104            $poll->map_value(
105                $c->req->param('mapfrom'),
106                $c->req->param('mapto') || undef,
107            );
108        }
109        foreach my $bid ($c->req->param('bsubvalid')) {
110            if (!$c->req->param($bid)) {
111                next;
112            } elsif($c->req->param($bid) eq 'invalid') {
113                $poll->ballot($bid)->mark_invalid(1);
114                $poll->commit;
115            } elsif($c->req->param($bid) eq 'valid') {
116                $poll->ballot($bid)->mark_invalid(0);
117                $poll->commit;
118            }
119        }
120        if ($c->req->param('decryptballot')) {
121            my $passphrase = $c->req->param('passphrase') ||
122                $c->session->{'vpass' . $c->stash->{voteid}};
123                $c->model('Vote')->poll($c->stash->{voteid})->decrypted_ballots(
124                    $passphrase
125                );
126        }
127        if ($c->req->param('publish')) {
128            $c->model('Vote')->poll($c->stash->{voteid})->compute_results;
129            $c->model('Vote')->commit;
130            $c->res->redirect($c->uri_for('/vote', $poll->uid));
131        }   
132    };
133    }
134}
135
136sub ballotfilter : Private {
137    my ($self, $c) = @_;
138
139    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
140    $c->req->params->{ballotvalue} = 's:needvalid'
141        if (!$c->req->param('ballotvalue'));
142
143    for ($c->req->param('ballotvalue') || '') {
144        /^v:(.*)/ and $c->stash->{ballotfilter} =
145            [ $poll->ballot_by_value($1) ];
146        /^s:invalid$/ and $c->stash->{ballotfilter} =
147            [ $poll->list_ballot_invalid ];
148        /^s:valid$/ and $c->stash->{ballotfilter} =
149            [ $poll->list_ballot_valid ];
150        /^s:needvalid$/ and $c->stash->{ballotfilter} =
151            [ $poll->list_ballot_needvalid ];
152    }
153}
154
155sub auth : Private {
156    my ($self, $c) = @_;
157    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
158    my $password = $c->session->{'vpass' . $poll->uid} ||
159        $c->req->param('vpass' . $poll->uid);
160
161    if (!$poll->auth_poll($password)) {
162        $c->stash->{page}{title} = $poll->info('label') .
163            ': ' . $c->localize('Administrator login');
164        $c->session->{'vpass' . $c->stash->{voteid}} = undef;
165        $c->stash->{template} = 'admin/login.tt';
166        return;
167    }
168    $c->session->{'vpass' . $poll->uid} = $password;
169    $c->session_expire_key( 'vpass' . $poll->uid => 1800 );
170    $c->session->{mypoll}{$poll->uid} = 1;
171    return 1;
172}
173
174sub default : Private {
175    my ( $self, $c, undef, $id ) = @_;
176    $c->stash->{voteid} = $id;
177    my $poll = $c->model('Vote')->poll($id);
178
179    $poll->info or do {
180        $c->res->redirect($c->uri_for('/'));
181        return;
182    };
183
184    $c->forward('auth') or return;
185    $c->forward('modify_poll');
186    $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->localize('Administration');
187}
188
189sub voters: LocalRegex('^(\w+)/voters$') {
190    my ($self, $c) = @_;
191    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
192    my $id = $c->stash->{voteid};
193    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
194
195    $poll->info or do {
196        $c->res->redirect($c->uri_for('/'));
197        return;
198    };
199
200    $c->forward('auth') or return;
201   
202    for ($poll->status || '') {
203        /^(BEFORE|RUNNING)$/ and do {
204            if($c->req->param('delvoting')) {
205                $poll->delete_voting($c->req->param('delvoting'))
206                    and $poll->commit;
207            } elsif ($c->req->param('mail')) {
208                $poll->addupd_voting($c->req->param('mail'), $c->req->param('id'))
209                    and $poll->commit;
210            } elsif($c->req->param('mailpasswd')) {
211                # TODO
212                foreach my $vkey ($poll->list_voting_no_passwd) {
213                    my $voting = $poll->voting($vkey);
214                    my $pass = $voting->gen_passwd;
215                    $c->forward(
216                        q'Epoll::View::Mail', 'render',
217                        [ 'voting_passwd.tt', {
218                            From => $poll->info->{owner},
219                            To => $voting->info->{mail},
220                            Subject => $c->localize("Voting invitation"),
221                            mail => {
222                                voteid => $id,
223                                mail => $voting->info->{mail},
224                                passwd => $pass,
225                            }
226                        } ]
227                    );
228                }
229            }
230        };
231    }
232
233    $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->localize('Administration') . ', ' . $c->localize('voters list');
234}
235
236sub votersimport: LocalRegex('^(\w+)/voters/import$') {
237    my ($self, $c) = @_;
238    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
239    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
240
241    if ($c->req->param('importtype')) {
242        if (my $handle = $poll->import_handle($c->req->param('importtype'))) {
243            $c->stash->{import_handle} = $handle;
244            if ($c->req->param('add_import')) {
245                warn $c->session->{import_param};
246                $handle->load_xml_params($c->session->{import_param});
247                if ($poll->voting_from_import($handle,
248                        ext_auth => $c->req->param('ext_auth') || undef,
249                        desc => $c->req->param('desc') || undef,
250                    )) {
251                    $poll->commit;
252                    $c->res->redirect($c->uri_for($c->stash->{voteid},
253                            'voters'));
254                    return;
255                }
256            } else {
257                $handle->load_web_params($c);
258            }
259            $c->stash->{voters} = [ $handle->fetch_voters() ];
260            $c->{stash}->{importtt} = 'importv/' .
261                lc($c->req->param('importtype')) . '.tt';
262            warn $handle->xml_params;
263            $c->session->{import_param} = $handle->xml_params;
264        } else {
265            warn "Fail to load input handle";
266        }
267    }
268
269}
270
271sub ballot: LocalRegex('^(\w+)/ballot$') {
272    my ($self, $c) = @_;
273    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
274    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
275
276    $poll->info or do {
277        $c->res->redirect($c->uri_for('/'));
278        return;
279    };
280
281    $c->forward('auth') or return;
282    $c->forward('modify_poll');
283    $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->localize('Administration') . ', '. $->l('ballots');
284}
285
286sub date: LocalRegex('^(\w+)/date$') {
287    my ($self, $c) = @_;
288    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
289    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
290
291    $poll->info or do {
292        $c->res->redirect($c->uri_for('/'));
293        return;
294    };
295
296    $c->forward('auth') or return;
297    $c->forward('modify_poll');
298    $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->localize('Administration') . ', '. $c->localize('Date');
299}
300
301sub privatekey : LocalRegex('^(\w+)/privatekey$') {
302    my ($self, $c) = @_;
303    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
304    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
305
306    $poll->info or do {
307        $c->res->redirect($c->uri_for('/'));
308        return;
309    };
310    $c->response->body($poll->info('private_key') || '');
311}
312
313sub validate: LocalRegex('^(\w+)/validate$') {
314    my ($self, $c) = @_;
315    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
316    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
317
318    $poll->info or do {
319        $c->res->redirect($c->uri_for('/'));
320        return;
321    };
322
323    $c->forward('auth') or return;
324    $c->forward('modify_poll');
325    $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->localize('Administration') . ', ' . $c->localize('Validation');
326
327    $c->forward('ballotfilter');
328}
329
330sub delete: LocalRegex('^(\w+)/delete$') {
331    my ($self, $c) = @_;
332    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
333    my $poll = $c->model('Vote')->results($c->stash->{voteid});
334
335    $poll->info or do {
336        $c->res->redirect($c->uri_for('/'));
337        return;
338    };
339
340    $c->forward('auth') or return;
341
342    if ($c->req->param('delete_ballot')) {
343        $c->stash->{template} = 'admin/delete_ballot_confirm.tt';
344        return;
345    }
346   
347    if ($c->req->param('delete_poll')) {
348        $c->stash->{template} = 'admin/delete_poll_confirm.tt';
349        return;
350    }
351
352    if ($c->req->param('delete_ballot_confirm')) {
353        $poll->delete_ballots;
354        $c->response->redirect($c->uri_for($poll->uid));
355    }
356
357    if ($c->req->param('delete_poll_confirm')) {
358        $c->model('Vote')->delete_poll($poll->voteid);
359        $c->response->redirect($c->uri_for('/'));
360        $c->detach;
361    }
362}
363
364sub ajaxsettings: LocalRegex('^(\w+)/ajaxsettings$') {
365    my ($self, $c) = @_;
366    ($c->stash->{voteid}) = @{ $c->req->snippets || [] };
367    my $poll = $c->model('Vote')->poll($c->stash->{voteid});
368
369    $poll->info or do {
370        $c->res->redirect($c->uri_for('/'));
371        return;
372    };
373
374    $c->forward('auth') or return;
375    $c->stash->{ajax} = 1;
376    $c->stash->{pollsettings} = $c->req->params;
377    $c->stash->{template} = 'admin/includes/poll_extra_settings.tt';
378}
379
380sub end : Private {
381    my ($self, $c) = @_;
382    if ($c->res->body) { return }
383    elsif ($c->stash->{latex}) { $c->forward(qw/Epoll::View::Latex/) }
384    elsif ($c->stash->{ajax}) { $c->forward(qw/Epoll::View::Ajax/) }
385    else { $c->forward(qw/Epoll::View::TT/) }
386}
387
388=head1 AUTHOR
389
390Thauvin Olivier
391
392=head1 LICENSE
393
394This library is free software, you can redistribute it and/or modify
395it under the same terms as Perl itself or CeCILL.
396
397=cut
398
3991;
Note: See TracBrowser for help on using the repository browser.