Moving to GitHub.
[platal.git] / include / marketing.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
0337d704 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
e654517d 22class 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 );
0337d704 30
e654517d 31 private $engine;
32 public $sender_mail;
33 public $user;
0337d704 34
e654517d 35 private $type;
36 private $data;
37 private $from;
38 private $sender;
232577de 39 private $personal_notes;
e654517d 40
41 private $hash = '';
42
232577de 43 public function __construct($uid, $email, $type, $data, $from, $sender = null, $personal_notes = null)
e654517d 44 {
45 $this->user = $this->getUser($uid, $email);
46 $this->sender_mail = $this->getFrom($from, $sender);
232577de 47 $this->engine =& $this->getEngine($type, $data, $from == 'user' ? $sender : null, $personal_notes);
e654517d 48
49 $this->type = $type;
50 $this->data = $data;
51 $this->from = $from;
eaf30d86 52 $this->sender = $sender;
232577de 53 $this->personal_notes = $personal_notes;
e654517d 54 }
55
56 private function getUser($uid, $email)
57 {
56081a9c
VZ
58 $user = User::getSilent($uid);
59 if (!$user) {
e654517d 60 return null;
eaf30d86 61 }
56081a9c
VZ
62
63 global $globals;
64 return array(
5daf68f6
VZ
65 'user' => $user,
66 'id' => $user->id(),
67 'sexe' => $user->isFemale(),
68 'mail' => $email,
69 'to' => '"' . $user->fullName() . '" <' . $email . '>',
c3d23491
VB
70 'forlife_email' => $user->hruid . "@" . $user->mainEmailDomain(),
71 'forlife_email2' => $user->hruid . "@" . $user->alternateEmailDomain()
56081a9c 72 );
e654517d 73 }
74
75 private function getFrom($from, $sender)
76 {
799cdbcd 77 global $globals;
eaf30d86 78
56081a9c 79 if ($from == 'staff' || !($user = User::getSilent($sender))) {
c3e0d3c1 80 return "\"L'équipe de Polytechnique.org\" <register@" . $globals->mail->domain . '>';
e654517d 81 }
5daf68f6 82 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
e654517d 83 }
84
232577de 85 private function &getEngine($type, $data, $from, $personal_notes)
e654517d 86 {
87 $class = $type . 'Marketing';
88 if (!class_exists($class, false)) {
89 $class= 'DefaultMarketing';
90 }
232577de 91 $engine = new $class($data, $from, $personal_notes);
8c4a0c30 92 if (!$engine instanceof MarketingEngine) {
f62bd784 93 $engine = null;
f62bd784 94 }
95 return $engine;
e654517d 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);
232577de 118 $text = str_replace(array('%%hash%%', '%%sender%%', '%%personal_notes%%'),
c3e0d3c1 119 array($this->hash, "Cordialement,\n-- \n" . $this->sender_mail, ''), $text);
e654517d 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
232577de
SJ
132 (uid, sender, email, date, last, nb, type, hash, message, message_data, personal_notes)
133 VALUES ({?}, {?}, {?}, NOW(), 0, 0, {?}, {?}, {?}, {?}, {?})',
e654517d 134 $this->user['id'], $this->sender, $this->user['mail'], $this->from, $this->hash,
232577de 135 $this->type, $this->data, $this->personal_notes);
e654517d 136 $this->engine->process($this->user);
137 if ($valid) {
c3e0d3c1
SJ
138 $sender = User::getSilent($this->sender);
139 $valid = new MarkReq($sender, $this->user['user'], $this->user['mail'],
232577de 140 $this->from == 'user', $this->type, $this->data, $this->personal_notes);
e654517d 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
8ded5b5e 165 static public function get($uid, $email, $recentOnly = false)
e654517d 166 {
232577de 167 $res = XDB::query("SELECT uid, email, message, message_data, type, sender, personal_notes
e654517d 168 FROM register_marketing
8ded5b5e 169 WHERE uid = {?}
170 AND email = {?}".(
171 $recentOnly ? ' AND DATEDIFF(NOW(), last) < 30' : ''), $uid, $email);
172
e654517d 173 if ($res->numRows() == 0) {
174 return null;
175 }
9707997c 176 list ($uid, $email, $type, $data, $from, $sender, $personal_notes) = $res->fetchOneRow();
232577de 177 return new Marketing($uid, $email, $type, $data, $from, $sender, $personal_notes);
e654517d 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 {
eaf30d86 185 XDB::execute("DELETE FROM register_marketing WHERE uid = {?} AND email = {?}", $uid, $email);
e654517d 186 }
187 }
188
26ba053e 189 static public function relance(PlUser $user, $nbx = -1)
e654517d 190 {
191 global $globals;
192
193 if ($nbx < 0) {
19721970 194 $nbx = $globals->core->NbIns;
e654517d 195 }
eaf30d86 196
0c1e3a66
FB
197 $res = XDB::fetchOneCell('SELECT r.date, r.email, r.bestalias
198 FROM register_pending
199 WHERE r.hash = \'INSCRIT\' AND uid = {?}',
38c6fe96
FB
200 $user->id());
201 if (!$res) {
e654517d 202 return false;
38c6fe96
FB
203 } else {
204 list($date, $email, $alias) = $res;
e654517d 205 }
eaf30d86 206
e654517d 207 $hash = rand_url_id(12);
208 $pass = rand_pass();
c67ba12a 209 $pass_encrypted = sha1($pass);
e654517d 210 $fdate = strftime('%d %B %Y', strtotime($date));
eaf30d86 211
b71f7275 212 $mymail = new PlMailer('marketing/relance.mail.tpl');
e654517d 213 $mymail->assign('nbdix', $nbx);
214 $mymail->assign('fdate', $fdate);
215 $mymail->assign('lusername', $alias);
216 $mymail->assign('nveau_pass', $pass);
217 $mymail->assign('baseurl', $globals->baseurl);
218 $mymail->assign('lins_id', $hash);
219 $mymail->assign('lemail', $email);
f036c896 220 $mymail->assign('subj', ucfirst($globals->mail->domain) . ' : ' . $alias);
e654517d 221 $mymail->send();
222 XDB::execute('UPDATE register_pending
223 SET hash={?}, password={?}, relance=NOW()
38c6fe96
FB
224 WHERE uid={?}', $hash, $pass_encrypted, $user->id());
225 return $user->fullName();
e654517d 226 }
0337d704 227}
0337d704 228
e654517d 229interface MarketingEngine
0337d704 230{
232577de 231 public function __construct($data, $from, $personal_notes = null);
e654517d 232 public function getTitle();
233 public function getText(array $user);
234 public function process(array $user);
0337d704 235}
236
e654517d 237class AnnuaireMarketing implements MarketingEngine
238{
239 protected $titre;
240 protected $intro;
24490a67 241 protected $signature;
232577de 242 protected $personal_notes;
0337d704 243
232577de 244 public function __construct($data, $from, $personal_notes = null)
e654517d 245 {
24490a67 246 $this->titre = "Rejoins la communauté polytechnicienne sur Internet";
247 $this->intro = " Tu n'as pas de fiche dans l'annuaire des polytechniciens sur Internet. "
248 . "Pour y figurer, il te suffit de visiter cette page ou de copier cette adresse "
e654517d 249 . "dans la barre de ton navigateur :";
24490a67 250 if ($from === null) {
c3e0d3c1
SJ
251 $page = new XorgPage();
252 $page->changeTpl('include/signature.mail.tpl', NO_SKIN);
253 $page->assign('mail_part', 'text');
254 $this->signature = $page->raw();
24490a67 255 } else {
c3e0d3c1 256 $this->signature = '%%sender%%';
24490a67 257 }
232577de
SJ
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 }
e654517d 263 }
264
265 public function getTitle()
266 {
267 return $this->titre;
268 }
269
270 private function getIntro()
271 {
272 return $this->intro;
273 }
274
24490a67 275 public function getSignature()
276 {
277 return $this->signature;
278 }
279
232577de
SJ
280 public function getPersonalNotes()
281 {
282 return $this->personal_notes;
283 }
284
26ba053e 285 protected function prepareText(PlPage $page, array $user)
e654517d 286 {
287 $page->assign('intro', $this->getIntro());
288 $page->assign('u', $user);
24490a67 289 $page->assign('sign', $this->getSignature());
232577de 290 $page->assign('personal_notes', $this->getPersonalNotes());
e654517d 291 }
292
293 public function getText(array $user)
294 {
f70f2bcd
FB
295 $page = new XorgPage();
296 $page->changeTpl('marketing/marketing.mail.tpl', NO_SKIN);
e654517d 297 $this->prepareText($page, $user);
298 return $page->raw();
299 }
300
301 public function process(array $user)
302 {
303 }
304}
305
306class ListMarketing extends AnnuaireMarketing
0337d704 307{
e654517d 308 private $name;
309 private $domain;
232577de 310 public function __construct($data, $from, $personal_notes = null)
e654517d 311 {
312 list($this->name, $this->domain) = explode('@', $data);
56081a9c
VZ
313 if ($from && ($user = User::getSilent($from))) {
314 $from = $user->fullName();
e654517d 315 } else {
316 $from = "Je";
317 }
318 $this->titre = "Un camarade solicite ton inscription à $data";
24490a67 319 $this->intro = "Polytechnique.org, l'annuaire des polytechniciens sur internet, "
320 . "fournit de nombreux services aux groupes X, ainsi que des listes "
321 . "de diffusion pour les X en faisant la demande.\n\n"
322 . "$from solicite ton inscription à la liste <$data>. "
323 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent "
324 . "profiter de l'ensemble de nos services, c'est pourquoi nous te "
325 . "proposons auparavant de t'inscrire sur notre site. Pour cela, il "
326 . "te suffit de visiter cette page ou de copier cette adresse dans "
327 . "la barre de ton navigateur :";
e654517d 328 }
0337d704 329
e654517d 330 public function process(array $user)
331 {
00ba8a74
SJ
332 return XDB::execute("INSERT IGNORE INTO register_subs (uid, type, sub, domain)
333 VALUES ({?}, 'list', {?}, {?})",
334 $user['id'], $this->name, $this->domain);
0337d704 335 }
e654517d 336}
0337d704 337
e654517d 338class GroupMarketing extends AnnuaireMarketing
339{
340 private $group;
232577de 341 public function __construct($data, $from, $personal_notes = null)
e654517d 342 {
343 $this->group = $data;
56081a9c
VZ
344 if ($from && ($user = User::getSilent($from))) {
345 $from = $user->fullName() . " vient";
e654517d 346 } else {
347 $from = "Je viens";
348 }
349 $this->titre = "Profite de ton inscription au groupe \"$data\" pour découvrir Polytechnique.org";
24490a67 350 $this->intro = "Polytechnique.org, l'annuaire des polytechniciens sur internet, fournit "
351 . "de nombreux services aux groupes X ( listes de diffusion, paiement en "
352 . "ligne, sites internet...), en particulier pour le groupe \"$data\"\n\n"
353 . "$from de t'inscrire dans l'annuaire du groupe \"$data\". "
354 . "Cependant, seuls les X inscrits sur Polytechnique.org peuvent profiter "
355 . "de l'ensemble de nos services, c'est pourquoi nous te proposons de "
356 . "t'inscrire sur notre site . Pour cela, il te suffit de visiter cette page "
357 . "ou de copier cette adresse dans la barre de ton navigateur :";
0337d704 358 }
359
e654517d 360 public function process(array $user)
361 {
00ba8a74
SJ
362 return XDB::execute("INSERT IGNORE INTO register_subs (uid, type, sub, domain)
363 VALUES ({?}, 'group', {?}, '')",
364 $user['id'], $this->group);
e654517d 365 }
0337d704 366}
367
e654517d 368/// Make AnnuaireMarketing to be the default message
369class DefaultMarketing extends AnnuaireMarketing
370{
371}
0337d704 372
448c8cdc 373// vim:set et sw=4 sts=4 sws=4 fenc=utf-8:
0337d704 374?>