Arrange templates by module
[platal.git] / modules / events.php
CommitLineData
74e0093f 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
22class EventsModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
1a828cd4 27 'events' => $this->make_hook('ev', AUTH_COOKIE),
d1c97e42 28 'rss' => $this->make_hook('rss', AUTH_PUBLIC),
1a828cd4 29 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
92423144 30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
b829e258 31
dc767839 32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, '', NO_AUTH),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
34
164ca57a 35 'nl' => $this->make_hook('nl', AUTH_COOKIE),
36 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE),
383eaddd 37 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP),
92423144 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'),
74e0093f 41 );
42 }
43
8d8f7607 44 function on_subscribe($forlife, $uid, $promo, $password)
45 {
46 require_once 'newsletter.inc.php';
47 subscribe_nl($uid);
48 }
49
dc767839 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 >= {?})
5d617d8d 62 AND (state = 'active')
dc767839 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
096f0dca 74 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
1a828cd4 75 {
8b1f8e12 76 $page->changeTpl('events/index.tpl');
dc767839 77 $page->addJsLink('ajax.js');
78 $page->assign('tips', $this->get_tips());
79
08cce2ff 80 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
cab08090 81 WHERE user_id={?}', S::v('uid'));
1a828cd4 82 list($date, $naissance) = $res->fetchOneRow();
83
089a5801 84 // incitation à mettre à jour la fiche
1a828cd4 85
86 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
87 substr($date, 0, 4));
88 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
89 // si fiche date de + de 400j;
90 $page->assign('fiche_incitation', $date);
91 }
92
93 // Souhaite bon anniversaire
94
95 if (substr($naissance, 5) == date('m-d')) {
96 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
97 }
98
089a5801 99 // incitation à mettre une photo
1a828cd4 100
08cce2ff 101 $res = XDB::query('SELECT COUNT(*) FROM photo
cab08090 102 WHERE uid={?}', S::v('uid'));
1a828cd4 103 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
104
089a5801 105 // Incitation à se géolocaliser
1a828cd4 106 require_once 'geoloc.inc.php';
cab08090 107 $res = localize_addresses(S::v('uid', -1));
1a828cd4 108 $page->assign('geoloc_incitation', count($res));
109
1a828cd4 110 // ajout du lien RSS
cab08090 111 if (S::has('core_rss_hash')) {
162370e7 112 $page->setRssLink('Polytechnique.org :: News',
113 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
1a828cd4 114 }
115
116 // cache les evenements lus et raffiche les evenements a relire
4e61caea 117 if ($action == 'read' && $eid) {
eadff9f9 118 XDB::execute('DELETE evenements_vus.*
119 FROM evenements_vus AS ev
120 INNER JOIN evenements AS e ON e.id = ev.evt_id
121 WHERE peremption < NOW()');
08cce2ff 122 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
096f0dca 123 $eid, S::v('uid'));
124 pl_redirect('events#'.$pound);
1a828cd4 125 }
126
4e61caea 127 if ($action == 'unread' && $eid) {
08cce2ff 128 XDB::execute('DELETE FROM evenements_vus
eadff9f9 129 WHERE evt_id = {?} AND user_id = {?}',
4e61caea 130 $eid, S::v('uid'));
096f0dca 131 pl_redirect('events#newsid'.$eid);
1a828cd4 132 }
133
134 // affichage des evenements
089a5801 135 // annonces promos triées par présence d'une limite sur les promos
1a828cd4 136 // puis par dates croissantes d'expiration
cab08090 137 $promo = S::v('promo');
1a828cd4 138 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
2f678da1 139 FROM evenements AS e
140 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
141 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
142 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
1a828cd4 143 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
144 AND (e.promo_min = 0 || e.promo_min <= {?})
145 AND (e.promo_max = 0 || e.promo_max >= {?})
146 AND ev.user_id IS NULL
147 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
148 $page->assign('evenement',
cab08090 149 XDB::iterator($sql, S::v('uid'),
1a828cd4 150 $promo, $promo)
151 );
152
153 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
154 FROM evenements AS e
155 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
156 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
157 AND (e.promo_min = 0 || e.promo_min <= {?})
158 AND (e.promo_max = 0 || e.promo_max >= {?})
159 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
160 $page->assign('evenement_summary',
cab08090 161 XDB::iterator($sql, S::v('uid'),
1a828cd4 162 $promo, $promo)
163 );
1a828cd4 164 }
165
d1c97e42 166 function handler_rss(&$page, $user = null, $hash = null)
167 {
168 require_once 'rss.inc.php';
169
8b1f8e12 170 $uid = init_rss('events/rss.tpl', $user, $hash);
d1c97e42 171
172 $rss = XDB::iterator(
173 'SELECT e.id, e.titre, e.texte, e.creation_date,
174 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
175 FROM auth_user_md5 AS u
176 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
177 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
178 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
179 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
180 AND peremption >= NOW()', $uid);
181 $page->assign('rss', $rss);
182 }
183
1a828cd4 184 function handler_ev_submit(&$page)
74e0093f 185 {
8b1f8e12 186 $page->changeTpl('events/submit.tpl');
74e0093f 187
5e2307dc 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');
74e0093f 195
d1c97e42 196 if (($promo_min > $promo_max && $promo_max != 0)||
2086ab7f 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
edd50750 204 require_once('url_catcher.inc.php');
205 $texte_catch_url = url_catcher($texte);
206
74e0093f 207 $page->assign('titre', $titre);
208 $page->assign('texte', $texte);
edd50750 209 $page->assign('texte_html', $texte_catch_url);
74e0093f 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') {
edd50750 217 $texte = $texte_catch_url;
74e0093f 218 require_once 'validations.inc.php';
219 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
cab08090 220 $peremption, $valid_mesg, S::v('uid'));
74e0093f 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);
74e0093f 240 }
b829e258 241
dc767839 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);
5d617d8d 257 $table_editor->describe('state', 'actif', true);
dc767839 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
164ca57a 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());
1a013db7 277 $page->assign('nl_list', get_nl_list());
164ca57a 278 }
279
280 function handler_nl_show(&$page, $nid = 'last')
b829e258 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')) {
cab08090 290 $nl->sendTo(S::v('prenom'), S::v('nom'),
291 S::v('bestalias'), S::v('femme'),
91f42324 292 S::v('mail_fmt') != 'texte');
b829e258 293 }
b829e258 294 }
164ca57a 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')) {
5e2307dc 303 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
164ca57a 304 $page->assign('art', $art);
305 } elseif (Post::has('valid')) {
306 require_once('validations.inc.php');
5e2307dc 307 $art = new NLReq(S::v('uid'), Post::v('title'),
308 Post::v('body'), Post::v('append'));
164ca57a 309 $art->submit();
310 $page->assign('submited', true);
311 }
164ca57a 312 }
92423144 313
506a46ed 314 function handler_admin_events(&$page, $action = 'list', $eid = null)
315 {
8b1f8e12 316 $page->changeTpl('events/admin_events.tpl');
92423144 317 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
506a46ed 318 $page->register_modifier('hde', 'html_entity_decode');
319
320 $arch = $action == 'archives';
321 $page->assign('action', $action);
322
01248e3a 323 if (Post::v('action') == "Proposer" && $eid) {
2086ab7f 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 }
92423144 339 }
506a46ed 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,
92423144 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)
506a46ed 410 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
92423144 411 $page->assign('evs', XDB::iterator($sql));
412 }
506a46ed 413 $page->assign('arch', $arch);
92423144 414 }
415
416 function handler_admin_nl(&$page, $new = false) {
163eddd2 417 $page->changeTpl('newsletter/admin.tpl');
92423144 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
1a013db7 426 $page->assign('nl_list', get_nl_slist());
92423144 427 }
428
429 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
163eddd2 430 $page->changeTpl('newsletter/edit.tpl');
92423144 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') {
ed76a506 442 $nl->_title = Post::v('title');
8269d2d6 443 $nl->_title_mail= Post::v('title_mail');
ed76a506 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 }
92423144 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
ed76a506 462 if($action == 'edit' && $aid != 'update') {
92423144 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) {
92423144 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
74e0093f 484}
485
486?>