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->assign('xorg_rss',
104 array('title' => 'Polytechnique.org :: News',
105 'href' => '/rss/'.S
::v('forlife')
106 .'/'.S
::v('core_rss_hash').'/rss.xml')
110 // cache les evenements lus et raffiche les evenements a relire
111 if ($action == 'read' && $eid) {
112 XDB
::execute('DELETE evenements_vus.*
113 FROM evenements_vus AS ev
114 INNER JOIN evenements AS e ON e.id = ev.evt_id
115 WHERE peremption < NOW()');
116 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
118 pl_redirect('events#'.$pound);
121 if ($action == 'unread' && $eid) {
122 XDB
::execute('DELETE FROM evenements_vus
123 WHERE evt_id = {?} AND user_id = {?}',
125 pl_redirect('events#newsid'.$eid);
128 // affichage des evenements
129 // annonces promos triées par présence d'une limite sur les promos
130 // puis par dates croissantes d'expiration
131 $promo = S
::v('promo');
132 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
134 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
135 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
136 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
137 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
138 AND (e.promo_min = 0 || e.promo_min <= {?})
139 AND (e.promo_max = 0 || e.promo_max >= {?})
140 AND ev.user_id IS NULL
141 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
142 $page->assign('evenement',
143 XDB
::iterator($sql, S
::v('uid'),
147 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
149 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
150 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
151 AND (e.promo_min = 0 || e.promo_min <= {?})
152 AND (e.promo_max = 0 || e.promo_max >= {?})
153 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
154 $page->assign('evenement_summary',
155 XDB
::iterator($sql, S
::v('uid'),
160 function handler_ev_submit(&$page)
162 $page->changeTpl('evenements.tpl');
164 $titre = Post
::v('titre');
165 $texte = Post
::v('texte');
166 $promo_min = Post
::i('promo_min');
167 $promo_max = Post
::i('promo_max');
168 $peremption = Post
::i('peremption');
169 $valid_mesg = Post
::v('valid_mesg');
170 $action = Post
::v('action');
172 $page->assign('titre', $titre);
173 $page->assign('texte', $texte);
174 $page->assign('promo_min', $promo_min);
175 $page->assign('promo_max', $promo_max);
176 $page->assign('peremption', $peremption);
177 $page->assign('valid_mesg', $valid_mesg);
178 $page->assign('action', strtolower($action));
180 if ($action == 'Confirmer') {
181 $texte = preg_replace('/((?:https?|ftp):\/\/(?:\.*,*[a-z@0-9~%$£µ&i#\-+=_\/\?])*)/i',
182 '<a href="\\0">\\0</a>', $texte);
183 $texte = preg_replace('/(?:mailto:)?([a-z0-9.\-+_]+@([\-.+_]?[a-z0-9])+)/i',
184 '<a href="mailto:\\0">\\0</a>', $texte);
185 require_once 'validations.inc.php';
186 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
187 $peremption, $valid_mesg, S
::v('uid'));
189 $page->assign('ok', true
);
193 for ($i = 1 ; $i < 30 ; $i++
) {
194 $time = time() +
3600 * 24 * $i;
195 $p_stamp = date('Ymd', $time);
196 $year = date('Y', $time);
197 $month = date('m', $time);
198 $day = date('d', $time);
200 $select .= "<option value=\"$p_stamp\"";
201 if ($p_stamp == strtr($peremption, array("-" => ""))) {
202 $select .= " selected='selected'";
204 $select .= "> $day / $month / $year</option>\n";
206 $page->assign('select',$select);
209 function handler_nl(&$page, $action = null
)
211 require_once 'newsletter.inc.php';
213 $page->changeTpl('newsletter/index.tpl');
214 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
217 case 'out': unsubscribe_nl(); break;
218 case 'in': subscribe_nl(); break;
222 $page->assign('nls', get_nl_state());
223 $page->assign('nl_list', get_nl_list());
226 function handler_nl_show(&$page, $nid = 'last')
228 $page->changeTpl('newsletter/show.tpl');
230 require_once 'newsletter.inc.php';
232 $nl = new NewsLetter($nid);
233 $page->assign_by_ref('nl', $nl);
235 if (Post
::has('send')) {
236 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
237 S
::v('bestalias'), S
::v('femme'),
238 S
::v('mail_fmt') != 'text');
242 function handler_nl_submit(&$page)
244 $page->changeTpl('newsletter/submit.tpl');
246 require_once 'newsletter.inc.php';
248 if (Post
::has('see')) {
249 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
250 $page->assign('art', $art);
251 } elseif (Post
::has('valid')) {
252 require_once('validations.inc.php');
253 $art = new NLReq(S
::v('uid'), Post
::v('title'),
254 Post
::v('body'), Post
::v('append'));
256 $page->assign('submited', true
);
260 function handler_admin_events(&$page, $action = 'list', $eid = null
)
262 $page->changeTpl('admin/evenements.tpl');
263 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
264 $page->register_modifier('hde', 'html_entity_decode');
266 $arch = $action == 'archives';
267 $page->assign('action', $action);
269 if (Post
::v('action') == "Proposer" && $eid) {
270 XDB
::execute('UPDATE evenements
271 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
273 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
274 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
277 if ($action == 'edit') {
278 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
280 WHERE id={?}', $eid);
281 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
282 $page->assign('titre',$titre);
283 $page->assign('texte',$texte);
284 $page->assign('promo_min',$promo_min);
285 $page->assign('promo_max',$promo_max);
286 $page->assign('peremption',$peremption);
289 for ($i = 1 ; $i < 30 ; $i++
) {
290 $p_stamp=date("Ymd",time()+
3600*24*$i);
291 $year=substr($p_stamp,0,4);
292 $month=substr($p_stamp,4,2);
293 $day=substr($p_stamp,6,2);
295 $select .= "<option value=\"$p_stamp\""
296 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
297 . "> $day / $month / $year</option>\n";
299 $page->assign('select',$select);
303 XDB
::execute('DELETE from evenements
304 WHERE id = {?}', $eid);
308 XDB
::execute('UPDATE evenements
309 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
310 WHERE id = {?}', $eid);
314 XDB
::execute('UPDATE evenements
315 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
316 WHERE id = {?}', $eid);
317 $action = 'archives';
322 XDB
::execute('UPDATE evenements
323 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
324 WHERE id = {?}', $eid);
328 XDB
::execute('UPDATE evenements
329 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
330 WHERE id = {?}', $eid);
334 $pid = ($eid && $action == 'preview') ?
$eid : -1;
335 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
336 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
337 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
338 e.promo_min, e.promo_max,
339 FIND_IN_SET('valide', e.flags) AS fvalide,
340 FIND_IN_SET('archive', e.flags) AS farch,
341 u.promo, u.nom, u.prenom, a.alias AS forlife
343 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
344 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
345 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
346 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
347 $page->assign('evs', XDB
::iterator($sql));
349 $page->assign('arch', $arch);
352 function handler_admin_nl(&$page, $new = false
) {
353 $page->changeTpl('newsletter/admin.tpl');
354 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
355 require_once("newsletter.inc.php");
359 pl_redirect("admin/newsletter");
362 $page->assign('nl_list', get_nl_slist());
365 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
366 $page->changeTpl('newsletter/edit.tpl');
367 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
368 require_once("newsletter.inc.php");
370 $nl = new NewsLetter($nid);
372 if($action == 'delete') {
373 $nl->delArticle($aid);
374 pl_redirect("admin/newsletter/edit/$nid");
377 if($aid == 'update') {
378 $nl->_title
= Post
::v('title');
379 $nl->_date
= Post
::v('date');
380 $nl->_head
= Post
::v('head');
384 if(Post
::v('save')) {
385 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
386 $aid, Post
::v('cid'), Post
::v('pos'));
387 $nl->saveArticle($art);
388 pl_redirect("admin/newsletter/edit/$nid");
391 if($action == 'edit') {
393 if(Post
::has('title')) {
394 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
395 $eaid, Post
::v('cid'), Post
::v('pos'));
397 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
399 $page->assign('art', $art);
402 $page->assign_by_ref('nl',$nl);
404 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
405 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
406 $page->assign('title', 'Gestion des catégories de la newsletter');
407 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
408 $table_editor->describe('titre','intitulé',true
);
409 $table_editor->describe('pos','position',true
);
410 $table_editor->apply($page, $action, $id);