Merge x-org-id and from header.
[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),
b829e258 29
164ca57a 30 'nl' => $this->make_hook('nl', AUTH_COOKIE),
31 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE),
32 'nl/submit' => $this->make_hook('nl_submit', AUTH_COOKIE),
74e0093f 33 );
34 }
35
1a828cd4 36 function handler_ev(&$page)
37 {
38 global $globals;
39
40 $page->changeTpl('login.tpl');
41
42 $res = $globals->xdb->query('SELECT date, naissance FROM auth_user_md5
43 WHERE user_id={?}', Session::getInt('uid'));
44 list($date, $naissance) = $res->fetchOneRow();
45
46 // incitation à mettre à jour la fiche
47
48 $d2 = mktime(0, 0, 0, substr($date, 5, 2), substr($date, 8, 2),
49 substr($date, 0, 4));
50 if( (time() - $d2) > 60 * 60 * 24 * 400 ) {
51 // si fiche date de + de 400j;
52 $page->assign('fiche_incitation', $date);
53 }
54
55 // Souhaite bon anniversaire
56
57 if (substr($naissance, 5) == date('m-d')) {
58 $page->assign('birthday', date('Y') - substr($naissance, 0, 4));
59 }
60
61 // incitation à mettre une photo
62
63 $res = $globals->xdb->query('SELECT COUNT(*) FROM photo
64 WHERE uid={?}', Session::getInt('uid'));
65 $page->assign('photo_incitation', $res->fetchOneCell() == 0);
66
67 // Incitation à se géolocaliser
68 require_once 'geoloc.inc.php';
69 $res = localize_addresses(Session::getInt('uid', -1));
70 $page->assign('geoloc_incitation', count($res));
71
72 // affichage de la boîte avec quelques liens
73 require_once 'login.conf.php';
74 $pub_nbElem = $pub_nbLig * $pub_nbCol ;
75 if (count($pub_tjs) <= $pub_nbElem) {
76 $publicite = array_slice($pub_tjs, 0, $pub_nbElem);
77 } else {
78 $publicite = $pub_tjs ;
79 }
80
81 $nbAlea = $pub_nbElem - count($publicite) ;
82 if ($nbAlea > 0) {
83 $choix = array_rand($pub_rnd,$nbAlea) ;
84 foreach ($choix as $url) {
85 $publicite[$url] = $pub_rnd[$url] ;
86 }
87 }
88 $publicite = array_chunk( $publicite , $pub_nbLig , true ) ;
89 $page->assign_by_ref('publicite', $publicite);
90
91 // ajout du lien RSS
92
93 if (Session::has('core_rss_hash')) {
94 $page->assign('xorg_rss',
95 array('title' => 'Polytechnique.org :: News',
96 'href' => '/rss/'.Session::get('forlife')
97 .'/'.Session::get('core_rss_hash').'/rss.xml')
98 );
99 }
100
101 // cache les evenements lus et raffiche les evenements a relire
102 if (Env::has('lu')){
103 $globals->xdb->execute('DELETE FROM evenements_vus AS ev
104 INNER JOIN evenements AS e ON e.id = ev.evt_id
105 WHERE peremption < NOW)');
106 $globals->xdb->execute('REPLACE INTO evenements_vus VALUES({?},{?})',
107 Env::get('lu'), Session::getInt('uid'));
108 }
109
110 if (Env::has('nonlu')){
111 $globals->xdb->execute('DELETE FROM evenements_vus
112 WHERE evt_id = {?} AND user_id = {?}',
113 Env::get('nonlu'), Session::getInt('uid'));
114 }
115
116 // affichage des evenements
117 // annonces promos triées par présence d'une limite sur les promos
118 // puis par dates croissantes d'expiration
119 $promo = Session::getInt('promo');
120 $sql = "SELECT e.id,e.titre,e.texte,a.user_id,a.nom,a.prenom,a.promo,l.alias AS forlife
121 FROM evenements AS e
122 INNER JOIN auth_user_md5 AS a ON e.user_id=a.user_id
123 INNER JOIN aliases AS l ON ( a.user_id=l.id AND l.type='a_vie' )
124 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
125 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
126 AND (e.promo_min = 0 || e.promo_min <= {?})
127 AND (e.promo_max = 0 || e.promo_max >= {?})
128 AND ev.user_id IS NULL
129 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
130 $page->assign('evenement',
131 $globals->xdb->iterator($sql, Session::getInt('uid'),
132 $promo, $promo)
133 );
134
135 $sql = "SELECT e.id,e.titre, ev.user_id IS NULL AS nonlu
136 FROM evenements AS e
137 LEFT JOIN evenements_vus AS ev ON (e.id = ev.evt_id AND ev.user_id = {?})
138 WHERE FIND_IN_SET(e.flags, 'valide') AND peremption >= NOW()
139 AND (e.promo_min = 0 || e.promo_min <= {?})
140 AND (e.promo_max = 0 || e.promo_max >= {?})
141 ORDER BY (e.promo_min != 0 AND e.promo_max != 0) DESC, e.peremption";
142 $page->assign('evenement_summary',
143 $globals->xdb->iterator($sql, Session::getInt('uid'),
144 $promo, $promo)
145 );
146
147 return PL_OK;
148 }
149
150 function handler_ev_submit(&$page)
74e0093f 151 {
152 global $globals;
153 $page->changeTpl('evenements.tpl');
154
155 $titre = Post::get('titre');
156 $texte = Post::get('texte');
157 $promo_min = Post::getInt('promo_min');
158 $promo_max = Post::getInt('promo_max');
159 $peremption = Post::getInt('peremption');
160 $valid_mesg = Post::get('valid_mesg');
161 $action = Post::get('action');
162
163 $page->assign('titre', $titre);
164 $page->assign('texte', $texte);
165 $page->assign('promo_min', $promo_min);
166 $page->assign('promo_max', $promo_max);
167 $page->assign('peremption', $peremption);
168 $page->assign('valid_mesg', $valid_mesg);
169 $page->assign('action', strtolower($action));
170
171 if ($action == 'Confirmer') {
172 $texte = preg_replace('/((http|ftp)+(s)?:\/\/[^<>\s]+)/i',
173 '<a href=\"\\0\">\\0</a>', $texte);
174 $texte = preg_replace('/([^,\s]+@[^,\s]+)/i',
175 '<a href=\"mailto:\\0\">\\0</a>', $texte);
176 require_once 'validations.inc.php';
177 $evtreq = new EvtReq($titre, $texte, $promo_min, $promo_max,
178 $peremption, $valid_mesg, Session::getInt('uid'));
179 $evtreq->submit();
180 $page->assign('ok', true);
181 }
182
183 $select = '';
184 for ($i = 1 ; $i < 30 ; $i++) {
185 $time = time() + 3600 * 24 * $i;
186 $p_stamp = date('Ymd', $time);
187 $year = date('Y', $time);
188 $month = date('m', $time);
189 $day = date('d', $time);
190
191 $select .= "<option value=\"$p_stamp\"";
192 if ($p_stamp == strtr($peremption, array("-" => ""))) {
193 $select .= " selected='selected'";
194 }
195 $select .= "> $day / $month / $year</option>\n";
196 }
197 $page->assign('select',$select);
198
199 return PL_OK;
200 }
b829e258 201
164ca57a 202 function handler_nl(&$page, $action = null)
203 {
204 require_once 'newsletter.inc.php';
205
206 $page->changeTpl('newsletter/index.tpl');
207 $page->assign('xorg_title','Polytechnique.org - Lettres mensuelles');
208
209 switch ($action) {
210 case 'out': unsubscribe_nl(); break;
211 case 'in': subscribe_nl(); break;
212 default: ;
213 }
214
215 $page->assign('nls', get_nl_state());
216 $page->assign_by_ref('nl_list', get_nl_list());
217
218 return PL_OK;
219 }
220
221 function handler_nl_show(&$page, $nid = 'last')
b829e258 222 {
223 $page->changeTpl('newsletter/show.tpl');
224
225 require_once 'newsletter.inc.php';
226
227 $nl = new NewsLetter($nid);
228 $page->assign_by_ref('nl', $nl);
229
230 if (Post::has('send')) {
231 $nl->sendTo(Session::get('prenom'), Session::get('nom'),
232 Session::get('bestalias'), Session::get('femme'),
233 Session::get('mail_fmt') != 'text');
234 }
235
236 return PL_OK;
237 }
164ca57a 238
239 function handler_nl_submit(&$page)
240 {
241 $page->changeTpl('newsletter/submit.tpl');
242
243 require_once 'newsletter.inc.php';
244
245 if (Post::has('see')) {
246 $art = new NLArticle(Post::get('title'), Post::get('body'), Post::get('append'));
247 $page->assign('art', $art);
248 } elseif (Post::has('valid')) {
249 require_once('validations.inc.php');
250 $art = new NLReq(Session::getInt('uid'), Post::get('title'),
251 Post::get('body'), Post::get('append'));
252 $art->submit();
253 $page->assign('submited', true);
254 }
255
256 return PL_OK;
257 }
74e0093f 258}
259
260?>