ajout de checkbox dans les table editor pour les champs set unique + flag actif pour...
[platal.git] / modules / events.php
CommitLineData
74e0093f 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
22class EventsModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
1a828cd4 27 'events' => $this->make_hook('ev', AUTH_COOKIE),
0535ed02 28 'send_bug' => $this->make_hook('bug', AUTH_COOKIE),
1a828cd4 29 'events/submit' => $this->make_hook('ev_submit', AUTH_MDP),
92423144 30 'admin/events' => $this->make_hook('admin_events', AUTH_MDP, 'admin'),
b829e258 31
dc767839 32 'ajax/tips' => $this->make_hook('tips', AUTH_COOKIE, '', NO_AUTH),
33 'admin/tips' => $this->make_hook('admin_tips', AUTH_MDP, 'admin'),
34
164ca57a 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),
92423144 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'),
74e0093f 41 );
42 }
43
8d8f7607 44 function on_subscribe($forlife, $uid, $promo, $password)
45 {
46 require_once 'newsletter.inc.php';
47 subscribe_nl($uid);
48 }
49
dc767839 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 >= {?})
5d617d8d 62 AND (state = 'active')
dc767839 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
0535ed02 74 function handler_bug(&$page)
75 {
76 $this->handler_ev($page);
77 $page->assign('bug', 1);
78 }
79
096f0dca 80 function handler_ev(&$page, $action = 'list', $eid = null, $pound = null)
1a828cd4 81 {
1a828cd4 82 $page->changeTpl('login.tpl');
dc767839 83 $page->addJsLink('ajax.js');
84 $page->assign('tips', $this->get_tips());
85
4c92c8b2 86 // donne la derniere date de session
87 $page->assign('lastlogin', strftime("%Y%m%d%H%M%S",S::i('lastlogin')));
1a828cd4 88
08cce2ff 89 $res = XDB::query('SELECT date, naissance FROM auth_user_md5
cab08090 90 WHERE user_id={?}', S::v('uid'));
1a828cd4 91 list($date, $naissance) = $res->fetchOneRow();
92
089a5801 93 // incitation à mettre à jour la fiche
1a828cd4 94
95 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
96 substr($date, 0, 4));
97 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
98 // si fiche date de + de 400j;
99 $page->assign('fiche_incitation', $date);
100 }
101
102 // Souhaite bon anniversaire
103
104 if (substr($naissance, 5) == date('m-d')) {
105 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
106 }
107
089a5801 108 // incitation à mettre une photo
1a828cd4 109
08cce2ff 110 $res = XDB::query('SELECT COUNT(*) FROM photo
cab08090 111 WHERE uid={?}', S::v('uid'));
1a828cd4 112 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
113
089a5801 114 // Incitation à se géolocaliser
1a828cd4 115 require_once 'geoloc.inc.php';
cab08090 116 $res = localize_addresses(S::v('uid', -1));
1a828cd4 117 $page->assign('geoloc_incitation', count($res));
118
1a828cd4 119 // ajout du lien RSS
cab08090 120 if (S::has('core_rss_hash')) {
162370e7 121 $page->setRssLink('Polytechnique.org :: News',
122 '/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
1a828cd4 123 }
124
125 // cache les evenements lus et raffiche les evenements a relire
4e61caea 126 if ($action == 'read' && $eid) {
eadff9f9 127 XDB::execute('DELETE evenements_vus.*
128 FROM evenements_vus AS ev
129 INNER JOIN evenements AS e ON e.id = ev.evt_id
130 WHERE peremption < NOW()');
08cce2ff 131 XDB::execute('REPLACE INTO evenements_vus VALUES({?},{?})',
096f0dca 132 $eid, S::v('uid'));
133 pl_redirect('events#'.$pound);
1a828cd4 134 }
135
4e61caea 136 if ($action == 'unread' && $eid) {
08cce2ff 137 XDB::execute('DELETE FROM evenements_vus
eadff9f9 138 WHERE evt_id = {?} AND user_id = {?}',
4e61caea 139 $eid, S::v('uid'));
096f0dca 140 pl_redirect('events#newsid'.$eid);
1a828cd4 141 }
142
143 // affichage des evenements
089a5801 144 // annonces promos triées par présence d'une limite sur les promos
1a828cd4 145 // puis par dates croissantes d'expiration
cab08090 146 $promo = S::v('promo');
1a828cd4 147 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
2f678da1 148 FROM evenements AS e
149 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
150 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
151 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
1a828cd4 152 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
153 AND (e.promo_min = 0 || e.promo_min <= {?})
154 AND (e.promo_max = 0 || e.promo_max >= {?})
155 AND ev.user_id IS NULL
156 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
157 $page->assign('evenement',
cab08090 158 XDB::iterator($sql, S::v('uid'),
1a828cd4 159 $promo, $promo)
160 );
161
162 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
163 FROM evenements AS e
164 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
165 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
166 AND (e.promo_min = 0 || e.promo_min <= {?})
167 AND (e.promo_max = 0 || e.promo_max >= {?})
168 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
169 $page->assign('evenement_summary',
cab08090 170 XDB::iterator($sql, S::v('uid'),
1a828cd4 171 $promo, $promo)
172 );
1a828cd4 173 }
174
175 function handler_ev_submit(&$page)
74e0093f 176 {
74e0093f 177 $page->changeTpl('evenements.tpl');
178
5e2307dc 179 $titre = Post::v('titre');
180 $texte = Post::v('texte');
181 $promo_min = Post::i('promo_min');
182 $promo_max = Post::i('promo_max');
183 $peremption = Post::i('peremption');
184 $valid_mesg = Post::v('valid_mesg');
185 $action = Post::v('action');
74e0093f 186
2086ab7f 187 if ($promo_min > $promo_max ||
188 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
189 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
190 {
191 $page->trig("L'intervalle de promotions n'est pas valide");
192 $action = null;
193 }
194
edd50750 195 require_once('url_catcher.inc.php');
196 $texte_catch_url = url_catcher($texte);
197
74e0093f 198 $page->assign('titre', $titre);
199 $page->assign('texte', $texte);
edd50750 200 $page->assign('texte_html', $texte_catch_url);
74e0093f 201 $page->assign('promo_min', $promo_min);
202 $page->assign('promo_max', $promo_max);
203 $page->assign('peremption', $peremption);
204 $page->assign('valid_mesg', $valid_mesg);
205 $page->assign('action', strtolower($action));
206
207 if ($action == 'Confirmer') {
edd50750 208 $texte = $texte_catch_url;
74e0093f 209 require_once 'validations.inc.php';
210 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
cab08090 211 $peremption, $valid_mesg, S::v('uid'));
74e0093f 212 $evtreq->submit();
213 $page->assign('ok', true);
214 }
215
216 $select = '';
217 for ($i = 1 ; $i < 30 ; $i++) {
218 $time = time() + 3600 * 24 * $i;
219 $p_stamp = date('Ymd', $time);
220 $year = date('Y', $time);
221 $month = date('m', $time);
222 $day = date('d', $time);
223
224 $select .= "<option value=\"$p_stamp\"";
225 if ($p_stamp == strtr($peremption, array("-" => ""))) {
226 $select .= " selected='selected'";
227 }
228 $select .= "> $day / $month / $year</option>\n";
229 }
230 $page->assign('select',$select);
74e0093f 231 }
b829e258 232
dc767839 233 function handler_tips(&$page, $tips = null)
234 {
235 $page->changeTpl('include/tips.tpl', NO_SKIN);
236 $page->assign('tips', $this->get_tips($tips));
237 }
238
239 function handler_admin_tips(&$page, $action = 'list', $id = null)
240 {
241 $page->assign('xorg_title', 'Polytechnique.org - Administration - Astuces');
242 $page->assign('title', 'Gestion des Astuces');
243 $table_editor = new PLTableEditor('admin/tips', 'tips', 'id');
244 $table_editor->describe('peremption', 'date de péremption', true);
245 $table_editor->describe('promo_min', 'promo. min (0 aucune)', false);
246 $table_editor->describe('promo_max', 'promo. max (0 aucune)', false);
247 $table_editor->describe('titre', 'titre', true);
5d617d8d 248 $table_editor->describe('state', 'actif', true);
dc767839 249 $table_editor->describe('text', 'texte (html) de l\'astuce', false);
250 $table_editor->describe('priorite', 'priorité (0=min, 256=max)', false);
251 $table_editor->apply($page, $action, $id);
252 }
253
164ca57a 254 function handler_nl(&$page, $action = null)
255 {
256 require_once 'newsletter.inc.php';
257
258 $page->changeTpl('newsletter/index.tpl');
259 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
260
261 switch ($action) {
262 case 'out': unsubscribe_nl(); break;
263 case 'in': subscribe_nl(); break;
264 default: ;
265 }
266
267 $page->assign('nls', get_nl_state());
1a013db7 268 $page->assign('nl_list', get_nl_list());
164ca57a 269 }
270
271 function handler_nl_show(&$page, $nid = 'last')
b829e258 272 {
273 $page->changeTpl('newsletter/show.tpl');
274
275 require_once 'newsletter.inc.php';
276
277 $nl = new NewsLetter($nid);
278 $page->assign_by_ref('nl', $nl);
279
280 if (Post::has('send')) {
cab08090 281 $nl->sendTo(S::v('prenom'), S::v('nom'),
282 S::v('bestalias'), S::v('femme'),
91f42324 283 S::v('mail_fmt') != 'texte');
b829e258 284 }
b829e258 285 }
164ca57a 286
287 function handler_nl_submit(&$page)
288 {
289 $page->changeTpl('newsletter/submit.tpl');
290
291 require_once 'newsletter.inc.php';
292
293 if (Post::has('see')) {
5e2307dc 294 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
164ca57a 295 $page->assign('art', $art);
296 } elseif (Post::has('valid')) {
297 require_once('validations.inc.php');
5e2307dc 298 $art = new NLReq(S::v('uid'), Post::v('title'),
299 Post::v('body'), Post::v('append'));
164ca57a 300 $art->submit();
301 $page->assign('submited', true);
302 }
164ca57a 303 }
92423144 304
506a46ed 305 function handler_admin_events(&$page, $action = 'list', $eid = null)
306 {
92423144 307 $page->changeTpl('admin/evenements.tpl');
308 $page->assign('xorg_title','Polytechnique.org - Administration - Evenements');
506a46ed 309 $page->register_modifier('hde', 'html_entity_decode');
310
311 $arch = $action == 'archives';
312 $page->assign('action', $action);
313
01248e3a 314 if (Post::v('action') == "Proposer" && $eid) {
2086ab7f 315 $promo_min = Post::i('promo_min');
316 $promo_max = Post::i('promo_max');
317 if ($promo_min > $promo_max ||
318 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
319 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
320 {
321 $page->trig("L'intervalle de promotions $promo_min -> $promo_max n'est pas valide");
322 $action = 'edit';
323 } else {
324 XDB::execute('UPDATE evenements
325 SET titre={?}, texte={?}, peremption={?}, promo_min={?}, promo_max={?}
326 WHERE id = {?}',
327 Post::v('titre'), Post::v('texte'), Post::v('peremption'),
328 Post::v('promo_min'), Post::v('promo_max'), $eid);
329 }
92423144 330 }
506a46ed 331
332 if ($action == 'edit') {
333 $res = XDB::query('SELECT titre, texte, peremption, promo_min, promo_max
334 FROM evenements
335 WHERE id={?}', $eid);
336 list($titre, $texte, $peremption, $promo_min, $promo_max) = $res->fetchOneRow();
337 $page->assign('titre',$titre);
338 $page->assign('texte',$texte);
339 $page->assign('promo_min',$promo_min);
340 $page->assign('promo_max',$promo_max);
341 $page->assign('peremption',$peremption);
342
343 $select = "";
344 for ($i = 1 ; $i < 30 ; $i++) {
345 $p_stamp=date("Ymd",time()+3600*24*$i);
346 $year=substr($p_stamp,0,4);
347 $month=substr($p_stamp,4,2);
348 $day=substr($p_stamp,6,2);
349
350 $select .= "<option value=\"$p_stamp\""
351 . (($p_stamp == strtr($peremption, array("-" => ""))) ? " selected" : "")
352 . "> $day / $month / $year</option>\n";
353 }
354 $page->assign('select',$select);
355 } else {
356 switch ($action) {
357 case 'delete':
358 XDB::execute('DELETE from evenements
359 WHERE id = {?}', $eid);
360 break;
361
362 case "archive":
363 XDB::execute('UPDATE evenements
364 SET creation_date = creation_date, flags = CONCAT(flags,",archive")
365 WHERE id = {?}', $eid);
366 break;
367
368 case "unarchive":
369 XDB::execute('UPDATE evenements
370 SET creation_date = creation_date, flags = REPLACE(flags,"archive","")
371 WHERE id = {?}', $eid);
372 $action = 'archives';
373 $arch = true;
374 break;
375
376 case "valid":
377 XDB::execute('UPDATE evenements
378 SET creation_date = creation_date, flags = CONCAT(flags,",valide")
379 WHERE id = {?}', $eid);
380 break;
381
382 case "unvalid":
383 XDB::execute('UPDATE evenements
384 SET creation_date = creation_date, flags = REPLACE(flags,"valide", "")
385 WHERE id = {?}', $eid);
386 break;
387 }
388
389 $pid = ($eid && $action == 'preview') ? $eid : -1;
390 $sql = "SELECT e.id, e.titre, e.texte,e.id = $pid AS preview,
92423144 391 DATE_FORMAT(e.creation_date,'%d/%m/%Y %T') AS creation_date,
392 DATE_FORMAT(e.peremption,'%d/%m/%Y') AS peremption,
393 e.promo_min, e.promo_max,
394 FIND_IN_SET('valide', e.flags) AS fvalide,
395 FIND_IN_SET('archive', e.flags) AS farch,
396 u.promo, u.nom, u.prenom, a.alias AS forlife
397 FROM evenements AS e
398 INNER JOIN auth_user_md5 AS u ON(e.user_id = u.user_id)
399 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
400 WHERE ".($arch ? "" : "!")."FIND_IN_SET('archive',e.flags)
506a46ed 401 ORDER BY FIND_IN_SET('valide',e.flags), e.peremption DESC";
92423144 402 $page->assign('evs', XDB::iterator($sql));
403 }
506a46ed 404 $page->assign('arch', $arch);
92423144 405 }
406
407 function handler_admin_nl(&$page, $new = false) {
163eddd2 408 $page->changeTpl('newsletter/admin.tpl');
92423144 409 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : liste');
410 require_once("newsletter.inc.php");
411
412 if($new) {
413 insert_new_nl();
414 pl_redirect("admin/newsletter");
415 }
416
1a013db7 417 $page->assign('nl_list', get_nl_slist());
92423144 418 }
419
420 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
163eddd2 421 $page->changeTpl('newsletter/edit.tpl');
92423144 422 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Edition');
423 require_once("newsletter.inc.php");
424
425 $nl = new NewsLetter($nid);
426
427 if($action == 'delete') {
428 $nl->delArticle($aid);
429 pl_redirect("admin/newsletter/edit/$nid");
430 }
431
432 if($aid == 'update') {
ed76a506 433 $nl->_title = Post::v('title');
8269d2d6 434 $nl->_title_mail= Post::v('title_mail');
ed76a506 435 $nl->_date = Post::v('date');
436 $nl->_head = Post::v('head');
437 $nl->_shortname = strlen(Post::v('shortname')) ? Post::v('shortname') : null;
438 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname) && !is_numeric($nl->_shortname)) {
439 $nl->save();
440 } else {
441 $page->trig('Le nom de la NL n\'est pas valide');
442 pl_redirect('admin/newsletter/edit/' . $nl->_id);
443 }
92423144 444 }
445
446 if(Post::v('save')) {
447 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
448 $aid, Post::v('cid'), Post::v('pos'));
449 $nl->saveArticle($art);
450 pl_redirect("admin/newsletter/edit/$nid");
451 }
452
ed76a506 453 if($action == 'edit' && $aid != 'update') {
92423144 454 $eaid = $aid;
455 if(Post::has('title')) {
456 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
457 $eaid, Post::v('cid'), Post::v('pos'));
458 } else {
459 $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
460 }
461 $page->assign('art', $art);
462 }
463
464 $page->assign_by_ref('nl',$nl);
465 }
466 function handler_admin_nl_cat(&$page, $action = 'list', $id = null) {
92423144 467 $page->assign('xorg_title','Polytechnique.org - Administration - Newsletter : Catégories');
468 $page->assign('title', 'Gestion des catégories de la newsletter');
469 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
470 $table_editor->describe('titre','intitulé',true);
471 $table_editor->describe('pos','position',true);
472 $table_editor->apply($page, $action, $id);
473 }
474
74e0093f 475}
476
477?>