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