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 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE
, '', NO_AUTH
),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP
, 'admin'),
35 'nl' => $this->make_hook('nl', AUTH_COOKIE
),
36 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE
),
37 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE
),
38 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP
, 'admin'),
39 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP
, 'admin'),
40 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP
, 'admin'),
44 function on_subscribe($forlife, $uid, $promo, $password)
46 require_once 'newsletter.inc.php';
50 function get_tips($exclude = null
)
52 $exclude = is_null($exclude) ?
'' : ' AND id != ' . $exclude . ' ';
53 $priority = rand(0, 510);
55 $priority = (int)($priority/2);
56 $res = XDB
::query("SELECT *
58 WHERE (peremption = '0000-00-00' OR peremption > CURDATE())
59 AND (promo_min = 0 OR promo_min <= {?})
60 AND (promo_max = 0 OR promo_max >= {?})
65 S
::i('promo'), S
::i('promo'), $priority);
66 } while ($priority && !$res->numRows());
67 if (!$res->numRows()) {
70 return $res->fetchOneAssoc();
73 function handler_bug(&$page)
75 $this->handler_ev($page);
76 $page->assign('bug', 1);
79 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
81 $page->changeTpl('login.tpl');
82 $page->addJsLink('ajax.js');
83 $page->assign('tips', $this->get_tips());
85 // donne la derniere date de session
86 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S
::i('lastlogin')));
88 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
89 WHERE user_id={?}', S
::v('uid'));
90 list($date, $naissance) = $res->fetchOneRow();
92 // incitation à mettre à jour la fiche
94 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
96 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
97 // si fiche date de + de 400j;
98 $page->assign('fiche_incitation', $date);
101 // Souhaite bon anniversaire
103 if (substr($naissance, 5) == date('m-d')) {
104 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
107 // incitation à mettre une photo
109 $res = XDB
::query('SELECT COUNT(*) FROM photo
110 WHERE uid={?}', S
::v('uid'));
111 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
113 // Incitation à se géolocaliser
114 require_once 'geoloc.inc.php';
115 $res = localize_addresses(S
::v('uid', -1));
116 $page->assign('geoloc_incitation', count($res));
119 if (S
::has('core_rss_hash')) {
120 $page->setRssLink('Polytechnique.org :: News',
121 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
124 // cache les evenements lus et raffiche les evenements a relire
125 if ($action == 'read' && $eid) {
126 XDB
::execute('DELETE evenements_vus.*
127 FROM evenements_vus AS ev
128 INNER JOIN evenements AS e ON e.id = ev.evt_id
129 WHERE peremption < NOW()');
130 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
132 pl_redirect('events#'.$pound);
135 if ($action == 'unread' && $eid) {
136 XDB
::execute('DELETE FROM evenements_vus
137 WHERE evt_id = {?} AND user_id = {?}',
139 pl_redirect('events#newsid'.$eid);
142 // affichage des evenements
143 // annonces promos triées par présence d'une limite sur les promos
144 // puis par dates croissantes d'expiration
145 $promo = S
::v('promo');
146 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
148 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
149 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
150 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
151 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
152 AND (e.promo_min = 0 || e.promo_min <= {?})
153 AND (e.promo_max = 0 || e.promo_max >= {?})
154 AND ev.user_id IS NULL
155 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
156 $page->assign('evenement',
157 XDB
::iterator($sql, S
::v('uid'),
161 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
163 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
164 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
165 AND (e.promo_min = 0 || e.promo_min <= {?})
166 AND (e.promo_max = 0 || e.promo_max >= {?})
167 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
168 $page->assign('evenement_summary',
169 XDB
::iterator($sql, S
::v('uid'),
174 function handler_ev_submit(&$page)
176 $page->changeTpl('evenements.tpl');
178 $titre = Post
::v('titre');
179 $texte = Post
::v('texte');
180 $promo_min = Post
::i('promo_min');
181 $promo_max = Post
::i('promo_max');
182 $peremption = Post
::i('peremption');
183 $valid_mesg = Post
::v('valid_mesg');
184 $action = Post
::v('action');
186 if ($promo_min > $promo_max ||
187 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
188 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
190 $page->trig("L'intervalle de promotions n'est pas valide");
194 require_once('url_catcher.inc.php');
195 $texte_catch_url = url_catcher($texte);
197 $page->assign('titre', $titre);
198 $page->assign('texte', $texte);
199 $page->assign('texte_html', $texte_catch_url);
200 $page->assign('promo_min', $promo_min);
201 $page->assign('promo_max', $promo_max);
202 $page->assign('peremption', $peremption);
203 $page->assign('valid_mesg', $valid_mesg);
204 $page->assign('action', strtolower($action));
206 if ($action == 'Confirmer') {
207 $texte = $texte_catch_url;
208 require_once 'validations.inc.php';
209 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
210 $peremption, $valid_mesg, S
::v('uid'));
212 $page->assign('ok', true
);
216 for ($i = 1 ; $i < 30 ; $i++
) {
217 $time = time() +
3600 * 24 * $i;
218 $p_stamp = date('Ymd', $time);
219 $year = date('Y', $time);
220 $month = date('m', $time);
221 $day = date('d', $time);
223 $select .= "<option value=\"$p_stamp\"";
224 if ($p_stamp == strtr($peremption, array("-" => ""))) {
225 $select .= " selected='selected'";
227 $select .= "> $day / $month / $year</option>\n";
229 $page->assign('select',$select);
232 function handler_tips(&$page, $tips = null
)
234 $page->changeTpl('include/tips.tpl', NO_SKIN
);
235 $page->assign('tips', $this->get_tips($tips));
238 function handler_admin_tips(&$page, $action = 'list', $id = null
)
240 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
241 $page->assign('title', 'Gestion des Astuces');
242 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
243 $table_editor->describe('peremption', 'date de péremption', true
);
244 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
245 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
246 $table_editor->describe('titre', 'titre', true
);
247 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
248 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false
);
249 $table_editor->apply($page, $action, $id);
252 function handler_nl(&$page, $action = null
)
254 require_once 'newsletter.inc.php';
256 $page->changeTpl('newsletter/index.tpl');
257 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
260 case 'out': unsubscribe_nl(); break;
261 case 'in': subscribe_nl(); break;
265 $page->assign('nls', get_nl_state());
266 $page->assign('nl_list', get_nl_list());
269 function handler_nl_show(&$page, $nid = 'last')
271 $page->changeTpl('newsletter/show.tpl');
273 require_once 'newsletter.inc.php';
275 $nl = new NewsLetter($nid);
276 $page->assign_by_ref('nl', $nl);
278 if (Post
::has('send')) {
279 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
280 S
::v('bestalias'), S
::v('femme'),
281 S
::v('mail_fmt') != 'texte');
285 function handler_nl_submit(&$page)
287 $page->changeTpl('newsletter/submit.tpl');
289 require_once 'newsletter.inc.php';
291 if (Post
::has('see')) {
292 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
293 $page->assign('art', $art);
294 } elseif (Post
::has('valid')) {
295 require_once('validations.inc.php');
296 $art = new NLReq(S
::v('uid'), Post
::v('title'),
297 Post
::v('body'), Post
::v('append'));
299 $page->assign('submited', true
);
303 function handler_admin_events(&$page, $action = 'list', $eid = null
)
305 $page->changeTpl('admin/evenements.tpl');
306 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
307 $page->register_modifier('hde', 'html_entity_decode');
309 $arch = $action == 'archives';
310 $page->assign('action', $action);
312 if (Post
::v('action') == "Proposer" && $eid) {
313 $promo_min = Post
::i('promo_min');
314 $promo_max = Post
::i('promo_max');
315 if ($promo_min > $promo_max ||
316 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
317 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
319 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
322 XDB
::execute('UPDATE evenements
323 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
325 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
326 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
330 if ($action == 'edit') {
331 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
333 WHERE id={?}', $eid);
334 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
335 $page->assign('titre',$titre);
336 $page->assign('texte',$texte);
337 $page->assign('promo_min',$promo_min);
338 $page->assign('promo_max',$promo_max);
339 $page->assign('peremption',$peremption);
342 for ($i = 1 ; $i < 30 ; $i++
) {
343 $p_stamp=date("Ymd",time()+
3600*24*$i);
344 $year=substr($p_stamp,0,4);
345 $month=substr($p_stamp,4,2);
346 $day=substr($p_stamp,6,2);
348 $select .= "<option value=\"$p_stamp\""
349 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
350 . "> $day / $month / $year</option>\n";
352 $page->assign('select',$select);
356 XDB
::execute('DELETE from evenements
357 WHERE id = {?}', $eid);
361 XDB
::execute('UPDATE evenements
362 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
363 WHERE id = {?}', $eid);
367 XDB
::execute('UPDATE evenements
368 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
369 WHERE id = {?}', $eid);
370 $action = 'archives';
375 XDB
::execute('UPDATE evenements
376 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
377 WHERE id = {?}', $eid);
381 XDB
::execute('UPDATE evenements
382 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
383 WHERE id = {?}', $eid);
387 $pid = ($eid && $action == 'preview') ?
$eid : -1;
388 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
389 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
390 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
391 e.promo_min, e.promo_max,
392 FIND_IN_SET('valide', e.flags) AS fvalide,
393 FIND_IN_SET('archive', e.flags) AS farch,
394 u.promo, u.nom, u.prenom, a.alias AS forlife
396 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
397 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
398 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
399 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
400 $page->assign('evs', XDB
::iterator($sql));
402 $page->assign('arch', $arch);
405 function handler_admin_nl(&$page, $new = false
) {
406 $page->changeTpl('newsletter/admin.tpl');
407 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
408 require_once("newsletter.inc.php");
412 pl_redirect("admin/newsletter");
415 $page->assign('nl_list', get_nl_slist());
418 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
419 $page->changeTpl('newsletter/edit.tpl');
420 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
421 require_once("newsletter.inc.php");
423 $nl = new NewsLetter($nid);
425 if($action == 'delete') {
426 $nl->delArticle($aid);
427 pl_redirect("admin/newsletter/edit/$nid");
430 if($aid == 'update') {
431 $nl->_title
= Post
::v('title');
432 $nl->_title_mail
= Post
::v('title_mail');
433 $nl->_date
= Post
::v('date');
434 $nl->_head
= Post
::v('head');
435 $nl->_shortname
= strlen(Post
::v('shortname')) ? Post
::v('shortname') : null
;
436 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname
) && !is_numeric($nl->_shortname
)) {
439 $page->trig('Le nom de la NL n\'est pas valide');
440 pl_redirect('admin/newsletter/edit/' . $nl->_id
);
444 if(Post
::v('save')) {
445 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
446 $aid, Post
::v('cid'), Post
::v('pos'));
447 $nl->saveArticle($art);
448 pl_redirect("admin/newsletter/edit/$nid");
451 if($action == 'edit' && $aid != 'update') {
453 if(Post
::has('title')) {
454 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
455 $eaid, Post
::v('cid'), Post
::v('pos'));
457 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
459 $page->assign('art', $art);
462 $page->assign_by_ref('nl',$nl);
464 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
465 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
466 $page->assign('title', 'Gestion des catégories de la newsletter');
467 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
468 $table_editor->describe('titre','intitulé',true
);
469 $table_editor->describe('pos','position',true
);
470 $table_editor->apply($page, $action, $id);