Removes boggus getAliveUsersCount() function, fixes alive user count in marketing...
[platal.git] / modules / marketing.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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),
35 'marketing/broken' => $this->make_hook('broken', AUTH_COOKIE),
36 );
37 }
38
39 function handler_marketing(&$page)
40 {
41 $page->changeTpl('marketing/index.tpl');
42
43 $page->setTitle('Marketing');
44 $page->trigWarning("Les statistiques sont momentanéement désactivées");
45 }
46
47 function handler_private(&$page, $hruid = null,
48 $action = null, $value = null)
49 {
50 global $globals;
51 $page->changeTpl('marketing/private.tpl');
52
53 $user = User::getSilent($hruid);
54 if (!$user) {
55 return PL_NOT_FOUND;
56 }
57
58 // Retrieves marketed user details.
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');
64 }
65 $matricule = $user->profile()->xorg_id;
66
67 $matricule_X = Profile::getSchoolId($matricule);
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.
75 if ($action == 'del') {
76 S::assert_xsrf_token();
77 Marketing::clear($user->id(), $value);
78 }
79
80 if ($action == 'rel') {
81 $market = Marketing::get($user->id(), $value);
82 if ($market == null) {
83 $page->trigWarning("Aucun marketing n'a été effectué vers $value");
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',
91 '"Equipe Polytechnique.org" <register@' . $globals->mail->domain . '>');
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 }
97 }
98
99 if ($action == 'relforce') {
100 S::assert_xsrf_token();
101
102 $market = Marketing::get($user->id(), Post::v('to'));
103 if (is_null($market)) {
104 $market = new Marketing($user->id(), Post::v('to'), 'default', null, 'staff');
105 }
106 $market->send(Post::v('title'), Post::v('message'));
107 $page->trigSuccess("Email envoyé");
108 }
109
110 if ($action == 'insrel') {
111 S::assert_xsrf_token();
112 if (Marketing::relance($user->id())) {
113 $page->trigSuccess('relance faite');
114 }
115 }
116
117 if ($action == 'add' && Post::has('email') && Post::has('type')) {
118 $market = new Marketing($user->id(), Post::v('email'), 'default', null, Post::v('type'), S::v('uid'));
119 $market->add(false);
120 }
121
122 // Retrieves and display the existing marketing attempts.
123 $res = XDB::iterator(
124 "SELECT r.*, a.alias
125 FROM register_marketing AS r
126 LEFT JOIN aliases AS a ON (r.sender = a.uid AND a.type = 'a_vie')
127 WHERE r.uid = {?}
128 ORDER BY date", $user->id());
129 $page->assign('addr', $res);
130
131 $res = XDB::query("SELECT date, relance FROM register_pending
132 WHERE uid = {?}", $user->id());
133 if (list($pending, $relance) = $res->fetchOneRow()) {
134 $page->assign('pending', $pending);
135 $page->assign('relance', $relance);
136 }
137
138 $page->assign('path', 'marketing/private/' . $user->login());
139 }
140
141 function handler_broken(&$page, $uid = null)
142 {
143 $page->changeTpl('marketing/broken.tpl');
144
145 if (is_null($uid)) {
146 return PL_NOT_FOUND;
147 }
148
149 $user = User::get($uid);
150 if (!$user) {
151 return PL_NOT_FOUND;
152 } elseif ($user->login() == S::user()->login()) {
153 pl_redirect('emails/redirect');
154 }
155
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());
165 if (!$res->numRows()) {
166 return PL_NOT_FOUND;
167 }
168 $user->addProperties($res->fetchOneAssoc());
169 $page->assign('user', $user);
170
171 $email = null;
172 require_once 'emails.inc.php';
173 if (Post::has('mail')) {
174 $email = valide_email(Post::v('mail'));
175 }
176 if (Post::has('valide') && isvalid_email_redirection($email)) {
177 S::assert_xsrf_token();
178
179 // security stuff
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());
184 $state = $res->numRows() ? $res->fetchOneCell() : null;
185 if ($state == 'panne') {
186 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()} et est en panne.");
187 } elseif ($state == 'active') {
188 $page->trigWarning("L'adresse que tu as fournie est l'adresse actuelle de {$user->fullName()}");
189 } elseif ($user->email && !trim(Post::v('comment'))) {
190 $page->trigError("Il faut que tu ajoutes un commentaire à ta proposition pour justifier le "
191 . "besoin de changer la redirection de {$user->fullName()}.");
192 } else {
193 require_once 'validations.inc.php';
194 $valid = new BrokenReq(S::user(), $user, $email, trim(Post::v('comment')));
195 $valid->submit();
196 $page->assign('sent', true);
197 }
198 } elseif ($email) {
199 $page->trigError("L'adresse proposée n'est pas une adresse acceptable pour une redirection");
200 }
201 }
202
203 function handler_promo(&$page, $promo = null)
204 {
205 $page->changeTpl('marketing/promo.tpl');
206
207 if (is_null($promo)) {
208 $promo = S::v('promo', S::user()->promo());
209 }
210 $page->assign('promo', $promo);
211
212 $uf = new UserFilter(new PFC_And(new UFC_Promo('=', UserFilter::DISPLAY, $promo),
213 new PFC_Not(new UFC_Registered())),
214 array(new UFO_Name(Profile::LASTNAME), new UFO_Name(Profile::FIRSTNAME)));
215 $users = $uf->getUsers();
216 $page->assign('nonins', $users);
217 }
218
219 function handler_public(&$page, $hruid = null)
220 {
221 $page->changeTpl('marketing/public.tpl');
222
223 // Retrieves the user info, and checks the user is not yet registered.
224 $user = User::getSilent($hruid);
225 if (!$user || !$user->hasProfile()) {
226 return PL_NOT_FOUND;
227 }
228
229 if ($user->state != 'pending') {
230 $page->kill('Cet utilisateur est déjà inscrit');
231 }
232
233 // Displays the page, and handles the eventual user actions.
234 $page->assign('full_name', $user->fullName());
235 $page->assign('promo', $user->promo());
236
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)) {
243 $page->trigError('Email invalide&nbsp;!');
244 } else {
245 // On cherche les marketings précédents sur cette adresse
246 // email, en se restreignant au dernier mois
247
248 if (Marketing::get($user->id(), $email, true)) {
249 $page->assign('already', true);
250 } else {
251 $page->assign('ok', true);
252 check_email($email, "Une adresse surveillée est proposée au marketing par " . S::user()->login());
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);
255 $market->add();
256 }
257 }
258 } else {
259 global $globals;
260 require_once 'marketing.inc.php';
261
262 $sender = User::getSilent(S::v('uid'));
263 $market = new AnnuaireMarketing(null, true);
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 ));
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);
273 $page->assign('text', nl2br($text));
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());
277 }
278 }
279
280 function handler_week(&$page, $sorting = 'per_promo')
281 {
282 $page->changeTpl('marketing/this_week.tpl');
283
284 $sort = $sorting == 'per_promo' ? new UFO_Promo() : new UFO_Registration();
285
286 $uf = new UserFilter(new UFC_Registered(false, '>', strtotime('1 week ago')), $sort);
287 $page->assign('users', $uf->getUsers());
288 }
289
290 function handler_volontaire(&$page, $promo = null)
291 {
292 $page->changeTpl('marketing/volontaire.tpl');
293
294 $res = XDB::query(
295 'SELECT DISTINCT pd.promo
296 FROM register_marketing AS m
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');
300 $page->assign('promos', $res->fetchColumn());
301
302
303 if (!is_null($promo)) {
304 $it = XDB::iterator('SELECT m.uid, m.email, s.alias AS forlife
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 LEFT JOIN aliases AS s ON (m.sender = s.uid AND s.type = \'a_vie\')
309 WHERE pd.promo = {?}
310 ORDER BY pd.sort_name', $promo);
311 $page->assign('addr', $it);
312 }
313 }
314
315 function handler_relance(&$page)
316 {
317 $page->changeTpl('marketing/relance.tpl');
318
319 if (Post::has('relancer')) {
320 global $globals;
321 $nbdix = $globals->core->NbIns;
322
323 $sent = Array();
324 $users = User::getBulkUsersWithUIDs($_POST['relance']);
325 foreach ($users as $user) {
326 if ($tmp = Marketing::relance($user, $nbdix)) {
327 $sent[] = $tmp . ' a été relancé.';
328 }
329 }
330 $page->assign('sent', $sent);
331 }
332
333 $page->assign('relance', XDB::iterator('SELECT r.date, r.relance, r.uid
334 FROM register_pending AS r
335 WHERE hash != \'INSCRIT\'
336 ORDER BY date DESC'));
337 }
338 }
339
340 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
341 ?>