Changeset 157


Ignore:
Timestamp:
04/04/09 11:41:41 (15 years ago)
Author:
nanardon
Message:
  • split main Model code from C::Model
Location:
trunk/lib/Vote
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Vote/Model/Vote.pm

    • Property svn:keywords set to Id Rev
    r156 r157  
    44use warnings; 
    55use base 'Catalyst::Model'; 
    6 use Vote; 
    7 use DBI; 
    8 use Mail::Mailer; 
     6use base 'Vote::DB'; 
    97 
    108=head1 NAME 
     
    2119    my ($class) = @_; 
    2220     
    23     bless { 
    24         db => _newdb(), 
    25     }, $class; 
    26 } 
    27  
    28 sub _newdb { 
    29     my $db = DBI->connect( 
    30         'dbi:Pg:' . Vote->config->{db}, 
    31         undef, undef, 
    32         { 
    33             RaiseError => 0, 
    34             AutoCommit => 0, 
    35             PrintWarn => 0, 
    36             PrintError => 1, 
    37         } 
    38     ) or return; 
    39     $db->do(q{set DATESTYLE to 'DMY'}); 
    40     return $db; 
    41 } 
    42  
    43 sub db { 
    44     return $_[0]->{db} && $_[0]->{db}->ping 
    45         ? $_[0]->{db} 
    46         : $_[0]->_newdb(); 
    47 } 
    48  
    49 sub mail_header { 
    50     return( 
    51         'Content-Type' => 'text/plain; charset=UTF-8; format=flowed', 
    52         'Content-Transfer-Encoding' => '8bit', 
    53         'X-Epoll-version' => $Vote::VERSION, 
    54     ); 
    55 } 
    56  
    57 sub random_string { 
    58     my $lenght = $_[-1] || 8; 
    59  
    60     return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght)); 
    61 } 
    62  
    63 sub gen_enc_passwd { 
    64     my ($self, $passwd) = @_; 
    65  
    66     $passwd ||= random_string(8); 
    67     return(crypt($passwd, '$1$' . random_string(8) . '$')); 
    68 } 
    69  
    70 sub dbtime { 
    71     my ($self) = @_; 
    72     my $sth = $self->db->prepare( 
    73         q{select to_char(now(), 'DD/MM/YYYY HH24:MI:SS') as d} 
    74     ); 
    75  
    76     $sth->execute(); 
    77     my $res = $sth->fetchrow_hashref; 
    78     $sth->finish; 
    79     $res->{d}; 
    80 } 
    81  
    82 sub list_comming_vote { 
    83     my ($self) = @_; 
    84  
    85     my $sth = $self->db->prepare_cached( 
    86         q{ 
    87         select id from poll where 
    88         (start > now() and "end" > now()) or 
    89         "end" is null or start is null 
    90         } 
    91     ); 
    92  
    93     $sth->execute; 
    94     my @id; 
    95     while(my $res = $sth->fetchrow_hashref) { 
    96         push(@id, $res->{id}); 
    97     } 
    98  
    99     @id 
    100 } 
    101  
    102  
    103 sub list_running_vote { 
    104     my ($self) = @_; 
    105  
    106     my $sth = $self->db->prepare_cached( 
    107         q{ 
    108         select id from poll where start < now() and "end" > now() 
    109         } 
    110     ); 
    111  
    112     $sth->execute; 
    113     my @id; 
    114     while(my $res = $sth->fetchrow_hashref) { 
    115         push(@id, $res->{id}); 
    116     } 
    117  
    118     @id 
    119 } 
    120  
    121 sub list_closed_vote { 
    122     my ($self) = @_; 
    123  
    124     my $sth = $self->db->prepare_cached( 
    125         q{ 
    126         select id from poll where 
    127         start < now() and "end" < now() 
    128         } 
    129     ); 
    130  
    131     $sth->execute; 
    132     my @id; 
    133     while(my $res = $sth->fetchrow_hashref) { 
    134         push(@id, $res->{id}); 
    135     } 
    136  
    137     @id 
    138 } 
    139  
    140 sub vote_param { 
    141     my ($self, $voteid, %attr) = @_; 
    142  
    143     keys %attr or return; 
    144     my @online_f = qw(label start end owner password); 
    145  
    146     if (grep { exists($attr{$_}) } @online_f) { 
    147         my $sth = $self->db->prepare_cached( 
    148             q{update poll set } . 
    149             join(',', map { qq("$_" = ?) } grep { exists $attr{$_} } @online_f) . 
    150             q{ where id = ?} 
    151         ); 
    152         $sth->execute((map { $attr{$_} } grep { exists $attr{$_} } @online_f), $voteid) 
    153             or do { 
    154             $self->db->rollback; 
    155             return; 
    156         }; 
    157     } 
    158  
    159     # vote settings in settings table 
    160     foreach my $var (keys %attr) { 
    161         grep { $var eq $_ } @online_f and next; 
    162         $self->vote_set_settings($voteid, $var, $attr{$var}); 
    163     } 
    164     1 
    165 } 
    166  
    167 sub vote_status { 
    168     my ($self, $id) = @_; 
    169      
    170     my $sth = $self->db->prepare_cached( 
    171         q{ 
    172         select (start > now() or start is null) as before, 
    173                "end" < now() as after 
    174         from poll 
    175         where id = ? 
    176         } 
    177     ); 
    178     $sth->execute($id); 
    179     my $res = $sth->fetchrow_hashref; 
    180     $sth->finish; 
    181     $res or return; 
    182     if ($res->{before}) { 
    183         return 'BEFORE'; 
    184     } elsif ($res->{after}) { 
    185         return 'AFTER'; 
    186     } else { 
    187         return 'RUNNING'; 
    188     } 
    189 } 
    190  
    191 sub vote_info { 
    192     my ($self, $id) = @_; 
    193  
    194     my $sth = $self->db->prepare_cached( 
    195         q{ 
    196         select *, 
    197         to_char("start", 'DD/MM/YYYY') as dstart, 
    198         to_char("start", 'HH24:MI:SS') as hstart, 
    199         to_char("end", 'DD/MM/YYYY') as dend, 
    200         to_char("end", 'HH24:MI:SS') as hend 
    201         from poll where id = ? 
    202         } 
    203     ); 
    204  
    205     $sth->execute($id); 
    206     my $res = $sth->fetchrow_hashref; 
    207     $sth->finish; 
    208     if ($res) { 
    209         my $get = $self->db->prepare_cached( 
    210             q{select var, val from settings where poll = ?} 
    211         ); 
    212         $get->execute($id); 
    213         while (my $set = $get->fetchrow_hashref) { 
    214             $res->{$set->{var}} = $set->{val}; 
    215         } 
    216     } 
    217     $res->{free_choice} ||= 0; # avoiding undef 
    218     $res 
    219 } 
    220  
    221 sub vote_set_settings { 
    222     my ($self, $poll, $var, $val) = @_; 
    223  
    224     my $upd = $self->db->prepare_cached( 
    225         q{update settings set val = ? where poll = ? and var = ?} 
    226     ); 
    227  
    228     if ($upd->execute($val, $poll, $var) == 0) { 
    229         my $add = $self->db->prepare_cached( 
    230             q{insert into settings (poll, var, val) values (?,?,?)} 
    231         ); 
    232  
    233         $add->execute($poll, $var, $val); 
    234     } 
    235 } 
    236  
    237 sub vote_signing { 
    238     my ($self, $id) = @_; 
    239  
    240     my $sth = $self->db->prepare_cached( 
    241         q{ 
    242         select *, voting.key as vkey from voting left join signing 
    243         on signing.key = voting.key 
    244         where poll = ? order by voting.mail 
    245         } 
    246     ); 
    247     $sth->execute($id); 
    248     my @people; 
    249     while (my $res = $sth->fetchrow_hashref) { 
    250         push(@people, $res); 
    251     } 
    252     @people 
    253 } 
    254  
    255 sub vote_voting { 
    256     my ($self, $voteid) = @_; 
    257  
    258     my $sth = $self->db->prepare_cached( 
    259         q{ 
    260         select key from voting 
    261         where poll = ? order by voting.mail 
    262         } 
    263     ); 
    264     $sth->execute($voteid); 
    265     my @people; 
    266     while (my $res = $sth->fetchrow_hashref) { 
    267         push(@people, $res->{key}); 
    268     } 
    269     @people 
    270 } 
    271  
    272 sub voting_info { 
    273     my ($self, $id) = @_; 
    274  
    275     my $sth = $self->db->prepare_cached( 
    276         q{ 
    277         select *, voting.key as vkey from voting left join signing 
    278         on signing.key = voting.key 
    279         where voting.key = ? 
    280         } 
    281     ); 
    282     $sth->execute($id); 
    283  
    284     my $res = $sth->fetchrow_hashref; 
    285     $sth->finish; 
    286     $res 
    287 } 
    288  
    289 sub vote_choices { 
    290     my ($self, $id) = @_; 
    291  
    292     my $sth = $self->db->prepare_cached( 
    293         q{ 
    294         select key from choice where poll = ? 
    295         order by label 
    296         } 
    297     ); 
    298     $sth->execute($id); 
    299     my @ch; 
    300     while (my $res = $sth->fetchrow_hashref) { 
    301         push(@ch, $res->{key}); 
    302     } 
    303     @ch 
    304 } 
    305  
    306 sub choice_info { 
    307     my ($self, $chid) = @_; 
    308     my $sth = $self->db->prepare_cached( 
    309         q{select * from choice where key = ?} 
    310     ); 
    311     $sth->execute($chid); 
    312     my $res = $sth->fetchrow_hashref; 
    313     $sth->finish; 
    314     $res 
    315 } 
    316  
    317 sub vote_add_choice { 
    318     my ($self, $voteid, $label) = @_; 
    319  
    320     my $sth = $self->db->prepare_cached( 
    321         q{insert into choice (poll, label) values (?,?)} 
    322     ); 
    323  
    324     $sth->execute($voteid, $label) or do { 
    325         $self->db->rollback; 
    326         return; 
    327     }; 
    328  
    329     1 
    330 } 
    331  
    332 sub modify_choice { 
    333     my ($self, $chid, $label) = @_; 
    334  
    335     my $sth = $self->db->prepare_cached( 
    336         q{update choice set label = ? where key = ?} 
    337     ); 
    338     $sth->execute($label, $chid); 
    339 } 
    340  
    341 sub delete_choice { 
    342     my ($self, $chid) = @_; 
    343  
    344     my $sth = $self->db->prepare_cached( 
    345         q{delete from choice where key = ?} 
    346     ); 
    347  
    348     $sth->execute($chid); 
    349 } 
    350  
    351 sub voting_info_id { 
    352     my ($self, $mail, $voteid) = @_; 
    353  
    354     my $sth = $self->db->prepare_cached( 
    355         q{ 
    356         select * from voting where mail = ? and poll = ? 
    357         } 
    358     ); 
    359     $sth->execute($mail, $voteid); 
    360     my $res = $sth->fetchrow_hashref(); 
    361     $sth->finish; 
    362     $res 
    363 } 
    364  
    365 sub _register_signing { 
    366     my ($self, $mail, $voteid, $referal) = @_; 
    367  
    368     my $vinfo = $self->voting_info_id($mail, $voteid) or return; 
    369  
    370     my $sth = $self->db->prepare_cached( 
    371         q{ 
    372         insert into signing (key, referal) values (?,?) 
    373         } 
    374     ); 
    375     $sth->execute($vinfo->{key}, $referal) or do { 
    376         $self->db->rollback; 
    377         return; 
    378     }; 
    379  
    380     1; 
    381 } 
    382  
    383 sub gen_uid { 
    384     unpack("H*", join("", map { chr(rand(256)) } (0..15))) 
    385 } 
    386  
    387 sub _register_ballot { 
    388     my ($self, $voteid, $choice, $fchoice) = @_; 
    389  
    390     my $addb = $self->db->prepare_cached( 
    391         q{ 
    392         insert into ballot (id, poll, invalid) values (?,?,?) 
    393         } 
    394     ); 
    395     my $uid = gen_uid; 
    396     $addb->execute($uid, $voteid, scalar(@{$fchoice || []}) ? undef : 'f') or do { 
    397         self->db->rollback; 
    398         return; 
    399     }; 
    400  
    401     my $addbc = $self->db->prepare_cached( 
    402         q{ 
    403         insert into ballot_item (id, value, fromlist) values (?,?,?) 
    404         } 
    405     ); 
    406     foreach (@{ $choice || []}) { 
    407         $addbc->execute($uid, $_, 't') or do { 
    408             $self->db->rollback; 
    409             return; 
    410         }; 
    411     } 
    412     foreach (@{ $fchoice || []}) { 
    413         $_ or next; 
    414         $addbc->execute($uid, $_, 'f') or do { 
    415             $self->db->rollback; 
    416             return; 
    417         }; 
    418     } 
    419  
    420     $uid; 
    421 } 
    422  
    423 sub register_ballot { 
    424     my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_; 
    425  
    426     my $uid; 
    427     for (0..2) { # 3 try 
    428     # First we register voting has voted 
    429     $self->_register_signing($vid, $voteid, $referal) or return; # TODO error ? 
    430  
    431     # registring choices 
    432     $uid = $self->_register_ballot($voteid, $choice, $fchoice); 
    433     defined($uid) and last; 
    434  
    435     } 
    436     # everything went fine, saving! 
    437     $self->db->commit; 
    438  
    439      
    440     $uid 
    441 } 
    442  
    443 sub mail_ballot_confirm { 
    444     my ($self, $vid, $voteid, $info) = @_; 
    445     my $voteinfo = $self->vote_info($voteid) or return; 
    446     $info->{ballotid} or return; 
    447     my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); 
    448     $ENV{MAILADDRESS} = $vid; 
    449     $mailer->open({ 
    450         From => $vid, # TODO allow to configure this 
    451         To => $vid, 
    452         Subject => 'Confirmation de vote: ' . $voteinfo->{label}, 
    453         mail_header(), 
    454     }); 
    455     print $mailer <<EOF; 
    456  
    457 Vous venez de participer au vote: 
    458  
    459 -------- 
    460 $voteinfo->{label} 
    461 -------- 
    462  
    463 Votre bulletin est idéntifié sous le numéro: 
    464 $info->{ballotid} 
    465  
    466 Les résultats seront disponibles à cet url: 
    467 $info->{url} 
    468  
    469 Cordialement. 
    470 EOF 
    471     $mailer->close 
    472         or warn "couldn't send whole message: $!\n"; 
    473  
    474 } 
    475  
    476 sub vote_voting_count { 
    477     my ($self, $id) = @_; 
    478  
    479     my $sth = $self->db->prepare_cached( 
    480         q{ 
    481         select count(*) from voting 
    482         where poll = ? 
    483         } 
    484     ); 
    485     $sth->execute($id); 
    486     my $res = $sth->fetchrow_hashref; 
    487     $sth->finish; 
    488     $res->{count} 
    489 } 
    490  
    491 sub signing_count { vote_signing_count(@_) } 
    492  
    493 sub vote_signing_count { 
    494     my ($self, $voteid) = @_; 
    495  
    496     my $sth = $self->db->prepare_cached( 
    497         q{ 
    498         select count(*) from signing join voting 
    499         on voting.key = signing.key where poll = ? 
    500         } 
    501     ); 
    502  
    503     $sth->execute($voteid); 
    504     my $res = $sth->fetchrow_hashref; 
    505     $sth->finish; 
    506     $res->{count} 
    507 } 
    508  
    509 sub ballot_count { vote_ballot_count(@_) } 
    510  
    511 sub vote_ballot_count { 
    512     my ($self, $voteid) = @_; 
    513  
    514     my $sth = $self->db->prepare_cached( 
    515         q{ 
    516         select count(*) from ballot where poll = ? 
    517         } 
    518     ); 
    519  
    520     $sth->execute($voteid); 
    521     my $res = $sth->fetchrow_hashref; 
    522     $sth->finish; 
    523     $res->{count} 
    524 } 
    525  
    526 sub ballot_count_nonull { vote_ballot_count_nonull(@_) } 
    527  
    528 sub vote_ballot_count_nonull { 
    529     my ($self, $voteid) = @_; 
    530  
    531     my $sth = $self->db->prepare_cached( 
    532         q{ 
    533         select count(*) from ballot where poll = ? 
    534         and id in (select id from ballot_item) and 
    535         (invalid = 'false' or invalid is null) 
    536         } 
    537     ); 
    538  
    539     $sth->execute($voteid); 
    540     my $res = $sth->fetchrow_hashref; 
    541     $sth->finish; 
    542     $res->{count} 
    543 } 
    544  
    545 sub auth_voting { 
    546     my ($self, $poll, $mail, $password) = @_; 
    547     my $userinfo = $self->voting_info_id($mail, $poll) or return; 
    548  
    549     $userinfo->{passwd} or return; 
    550     if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) { 
    551         return 1; 
    552     } else { 
    553         return 0; 
    554     } 
    555 } 
    556  
    557 sub auth_poll { 
    558     my ($self, $voteid, $passwd) = @_; 
    559  
    560     my $vinfo = $self->vote_info($voteid) or return; 
    561  
    562     $vinfo->{password} or return; 
    563     $passwd or return; 
    564     if (crypt($passwd, $vinfo->{password} || '') eq $vinfo->{password}) { 
    565         return 1; 
    566     } else { 
    567         return 0; 
    568     } 
    569 } 
    570  
    571 sub voting_has_sign { 
    572     my ($self, $poll, $user) = @_; 
    573  
    574     my $sth = $self->db->prepare_cached( 
    575         q{ 
    576         select date from signing join voting 
    577         on voting.key = signing.key 
    578         where poll = ? and mail = ? 
    579         } 
    580     ); 
    581  
    582     $sth->execute($poll, $user); 
    583     my $res = $sth->fetchrow_hashref; 
    584     $sth->finish; 
    585     return $res->{date} 
    586 } 
    587  
    588 # Requete de decompte des voix: 
    589  
    590 sub vote_results_count { 
    591     my ($self, $voteid) = @_; 
    592  
    593     my $sth = $self->db->prepare( 
    594         q{ 
    595         select count(ballot.id), value from  
    596         (select ballot.id, coalesce(corrected, value) as "value" from ballot left join ballot_item 
    597         on ballot.id = ballot_item.id where ballot.poll = ? and (invalid = 'false'or invalid is null) 
    598         group by ballot.id, coalesce(corrected, value)) as ballot 
    599         group by value 
    600         order by count desc, value 
    601         } 
    602     ); 
    603     $sth->execute($voteid); 
    604     my @results; 
    605     while (my $res = $sth->fetchrow_hashref) { 
    606         push(@results, $res); 
    607     } 
    608     @results; 
    609 } 
    610  
    611 sub vote_results_nonull { 
    612     my ($self, $voteid) = @_; 
    613  
    614     my $sth = $self->db->prepare( 
    615         q{ 
    616         select count(ballot.id), value from  
    617         (select ballot.id, coalesce(corrected, value) as "value" from ballot join ballot_item 
    618         on ballot.id = ballot_item.id where ballot.poll = ? and (invalid = 'false'or invalid is null) 
    619         group by ballot.id, coalesce(corrected, value)) as ballot 
    620         group by value 
    621         order by count desc, value 
    622         } 
    623     ); 
    624     $sth->execute($voteid); 
    625     my @results; 
    626     while (my $res = $sth->fetchrow_hashref) { 
    627         push(@results, $res); 
    628     } 
    629     \@results; 
    630 } 
    631  
    632 sub list_vote_ballot { 
    633     my ($self, $voteid) = @_; 
    634  
    635     my $sth = $self->db->prepare_cached( 
    636         q{ 
    637         select id from ballot where poll = ? 
    638         order by id 
    639         } 
    640     ); 
    641     $sth->execute($voteid); 
    642     my @ids; 
    643     while (my $res = $sth->fetchrow_hashref) { 
    644         push(@ids, $res->{id}); 
    645     } 
    646     @ids 
    647 } 
    648  
    649 sub list_vote_ballot_needvalid { 
    650     my ($self, $voteid) = @_; 
    651  
    652     my $sth = $self->db->prepare_cached( 
    653         q{ 
    654         select id from ballot where poll = ? 
    655         and invalid is null order by id 
    656         } 
    657     ); 
    658     $sth->execute($voteid); 
    659     my @ids; 
    660     while (my $res = $sth->fetchrow_hashref) { 
    661         push(@ids, $res->{id}); 
    662     } 
    663     @ids 
    664 } 
    665  
    666 sub ballot_info { 
    667     my ($self, $ballotid) = @_; 
    668  
    669     my $sth = $self->db->prepare_cached( 
    670         q{ select * from ballot where id = ? } 
    671     ); 
    672  
    673     $sth->execute($ballotid); 
    674     my $res = $sth->fetchrow_hashref; 
    675     $sth->finish; 
    676     $res 
    677 } 
    678  
    679 sub mark_ballot_invalid { 
    680     my ($self, $ballotid, $invalid) = @_; 
    681  
    682     my $sth = $self->db->prepare_cached( 
    683         q{update ballot set invalid = ? where id = ?} 
    684     ); 
    685  
    686     $sth->execute($invalid ? 't' : 'f', $ballotid); 
    687 } 
    688  
    689 sub ballot_items { 
    690     my ($self, $ballotid) = @_; 
    691  
    692     my $sth = $self->db->prepare_cached( 
    693         q{select *, value as v from ballot_item where id = ?} 
    694     ); 
    695     $sth->execute($ballotid); 
    696     my @ids; 
    697     while (my $res = $sth->fetchrow_hashref) { 
    698         push(@ids, $res); 
    699     } 
    700     \@ids 
    701 } 
    702  
    703 sub vote_ballot_untrusted_values { 
    704     my ($self, $voteid) = @_; 
    705  
    706     my $getval = $self->db->prepare_cached( 
    707         q{ 
    708         select value from ballot join ballot_item 
    709         on ballot.id = ballot_item.id 
    710         where poll = ? and fromlist = false and corrected is null 
    711         group by value order by value 
    712         } 
    713     ); 
    714     $getval->execute($voteid); 
    715     my @vals; 
    716     while (my $res = $getval->fetchrow_hashref) { 
    717         push(@vals, $res->{value}); 
    718     } 
    719     @vals 
    720 } 
    721  
    722 sub vote_ballot_values { 
    723     my ($self, $voteid) = @_; 
    724  
    725     my $getval = $self->db->prepare_cached( 
    726         q{ 
    727         select coalesce(corrected, value) as value from ballot join ballot_item 
    728         on ballot.id = ballot_item.id 
    729         where poll = ? 
    730         group by coalesce(corrected, value) order by coalesce(corrected, value) 
    731         } 
    732     ); 
    733     $getval->execute($voteid); 
    734     my @vals; 
    735     while (my $res = $getval->fetchrow_hashref) { 
    736         push(@vals, $res->{value}); 
    737     } 
    738     @vals 
    739 } 
    740  
    741 sub vote_map_value { 
    742     my ($self, $voteid, $from, $to) = @_; 
    743  
    744     my $sth = $self->db->prepare_cached( 
    745         q{ 
    746         update ballot_item set corrected = ? where 
    747         id in (select id from ballot where poll = ?) 
    748         and (value = ? or corrected = ?) 
    749         } 
    750     ); 
    751  
    752     $sth->execute($to, $voteid, $from, $from) or $self->db->rollback; 
    753     $self->db->commit; 
    754 } 
    755  
    756 sub addupd_voting { 
    757     my ($self, $voteid, $mail, $id) = @_; 
    758  
    759     $mail =~ s/\s*$//; 
    760     $mail =~ s/^\s*//; 
    761     $mail = lc($mail); 
    762     $id =~ s/\s*$//; 
    763     $id =~ s/^\s//; 
    764     my $upd = $self->db->prepare_cached( 
    765         q{ 
    766         update voting set label = ? where mail = ? and poll = ? 
    767         } 
    768     ); 
    769  
    770     if ($upd->execute($id || '', $mail, $voteid) == 0) { 
    771         my $add = $self->db->prepare_cached(q{ 
    772             insert into voting (poll, label, mail) values (?,?,?) 
    773         }); 
    774  
    775         $add->execute($voteid, $id || '', $mail); 
    776     } 
    777 } 
    778  
    779 sub delete_voting { 
    780     my ($self, $key) = @_; 
    781  
    782     $self->voting_has_sign($key) and return; 
    783     my $sth = $self->db->prepare_cached( 
    784         q{delete from voting where key = ?} 
    785     ); 
    786  
    787     $sth->execute($key); 
    788 } 
    789  
    790 sub voting_from_file { 
    791     my ($self, $voteid, $fh, $delete) = @_; 
    792  
    793     if ($delete) { 
    794         my $sth = $self->db->prepare(q{delete from voting where poll = ?}); 
    795         $sth->execute($voteid); 
    796     } 
    797  
    798     while (my $line = <$fh>) { 
    799         chomp($line); 
    800         my ($mail, $name) = split(';', $line); 
    801         $mail or do { 
    802             $self->db->rollback; 
    803             return; 
    804         }; 
    805         $self->addupd_voting($voteid, $mail, $name || ''); 
    806     } 
    807     1; 
    808 } 
    809  
    810 sub mail_passwd_ifnul { 
    811     my ($self, $voteid, $mailinfo) = @_; 
    812  
    813     my $list_voting = $self->db->prepare_cached( 
    814         q{select key from voting where poll = ? and passwd is null or passwd = ''} 
    815     ); 
    816  
    817     $list_voting->execute($voteid); 
    818     while (my $res = $list_voting->fetchrow_hashref) { 
    819         $self->mail_voting_passwd($res->{key}, $mailinfo); 
    820     } 
    821 } 
    822  
    823 sub mail_voting_passwd { 
    824     my ($self, $id, $mailinfo) = @_; 
    825      
    826     my $vinfo = $self->voting_info($id) or return; 
    827     my $voteinfo = $self->vote_info($vinfo->{poll}); 
    828     $voteinfo->{description} ||= ""; 
    829  
    830     my $passwd = random_string(8); 
    831     my $encpasswd = $self->gen_enc_passwd($passwd); 
    832  
    833     my $upd_voting = $self->db->prepare_cached( 
    834         q{update voting set passwd = ? where key = ?} 
    835     ); 
    836  
    837     $upd_voting->execute($encpasswd, $id); 
    838  
    839     my $date = $voteinfo->{dstart} && $voteinfo->{dend} 
    840         ? sprintf("\n" . 'Vous pourrez voter entre le %s %s et le %s %s' . "\n", 
    841             $voteinfo->{dstart}, $voteinfo->{hstart}, $voteinfo->{dend}, $voteinfo->{hend}) 
    842         : ''; 
    843  
    844     # TODO complete this properly: 
    845     my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); 
    846     $ENV{MAILADDRESS} = $voteinfo->{owner}; 
    847     $mailer->open({ 
    848         From => $voteinfo->{owner}, 
    849         To => $vinfo->{mail}, 
    850         Subject => 'Invitation a voter: ' . $voteinfo->{label}, 
    851         'X-Epoll-poll' => $id, 
    852         mail_header(), 
    853     }); 
    854     print $mailer <<EOF; 
    855 Vous êtes convié à participer a ce vote: 
    856  
    857 -------- 
    858 $voteinfo->{label} 
    859 -------- 
    860 $voteinfo->{description} 
    861 -------- 
    862  
    863 Ã  l'adresse: 
    864  
    865 $mailinfo->{voteurl} 
    866 $date 
    867  
    868 --  
    869 Votre identifiant est: $vinfo->{mail} 
    870 Votre mot de passe est: $passwd 
    871  
    872 Conservez précieusement ces identifiants, il ne vous seront pas retransmits. 
    873  
    874 Cordialement. 
    875 EOF 
    876     $mailer->close or warn "couldn't send whole message: $!\n"; 
    877  
    878     $self->db->commit; 
    879 } 
    880  
    881 sub poll_request_info { 
    882     my ($self, $rid) = @_; 
    883  
    884     my $sth = $self->db->prepare_cached( 
    885         q{select * from poll_request where id = ?} 
    886     ); 
    887  
    888     $sth->execute($rid); 
    889     my $res = $sth->fetchrow_hashref; 
    890     $sth->finish; 
    891     $res 
    892 } 
    893  
    894 sub poll_from_request { 
    895     my ($self, $rid, $passwd) = @_; 
    896     my $rinfo = $self->poll_request_info($rid) or return; 
    897  
    898     my $encpasswd = $self->gen_enc_passwd($passwd); 
    899  
    900     my $getpollid = $self->db->prepare_cached( 
    901         q{select nextval('poll_id_seq')} 
    902     ); 
    903     $getpollid->execute(); 
    904     my $newpollid = $getpollid->fetchrow_hashref->{nextval}; 
    905      
    906     my $newpoll = $self->db->prepare_cached( 
    907         q{insert into poll (id, label, owner, password) values (?,?,?,?)} 
    908     ); 
    909  
    910     $newpoll->execute($newpollid, $rinfo->{label}, $rinfo->{mail}, $encpasswd); 
    911     # set some default 
    912     $self->vote_param($newpollid, 
    913         free_choice => 0, 
    914         choice_count => 1, 
    915     );      
    916  
    917     my $delreq = $self->db->prepare_cached( 
    918         q{delete from poll_request where id = ?} 
    919     ); 
    920  
    921     $delreq->execute($rid); 
    922     $self->db->commit; 
    923  
    924     $newpollid 
    925 } 
    926  
    927 sub create_poll_request { 
    928     my ($self, %info) = @_; 
    929  
    930     $info{mail} or return; 
    931     my $addreq = $self->db->prepare_cached( 
    932         q{insert into poll_request (id, label, mail) values (?,?,?)} 
    933     ); 
    934  
    935     my $reqid = gen_uid; 
    936  
    937     $addreq->execute($reqid, $info{label}, $info{mail}); 
    938     my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); 
    939     $ENV{MAILADDRESS} = undef; 
    940     $mailer->open({ 
    941         From => 'Voting system <nomail@nomail.com>', # TODO allow to configure this 
    942         To => $info{mail}, 
    943         Subject => 'Votre nouveau vote', 
    944         mail_header(), 
    945     }); 
    946     print $mailer <<EOF; 
    947  
    948 Vous avez demandez la création d'un nouveau vote: 
    949 $info{label} 
    950  
    951 Pour valider votre demande, veuiller allez visitez la page: 
    952 $info{url}/$reqid 
    953  
    954 A bientÃŽt 
    955 EOF 
    956     $mailer->close 
    957         or warn "couldn't send whole message: $!\n"; 
    958     $self->db->commit; 
    959     1; 
     21    bless Vote::DB->new(Vote->config->{db}), $class; 
    96022} 
    96123 
Note: See TracChangeset for help on using the changeset viewer.