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