Allows the marketer to add some personal text to the marketing email (Closes #946).
[platal.git] / include / marketing.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 Marketing
23 {
24 static private $engines = array(
25 //user name => array(class name, require data)
26 'annuaire' => array('AnnuaireMarketing', false),
27 'groupe' => array('GroupMarketing', true),
28 'liste' => array('ListMarketing', true),
29 );
30
31 private $engine;
32 public $sender_mail;
33 public $user;
34
35 private $type;
36 private $data;
37 private $from;
38 private $sender;
39 private $personal_notes;
40
41 private $hash = '';
42
43 public function __construct($uid, $email, $type, $data, $from, $sender = null, $personal_notes = null)
44 {
45 $this->user = $this->getUser($uid, $email);
46 $this->sender_mail = $this->getFrom($from, $sender);
47 $this->engine =& $this->getEngine($type, $data, $from == 'user' ? $sender : null, $personal_notes);
48
49 $this->type = $type;
50 $this->data = $data;
51 $this->from = $from;
52 $this->sender = $sender;
53 $this->personal_notes = $personal_notes;
54 }
55
56 private function getUser($uid, $email)
57 {
58 $user = User::getSilent($uid);
59 if (!$user) {
60 return null;
61 }
62
63 global $globals;
64 return array(
65 'user' => $user,
66 'id' => $user->id(),
67 'sexe' => $user->isFemale(),
68 'mail' => $email,
69 'to' => '"' . $user->fullName() . '" <' . $email . '>',
70 'forlife_email' => $user->login() . '@' . $globals->mail->domain,
71 'forlife_email2' => $user->login() . '@' . $globals->mail->domain2,
72 );
73 }
74
75 private function getFrom($from, $sender)
76 {
77 global $globals;
78
79 if ($from == 'staff' || !($user = User::getSilent($sender))) {
80 return '"L\'équipe de Polytechnique.org" <register@' . $globals->mail->domain . '>';
81 }
82 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
83 }
84
85 private function &getEngine($type, $data, $from, $personal_notes)
86 {
87 $class = $type . 'Marketing';
88 if (!class_exists($class, false)) {
89 $class= 'DefaultMarketing';
90 }
91 $engine = new $class($data, $from, $personal_notes);
92 if (!$engine instanceof MarketingEngine) {
93 $engine = null;
94 }
95 return $engine;
96 }
97
98 public function getTitle()
99 {
100 return $this->engine->getTitle();
101 }
102
103 public function getText()
104 {
105 return $this->engine->getText($this->user);
106 }
107
108 public function send($title = null, $text = null)
109 {
110 $this->hash = rand_url_id(12);
111 if (!$title) {
112 $title = $this->engine->getTitle();
113 }
114 if (!$text) {
115 $text = $this->engine->getText($this->user);
116 }
117 $sender = substr($this->sender_mail, 1, strpos($this->sender_mail, '"', 2)-1);
118 $text = str_replace(array('%%hash%%', '%%sender%%', '%%personal_notes%%'),
119 array($this->hash, $this->sender_mail, ''), $text);
120 $mailer = new PlMailer();
121 $mailer->setFrom($this->sender_mail);
122 $mailer->addTo($this->user['mail']);
123 $mailer->setSubject($title);
124 $mailer->setTxtBody($text);
125 $mailer->send();
126 $this->incr();
127 }
128
129 public function add($valid = true)
130 {
131 XDB::execute('INSERT IGNORE INTO register_marketing
132 (uid, sender, email, date, last, nb, type, hash, message, message_data, personal_notes)
133 VALUES ({?}, {?}, {?}, NOW(), 0, 0, {?}, {?}, {?}, {?}, {?})',
134 $this->user['id'], $this->sender, $this->user['mail'], $this->from, $this->hash,
135 $this->type, $this->data, $this->personal_notes);
136 $this->engine->process($this->user);
137 if ($valid) {
138 require_once 'validations.inc.php';
139 $valid = new MarkReq(User::getSilent($this->sender), $this->user['user'], $this->user['mail'],
140 $this->from == 'user', $this->type, $this->data, $this->personal_notes);
141 $valid->submit();
142 }
143 return true;
144 }
145
146 private function incr()
147 {
148 XDB::execute('UPDATE register_marketing
149 SET nb=nb+1, hash={?}, last=NOW()
150 WHERE uid={?} AND email={?}',
151 $this->hash, $this->user['id'], $this->user['mail']);
152 }
153
154 static public function getEngineList($exclude_data = true)
155 {
156 $array = array();
157 foreach (Marketing::$engines as $e => $d) {
158 if (!$d[1] || !$exclude_data) {
159 $array[] = $e;
160 }
161 }
162 return $array;
163 }
164
165 static public function get($uid, $email, $recentOnly = false)
166 {
167 $res = XDB::query("SELECT uid, email, message, message_data, type, sender, personal_notes
168 FROM register_marketing
169 WHERE uid = {?}
170 AND email = {?}".(
171 $recentOnly ? ' AND DATEDIFF(NOW(), last) < 30' : ''), $uid, $email);
172
173 if ($res->numRows() == 0) {
174 return null;
175 }
176 list ($uid, $email, $type, $data, $from, $senderi, $personal_notes) = $res->fetchOneRow();
177 return new Marketing($uid, $email, $type, $data, $from, $sender, $personal_notes);
178 }
179
180 static public function clear($uid, $email = null)
181 {
182 if (!$email) {
183 XDB::execute("DELETE FROM register_marketing WHERE uid = {?}", $uid);
184 } else {
185 XDB::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
186 }
187 }
188
189 static public function relance($uid, $nbx = -1)
190 {
191 global $globals;
192
193 if ($nbx < 0) {
194 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE deces=0");
195 $nbx = $res->fetchOneCell();
196 }
197
198 $res = XDB::query("SELECT r.date, u.promo, u.nom, u.prenom, r.email, r.bestalias
199 FROM register_pending AS r
200 INNER JOIN auth_user_md5 AS u ON u.user_id = r.uid
201 WHERE hash != 'INSCRIT' AND uid = {?} AND
202 (TO_DAYS(relance) IS NULL OR TO_DAYS(relance) < TO_DAYS(NOW()))",
203 $uid);
204 if (!list($date, $promo, $nom, $prenom, $email, $alias) = $res->fetchOneRow()) {
205 return false;
206 }
207
208 require_once('secure_hash.inc.php');
209 $hash = rand_url_id(12);
210 $pass = rand_pass();
211 $pass_encrypted = hash_encrypt($pass);
212 $fdate = strftime('%d %B %Y', strtotime($date));
213
214 $mymail = new PlMailer('marketing/relance.mail.tpl');
215 $mymail->assign('nbdix', $nbx);
216 $mymail->assign('fdate', $fdate);
217 $mymail->assign('lusername', $alias);
218 $mymail->assign('nveau_pass', $pass);
219 $mymail->assign('baseurl', $globals->baseurl);
220 $mymail->assign('lins_id', $hash);
221 $mymail->assign('lemail', $email);
222 $mymail->assign('subj', $alias.'@'.$globals->mail->domain);
223 $mymail->send();
224 XDB::execute('UPDATE register_pending
225 SET hash={?}, password={?}, relance=NOW()
226 WHERE uid={?}', $hash, $pass_encrypted, $uid);
227 return "$prenom $nom ($promo)";
228 }
229 }
230
231 interface MarketingEngine
232 {
233 public function __construct($data, $from, $personal_notes = null);
234 public function getTitle();
235 public function getText(array $user);
236 public function process(array $user);
237 }
238
239 class AnnuaireMarketing implements MarketingEngine
240 {
241 protected $titre;
242 protected $intro;
243 protected $signature;
244 protected $personal_notes;
245
246 public function __construct($data, $from, $personal_notes = null)
247 {
248 $this->titre = "Rejoins la communauté polytechnicienne sur Internet";
249 $this->intro = " Tu n'as pas de fiche dans l'annuaire des polytechniciens sur Internet. "
250 . "Pour y figurer, il te suffit de visiter cette page ou de copier cette adresse "
251 . "dans la barre de ton navigateur :";
252 if ($from === null) {
253 $this->signature = "L'équipe de Polytechnique.org,\n"
254 . "Le portail des élèves & anciens élèves de l'École polytechnique";
255 } else {
256 $this->signature = "%%sender%%";
257 }
258 if (is_null($personal_notes) || $personal_notes == '') {
259 $this->personal_notes = '%%personal_notes%%';
260 } else {
261 $this->personal_notes = "\n" . $personal_notes . "\n";
262 }
263 }
264
265 public function getTitle()
266 {
267 return $this->titre;
268 }
269
270 private function getIntro()
271 {
272 return $this->intro;
273 }
274
275 public function getSignature()
276 {
277 return $this->signature;
278 }
279
280 public function getPersonalNotes()
281 {
282 return $this->personal_notes;
283 }
284
285 protected function prepareText(PlPage &$page, array $user)
286 {
287 $page->assign('intro', $this->getIntro());
288 $page->assign('u', $user);
289 $page->assign('sign', $this->getSignature());
290 $res = XDB::query("SELECT COUNT(*) FROM auth_user_md5 WHERE perms IN ('user', 'admin') AND deces = 0");
291 $page->assign('num_users', $res->fetchOneCell());
292 $page->assign('personal_notes', $this->getPersonalNotes());
293 }
294
295 public function getText(array $user)
296 {
297 $page = new XorgPage();
298 $page->changeTpl('marketing/marketing.mail.tpl', NO_SKIN);
299 $this->prepareText($page, $user);
300 return $page->raw();
301 }
302
303 public function process(array $user)
304 {
305 }
306 }
307
308 class ListMarketing extends AnnuaireMarketing
309 {
310 private $name;
311 private $domain;
312 public function __construct($data, $from, $personal_notes = null)
313 {
314 list($this->name, $this->domain) = explode('@', $data);
315 if ($from && ($user = User::getSilent($from))) {
316 $from = $user->fullName();
317 } else {
318 $from = "Je";
319 }
320 $this->titre = "Un camarade solicite ton inscription à $data";
321 $this->intro = "Polytechnique.org, l'annuaire des polytechniciens sur internet, "
322 . "fournit de nombreux services aux groupes X, ainsi que des listes "
323 . "de diffusion pour les X en faisant la demande.\n\n"
324 . "$from solicite ton inscription à la liste <$data>. "
325 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
326 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
327 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
328 . "te suffit de visiter cette page ou de copier cette adresse dans "
329 . "la barre de ton navigateur :";
330 }
331
332 public function process(array $user)
333 {
334 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
335 VALUES ({?}, 'list', {?}, {?})",
336 $user['id'], $this->name, $this->domain);
337 }
338 }
339
340 class GroupMarketing extends AnnuaireMarketing
341 {
342 private $group;
343 public function __construct($data, $from, $personal_notes = null)
344 {
345 $this->group = $data;
346 if ($from && ($user = User::getSilent($from))) {
347 $from = $user->fullName() . " vient";
348 } else {
349 $from = "Je viens";
350 }
351 $this->titre = "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
352 $this->intro = "Polytechnique.org, l'annuaire des polytechniciens sur internet, fournit "
353 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
354 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
355 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
356 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
357 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
358 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
359 . "ou de copier cette adresse dans la barre de ton navigateur :";
360 }
361
362 public function process(array $user)
363 {
364 return XDB::execute("REPLACE INTO register_subs (uid, type, sub, domain)
365 VALUES ({?}, 'group', {?}, '')",
366 $user['id'], $this->group);
367 }
368 }
369
370 /// Make AnnuaireMarketing to be the default message
371 class DefaultMarketing extends AnnuaireMarketing
372 {
373 }
374
375 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
376 ?>