source: trunk/lib/Vote/Controller/Admin.pm @ 75

Last change on this file since 75 was 75, checked in by nanardon, 15 years ago
  • fix #8: allow to change voting list during poll
File size: 3.8 KB
Line 
1package Vote::Controller::Admin;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Vote::Controller::Admin - 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    $c->stash->{voteid} = $id;
38    my $vote = $c->model('Vote');
39
40    $vote->vote_info($id) or do {
41        $c->res->redirect($c->uri_for('/'));
42        return;
43    };
44
45    my $password = $c->session->{vpassword} || $c->req->param('vpassword');
46
47    if (!$c->model('Vote')->auth_poll($id, $password)) {
48        $c->stash->{page}{title} = $vote->vote_info($id)->{label} . ': Login d\'administration';
49        $c->session->{vpassword} = undef;
50        $c->stash->{template} = 'admin/login.tt';
51        return;
52    }
53
54    $c->session->{vpassword} = $password;
55
56    $c->stash->{page}{title} = $c->model('Vote')->vote_info($id)->{label} . ': Administration';
57
58    warn $vote->vote_status($id);
59    for ($vote->vote_status($id) || '') {
60    /^BEFORE$/ and do {
61        if ($c->req->param('addch')) {
62            $vote->vote_add_choice($id, $c->req->param('addch'))
63                and $vote->db->commit;
64        } elsif ($c->req->param('delch')) {
65            $vote->delete_choice($c->req->param('delch'))
66                and $vote->db->commit;
67        } elsif ($c->req->param('label')) {
68            if ($c->req->param('dstart')) {
69                $c->req->param('start',
70                    $c->req->param('dstart') . ' ' . ($c->req->param('hstart') || '')
71                );
72            }
73            if ($c->req->param('dend')) {
74                $c->req->param('end',
75                    $c->req->param('dend') . ' ' . ($c->req->param('hend') || '')
76                );
77            }
78            $vote->vote_param(
79                $id,
80                map { $_ => ($c->req->param($_) || undef) }
81                qw(label description start end choice_count free_choice)
82            ) and $vote->db->commit;
83        }
84    };
85
86    /^(BEFORE|RUNNING)$/ and do {
87        warn "XX";
88        if (my ($upload) = $c->req->upload('votinglist')) {
89            $vote->voting_from_file(
90                $id,
91                $upload->fh,
92                $c->req->param('delete'),
93            ) and $vote->db->commit;
94        } elsif($c->req->param('delvoting')) {
95            $vote->delete_voting($c->req->param('delvoting'))
96                and $vote->db->commit;
97        } elsif ($c->req->param('mail')) {
98            $vote->addupd_voting($id, $c->req->param('mail'), $c->req->param('id'))
99                and $vote->db->commit;
100        } elsif($c->req->param('mailpasswd')) {
101            $vote->mail_passwd_ifnul($id, {
102                voteurl => $c->uri_for('/ballot', $id),
103            });
104        }
105    };
106
107    /^AFTER$/ and do {
108        if ($c->req->param('mapfrom') && $c->req->param('mapto')) {
109            $vote->vote_map_value(
110                $id,
111                $c->req->param('mapfrom'),
112                $c->req->param('mapto'),
113            );
114        }
115        foreach my $bid ($vote->list_vote_ballot_needvalid($id)) {
116            if (!$c->req->param($bid)) {
117                next;
118            } elsif($c->req->param($bid) eq 'invalid') {
119                $vote->mark_ballot_invalid($bid, 1);
120                $vote->db->commit;
121            } elsif($c->req->param($bid) eq 'valid') {
122                $vote->mark_ballot_invalid($bid, 0);
123                $vote->db->commit;
124            }
125        }
126    };
127    }
128
129}
130
131=head1 AUTHOR
132
133Thauvin Olivier
134
135=head1 LICENSE
136
137This library is free software, you can redistribute it and/or modify
138it under the same terms as Perl itself.
139
140=cut
141
1421;
Note: See TracBrowser for help on using the repository browser.