-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-monitoramento-notifica.php
214 lines (177 loc) · 7.16 KB
/
wp-monitoramento-notifica.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php /* -*- Mode: php; c-basic-offset:4; -*- */
/* Copyright (C) 2014 Sergio Berlotto <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* This module holds all functions that exposes post attributes and post
* related information. This is an attempt to improve the modularization
* of this plugin.
*/
header('Content-Type:text/html; charset=utf-8');
include '../../../wp-load.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Notificações - De Olho nas Obras</title>
<style>
table{
width: 100%;
}
table th{
background-color: #5A5A5A;
color: #fff;
}
table tr:hover{
background-color: rgba(130,130,130,.3);
}
table{
border: 1px solid black;
}
td{
border-right: 1px dashed red;
padding-right: 8px;
}
.warning{
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<?php
$mail_recipients = array("[email protected]","[email protected]");
function calcula_data_r2($sdataR2){
#Formato de
$dias_intervalo = 32;
// $sdataR2 = "2014-01-28";
// $dataR2 = DateTime::createFromFormat('Y-m-d', $sdataR2);
// $dataR2 = DateTime::createFromFormat('d/m/Y', $sdataR2);
$hoje = date_create();
// $intervalo = date_diff($hoje, $dataR2);
// $out = $intervalo->format("%Y Years, %M Months and %d Days");
// $dias = $intervalo->y * 365.25 + $intervalo->m * 30 + $intervalo->d + $intervalo->h/24 + $intervalo->i / 60;
// Usa a função criada e pega o timestamp das duas datas:
$time_inicial = strtotime($sdataR2);
$time_final = strtotime($hoje->format('Y-m-d'));
// Calcula a diferença de segundos entre as duas datas:
$diferenca = $time_final - $time_inicial;
// Calcula a diferença de dias
$dias = (int)floor( $diferenca / (60 * 60 * 24));
// echo "<br>$dias dias de diferenca";
$resto = $dias % $dias_intervalo;
$prox = clone $hoje;
$prox->add(new DateInterval("P".$resto."D"));
$datas = array();
$datas['proxima'] = $prox;
$ant = clone $prox;
$ant->sub(new DateInterval("P".$dias_intervalo."D"));
$datas['anterior'] = $ant;
$penultima = clone $ant;
$penultima->sub(new DateInterval("P".($dias_intervalo)."D"));
$datas['penultima'] = $penultima;
return $datas;
}
function eh_dia_de_aviso($database){
//Esta rotina verifica se é dia de avisar a alguem do atraso na atualização
//da obra.
$periodicidade = 3; #em dias
// $dataR2 = DateTime::createFromFormat('Y-m-d', $database);
$hoje = date_create();
$data_aviso = clone $database;
while (true){
if($data_aviso == $hoje){
return true;
break;
}
if($data_aviso > $hoje){
return false;
break;
}
$data_aviso->add(new DateInterval("P".$periodicidade."D"));
}
}
//Busca as obras que não tiveram ainda Governo Informa entre a ultima R2 e a
global $wpdb;
$sql = $wpdb->prepare(" SELECT * FROM (
SELECT p.id, p.post_title title, DATE(p.post_date) post_date, p.post_status, p.post_parent,
p.post_type, pai.post_title parent_title, pai.id parent_id, pai.post_name parent_slug
FROM wp_posts p join wp_posts pai on p.post_parent = pai.id
WHERE p.id in (
SELECT object_id FROM wp_term_relationships
WHERE term_taxonomy_id = (
SELECT a.term_taxonomy_id
FROM `wp_term_taxonomy` a JOIN wp_terms b on a.term_id = b.term_id
WHERE b.name = 'post-format-status')
)
AND p.post_type = 'gdobra'
ORDER BY pai.post_title, p.post_date DESC
) t GROUP BY parent_title");
$obras = $wpdb->get_results($sql);
?>
<table>
<tr>
<th>Id</th>
<th>PaiID</th>
<th>Obra</th>
<th>Ult. Status</th>
<th>Data Status</th>
<th>Data Penultima R2</th>
<th>Data Ultima R2</th>
<th>Data Proxima R2</th>
</tr>
<?php
$obra_atrasada_subject = get_option("gd_obra_atrasada_subject");
$obra_atrasada_msg = get_option("gd_obra_atrasada_msg");
$baseurl = get_option("gd_base_url");
$headers = 'From: De Olho nas Obras <[email protected]>' . "\r\n";
foreach($obras as $obra){
$pdate = new DateTime($obra->post_date);
$data_ultima_r2_obra = get_post_custom_values("gdobra_ultima_r2", $obra->parent_id);
// print "<tr><td colspan=3>$obra->parent_id,$obra->parent_title,".gettype($data_ultima_r2_obra[0])."</td></tr>";
// print "<tr><td colspan=3>".$data_ultima_r2_obra[0]."</td></tr>";
if( $data_ultima_r2_obra != null ){
// echo "NAO EMPTY";
$data = calcula_data_r2($data_ultima_r2_obra[0]);
// echo "Proxima R2: ", $data['proxima']->format('d/m/Y');
// echo "<BR>R2 Anterior: ", $data['anterior']->format('d/m/Y');
// echo "<BR>R2 Penultima: ", $data['penultima']->format('d/m/Y');
$url = $baseurl."/deolho/obra/".$obra->parent_slug;
echo "<tr><td>$obra->id</td><td>$obra->parent_id</td><td><a href='$url' target='_blank'>$obra->parent_title</a>";
if ( $pdate < $data['penultima'] ){ #Se a obra está com sua atualização atrasada
if ( eh_dia_de_aviso($data['anterior']) ){ #avisa somente no dia ou de 3 em 3 dias
echo " <span class='warning'>[ATRASADA]</span>";
$to = get_post_custom_values("gdobra_atrasada_emails1", $obra->parent_id);
if( $to != null ){
// Set destination address(es)
// Accept either a comma-separated list or an array
$to = explode( ',', implode( ',', (array) $to ) );
foreach ( $to as $to_recipient ) {
echo "<br><span>Emails para: $to_recipient</span>";
wp_mail( $to_recipient, $obra_atrasada_subject, sprintf($obra_atrasada_msg, $obra->title, $url), $headers );
}
}
}
}
echo "</td><td>$obra->title</td><td>", $pdate->format("Y-m-d"), "</td>";
echo "<td>",$data['penultima']->format('Y-m-d'),"</td><td>",$data['anterior']->format('Y-m-d'),"</td><td>",$data['proxima']->format('Y-m-d'),"</td></tr>";
}
}
wp_reset_query();
?>
</table>
</body>
</html>