Commit | Line | Data |
---|---|---|
10b057be | 1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 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 | ||
22 | class 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'); |
46f272fe | 42 | $page->setTitle('Marketing'); |
20eca0a9 SJ |
43 | |
44 | $alive = new UserFilter(new PFC_Not(new UFC_Dead())); | |
45 | $registered = new UserFilter(new PFC_And(new UFC_Registered(), new PFC_Not(new UFC_Dead()))); | |
46 | $alive72 = new UserFilter(new PFC_And(new UFC_Promo('>=', UserFilter::GRADE_ING, 1972), new PFC_Not(new UFC_Dead()))); | |
47 | $registered72 = new UserFilter(new PFC_And(new UFC_Registered(), new UFC_Promo('>=', UserFilter::GRADE_ING, 1972), new PFC_Not(new UFC_Dead()))); | |
48 | $aliveWomen = new UserFilter(new PFC_And(new UFC_Sex(User::GENDER_FEMALE) , new PFC_Not(new UFC_Dead()))); | |
49 | $registeredWomen = new UserFilter(new PFC_And(new UFC_Registered(), new UFC_Sex(User::GENDER_FEMALE), new PFC_Not(new UFC_Dead()))); | |
50 | $statistics = array( | |
51 | 'alive' => $alive->getTotalCount(), | |
52 | 'registered' => $registered->getTotalCount(), | |
53 | 'alive72' => $alive72->getTotalCount(), | |
54 | 'registered72' => $registered72->getTotalCount(), | |
55 | 'womenAlive' => $aliveWomen->getTotalCount(), | |
56 | 'womenRegistered' => $registeredWomen->getTotalCount(), | |
57 | ); | |
58 | $statistics['registeredRate'] = $statistics['registered'] / $statistics['alive'] * 100; | |
59 | $statistics['registeredRate72'] = $statistics['registered72'] / $statistics['alive72'] * 100; | |
60 | $statistics['womenRegisteredRate'] = $statistics['womenRegistered'] / $statistics['womenAlive'] * 100; | |
61 | ||
62 | $registeredWeek = new UserFilter(new PFC_And(new UFC_Registered(false, '>=', strtotime('1 week ago')), new PFC_Not(new UFC_Dead()))); | |
63 | $registrationPending = XDB::fetchOneCell('SELECT COUNT(*) | |
64 | FROM register_pending'); | |
65 | $registrations = array( | |
66 | 'week' => $registeredWeek->getTotalCount(), | |
67 | 'pending' => $registrationPending, | |
68 | ); | |
69 | ||
70 | $ok = XDB::fetchOneCell('SELECT COUNT(*) | |
71 | FROM register_mstats | |
72 | WHERE success != \'0000-00-00\''); | |
73 | $okWeek = XDB::fetchOneCell('SELECT COUNT(*) | |
74 | FROM register_mstats | |
75 | WHERE success >= {?}', strtotime('1 week ago')); | |
76 | $res = XDB::fetchAllAssoc('SELECT type, COUNT(*) as count | |
77 | FROM register_marketing | |
78 | GROUP BY type'); | |
79 | $no = array(); | |
80 | foreach ($res as $value) { | |
81 | $no[$value['type']] = $value['count']; | |
82 | } | |
83 | $no['week'] = XDB::fetchOneCell('SELECT COUNT(*) | |
84 | FROM register_marketing | |
85 | WHERE last >= {?}', strtotime('1 week ago')); | |
86 | $marketings = array( | |
87 | 'ok' => $ok, | |
88 | 'okWeek' => $okWeek, | |
89 | 'noPerso' => (isset($no['user']) ? $no['user'] : 0), | |
90 | 'noXorg' => (isset($no['staff']) ? $no['staff'] : 0), | |
91 | 'noAX' => (isset($no['ax']) ? $no['ax'] : 0), | |
92 | 'noWeek' => $no['week'], | |
93 | ); | |
94 | ||
95 | $page->assign('statistics', $statistics); | |
96 | $page->assign('registrations', $registrations); | |
97 | $page->assign('marketings', $marketings); | |
10b057be | 98 | } |
99 | ||
2455266e | 100 | function handler_private(&$page, $hruid = null, |
926edcd1 | 101 | $action = null, $value = null) |
10b057be | 102 | { |
115c90db | 103 | global $globals; |
10b057be | 104 | $page->changeTpl('marketing/private.tpl'); |
105 | ||
2455266e VZ |
106 | $user = User::getSilent($hruid); |
107 | if (!$user) { | |
10b057be | 108 | return PL_NOT_FOUND; |
109 | } | |
110 | ||
2455266e | 111 | // Retrieves marketed user details. |
38c6fe96 FB |
112 | if ($user->state != 'pending') { |
113 | $page->kill('Cet utilisateur est déjà inscrit'); | |
114 | } | |
115 | if (!$user->hasProfile()) { | |
116 | $page->kill('Cet utilisateur n\'est pas concerné par le marketing'); | |
10b057be | 117 | } |
38c6fe96 | 118 | $matricule = $user->profile()->xorg_id; |
10b057be | 119 | |
69b46857 | 120 | $matricule_X = Profile::getSchoolId($matricule); |
2455266e VZ |
121 | |
122 | $page->assign('full_name', $user->fullName()); | |
123 | $page->assign('promo', $user->promo()); | |
124 | $page->assign('matricule', $matricule); | |
125 | $page->assign('matricule_X',$matricule_X); | |
126 | ||
127 | // Applies in-parameter action to the user. | |
926edcd1 | 128 | if ($action == 'del') { |
ce8ec5a3 | 129 | S::assert_xsrf_token(); |
2455266e | 130 | Marketing::clear($user->id(), $value); |
10b057be | 131 | } |
132 | ||
926edcd1 | 133 | if ($action == 'rel') { |
2455266e | 134 | $market = Marketing::get($user->id(), $value); |
e654517d | 135 | if ($market == null) { |
a7d35093 | 136 | $page->trigWarning("Aucun marketing n'a été effectué vers $value"); |
e654517d | 137 | } else { |
138 | $to = $market->user['to']; | |
139 | $title = $market->getTitle(); | |
140 | $text = $market->getText(); | |
141 | $from = $market->sender_mail; | |
142 | $page->assign('rel_from_user', $from); | |
143 | $page->assign('rel_from_staff', | |
1d55fe45 | 144 | '"Equipe Polytechnique.org" <register@' . $globals->mail->domain . '>'); |
e654517d | 145 | $page->assign('rel_to', $to); |
146 | $page->assign('rel_title', $title); | |
147 | $page->assign('rel_text', $text); | |
148 | $page->assign('rel_email', $value); | |
149 | } | |
10b057be | 150 | } |
151 | ||
926edcd1 | 152 | if ($action == 'relforce') { |
ce8ec5a3 VZ |
153 | S::assert_xsrf_token(); |
154 | ||
2455266e | 155 | $market = Marketing::get($user->id(), Post::v('to')); |
e654517d | 156 | if (is_null($market)) { |
2455266e | 157 | $market = new Marketing($user->id(), Post::v('to'), 'default', null, 'staff'); |
e654517d | 158 | } |
159 | $market->send(Post::v('title'), Post::v('message')); | |
faefdbb7 | 160 | $page->trigSuccess("Email envoyé"); |
10b057be | 161 | } |
162 | ||
926edcd1 | 163 | if ($action == 'insrel') { |
ce8ec5a3 | 164 | S::assert_xsrf_token(); |
2455266e | 165 | if (Marketing::relance($user->id())) { |
a7d35093 | 166 | $page->trigSuccess('relance faite'); |
10b057be | 167 | } |
168 | } | |
169 | ||
926edcd1 | 170 | if ($action == 'add' && Post::has('email') && Post::has('type')) { |
2455266e | 171 | $market = new Marketing($user->id(), Post::v('email'), 'default', null, Post::v('type'), S::v('uid')); |
e654517d | 172 | $market->add(false); |
10b057be | 173 | } |
174 | ||
2455266e | 175 | // Retrieves and display the existing marketing attempts. |
08cce2ff | 176 | $res = XDB::iterator( |
10b057be | 177 | "SELECT r.*, a.alias |
178 | FROM register_marketing AS r | |
9082ac5f SJ |
179 | LEFT JOIN aliases AS a ON (r.sender = a.uid AND a.type = 'a_vie') |
180 | WHERE r.uid = {?} | |
2455266e | 181 | ORDER BY date", $user->id()); |
10b057be | 182 | $page->assign('addr', $res); |
183 | ||
08cce2ff | 184 | $res = XDB::query("SELECT date, relance FROM register_pending |
2455266e | 185 | WHERE uid = {?}", $user->id()); |
54f962cd | 186 | if (list($pending, $relance) = $res->fetchOneRow()) { |
0025a0c5 | 187 | $page->assign('pending', $pending); |
188 | $page->assign('relance', $relance); | |
10b057be | 189 | } |
2455266e VZ |
190 | |
191 | $page->assign('path', 'marketing/private/' . $user->login()); | |
10b057be | 192 | } |
193 | ||
d216d84a | 194 | function handler_broken(&$page, $uid = null) |
195 | { | |
196 | $page->changeTpl('marketing/broken.tpl'); | |
197 | ||
198 | if (is_null($uid)) { | |
c441b5ed | 199 | return PL_NOT_FOUND; |
200 | } | |
35211d07 VZ |
201 | |
202 | $user = User::get($uid); | |
203 | if (!$user) { | |
c441b5ed | 204 | return PL_NOT_FOUND; |
35211d07 | 205 | } elseif ($user->login() == S::user()->login()) { |
c441b5ed | 206 | pl_redirect('emails/redirect'); |
d216d84a | 207 | } |
208 | ||
0001ba7a FB |
209 | $res = XDB::query('SELECT p.deathdate IS NULL AS alive, e.last, |
210 | IF(e.email IS NOT NULL, e.email, | |
211 | IF(FIND_IN_SET(\'googleapps\', eo.storage), \'googleapps\', NULL)) AS email | |
212 | FROM email_options AS eo | |
213 | LEFT JOIN account_profiles AS ap ON (ap.uid = eo.uid AND FIND_IN_SET(\'owner\', ap.perms)) | |
214 | LEFT JOIN profiles AS p ON (p.pid = ap.pid) | |
215 | LEFT JOIN emails AS e ON (e.flags = \'active\' AND e.uid = eo.uid) | |
216 | WHERE eo.uid = {?} | |
217 | ORDER BY e.panne_level, e.last', $user->id()); | |
d216d84a | 218 | if (!$res->numRows()) { |
c441b5ed | 219 | return PL_NOT_FOUND; |
d216d84a | 220 | } |
5daf68f6 VZ |
221 | $user->addProperties($res->fetchOneAssoc()); |
222 | $page->assign('user', $user); | |
d216d84a | 223 | |
0d693e2f | 224 | $email = null; |
c6310567 | 225 | require_once 'emails.inc.php'; |
0d693e2f | 226 | if (Post::has('mail')) { |
0d693e2f | 227 | $email = valide_email(Post::v('mail')); |
228 | } | |
229 | if (Post::has('valide') && isvalid_email_redirection($email)) { | |
ce8ec5a3 VZ |
230 | S::assert_xsrf_token(); |
231 | ||
0d693e2f | 232 | // security stuff |
35211d07 VZ |
233 | check_email($email, "Proposition d'une adresse surveillee pour " . $user->login() . " par " . S::user()->login()); |
234 | $res = XDB::query("SELECT flags | |
235 | FROM emails | |
236 | WHERE email = {?} AND uid = {?}", $email, $user->id()); | |
0381e170 | 237 | $state = $res->numRows() ? $res->fetchOneCell() : null; |
238 | if ($state == 'panne') { | |
5daf68f6 | 239 | $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()} et est en panne."); |
0381e170 | 240 | } elseif ($state == 'active') { |
5daf68f6 VZ |
241 | $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()}"); |
242 | } elseif ($user->email && !trim(Post::v('comment'))) { | |
a7d35093 | 243 | $page->trigError("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le " |
5daf68f6 | 244 | . "besoin de changer la redirection de {$user->fullName()}."); |
0d693e2f | 245 | } else { |
5daf68f6 | 246 | $valid = new BrokenReq(S::user(), $user, $email, trim(Post::v('comment'))); |
0d693e2f | 247 | $valid->submit(); |
248 | $page->assign('sent', true); | |
249 | } | |
250 | } elseif ($email) { | |
a7d35093 | 251 | $page->trigError("L'adresse proposée n'est pas une adresse acceptable pour une redirection"); |
d216d84a | 252 | } |
253 | } | |
254 | ||
10b057be | 255 | function handler_promo(&$page, $promo = null) |
256 | { | |
10b057be | 257 | $page->changeTpl('marketing/promo.tpl'); |
258 | ||
0025a0c5 | 259 | if (is_null($promo)) { |
72318f05 | 260 | $promo = S::v('promo', S::user()->promo()); |
10b057be | 261 | } |
262 | $page->assign('promo', $promo); | |
263 | ||
3c904393 SJ |
264 | $uf = new UserFilter(new PFC_And(new UFC_Promo('=', UserFilter::DISPLAY, $promo), |
265 | new PFC_Not(new UFC_Registered())), | |
913a4e90 | 266 | array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME))); |
38c6fe96 FB |
267 | $users = $uf->getUsers(); |
268 | $page->assign('nonins', $users); | |
10b057be | 269 | } |
270 | ||
2455266e | 271 | function handler_public(&$page, $hruid = null) |
10b057be | 272 | { |
10b057be | 273 | $page->changeTpl('marketing/public.tpl'); |
274 | ||
2455266e VZ |
275 | // Retrieves the user info, and checks the user is not yet registered. |
276 | $user = User::getSilent($hruid); | |
38c6fe96 | 277 | if (!$user || !$user->hasProfile()) { |
10b057be | 278 | return PL_NOT_FOUND; |
279 | } | |
280 | ||
38c6fe96 FB |
281 | if ($user->state != 'pending') { |
282 | $page->kill('Cet utilisateur est déjà inscrit'); | |
2455266e | 283 | } |
10b057be | 284 | |
2455266e VZ |
285 | // Displays the page, and handles the eventual user actions. |
286 | $page->assign('full_name', $user->fullName()); | |
287 | $page->assign('promo', $user->promo()); | |
10b057be | 288 | |
2455266e VZ |
289 | if (Post::has('valide')) { |
290 | S::assert_xsrf_token(); | |
291 | $email = trim(Post::v('mail')); | |
292 | ||
293 | require_once 'emails.inc.php'; | |
294 | if (!isvalid_email_redirection($email)) { | |
6bb2f79a | 295 | $page->trigError('Email invalide !'); |
2455266e VZ |
296 | } else { |
297 | // On cherche les marketings précédents sur cette adresse | |
298 | // email, en se restreignant au dernier mois | |
8ded5b5e | 299 | |
2455266e VZ |
300 | if (Marketing::get($user->id(), $email, true)) { |
301 | $page->assign('already', true); | |
10b057be | 302 | } else { |
2455266e VZ |
303 | $page->assign('ok', true); |
304 | check_email($email, "Une adresse surveillée est proposée au marketing par " . S::user()->login()); | |
232577de SJ |
305 | $market = new Marketing($user->id(), $email, 'default', null, Post::v('origine'), S::v('uid'), |
306 | Post::v('origine') == 'user' ? Post::v('personal_notes') : null); | |
2455266e | 307 | $market->add(); |
10b057be | 308 | } |
309 | } | |
6400e601 SJ |
310 | } else { |
311 | global $globals; | |
312 | require_once 'marketing.inc.php'; | |
313 | ||
314 | $sender = User::getSilent(S::v('uid')); | |
c3e0d3c1 | 315 | $perso_signature = 'Cordialement,<br />-- <br />' . $sender->fullName(); |
232577de | 316 | $market = new AnnuaireMarketing(null, true); |
6400e601 SJ |
317 | $text = $market->getText(array( |
318 | 'sexe' => $user->isFemale(), | |
319 | 'forlife_email' => $user->login() . '@' . $globals->mail->domain, | |
320 | 'forlife_email2' => $user->login() . '@' . $globals->mail->domain2 | |
321 | )); | |
232577de SJ |
322 | $text = str_replace('%%hash%%', '', $text); |
323 | $text = str_replace('%%personal_notes%%', '<em id="personal_notes_display"></em>', $text); | |
324 | $text = str_replace('%%sender%%', | |
c3e0d3c1 | 325 | '<span id="sender">' . $perso_signature . '</span>', $text); |
6400e601 | 326 | $page->assign('text', nl2br($text)); |
c3e0d3c1 SJ |
327 | $page->assign('perso_signature', $perso_signature); |
328 | $page->assign('mail_part', 'escaped_html'); | |
10b057be | 329 | } |
10b057be | 330 | } |
331 | ||
7c8a4874 | 332 | function handler_week(&$page, $sorting = 'per_promo') |
10b057be | 333 | { |
10b057be | 334 | $page->changeTpl('marketing/this_week.tpl'); |
335 | ||
38c6fe96 | 336 | $sort = $sorting == 'per_promo' ? new UFO_Promo() : new UFO_Registration(); |
10b057be | 337 | |
38c6fe96 FB |
338 | $uf = new UserFilter(new UFC_Registered(false, '>', strtotime('1 week ago')), $sort); |
339 | $page->assign('users', $uf->getUsers()); | |
10b057be | 340 | } |
341 | ||
7c8a4874 | 342 | function handler_volontaire(&$page, $promo = null) |
10b057be | 343 | { |
10b057be | 344 | $page->changeTpl('marketing/volontaire.tpl'); |
345 | ||
08cce2ff | 346 | $res = XDB::query( |
38c6fe96 | 347 | 'SELECT DISTINCT pd.promo |
10b057be | 348 | FROM register_marketing AS m |
38c6fe96 FB |
349 | INNER JOIN account_profiles AS ap ON (m.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms)) |
350 | INNER JOIN profile_display AS pd ON (pd.pid = ap.pid) | |
351 | ORDER BY pd.promo'); | |
10b057be | 352 | $page->assign('promos', $res->fetchColumn()); |
353 | ||
354 | ||
7c8a4874 | 355 | if (!is_null($promo)) { |
11f2c0a7 | 356 | $it = XDB::iterator('SELECT m.uid, m.email, s.alias AS forlife |
38c6fe96 FB |
357 | FROM register_marketing AS m |
358 | INNER JOIN account_profiles AS ap ON (m.uid = ap.uid AND FIND_IN_SET(\'owner\', ap.perms)) | |
359 | INNER JOIN profile_display AS pd ON (pd.pid = ap.pid) | |
11f2c0a7 RB |
360 | LEFT JOIN aliases AS s ON (m.sender = s.uid AND s.type = \'a_vie\') |
361 | WHERE pd.promo = {?} | |
362 | ORDER BY pd.sort_name', $promo); | |
38c6fe96 | 363 | $page->assign('addr', $it); |
10b057be | 364 | } |
10b057be | 365 | } |
366 | ||
367 | function handler_relance(&$page) | |
368 | { | |
10b057be | 369 | $page->changeTpl('marketing/relance.tpl'); |
370 | ||
371 | if (Post::has('relancer')) { | |
19721970 SJ |
372 | global $globals; |
373 | $nbdix = $globals->core->NbIns; | |
10b057be | 374 | |
375 | $sent = Array(); | |
38c6fe96 FB |
376 | $users = User::getBulkUsersWithUIDs($_POST['relance']); |
377 | foreach ($users as $user) { | |
378 | if ($tmp = Marketing::relance($user, $nbdix)) { | |
846c391e | 379 | $sent[] = $tmp . ' a été relancé.'; |
10b057be | 380 | } |
381 | } | |
382 | $page->assign('sent', $sent); | |
383 | } | |
384 | ||
38c6fe96 FB |
385 | $page->assign('relance', XDB::iterator('SELECT r.date, r.relance, r.uid |
386 | FROM register_pending AS r | |
387 | WHERE hash != \'INSCRIT\' | |
388 | ORDER BY date DESC')); | |
10b057be | 389 | } |
390 | } | |
391 | ||
a7de4ef7 | 392 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
10b057be | 393 | ?> |