Move profile includes into modules/profile/
[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 $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 >= {?})
50 AND (state = 'active')
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
62 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
63 {
64 $page->changeTpl('events/index.tpl');
65 $page->addJsLink('ajax.js');
66 $page->assign('tips', $this->get_tips());
67
68 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
69 WHERE user_id={?}', S::v('uid'));
70 list($date, $naissance) = $res->fetchOneRow();
71
72 // incitation à mettre à jour la fiche
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
87 // incitation à mettre une photo
88
89 $res = XDB::query('SELECT COUNT(*) FROM photo
90 WHERE uid={?}', S::v('uid'));
91 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
92
93 // Incitation à se géolocaliser
94 require_once 'geoloc.inc.php';
95 $res = localize_addresses(S::v('uid', -1));
96 $page->assign('geoloc_incitation', count($res));
97
98 // ajout du lien RSS
99 if (S::has('core_rss_hash')) {
100 $page->setRssLink('Polytechnique.org :: News',
101 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
102 }
103
104 // cache les evenements lus et raffiche les evenements a relire
105 if ($action == 'read' && $eid) {
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()');
110 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
111 $eid, S::v('uid'));
112 pl_redirect('events#'.$pound);
113 }
114
115 if ($action == 'unread' && $eid) {
116 XDB::execute('DELETE FROM evenements_vus
117 WHERE evt_id = {?} AND user_id = {?}',
118 $eid, S::v('uid'));
119 pl_redirect('events#newsid'.$eid);
120 }
121
122 // affichage des evenements
123 // annonces promos triées par présence d'une limite sur les promos
124 // puis par dates croissantes d'expiration
125 $promo = S::v('promo');
126 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
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 = {?})
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',
137 XDB::iterator($sql, S::v('uid'),
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',
149 XDB::iterator($sql, S::v('uid'),
150 $promo, $promo)
151 );
152 }
153
154 function handler_rss(&$page, $user = null, $hash = null)
155 {
156 require_once 'rss.inc.php';
157
158 $uid = init_rss('events/rss.tpl', $user, $hash);
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);
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 }
191
192 function handler_ev_submit(&$page)
193 {
194 $page->changeTpl('events/submit.tpl');
195 $page->addJsLink('ajax.js');
196
197 require_once('wiki.inc.php');
198 wiki_require_page('Xorg.Annonce');
199
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');
207
208 if (($promo_min > $promo_max && $promo_max != 0)||
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
216 require_once('url_catcher.inc.php');
217 $texte_catch_url = url_catcher($texte);
218
219 $page->assign('titre', $titre);
220 $page->assign('texte', $texte);
221 $page->assign('texte_html', $texte_catch_url);
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
228 if ($action && (!trim($texte) || !trim($titre))) {
229 $page->trig("L'article doit avoir un titre et un contenu");
230 } elseif ($action) {
231 $texte = $texte_catch_url;
232 require_once 'validations.inc.php';
233 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
234 $peremption, $valid_mesg, S::v('uid'));
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);
254 }
255
256 function handler_tips(&$page, $tips = null)
257 {
258 header('Content-Type: text/html; charset="UTF-8"');
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');
268 $table_editor->describe('peremption', 'date de péremption', true);
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);
272 $table_editor->describe('state', 'actif', true);
273 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
274 $table_editor->describe('priorite', '0<=priorité<=255', true);
275 $table_editor->list_on_edit(false);
276 $table_editor->apply($page, $action, $id);
277 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
278 $page->changeTpl('events/admin_tips.tpl');
279 }
280 }
281
282 function handler_admin_events(&$page, $action = 'list', $eid = null)
283 {
284 $page->changeTpl('events/admin.tpl');
285 $page->addJsLink('ajax.js');
286 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
287 $page->register_modifier('hde', 'html_entity_decode');
288
289 $arch = $action == 'archives';
290 $page->assign('action', $action);
291
292 if (Post::v('action') == "Proposer" && $eid) {
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 }
308 }
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);
317 $page->assign('texte_html', pl_entity_decode($texte));
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,
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)
380 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
381 $page->assign('evs', XDB::iterator($sql));
382 }
383 $page->assign('arch', $arch);
384 }
385}
386
387// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
388?>