Convert source code to UTF-8
[platal.git] / modules / events.php
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
22 class 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/submit' => $this->make_hook('ev_submit', AUTH_MDP),
30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
31
32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, '', NO_AUTH),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
34 );
35 }
36
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 >= {?})
49 AND (state = 'active')
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
61 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
62 {
63 $page->changeTpl('events/index.tpl');
64 $page->addJsLink('ajax.js');
65 $page->assign('tips', $this->get_tips());
66
67 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
68 WHERE user_id={?}', S::v('uid'));
69 list($date, $naissance) = $res->fetchOneRow();
70
71 // incitation à mettre à jour la fiche
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
86 // incitation à mettre une photo
87
88 $res = XDB::query('SELECT COUNT(*) FROM photo
89 WHERE uid={?}', S::v('uid'));
90 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
91
92 // Incitation à se géolocaliser
93 require_once 'geoloc.inc.php';
94 $res = localize_addresses(S::v('uid', -1));
95 $page->assign('geoloc_incitation', count($res));
96
97 // ajout du lien RSS
98 if (S::has('core_rss_hash')) {
99 $page->setRssLink('Polytechnique.org :: News',
100 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
101 }
102
103 // cache les evenements lus et raffiche les evenements a relire
104 if ($action == 'read' && $eid) {
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()');
109 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
110 $eid, S::v('uid'));
111 pl_redirect('events#'.$pound);
112 }
113
114 if ($action == 'unread' && $eid) {
115 XDB::execute('DELETE FROM evenements_vus
116 WHERE evt_id = {?} AND user_id = {?}',
117 $eid, S::v('uid'));
118 pl_redirect('events#newsid'.$eid);
119 }
120
121 // affichage des evenements
122 // annonces promos triées par présence d'une limite sur les promos
123 // puis par dates croissantes d'expiration
124 $promo = S::v('promo');
125 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
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 = {?})
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',
136 XDB::iterator($sql, S::v('uid'),
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',
148 XDB::iterator($sql, S::v('uid'),
149 $promo, $promo)
150 );
151 }
152
153 function handler_rss(&$page, $user = null, $hash = null)
154 {
155 require_once 'rss.inc.php';
156
157 $uid = init_rss('events/rss.tpl', $user, $hash);
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
171 function handler_ev_submit(&$page)
172 {
173 $page->changeTpl('events/submit.tpl');
174
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');
182
183 if (($promo_min > $promo_max && $promo_max != 0)||
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
191 require_once('url_catcher.inc.php');
192 $texte_catch_url = url_catcher($texte);
193
194 $page->assign('titre', $titre);
195 $page->assign('texte', $texte);
196 $page->assign('texte_html', $texte_catch_url);
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 && (!trim($texte) || !trim($titre))) {
204 $page->trig("L'article doit avoir un titre et un contenu");
205 } elseif ($action == 'Confirmer') {
206 $texte = $texte_catch_url;
207 require_once 'validations.inc.php';
208 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
209 $peremption, $valid_mesg, S::v('uid'));
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);
229 }
230
231 function handler_tips(&$page, $tips = null)
232 {
233 header('Content-Type: text/html; charset="ISO-8859-15"');
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');
243 $table_editor->describe('peremption', 'date de péremption', true);
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);
247 $table_editor->describe('state', 'actif', true);
248 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
249 $table_editor->describe('priorite', '0<=priorité<=255', true);
250 $table_editor->list_on_edit(false);
251 $table_editor->apply($page, $action, $id);
252 if (($action == 'edit' && !is_null($id)) || $action == 'update') {
253 $page->changeTpl('events/admin_tips.tpl');
254 }
255 }
256
257 function handler_admin_events(&$page, $action = 'list', $eid = null)
258 {
259 $page->changeTpl('events/admin.tpl');
260 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
261 $page->register_modifier('hde', 'html_entity_decode');
262
263 $arch = $action == 'archives';
264 $page->assign('action', $action);
265
266 if (Post::v('action') == "Proposer" && $eid) {
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 }
282 }
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,
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)
353 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
354 $page->assign('evs', XDB::iterator($sql));
355 }
356 $page->assign('arch', $arch);
357 }
358 }
359
360 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
361 ?>