Switches photo handler to new User model, and rewrites the method flow to handle...
[platal.git] / modules / marketing.php
CommitLineData
10b057be 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
10b057be 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class MarketingModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
27 'marketing' => $this->make_hook('marketing', AUTH_MDP, 'admin'),
28 'marketing/promo' => $this->make_hook('promo', AUTH_MDP, 'admin'),
29 'marketing/relance' => $this->make_hook('relance', AUTH_MDP, 'admin'),
30 'marketing/this_week' => $this->make_hook('week', AUTH_MDP, 'admin'),
31 'marketing/volontaire' => $this->make_hook('volontaire', AUTH_MDP, 'admin'),
32
33 'marketing/private' => $this->make_hook('private', AUTH_MDP, 'admin'),
34 'marketing/public' => $this->make_hook('public', AUTH_COOKIE),
d216d84a 35 'marketing/broken' => $this->make_hook('broken', AUTH_COOKIE),
10b057be 36 );
37 }
38
39 function handler_marketing(&$page)
40 {
10b057be 41 $page->changeTpl('marketing/index.tpl');
42
46f272fe 43 $page->setTitle('Marketing');
10b057be 44
45 // Quelques statistiques
46
08cce2ff 47 $res = XDB::query(
10b057be 48 "SELECT COUNT(*) AS vivants,
49 COUNT(NULLIF(perms='admin' OR perms='user', 0)) AS inscrits,
50 100*COUNT(NULLIF(perms='admin' OR perms='user', 0))/COUNT(*) AS ins_rate,
51 COUNT(NULLIF(promo >= 1972, 0)) AS vivants72,
52 COUNT(NULLIF(promo >= 1972 AND (perms='admin' OR perms='user'), 0)) AS inscrits72,
53 100 * COUNT(NULLIF(promo >= 1972 AND (perms='admin' OR perms='user'), 0)) /
54 COUNT(NULLIF(promo >= 1972, 0)) AS ins72_rate,
55 COUNT(NULLIF(FIND_IN_SET('femme', flags), 0)) AS vivantes,
56 COUNT(NULLIF(FIND_IN_SET('femme', flags) AND (perms='admin' OR perms='user'), 0)) AS inscrites,
57 100 * COUNT(NULLIF(FIND_IN_SET('femme', flags) AND (perms='admin' OR perms='user'), 0)) /
58 COUNT(NULLIF(FIND_IN_SET('femme', flags), 0)) AS inse_rate
59 FROM auth_user_md5
60 WHERE deces = 0");
61 $stats = $res->fetchOneAssoc();
62 $page->assign('stats', $stats);
63
08cce2ff 64 $res = XDB::query("SELECT count(*) FROM auth_user_md5 WHERE date_ins > ".
10b057be 65 date('Ymd000000', strtotime('1 week ago')));
66 $page->assign('nbInsSem', $res->fetchOneCell());
67
08cce2ff 68 $res = XDB::query("SELECT count(*) FROM register_pending WHERE hash != 'INSCRIT'");
10b057be 69 $page->assign('nbInsEnCours', $res->fetchOneCell());
70
08cce2ff 71 $res = XDB::query("SELECT count(*) FROM register_marketing");
10b057be 72 $page->assign('nbInsMarket', $res->fetchOneCell());
73
08cce2ff 74 $res = XDB::query("SELECT count(*) FROM register_mstats
10b057be 75 WHERE TO_DAYS(NOW()) - TO_DAYS(success) <= 7");
76 $page->assign('nbInsMarkOK', $res->fetchOneCell());
10b057be 77 }
78
926edcd1 79 function handler_private(&$page, $uid = null,
80 $action = null, $value = null)
10b057be 81 {
115c90db 82 global $globals;
10b057be 83 $page->changeTpl('marketing/private.tpl');
84
85 if (is_null($uid)) {
86 return PL_NOT_FOUND;
87 }
88
926edcd1 89 $page->assign('path', 'marketing/private/'.$uid);
90
e654517d 91 $res = XDB::query("SELECT nom, prenom, promo, matricule
92 FROM auth_user_md5
93 WHERE user_id={?} AND perms='pending'", $uid);
10b057be 94
95 if (list($nom, $prenom, $promo, $matricule) = $res->fetchOneRow()) {
433336f3 96 require_once('user.func.inc.php');
10b057be 97 $matricule_X = get_X_mat($matricule);
0025a0c5 98 $page->assign('nom', $nom);
99 $page->assign('prenom', $prenom);
100 $page->assign('promo', $promo);
101 $page->assign('matricule', $matricule);
10b057be 102 $page->assign('matricule_X',$matricule_X);
103 } else {
104 $page->kill('uid invalide');
105 }
106
926edcd1 107 if ($action == 'del') {
ce8ec5a3 108 S::assert_xsrf_token();
e654517d 109 Marketing::clear($uid, $value);
10b057be 110 }
111
926edcd1 112 if ($action == 'rel') {
e654517d 113 $market = Marketing::get($uid, $value);
114 if ($market == null) {
a7d35093 115 $page->trigWarning("Aucun marketing n'a été effectué vers $value");
e654517d 116 } else {
117 $to = $market->user['to'];
118 $title = $market->getTitle();
119 $text = $market->getText();
120 $from = $market->sender_mail;
121 $page->assign('rel_from_user', $from);
122 $page->assign('rel_from_staff',
1d55fe45 123 '"Equipe Polytechnique.org" <register@' . $globals->mail->domain . '>');
e654517d 124 $page->assign('rel_to', $to);
125 $page->assign('rel_title', $title);
126 $page->assign('rel_text', $text);
127 $page->assign('rel_email', $value);
128 }
10b057be 129 }
130
926edcd1 131 if ($action == 'relforce') {
ce8ec5a3
VZ
132 S::assert_xsrf_token();
133
e654517d 134 $market = Marketing::get($uid, Post::v('to'));
135 if (is_null($market)) {
136 $market = new Marketing($uid, Post::v('to'), 'default', null, 'staff');
137 }
138 $market->send(Post::v('title'), Post::v('message'));
faefdbb7 139 $page->trigSuccess("Email envoyé");
10b057be 140 }
141
926edcd1 142 if ($action == 'insrel') {
ce8ec5a3 143 S::assert_xsrf_token();
e654517d 144 if (Marketing::relance($uid)) {
a7d35093 145 $page->trigSuccess('relance faite');
10b057be 146 }
147 }
148
926edcd1 149 if ($action == 'add' && Post::has('email') && Post::has('type')) {
e654517d 150 $market = new Marketing($uid, Post::v('email'), 'default', null, Post::v('type'), S::v('uid'));
151 $market->add(false);
10b057be 152 }
153
08cce2ff 154 $res = XDB::iterator(
10b057be 155 "SELECT r.*, a.alias
156 FROM register_marketing AS r
157 INNER JOIN aliases AS a ON (r.sender=a.id AND a.type = 'a_vie')
158 WHERE uid={?}
159 ORDER BY date", $uid);
160 $page->assign('addr', $res);
161
08cce2ff 162 $res = XDB::query("SELECT date, relance FROM register_pending
e654517d 163 WHERE uid = {?}", $uid);
54f962cd 164 if (list($pending, $relance) = $res->fetchOneRow()) {
0025a0c5 165 $page->assign('pending', $pending);
166 $page->assign('relance', $relance);
10b057be 167 }
10b057be 168 }
169
d216d84a 170 function handler_broken(&$page, $uid = null)
171 {
172 $page->changeTpl('marketing/broken.tpl');
173
174 if (is_null($uid)) {
c441b5ed 175 return PL_NOT_FOUND;
176 }
35211d07
VZ
177
178 $user = User::get($uid);
179 if (!$user) {
c441b5ed 180 return PL_NOT_FOUND;
35211d07 181 } elseif ($user->login() == S::user()->login()) {
c441b5ed 182 pl_redirect('emails/redirect');
d216d84a 183 }
184
0d693e2f 185 $res = Xdb::query("SELECT u.nom, u.prenom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe,
35211d07 186 u.deces = '0000-00-00' AS alive, u.hruid, a.alias AS forlife, b.alias AS bestalias,
0e5ec860 187 IF(e.email IS NOT NULL, e.email, IF(FIND_IN_SET('googleapps', u.mail_storage), 'googleapps', NULL)) AS email, e.last
d216d84a 188 FROM auth_user_md5 AS u
0d693e2f 189 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
190 INNER JOIN aliases AS b ON (b.id = u.user_id AND FIND_IN_SET('bestalias', b.flags))
191 LEFT JOIN emails AS e ON (e.flags = 'active' AND e.uid = u.user_id)
35211d07
VZ
192 WHERE u.hruid = {?}
193 ORDER BY e.panne_level, e.last", $user->login());
d216d84a 194 if (!$res->numRows()) {
c441b5ed 195 return PL_NOT_FOUND;
d216d84a 196 }
35211d07
VZ
197 $user_data = $res->fetchOneAssoc();
198 $page->assign('user', $user_data);
d216d84a 199
0d693e2f 200 $email = null;
c6310567 201 require_once 'emails.inc.php';
0d693e2f 202 if (Post::has('mail')) {
0d693e2f 203 $email = valide_email(Post::v('mail'));
204 }
205 if (Post::has('valide') && isvalid_email_redirection($email)) {
ce8ec5a3
VZ
206 S::assert_xsrf_token();
207
0d693e2f 208 // security stuff
35211d07
VZ
209 check_email($email, "Proposition d'une adresse surveillee pour " . $user->login() . " par " . S::user()->login());
210 $res = XDB::query("SELECT flags
211 FROM emails
212 WHERE email = {?} AND uid = {?}", $email, $user->id());
0381e170 213 $state = $res->numRows() ? $res->fetchOneCell() : null;
214 if ($state == 'panne') {
35211d07 215 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user_data['prenom']} et est en panne.");
0381e170 216 } elseif ($state == 'active') {
35211d07
VZ
217 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user_data['prenom']}");
218 } elseif ($user_data['email'] && !trim(Post::v('comment'))) {
a7d35093 219 $page->trigError("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
35211d07 220 . "besoin de changer la redirection de " . $user_data['prenom']);
0d693e2f 221 } else {
222 require_once 'validations.inc.php';
35211d07 223 $valid = new BrokenReq(S::i('uid'), $user_data, $email, trim(Post::v('comment')));
0d693e2f 224 $valid->submit();
225 $page->assign('sent', true);
226 }
227 } elseif ($email) {
a7d35093 228 $page->trigError("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
d216d84a 229 }
230 }
231
10b057be 232 function handler_promo(&$page, $promo = null)
233 {
10b057be 234 $page->changeTpl('marketing/promo.tpl');
235
0025a0c5 236 if (is_null($promo)) {
cab08090 237 $promo = S::v('promo');
10b057be 238 }
239 $page->assign('promo', $promo);
240
241 $sql = "SELECT u.user_id, u.nom, u.prenom, u.last_known_email, u.matricule_ax,
e654517d 242 IF(MAX(m.last) > p.relance, MAX(m.last), p.relance) AS dern_rel, p.email
10b057be 243 FROM auth_user_md5 AS u
244 LEFT JOIN register_pending AS p ON p.uid = u.user_id
245 LEFT JOIN register_marketing AS m ON m.uid = u.user_id
246 WHERE u.promo = {?} AND u.deces = 0 AND u.perms='pending'
247 GROUP BY u.user_id
248 ORDER BY nom, prenom";
08cce2ff 249 $page->assign('nonins', XDB::iterator($sql, $promo));
10b057be 250 }
251
252 function handler_public(&$page, $uid = null)
253 {
10b057be 254 $page->changeTpl('marketing/public.tpl');
255
256 if (is_null($uid)) {
257 return PL_NOT_FOUND;
258 }
259
08cce2ff 260 $res = XDB::query("SELECT nom, prenom, promo FROM auth_user_md5
10b057be 261 WHERE user_id={?} AND perms='pending'", $uid);
262
263 if (list($nom, $prenom, $promo) = $res->fetchOneRow()) {
264 $page->assign('prenom', $prenom);
265 $page->assign('nom', $nom);
266 $page->assign('promo', $promo);
267
7c8a4874 268 if (Post::has('valide')) {
92e00e50 269 S::assert_xsrf_token();
5e2307dc 270 $email = trim(Post::v('mail'));
8ded5b5e 271
c6310567 272 require_once 'emails.inc.php';
10b057be 273 if (!isvalid_email_redirection($email)) {
a7d35093 274 $page->trigError("Email invalide !");
10b057be 275 } else {
8ded5b5e 276 // On cherche les marketings précédents sur cette adresse
277 // email, en se restreignant au dernier mois
eaf30d86 278
8ded5b5e 279 if (Marketing::get($uid, $email, true)) {
280 $page->assign('already', true);
281 } else {
282 $page->assign('ok', true);
283 check_email($email, "Une adresse surveillée est proposée au marketing par " . S::v('forlife'));
284 $market = new Marketing($uid, $email, 'default', null, Post::v('origine'), S::v('uid'));
285 $market->add();
286 }
10b057be 287 }
288 }
289 }
10b057be 290 }
291
7c8a4874 292 function handler_week(&$page, $sorting = 'per_promo')
10b057be 293 {
10b057be 294 $page->changeTpl('marketing/this_week.tpl');
295
7c8a4874 296 $sort = $sorting == 'per_promo' ? 'promo' : 'date_ins';
10b057be 297
298 $sql = "SELECT a.alias AS forlife, u.date_ins, u.promo, u.nom, u.prenom
299 FROM auth_user_md5 AS u
300 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
301 WHERE u.date_ins > ".date("Ymd000000", strtotime ('1 week ago'))."
302 ORDER BY u.$sort DESC";
08cce2ff 303 $page->assign('ins', XDB::iterator($sql));
10b057be 304 }
305
7c8a4874 306 function handler_volontaire(&$page, $promo = null)
10b057be 307 {
10b057be 308 $page->changeTpl('marketing/volontaire.tpl');
309
08cce2ff 310 $res = XDB::query(
10b057be 311 "SELECT
312 DISTINCT a.promo
313 FROM register_marketing AS m
314 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid
315 ORDER BY a.promo");
316 $page->assign('promos', $res->fetchColumn());
317
318
7c8a4874 319 if (!is_null($promo)) {
10b057be 320 $sql = "SELECT a.nom, a.prenom, a.user_id,
321 m.email, sa.alias AS forlife
322 FROM register_marketing AS m
323 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid AND a.promo = {?}
324 INNER JOIN aliases AS sa ON (m.sender = sa.id AND sa.type='a_vie')
325 ORDER BY a.nom";
08cce2ff 326 $page->assign('addr', XDB::iterator($sql, $promo));
10b057be 327 }
10b057be 328 }
329
330 function handler_relance(&$page)
331 {
10b057be 332 $page->changeTpl('marketing/relance.tpl');
333
334 if (Post::has('relancer')) {
08cce2ff 335 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
10b057be 336 $nbdix = $res->fetchOneCell();
337
338 $sent = Array();
339 foreach (array_keys($_POST['relance']) as $uid) {
e654517d 340 if ($tmp = Marketing::relance($uid, $nbdix)) {
a7de4ef7 341 $sent[] = $tmp.' a été relancé';
10b057be 342 }
343 }
344 $page->assign('sent', $sent);
345 }
346
347 $sql = "SELECT r.date, r.relance, r.uid, u.promo, u.nom, u.prenom
348 FROM register_pending AS r
349 INNER JOIN auth_user_md5 AS u ON r. uid = u.user_id
350 WHERE hash!='INSCRIT'
351 ORDER BY date DESC";
08cce2ff 352 $page->assign('relance', XDB::iterator($sql));
10b057be 353 }
354}
355
a7de4ef7 356// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
10b057be 357?>