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