1 | package Vote::Controller::Ballot; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Controller'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | Vote::Controller::Ballot - Catalyst Controller |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Controller. |
---|
14 | |
---|
15 | =head1 METHODS |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | |
---|
20 | =head2 index |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub index : Private { |
---|
25 | my ( $self, $c ) = @_; |
---|
26 | } |
---|
27 | |
---|
28 | sub default : Private { |
---|
29 | my ( $self, $c, undef, $id ) = @_; |
---|
30 | |
---|
31 | $c->stash->{voteid} = $id; |
---|
32 | |
---|
33 | my $uid = $c->session->{uid} || $c->req->param('uid'); |
---|
34 | my $password = $c->session->{password} || $c->req->param('password'); |
---|
35 | |
---|
36 | if (!$c->model('Vote')->auth_voting($id, $uid, $password)) { |
---|
37 | $c->delete_session('invalid user/pass'); |
---|
38 | $c->stash->{template} = 'ballot/login.tt'; |
---|
39 | return; |
---|
40 | } |
---|
41 | |
---|
42 | $c->session->{uid} = $uid; |
---|
43 | $c->session->{password} = $password; |
---|
44 | |
---|
45 | # login succeed, but those this user has already voted |
---|
46 | if (my $date = $c->model('Vote')->voting_has_sign($id, $uid)) { |
---|
47 | $c->stash->{uid} = $c->session->{uid}; |
---|
48 | $c->stash->{template} = 'ballot/signed.tt'; |
---|
49 | $c->stash->{signed_date} = $date; |
---|
50 | $c->delete_session('invalid user/pass'); |
---|
51 | return; |
---|
52 | } |
---|
53 | |
---|
54 | my $vote = $c->model('Vote'); |
---|
55 | my %choices; |
---|
56 | foreach ($vote->vote_choices($id)) { |
---|
57 | $choices{$_->{key}} = $_->{label}; |
---|
58 | } |
---|
59 | $c->stash->{choices} = { %choices }; |
---|
60 | $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') }; |
---|
61 | |
---|
62 | if ($c->req->param('confirm')) { |
---|
63 | $vote->register_ballot( |
---|
64 | $uid, |
---|
65 | $id, |
---|
66 | [ grep { $_ } ( |
---|
67 | map { $choices{$_} } $c->req->param('sbal'), |
---|
68 | $c->req->param('fsbal'), |
---|
69 | ) ], |
---|
70 | $c->req->address, |
---|
71 | ); # TODO trap error |
---|
72 | $c->forward('done'); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | sub done : Private { |
---|
77 | my ( $self, $c ) = @_; |
---|
78 | $c->response->body('Vote réussi.'); |
---|
79 | $c->delete_session('Vote terminé'); |
---|
80 | } |
---|
81 | |
---|
82 | =head1 AUTHOR |
---|
83 | |
---|
84 | Thauvin Olivier |
---|
85 | |
---|
86 | =head1 LICENSE |
---|
87 | |
---|
88 | This library is free software, you can redistribute it and/or modify |
---|
89 | it under the same terms as Perl itself. |
---|
90 | |
---|
91 | =cut |
---|
92 | |
---|
93 | 1; |
---|