signaler un bug envoie un mail au support au lieu de creer un bug flyspray
[platal.git] / modules / events.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class EventsModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
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'),
31
32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, '', NO_AUTH),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
34
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'),
41 );
42 }
43
44 function on_subscribe($forlife, $uid, $promo, $password)
45 {
46 require_once 'newsletter.inc.php';
47 subscribe_nl($uid);
48 }
49
50 function get_tips($exclude = null)
51 {
52 $exclude = is_null($exclude) ? '' : ' AND id != ' . $exclude . ' ';
53 $priority = rand(0, 510);
54 do {
55 $priority = (int)($priority/2);
56 $res = XDB::query("SELECT *
57 FROM tips
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 >= {?})
61 AND (priorite >= {?})
62 AND (state = 'active')
63 $exclude
64 ORDER BY RAND()
65 LIMIT 1",
66 S::i('promo'), S::i('promo'), $priority);
67 } while ($priority && !$res->numRows());
68 if (!$res->numRows()) {
69 return null;
70 }
71 return $res->fetchOneAssoc();
72 }
73
74 function handler_bug(&$page)
75 {
76 $page->changeTpl('bug.tpl',SIMPLE);
77 $page->addJsLink('close_on_esc.js');
78 if (Env::has('send')) {
79 $page->assign('bug_sent',1);
80
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'));
87 $mymail->send();
88 }
89 }
90
91 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
92 {
93 $page->changeTpl('login.tpl');
94 $page->addJsLink('ajax.js');
95 $page->assign('tips', $this->get_tips());
96
97 // donne la derniere date de session
98 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S::i('lastlogin')));
99
100 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
101 WHERE user_id={?}', S::v('uid'));
102 list($date, $naissance) = $res->fetchOneRow();
103
104 // incitation à mettre à jour la fiche
105
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);
111 }
112
113 // Souhaite bon anniversaire
114
115 if (substr($naissance, 5) == date('m-d')) {
116 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
117 }
118
119 // incitation à mettre une photo
120
121 $res = XDB::query('SELECT COUNT(*) FROM photo
122 WHERE uid={?}', S::v('uid'));
123 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
124
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));
129
130 // ajout du lien RSS
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');
134 }
135
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({?},{?})',
143 $eid, S::v('uid'));
144 pl_redirect('events#'.$pound);
145 }
146
147 if ($action == 'unread' && $eid) {
148 XDB::execute('DELETE FROM evenements_vus
149 WHERE evt_id = {?} AND user_id = {?}',
150 $eid, S::v('uid'));
151 pl_redirect('events#newsid'.$eid);
152 }
153
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
159 FROM evenements AS e
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'),
170 $promo, $promo)
171 );
172
173 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
174 FROM evenements AS e
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'),
182 $promo, $promo)
183 );
184 }
185
186 function handler_ev_submit(&$page)
187 {
188 $page->changeTpl('evenements.tpl');
189
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');
197
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)))
201 {
202 $page->trig("L'intervalle de promotions n'est pas valide");
203 $action = null;
204 }
205
206 require_once('url_catcher.inc.php');
207 $texte_catch_url = url_catcher($texte);
208
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));
217
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'));
223 $evtreq->submit();
224 $page->assign('ok', true);
225 }
226
227 $select = '';
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);
234
235 $select .= "<option value=\"$p_stamp\"";
236 if ($p_stamp == strtr($peremption, array("-" => ""))) {
237 $select .= " selected='selected'";
238 }
239 $select .= "> $day / $month / $year</option>\n";
240 }
241 $page->assign('select',$select);
242 }
243
244 function handler_tips(&$page, $tips = null)
245 {
246 $page->changeTpl('include/tips.tpl', NO_SKIN);
247 $page->assign('tips', $this->get_tips($tips));
248 }
249
250 function handler_admin_tips(&$page, $action = 'list', $id = null)
251 {
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);
263 }
264
265 function handler_nl(&$page, $action = null)
266 {
267 require_once 'newsletter.inc.php';
268
269 $page->changeTpl('newsletter/index.tpl');
270 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
271
272 switch ($action) {
273 case 'out': unsubscribe_nl(); break;
274 case 'in': subscribe_nl(); break;
275 default: ;
276 }
277
278 $page->assign('nls', get_nl_state());
279 $page->assign('nl_list', get_nl_list());
280 }
281
282 function handler_nl_show(&$page, $nid = 'last')
283 {
284 $page->changeTpl('newsletter/show.tpl');
285
286 require_once 'newsletter.inc.php';
287
288 $nl = new NewsLetter($nid);
289 $page->assign_by_ref('nl', $nl);
290
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');
295 }
296 }
297
298 function handler_nl_submit(&$page)
299 {
300 $page->changeTpl('newsletter/submit.tpl');
301
302 require_once 'newsletter.inc.php';
303
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'));
311 $art->submit();
312 $page->assign('submited', true);
313 }
314 }
315
316 function handler_admin_events(&$page, $action = 'list', $eid = null)
317 {
318 $page->changeTpl('admin/evenements.tpl');
319 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
320 $page->register_modifier('hde', 'html_entity_decode');
321
322 $arch = $action == 'archives';
323 $page->assign('action', $action);
324
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)))
331 {
332 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
333 $action = 'edit';
334 } else {
335 XDB::execute('UPDATE evenements
336 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
337 WHERE id = {?}',
338 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
339 Post::v('promo_min'), Post::v('promo_max'), $eid);
340 }
341 }
342
343 if ($action == 'edit') {
344 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max
345 FROM evenements
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);
353
354 $select = "";
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);
360
361 $select .= "<option value=\"$p_stamp\""
362 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
363 . "> $day / $month / $year</option>\n";
364 }
365 $page->assign('select',$select);
366 } else {
367 switch ($action) {
368 case 'delete':
369 XDB::execute('DELETE from evenements
370 WHERE id = {?}', $eid);
371 break;
372
373 case "archive":
374 XDB::execute('UPDATE evenements
375 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
376 WHERE id = {?}', $eid);
377 break;
378
379 case "unarchive":
380 XDB::execute('UPDATE evenements
381 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
382 WHERE id = {?}', $eid);
383 $action = 'archives';
384 $arch = true;
385 break;
386
387 case "valid":
388 XDB::execute('UPDATE evenements
389 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
390 WHERE id = {?}', $eid);
391 break;
392
393 case "unvalid":
394 XDB::execute('UPDATE evenements
395 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
396 WHERE id = {?}', $eid);
397 break;
398 }
399
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
408 FROM evenements AS e
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));
414 }
415 $page->assign('arch', $arch);
416 }
417
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");
422
423 if($new) {
424 insert_new_nl();
425 pl_redirect("admin/newsletter");
426 }
427
428 $page->assign('nl_list', get_nl_slist());
429 }
430
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");
435
436 $nl = new NewsLetter($nid);
437
438 if($action == 'delete') {
439 $nl->delArticle($aid);
440 pl_redirect("admin/newsletter/edit/$nid");
441 }
442
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)) {
450 $nl->save();
451 } else {
452 $page->trig('Le nom de la NL n\'est pas valide');
453 pl_redirect('admin/newsletter/edit/' . $nl->_id);
454 }
455 }
456
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");
462 }
463
464 if($action == 'edit' && $aid != 'update') {
465 $eaid = $aid;
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'));
469 } else {
470 $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
471 }
472 $page->assign('art', $art);
473 }
474
475 $page->assign_by_ref('nl',$nl);
476 }
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);
484 }
485
486 }
487
488 ?>