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
, '', 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
, '', 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(e.flags, 'valide') 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(e.flags, 'valide') 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('e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY)',
179 'e.creation_date DESC', $array, 'news');
180 $this->get_events('e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY)
181 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)',
182 'e.peremption, e.creation_date DESC', $array, 'end');
183 $this->get_events('e.peremption >= DATE_ADD(CURDATE(), INTERVAL 2 DAY)
184 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)',
185 'e.peremption, e.creation_date DESC', $array, 'body');
186 $page->assign_by_ref('events', $array);
189 function handler_rss(&$page, $user = null
, $hash = null
)
191 require_once 'rss.inc.php';
193 $uid = init_rss('events/rss.tpl', $user, $hash);
195 $rss = XDB
::iterator(
196 'SELECT e.id, e.titre, e.texte, e.creation_date,
197 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
198 FROM auth_user_md5 AS u
199 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
200 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
201 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
202 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
203 AND peremption >= NOW()', $uid);
204 $page->assign('rss', $rss);
207 function handler_preview(&$page)
209 require_once('url_catcher.inc.php');
210 $page->changeTpl('events/preview.tpl', NO_SKIN
);
211 $texte = Get
::v('texte');
212 if (!is_utf8($texte)) {
213 $texte = utf8_encode($texte);
215 if (strpos($_SERVER['HTTP_REFERER'], 'admin') === false
) {
216 $texte = url_catcher(pl_entities($texte));
218 $titre = Get
::v('titre');
219 if (!is_utf8($titre)) {
220 $titre = utf8_encode($titre);
222 $page->assign('texte_html', $texte);
223 $page->assign('titre', $titre);
224 header('Content-Type: text/html; charset=utf-8');
227 function handler_ev_submit(&$page)
229 $page->changeTpl('events/submit.tpl');
230 $page->addJsLink('ajax.js');
232 require_once('wiki.inc.php');
233 wiki_require_page('Xorg.Annonce');
235 $titre = Post
::v('titre');
236 $texte = Post
::v('texte');
237 $promo_min = Post
::i('promo_min');
238 $promo_max = Post
::i('promo_max');
239 $peremption = Post
::i('peremption');
240 $valid_mesg = Post
::v('valid_mesg');
241 $action = Post
::v('action');
243 if (($promo_min > $promo_max && $promo_max != 0)||
244 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
245 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
247 $page->trig("L'intervalle de promotions n'est pas valide");
251 require_once('url_catcher.inc.php');
252 $texte_catch_url = url_catcher($texte);
254 $page->assign('titre', $titre);
255 $page->assign('texte', $texte);
256 $page->assign('texte_html', $texte_catch_url);
257 $page->assign('promo_min', $promo_min);
258 $page->assign('promo_max', $promo_max);
259 $page->assign('peremption', $peremption);
260 $page->assign('valid_mesg', $valid_mesg);
261 $page->assign('action', strtolower($action));
263 if ($action && (!trim($texte) ||
!trim($titre))) {
264 $page->trig("L'article doit avoir un titre et un contenu");
266 $texte = $texte_catch_url;
267 require_once 'validations.inc.php';
268 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
269 $peremption, $valid_mesg, S
::v('uid'));
271 $page->assign('ok', true
);
275 for ($i = 1 ; $i < 30 ; $i++
) {
276 $time = time() +
3600 * 24 * $i;
277 $p_stamp = date('Ymd', $time);
278 $year = date('Y', $time);
279 $month = date('m', $time);
280 $day = date('d', $time);
282 $select .= "<option value=\"$p_stamp\"";
283 if ($p_stamp == strtr($peremption, array("-" => ""))) {
284 $select .= " selected='selected'";
286 $select .= "> $day / $month / $year</option>\n";
288 $page->assign('select',$select);
291 function handler_tips(&$page, $tips = null
)
293 header('Content-Type: text/html; charset="UTF-8"');
294 $page->changeTpl('include/tips.tpl', NO_SKIN
);
295 $page->assign('tips', $this->get_tips($tips));
298 function handler_admin_tips(&$page, $action = 'list', $id = null
)
300 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
301 $page->assign('title', 'Gestion des Astuces');
302 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
303 $table_editor->describe('peremption', 'date de péremption', true
);
304 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
305 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
306 $table_editor->describe('titre', 'titre', true
);
307 $table_editor->describe('state', 'actif', true
);
308 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
309 $table_editor->describe('priorite', '0<=priorité<=255', true
);
310 $table_editor->list_on_edit(false
);
311 $table_editor->apply($page, $action, $id);
312 if (($action == 'edit' && !is_null($id)) ||
$action == 'update') {
313 $page->changeTpl('events/admin_tips.tpl');
317 function handler_admin_events(&$page, $action = 'list', $eid = null
)
319 $page->changeTpl('events/admin.tpl');
320 $page->addJsLink('ajax.js');
321 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
322 $page->register_modifier('hde', 'html_entity_decode');
324 $arch = $action == 'archives';
325 $page->assign('action', $action);
327 if (Post
::v('action') == "Proposer" && $eid) {
328 $promo_min = Post
::i('promo_min');
329 $promo_max = Post
::i('promo_max');
330 if ($promo_min > $promo_max ||
331 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
332 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
334 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
337 XDB
::execute('UPDATE evenements
338 SET creation_date = creation_date,
339 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
341 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
342 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
346 if ($action == 'edit') {
347 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
349 WHERE id={?}', $eid);
350 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
351 $page->assign('titre',$titre);
352 $page->assign('texte',$texte);
353 $page->assign('texte_html', pl_entity_decode($texte));
354 $page->assign('promo_min',$promo_min);
355 $page->assign('promo_max',$promo_max);
356 $page->assign('peremption',$peremption);
359 for ($i = 1 ; $i < 30 ; $i++
) {
360 $p_stamp=date("Ymd",time()+
3600*24*$i);
361 $year=substr($p_stamp,0,4);
362 $month=substr($p_stamp,4,2);
363 $day=substr($p_stamp,6,2);
365 $select .= "<option value=\"$p_stamp\""
366 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
367 . "> $day / $month / $year</option>\n";
369 $page->assign('select',$select);
373 XDB
::execute('DELETE from evenements
374 WHERE id = {?}', $eid);
378 XDB
::execute('UPDATE evenements
379 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
380 WHERE id = {?}', $eid);
384 XDB
::execute('UPDATE evenements
385 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
386 WHERE id = {?}', $eid);
387 $action = 'archives';
392 XDB
::execute('UPDATE evenements
393 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
394 WHERE id = {?}', $eid);
398 XDB
::execute('UPDATE evenements
399 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
400 WHERE id = {?}', $eid);
404 $pid = ($eid && $action == 'preview') ?
$eid : -1;
405 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
406 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
407 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
408 e.promo_min, e.promo_max,
409 FIND_IN_SET('valide', e.flags) AS fvalide,
410 FIND_IN_SET('archive', e.flags) AS farch,
411 u.promo, u.nom, u.prenom, a.alias AS forlife
413 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
414 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
415 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
416 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
417 $page->assign('evs', XDB
::iterator($sql));
419 $page->assign('arch', $arch);
423 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: