apps / action / action /
Newer Older
188 lines | 7.812kb
initial commit
admin cloud-section (root) authored on 2016-12-10
1
#!/usr/bin/perl -w
2
use strict;
3
use warnings;
4
use DBI;
5
use utf8;
6
use POSIX qw(strftime);
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
7
#use Data::Dumper;
initial commit
admin cloud-section (root) authored on 2016-12-10
8
use Email::Send::SMTP::Gmail;
9

            
10
my $scriptconf = $ENV{PWD} . "/$0";
11
$scriptconf =~ s/\.pl$//;
12
$scriptconf =~ s/$/.conf/;
13
if (-r $scriptconf) {
14
    package cfg;
15
    unless (my $return = do $scriptconf) {
16
        warn "couldn't parse $scriptconf: $@" if $@;
17
        warn "couldn't do $scriptconf: $!"    unless defined $return;
18
        warn "couldn't run $scriptconf"       unless $return;
19
    }
20
}
21
else {
22
    print "pas de config\n";
23
    exit;
24
}
25

            
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
26
if (-r $cfg::config{notification}->{flag}) {
27
    open FLAG, $cfg::config{notification}->{flag};
28
    while (<FLAG>) {
29
        if (/^to:(.+)$/) {
30
            $cfg::config{test}->{address} = $1;
31
        }
32
        elsif (/^intro:([[:alnum:]]+)/) {
fix: mauvais contenu de vari...
admin cloud-section (root) authored on 2017-02-18
33
            $cfg::config{test}->{intro} = $1;
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
34
        }
35
    }
36
    close FLAG;
fix: mauvais contenu de vari...
admin cloud-section (root) authored on 2017-02-18
37
    if (exists $cfg::config{test}->{address} and exists $cfg::config{test}->{intro}) {
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
38
        delete $cfg::config{notification}->{to};
39
        $cfg::config{notification}->{to}->{test}->{address} = $cfg::config{test}->{address};
fix: mauvais contenu de vari...
admin cloud-section (root) authored on 2017-02-18
40
        $cfg::config{notification}->{to}->{test}->{intro} = '@' . $cfg::config{test}->{intro};
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
41
        $cfg::config{notification}->{to}->{test}->{outro} = "pour test";
42
        $cfg::config{notification}->{to}->{test}->{subject} = "test de l'envoi des actions de la section";
43
    }
44
}
45

            
initial commit
admin cloud-section (root) authored on 2016-12-10
46
my $dbh = DBI->connect($cfg::config{db}->{driver}, $cfg::config{db}->{user}, $cfg::config{db}->{password}, {'RaiseError' => 1, 'AutoCommit' => 1});
47
$dbh->{'mysql_enable_utf8'} = 1;
48
$dbh->do(qq{SET NAMES "utf8"});
49

            
50
my $request;
51
my $sth;
52
my $polls;
53
my $slots;
54
my $votes;
55
my %tables = ();
56
$tables{polls} = $cfg::config{prefix} . "poll";
57
$tables{votes} = $cfg::config{prefix} . "vote";
58
$tables{slots} = $cfg::config{prefix} . "slot";
59

            
ajout de la description des ...
admin cloud-section (root) authored on 2017-02-13
60
$request = "select id as poll_id, admin_name as category, admin_id, title, recurrent, restricted, description from $tables{polls}";
initial commit
admin cloud-section (root) authored on 2016-12-10
61
$sth = $dbh->prepare($request);
62
$polls = $dbh->selectall_hashref($sth, [ qw(category poll_id) ]);
63

            
64
# admin_name = catégorie
65
my $dt = strftime "%s", localtime;
66
foreach my $category (keys(%{$polls})) {
67
    foreach my $poll_id (keys(%{$polls->{$category}})) {
68
        $request = "select id as slot_id, title, poll_id from $tables{slots} where poll_id = '$poll_id'";
69
        $sth = $dbh->prepare($request);
70
        $slots =  $dbh->selectall_hashref($sth, [ qw(poll_id slot_id) ]);
71

            
72
        $request = "select id as vote_id, uniqId, poll_id, choices from $tables{votes} where poll_id = '$poll_id'";
73
        $sth = $dbh->prepare($request);
74
        $votes =  $dbh->selectall_hashref($sth, [ qw(poll_id uniqId) ]);
75

            
76
        my $last_date = 0;
77
        my @deleted = ();
78
        foreach my $slot_id (keys(%{$slots->{$poll_id}})) {
79
            if ($slots->{$poll_id}->{$slot_id}->{title} < $dt) {
80
                $slots->{$poll_id}->{$slot_id}->{to_delete} = 1;
81
            }
82
            my $title = $slots->{$poll_id}->{$slot_id}->{title};
83
            $last_date = strftime "%s", localtime($title) if ($title > $last_date);
84
        }
85

            
86
# PURGE DES ACTIONS DÉJÀ PASSÉES ET NON RÉCURRENTES
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
87
        if ($last_date < $dt and ( ! defined $polls->{$category}->{$poll_id}->{recurrent} or $polls->{$category}->{$poll_id}->{recurrent} < 1)) {
initial commit
admin cloud-section (root) authored on 2016-12-10
88
            print "purge de $category/$polls->{$category}->{$poll_id}->{title}\n";
89
            $request = "delete from $tables{polls} where id = '$poll_id'";
90
            delete $polls->{$category}->{$poll_id};
91
            $dbh->do($request);
92
#            print $request . "\n";
93
            $request = "delete from $tables{slots} where poll_id = '$poll_id'";
94
            delete $slots->{$poll_id};
95
            $dbh->do($request);
96
#            print $request . "\n";
97
            $request = "delete from $tables{votes} where poll_id = '$poll_id'";
98
            delete $votes->{$poll_id};
99
            $dbh->do($request);
100
#            print $request . "\n";
101
            next;
102
        }
103

            
104
# SUPPRESSION DES VOTES PASSÉS ET AJOUT DES VOTES SUIVANTS
105
        foreach my $slot_id (keys(%{$slots->{$poll_id}})) {
106
            next if (! exists $slots->{$poll_id}->{$slot_id}->{to_delete});
107
            $request = "delete from $tables{slots} where poll_id = '$poll_id' and title = '$slots->{$poll_id}->{$slot_id}->{title}'"; 
108
            $dbh->do($request);
109
#            print $request . "\n";            
110
            if ($polls->{$category}->{$poll_id}->{recurrent} > 0) {
111
                $last_date += $polls->{$category}->{$poll_id}->{recurrent} * 86400;
112
                my $new_date = strftime "%s", localtime($last_date);
113
                $request = "insert into $tables{slots} (poll_id, title, moments) values('$slots->{$poll_id}->{$slot_id}->{poll_id}', '$new_date', NULL)";
114
                $dbh->do($request);
115
#                print $request . "\n";
116
                foreach my $uniqId (keys(%{$votes->{$poll_id}})) {
117
                    my $vote = substr $votes->{$poll_id}->{$uniqId}->{choices}, 1;
118
                    if ($vote eq "") {
119
                        $request = "delete from $tables{votes} where uniqId = '$uniqId'";
120
                        delete $votes->{$poll_id}->{$uniqId};
121
                        $dbh->do($request);
122
#                        print $request . "\n";
123
                    }
124
                    else {
125
                        $request = "update $tables{votes} set choices='$vote' where uniqId = '$uniqId'";
126
                        $dbh->do($request);
127
#                        print $request . "\n";
128
                    }
129
                }
130
            }
131
        }
132
    }
133
}
134
# commit ineffective with AutoCommit enabled
135
# $dbh->commit();
136
$dbh->disconnect();
137

            
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
138
my ($mail,$error)=Email::Send::SMTP::Gmail->new(%{$cfg::config{notification}{from}});
139
die "session error: $error" unless ($mail!=-1);
initial commit
admin cloud-section (root) authored on 2016-12-10
140

            
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
141
foreach my $dest (keys(%{$cfg::config{notification}->{to}})) {
142

            
ajout de la possibilité de m...
admin cloud-section (root) authored on 2017-02-13
143
    my $data = '';
144

            
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
145
    if (substr($cfg::config{notification}->{to}->{$dest}->{intro}, 0, 1) eq "@"
146
    and -r $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix}) {
147
        open INTRO, $cfg::config{notification}->{intro_default_path} . substr($cfg::config{notification}->{to}->{$dest}->{intro},1) . $cfg::config{notification}->{intro_suffix};
ajout de la possibilité de m...
admin cloud-section (root) authored on 2017-02-13
148
        while (<INTRO>) {
149
            if (/^<body /../^<\/body>/) {
150
                $data .= $_ unless /^<body / or /^<\/body>/;
151
            }
152
        }
153
        close INTRO;
154
    }
155
    else {
156
        $data .= $cfg::config{notification}->{to}->{$dest}->{intro};
157
    }
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
158

            
159
    foreach my $category (keys(%{$polls})) {
160
        next unless (keys%{$polls->{$category}});
161
        $data .= "<h1>$category</h1>";
162
        foreach my $poll_id (keys(%{$polls->{$category}})) {
163
            my $title    = $polls->{$category}->{$poll_id}->{title};
164
            if (exists $cfg::config{notification}->{to}->{$dest}->{admin}) {
165
                my $admin_id = $polls->{$category}->{$poll_id}->{admin_id};
166
                $data .= "<a href=$cfg::config{url}/adminstuds.php?poll=$admin_id>$title</a><br>";
167
            }
168
            else {
169
                $data .= "<a href=$cfg::config{url}/studs.php?poll=$poll_id>$title</a><br>";
ajout de la description des ...
admin cloud-section (root) authored on 2017-02-13
170
                $data .= $polls->{$category}->{$poll_id}->{description} . "<br><br>";
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
171
            }
initial commit
admin cloud-section (root) authored on 2016-12-10
172
        }
173
    }
174

            
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
175
    $data .= $cfg::config{notification}->{to}->{$dest}->{outro};
176

            
177
    utf8::decode($data);
178

            
initial commit
admin cloud-section (root) authored on 2016-12-10
179
    $mail->send(
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
180
            -from=>$cfg::config{notification}{from_infos}{from},
181
            -replyto=>$cfg::config{notification}{from_infos}{replyto},
sms/action: envoi du mail de...
admin cloud-section (root) authored on 2017-02-17
182
            -to=>$cfg::config{notification}->{to}->{$dest}->{address},
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
183
            -subject=>$cfg::config{notification}->{to}->{$dest}->{subject},
initial commit
admin cloud-section (root) authored on 2016-12-10
184
            -contenttype=>'text/html',
modification dans la partie ...
admin cloud-section (root) authored on 2016-12-17
185
            -body=>$data
initial commit
admin cloud-section (root) authored on 2016-12-10
186
        );
187
}
188
$mail->bye();