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