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