source: trunk/lib/Vote/Controller/Ballot.pm @ 74

Last change on this file since 74 was 53, checked in by nanardon, 15 years ago
  • rollback database before doing anything
File size: 3.2 KB
Line 
1package Vote::Controller::Ballot;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Vote::Controller::Ballot - 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 ) = @_;
26    $c->model('Vote')->db->rollback;
27}
28
29sub index : Private {
30    my ( $self, $c ) = @_;
31
32    $c->res->redirect($c->uri_for('/'));
33}
34
35sub default : Private {
36    my ( $self, $c, undef, $id ) = @_;
37
38    $c->stash->{voteid} = $id;
39
40    if ($c->model('Vote')->vote_status($id) ne 'RUNNING') {
41        $c->stash->{template} = 'ballot/closed.tt';
42        return;
43    }
44
45    my $mail = $c->session->{mail} || $c->req->param('mail');
46    my $password = $c->session->{password} || $c->req->param('password');
47
48    if (!$c->model('Vote')->auth_voting($id, $mail, $password)) {
49        $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Login';
50        $c->delete_session('invalid user/pass');
51        $c->stash->{template} = 'ballot/login.tt';
52        return;
53    }
54
55    $c->session->{mail} = $mail;
56    $c->session->{password} = $password;
57
58    $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Bulletin';
59
60    # login succeed, but those this user has already voted
61    if (my $date = $c->model('Vote')->voting_has_sign($id, $mail)) {
62        $c->stash->{mail} = $c->session->{mail};
63        $c->stash->{template} = 'ballot/signed.tt';
64        $c->stash->{signed_date} = $date;
65        $c->delete_session('already signed');
66        return;
67    }
68
69    my $vote = $c->model('Vote');
70    my %choices;
71    foreach ($vote->vote_choices($id)) {
72        $choices{$vote->choice_info($_)->{key}} = $vote->choice_info($_)->{label};
73    }
74    $c->stash->{choices} = { %choices };
75    $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') };
76    $c->stash->{fsbal} = [ grep { $_ } map {
77        s/^\s+//;
78        s/\s+$//;
79        s/\s+/ /g;
80        lc($_)
81    } ($c->req->param('fsbal')) ];
82
83    my @sbalval = grep { $_ } map { $choices{$_} } $c->req->param('sbal');
84
85    if (scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})
86        > $vote->vote_info($id)->{choice_count}) {
87        $c->req->param('ballot', '');
88        $c->stash->{vote_error} = 'Seulement ' .
89            $vote->vote_info($id)->{choice_count} . ' choix possible';
90        return;
91    }
92    {
93        my %uniq;
94        $uniq{lc($_)} = 1 foreach(@sbalval, @{$c->stash->{fsbal} || []});
95        if (scalar(keys %uniq) != scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})) {
96            $c->req->param('ballot', '');
97            $c->stash->{vote_error} = 'Une valeur est en double';
98            return;
99        }
100    }
101
102    if ($c->req->param('confirm')) {
103        $c->stash->{ballotid} = $vote->register_ballot(
104            $mail,
105            $id,
106            [ @sbalval ],
107            [ @{ $c->stash->{fsbal} } ],
108            $c->req->address,
109        ); # TODO trap error
110        $c->stash->{template} = 'ballot/done.tt';
111        $c->delete_session('Vote terminé');
112    }
113}
114
115=head1 AUTHOR
116
117Thauvin Olivier
118
119=head1 LICENSE
120
121This library is free software, you can redistribute it and/or modify
122it under the same terms as Perl itself.
123
124=cut
125
1261;
Note: See TracBrowser for help on using the repository browser.