création d'une page html pou...
|
1 |
#!/usr/bin/perl -w |
2 |
use strict; |
|
3 |
use warnings; |
|
4 |
use DBI; |
|
5 |
use POSIX qw(strftime); |
|
6 |
use Data::Dumper; |
|
7 | ||
8 |
my $scriptconf = $ENV{PWD} . "/action.conf"; |
|
9 |
if (-r $scriptconf) { |
|
10 |
package cfg; |
|
11 |
unless (my $return = do $scriptconf) { |
|
12 |
warn "couldn't parse $scriptconf: $@" if $@; |
|
13 |
warn "couldn't do $scriptconf: $!" unless defined $return; |
|
14 |
warn "couldn't run $scriptconf" unless $return; |
|
15 |
} |
|
16 |
} |
|
17 |
else { |
|
18 |
print "pas de config\n"; |
|
19 |
exit; |
|
20 |
} |
|
21 | ||
22 |
my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1, 'AutoCommit' => 1}); |
|
23 | ||
24 |
my $request; |
|
25 |
my $sth; |
|
26 |
my $polls; |
|
27 |
my $slots; |
|
28 |
my $votes; |
|
29 |
my %tables = (); |
|
30 |
$tables{polls} = $cfg::config{prefix} . "poll"; |
|
31 |
$tables{votes} = $cfg::config{prefix} . "vote"; |
|
32 |
$tables{slots} = $cfg::config{prefix} . "slot"; |
|
33 | ||
34 |
$request = "select id as poll_id, admin_name as category, admin_id, title, recurrent, description from $tables{polls}"; |
|
35 |
$sth = $dbh->prepare($request); |
|
36 |
$polls = $dbh->selectall_hashref($sth, [ qw(category poll_id) ]); |
|
37 | ||
38 |
# admin_name = catégorie |
|
39 |
my $dt = strftime "%s", localtime; |
|
40 |
foreach my $category (keys(%{$polls})) { |
|
41 |
foreach my $poll_id (keys(%{$polls->{$category}})) { |
|
42 |
$request = "select id as slot_id, title, poll_id from $tables{slots} where poll_id = '$poll_id'"; |
|
43 |
$sth = $dbh->prepare($request); |
|
44 |
$slots = $dbh->selectall_hashref($sth, [ qw(poll_id slot_id) ]); |
|
45 | ||
46 |
$request = "select id as vote_id, uniqId, poll_id, choices from $tables{votes} where poll_id = '$poll_id'"; |
|
47 |
$sth = $dbh->prepare($request); |
|
48 |
$votes = $dbh->selectall_hashref($sth, [ qw(poll_id uniqId) ]); |
|
49 | ||
50 |
my $last_date = 0; |
|
51 |
my @deleted = (); |
|
52 |
$polls->{$category}->{$poll_id}->{next} = strftime "%s", 0, 0, 0, 1, 12, 2099; |
|
53 |
$polls->{$category}->{$poll_id}->{compteur} = 0; |
|
54 |
foreach my $slot_id (keys(%{$slots->{$poll_id}})) { |
|
55 |
my $title = $slots->{$poll_id}->{$slot_id}->{title}; |
|
56 |
if ($title lt $polls->{$category}->{$poll_id}->{next}) { |
|
57 |
$polls->{$category}->{$poll_id}->{next} = $title; |
|
58 |
} |
|
59 |
foreach my $uniqId (keys(%{$votes->{$poll_id}})) { |
|
60 |
$polls->{$category}->{$poll_id}->{compteur}++; |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
$dbh->disconnect(); |
|
66 | ||
67 |
my $data = '<!DOCTYPE html><html lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>'; |
|
68 | ||
69 |
foreach my $category (keys(%{$polls})) { |
|
70 |
next unless (keys%{$polls->{$category}}); |
|
71 |
$data .= "<center><h1>$category</h1></center>"; |
|
72 |
foreach my $poll_id (keys(%{$polls->{$category}})) { |
|
73 |
my $title = $polls->{$category}->{$poll_id}->{title}; |
|
74 |
$data .= "<b>" . strftime("[%a %e %b] ", localtime($polls->{$category}->{$poll_id}->{next})). $title . "</b><br>"; |
|
75 |
$data .= "($polls->{$category}->{$poll_id}->{compteur} inscrits)<br>" if ($polls->{$category}->{$poll_id}->{compteur} ge 0); |
|
76 |
$data .= $polls->{$category}->{$poll_id}->{description} . "<br><br>"; |
|
77 |
} |
|
78 |
} |
|
79 | ||
80 |
$data .= strftime("%c", localtime()) . '</body></html>'; |
|
81 | ||
82 |
print $data; |