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