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