1 | package Epoll::Controller::Ballot; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Controller'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | Epoll::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 begin : Private { |
---|
25 | my ( $self, $c ) = @_; |
---|
26 | $c->model('Vote')->rollback; |
---|
27 | } |
---|
28 | |
---|
29 | sub index : Private { |
---|
30 | my ( $self, $c ) = @_; |
---|
31 | |
---|
32 | $c->res->redirect($c->uri_for('/')); |
---|
33 | } |
---|
34 | |
---|
35 | sub default : Private { |
---|
36 | my ( $self, $c, undef, $id ) = @_; |
---|
37 | |
---|
38 | $c->stash->{voteid} = $id; |
---|
39 | my $poll = $c->model('Vote')->poll($id); |
---|
40 | my $puid = $poll->uid; |
---|
41 | |
---|
42 | if ($poll->status ne 'RUNNING') { |
---|
43 | $c->stash->{template} = 'ballot/closed.tt'; |
---|
44 | return; |
---|
45 | } |
---|
46 | |
---|
47 | my $mail = $c->session->{$puid}{mail} || $c->req->param('mail'); |
---|
48 | my $password = $c->session->{$puid}{password} || $c->req->param('password'); |
---|
49 | |
---|
50 | if (!$c->model('Vote')->poll($id)->auth_voting($mail, $password)) { |
---|
51 | $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->l('Login'); |
---|
52 | $c->session->{$puid} = undef; |
---|
53 | $c->stash->{template} = 'ballot/login.tt'; |
---|
54 | if (defined($c->req->param('password'))) { |
---|
55 | # show login failure: |
---|
56 | $c->stash->{login_failure} = 1; |
---|
57 | } |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | $c->session_expire_key( $puid => 600 ); |
---|
62 | $c->session->{$puid}{mail} = $mail; |
---|
63 | $c->session->{$puid}{password} = $password; |
---|
64 | $c->session->{mypoll}{$puid} ||= 0; |
---|
65 | |
---|
66 | $c->stash->{page}{title} = $poll->info('label') . ': ' . $c->l('Bulletin'); |
---|
67 | |
---|
68 | # login succeed, but those this user has already voted |
---|
69 | if (my $date = $poll->voting_has_sign($mail)) { |
---|
70 | $c->stash->{mail} = $c->session->{$puid}{mail}; |
---|
71 | $c->stash->{template} = 'ballot/signed.tt'; |
---|
72 | $c->stash->{signed_date} = $date; |
---|
73 | $c->session->{$puid} = undef; |
---|
74 | return; |
---|
75 | } |
---|
76 | |
---|
77 | my $vote = $c->model('Vote'); |
---|
78 | my %choices; |
---|
79 | foreach ($poll->choices_keys) { |
---|
80 | $choices{$_} = $poll->choice($_)->info->{label}; |
---|
81 | } |
---|
82 | $c->stash->{choices} = { %choices }; |
---|
83 | $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') }; |
---|
84 | $c->stash->{fsbal} = [ grep { $_ } map { |
---|
85 | s/^\s+//; |
---|
86 | s/\s+$//; |
---|
87 | s/\s+/ /g; |
---|
88 | lc($_) |
---|
89 | } ($c->req->param('fsbal')) ]; |
---|
90 | $c->stash->{esbal} = [ grep { $_ } $c->req->param('esbal') ]; |
---|
91 | |
---|
92 | $c->request->parameters->{fsbal} = $c->stash->{fsbal}; |
---|
93 | |
---|
94 | my @sbalval = grep { $_ } map { lc($choices{$_} || '') } $c->req->param('sbal'); |
---|
95 | |
---|
96 | my @ballot = grep { $_ } ( |
---|
97 | @sbalval, |
---|
98 | @{$c->stash->{fsbal} || []}, |
---|
99 | @{$c->stash->{esbal} || []}, |
---|
100 | ); |
---|
101 | if (scalar(@ballot) > $poll->info('choice_count')) { |
---|
102 | $c->req->parameters->{'ballot'} = ''; |
---|
103 | $c->stash->{vote_error} = $c->l('Only ') . |
---|
104 | $poll->info('choice_count') . ' ' . $c->l('possible choices'); |
---|
105 | return; |
---|
106 | } |
---|
107 | { |
---|
108 | my %uniq; |
---|
109 | foreach(@ballot) { |
---|
110 | $uniq{lc($_)} ||= 0; # avoid undef |
---|
111 | $uniq{lc($_)}++; |
---|
112 | } |
---|
113 | my @twices = grep { $uniq{$_} > 1 } (sort keys %uniq); |
---|
114 | if (scalar(@twices)) { |
---|
115 | $c->req->parameters->{'ballot'} = ''; |
---|
116 | $c->stash->{vote_error} = $c->l('One or more values are duplicated:') . |
---|
117 | join(' ,', map { qq'"$_"' } @twices); |
---|
118 | return; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | if ($c->req->param('confirm')) { |
---|
123 | $c->stash->{ballotid} = $poll->register_ballot( |
---|
124 | $mail, |
---|
125 | [ @ballot ], |
---|
126 | $c->req->address, |
---|
127 | ); # TODO trap error |
---|
128 | $c->forward( |
---|
129 | q'Epoll::View::Mail', 'render', |
---|
130 | [ 'ballot_confirm.tt', { |
---|
131 | From => $mail, |
---|
132 | To => $mail, |
---|
133 | Subject => $c->l('Vote confirmation:') . $poll->info('label'), |
---|
134 | mail => { |
---|
135 | ballotid => $c->stash->{ballotid}, |
---|
136 | voteid => $id, |
---|
137 | } |
---|
138 | } ] |
---|
139 | ) unless($poll->info->{no_mail_confirm_vote}); |
---|
140 | |
---|
141 | $c->stash->{template} = 'ballot/done.tt'; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | sub end : Private { |
---|
146 | my ($self, $c) = @_; |
---|
147 | if ($c->res->body) { return } |
---|
148 | else { $c->forward(qw/Epoll::View::TT/) } |
---|
149 | } |
---|
150 | |
---|
151 | =head1 AUTHOR |
---|
152 | |
---|
153 | Thauvin Olivier |
---|
154 | |
---|
155 | =head1 LICENSE |
---|
156 | |
---|
157 | This library is free software, you can redistribute it and/or modify |
---|
158 | it under the same terms as Perl itself or CeCILL. |
---|
159 | |
---|
160 | =cut |
---|
161 | |
---|
162 | 1; |
---|