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