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