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 | ||
amélioration de l'affichage
|
46 |
$request = "select id as vote_id, uniqId, poll_id, choices, name from $tables{votes} where poll_id = '$poll_id'"; |
création d'une page html pou...
|
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 |
} |
|
amélioration de l'affichage
|
60 |
foreach my $uniqId (keys(%{$votes->{$poll_id}})) { |
61 |
$polls->{$category}->{$poll_id}->{compteur}++; |
|
62 |
$polls->{$category}->{$poll_id}->{liste} .= $votes->{$poll_id}->{$uniqId}->{name} . ", " if (substr($votes->{$poll_id}->{$uniqId}->{choices},0, 1) eq '2'); |
|
63 |
} |
|
64 |
$polls->{$category}->{$poll_id}->{liste} =~ s/,\s*$//; |
|
création d'une page html pou...
|
65 |
} |
66 |
} |
|
67 |
$dbh->disconnect(); |
|
68 | ||
69 |
my $data = '<!DOCTYPE html><html lang="fr"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>'; |
|
70 | ||
71 |
foreach my $category (keys(%{$polls})) { |
|
72 |
next unless (keys%{$polls->{$category}}); |
|
73 |
$data .= "<center><h1>$category</h1></center>"; |
|
74 |
foreach my $poll_id (keys(%{$polls->{$category}})) { |
|
75 |
my $title = $polls->{$category}->{$poll_id}->{title}; |
|
76 |
$data .= "<b>" . strftime("[%a %e %b] ", localtime($polls->{$category}->{$poll_id}->{next})). $title . "</b><br>"; |
|
amélioration de l'affichage
|
77 |
if ($polls->{$category}->{$poll_id}->{compteur} gt 0) { |
78 |
$data .= "<i>$polls->{$category}->{$poll_id}->{compteur} inscrit-e"; |
|
79 |
$data .= "-s" if $polls->{$category}->{$poll_id}->{compteur} gt 1; |
|
80 |
$data .= " dont présent-e: $polls->{$category}->{$poll_id}->{liste}" if length($polls->{$category}->{$poll_id}->{liste}) gt 0; |
|
81 |
$data .= "</i><br>"; |
|
82 |
} |
|
création d'une page html pou...
|
83 |
$data .= $polls->{$category}->{$poll_id}->{description} . "<br><br>"; |
84 |
} |
|
85 |
} |
|
86 | ||
amélioration de l'affichage
|
87 |
$data .= "<HR><center>" . strftime("%c", localtime()) . '</center></body></html>'; |
88 | ||
89 |
$data =~ s/\n/<br>/g; |
|
création d'une page html pou...
|
90 | |
91 |
print $data; |