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
, 'user', NO_HTTPS
),
29 'events/preview' => $this->make_hook('preview', AUTH_PUBLIC
, 'user', NO_AUTH
),
30 'events/photo' => $this->make_hook('photo', AUTH_PUBLIC
),
31 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
32 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
34 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE
, 'user', NO_AUTH
),
35 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP
, 'admin'),
39 private function get_tips($exclude = null
)
42 // Add a new special tip when changing plat/al version
43 if ($globals->version
!= S
::v('last_version') && is_null($exclude)) {
44 XDB
::execute('UPDATE auth_user_quick
45 SET last_version = {?}
47 $globals->version
, S
::i('uid'));
48 return array('id' => 0,
49 'titre' => 'Bienvenue sur la nouvelle version du site !',
50 'text' => 'Le site a été mis à jour depuis ta dernière visite vers la version ' . $globals->version
51 . '.<br /> Nous t\'invitons à <a href="review">faire un tour d\'horizon des '
52 . 'nouveautés</a>.<br /><br />'
53 . 'Tu peux également retrouver ces informations sur <a href="banana/xorg.m4x.innovation">'
54 . 'les fora</a>, ou sur <a href="changelog">la liste exhaustive des modifications</a>',
62 $exclude = is_null($exclude) ?
'' : ' AND id != ' . $exclude . ' ';
63 $priority = rand(0, 510);
65 $priority = (int)($priority/2);
66 $res = XDB
::query("SELECT *
68 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
69 AND (promo_min = 0 OR promo_min <= {?})
70 AND (promo_max = 0 OR promo_max >= {?})
72 AND (state = 'active')
76 S
::i('promo'), S
::i('promo'), $priority);
77 } while ($priority && !$res->numRows());
78 if (!$res->numRows()) {
81 return $res->fetchOneAssoc();
84 private function get_events($where, $order, array &$array, $name)
86 // affichage des evenements
87 // annonces promos triées par présence d'une limite sur les promos
88 // puis par dates croissantes d'expiration
89 $promo = S
::v('promo');
91 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
93 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
94 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
95 AND (e.promo_min = 0 || e.promo_min <= {?})
96 AND (e.promo_max = 0 || e.promo_max >= {?})
99 $sum = XDB
::iterator($sql, $uid, $promo, $promo);
100 if (!$sum->total()) {
103 $sql = "SELECT e.id,e.titre,e.texte,e.post_id,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife,
104 p.x, p.y, p.attach IS NOT NULL AS img, FIND_IN_SET('wiki', e.flags) AS wiki
106 LEFT JOIN evenements_photo AS p ON (e.id = p.eid)
107 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
108 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
109 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
110 WHERE FIND_IN_SET('valide', e.flags) AND peremption >= NOW()
111 AND (e.promo_min = 0 || e.promo_min <= {?})
112 AND (e.promo_max = 0 || e.promo_max >= {?})
113 AND ev.user_id IS NULL
116 $evt = XDB
::iterator($sql, $uid, $promo, $promo);
117 $array[$name] = array('events' => $evt, 'summary' => $sum);
121 private function upload_image(PlatalPage
&$page, PlUpload
&$upload)
123 if (@!$_FILES['image']['tmp_name'] && !Env
::v('image_url')) {
126 if (!$upload->upload($_FILES['image']) && !$upload->download(Env
::v('image_url'))) {
127 $page->trig('Impossible de télécharger l\'image');
129 } elseif (!$upload->isType('image')) {
130 $page->trig('Le fichier n\'est pas une image valide au format JPEG, GIF ou PNG');
133 } elseif (!$upload->resizeImage(200, 300, 100, 100, 32284)) {
134 $page->trig('Impossible de retraiter l\'image');
140 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
142 $page->changeTpl('events/index.tpl');
143 $page->addJsLink('ajax.js');
144 $page->assign('tips', $this->get_tips());
146 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
147 WHERE user_id={?}', S
::v('uid'));
148 list($date, $naissance) = $res->fetchOneRow();
150 // incitation à mettre à jour la fiche
152 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
153 substr($date, 0, 4));
154 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
155 // si fiche date de + de 400j;
156 $page->assign('fiche_incitation', $date);
159 // Souhaite bon anniversaire
161 if (substr($naissance, 5) == date('m-d')) {
162 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
165 // incitation à mettre une photo
167 $res = XDB
::query('SELECT COUNT(*) FROM photo
168 WHERE uid={?}', S
::v('uid'));
169 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
171 // Incitation à se géolocaliser
172 require_once 'geoloc.inc.php';
173 $res = localize_addresses(S
::v('uid', -1));
174 $page->assign('geoloc_incitation', count($res));
177 if (S
::has('core_rss_hash')) {
178 $page->setRssLink('Polytechnique.org :: News',
179 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
182 // cache les evenements lus et raffiche les evenements a relire
183 if ($action == 'read' && $eid) {
184 XDB
::execute('DELETE evenements_vus.*
185 FROM evenements_vus AS ev
186 INNER JOIN evenements AS e ON e.id = ev.evt_id
187 WHERE peremption < NOW()');
188 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
190 pl_redirect('events#'.$pound);
193 if ($action == 'unread' && $eid) {
194 XDB
::execute('DELETE FROM evenements_vus
195 WHERE evt_id = {?} AND user_id = {?}',
197 pl_redirect('events#newsid'.$eid);
201 $this->get_events('FIND_IN_SET(\'important\', e.flags)', 'e.creation_date DESC', $array, 'important');
202 $this->get_events('e.creation_date > DATE_SUB(CURDATE(), INTERVAL 2 DAY)
203 AND NOT FIND_IN_SET(\'important\', e.flags)',
204 'e.creation_date DESC', $array, 'news');
205 $this->get_events('e.peremption < DATE_ADD(CURDATE(), INTERVAL 2 DAY)
206 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
207 AND NOT FIND_IN_SET(\'important\', e.flags)',
208 'e.peremption, e.creation_date DESC', $array, 'end');
209 $this->get_events('e.peremption >= DATE_ADD(CURDATE(), INTERVAL 2 DAY)
210 AND e.creation_date <= DATE_SUB(CURDATE(), INTERVAL 2 DAY)
211 AND NOT FIND_IN_SET(\'important\', e.flags)',
212 'e.peremption, e.creation_date DESC', $array, 'body');
213 $page->assign_by_ref('events', $array);
216 function handler_photo(&$page, $eid = null
, $valid = null
)
218 if ($eid && $eid != 'valid') {
219 $res = XDB
::query("SELECT * FROM evenements_photo WHERE eid = {?}", $eid);
220 if ($res->numRows()) {
221 $photo = $res->fetchOneAssoc();
222 header('Content-Type: image/' . $photo['attachmime']);
223 echo $photo['attach'];
226 } elseif ($eid == 'valid') {
227 require_once 'validations.inc.php';
228 $valid = Validate
::get_request_by_id($valid);
229 if ($valid && $valid->img
) {
230 header('Content-Type: image/' . $valid->imgtype
);
235 $upload = new PlUpload(S
::v('forlife'), 'event');
236 if ($upload->exists() && $upload->isType('image')) {
237 header('Content-Type: ' . $upload->contentType());
238 echo $upload->getContents();
243 header('Content-Type: image/png');
244 echo file_get_contents($globals->spoolroot
. '/htdocs/images/logo.png');
248 function handler_rss(&$page, $user = null
, $hash = null
)
250 require_once 'rss.inc.php';
252 $uid = init_rss('events/rss.tpl', $user, $hash);
254 $rss = XDB
::iterator(
255 'SELECT e.id, e.titre, e.texte, e.creation_date, e.post_id, p.attachmime IS NOT NULL AS photo,
256 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
257 FROM auth_user_md5 AS u
258 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
259 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
260 LEFT JOIN evenements_photo AS p ON (p.eid = e.id)
261 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
262 WHERE u.user_id = {?} AND FIND_IN_SET("valide", e.flags)
263 AND peremption >= NOW()', $uid);
264 $page->assign('rss', $rss);
267 function handler_preview(&$page)
269 $page->changeTpl('events/preview.tpl', NO_SKIN
);
270 $texte = Get
::v('texte');
271 if (!is_utf8($texte)) {
272 $texte = utf8_encode($texte);
274 $titre = Get
::v('titre');
275 if (!is_utf8($titre)) {
276 $titre = utf8_encode($titre);
278 $page->assign('texte', $texte);
279 $page->assign('titre', $titre);
280 header('Content-Type: text/html; charset=utf-8');
283 function handler_ev_submit(&$page)
285 $page->changeTpl('events/submit.tpl');
286 $page->addJsLink('ajax.js');
288 require_once('wiki.inc.php');
289 wiki_require_page('Xorg.Annonce');
291 $titre = Post
::v('titre');
292 $texte = Post
::v('texte');
293 $promo_min = Post
::i('promo_min');
294 $promo_max = Post
::i('promo_max');
295 $peremption = Post
::i('peremption');
296 $valid_mesg = Post
::v('valid_mesg');
297 $action = Post
::v('action');
298 $upload = new PlUpload(S
::v('forlife'), 'event');
299 $this->upload_image($page, $upload);
301 if (($promo_min > $promo_max && $promo_max != 0)||
302 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
303 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
305 $page->trig("L'intervalle de promotions n'est pas valide");
309 $page->assign('titre', $titre);
310 $page->assign('texte', $texte);
311 $page->assign('promo_min', $promo_min);
312 $page->assign('promo_max', $promo_max);
313 $page->assign('peremption', $peremption);
314 $page->assign('valid_mesg', $valid_mesg);
315 $page->assign('action', strtolower($action));
316 $page->assign_by_ref('upload', $upload);
318 if ($action == 'Supprimer l\'image') {
320 $page->assign('action', false
);
321 } elseif ($action && (!trim($texte) ||
!trim($titre))) {
322 $page->trig("L'article doit avoir un titre et un contenu");
324 require_once 'validations.inc.php';
325 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
326 $peremption, $valid_mesg, S
::v('uid'), $upload);
328 $page->assign('ok', true
);
329 } elseif (!Env
::v('preview')) {
334 function handler_tips(&$page, $tips = null
)
336 header('Content-Type: text/html; charset="UTF-8"');
337 $page->changeTpl('include/tips.tpl', NO_SKIN
);
338 $page->assign('tips', $this->get_tips($tips));
341 function handler_admin_tips(&$page, $action = 'list', $id = null
)
343 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
344 $page->assign('title', 'Gestion des Astuces');
345 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
346 $table_editor->describe('peremption', 'date de péremption', true
);
347 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
348 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
349 $table_editor->describe('titre', 'titre', true
);
350 $table_editor->describe('state', 'actif', true
);
351 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
352 $table_editor->describe('priorite', '0<=priorité<=255', true
);
353 $table_editor->list_on_edit(false
);
354 $table_editor->apply($page, $action, $id);
355 if (($action == 'edit' && !is_null($id)) ||
$action == 'update') {
356 $page->changeTpl('events/admin_tips.tpl');
360 function handler_admin_events(&$page, $action = 'list', $eid = null
)
362 $page->changeTpl('events/admin.tpl');
363 $page->addJsLink('ajax.js');
364 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
365 $page->register_modifier('hde', 'html_entity_decode');
367 $arch = $action == 'archives';
368 $page->assign('action', $action);
370 $upload = new PlUpload(S
::v('forlife'), 'event');
371 if ((Env
::has('preview') || Post
::v('action') == "Proposer") && $eid) {
373 $this->upload_image($page, $upload);
376 if (Post
::v('action') == 'Pas d\'image' && $eid) {
378 XDB
::execute("DELETE FROM evenements_photo WHERE eid = {?}", $eid);
380 } elseif (Post
::v('action') == 'Supprimer l\'image' && $eid) {
383 } elseif (Post
::v('action') == "Proposer" && $eid) {
384 $promo_min = Post
::i('promo_min');
385 $promo_max = Post
::i('promo_max');
386 if (($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
387 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020 ||
$promo_max < $promo_min)))
389 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
392 $res = XDB
::query('SELECT flags FROM evenements WHERE id = {?}', $eid);
393 $flags = new FlagSet($res->fetchOneCell());
394 $flags->addFlag('wiki');
395 if (Post
::v('important')) {
396 $flags->addFlag('important');
398 $flags->rmFlag('important');
401 XDB
::execute('UPDATE evenements
402 SET creation_date = creation_date,
403 titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?},
406 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
407 Post
::v('promo_min'), Post
::v('promo_max'),
408 $flags->flags(), $eid);
409 if ($upload->exists() && list($x, $y, $type) = $upload->imageInfo()) {
410 XDB
::execute('REPLACE INTO evenements_photo
411 SET eid = {?}, attachmime = {?}, x = {?}, y = {?}, attach = {?}',
412 $eid, $type, $x, $y, $upload->getContents());
418 if ($action == 'edit') {
419 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max, FIND_IN_SET(\'important\', flags),
422 LEFT JOIN evenements_photo AS p ON(e.id = p.eid)
423 WHERE id={?}', $eid);
424 list($titre, $texte, $peremption, $promo_min, $promo_max, $important, $img) = $res->fetchOneRow();
425 $page->assign('titre',$titre);
426 $page->assign('texte',$texte);
427 $page->assign('promo_min',$promo_min);
428 $page->assign('promo_max',$promo_max);
429 $page->assign('peremption',$peremption);
430 $page->assign('important', $important);
431 $page->assign('eid', $eid);
432 $page->assign('img', $img);
433 $page->assign_by_ref('upload', $upload);
436 for ($i = 1 ; $i < 30 ; $i++
) {
437 $p_stamp=date("Ymd",time()+
3600*24*$i);
438 $year=substr($p_stamp,0,4);
439 $month=substr($p_stamp,4,2);
440 $day=substr($p_stamp,6,2);
442 $select .= "<option value=\"$p_stamp\""
443 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
444 . "> $day / $month / $year</option>\n";
446 $page->assign('select',$select);
450 XDB
::execute('DELETE from evenements
451 WHERE id = {?}', $eid);
455 XDB
::execute('UPDATE evenements
456 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
457 WHERE id = {?}', $eid);
461 XDB
::execute('UPDATE evenements
462 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
463 WHERE id = {?}', $eid);
464 $action = 'archives';
469 XDB
::execute('UPDATE evenements
470 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
471 WHERE id = {?}', $eid);
475 XDB
::execute('UPDATE evenements
476 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
477 WHERE id = {?}', $eid);
481 $pid = ($eid && $action == 'preview') ?
$eid : -1;
482 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
483 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
484 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
485 e.promo_min, e.promo_max,
486 FIND_IN_SET('valide', e.flags) AS fvalide,
487 FIND_IN_SET('archive', e.flags) AS farch,
488 u.promo, u.nom, u.prenom, a.alias AS forlife,
489 FIND_IN_SET('wiki', flags) AS wiki
491 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
492 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
493 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
494 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
495 $page->assign('evs', XDB
::iterator($sql));
497 $page->assign('arch', $arch);
498 $page->assign('admin_evts', true
);
502 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: