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