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

Last change on this file since 205 was 205, checked in by nanardon, 15 years ago
  • cleanup code
File size: 4.0 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')->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')->poll($id)->status 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        if (defined($c->req->param('password'))) {
53            $c->stash->{login_failure} = 1;
54        }
55        return;
56    }
57
58    $c->session->{mail} = $mail;
59    $c->session->{password} = $password;
60
61    $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Bulletin';
62
63    # login succeed, but those this user has already voted
64    if (my $date = $c->model('Vote')->voting_has_sign($id, $mail)) {
65        $c->stash->{mail} = $c->session->{mail};
66        $c->stash->{template} = 'ballot/signed.tt';
67        $c->stash->{signed_date} = $date;
68        $c->delete_session('already signed');
69        return;
70    }
71
72    my $vote = $c->model('Vote');
73    my %choices;
74    foreach ($vote->vote_choices($id)) {
75        $choices{$vote->choice_info($_)->{key}} = $vote->choice_info($_)->{label};
76    }
77    $c->stash->{choices} = { %choices };
78    $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') };
79    $c->stash->{fsbal} = [ grep { $_ } map {
80        s/^\s+//;
81        s/\s+$//;
82        s/\s+/ /g;
83        lc($_)
84    } ($c->req->param('fsbal')) ];
85    $c->request->parameters->{fsbal} = $c->stash->{fsbal};
86
87    my @sbalval = grep { $_ } map { lc($choices{$_} || '') } $c->req->param('sbal');
88
89    if (scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})
90        > $vote->vote_info($id)->{choice_count}) {
91        $c->req->parameters->{'ballot'} = '';
92        $c->stash->{vote_error} = 'Seulement ' .
93            $vote->vote_info($id)->{choice_count} . ' choix possible';
94        return;
95    }
96    {
97        my %uniq;
98        foreach(@sbalval, @{$c->stash->{fsbal} || []}) {
99            $uniq{lc($_)} ||= 0; # avoid undef
100            $uniq{lc($_)}++;
101        }
102        my @twices = grep { $uniq{$_} > 1 } (sort keys %uniq);
103        if (scalar(@twices)) {
104            $c->req->parameters->{'ballot'} = '';
105            $c->stash->{vote_error} = 'Une ou plusieurs valeurs sont en double: ' .
106                join(' ,', map { qq'"$_"' } @twices);
107            return;
108        }
109    }
110
111    if ($c->req->param('confirm')) {
112        $c->stash->{ballotid} = $vote->register_ballot(
113            $mail,
114            $id,
115            [ @sbalval ],
116            [ @{ $c->stash->{fsbal} } ],
117            $c->req->address,
118        ); # TODO trap error
119        $c->forward(
120            q'Vote::View::Mail', 'render',
121            [ 'ballot_confirm.tt', {
122                From => $mail,
123                To => $mail,
124                Subject => 'Confirmation de vote: ' . $vote->vote_info($id)->{label},
125                mail => {
126                    ballotid => $c->stash->{ballotid},
127                    voteid => $id,
128                }
129            } ]
130        );
131
132        $c->stash->{template} = 'ballot/done.tt';
133        $c->delete_session('Vote terminé');
134    }
135}
136
137sub end : Private {
138    my ($self, $c) = @_;
139    if ($c->res->body) { return }
140    else { $c->forward(qw/Vote::View::TT/) }
141}
142
143=head1 AUTHOR
144
145Thauvin Olivier
146
147=head1 LICENSE
148
149This library is free software, you can redistribute it and/or modify
150it under the same terms as Perl itself or CeCILL.
151
152=cut
153
1541;
Note: See TracBrowser for help on using the repository browser.