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