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 | if ($c->model('Vote')->vote_status($id) ne 'RUNNING') { |
---|
34 | $c->stash->{template} = 'ballot/closed.tt'; |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | my $mail = $c->session->{mail} || $c->req->param('mail'); |
---|
39 | my $password = $c->session->{password} || $c->req->param('password'); |
---|
40 | |
---|
41 | if (!$c->model('Vote')->auth_voting($id, $mail, $password)) { |
---|
42 | $c->delete_session('invalid user/pass'); |
---|
43 | $c->stash->{template} = 'ballot/login.tt'; |
---|
44 | return; |
---|
45 | } |
---|
46 | |
---|
47 | $c->session->{mail} = $mail; |
---|
48 | $c->session->{password} = $password; |
---|
49 | |
---|
50 | # login succeed, but those this user has already voted |
---|
51 | if (my $date = $c->model('Vote')->voting_has_sign($id, $mail)) { |
---|
52 | $c->stash->{uid} = $c->session->{uid}; |
---|
53 | $c->stash->{template} = 'ballot/signed.tt'; |
---|
54 | $c->stash->{signed_date} = $date; |
---|
55 | $c->delete_session('invalid user/pass'); |
---|
56 | return; |
---|
57 | } |
---|
58 | |
---|
59 | my $vote = $c->model('Vote'); |
---|
60 | my %choices; |
---|
61 | foreach ($vote->vote_choices($id)) { |
---|
62 | $choices{$vote->choice_info($_)->{key}} = $vote->choice_info($_)->{label}; |
---|
63 | } |
---|
64 | $c->stash->{choices} = { %choices }; |
---|
65 | $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') }; |
---|
66 | $c->stash->{fsbal} = [ grep { $_ } map { |
---|
67 | s/^\s+//; |
---|
68 | s/\s+$//; |
---|
69 | s/\s+/ /g; |
---|
70 | lc($_) |
---|
71 | } ($c->req->param('fsbal')) ]; |
---|
72 | |
---|
73 | my @sbalval = grep { $_ } map { $choices{$_} } $c->req->param('sbal'); |
---|
74 | |
---|
75 | if (scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []}) |
---|
76 | > $vote->vote_info($id)->{choice_count}) { |
---|
77 | $c->req->param('ballot', ''); |
---|
78 | $c->stash->{vote_error} = 'Seulement ' . |
---|
79 | $vote->vote_info($id)->{choice_count} . ' choix possible'; |
---|
80 | return; |
---|
81 | } |
---|
82 | { |
---|
83 | my %uniq; |
---|
84 | $uniq{lc($_)} = 1 foreach(@sbalval, @{$c->stash->{fsbal} || []}); |
---|
85 | if (scalar(keys %uniq) != scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})) { |
---|
86 | $c->req->param('ballot', ''); |
---|
87 | $c->stash->{vote_error} = 'Une valeur est en double'; |
---|
88 | return; |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if ($c->req->param('confirm')) { |
---|
93 | $vote->register_ballot( |
---|
94 | $mail, |
---|
95 | $id, |
---|
96 | [ @sbalval ], |
---|
97 | [ @{ $c->stash->{fsbal} } ], |
---|
98 | $c->req->address, |
---|
99 | ); # TODO trap error |
---|
100 | $c->stash->{template} = 'ballot/done.tt'; |
---|
101 | $c->delete_session('Vote terminé'); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | =head1 AUTHOR |
---|
106 | |
---|
107 | Thauvin Olivier |
---|
108 | |
---|
109 | =head1 LICENSE |
---|
110 | |
---|
111 | This library is free software, you can redistribute it and/or modify |
---|
112 | it under the same terms as Perl itself. |
---|
113 | |
---|
114 | =cut |
---|
115 | |
---|
116 | 1; |
---|