add jquery script, use it to autoload on xnet when xorg cookie is present
[platal.git] / modules / events.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 'rss' => $this->make_hook('rss', AUTH_PUBLIC),
29 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC, 'user', NO_AUTH),
30 'events/photo' => $this->make_hook('photo', AUTH_COOKIE),
31 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
32 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
33
34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, 'user', NO_AUTH),
35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
36 );
37 }
38
39 private function get_tips($exclude = null)
40 {
41 global $globals;
42 // Add a new special tip when changing plat/al version
43 if ($globals->version != S::v('last_version') && is_null($exclude)) {
44 XDB::execute('UPDATE auth_user_quick
45 SET last_version = {?}
46 WHERE user_id = {?}',
47 $globals->version, S::i('uid'));
48 return array('id' => 0,
49 'titre' => 'Bienvenue sur la nouvelle version du site !',
50 'text' => 'Le site a été mis à jour depuis ta dernière visite vers la version ' . $globals->version
51 . '. Nous t\'invitons à en découvrir les nouveautés en te rendant sur '
52 . '<a href="banana/xorg.m4x.innovation">nos forums</a> ou en consultant '
53 . '<a href="changelog">la liste exhaustive des modifications</a>',
54 'priorite' => 255,
55 'promo_min' => 0,
56 'promo_max' => 0,
57 'state' => 'active',
58 'special' => true);
59 }
60
61 $exclude = is_null($exclude) ? '' : ' AND id != ' . $exclude . ' ';
62 $priority = rand(0, 510);
63 do {
64 $priority = (int)($priority/2);
65 $res = XDB::query("SELECT *
66 FROM tips
67 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
68 AND (promo_min = 0 OR promo_min <= {?})
69 AND (promo_max = 0 OR promo_max >= {?})
70 AND (priorite >= {?})
71 AND (state = 'active')
72 $exclude
73 ORDER BY RAND()
74 LIMIT 1",
75 S::i('promo'), S::i('promo'), $priority);
76 } while ($priority && !$res->numRows());
77 if (!$res->numRows()) {
78 return null;
79 }
80 return $res->fetchOneAssoc();
81 }
82
83 private function get_events($where, $order, array &$array, $name)
84 {
85 // affichage des evenements
86 // annonces promos triées par présence d'une limite sur les promos
87 // puis par dates croissantes d'expiration
88 $promo = S::v('promo');
89 $uid = S::i('uid');
90 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
91 FROM evenements AS e
92 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
93 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
94 AND (e.promo_min = 0 || e.promo_min <= {?})
95 AND (e.promo_max = 0 || e.promo_max >= {?})
96 AND $where
97 ORDER BY $order";
98 $sum = XDB::iterator($sql, $uid, $promo, $promo);
99 if (!$sum->total()) {
100 return false;
101 }
102 $sql = "SELECT e.id,e.titre,e.texte,e.post_id,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife,
103 p.x, p.y, p.attach IS NOT NULL AS img
104 FROM evenements AS e
105 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
106 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
107 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
108 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
109 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
110 AND (e.promo_min = 0 || e.promo_min <= {?})
111 AND (e.promo_max = 0 || e.promo_max >= {?})
112 AND ev.user_id IS NULL
113 AND $where
114 ORDER BY $order";
115 $evt = XDB::iterator($sql, $uid, $promo, $promo);
116 $array[$name] = array('events' => $evt, 'summary' => $sum);
117 return true;
118 }
119
120 private function upload_image(PlatalPage &$page, PlUpload &$upload)
121 {
122 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
123 return true;
124 }
125 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
126 $page->trig('Impossible de télécharger l\'image');
127 return false;
128 } elseif (!$upload->isType('image')) {
129 $page->trig('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG');
130 $upload->rm();
131 return false;
132 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
133 $page->trig('Impossible de retraiter l\'image');
134 return false;
135 }
136 return true;
137 }
138
139 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
140 {
141 $page->changeTpl('events/index.tpl');
142 $page->addJsLink('ajax.js');
143 $page->assign('tips', $this->get_tips());
144
145 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
146 WHERE user_id={?}', S::v('uid'));
147 list($date, $naissance) = $res->fetchOneRow();
148
149 // incitation à mettre à jour la fiche
150
151 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
152 substr($date, 0, 4));
153 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
154 // si fiche date de + de 400j;
155 $page->assign('fiche_incitation', $date);
156 }
157
158 // Souhaite bon anniversaire
159
160 if (substr($naissance, 5) == date('m-d')) {
161 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
162 }
163
164 // incitation à mettre une photo
165
166 $res = XDB::query('SELECT COUNT(*) FROM photo
167 WHERE uid={?}', S::v('uid'));
168 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
169
170 // Incitation à se géolocaliser
171 require_once 'geoloc.inc.php';
172 $res = localize_addresses(S::v('uid', -1));
173 $page->assign('geoloc_incitation', count($res));
174
175 // ajout du lien RSS
176 if (S::has('core_rss_hash')) {
177 $page->setRssLink('Polytechnique.org :: News',
178 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
179 }
180
181 // cache les evenements lus et raffiche les evenements a relire
182 if ($action == 'read' && $eid) {
183 XDB::execute('DELETE evenements_vus.*
184 FROM evenements_vus AS ev
185 INNER JOIN evenements AS e ON e.id = ev.evt_id
186 WHERE peremption < NOW()');
187 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
188 $eid, S::v('uid'));
189 pl_redirect('events#'.$pound);
190 }
191
192 if ($action == 'unread' && $eid) {
193 XDB::execute('DELETE FROM evenements_vus
194 WHERE evt_id = {?} AND user_id = {?}',
195 $eid, S::v('uid'));
196 pl_redirect('events#newsid'.$eid);
197 }
198
199 $array = array();
200 $this->get_events('FIND_IN_SET(\'important\', e.flags)', 'e.creation_date DESC', $array, 'important');
201 $this->get_events('e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY)
202 AND NOT FIND_IN_SET(\'important\', e.flags)',
203 'e.creation_date DESC', $array, 'news');
204 $this->get_events('e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY)
205 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
206 AND NOT FIND_IN_SET(\'important\', e.flags)',
207 'e.peremption, e.creation_date DESC', $array, 'end');
208 $this->get_events('e.peremption >= DATE_ADD(CURDATE(), INTERVAL 2 DAY)
209 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
210 AND NOT FIND_IN_SET(\'important\', e.flags)',
211 'e.peremption, e.creation_date DESC', $array, 'body');
212 $page->assign_by_ref('events', $array);
213 }
214
215 function handler_photo(&$page, $eid = null, $valid = null)
216 {
217 if ($eid && $eid != 'valid') {
218 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
219 if ($res->numRows()) {
220 $photo = $res->fetchOneAssoc();
221 header('Content-Type: image/' . $photo['attachmime']);
222 echo $photo['attach'];
223 exit;
224 }
225 } elseif ($eid == 'valid') {
226 require_once 'validations.inc.php';
227 $valid = Validate::get_request_by_id($valid);
228 if ($valid && $valid->img) {
229 header('Content-Type: image/' . $valid->imgtype);
230 echo $valid->img;
231 exit;
232 }
233 } else {
234 $upload = new PlUpload(S::v('forlife'), 'event');
235 if ($upload->exists() && $upload->isType('image')) {
236 header('Content-Type: ' . $upload->contentType());
237 echo $upload->getContents();
238 exit;
239 }
240 }
241 global $globals;
242 header('Content-Type: image/png');
243 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
244 exit;
245 }
246
247 function handler_rss(&$page, $user = null, $hash = null)
248 {
249 require_once 'rss.inc.php';
250
251 $uid = init_rss('events/rss.tpl', $user, $hash);
252
253 $rss = XDB::iterator(
254 'SELECT e.id, e.titre, e.texte, e.creation_date,
255 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
256 FROM auth_user_md5 AS u
257 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
258 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
259 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
260 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
261 AND peremption >= NOW()', $uid);
262 $page->assign('rss', $rss);
263 }
264
265 function handler_preview(&$page)
266 {
267 $page->changeTpl('events/preview.tpl', NO_SKIN);
268 $texte = Get::v('texte');
269 if (!is_utf8($texte)) {
270 $texte = utf8_encode($texte);
271 }
272 if (strpos($_SERVER['HTTP_REFERER'], 'admin') === false) {
273 $texte = MiniWiki::WikiToHTML($texte);
274 }
275 $titre = Get::v('titre');
276 if (!is_utf8($titre)) {
277 $titre = utf8_encode($titre);
278 }
279 $page->assign('texte_html', $texte);
280 $page->assign('titre', $titre);
281 header('Content-Type: text/html; charset=utf-8');
282 }
283
284 function handler_ev_submit(&$page)
285 {
286 $page->changeTpl('events/submit.tpl');
287 $page->addJsLink('ajax.js');
288
289 require_once('wiki.inc.php');
290 wiki_require_page('Xorg.Annonce');
291
292 $titre = Post::v('titre');
293 $texte = Post::v('texte');
294 $promo_min = Post::i('promo_min');
295 $promo_max = Post::i('promo_max');
296 $peremption = Post::i('peremption');
297 $valid_mesg = Post::v('valid_mesg');
298 $action = Post::v('action');
299 $upload = new PlUpload(S::v('forlife'), 'event');
300 $this->upload_image($page, $upload);
301
302 if (($promo_min > $promo_max && $promo_max != 0)||
303 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
304 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
305 {
306 $page->trig("L'intervalle de promotions n'est pas valide");
307 $action = null;
308 }
309
310 $texte_catch_url = MiniWiki::WikiToHTML($texte);
311
312 $page->assign('titre', $titre);
313 $page->assign('texte', $texte);
314 $page->assign('texte_html', $texte_catch_url);
315 $page->assign('promo_min', $promo_min);
316 $page->assign('promo_max', $promo_max);
317 $page->assign('peremption', $peremption);
318 $page->assign('valid_mesg', $valid_mesg);
319 $page->assign('action', strtolower($action));
320 $page->assign_by_ref('upload', $upload);
321
322 if ($action == 'Supprimer l\'image') {
323 $upload->rm();
324 $page->assign('action', false);
325 } elseif ($action && (!trim($texte) || !trim($titre))) {
326 $page->trig("L'article doit avoir un titre et un contenu");
327 } elseif ($action) {
328 $texte = $texte_catch_url;
329 require_once 'validations.inc.php';
330 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
331 $peremption, $valid_mesg, S::v('uid'), $upload);
332 $evtreq->submit();
333 $page->assign('ok', true);
334 } elseif (!Env::v('preview')) {
335 $upload->rm();
336 }
337
338 $select = '';
339 for ($i = 1 ; $i < 30 ; $i++) {
340 $time = time() + 3600 * 24 * $i;
341 $p_stamp = date('Ymd', $time);
342 $year = date('Y', $time);
343 $month = date('m', $time);
344 $day = date('d', $time);
345
346 $select .= "<option value=\"$p_stamp\"";
347 if ($p_stamp == strtr($peremption, array("-" => ""))) {
348 $select .= " selected='selected'";
349 }
350 $select .= "> $day / $month / $year</option>\n";
351 }
352 $page->assign('select',$select);
353 }
354
355 function handler_tips(&$page, $tips = null)
356 {
357 header('Content-Type: text/html; charset="UTF-8"');
358 $page->changeTpl('include/tips.tpl', NO_SKIN);
359 $page->assign('tips', $this->get_tips($tips));
360 }
361
362 function handler_admin_tips(&$page, $action = 'list', $id = null)
363 {
364 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
365 $page->assign('title', 'Gestion des Astuces');
366 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
367 $table_editor->describe('peremption', 'date de péremption', true);
368 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
369 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
370 $table_editor->describe('titre', 'titre', true);
371 $table_editor->describe('state', 'actif', true);
372 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
373 $table_editor->describe('priorite', '0<=priorité<=255', true);
374 $table_editor->list_on_edit(false);
375 $table_editor->apply($page, $action, $id);
376 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
377 $page->changeTpl('events/admin_tips.tpl');
378 }
379 }
380
381 function handler_admin_events(&$page, $action = 'list', $eid = null)
382 {
383 $page->changeTpl('events/admin.tpl');
384 $page->addJsLink('ajax.js');
385 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
386 $page->register_modifier('hde', 'html_entity_decode');
387
388 $arch = $action == 'archives';
389 $page->assign('action', $action);
390
391 $upload = new PlUpload(S::v('forlife'), 'event');
392 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
393 $action = 'edit';
394 $this->upload_image($page, $upload);
395 }
396
397 if (Post::v('action') == 'Pas d\'image' && $eid) {
398 $upload->rm();
399 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
400 $action = 'edit';
401 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
402 $upload->rm();
403 $action = 'edit';
404 } elseif (Post::v('action') == "Proposer" && $eid) {
405 $promo_min = Post::i('promo_min');
406 $promo_max = Post::i('promo_max');
407 if ($promo_min > $promo_max ||
408 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
409 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
410 {
411 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
412 $action = 'edit';
413 } else {
414 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
415 $flags = new FlagSet($res->fetchOneCell());
416 if (Post::v('important')) {
417 $flags->addFlag('important');
418 } else {
419 $flags->rmFlag('important');
420 }
421 XDB::execute('UPDATE evenements
422 SET creation_date = creation_date,
423 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
424 flags = {?}
425 WHERE id = {?}',
426 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
427 Post::v('promo_min'), Post::v('promo_max'),
428 $flags->flags(), $eid);
429 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
430 XDB::execute('REPLACE INTO evenements_photo
431 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
432 $eid, $type, $x, $y, $upload->getContents());
433 $upload->rm();
434 }
435 }
436 }
437
438 if ($action == 'edit') {
439 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
440 attach IS NOT NULL
441 FROM evenements AS e
442 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
443 WHERE id={?}', $eid);
444 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
445 $page->assign('titre',$titre);
446 $page->assign('texte',$texte);
447 $page->assign('texte_html', pl_entity_decode($texte));
448 $page->assign('promo_min',$promo_min);
449 $page->assign('promo_max',$promo_max);
450 $page->assign('peremption',$peremption);
451 $page->assign('important', $important);
452 $page->assign('eid', $eid);
453 $page->assign('img', $img);
454 $page->assign_by_ref('upload', $upload);
455
456 $select = "";
457 for ($i = 1 ; $i < 30 ; $i++) {
458 $p_stamp=date("Ymd",time()+3600*24*$i);
459 $year=substr($p_stamp,0,4);
460 $month=substr($p_stamp,4,2);
461 $day=substr($p_stamp,6,2);
462
463 $select .= "<option value=\"$p_stamp\""
464 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
465 . "> $day / $month / $year</option>\n";
466 }
467 $page->assign('select',$select);
468 } else {
469 switch ($action) {
470 case 'delete':
471 XDB::execute('DELETE from evenements
472 WHERE id = {?}', $eid);
473 break;
474
475 case "archive":
476 XDB::execute('UPDATE evenements
477 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
478 WHERE id = {?}', $eid);
479 break;
480
481 case "unarchive":
482 XDB::execute('UPDATE evenements
483 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
484 WHERE id = {?}', $eid);
485 $action = 'archives';
486 $arch = true;
487 break;
488
489 case "valid":
490 XDB::execute('UPDATE evenements
491 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
492 WHERE id = {?}', $eid);
493 break;
494
495 case "unvalid":
496 XDB::execute('UPDATE evenements
497 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
498 WHERE id = {?}', $eid);
499 break;
500 }
501
502 $pid = ($eid && $action == 'preview') ? $eid : -1;
503 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
504 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
505 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
506 e.promo_min, e.promo_max,
507 FIND_IN_SET('valide', e.flags) AS fvalide,
508 FIND_IN_SET('archive', e.flags) AS farch,
509 u.promo, u.nom, u.prenom, a.alias AS forlife
510 FROM evenements AS e
511 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
512 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
513 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
514 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
515 $page->assign('evs', XDB::iterator($sql));
516 }
517 $page->assign('arch', $arch);
518 $page->assign('admin_evts', true);
519 }
520 }
521
522 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
523 ?>