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