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 'rss' => $this->make_hook('rss', AUTH_PUBLIC
),
29 'send_bug' => $this->make_hook('bug', AUTH_COOKIE
),
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'),
36 'nl' => $this->make_hook('nl', AUTH_COOKIE
),
37 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE
),
38 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP
),
39 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP
, 'admin'),
40 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP
, 'admin'),
41 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP
, 'admin'),
45 function on_subscribe($forlife, $uid, $promo, $password)
47 require_once 'newsletter.inc.php';
51 function get_tips($exclude = null
)
53 $exclude = is_null($exclude) ?
'' : ' AND id != ' . $exclude . ' ';
54 $priority = rand(0, 510);
56 $priority = (int)($priority/2);
57 $res = XDB
::query("SELECT *
59 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
60 AND (promo_min = 0 OR promo_min <= {?})
61 AND (promo_max = 0 OR promo_max >= {?})
63 AND (state = 'active')
67 S
::i('promo'), S
::i('promo'), $priority);
68 } while ($priority && !$res->numRows());
69 if (!$res->numRows()) {
72 return $res->fetchOneAssoc();
75 function handler_bug(&$page)
77 $page->changeTpl('bug.tpl',SIMPLE
);
78 $page->addJsLink('close_on_esc.js');
79 if (Env
::has('send')) {
80 $page->assign('bug_sent',1);
81 $mymail = new PlMailer();
82 $mymail->setFrom('"'.S
::v('prenom').' '.S
::v('nom').'" <'.S
::v('bestalias').'@polytechnique.org>');
83 $mymail->addTo('support+platal@polytechnique.org');
84 $mymail->setSubject('Plat/al '.Env
::v('task_type').' : '.Env
::v('item_summary'));
85 $mymail->setTxtBody(Env
::v('detailed_desc'));
90 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
92 $page->changeTpl('login.tpl');
93 $page->addJsLink('ajax.js');
94 $page->assign('tips', $this->get_tips());
96 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
97 WHERE user_id={?}', S
::v('uid'));
98 list($date, $naissance) = $res->fetchOneRow();
100 // incitation à mettre à jour la fiche
102 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
103 substr($date, 0, 4));
104 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
105 // si fiche date de + de 400j;
106 $page->assign('fiche_incitation', $date);
109 // Souhaite bon anniversaire
111 if (substr($naissance, 5) == date('m-d')) {
112 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
115 // incitation à mettre une photo
117 $res = XDB
::query('SELECT COUNT(*) FROM photo
118 WHERE uid={?}', S
::v('uid'));
119 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
121 // Incitation à se géolocaliser
122 require_once 'geoloc.inc.php';
123 $res = localize_addresses(S
::v('uid', -1));
124 $page->assign('geoloc_incitation', count($res));
127 if (S
::has('core_rss_hash')) {
128 $page->setRssLink('Polytechnique.org :: News',
129 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
132 // cache les evenements lus et raffiche les evenements a relire
133 if ($action == 'read' && $eid) {
134 XDB
::execute('DELETE evenements_vus.*
135 FROM evenements_vus AS ev
136 INNER JOIN evenements AS e ON e.id = ev.evt_id
137 WHERE peremption < NOW()');
138 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
140 pl_redirect('events#'.$pound);
143 if ($action == 'unread' && $eid) {
144 XDB
::execute('DELETE FROM evenements_vus
145 WHERE evt_id = {?} AND user_id = {?}',
147 pl_redirect('events#newsid'.$eid);
150 // affichage des evenements
151 // annonces promos triées par présence d'une limite sur les promos
152 // puis par dates croissantes d'expiration
153 $promo = S
::v('promo');
154 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
156 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
157 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
158 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
159 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
160 AND (e.promo_min = 0 || e.promo_min <= {?})
161 AND (e.promo_max = 0 || e.promo_max >= {?})
162 AND ev.user_id IS NULL
163 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
164 $page->assign('evenement',
165 XDB
::iterator($sql, S
::v('uid'),
169 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
171 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
172 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
173 AND (e.promo_min = 0 || e.promo_min <= {?})
174 AND (e.promo_max = 0 || e.promo_max >= {?})
175 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
176 $page->assign('evenement_summary',
177 XDB
::iterator($sql, S
::v('uid'),
182 function handler_rss(&$page, $user = null
, $hash = null
)
184 require_once 'rss.inc.php';
186 $uid = init_rss('rss.tpl', $user, $hash);
188 $rss = XDB
::iterator(
189 'SELECT e.id, e.titre, e.texte, e.creation_date,
190 IF(u2.nom_usage = "", u2.nom, u2.nom_usage) AS nom, u2.prenom, u2.promo
191 FROM auth_user_md5 AS u
192 INNER JOIN evenements AS e ON ( (e.promo_min = 0 || e.promo_min <= u.promo)
193 AND (e.promo_max = 0 || e.promo_max >= u.promo) )
194 INNER JOIN auth_user_md5 AS u2 ON (u2.user_id = e.user_id)
195 WHERE u.user_id = {?} AND FIND_IN_SET(e.flags, "valide")
196 AND peremption >= NOW()', $uid);
197 $page->assign('rss', $rss);
200 function handler_ev_submit(&$page)
202 $page->changeTpl('evenements.tpl');
204 $titre = Post
::v('titre');
205 $texte = Post
::v('texte');
206 $promo_min = Post
::i('promo_min');
207 $promo_max = Post
::i('promo_max');
208 $peremption = Post
::i('peremption');
209 $valid_mesg = Post
::v('valid_mesg');
210 $action = Post
::v('action');
212 if (($promo_min > $promo_max && $promo_max != 0)||
213 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
214 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
216 $page->trig("L'intervalle de promotions n'est pas valide");
220 require_once('url_catcher.inc.php');
221 $texte_catch_url = url_catcher($texte);
223 $page->assign('titre', $titre);
224 $page->assign('texte', $texte);
225 $page->assign('texte_html', $texte_catch_url);
226 $page->assign('promo_min', $promo_min);
227 $page->assign('promo_max', $promo_max);
228 $page->assign('peremption', $peremption);
229 $page->assign('valid_mesg', $valid_mesg);
230 $page->assign('action', strtolower($action));
232 if ($action == 'Confirmer') {
233 $texte = $texte_catch_url;
234 require_once 'validations.inc.php';
235 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
236 $peremption, $valid_mesg, S
::v('uid'));
238 $page->assign('ok', true
);
242 for ($i = 1 ; $i < 30 ; $i++
) {
243 $time = time() +
3600 * 24 * $i;
244 $p_stamp = date('Ymd', $time);
245 $year = date('Y', $time);
246 $month = date('m', $time);
247 $day = date('d', $time);
249 $select .= "<option value=\"$p_stamp\"";
250 if ($p_stamp == strtr($peremption, array("-" => ""))) {
251 $select .= " selected='selected'";
253 $select .= "> $day / $month / $year</option>\n";
255 $page->assign('select',$select);
258 function handler_tips(&$page, $tips = null
)
260 $page->changeTpl('include/tips.tpl', NO_SKIN
);
261 $page->assign('tips', $this->get_tips($tips));
264 function handler_admin_tips(&$page, $action = 'list', $id = null
)
266 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
267 $page->assign('title', 'Gestion des Astuces');
268 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
269 $table_editor->describe('peremption', 'date de péremption', true
);
270 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
271 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
272 $table_editor->describe('titre', 'titre', true
);
273 $table_editor->describe('state', 'actif', true
);
274 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
275 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false
);
276 $table_editor->apply($page, $action, $id);
279 function handler_nl(&$page, $action = null
)
281 require_once 'newsletter.inc.php';
283 $page->changeTpl('newsletter/index.tpl');
284 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
287 case 'out': unsubscribe_nl(); break;
288 case 'in': subscribe_nl(); break;
292 $page->assign('nls', get_nl_state());
293 $page->assign('nl_list', get_nl_list());
296 function handler_nl_show(&$page, $nid = 'last')
298 $page->changeTpl('newsletter/show.tpl');
300 require_once 'newsletter.inc.php';
302 $nl = new NewsLetter($nid);
303 $page->assign_by_ref('nl', $nl);
305 if (Post
::has('send')) {
306 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
307 S
::v('bestalias'), S
::v('femme'),
308 S
::v('mail_fmt') != 'texte');
312 function handler_nl_submit(&$page)
314 $page->changeTpl('newsletter/submit.tpl');
316 require_once 'newsletter.inc.php';
318 if (Post
::has('see')) {
319 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
320 $page->assign('art', $art);
321 } elseif (Post
::has('valid')) {
322 require_once('validations.inc.php');
323 $art = new NLReq(S
::v('uid'), Post
::v('title'),
324 Post
::v('body'), Post
::v('append'));
326 $page->assign('submited', true
);
330 function handler_admin_events(&$page, $action = 'list', $eid = null
)
332 $page->changeTpl('admin/evenements.tpl');
333 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
334 $page->register_modifier('hde', 'html_entity_decode');
336 $arch = $action == 'archives';
337 $page->assign('action', $action);
339 if (Post
::v('action') == "Proposer" && $eid) {
340 $promo_min = Post
::i('promo_min');
341 $promo_max = Post
::i('promo_max');
342 if ($promo_min > $promo_max ||
343 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
344 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
346 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
349 XDB
::execute('UPDATE evenements
350 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
352 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
353 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
357 if ($action == 'edit') {
358 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
360 WHERE id={?}', $eid);
361 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
362 $page->assign('titre',$titre);
363 $page->assign('texte',$texte);
364 $page->assign('promo_min',$promo_min);
365 $page->assign('promo_max',$promo_max);
366 $page->assign('peremption',$peremption);
369 for ($i = 1 ; $i < 30 ; $i++
) {
370 $p_stamp=date("Ymd",time()+
3600*24*$i);
371 $year=substr($p_stamp,0,4);
372 $month=substr($p_stamp,4,2);
373 $day=substr($p_stamp,6,2);
375 $select .= "<option value=\"$p_stamp\""
376 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
377 . "> $day / $month / $year</option>\n";
379 $page->assign('select',$select);
383 XDB
::execute('DELETE from evenements
384 WHERE id = {?}', $eid);
388 XDB
::execute('UPDATE evenements
389 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
390 WHERE id = {?}', $eid);
394 XDB
::execute('UPDATE evenements
395 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
396 WHERE id = {?}', $eid);
397 $action = 'archives';
402 XDB
::execute('UPDATE evenements
403 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
404 WHERE id = {?}', $eid);
408 XDB
::execute('UPDATE evenements
409 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
410 WHERE id = {?}', $eid);
414 $pid = ($eid && $action == 'preview') ?
$eid : -1;
415 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
416 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
417 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
418 e.promo_min, e.promo_max,
419 FIND_IN_SET('valide', e.flags) AS fvalide,
420 FIND_IN_SET('archive', e.flags) AS farch,
421 u.promo, u.nom, u.prenom, a.alias AS forlife
423 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
424 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
425 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
426 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
427 $page->assign('evs', XDB
::iterator($sql));
429 $page->assign('arch', $arch);
432 function handler_admin_nl(&$page, $new = false
) {
433 $page->changeTpl('newsletter/admin.tpl');
434 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
435 require_once("newsletter.inc.php");
439 pl_redirect("admin/newsletter");
442 $page->assign('nl_list', get_nl_slist());
445 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
446 $page->changeTpl('newsletter/edit.tpl');
447 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
448 require_once("newsletter.inc.php");
450 $nl = new NewsLetter($nid);
452 if($action == 'delete') {
453 $nl->delArticle($aid);
454 pl_redirect("admin/newsletter/edit/$nid");
457 if($aid == 'update') {
458 $nl->_title
= Post
::v('title');
459 $nl->_title_mail
= Post
::v('title_mail');
460 $nl->_date
= Post
::v('date');
461 $nl->_head
= Post
::v('head');
462 $nl->_shortname
= strlen(Post
::v('shortname')) ? Post
::v('shortname') : null
;
463 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname
) && !is_numeric($nl->_shortname
)) {
466 $page->trig('Le nom de la NL n\'est pas valide');
467 pl_redirect('admin/newsletter/edit/' . $nl->_id
);
471 if(Post
::v('save')) {
472 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
473 $aid, Post
::v('cid'), Post
::v('pos'));
474 $nl->saveArticle($art);
475 pl_redirect("admin/newsletter/edit/$nid");
478 if($action == 'edit' && $aid != 'update') {
480 if(Post
::has('title')) {
481 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
482 $eaid, Post
::v('cid'), Post
::v('pos'));
484 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
486 $page->assign('art', $art);
489 $page->assign_by_ref('nl',$nl);
491 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
492 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
493 $page->assign('title', 'Gestion des catégories de la newsletter');
494 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
495 $table_editor->describe('titre','intitulé',true
);
496 $table_editor->describe('pos','position',true
);
497 $table_editor->apply($page, $action, $id);