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 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP
),
29 'admin/events' => $this->make_hook('admin_events', AUTH_MDP
, 'admin'),
31 'nl' => $this->make_hook('nl', AUTH_COOKIE
),
32 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE
),
33 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE
),
34 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP
, 'admin'),
35 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP
, 'admin'),
36 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP
, 'admin'),
40 function on_subscribe($forlife, $uid, $promo, $password)
42 require_once 'newsletter.inc.php';
46 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
48 $page->changeTpl('login.tpl');
50 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
51 WHERE user_id={?}', S
::v('uid'));
52 list($date, $naissance) = $res->fetchOneRow();
54 // incitation à mettre à jour la fiche
56 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
58 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
59 // si fiche date de + de 400j;
60 $page->assign('fiche_incitation', $date);
63 // Souhaite bon anniversaire
65 if (substr($naissance, 5) == date('m-d')) {
66 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
69 // incitation à mettre une photo
71 $res = XDB
::query('SELECT COUNT(*) FROM photo
72 WHERE uid={?}', S
::v('uid'));
73 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
75 // Incitation à se géolocaliser
76 require_once 'geoloc.inc.php';
77 $res = localize_addresses(S
::v('uid', -1));
78 $page->assign('geoloc_incitation', count($res));
80 // affichage de la boîte avec quelques liens
81 /* Bandeau de publicité sur la page de login */
83 'password' => 'Changer mon mot de passe' ,
84 'Docs/Dons' => 'Faire un don à l\'association Polytechnique.org'
87 // Liens apparaissant de façon aléatoire
89 'nl/show' => 'Afficher la dernière newsletter' ,
90 'http://www.polytechnique.net' => 'Vers les autres sites polytechniciens' ,
91 "trombi/{$_SESSION["promo
"]}" => "Voir le trombi de ma promo" ,
92 'banana' => 'Un petit tour du côté des forums !!'
95 $choix = array_rand($pub_rnd, 2);
96 foreach ($choix as $url) {
97 $publicite[$url] = $pub_rnd[$url] ;
99 $page->assign('publicite', array_chunk($publicite, 2, true
));
102 if (S
::has('core_rss_hash')) {
103 $page->setRssLink('Polytechnique.org :: News',
104 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
107 // cache les evenements lus et raffiche les evenements a relire
108 if ($action == 'read' && $eid) {
109 XDB
::execute('DELETE evenements_vus.*
110 FROM evenements_vus AS ev
111 INNER JOIN evenements AS e ON e.id = ev.evt_id
112 WHERE peremption < NOW()');
113 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
115 pl_redirect('events#'.$pound);
118 if ($action == 'unread' && $eid) {
119 XDB
::execute('DELETE FROM evenements_vus
120 WHERE evt_id = {?} AND user_id = {?}',
122 pl_redirect('events#newsid'.$eid);
125 // affichage des evenements
126 // annonces promos triées par présence d'une limite sur les promos
127 // puis par dates croissantes d'expiration
128 $promo = S
::v('promo');
129 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
131 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
132 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
133 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
134 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
135 AND (e.promo_min = 0 || e.promo_min <= {?})
136 AND (e.promo_max = 0 || e.promo_max >= {?})
137 AND ev.user_id IS NULL
138 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
139 $page->assign('evenement',
140 XDB
::iterator($sql, S
::v('uid'),
144 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
146 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
147 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
148 AND (e.promo_min = 0 || e.promo_min <= {?})
149 AND (e.promo_max = 0 || e.promo_max >= {?})
150 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
151 $page->assign('evenement_summary',
152 XDB
::iterator($sql, S
::v('uid'),
157 function handler_ev_submit(&$page)
159 $page->changeTpl('evenements.tpl');
161 $titre = Post
::v('titre');
162 $texte = Post
::v('texte');
163 $promo_min = Post
::i('promo_min');
164 $promo_max = Post
::i('promo_max');
165 $peremption = Post
::i('peremption');
166 $valid_mesg = Post
::v('valid_mesg');
167 $action = Post
::v('action');
169 if ($promo_min > $promo_max ||
170 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
171 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
173 $page->trig("L'intervalle de promotions n'est pas valide");
177 require_once('url_catcher.inc.php');
178 $texte_catch_url = url_catcher($texte);
180 $page->assign('titre', $titre);
181 $page->assign('texte', $texte);
182 $page->assign('texte_html', $texte_catch_url);
183 $page->assign('promo_min', $promo_min);
184 $page->assign('promo_max', $promo_max);
185 $page->assign('peremption', $peremption);
186 $page->assign('valid_mesg', $valid_mesg);
187 $page->assign('action', strtolower($action));
189 if ($action == 'Confirmer') {
190 $texte = $texte_catch_url;
191 require_once 'validations.inc.php';
192 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
193 $peremption, $valid_mesg, S
::v('uid'));
195 $page->assign('ok', true
);
199 for ($i = 1 ; $i < 30 ; $i++
) {
200 $time = time() +
3600 * 24 * $i;
201 $p_stamp = date('Ymd', $time);
202 $year = date('Y', $time);
203 $month = date('m', $time);
204 $day = date('d', $time);
206 $select .= "<option value=\"$p_stamp\"";
207 if ($p_stamp == strtr($peremption, array("-" => ""))) {
208 $select .= " selected='selected'";
210 $select .= "> $day / $month / $year</option>\n";
212 $page->assign('select',$select);
215 function handler_nl(&$page, $action = null
)
217 require_once 'newsletter.inc.php';
219 $page->changeTpl('newsletter/index.tpl');
220 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
223 case 'out': unsubscribe_nl(); break;
224 case 'in': subscribe_nl(); break;
228 $page->assign('nls', get_nl_state());
229 $page->assign('nl_list', get_nl_list());
232 function handler_nl_show(&$page, $nid = 'last')
234 $page->changeTpl('newsletter/show.tpl');
236 require_once 'newsletter.inc.php';
238 $nl = new NewsLetter($nid);
239 $page->assign_by_ref('nl', $nl);
241 if (Post
::has('send')) {
242 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
243 S
::v('bestalias'), S
::v('femme'),
244 S
::v('mail_fmt') != 'texte');
248 function handler_nl_submit(&$page)
250 $page->changeTpl('newsletter/submit.tpl');
252 require_once 'newsletter.inc.php';
254 if (Post
::has('see')) {
255 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
256 $page->assign('art', $art);
257 } elseif (Post
::has('valid')) {
258 require_once('validations.inc.php');
259 $art = new NLReq(S
::v('uid'), Post
::v('title'),
260 Post
::v('body'), Post
::v('append'));
262 $page->assign('submited', true
);
266 function handler_admin_events(&$page, $action = 'list', $eid = null
)
268 $page->changeTpl('admin/evenements.tpl');
269 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
270 $page->register_modifier('hde', 'html_entity_decode');
272 $arch = $action == 'archives';
273 $page->assign('action', $action);
275 if (Post
::v('action') == "Proposer" && $eid) {
276 $promo_min = Post
::i('promo_min');
277 $promo_max = Post
::i('promo_max');
278 if ($promo_min > $promo_max ||
279 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
280 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
282 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
285 XDB
::execute('UPDATE evenements
286 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
288 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
289 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
293 if ($action == 'edit') {
294 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
296 WHERE id={?}', $eid);
297 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
298 $page->assign('titre',$titre);
299 $page->assign('texte',$texte);
300 $page->assign('promo_min',$promo_min);
301 $page->assign('promo_max',$promo_max);
302 $page->assign('peremption',$peremption);
305 for ($i = 1 ; $i < 30 ; $i++
) {
306 $p_stamp=date("Ymd",time()+
3600*24*$i);
307 $year=substr($p_stamp,0,4);
308 $month=substr($p_stamp,4,2);
309 $day=substr($p_stamp,6,2);
311 $select .= "<option value=\"$p_stamp\""
312 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
313 . "> $day / $month / $year</option>\n";
315 $page->assign('select',$select);
319 XDB
::execute('DELETE from evenements
320 WHERE id = {?}', $eid);
324 XDB
::execute('UPDATE evenements
325 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
326 WHERE id = {?}', $eid);
330 XDB
::execute('UPDATE evenements
331 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
332 WHERE id = {?}', $eid);
333 $action = 'archives';
338 XDB
::execute('UPDATE evenements
339 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
340 WHERE id = {?}', $eid);
344 XDB
::execute('UPDATE evenements
345 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
346 WHERE id = {?}', $eid);
350 $pid = ($eid && $action == 'preview') ?
$eid : -1;
351 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
352 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
353 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
354 e.promo_min, e.promo_max,
355 FIND_IN_SET('valide', e.flags) AS fvalide,
356 FIND_IN_SET('archive', e.flags) AS farch,
357 u.promo, u.nom, u.prenom, a.alias AS forlife
359 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
360 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
361 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
362 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
363 $page->assign('evs', XDB
::iterator($sql));
365 $page->assign('arch', $arch);
368 function handler_admin_nl(&$page, $new = false
) {
369 $page->changeTpl('newsletter/admin.tpl');
370 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
371 require_once("newsletter.inc.php");
375 pl_redirect("admin/newsletter");
378 $page->assign('nl_list', get_nl_slist());
381 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
382 $page->changeTpl('newsletter/edit.tpl');
383 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
384 require_once("newsletter.inc.php");
386 $nl = new NewsLetter($nid);
388 if($action == 'delete') {
389 $nl->delArticle($aid);
390 pl_redirect("admin/newsletter/edit/$nid");
393 if($aid == 'update') {
394 $nl->_title
= Post
::v('title');
395 $nl->_title_mail
= Post
::v('title_mail');
396 $nl->_date
= Post
::v('date');
397 $nl->_head
= Post
::v('head');
398 $nl->_shortname
= strlen(Post
::v('shortname')) ? Post
::v('shortname') : null
;
399 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname
) && !is_numeric($nl->_shortname
)) {
402 $page->trig('Le nom de la NL n\'est pas valide');
403 pl_redirect('admin/newsletter/edit/' . $nl->_id
);
407 if(Post
::v('save')) {
408 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
409 $aid, Post
::v('cid'), Post
::v('pos'));
410 $nl->saveArticle($art);
411 pl_redirect("admin/newsletter/edit/$nid");
414 if($action == 'edit' && $aid != 'update') {
416 if(Post
::has('title')) {
417 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
418 $eaid, Post
::v('cid'), Post
::v('pos'));
420 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
422 $page->assign('art', $art);
425 $page->assign_by_ref('nl',$nl);
427 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
428 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
429 $page->assign('title', 'Gestion des catégories de la newsletter');
430 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
431 $table_editor->describe('titre','intitulé',true
);
432 $table_editor->describe('pos','position',true
);
433 $table_editor->apply($page, $action, $id);