charset html header
[platal.git] / modules / marketing.php
CommitLineData
10b057be 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
43 $page->assign('xorg_title','Polytechnique.org - Marketing');
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 {
10b057be 82 $page->changeTpl('marketing/private.tpl');
83
84 if (is_null($uid)) {
85 return PL_NOT_FOUND;
86 }
87
926edcd1 88 $page->assign('path', 'marketing/private/'.$uid);
89
e654517d 90 $res = XDB::query("SELECT nom, prenom, promo, matricule
91 FROM auth_user_md5
92 WHERE user_id={?} AND perms='pending'", $uid);
10b057be 93
94 if (list($nom, $prenom, $promo, $matricule) = $res->fetchOneRow()) {
433336f3 95 require_once('user.func.inc.php');
10b057be 96 $matricule_X = get_X_mat($matricule);
0025a0c5 97 $page->assign('nom', $nom);
98 $page->assign('prenom', $prenom);
99 $page->assign('promo', $promo);
100 $page->assign('matricule', $matricule);
10b057be 101 $page->assign('matricule_X',$matricule_X);
102 } else {
103 $page->kill('uid invalide');
104 }
105
926edcd1 106 if ($action == 'del') {
e654517d 107 Marketing::clear($uid, $value);
10b057be 108 }
109
926edcd1 110 if ($action == 'rel') {
e654517d 111 $market = Marketing::get($uid, $value);
112 if ($market == null) {
113 $page->trig("Aucun marketing n'a été effectué vers $value");
114 } else {
115 $to = $market->user['to'];
116 $title = $market->getTitle();
117 $text = $market->getText();
118 $from = $market->sender_mail;
119 $page->assign('rel_from_user', $from);
120 $page->assign('rel_from_staff',
1d55fe45 121 '"Equipe Polytechnique.org" <register@' . $globals->mail->domain . '>');
e654517d 122 $page->assign('rel_to', $to);
123 $page->assign('rel_title', $title);
124 $page->assign('rel_text', $text);
125 $page->assign('rel_email', $value);
126 }
10b057be 127 }
128
926edcd1 129 if ($action == 'relforce') {
e654517d 130 $market = Marketing::get($uid, Post::v('to'));
131 if (is_null($market)) {
132 $market = new Marketing($uid, Post::v('to'), 'default', null, 'staff');
133 }
134 $market->send(Post::v('title'), Post::v('message'));
a7de4ef7 135 $page->trig("Mail envoyé");
10b057be 136 }
137
926edcd1 138 if ($action == 'insrel') {
e654517d 139 if (Marketing::relance($uid)) {
10b057be 140 $page->trig('relance faite');
141 }
142 }
143
926edcd1 144 if ($action == 'add' && Post::has('email') && Post::has('type')) {
e654517d 145 $market = new Marketing($uid, Post::v('email'), 'default', null, Post::v('type'), S::v('uid'));
146 $market->add(false);
10b057be 147 }
148
08cce2ff 149 $res = XDB::iterator(
10b057be 150 "SELECT r.*, a.alias
151 FROM register_marketing AS r
152 INNER JOIN aliases AS a ON (r.sender=a.id AND a.type = 'a_vie')
153 WHERE uid={?}
154 ORDER BY date", $uid);
155 $page->assign('addr', $res);
156
08cce2ff 157 $res = XDB::query("SELECT date, relance FROM register_pending
e654517d 158 WHERE uid = {?}", $uid);
54f962cd 159 if (list($pending, $relance) = $res->fetchOneRow()) {
0025a0c5 160 $page->assign('pending', $pending);
161 $page->assign('relance', $relance);
10b057be 162 }
10b057be 163 }
164
d216d84a 165 function handler_broken(&$page, $uid = null)
166 {
c441b5ed 167 require_once('user.func.inc.php');
d216d84a 168 $page->changeTpl('marketing/broken.tpl');
169
170 if (is_null($uid)) {
c441b5ed 171 return PL_NOT_FOUND;
172 }
173 $forlife = get_user_forlife($uid);
174 if (!$forlife) {
175 return PL_NOT_FOUND;
176 } elseif ($forlife == S::v('forlife')) {
177 pl_redirect('emails/redirect');
d216d84a 178 }
179
0d693e2f 180 $res = Xdb::query("SELECT u.nom, u.prenom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe,
181 a.alias AS forlife, b.alias AS bestalias, e.email, e.last
d216d84a 182 FROM auth_user_md5 AS u
0d693e2f 183 INNER JOIN aliases AS a ON (a.id = u.user_id AND a.type = 'a_vie')
184 INNER JOIN aliases AS b ON (b.id = u.user_id AND FIND_IN_SET('bestalias', b.flags))
185 LEFT JOIN emails AS e ON (e.flags = 'active' AND e.uid = u.user_id)
186 WHERE a.alias = {?}
187 ORDER BY e.panne_level, e.last", $forlife);
d216d84a 188 if (!$res->numRows()) {
c441b5ed 189 return PL_NOT_FOUND;
d216d84a 190 }
191 $user = $res->fetchOneAssoc();
192 $page->assign('user', $user);
193
0d693e2f 194 $email = null;
195 if (Post::has('mail')) {
196 require_once 'emails.inc.php';
197 $email = valide_email(Post::v('mail'));
198 }
199 if (Post::has('valide') && isvalid_email_redirection($email)) {
200 // security stuff
201 check_email($email, "Proposition d'une adresse surveillee pour " . $user['forlife'] . " par " . S::v('forlife'));
0381e170 202 $res = XDB::query("SELECT state
203 FROM emails AS e
204 INNER JOIN aliases AS a ON (a.id = e.uid)
205 WHERE e.email = {?} AND a.alias = {?}", $email, $user['forlife']);
206 $state = $res->numRows() ? $res->fetchOneCell() : null;
207 if ($state == 'panne') {
208 $page->trig("L'adresse que tu as fournie est l'adresse actuelle de {$user['prenom']} et est en panne.");
209 } elseif ($state == 'active') {
210 $page->trig("L'adresse que tu as fournie est l'adresse actuelle de {$user['prenom']}");
211 } elseif ($user['email'] && !trim(Post::v('comment'))) {
a7de4ef7 212 $page->trig("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
0d693e2f 213 ."besoin de changer la redirection de " . $user['prenom']);
214 } else {
215 require_once 'validations.inc.php';
216 $valid = new BrokenReq(S::i('uid'), $user, $email, trim(Post::v('comment')));
217 $valid->submit();
218 $page->assign('sent', true);
219 }
220 } elseif ($email) {
a7de4ef7 221 $page->trig("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
d216d84a 222 }
223 }
224
10b057be 225 function handler_promo(&$page, $promo = null)
226 {
10b057be 227 $page->changeTpl('marketing/promo.tpl');
228
0025a0c5 229 if (is_null($promo)) {
cab08090 230 $promo = S::v('promo');
10b057be 231 }
232 $page->assign('promo', $promo);
233
234 $sql = "SELECT u.user_id, u.nom, u.prenom, u.last_known_email, u.matricule_ax,
e654517d 235 IF(MAX(m.last) > p.relance, MAX(m.last), p.relance) AS dern_rel, p.email
10b057be 236 FROM auth_user_md5 AS u
237 LEFT JOIN register_pending AS p ON p.uid = u.user_id
238 LEFT JOIN register_marketing AS m ON m.uid = u.user_id
239 WHERE u.promo = {?} AND u.deces = 0 AND u.perms='pending'
240 GROUP BY u.user_id
241 ORDER BY nom, prenom";
08cce2ff 242 $page->assign('nonins', XDB::iterator($sql, $promo));
10b057be 243 }
244
245 function handler_public(&$page, $uid = null)
246 {
10b057be 247 $page->changeTpl('marketing/public.tpl');
248
249 if (is_null($uid)) {
250 return PL_NOT_FOUND;
251 }
252
08cce2ff 253 $res = XDB::query("SELECT nom, prenom, promo FROM auth_user_md5
10b057be 254 WHERE user_id={?} AND perms='pending'", $uid);
255
256 if (list($nom, $prenom, $promo) = $res->fetchOneRow()) {
257 $page->assign('prenom', $prenom);
258 $page->assign('nom', $nom);
259 $page->assign('promo', $promo);
260
7c8a4874 261 if (Post::has('valide')) {
10b057be 262 require_once('xorg.misc.inc.php');
5e2307dc 263 $email = trim(Post::v('mail'));
799cdbcd 264 $market = Marketing::get($uid, $email);
10b057be 265 if (!isvalid_email_redirection($email)) {
266 $page->trig("Email invalide !");
e654517d 267 } elseif ($market) {
10b057be 268 $page->assign('already', true);
269 } else {
270 $page->assign('ok', true);
a7de4ef7 271 check_email($email, "Une adresse surveillée est proposée au marketing par " . S::v('forlife'));
e654517d 272 $market = new Marketing($uid, $email, 'default', null, Post::v('origine'), S::v('uid'));
273 $market->add();
10b057be 274 }
275 }
276 }
10b057be 277 }
278
7c8a4874 279 function handler_week(&$page, $sorting = 'per_promo')
10b057be 280 {
10b057be 281 $page->changeTpl('marketing/this_week.tpl');
282
7c8a4874 283 $sort = $sorting == 'per_promo' ? 'promo' : 'date_ins';
10b057be 284
285 $sql = "SELECT a.alias AS forlife, u.date_ins, u.promo, u.nom, u.prenom
286 FROM auth_user_md5 AS u
287 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
288 WHERE u.date_ins > ".date("Ymd000000", strtotime ('1 week ago'))."
289 ORDER BY u.$sort DESC";
08cce2ff 290 $page->assign('ins', XDB::iterator($sql));
10b057be 291 }
292
7c8a4874 293 function handler_volontaire(&$page, $promo = null)
10b057be 294 {
10b057be 295 $page->changeTpl('marketing/volontaire.tpl');
296
08cce2ff 297 $res = XDB::query(
10b057be 298 "SELECT
299 DISTINCT a.promo
300 FROM register_marketing AS m
301 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid
302 ORDER BY a.promo");
303 $page->assign('promos', $res->fetchColumn());
304
305
7c8a4874 306 if (!is_null($promo)) {
10b057be 307 $sql = "SELECT a.nom, a.prenom, a.user_id,
308 m.email, sa.alias AS forlife
309 FROM register_marketing AS m
310 INNER JOIN auth_user_md5 AS a ON a.user_id = m.uid AND a.promo = {?}
311 INNER JOIN aliases AS sa ON (m.sender = sa.id AND sa.type='a_vie')
312 ORDER BY a.nom";
08cce2ff 313 $page->assign('addr', XDB::iterator($sql, $promo));
10b057be 314 }
10b057be 315 }
316
317 function handler_relance(&$page)
318 {
10b057be 319 $page->changeTpl('marketing/relance.tpl');
320
321 if (Post::has('relancer')) {
08cce2ff 322 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
10b057be 323 $nbdix = $res->fetchOneCell();
324
325 $sent = Array();
326 foreach (array_keys($_POST['relance']) as $uid) {
e654517d 327 if ($tmp = Marketing::relance($uid, $nbdix)) {
a7de4ef7 328 $sent[] = $tmp.' a été relancé';
10b057be 329 }
330 }
331 $page->assign('sent', $sent);
332 }
333
334 $sql = "SELECT r.date, r.relance, r.uid, u.promo, u.nom, u.prenom
335 FROM register_pending AS r
336 INNER JOIN auth_user_md5 AS u ON r. uid = u.user_id
337 WHERE hash!='INSCRIT'
338 ORDER BY date DESC";
08cce2ff 339 $page->assign('relance', XDB::iterator($sql));
10b057be 340 }
341}
342
a7de4ef7 343// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
10b057be 344?>