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