New PlMailer based on Hermes code:
[platal.git] / modules / events.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 EventsModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
27 'events' => $this->make_hook('ev', AUTH_COOKIE),
28 'send_bug' => $this->make_hook('bug', AUTH_COOKIE),
29 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
31
32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, '', NO_AUTH),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
34
35 'nl' => $this->make_hook('nl', AUTH_COOKIE),
36 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE),
37 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE),
38 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'),
39 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'),
40 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'),
41 );
42 }
43
44 function on_subscribe($forlife, $uid, $promo, $password)
45 {
46 require_once 'newsletter.inc.php';
47 subscribe_nl($uid);
48 }
49
50 function get_tips($exclude = null)
51 {
52 $exclude = is_null($exclude) ? '' : ' AND id != ' . $exclude . ' ';
53 $priority = rand(0, 510);
54 do {
55 $priority = (int)($priority/2);
56 $res = XDB::query("SELECT *
57 FROM tips
58 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
59 AND (promo_min = 0 OR promo_min <= {?})
60 AND (promo_max = 0 OR promo_max >= {?})
61 AND (priorite >= {?})
62 AND (state = 'active')
63 $exclude
64 ORDER BY RAND()
65 LIMIT 1",
66 S::i('promo'), S::i('promo'), $priority);
67 } while ($priority && !$res->numRows());
68 if (!$res->numRows()) {
69 return null;
70 }
71 return $res->fetchOneAssoc();
72 }
73
74 function handler_bug(&$page)
75 {
76 $page->changeTpl('bug.tpl',SIMPLE);
77 $page->addJsLink('close_on_esc.js');
78 if (Env::has('send')) {
79 $page->assign('bug_sent',1);
80 $mymail = new PlMailer();
81 $mymail->setFrom('"'.S::v('prenom').' '.S::v('nom').'" <'.S::v('bestalias').'@polytechnique.org>');
82 $mymail->addTo('support+platal@polytechnique.org');
83 $mymail->setSubject('Plat/al '.Env::v('task_type').' : '.Env::v('item_summary'));
84 $mymail->setTxtBody(Env::v('detailed_desc'));
85 $mymail->send();
86 }
87 }
88
89 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
90 {
91 $page->changeTpl('login.tpl');
92 $page->addJsLink('ajax.js');
93 $page->assign('tips', $this->get_tips());
94
95 // donne la derniere date de session
96 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S::i('lastlogin')));
97
98 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
99 WHERE user_id={?}', S::v('uid'));
100 list($date, $naissance) = $res->fetchOneRow();
101
102 // incitation à mettre à jour la fiche
103
104 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
105 substr($date, 0, 4));
106 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
107 // si fiche date de + de 400j;
108 $page->assign('fiche_incitation', $date);
109 }
110
111 // Souhaite bon anniversaire
112
113 if (substr($naissance, 5) == date('m-d')) {
114 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
115 }
116
117 // incitation à mettre une photo
118
119 $res = XDB::query('SELECT COUNT(*) FROM photo
120 WHERE uid={?}', S::v('uid'));
121 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
122
123 // Incitation à se géolocaliser
124 require_once 'geoloc.inc.php';
125 $res = localize_addresses(S::v('uid', -1));
126 $page->assign('geoloc_incitation', count($res));
127
128 // ajout du lien RSS
129 if (S::has('core_rss_hash')) {
130 $page->setRssLink('Polytechnique.org :: News',
131 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
132 }
133
134 // cache les evenements lus et raffiche les evenements a relire
135 if ($action == 'read' && $eid) {
136 XDB::execute('DELETE evenements_vus.*
137 FROM evenements_vus AS ev
138 INNER JOIN evenements AS e ON e.id = ev.evt_id
139 WHERE peremption < NOW()');
140 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
141 $eid, S::v('uid'));
142 pl_redirect('events#'.$pound);
143 }
144
145 if ($action == 'unread' && $eid) {
146 XDB::execute('DELETE FROM evenements_vus
147 WHERE evt_id = {?} AND user_id = {?}',
148 $eid, S::v('uid'));
149 pl_redirect('events#newsid'.$eid);
150 }
151
152 // affichage des evenements
153 // annonces promos triées par présence d'une limite sur les promos
154 // puis par dates croissantes d'expiration
155 $promo = S::v('promo');
156 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
157 FROM evenements AS e
158 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
159 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
160 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
161 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
162 AND (e.promo_min = 0 || e.promo_min <= {?})
163 AND (e.promo_max = 0 || e.promo_max >= {?})
164 AND ev.user_id IS NULL
165 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
166 $page->assign('evenement',
167 XDB::iterator($sql, S::v('uid'),
168 $promo, $promo)
169 );
170
171 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
172 FROM evenements AS e
173 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
174 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
175 AND (e.promo_min = 0 || e.promo_min <= {?})
176 AND (e.promo_max = 0 || e.promo_max >= {?})
177 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
178 $page->assign('evenement_summary',
179 XDB::iterator($sql, S::v('uid'),
180 $promo, $promo)
181 );
182 }
183
184 function handler_ev_submit(&$page)
185 {
186 $page->changeTpl('evenements.tpl');
187
188 $titre = Post::v('titre');
189 $texte = Post::v('texte');
190 $promo_min = Post::i('promo_min');
191 $promo_max = Post::i('promo_max');
192 $peremption = Post::i('peremption');
193 $valid_mesg = Post::v('valid_mesg');
194 $action = Post::v('action');
195
196 if ($promo_min > $promo_max ||
197 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
198 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
199 {
200 $page->trig("L'intervalle de promotions n'est pas valide");
201 $action = null;
202 }
203
204 require_once('url_catcher.inc.php');
205 $texte_catch_url = url_catcher($texte);
206
207 $page->assign('titre', $titre);
208 $page->assign('texte', $texte);
209 $page->assign('texte_html', $texte_catch_url);
210 $page->assign('promo_min', $promo_min);
211 $page->assign('promo_max', $promo_max);
212 $page->assign('peremption', $peremption);
213 $page->assign('valid_mesg', $valid_mesg);
214 $page->assign('action', strtolower($action));
215
216 if ($action == 'Confirmer') {
217 $texte = $texte_catch_url;
218 require_once 'validations.inc.php';
219 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
220 $peremption, $valid_mesg, S::v('uid'));
221 $evtreq->submit();
222 $page->assign('ok', true);
223 }
224
225 $select = '';
226 for ($i = 1 ; $i < 30 ; $i++) {
227 $time = time() + 3600 * 24 * $i;
228 $p_stamp = date('Ymd', $time);
229 $year = date('Y', $time);
230 $month = date('m', $time);
231 $day = date('d', $time);
232
233 $select .= "<option value=\"$p_stamp\"";
234 if ($p_stamp == strtr($peremption, array("-" => ""))) {
235 $select .= " selected='selected'";
236 }
237 $select .= "> $day / $month / $year</option>\n";
238 }
239 $page->assign('select',$select);
240 }
241
242 function handler_tips(&$page, $tips = null)
243 {
244 $page->changeTpl('include/tips.tpl', NO_SKIN);
245 $page->assign('tips', $this->get_tips($tips));
246 }
247
248 function handler_admin_tips(&$page, $action = 'list', $id = null)
249 {
250 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
251 $page->assign('title', 'Gestion des Astuces');
252 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
253 $table_editor->describe('peremption', 'date de péremption', true);
254 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
255 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
256 $table_editor->describe('titre', 'titre', true);
257 $table_editor->describe('state', 'actif', true);
258 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
259 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false);
260 $table_editor->apply($page, $action, $id);
261 }
262
263 function handler_nl(&$page, $action = null)
264 {
265 require_once 'newsletter.inc.php';
266
267 $page->changeTpl('newsletter/index.tpl');
268 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
269
270 switch ($action) {
271 case 'out': unsubscribe_nl(); break;
272 case 'in': subscribe_nl(); break;
273 default: ;
274 }
275
276 $page->assign('nls', get_nl_state());
277 $page->assign('nl_list', get_nl_list());
278 }
279
280 function handler_nl_show(&$page, $nid = 'last')
281 {
282 $page->changeTpl('newsletter/show.tpl');
283
284 require_once 'newsletter.inc.php';
285
286 $nl = new NewsLetter($nid);
287 $page->assign_by_ref('nl', $nl);
288
289 if (Post::has('send')) {
290 $nl->sendTo(S::v('prenom'), S::v('nom'),
291 S::v('bestalias'), S::v('femme'),
292 S::v('mail_fmt') != 'texte');
293 }
294 }
295
296 function handler_nl_submit(&$page)
297 {
298 $page->changeTpl('newsletter/submit.tpl');
299
300 require_once 'newsletter.inc.php';
301
302 if (Post::has('see')) {
303 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
304 $page->assign('art', $art);
305 } elseif (Post::has('valid')) {
306 require_once('validations.inc.php');
307 $art = new NLReq(S::v('uid'), Post::v('title'),
308 Post::v('body'), Post::v('append'));
309 $art->submit();
310 $page->assign('submited', true);
311 }
312 }
313
314 function handler_admin_events(&$page, $action = 'list', $eid = null)
315 {
316 $page->changeTpl('admin/evenements.tpl');
317 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
318 $page->register_modifier('hde', 'html_entity_decode');
319
320 $arch = $action == 'archives';
321 $page->assign('action', $action);
322
323 if (Post::v('action') == "Proposer" && $eid) {
324 $promo_min = Post::i('promo_min');
325 $promo_max = Post::i('promo_max');
326 if ($promo_min > $promo_max ||
327 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
328 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
329 {
330 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
331 $action = 'edit';
332 } else {
333 XDB::execute('UPDATE evenements
334 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
335 WHERE id = {?}',
336 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
337 Post::v('promo_min'), Post::v('promo_max'), $eid);
338 }
339 }
340
341 if ($action == 'edit') {
342 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max
343 FROM evenements
344 WHERE id={?}', $eid);
345 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
346 $page->assign('titre',$titre);
347 $page->assign('texte',$texte);
348 $page->assign('promo_min',$promo_min);
349 $page->assign('promo_max',$promo_max);
350 $page->assign('peremption',$peremption);
351
352 $select = "";
353 for ($i = 1 ; $i < 30 ; $i++) {
354 $p_stamp=date("Ymd",time()+3600*24*$i);
355 $year=substr($p_stamp,0,4);
356 $month=substr($p_stamp,4,2);
357 $day=substr($p_stamp,6,2);
358
359 $select .= "<option value=\"$p_stamp\""
360 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
361 . "> $day / $month / $year</option>\n";
362 }
363 $page->assign('select',$select);
364 } else {
365 switch ($action) {
366 case 'delete':
367 XDB::execute('DELETE from evenements
368 WHERE id = {?}', $eid);
369 break;
370
371 case "archive":
372 XDB::execute('UPDATE evenements
373 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
374 WHERE id = {?}', $eid);
375 break;
376
377 case "unarchive":
378 XDB::execute('UPDATE evenements
379 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
380 WHERE id = {?}', $eid);
381 $action = 'archives';
382 $arch = true;
383 break;
384
385 case "valid":
386 XDB::execute('UPDATE evenements
387 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
388 WHERE id = {?}', $eid);
389 break;
390
391 case "unvalid":
392 XDB::execute('UPDATE evenements
393 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
394 WHERE id = {?}', $eid);
395 break;
396 }
397
398 $pid = ($eid && $action == 'preview') ? $eid : -1;
399 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
400 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
401 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
402 e.promo_min, e.promo_max,
403 FIND_IN_SET('valide', e.flags) AS fvalide,
404 FIND_IN_SET('archive', e.flags) AS farch,
405 u.promo, u.nom, u.prenom, a.alias AS forlife
406 FROM evenements AS e
407 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
408 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
409 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
410 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
411 $page->assign('evs', XDB::iterator($sql));
412 }
413 $page->assign('arch', $arch);
414 }
415
416 function handler_admin_nl(&$page, $new = false) {
417 $page->changeTpl('newsletter/admin.tpl');
418 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
419 require_once("newsletter.inc.php");
420
421 if($new) {
422 insert_new_nl();
423 pl_redirect("admin/newsletter");
424 }
425
426 $page->assign('nl_list', get_nl_slist());
427 }
428
429 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
430 $page->changeTpl('newsletter/edit.tpl');
431 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
432 require_once("newsletter.inc.php");
433
434 $nl = new NewsLetter($nid);
435
436 if($action == 'delete') {
437 $nl->delArticle($aid);
438 pl_redirect("admin/newsletter/edit/$nid");
439 }
440
441 if($aid == 'update') {
442 $nl->_title = Post::v('title');
443 $nl->_title_mail= Post::v('title_mail');
444 $nl->_date = Post::v('date');
445 $nl->_head = Post::v('head');
446 $nl->_shortname = strlen(Post::v('shortname')) ? Post::v('shortname') : null;
447 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname) && !is_numeric($nl->_shortname)) {
448 $nl->save();
449 } else {
450 $page->trig('Le nom de la NL n\'est pas valide');
451 pl_redirect('admin/newsletter/edit/' . $nl->_id);
452 }
453 }
454
455 if(Post::v('save')) {
456 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
457 $aid, Post::v('cid'), Post::v('pos'));
458 $nl->saveArticle($art);
459 pl_redirect("admin/newsletter/edit/$nid");
460 }
461
462 if($action == 'edit' && $aid != 'update') {
463 $eaid = $aid;
464 if(Post::has('title')) {
465 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
466 $eaid, Post::v('cid'), Post::v('pos'));
467 } else {
468 $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
469 }
470 $page->assign('art', $art);
471 }
472
473 $page->assign_by_ref('nl',$nl);
474 }
475 function handler_admin_nl_cat(&$page, $action = 'list', $id = null) {
476 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
477 $page->assign('title', 'Gestion des catégories de la newsletter');
478 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
479 $table_editor->describe('titre','intitulé',true);
480 $table_editor->describe('pos','position',true);
481 $table_editor->apply($page, $action, $id);
482 }
483
484 }
485
486 ?>