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

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