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

Last change on this file since 82 was 82, checked in by nanardon, 15 years ago
  • fix request->param usage, fixing nul ballot view
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    $c->request->parameters->{fsbal} = $c->stash->{fsbal};
83
84    my @sbalval = grep { $_ } map { $choices{$_} } $c->req->param('sbal');
85
86    if (scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})
87        > $vote->vote_info($id)->{choice_count}) {
88        $c->req->parameters->{'ballot'} = '';
89        $c->stash->{vote_error} = 'Seulement ' .
90            $vote->vote_info($id)->{choice_count} . ' choix possible';
91        return;
92    }
93    {
94        my %uniq;
95        $uniq{lc($_)} = 1 foreach(@sbalval, @{$c->stash->{fsbal} || []});
96        if (scalar(keys %uniq) != scalar(@sbalval) + scalar(@{$c->stash->{fsbal} || []})) {
97            $c->req->parameters->{'ballot'} = '';
98            $c->stash->{vote_error} = 'Une valeur est en double';
99            return;
100        }
101    }
102
103    if ($c->req->param('confirm')) {
104        $c->stash->{ballotid} = $vote->register_ballot(
105            $mail,
106            $id,
107            [ @sbalval ],
108            [ @{ $c->stash->{fsbal} } ],
109            $c->req->address,
110        ); # TODO trap error
111        $c->stash->{template} = 'ballot/done.tt';
112        $c->delete_session('Vote terminé');
113    }
114}
115
116=head1 AUTHOR
117
118Thauvin Olivier
119
120=head1 LICENSE
121
122This library is free software, you can redistribute it and/or modify
123it under the same terms as Perl itself.
124
125=cut
126
1271;
Note: See TracBrowser for help on using the repository browser.