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

Last change on this file since 219 was 219, checked in by nanardon, 15 years ago
  • add specific object to manage ballot data
File size: 3.9 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->request->parameters->{fsbal} = $c->stash->{fsbal};
87
88    my @sbalval = grep { $_ } map { lc($choices{$_} || '') } $c->req->param('sbal');
89
90    my @ballot = grep { $_ } (@sbalval, @{$c->stash->{fsbal} || []});
91    if (scalar(@ballot) > $poll->info('choice_count')) {
92        $c->req->parameters->{'ballot'} = '';
93        $c->stash->{vote_error} = 'Seulement ' .
94            $poll->info('choice_count') . ' choix possible';
95        return;
96    }
97    {
98        my %uniq;
99        foreach(@ballot) {
100            $uniq{lc($_)} ||= 0; # avoid undef
101            $uniq{lc($_)}++;
102        }
103        my @twices = grep { $uniq{$_} > 1 } (sort keys %uniq);
104        if (scalar(@twices)) {
105            $c->req->parameters->{'ballot'} = '';
106            $c->stash->{vote_error} = 'Une ou plusieurs valeurs sont en double: ' .
107                join(' ,', map { qq'"$_"' } @twices);
108            return;
109        }
110    }
111
112    if ($c->req->param('confirm')) {
113        $c->stash->{ballotid} = $poll->register_ballot(
114            $mail,
115            [ @ballot ],
116            $c->req->address,
117        ); # TODO trap error
118        $c->forward(
119            q'Vote::View::Mail', 'render',
120            [ 'ballot_confirm.tt', {
121                From => $mail,
122                To => $mail,
123                Subject => 'Confirmation de vote: ' . $poll->info('label'),
124                mail => {
125                    ballotid => $c->stash->{ballotid},
126                    voteid => $id,
127                }
128            } ]
129        );
130
131        $c->stash->{template} = 'ballot/done.tt';
132        $c->delete_session('Vote terminé');
133    }
134}
135
136sub end : Private {
137    my ($self, $c) = @_;
138    if ($c->res->body) { return }
139    else { $c->forward(qw/Vote::View::TT/) }
140}
141
142=head1 AUTHOR
143
144Thauvin Olivier
145
146=head1 LICENSE
147
148This library is free software, you can redistribute it and/or modify
149it under the same terms as Perl itself or CeCILL.
150
151=cut
152
1531;
Note: See TracBrowser for help on using the repository browser.