4e25980e628bfc443d26714d89a47b4c7b145ce7
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 >= {?})
62 AND (state = 'active')
66 S
::i('promo'), S
::i('promo'), $priority);
67 } while ($priority && !$res->numRows());
68 if (!$res->numRows()) {
71 return $res->fetchOneAssoc();
74 function handler_bug(&$page)
76 $page->changeTpl('bug.tpl',SIMPLE
);
77 $page->addJsLink('close_on_esc.js');
78 if (Env
::has('send')) {
79 $page->assign('bug_sent',1);
81 require_once "diogenes/diogenes.hermes.inc.php";
82 $mymail = new HermesMailer();
83 $mymail->setFrom('"'.S
::v('prenom').' '.S
::v('nom').'" <'.S
::v('bestalias').'@polytechnique.org>');
84 $mymail->addTo('support+platal@polytechnique.org');
85 $mymail->setSubject('Plat/al '.Env
::v('task_type').' : '.Env
::v('item_summary'));
86 $mymail->setTxtBody(Env
::v('detailed_desc'));
91 function handler_ev(&$page, $action = 'list', $eid = null
, $pound = null
)
93 $page->changeTpl('login.tpl');
94 $page->addJsLink('ajax.js');
95 $page->assign('tips', $this->get_tips());
97 // donne la derniere date de session
98 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S
::i('lastlogin')));
100 $res = XDB
::query('SELECT date, naissance FROM auth_user_md5
101 WHERE user_id={?}', S
::v('uid'));
102 list($date, $naissance) = $res->fetchOneRow();
104 // incitation à mettre à jour la fiche
106 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
107 substr($date, 0, 4));
108 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
109 // si fiche date de + de 400j;
110 $page->assign('fiche_incitation', $date);
113 // Souhaite bon anniversaire
115 if (substr($naissance, 5) == date('m-d')) {
116 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
119 // incitation à mettre une photo
121 $res = XDB
::query('SELECT COUNT(*) FROM photo
122 WHERE uid={?}', S
::v('uid'));
123 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
125 // Incitation à se géolocaliser
126 require_once 'geoloc.inc.php';
127 $res = localize_addresses(S
::v('uid', -1));
128 $page->assign('geoloc_incitation', count($res));
131 if (S
::has('core_rss_hash')) {
132 $page->setRssLink('Polytechnique.org :: News',
133 '/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
136 // cache les evenements lus et raffiche les evenements a relire
137 if ($action == 'read' && $eid) {
138 XDB
::execute('DELETE evenements_vus.*
139 FROM evenements_vus AS ev
140 INNER JOIN evenements AS e ON e.id = ev.evt_id
141 WHERE peremption < NOW()');
142 XDB
::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
144 pl_redirect('events#'.$pound);
147 if ($action == 'unread' && $eid) {
148 XDB
::execute('DELETE FROM evenements_vus
149 WHERE evt_id = {?} AND user_id = {?}',
151 pl_redirect('events#newsid'.$eid);
154 // affichage des evenements
155 // annonces promos triées par présence d'une limite sur les promos
156 // puis par dates croissantes d'expiration
157 $promo = S
::v('promo');
158 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
160 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
161 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
162 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
163 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
164 AND (e.promo_min = 0 || e.promo_min <= {?})
165 AND (e.promo_max = 0 || e.promo_max >= {?})
166 AND ev.user_id IS NULL
167 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
168 $page->assign('evenement',
169 XDB
::iterator($sql, S
::v('uid'),
173 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
175 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
176 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
177 AND (e.promo_min = 0 || e.promo_min <= {?})
178 AND (e.promo_max = 0 || e.promo_max >= {?})
179 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
180 $page->assign('evenement_summary',
181 XDB
::iterator($sql, S
::v('uid'),
186 function handler_ev_submit(&$page)
188 $page->changeTpl('evenements.tpl');
190 $titre = Post
::v('titre');
191 $texte = Post
::v('texte');
192 $promo_min = Post
::i('promo_min');
193 $promo_max = Post
::i('promo_max');
194 $peremption = Post
::i('peremption');
195 $valid_mesg = Post
::v('valid_mesg');
196 $action = Post
::v('action');
198 if ($promo_min > $promo_max ||
199 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
200 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
202 $page->trig("L'intervalle de promotions n'est pas valide");
206 require_once('url_catcher.inc.php');
207 $texte_catch_url = url_catcher($texte);
209 $page->assign('titre', $titre);
210 $page->assign('texte', $texte);
211 $page->assign('texte_html', $texte_catch_url);
212 $page->assign('promo_min', $promo_min);
213 $page->assign('promo_max', $promo_max);
214 $page->assign('peremption', $peremption);
215 $page->assign('valid_mesg', $valid_mesg);
216 $page->assign('action', strtolower($action));
218 if ($action == 'Confirmer') {
219 $texte = $texte_catch_url;
220 require_once 'validations.inc.php';
221 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
222 $peremption, $valid_mesg, S
::v('uid'));
224 $page->assign('ok', true
);
228 for ($i = 1 ; $i < 30 ; $i++
) {
229 $time = time() +
3600 * 24 * $i;
230 $p_stamp = date('Ymd', $time);
231 $year = date('Y', $time);
232 $month = date('m', $time);
233 $day = date('d', $time);
235 $select .= "<option value=\"$p_stamp\"";
236 if ($p_stamp == strtr($peremption, array("-" => ""))) {
237 $select .= " selected='selected'";
239 $select .= "> $day / $month / $year</option>\n";
241 $page->assign('select',$select);
244 function handler_tips(&$page, $tips = null
)
246 $page->changeTpl('include/tips.tpl', NO_SKIN
);
247 $page->assign('tips', $this->get_tips($tips));
250 function handler_admin_tips(&$page, $action = 'list', $id = null
)
252 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
253 $page->assign('title', 'Gestion des Astuces');
254 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
255 $table_editor->describe('peremption', 'date de péremption', true
);
256 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false
);
257 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false
);
258 $table_editor->describe('titre', 'titre', true
);
259 $table_editor->describe('state', 'actif', true
);
260 $table_editor->describe('text', 'texte (html) de l\'astuce', false
);
261 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false
);
262 $table_editor->apply($page, $action, $id);
265 function handler_nl(&$page, $action = null
)
267 require_once 'newsletter.inc.php';
269 $page->changeTpl('newsletter/index.tpl');
270 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
273 case 'out': unsubscribe_nl(); break;
274 case 'in': subscribe_nl(); break;
278 $page->assign('nls', get_nl_state());
279 $page->assign('nl_list', get_nl_list());
282 function handler_nl_show(&$page, $nid = 'last')
284 $page->changeTpl('newsletter/show.tpl');
286 require_once 'newsletter.inc.php';
288 $nl = new NewsLetter($nid);
289 $page->assign_by_ref('nl', $nl);
291 if (Post
::has('send')) {
292 $nl->sendTo(S
::v('prenom'), S
::v('nom'),
293 S
::v('bestalias'), S
::v('femme'),
294 S
::v('mail_fmt') != 'texte');
298 function handler_nl_submit(&$page)
300 $page->changeTpl('newsletter/submit.tpl');
302 require_once 'newsletter.inc.php';
304 if (Post
::has('see')) {
305 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'));
306 $page->assign('art', $art);
307 } elseif (Post
::has('valid')) {
308 require_once('validations.inc.php');
309 $art = new NLReq(S
::v('uid'), Post
::v('title'),
310 Post
::v('body'), Post
::v('append'));
312 $page->assign('submited', true
);
316 function handler_admin_events(&$page, $action = 'list', $eid = null
)
318 $page->changeTpl('admin/evenements.tpl');
319 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
320 $page->register_modifier('hde', 'html_entity_decode');
322 $arch = $action == 'archives';
323 $page->assign('action', $action);
325 if (Post
::v('action') == "Proposer" && $eid) {
326 $promo_min = Post
::i('promo_min');
327 $promo_max = Post
::i('promo_max');
328 if ($promo_min > $promo_max ||
329 ($promo_min != 0 && ($promo_min <= 1900 ||
$promo_min >= 2020)) ||
330 ($promo_max != 0 && ($promo_max <= 1900 ||
$promo_max >= 2020)))
332 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
335 XDB
::execute('UPDATE evenements
336 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
338 Post
::v('titre'), Post
::v('texte'), Post
::v('peremption'),
339 Post
::v('promo_min'), Post
::v('promo_max'), $eid);
343 if ($action == 'edit') {
344 $res = XDB
::query('SELECT titre, texte, peremption, promo_min, promo_max
346 WHERE id={?}', $eid);
347 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
348 $page->assign('titre',$titre);
349 $page->assign('texte',$texte);
350 $page->assign('promo_min',$promo_min);
351 $page->assign('promo_max',$promo_max);
352 $page->assign('peremption',$peremption);
355 for ($i = 1 ; $i < 30 ; $i++
) {
356 $p_stamp=date("Ymd",time()+
3600*24*$i);
357 $year=substr($p_stamp,0,4);
358 $month=substr($p_stamp,4,2);
359 $day=substr($p_stamp,6,2);
361 $select .= "<option value=\"$p_stamp\""
362 . (($p_stamp == strtr($peremption, array("-" => ""))) ?
" selected" : "")
363 . "> $day / $month / $year</option>\n";
365 $page->assign('select',$select);
369 XDB
::execute('DELETE from evenements
370 WHERE id = {?}', $eid);
374 XDB
::execute('UPDATE evenements
375 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
376 WHERE id = {?}', $eid);
380 XDB
::execute('UPDATE evenements
381 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
382 WHERE id = {?}', $eid);
383 $action = 'archives';
388 XDB
::execute('UPDATE evenements
389 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
390 WHERE id = {?}', $eid);
394 XDB
::execute('UPDATE evenements
395 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
396 WHERE id = {?}', $eid);
400 $pid = ($eid && $action == 'preview') ?
$eid : -1;
401 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
402 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
403 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
404 e.promo_min, e.promo_max,
405 FIND_IN_SET('valide', e.flags) AS fvalide,
406 FIND_IN_SET('archive', e.flags) AS farch,
407 u.promo, u.nom, u.prenom, a.alias AS forlife
409 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
410 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
411 WHERE ".($arch ?
"" : "!")."FIND_IN_SET('archive',e.flags)
412 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
413 $page->assign('evs', XDB
::iterator($sql));
415 $page->assign('arch', $arch);
418 function handler_admin_nl(&$page, $new = false
) {
419 $page->changeTpl('newsletter/admin.tpl');
420 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
421 require_once("newsletter.inc.php");
425 pl_redirect("admin/newsletter");
428 $page->assign('nl_list', get_nl_slist());
431 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null
, $action = 'edit') {
432 $page->changeTpl('newsletter/edit.tpl');
433 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
434 require_once("newsletter.inc.php");
436 $nl = new NewsLetter($nid);
438 if($action == 'delete') {
439 $nl->delArticle($aid);
440 pl_redirect("admin/newsletter/edit/$nid");
443 if($aid == 'update') {
444 $nl->_title
= Post
::v('title');
445 $nl->_title_mail
= Post
::v('title_mail');
446 $nl->_date
= Post
::v('date');
447 $nl->_head
= Post
::v('head');
448 $nl->_shortname
= strlen(Post
::v('shortname')) ? Post
::v('shortname') : null
;
449 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname
) && !is_numeric($nl->_shortname
)) {
452 $page->trig('Le nom de la NL n\'est pas valide');
453 pl_redirect('admin/newsletter/edit/' . $nl->_id
);
457 if(Post
::v('save')) {
458 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
459 $aid, Post
::v('cid'), Post
::v('pos'));
460 $nl->saveArticle($art);
461 pl_redirect("admin/newsletter/edit/$nid");
464 if($action == 'edit' && $aid != 'update') {
466 if(Post
::has('title')) {
467 $art = new NLArticle(Post
::v('title'), Post
::v('body'), Post
::v('append'),
468 $eaid, Post
::v('cid'), Post
::v('pos'));
470 $art = ($eaid == 'new') ?
new NLArticle() : $nl->getArt($eaid);
472 $page->assign('art', $art);
475 $page->assign_by_ref('nl',$nl);
477 function handler_admin_nl_cat(&$page, $action = 'list', $id = null
) {
478 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
479 $page->assign('title', 'Gestion des catégories de la newsletter');
480 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
481 $table_editor->describe('titre','intitulé',true
);
482 $table_editor->describe('pos','position',true
);
483 $table_editor->apply($page, $action, $id);