Can choose the CSV separator
[platal.git] / modules / events.php
CommitLineData
74e0093f 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 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(
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
089a5801 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
089a5801 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
089a5801 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
089a5801 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
203 if ($action == 'Confirmer') {
edd50750 204 $texte = $texte_catch_url;
74e0093f 205 require_once 'validations.inc.php';
206 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
cab08090 207 $peremption, $valid_mesg, S::v('uid'));
74e0093f 208 $evtreq->submit();
209 $page->assign('ok', true);
210 }
211
212 $select = '';
213 for ($i = 1 ; $i < 30 ; $i++) {
214 $time = time() + 3600 * 24 * $i;
215 $p_stamp = date('Ymd', $time);
216 $year = date('Y', $time);
217 $month = date('m', $time);
218 $day = date('d', $time);
219
220 $select .= "<option value=\"$p_stamp\"";
221 if ($p_stamp == strtr($peremption, array("-" => ""))) {
222 $select .= " selected='selected'";
223 }
224 $select .= "> $day / $month / $year</option>\n";
225 }
226 $page->assign('select',$select);
74e0093f 227 }
b829e258 228
dc767839 229 function handler_tips(&$page, $tips = null)
230 {
231 $page->changeTpl('include/tips.tpl', NO_SKIN);
232 $page->assign('tips', $this->get_tips($tips));
233 }
234
235 function handler_admin_tips(&$page, $action = 'list', $id = null)
236 {
237 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
238 $page->assign('title', 'Gestion des Astuces');
239 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
240 $table_editor->describe('peremption', 'date de péremption', true);
241 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
242 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
243 $table_editor->describe('titre', 'titre', true);
5d617d8d 244 $table_editor->describe('state', 'actif', true);
dc767839 245 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
246 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false);
247 $table_editor->apply($page, $action, $id);
248 }
249
506a46ed 250 function handler_admin_events(&$page, $action = 'list', $eid = null)
251 {
e2efba7d 252 $page->changeTpl('events/admin.tpl');
92423144 253 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
506a46ed 254 $page->register_modifier('hde', 'html_entity_decode');
255
256 $arch = $action == 'archives';
257 $page->assign('action', $action);
258
01248e3a 259 if (Post::v('action') == "Proposer" && $eid) {
2086ab7f 260 $promo_min = Post::i('promo_min');
261 $promo_max = Post::i('promo_max');
262 if ($promo_min > $promo_max ||
263 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
264 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
265 {
266 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
267 $action = 'edit';
268 } else {
269 XDB::execute('UPDATE evenements
270 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
271 WHERE id = {?}',
272 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
273 Post::v('promo_min'), Post::v('promo_max'), $eid);
274 }
92423144 275 }
506a46ed 276
277 if ($action == 'edit') {
278 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max
279 FROM evenements
280 WHERE id={?}', $eid);
281 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
282 $page->assign('titre',$titre);
283 $page->assign('texte',$texte);
284 $page->assign('promo_min',$promo_min);
285 $page->assign('promo_max',$promo_max);
286 $page->assign('peremption',$peremption);
287
288 $select = "";
289 for ($i = 1 ; $i < 30 ; $i++) {
290 $p_stamp=date("Ymd",time()+3600*24*$i);
291 $year=substr($p_stamp,0,4);
292 $month=substr($p_stamp,4,2);
293 $day=substr($p_stamp,6,2);
294
295 $select .= "<option value=\"$p_stamp\""
296 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
297 . "> $day / $month / $year</option>\n";
298 }
299 $page->assign('select',$select);
300 } else {
301 switch ($action) {
302 case 'delete':
303 XDB::execute('DELETE from evenements
304 WHERE id = {?}', $eid);
305 break;
306
307 case "archive":
308 XDB::execute('UPDATE evenements
309 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
310 WHERE id = {?}', $eid);
311 break;
312
313 case "unarchive":
314 XDB::execute('UPDATE evenements
315 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
316 WHERE id = {?}', $eid);
317 $action = 'archives';
318 $arch = true;
319 break;
320
321 case "valid":
322 XDB::execute('UPDATE evenements
323 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
324 WHERE id = {?}', $eid);
325 break;
326
327 case "unvalid":
328 XDB::execute('UPDATE evenements
329 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
330 WHERE id = {?}', $eid);
331 break;
332 }
333
334 $pid = ($eid && $action == 'preview') ? $eid : -1;
335 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
92423144 336 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
337 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
338 e.promo_min, e.promo_max,
339 FIND_IN_SET('valide', e.flags) AS fvalide,
340 FIND_IN_SET('archive', e.flags) AS farch,
341 u.promo, u.nom, u.prenom, a.alias AS forlife
342 FROM evenements AS e
343 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
344 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
345 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
506a46ed 346 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
92423144 347 $page->assign('evs', XDB::iterator($sql));
348 }
506a46ed 349 $page->assign('arch', $arch);
e2efba7d 350 }
74e0093f 351}
352
353?>