2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 'send_bug' => $this->make_hook('bug', AUTH_COOKIE
),
29 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
32 'nl' => $this->make_hook('nl', AUTH_COOKIE
),
33 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE
),
34 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE
),
35 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP
, 'admin'),
36 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP
, 'admin'),
37 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP
, 'admin'),
41 function on_subscribe($forlife, $uid, $promo, $password)
43 require_once 'newsletter.inc.php';
47 function handler_bug(&$page)
49 $this->handler_ev($page);
50 $page->assign('bug', 1);
53 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
55 $page->changeTpl('login.tpl');
57 // donne la derniere date de session
58 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S
::i('lastlogin')));
60 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
61 WHERE user_id={?}', S
::v('uid'));
62 list($date, $naissance) = $res->fetchOneRow();
64 // incitation à mettre à jour la fiche
66 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
68 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
69 // si fiche date de + de 400j;
70 $page->assign('fiche_incitation', $date);
73 // Souhaite bon anniversaire
75 if (substr($naissance, 5) == date('m-d')) {
76 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
79 // incitation à mettre une photo
81 $res = XDB
::query('SELECT COUNT(*) FROM photo
82 WHERE uid={?}', S
::v('uid'));
83 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
85 // Incitation à se géolocaliser
86 require_once 'geoloc.inc.php';
87 $res = localize_addresses(S
::v('uid', -1));
88 $page->assign('geoloc_incitation', count($res));
90 // affichage de la boîte avec quelques liens
91 /* Bandeau de publicité sur la page de login */
93 'password' => 'Changer mon mot de passe' ,
94 'Docs/Dons' => 'Faire un don à l\'association Polytechnique.org'
97 // Liens apparaissant de façon aléatoire
99 'nl/show' => 'Afficher la dernière newsletter' ,
100 'http://www.polytechnique.net/login' => 'Vers les autres sites polytechniciens' ,
101 "trombi/{$_SESSION["promo
"]}" => "Voir le trombi de ma promo" ,
102 'banana' => 'Un petit tour du côté des forums !!'
105 $choix = array_rand($pub_rnd, 2);
106 foreach ($choix as $url) {
107 $publicite[$url] = $pub_rnd[$url] ;
109 $page->assign('publicite', array_chunk($publicite, 2, true
));
112 if (S
::has('core_rss_hash')) {
113 $page->setRssLink('Polytechnique.org :: News',
114 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
117 // cache les evenements lus et raffiche les evenements a relire
118 if ($action == 'read' && $eid) {
119 XDB
::execute('DELETE evenements_vus.*
120 FROM evenements_vus AS ev
121 INNER JOIN evenements AS e ON e.id = ev.evt_id
122 WHERE peremption < NOW()');
123 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
125 pl_redirect('events#'.$pound);
128 if ($action == 'unread' && $eid) {
129 XDB
::execute('DELETE FROM evenements_vus
130 WHERE evt_id = {?} AND user_id = {?}',
132 pl_redirect('events#newsid'.$eid);
135 // affichage des evenements
136 // annonces promos triées par présence d'une limite sur les promos
137 // puis par dates croissantes d'expiration
138 $promo = S
::v('promo');
139 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
141 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
142 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
143 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
144 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
145 AND (e.promo_min = 0 || e.promo_min <= {?})
146 AND (e.promo_max = 0 || e.promo_max >= {?})
147 AND ev.user_id IS NULL
148 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
149 $page->assign('evenement',
150 XDB
::iterator($sql, S
::v('uid'),
154 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
156 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
157 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
158 AND (e.promo_min = 0 || e.promo_min <= {?})
159 AND (e.promo_max = 0 || e.promo_max >= {?})
160 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
161 $page->assign('evenement_summary',
162 XDB
::iterator($sql, S
::v('uid'),
167 function handler_ev_submit(&$page)
169 $page->changeTpl('evenements.tpl');
171 $titre = Post
::v('titre');
172 $texte = Post
::v('texte');
173 $promo_min = Post
::i('promo_min');
174 $promo_max = Post
::i('promo_max');
175 $peremption = Post
::i('peremption');
176 $valid_mesg = Post
::v('valid_mesg');
177 $action = Post
::v('action');
179 if ($promo_min > $promo_max ||
180 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
181 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
183 $page->trig("L'intervalle de promotions n'est pas valide");
187 require_once('url_catcher.inc.php');
188 $texte_catch_url = url_catcher($texte);
190 $page->assign('titre', $titre);
191 $page->assign('texte', $texte);
192 $page->assign('texte_html', $texte_catch_url);
193 $page->assign('promo_min', $promo_min);
194 $page->assign('promo_max', $promo_max);
195 $page->assign('peremption', $peremption);
196 $page->assign('valid_mesg', $valid_mesg);
197 $page->assign('action', strtolower($action));
199 if ($action == 'Confirmer') {
200 $texte = $texte_catch_url;
201 require_once 'validations.inc.php';
202 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
203 $peremption, $valid_mesg, S
::v('uid'));
205 $page->assign('ok', true
);
209 for ($i = 1 ; $i < 30 ; $i++
) {
210 $time = time() +
3600 * 24 * $i;
211 $p_stamp = date('Ymd', $time);
212 $year = date('Y', $time);
213 $month = date('m', $time);
214 $day = date('d', $time);
216 $select .= "<option value=\"$p_stamp\"";
217 if ($p_stamp == strtr($peremption, array("-" => ""))) {
218 $select .= " selected='selected'";
220 $select .= "> $day / $month / $year</option>\n";
222 $page->assign('select',$select);
225 function handler_nl(&$page, $action = null
)
227 require_once 'newsletter.inc.php';
229 $page->changeTpl('newsletter/index.tpl');
230 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
233 case 'out': unsubscribe_nl(); break;
234 case 'in': subscribe_nl(); break;
238 $page->assign('nls', get_nl_state());
239 $page->assign('nl_list', get_nl_list());
242 function handler_nl_show(&$page, $nid = 'last')
244 $page->changeTpl('newsletter/show.tpl');
246 require_once 'newsletter.inc.php';
248 $nl = new NewsLetter($nid);
249 $page->assign_by_ref('nl', $nl);
251 if (Post
::has('send')) {
252 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
253 S
::v('bestalias'), S
::v('femme'),
254 S
::v('mail_fmt') != 'texte');
258 function handler_nl_submit(&$page)
260 $page->changeTpl('newsletter/submit.tpl');
262 require_once 'newsletter.inc.php';
264 if (Post
::has('see')) {
265 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
266 $page->assign('art', $art);
267 } elseif (Post
::has('valid')) {
268 require_once('validations.inc.php');
269 $art = new NLReq(S
::v('uid'), Post
::v('title'),
270 Post
::v('body'), Post
::v('append'));
272 $page->assign('submited', true
);
276 function handler_admin_events(&$page, $action = 'list', $eid = null
)
278 $page->changeTpl('admin/evenements.tpl');
279 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
280 $page->register_modifier('hde', 'html_entity_decode');
282 $arch = $action == 'archives';
283 $page->assign('action', $action);
285 if (Post
::v('action') == "Proposer" && $eid) {
286 $promo_min = Post
::i('promo_min');
287 $promo_max = Post
::i('promo_max');
288 if ($promo_min > $promo_max ||
289 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
290 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
292 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
295 XDB
::execute('UPDATE evenements
296 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
298 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
299 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
303 if ($action == 'edit') {
304 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
306 WHERE id={?}', $eid);
307 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
308 $page->assign('titre',$titre);
309 $page->assign('texte',$texte);
310 $page->assign('promo_min',$promo_min);
311 $page->assign('promo_max',$promo_max);
312 $page->assign('peremption',$peremption);
315 for ($i = 1 ; $i < 30 ; $i++
) {
316 $p_stamp=date("Ymd",time()+
3600*24*$i);
317 $year=substr($p_stamp,0,4);
318 $month=substr($p_stamp,4,2);
319 $day=substr($p_stamp,6,2);
321 $select .= "<option value=\"$p_stamp\""
322 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
323 . "> $day / $month / $year</option>\n";
325 $page->assign('select',$select);
329 XDB
::execute('DELETE from evenements
330 WHERE id = {?}', $eid);
334 XDB
::execute('UPDATE evenements
335 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
336 WHERE id = {?}', $eid);
340 XDB
::execute('UPDATE evenements
341 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
342 WHERE id = {?}', $eid);
343 $action = 'archives';
348 XDB
::execute('UPDATE evenements
349 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
350 WHERE id = {?}', $eid);
354 XDB
::execute('UPDATE evenements
355 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
356 WHERE id = {?}', $eid);
360 $pid = ($eid && $action == 'preview') ?
$eid : -1;
361 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
362 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
363 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
364 e.promo_min, e.promo_max,
365 FIND_IN_SET('valide', e.flags) AS fvalide,
366 FIND_IN_SET('archive', e.flags) AS farch,
367 u.promo, u.nom, u.prenom, a.alias AS forlife
369 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
370 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
371 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
372 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
373 $page->assign('evs', XDB
::iterator($sql));
375 $page->assign('arch', $arch);
378 function handler_admin_nl(&$page, $new = false
) {
379 $page->changeTpl('newsletter/admin.tpl');
380 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
381 require_once("newsletter.inc.php");
385 pl_redirect("admin/newsletter");
388 $page->assign('nl_list', get_nl_slist());
391 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
392 $page->changeTpl('newsletter/edit.tpl');
393 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
394 require_once("newsletter.inc.php");
396 $nl = new NewsLetter($nid);
398 if($action == 'delete') {
399 $nl->delArticle($aid);
400 pl_redirect("admin/newsletter/edit/$nid");
403 if($aid == 'update') {
404 $nl->_title
= Post
::v('title');
405 $nl->_title_mail
= Post
::v('title_mail');
406 $nl->_date
= Post
::v('date');
407 $nl->_head
= Post
::v('head');
408 $nl->_shortname
= strlen(Post
::v('shortname')) ? Post
::v('shortname') : null
;
409 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname
) && !is_numeric($nl->_shortname
)) {
412 $page->trig('Le nom de la NL n\'est pas valide');
413 pl_redirect('admin/newsletter/edit/' . $nl->_id
);
417 if(Post
::v('save')) {
418 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
419 $aid, Post
::v('cid'), Post
::v('pos'));
420 $nl->saveArticle($art);
421 pl_redirect("admin/newsletter/edit/$nid");
424 if($action == 'edit' && $aid != 'update') {
426 if(Post
::has('title')) {
427 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
428 $eaid, Post
::v('cid'), Post
::v('pos'));
430 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
432 $page->assign('art', $art);
435 $page->assign_by_ref('nl',$nl);
437 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
438 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
439 $page->assign('title', 'Gestion des catégories de la newsletter');
440 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
441 $table_editor->describe('titre','intitulé',true
);
442 $table_editor->describe('pos','position',true
);
443 $table_editor->apply($page, $action, $id);