2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class EventsModule
extends PLModule
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
, 'user', NO_AUTH
),
30 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
31 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
33 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE
, 'user', NO_AUTH
),
34 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP
, 'admin'),
38 function get_tips($exclude = null
)
41 // Add a new special tip when changing plat/al version
42 if ($globals->version
!= S
::v('last_version') && is_null($exclude)) {
43 XDB
::execute('UPDATE auth_user_quick
44 SET last_version = {?}
46 $globals->version
, S
::i('uid'));
47 return array('id' => 0,
48 'titre' => 'Bienvenue sur la nouvelle version du site !',
49 'text' => 'Le site a été mis à jour depuis ta dernière visite vers la version ' . $globals->version
50 . '. Nous t\'invitons à en découvrir les nouveautés en te rendant sur '
51 . '<a href="banana/xorg.m4x.innovation">nos forums</a> ou en consultant '
52 . '<a href="changelog">la liste exhaustive des modifications</a>',
60 $exclude = is_null($exclude) ?
'' : ' AND id != ' . $exclude . ' ';
61 $priority = rand(0, 510);
63 $priority = (int)($priority/2);
64 $res = XDB
::query("SELECT *
66 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
67 AND (promo_min = 0 OR promo_min <= {?})
68 AND (promo_max = 0 OR promo_max >= {?})
70 AND (state = 'active')
74 S
::i('promo'), S
::i('promo'), $priority);
75 } while ($priority && !$res->numRows());
76 if (!$res->numRows()) {
79 return $res->fetchOneAssoc();
82 function get_events($where, $order, array &$array, $name)
84 // affichage des evenements
85 // annonces promos triées par présence d'une limite sur les promos
86 // puis par dates croissantes d'expiration
87 $promo = S
::v('promo');
89 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
91 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
92 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
93 AND (e.promo_min = 0 || e.promo_min <= {?})
94 AND (e.promo_max = 0 || e.promo_max >= {?})
97 $sum = XDB
::iterator($sql, $uid, $promo, $promo);
101 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
103 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
104 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
105 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
106 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
107 AND (e.promo_min = 0 || e.promo_min <= {?})
108 AND (e.promo_max = 0 || e.promo_max >= {?})
109 AND ev.user_id IS NULL
112 $evt = XDB
::iterator($sql, $uid, $promo, $promo);
113 $array[$name] = array('events' => $evt, 'summary' => $sum);
117 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
119 $page->changeTpl('events/index.tpl');
120 $page->addJsLink('ajax.js');
121 $page->assign('tips', $this->get_tips());
123 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
124 WHERE user_id={?}', S
::v('uid'));
125 list($date, $naissance) = $res->fetchOneRow();
127 // incitation à mettre à jour la fiche
129 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
130 substr($date, 0, 4));
131 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
132 // si fiche date de + de 400j;
133 $page->assign('fiche_incitation', $date);
136 // Souhaite bon anniversaire
138 if (substr($naissance, 5) == date('m-d')) {
139 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
142 // incitation à mettre une photo
144 $res = XDB
::query('SELECT COUNT(*) FROM photo
145 WHERE uid={?}', S
::v('uid'));
146 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
148 // Incitation à se géolocaliser
149 require_once 'geoloc.inc.php';
150 $res = localize_addresses(S
::v('uid', -1));
151 $page->assign('geoloc_incitation', count($res));
154 if (S
::has('core_rss_hash')) {
155 $page->setRssLink('Polytechnique.org :: News',
156 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
159 // cache les evenements lus et raffiche les evenements a relire
160 if ($action == 'read' && $eid) {
161 XDB
::execute('DELETE evenements_vus.*
162 FROM evenements_vus AS ev
163 INNER JOIN evenements AS e ON e.id = ev.evt_id
164 WHERE peremption < NOW()');
165 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
167 pl_redirect('events#'.$pound);
170 if ($action == 'unread' && $eid) {
171 XDB
::execute('DELETE FROM evenements_vus
172 WHERE evt_id = {?} AND user_id = {?}',
174 pl_redirect('events#newsid'.$eid);
178 $this->get_events('FIND_IN_SET(\'important\', e.flags)', 'e.creation_date DESC', $array, 'important');
179 $this->get_events('e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY)
180 AND NOT FIND_IN_SET(\'important\', e.flags)',
181 'e.creation_date DESC', $array, 'news');
182 $this->get_events('e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY)
183 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
184 AND NOT FIND_IN_SET(\'important\', e.flags)',
185 'e.peremption, e.creation_date DESC', $array, 'end');
186 $this->get_events('e.peremption >= DATE_ADD(CURDATE(), INTERVAL 2 DAY)
187 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
188 AND NOT FIND_IN_SET(\'important\', e.flags)',
189 'e.peremption, e.creation_date DESC', $array, 'body');
190 $page->assign_by_ref('events', $array);
193 function handler_rss(&$page, $user = null
, $hash = null
)
195 require_once 'rss.inc.php';
197 $uid = init_rss('events/rss.tpl', $user, $hash);
199 $rss = XDB
::iterator(
200 'SELECT e.id, e.titre, e.texte, e.creation_date,
201 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
202 FROM auth_user_md5 AS u
203 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
204 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
205 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
206 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
207 AND peremption >= NOW()', $uid);
208 $page->assign('rss', $rss);
211 function handler_preview(&$page)
213 require_once('url_catcher.inc.php');
214 $page->changeTpl('events/preview.tpl', NO_SKIN
);
215 $texte = Get
::v('texte');
216 if (!is_utf8($texte)) {
217 $texte = utf8_encode($texte);
219 if (strpos($_SERVER['HTTP_REFERER'], 'admin') === false
) {
220 $texte = url_catcher(pl_entities($texte));
222 $titre = Get
::v('titre');
223 if (!is_utf8($titre)) {
224 $titre = utf8_encode($titre);
226 $page->assign('texte_html', $texte);
227 $page->assign('titre', $titre);
228 header('Content-Type: text/html; charset=utf-8');
231 function handler_ev_submit(&$page)
233 $page->changeTpl('events/submit.tpl');
234 $page->addJsLink('ajax.js');
236 require_once('wiki.inc.php');
237 wiki_require_page('Xorg.Annonce');
239 $titre = Post
::v('titre');
240 $texte = Post
::v('texte');
241 $promo_min = Post
::i('promo_min');
242 $promo_max = Post
::i('promo_max');
243 $peremption = Post
::i('peremption');
244 $valid_mesg = Post
::v('valid_mesg');
245 $action = Post
::v('action');
247 if (($promo_min > $promo_max && $promo_max != 0)||
248 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
249 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
251 $page->trig("L'intervalle de promotions n'est pas valide");
255 require_once('url_catcher.inc.php');
256 $texte_catch_url = url_catcher($texte);
258 $page->assign('titre', $titre);
259 $page->assign('texte', $texte);
260 $page->assign('texte_html', $texte_catch_url);
261 $page->assign('promo_min', $promo_min);
262 $page->assign('promo_max', $promo_max);
263 $page->assign('peremption', $peremption);
264 $page->assign('valid_mesg', $valid_mesg);
265 $page->assign('action', strtolower($action));
267 if ($action && (!trim($texte) ||
!trim($titre))) {
268 $page->trig("L'article doit avoir un titre et un contenu");
270 $texte = $texte_catch_url;
271 require_once 'validations.inc.php';
272 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
273 $peremption, $valid_mesg, S
::v('uid'));
275 $page->assign('ok', true
);
279 for ($i = 1 ; $i < 30 ; $i++
) {
280 $time = time() +
3600 * 24 * $i;
281 $p_stamp = date('Ymd', $time);
282 $year = date('Y', $time);
283 $month = date('m', $time);
284 $day = date('d', $time);
286 $select .= "<option value=\"$p_stamp\"";
287 if ($p_stamp == strtr($peremption, array("-" => ""))) {
288 $select .= " selected='selected'";
290 $select .= "> $day / $month / $year</option>\n";
292 $page->assign('select',$select);
295 function handler_tips(&$page, $tips = null
)
297 header('Content-Type: text/html; charset="UTF-8"');
298 $page->changeTpl('include/tips.tpl', NO_SKIN
);
299 $page->assign('tips', $this->get_tips($tips));
302 function handler_admin_tips(&$page, $action = 'list', $id = null
)
304 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
305 $page->assign('title', 'Gestion des Astuces');
306 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
307 $table_editor->describe('peremption', 'date de péremption', true
);
308 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
309 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
310 $table_editor->describe('titre', 'titre', true
);
311 $table_editor->describe('state', 'actif', true
);
312 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
313 $table_editor->describe('priorite', '0<=priorité<=255', true
);
314 $table_editor->list_on_edit(false
);
315 $table_editor->apply($page, $action, $id);
316 if (($action == 'edit' && !is_null($id)) ||
$action == 'update') {
317 $page->changeTpl('events/admin_tips.tpl');
321 function handler_admin_events(&$page, $action = 'list', $eid = null
)
323 $page->changeTpl('events/admin.tpl');
324 $page->addJsLink('ajax.js');
325 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
326 $page->register_modifier('hde', 'html_entity_decode');
328 $arch = $action == 'archives';
329 $page->assign('action', $action);
331 if (Post
::v('action') == "Proposer" && $eid) {
332 $promo_min = Post
::i('promo_min');
333 $promo_max = Post
::i('promo_max');
334 if ($promo_min > $promo_max ||
335 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
336 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
338 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
341 $res = XDB
::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
342 $flags = new FlagSet($res->fetchOneCell());
343 if (Post
::v('important')) {
344 $flags->addFlag('important');
346 $flags->rmFlag('important');
348 XDB
::execute('UPDATE evenements
349 SET creation_date = creation_date,
350 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
353 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
354 Post
::v('promo_min'), Post
::v('promo_max'),
355 $flags->flags(), $eid);
359 if ($action == 'edit') {
360 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags)
362 WHERE id={?}', $eid);
363 list($titre, $texte, $peremption, $promo_min, $promo_max, $important) = $res->fetchOneRow();
364 $page->assign('titre',$titre);
365 $page->assign('texte',$texte);
366 $page->assign('texte_html', pl_entity_decode($texte));
367 $page->assign('promo_min',$promo_min);
368 $page->assign('promo_max',$promo_max);
369 $page->assign('peremption',$peremption);
370 $page->assign('important', $important);
373 for ($i = 1 ; $i < 30 ; $i++
) {
374 $p_stamp=date("Ymd",time()+
3600*24*$i);
375 $year=substr($p_stamp,0,4);
376 $month=substr($p_stamp,4,2);
377 $day=substr($p_stamp,6,2);
379 $select .= "<option value=\"$p_stamp\""
380 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
381 . "> $day / $month / $year</option>\n";
383 $page->assign('select',$select);
387 XDB
::execute('DELETE from evenements
388 WHERE id = {?}', $eid);
392 XDB
::execute('UPDATE evenements
393 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
394 WHERE id = {?}', $eid);
398 XDB
::execute('UPDATE evenements
399 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
400 WHERE id = {?}', $eid);
401 $action = 'archives';
406 XDB
::execute('UPDATE evenements
407 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
408 WHERE id = {?}', $eid);
412 XDB
::execute('UPDATE evenements
413 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
414 WHERE id = {?}', $eid);
418 $pid = ($eid && $action == 'preview') ?
$eid : -1;
419 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
420 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
421 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
422 e.promo_min, e.promo_max,
423 FIND_IN_SET('valide', e.flags) AS fvalide,
424 FIND_IN_SET('archive', e.flags) AS farch,
425 u.promo, u.nom, u.prenom, a.alias AS forlife
427 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
428 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
429 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
430 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
431 $page->assign('evs', XDB
::iterator($sql));
433 $page->assign('arch', $arch);
434 $page->assign('admin_evts', true
);
438 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: