forked from GoteoFoundation/goteo-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailing.php
333 lines (285 loc) · 14.7 KB
/
mailing.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/*
* Copyright (C) 2012 Platoniq y Fundación Fuentes Abiertas (see README for details)
* This file is part of Goteo.
*
* Goteo 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.
*
* Goteo 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 Goteo. If not, see <http://www.gnu.org/licenses/agpl.txt>.
*
*/
namespace Goteo\Controller\Admin {
use Goteo\Core\View,
Goteo\Core\Redirection,
Goteo\Core\Error,
Goteo\Library\Message,
Goteo\Library\Feed,
Goteo\Library\Template,
Goteo\Library\Mail,
Goteo\Library\Newsletter,
Goteo\Model;
class Mailing {
public static function process ($action = 'list', $id = null, $filters = array()) {
// año fiscal
$year = Model\User\Donor::$currYear;
$year0 = $year;
$year1 = $year-1;
$errors = array();
$node = isset($_SESSION['admin_node']) ? $_SESSION['admin_node'] : \GOTEO_NODE;
// Valores de filtro
$interests = Model\User\Interest::getAll();
$status = Model\Project::status();
$methods = Model\Invest::methods();
$types = array(
'investor' => 'Cofinanciadores',
'owner' => 'Autores',
'user' => 'Usuarios'
);
$roles = array(
'admin' => 'Administrador',
'checker' => 'Revisor',
'translator' => 'Traductor'
);
// una variable de sesion para mantener los datos de todo esto
if (!isset($_SESSION['mailing'])) {
$_SESSION['mailing'] = array();
}
switch ($action) {
case 'edit':
$_SESSION['mailing']['receivers'] = array();
$values = array();
$sqlFields = '';
$sqlInner = '';
$sqlFilter = '';
// cargamos los destiantarios
//----------------------------
// por tipo de usuario
switch ($filters['type']) {
case 'investor':
$sqlInner .= "INNER JOIN invest
ON invest.user = user.id
AND (invest.status = 0 OR invest.status = 1 OR invest.status = 3 OR invest.status = 4)
INNER JOIN project
ON project.id = invest.project
";
$sqlFields .= ", project.name as project";
$sqlFields .= ", project.id as projectId";
break;
case 'owner':
$sqlInner .= "INNER JOIN project
ON project.owner = user.id
";
$sqlFields .= ", project.name as project";
$sqlFields .= ", project.id as projectId";
break;
default :
break;
}
$_SESSION['mailing']['filters_txt'] = 'los <strong>' . $types[$filters['type']] . '</strong> ';
if (!empty($filters['project']) && !empty($sqlInner)) {
$sqlFilter .= " AND project.name LIKE (:project) ";
$values[':project'] = '%'.$filters['project'].'%';
$_SESSION['mailing']['filters_txt'] .= 'de proyectos que su nombre contenga <strong>\'' . $filters['project'] . '\'</strong> ';
} elseif (empty($filters['project']) && !empty($sqlInner)) {
$_SESSION['mailing']['filters_txt'] .= 'de cualquier proyecto ';
}
if (isset($filters['status']) && $filters['status'] > -1 && !empty($sqlInner)) {
$sqlFilter .= "AND project.status = :status ";
$values[':status'] = $filters['status'];
$_SESSION['mailing']['filters_txt'] .= 'en estado <strong>' . $status[$filters['status']] . '</strong> ';
} elseif ($filters['status'] < 0 && !empty($sqlInner)) {
$_SESSION['mailing']['filters_txt'] .= 'en cualquier estado ';
}
if ($filters['type'] == 'investor') {
if (!empty($filters['method']) && !empty($sqlInner)) {
$sqlFilter .= "AND invest.method = :method ";
$values[':method'] = $filters['method'];
$_SESSION['mailing']['filters_txt'] .= 'mediante <strong>' . $methods[$filters['method']] . '</strong> ';
} elseif (empty($filters['method']) && !empty($sqlInner)) {
$_SESSION['mailing']['filters_txt'] .= 'mediante cualquier metodo ';
}
}
if (!empty($filters['interest'])) {
$sqlInner .= "INNER JOIN user_interest
ON user_interest.user = user.id
AND user_interest.interest = :interest
";
$values[':interest'] = $filters['interest'];
if ($filters['interest'] == 15) {
$_SESSION['mailing']['filters_txt'] .= 'del grupo de testeo ';
} else {
$_SESSION['mailing']['filters_txt'] .= 'interesados en fin <strong>' . $interests[$filters['interest']] . '</strong> ';
}
}
if (!empty($filters['role'])) {
$sqlInner .= "INNER JOIN user_role
ON user_role.user_id = user.id
AND user_role.role_id = :role
";
$values[':role'] = $filters['role'];
$_SESSION['mailing']['filters_txt'] .= 'que sean <strong>' . $roles[$filters['role']] . '</strong> ';
}
if (!empty($filters['name'])) {
$sqlFilter .= " AND ( user.name LIKE (:name) OR user.email LIKE (:name) ) ";
$values[':name'] = '%'.$filters['name'].'%';
$_SESSION['mailing']['filters_txt'] .= 'que su nombre o email contenga <strong>\'' . $filters['name'] . '\'</strong> ';
}
if (!empty($filters['donant'])) {
if ($filters['type'] == 'investor') {
$sqlFilter .= " AND invest.resign = 1
AND invest.status IN (1, 3)
AND invest.charged >= '{$year0}-01-01'
AND invest.charged < '{$year1}-01-01'
AND (project.passed IS NOT NULL AND project.passed != '0000-00-00')
";
$_SESSION['mailing']['filters_txt'] .= 'que haya hecho algun donativo ';
} else {
Message::Error('Solo se filtran donantes si se envia "A los: Cofinanciadores"');
}
}
if ($node != \GOTEO_NODE) {
$sqlFilter .= " AND user.node = :node";
$values[':node'] = $node;
if (!empty($sqlInner)) {
$sqlFilter .= " AND project.node = :node";
}
}
$sql = "SELECT
user.id as id,
user.id as user,
user.name as name,
user.email as email
$sqlFields
FROM user
$sqlInner
WHERE user.active = 1
$sqlFilter
GROUP BY user.id
ORDER BY user.name ASC
";
// die('<pre>'.$sql . '<br />'.print_r($values, 1).'</pre>');
if ($query = Model\User::query($sql, $values)) {
foreach ($query->fetchAll(\PDO::FETCH_OBJ) as $receiver) {
$_SESSION['mailing']['receivers'][$receiver->id] = $receiver;
}
} else {
Message::Error('Fallo el SQL!!!!! <br />' . $sql . '<pre>'.print_r($values, 1).'</pre>');
}
// si no hay destinatarios, salta a la lista con mensaje de error
if (empty($_SESSION['mailing']['receivers'])) {
Message::Error('No se han encontrado destinatarios para ' . $_SESSION['mailing']['filters_txt']);
throw new Redirection('/admin/mailing/list');
}
// si hay, mostramos el formulario de envio
return new View(
'view/admin/index.html.php',
array(
'folder' => 'mailing',
'file' => 'edit',
'filters' => $filters,
'interests' => $interests,
'status' => $status,
'types' => $types,
'roles' => $roles
)
);
break;
case 'send':
// die(\trace($_POST));
$URL = (NODE_ID != GOTEO_NODE) ? NODE_URL : SITE_URL;
// Enviando contenido recibido a destinatarios recibidos
$receivers = array();
$subject = $_POST['subject'];
$templateId = !empty($_POST['template']) ? $_POST['template'] : 11;
$content = \str_replace('%SITEURL%', $URL, $_POST['content']);
// quito usuarios desmarcados
foreach ($_SESSION['mailing']['receivers'] as $usr=>$userData) {
$errors = array();
$campo = 'receiver_'.$usr;
if (!isset($_POST[$campo])) {
$_SESSION['mailing']['receivers'][$usr]->ok = null;
} else {
$receivers[] = $userData;
}
}
// montamos el mailing
// - se crea un registro de tabla mail
$sql = "INSERT INTO mail (id, email, html, template, node) VALUES ('', :email, :html, :template, :node)";
$values = array (
':email' => 'any',
':html' => $content,
':template' => $templateId,
':node' => $node
);
$query = \Goteo\Core\Model::query($sql, $values);
$mailId = \Goteo\Core\Model::insertId();
// - se usa el metodo initializeSending para grabar el envío (parametro para autoactivar)
// - initiateSending ($mailId, $subject, $receivers, $autoactive = 0)
if (\Goteo\Library\Sender::initiateSending($mailId, $subject, $receivers, 1)) {
$ok = true;
// Evento Feed
$log = new Feed();
$log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing',
\vsprintf("El admin %s ha iniciado una %s a %s", array(
Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id),
Feed::item('relevant', 'Comunicacion masiva'),
$_SESSION['mailing']['filters_txt']
)));
$log->doAdmin('admin');
unset($log);
} else {
$ok = false;
// Evento Feed
$log = new Feed();
$log->populate('comunicación masiva a usuarios (admin)', '/admin/mailing',
\vsprintf("El admin %s le ha %s una %s a %s", array(
Feed::item('user', $_SESSION['user']->name, $_SESSION['user']->id),
Feed::item('relevant', 'fallado'),
Feed::item('relevant', 'Comunicacion masiva'),
$_SESSION['mailing']['filters_txt']
)));
$log->doAdmin('admin');
unset($log);
}
return new View(
'view/admin/index.html.php',
array(
'folder' => 'mailing',
'file' => 'send',
'subject' => $subject,
'interests' => $interests,
'status' => $status,
'methods' => $methods,
'types' => $types,
'roles' => $roles,
'users' => $receivers,
'ok' => $ok
)
);
break;
}
return new View(
'view/admin/index.html.php',
array(
'folder' => 'mailing',
'file' => 'list',
'interests' => $interests,
'status' => $status,
'methods' => $methods,
'types' => $types,
'roles' => $roles,
'filters' => $filters
)
);
}
}
}