Only remains promo importer to rewrite in modules/admin.php.
[platal.git] / modules / events.php
CommitLineData
74e0093f 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
74e0093f 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),
8fc4efa3 28 'rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
9ed396c0 29 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC, 'user', NO_AUTH),
f62bd784 30 'events/photo' => $this->make_hook('photo', AUTH_PUBLIC),
1a828cd4 31 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
92423144 32 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
b829e258 33
9ed396c0 34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, 'user', NO_AUTH),
dc767839 35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
74e0093f 36 );
37 }
38
02838718 39 private function get_tips($exclude = null)
dc767839 40 {
6ce5dee4 41 global $globals;
42 // Add a new special tip when changing plat/al version
908db125 43 if ($globals->version != S::v('last_version') && is_null($exclude)) {
bc910323 44 XDB::execute('UPDATE accounts
6ce5dee4 45 SET last_version = {?}
bc910323 46 WHERE uid = {?}',
6ce5dee4 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
44837f02
FB
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">'
661e78f7 54 . 'les forums</a>, ou sur <a href="changelog">la liste exhaustive des modifications</a>.',
6ce5dee4 55 'priorite' => 255,
56 'promo_min' => 0,
57 'promo_max' => 0,
58 'state' => 'active',
59 'special' => true);
908db125 60 }
6ce5dee4 61
73279cf8 62 $exclude = is_null($exclude) ? '' : ' AND id != ' . intval($exclude) . ' ';
dc767839 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 >= {?})
5d617d8d 72 AND (state = 'active')
dc767839 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;
eaf30d86 80 }
dc767839 81 return $res->fetchOneAssoc();
82 }
83
04334c61 84 private function upload_image(PlPage &$page, PlUpload &$upload)
02838718 85 {
86 if (@!$_FILES['image']['tmp_name'] && !Env::v('image_url')) {
87 return true;
88 }
89 if (!$upload->upload($_FILES['image']) && !$upload->download(Env::v('image_url'))) {
a7d35093 90 $page->trigError('Impossible de télécharger l\'image');
02838718 91 return false;
92 } elseif (!$upload->isType('image')) {
a7d35093 93 $page->trigError('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG.');
02838718 94 $upload->rm();
95 return false;
96 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
a7d35093 97 $page->trigError('Impossible de retraiter l\'image');
02838718 98 return false;
99 }
100 return true;
101 }
102
096f0dca 103 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
1a828cd4 104 {
8b1f8e12 105 $page->changeTpl('events/index.tpl');
dc767839 106 $page->addJsLink('ajax.js');
107 $page->assign('tips', $this->get_tips());
108
c52838d6
VZ
109 // Profile update (appears when profile is > 400d old), and birthday
110 // oneboxes.
bc910323
FB
111 $user = S::user();
112 $profile = $user->profile();
113 if (!is_null($profile)) {
114 if (strtotime($profile->last_change) < time() - (400 * 86400)) {
115 $page->assign('fiche_incitation', $profile->last_change);
116 }
117 if ($profile->next_birthday == date('Y-m-d')) {
118 $birthyear = (int)date('Y', strtotime($profile->birthdate));
119 $curyear = (int)date('Y');
120 $page->assign('birthday', $curyear - $birthyear);
121 }
1a828cd4 122 }
123
c52838d6 124 // No-photo onebox.
bc910323
FB
125 $res = XDB::query("SELECT COUNT(*)
126 FROM photo
127 WHERE uid = {?}",
128 S::user()->id());
1a828cd4 129 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
130
c52838d6 131 // Geo-location onebox.
1a828cd4 132 require_once 'geoloc.inc.php';
c52838d6 133 $res = localize_addresses(S::user()->id());
1a828cd4 134 $page->assign('geoloc_incitation', count($res));
135
c52838d6 136 // Direct link to the RSS feed, when available.
84fc72ff 137 if (S::hasAuthToken()) {
162370e7 138 $page->setRssLink('Polytechnique.org :: News',
84fc72ff 139 '/rss/'.S::v('hruid') .'/'.S::v('token').'/rss.xml');
1a828cd4 140 }
141
c52838d6 142 // Hide the read event, and reload the page to get to the next event.
4e61caea 143 if ($action == 'read' && $eid) {
12500b65 144 XDB::execute('DELETE ev.*
eaf30d86 145 FROM evenements_vus AS ev
eadff9f9 146 INNER JOIN evenements AS e ON e.id = ev.evt_id
147 WHERE peremption < NOW()');
08cce2ff 148 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
096f0dca 149 $eid, S::v('uid'));
150 pl_redirect('events#'.$pound);
1a828cd4 151 }
152
c52838d6 153 // Unhide the requested event, and reload the page to display it.
4e61caea 154 if ($action == 'unread' && $eid) {
08cce2ff 155 XDB::execute('DELETE FROM evenements_vus
eadff9f9 156 WHERE evt_id = {?} AND user_id = {?}',
4e61caea 157 $eid, S::v('uid'));
096f0dca 158 pl_redirect('events#newsid'.$eid);
1a828cd4 159 }
160
c52838d6 161 // Fetch the events to display, along with their metadata.
d0e45ced 162 $array = array();
a087cc8d 163 $it = XDB::iterator("SELECT e.id, e.titre, e.texte, e.post_id, e.user_id,
c321aa99
FB
164 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki,
165 FIND_IN_SET('important', e.flags) AS important,
166 e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS news,
167 e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY) AS end,
a087cc8d 168 ev.user_id IS NULL AS nonlu, e.promo_min, e.promo_max
c321aa99 169 FROM evenements AS e
fb2c09c9 170 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
fb2c09c9 171 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
c321aa99 172 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
c321aa99 173 ORDER BY important DESC, news DESC, end DESC, e.peremption, e.creation_date DESC",
a087cc8d 174 S::i('uid'));
c321aa99 175 $cats = array('important', 'news', 'end', 'body');
a0b45322
FB
176
177 $this->load('feed.inc.php');
178 $user = S::user();
179 $body = EventFeed::nextEvent($it, $user);
c321aa99
FB
180 foreach ($cats as $cat) {
181 $data = array();
182 if (!$body) {
183 continue;
184 }
185 do {
186 if ($cat == 'body' || $body[$cat]) {
187 $data[] = $body;
188 } else {
189 break;
190 }
a0b45322 191 $body = EventFeed::nextEvent($it);
c321aa99
FB
192 } while ($body);
193 if (!empty($data)) {
194 $array[$cat] = $data;
195 }
196 }
a087cc8d 197
d0e45ced 198 $page->assign_by_ref('events', $array);
1a828cd4 199 }
200
02838718 201 function handler_photo(&$page, $eid = null, $valid = null)
202 {
203 if ($eid && $eid != 'valid') {
204 $res = XDB::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
205 if ($res->numRows()) {
206 $photo = $res->fetchOneAssoc();
207 header('Content-Type: image/' . $photo['attachmime']);
208 echo $photo['attach'];
209 exit;
210 }
211 } elseif ($eid == 'valid') {
212 require_once 'validations.inc.php';
213 $valid = Validate::get_request_by_id($valid);
214 if ($valid && $valid->img) {
215 header('Content-Type: image/' . $valid->imgtype);
216 echo $valid->img;
217 exit;
218 }
219 } else {
f3df6d38 220 $upload = new PlUpload(S::user()->login(), 'event');
02838718 221 if ($upload->exists() && $upload->isType('image')) {
222 header('Content-Type: ' . $upload->contentType());
223 echo $upload->getContents();
224 exit;
225 }
226 }
227 global $globals;
228 header('Content-Type: image/png');
229 echo file_get_contents($globals->spoolroot . '/htdocs/images/logo.png');
230 exit;
231 }
232
d1c97e42 233 function handler_rss(&$page, $user = null, $hash = null)
eaf30d86 234 {
460d8f55 235 $this->load('feed.inc.php');
61fa44d9 236 $feed = new EventFeed();
8fe7768e 237 return $feed->run($page, $user, $hash);
db3bd146 238 }
239
240 function handler_preview(&$page)
241 {
db3bd146 242 $page->changeTpl('events/preview.tpl', NO_SKIN);
243 $texte = Get::v('texte');
244 if (!is_utf8($texte)) {
245 $texte = utf8_encode($texte);
246 }
db3bd146 247 $titre = Get::v('titre');
248 if (!is_utf8($titre)) {
249 $titre = utf8_encode($titre);
250 }
794feea7 251 $page->assign('texte', $texte);
db3bd146 252 $page->assign('titre', $titre);
253 header('Content-Type: text/html; charset=utf-8');
254 }
d1c97e42 255
1a828cd4 256 function handler_ev_submit(&$page)
74e0093f 257 {
8b1f8e12 258 $page->changeTpl('events/submit.tpl');
db3bd146 259 $page->addJsLink('ajax.js');
eaf30d86 260
8f201b69
FB
261 $wp = new PlWikiPage('Xorg.Annonce');
262 $wp->buildCache();
74e0093f 263
5e2307dc 264 $titre = Post::v('titre');
265 $texte = Post::v('texte');
266 $promo_min = Post::i('promo_min');
267 $promo_max = Post::i('promo_max');
268 $peremption = Post::i('peremption');
269 $valid_mesg = Post::v('valid_mesg');
270 $action = Post::v('action');
f3df6d38 271 $upload = new PlUpload(S::user()->login(), 'event');
02838718 272 $this->upload_image($page, $upload);
74e0093f 273
d1c97e42 274 if (($promo_min > $promo_max && $promo_max != 0)||
2086ab7f 275 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
276 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
277 {
a7d35093 278 $page->trigError("L'intervalle de promotions n'est pas valide");
2086ab7f 279 $action = null;
280 }
281
74e0093f 282 $page->assign('titre', $titre);
283 $page->assign('texte', $texte);
284 $page->assign('promo_min', $promo_min);
285 $page->assign('promo_max', $promo_max);
286 $page->assign('peremption', $peremption);
287 $page->assign('valid_mesg', $valid_mesg);
288 $page->assign('action', strtolower($action));
02838718 289 $page->assign_by_ref('upload', $upload);
74e0093f 290
02838718 291 if ($action == 'Supprimer l\'image') {
292 $upload->rm();
293 $page->assign('action', false);
294 } elseif ($action && (!trim($texte) || !trim($titre))) {
a7d35093 295 $page->trigError("L'article doit avoir un titre et un contenu");
db3bd146 296 } elseif ($action) {
7b642046
VZ
297 S::assert_xsrf_token();
298
74e0093f 299 require_once 'validations.inc.php';
300 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
5daf68f6 301 $peremption, $valid_mesg, S::user(), $upload);
74e0093f 302 $evtreq->submit();
303 $page->assign('ok', true);
02838718 304 } elseif (!Env::v('preview')) {
305 $upload->rm();
74e0093f 306 }
74e0093f 307 }
b829e258 308
dc767839 309 function handler_tips(&$page, $tips = null)
310 {
493b6abe 311 header('Content-Type: text/html; charset="UTF-8"');
dc767839 312 $page->changeTpl('include/tips.tpl', NO_SKIN);
313 $page->assign('tips', $this->get_tips($tips));
314 }
315
316 function handler_admin_tips(&$page, $action = 'list', $id = null)
317 {
46f272fe 318 $page->setTitle('Administration - Astuces');
dc767839 319 $page->assign('title', 'Gestion des Astuces');
320 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
a7de4ef7 321 $table_editor->describe('peremption', 'date de péremption', true);
dc767839 322 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
323 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
324 $table_editor->describe('titre', 'titre', true);
5d617d8d 325 $table_editor->describe('state', 'actif', true);
dc767839 326 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
a7de4ef7 327 $table_editor->describe('priorite', '0<=priorité<=255', true);
3851b0a6 328 $table_editor->list_on_edit(false);
dc767839 329 $table_editor->apply($page, $action, $id);
3851b0a6 330 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
3032cbd8 331 $page->changeTpl('events/admin_tips.tpl');
332 }
dc767839 333 }
334
eaf30d86 335 function handler_admin_events(&$page, $action = 'list', $eid = null)
506a46ed 336 {
e2efba7d 337 $page->changeTpl('events/admin.tpl');
db3bd146 338 $page->addJsLink('ajax.js');
46f272fe 339 $page->setTitle('Administration - Evenements');
506a46ed 340 $page->register_modifier('hde', 'html_entity_decode');
341
342 $arch = $action == 'archives';
343 $page->assign('action', $action);
02838718 344
f3df6d38 345 $upload = new PlUpload(S::user()->login(), 'event');
02838718 346 if ((Env::has('preview') || Post::v('action') == "Proposer") && $eid) {
347 $action = 'edit';
348 $this->upload_image($page, $upload);
349 }
350
351 if (Post::v('action') == 'Pas d\'image' && $eid) {
7b642046 352 S::assert_xsrf_token();
02838718 353 $upload->rm();
354 XDB::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
355 $action = 'edit';
356 } elseif (Post::v('action') == 'Supprimer l\'image' && $eid) {
7b642046 357 S::assert_xsrf_token();
02838718 358 $upload->rm();
359 $action = 'edit';
360 } elseif (Post::v('action') == "Proposer" && $eid) {
7b642046 361 S::assert_xsrf_token();
2086ab7f 362 $promo_min = Post::i('promo_min');
363 $promo_max = Post::i('promo_max');
1b602af5 364 if (($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
365 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020 || $promo_max < $promo_min)))
2086ab7f 366 {
a7d35093 367 $page->trigError("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
2086ab7f 368 $action = 'edit';
369 } else {
9ed396c0 370 $res = XDB::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
113f6de8 371 $flags = new PlFlagSet($res->fetchOneCell());
794feea7 372 $flags->addFlag('wiki');
9ed396c0 373 if (Post::v('important')) {
374 $flags->addFlag('important');
375 } else {
376 $flags->rmFlag('important');
eaf30d86 377 }
794feea7 378
2086ab7f 379 XDB::execute('UPDATE evenements
eaf30d86 380 SET creation_date = creation_date,
9ed396c0 381 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
382 flags = {?}
eaf30d86 383 WHERE id = {?}',
2086ab7f 384 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
9ed396c0 385 Post::v('promo_min'), Post::v('promo_max'),
77e786e1 386 $flags, $eid);
02838718 387 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
388 XDB::execute('REPLACE INTO evenements_photo
389 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
390 $eid, $type, $x, $y, $upload->getContents());
391 $upload->rm();
392 }
eaf30d86 393 }
92423144 394 }
506a46ed 395
396 if ($action == 'edit') {
02838718 397 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
398 attach IS NOT NULL
399 FROM evenements AS e
400 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
506a46ed 401 WHERE id={?}', $eid);
02838718 402 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
506a46ed 403 $page->assign('titre',$titre);
404 $page->assign('texte',$texte);
405 $page->assign('promo_min',$promo_min);
406 $page->assign('promo_max',$promo_max);
407 $page->assign('peremption',$peremption);
9ed396c0 408 $page->assign('important', $important);
02838718 409 $page->assign('eid', $eid);
410 $page->assign('img', $img);
411 $page->assign_by_ref('upload', $upload);
506a46ed 412
413 $select = "";
414 for ($i = 1 ; $i < 30 ; $i++) {
415 $p_stamp=date("Ymd",time()+3600*24*$i);
416 $year=substr($p_stamp,0,4);
417 $month=substr($p_stamp,4,2);
418 $day=substr($p_stamp,6,2);
419
eaf30d86 420 $select .= "<option value=\"$p_stamp\""
506a46ed 421 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
422 . "> $day / $month / $year</option>\n";
423 }
424 $page->assign('select',$select);
425 } else {
426 switch ($action) {
427 case 'delete':
7b642046 428 S::assert_xsrf_token();
506a46ed 429 XDB::execute('DELETE from evenements
430 WHERE id = {?}', $eid);
431 break;
432
433 case "archive":
7b642046 434 S::assert_xsrf_token();
506a46ed 435 XDB::execute('UPDATE evenements
436 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
437 WHERE id = {?}', $eid);
438 break;
439
440 case "unarchive":
7b642046 441 S::assert_xsrf_token();
506a46ed 442 XDB::execute('UPDATE evenements
443 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
444 WHERE id = {?}', $eid);
445 $action = 'archives';
446 $arch = true;
447 break;
448
449 case "valid":
7b642046 450 S::assert_xsrf_token();
506a46ed 451 XDB::execute('UPDATE evenements
452 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
453 WHERE id = {?}', $eid);
454 break;
455
456 case "unvalid":
7b642046 457 S::assert_xsrf_token();
506a46ed 458 XDB::execute('UPDATE evenements
459 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
460 WHERE id = {?}', $eid);
461 break;
462 }
463
464 $pid = ($eid && $action == 'preview') ? $eid : -1;
bc910323 465 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview, e.user_id,
92423144 466 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
467 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
468 e.promo_min, e.promo_max,
469 FIND_IN_SET('valide', e.flags) AS fvalide,
470 FIND_IN_SET('archive', e.flags) AS farch,
c321aa99 471 FIND_IN_SET('wiki', e.flags) AS wiki
92423144 472 FROM evenements AS e
92423144 473 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
506a46ed 474 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
92423144 475 $page->assign('evs', XDB::iterator($sql));
476 }
506a46ed 477 $page->assign('arch', $arch);
9ed396c0 478 $page->assign('admin_evts', true);
eaf30d86 479 }
74e0093f 480}
481
a7de4ef7 482// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
74e0093f 483?>