source: trunk/lib/Vote/DB/Voting.pm @ 176

Last change on this file since 176 was 161, checked in by nanardon, 15 years ago
  • fix due to oo change
  • Property svn:keywords set to Id Rev
File size: 3.1 KB
Line 
1package Vote::DB::Voting;
2
3# $Id$
4
5use strict;
6use warnings;
7use Mail::Mailer;
8use base 'Vote::DB::common';
9use Vote::DB::Poll;
10
11=head1 NAME
12
13Vote::Model::Vote - Catalyst Model
14
15=head1 DESCRIPTION
16
17Catalyst Model.
18
19=cut
20
21sub new {
22    my ($class, $dbstring, $key) = @_;
23   
24    bless {
25        key => $key,
26        dbstring => $dbstring,
27        db => Vote::DB::common::_newdb($dbstring),
28    }, $class;
29}
30
31sub votingkey { $_[0]->{key} }
32
33sub poll {
34    my ($self) = @_;
35    Vote::DB::Poll->new($self->{dbstring}, $self->info->{poll});
36}
37
38sub info {
39    my ($self) = @_;
40
41    my $sth = $self->db->prepare_cached(
42        q{
43        select *, voting.key as vkey from voting left join signing
44        on signing.key = voting.key
45        where voting.key = ?
46        }
47    );
48    $sth->execute($self->votingkey);
49
50    my $res = $sth->fetchrow_hashref;
51    $sth->finish;
52    $res
53}
54
55sub auth {
56    my ($self, $password) = @_;
57    my $userinfo = $self->info or return;
58
59    $userinfo->{passwd} or return;
60    if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) {
61        return 1;
62    } else {
63        return 0;
64    }
65}
66
67sub has_sign {
68    my ($self) = @_;
69
70    my $sth = $self->db->prepare_cached(
71        q{
72        select date from signing join voting
73        on voting.key = signing.key
74        where voting.key = ?
75        }
76    );
77
78    $sth->execute($self->votingkey);
79    my $res = $sth->fetchrow_hashref;
80    $sth->finish;
81    return $res->{date}
82}
83
84sub mail_voting_passwd {
85    my ($self, $mailinfo) = @_;
86   
87    my $vinfo = $self->info or return;
88    my $voteinfo = $self->poll->info;
89    $voteinfo->{description} ||= "";
90
91    my $passwd = Vote::DB::common::random_string(8);
92    my $encpasswd = $self->gen_enc_passwd($passwd);
93
94    my $upd_voting = $self->db->prepare_cached(
95        q{update voting set passwd = ? where key = ?}
96    );
97
98    $upd_voting->execute($encpasswd, $self->votingkey);
99
100    my $date = $voteinfo->{dstart} && $voteinfo->{dend}
101        ? sprintf("\n" . 'Vous pourrez voter entre le %s %s et le %s %s' . "\n",
102            $voteinfo->{dstart}, $voteinfo->{hstart}, $voteinfo->{dend}, $voteinfo->{hend})
103        : '';
104
105    # TODO complete this properly:
106    my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost');
107    $ENV{MAILADDRESS} = $voteinfo->{owner};
108    $mailer->open({
109        From => $voteinfo->{owner},
110        To => $vinfo->{mail},
111        Subject => 'Invitation a voter: ' . $voteinfo->{label},
112        'X-Epoll-poll' => $self->poll->voteid,
113        Vote::DB::common::mail_header(),
114    });
115    print $mailer <<EOF;
116Vous êtes convié à participer a ce vote:
117
118--------
119$voteinfo->{label}
120--------
121$voteinfo->{description}
122--------
123
124à l'adresse:
125
126$mailinfo->{voteurl}
127$date
128
129--
130Votre identifiant est: $vinfo->{mail}
131Votre mot de passe est: $passwd
132
133Conservez précieusement ces identifiants, il ne vous seront pas retransmits.
134
135Cordialement.
136EOF
137    $mailer->close or warn "couldn't send whole message: $!\n";
138
139    $self->db->commit;
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.