we don't need diogenes session anymore
[platal.git] / modules / carnet.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 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
c9f82d49 22class CarnetModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
b48a0758 27 'carnet' => $this->make_hook('index', AUTH_COOKIE),
28 'carnet/panel' => $this->make_hook('panel', AUTH_COOKIE),
29 'carnet/notifs' => $this->make_hook('notifs', AUTH_COOKIE),
fc12cbd1 30
b48a0758 31 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE),
32 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE),
33
34 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC),
35 'carnet/ical' => $this->make_hook('ical', AUTH_PUBLIC),
c9f82d49 36 );
37 }
0337d704 38
fc12cbd1 39 function _add_rss_link(&$page)
40 {
fd8f77de 41 if (!Session::has('core_rss_hash')) {
fc12cbd1 42 return;
fd8f77de 43 }
fc12cbd1 44 $page->assign('xorg_rss',
45 array('title' => 'Polytechnique.org :: Carnet',
46 'href' => '/carnet/rss/'.Session::get('forlife')
47 .'/'.Session::get('core_rss_hash').'/rss.xml')
48 );
49 }
50
b48a0758 51 function handler_index(&$page)
52 {
53 $page->changeTpl('carnet/index.tpl');
54 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
55 $this->_add_rss_link($page);
b48a0758 56 }
57
fc12cbd1 58 function handler_panel(&$page)
59 {
60 $page->changeTpl('carnet/panel.tpl');
61
62 if (Get::has('read')) {
63 global $globals;
64
65 $_SESSION['watch_last'] = Get::get('read');
66 redirect($globals->baseurl.'/carnet/panel');
67 }
68
69 require_once 'notifs.inc.php';
70
71 $page->assign('now',date('YmdHis'));
72 $notifs = new Notifs(Session::getInt('uid'), true);
73
74 $page->assign('notifs', $notifs);
75 $page->assign('today', date('Y-m-d'));
76 $this->_add_rss_link($page);
fc12cbd1 77 }
78
b48a0758 79 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
80 {
81 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
82 $p = intval($matches[1]);
83 if($p<1900 || $p>2100) {
84 $page->trig("la promo entrée est invalide");
85 } else {
86 if ($action == 'add_promo') {
87 $watch->_promos->add($p);
88 } else {
89 $watch->_promos->del($p);
90 }
91 }
92 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
93 $p1 = intval($matches[1]);
94 $p2 = intval($matches[2]);
95 if($p1<1900 || $p1>2100) {
96 $page->trig('la première promo de la plage entrée est invalide');
97 } elseif($p2<1900 || $p2>2100) {
98 $page->trig('la seconde promo de la plage entrée est invalide');
99 } else {
100 if ($action == 'add_promo') {
101 $watch->_promos->addRange($p1, $p2);
102 } else {
103 $watch->_promos->delRange($p1, $p2);
104 }
105 }
106 } else {
107 $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect.");
108 }
109 }
110
111 function handler_notifs(&$page, $action = null, $arg = null)
112 {
113 global $globals;
114
115 $page->changeTpl('carnet/notifs.tpl');
116
117 require_once 'notifs.inc.php';
118
119 $watch = new Watch(Session::getInt('uid'));
120
121 $res = $globals->xdb->query("SELECT promo_sortie
122 FROM auth_user_md5
123 WHERE user_id = {?}",
124 Session::getInt('uid', -1));
125 $promo_sortie = $res->fetchOneCell();
126 $page->assign('promo_sortie', $promo_sortie);
127
128 switch ($action) {
129 case 'add_promo':
130 case 'del_promo':
131 $this->_handler_notifs_promos($page, $watch, $action, $arg);
132 break;
133
134 case 'del_nonins':
135 $watch->_nonins->del($arg);
136 break;
137
138 case 'add_nonins':
139 $watch->_nonins->add($arg);
140 break;
141 }
142
143 if (Env::has('subs')) $watch->_subs->update('sub');
144 if (Env::has('flags_contacts')) {
145 $watch->watch_contacts = Env::getBool('contacts');
146 $watch->saveFlags();
147 }
148 if (Env::has('flags_mail')) {
149 $watch->watch_mail = Env::getBool('mail');
150 $watch->saveFlags();
151 }
152
153 $page->assign_by_ref('watch', $watch);
b48a0758 154 }
155
156 function _get_list($offset, $limit) {
157 global $globals;
158 $uid = Session::getInt('uid');
159 $res = $globals->xdb->query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
160 $total = $res->fetchOneCell();
161
162 $order = Get::get('order');
163 $orders = Array(
164 'nom' => 'nom DESC, u.prenom, u.promo',
165 'promo' => 'promo DESC, nom, u.prenom',
166 'last' => 'u.date DESC, nom, u.prenom, promo');
167 if ($order != 'promo' && $order != 'last')
168 $order = 'nom';
169 $order = $orders[$order];
170 if (Get::get('inv') == '')
171 $order = str_replace(" DESC,", ",", $order);
172
173 $res = $globals->xdb->query("
174 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
175 FROM contacts AS c
176 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
177 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
178 WHERE c.uid = {?}
179 ORDER BY $order
180 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
181 $list = $res->fetchAllAssoc();
182
183 return Array($total, $list);
184 }
185
186 function handler_contacts(&$page, $action = null)
187 {
188 global $globals;
189
190 $page->changeTpl('carnet/mescontacts.tpl');
191 require_once("applis.func.inc.php");
192 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
193
194 $uid = Session::getInt('uid');
195 $user = Env::get('user');
196
197 switch (Env::get('action')) {
198 case 'retirer':
199 if (is_numeric($user)) {
200 if ($globals->xdb->execute('DELETE FROM contacts
201 WHERE uid = {?} AND contact = {?}',
202 $uid, $user))
203 {
204 $page->trig("Contact retiré !");
205 }
206 } else {
207 if ($globals->xdb->execute(
208 'DELETE FROM contacts
209 USING contacts AS c
210 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
211 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
212 {
213 $page->trig("Contact retiré !");
214 }
215 }
216 break;
217
218 case 'ajouter':
219 require_once('user.func.inc.php');
220 if (($login = get_user_login($user)) !== false) {
221 if ($globals->xdb->execute(
222 'INSERT INTO contacts (uid, contact)
223 SELECT {?}, id
224 FROM aliases
225 WHERE alias = {?}', $uid, $login))
226 {
227 $page->trig('Contact ajouté !');
228 } else {
229 $page->trig('Contact déjà dans la liste !');
230 }
231 }
232 }
233
234 if ($action == 'trombi') {
235 require_once 'trombi.inc.php';
236
237 $trombi = new Trombi(array($this, '_get_list'));
238 $trombi->setNbRows(4);
239 $page->assign_by_ref('trombi',$trombi);
240
241 $order = Get::get('order');
242 if ($order != 'promo' && $order != 'last')
243 $order = 'nom';
244 $page->assign('order', $order);
245 $page->assign('inv', Get::get('inv'));
246
247 } else {
248
249 $order = Get::get('order');
250 $orders = Array(
251 'nom' => 'sortkey DESC, a.prenom, a.promo',
252 'promo' => 'promo DESC, sortkey, a.prenom',
253 'last' => 'a.date DESC, sortkey, a.prenom, promo');
254 if ($order != 'promo' && $order != 'last')
255 $order = 'nom';
256 $page->assign('order', $order);
257 $page->assign('inv', Get::get('inv'));
258 $order = $orders[$order];
259 if (Get::get('inv') == '')
260 $order = str_replace(" DESC,", ",", $order);
261
262 $sql = "SELECT contact AS id,
263 a.*, l.alias AS forlife,
264 1 AS inscrit,
265 a.perms != 'pending' AS wasinscrit,
266 a.deces != 0 AS dcd, a.deces, a.matricule_ax,
267 FIND_IN_SET('femme', a.flags) AS sexe,
268 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
269 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
270 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
271 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
272 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
273 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey
274 FROM contacts AS c
275 INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact)
276 INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie')
277 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id)
278 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
279 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
280 LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2)
281 LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0)
282 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
283 LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1)
284 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
285 LEFT JOIN adresses AS adr ON (a.user_id = adr.uid
286 AND FIND_IN_SET('active', adr.statut))
287 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
288 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
289 WHERE c.uid = $uid
290 ORDER BY ".$order;
291
292 $page->assign_by_ref('citer', $globals->xdb->iterator($sql));
293 }
b48a0758 294 }
295
296 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
297 {
298 global $globals;
299
300 require_once 'contacts.pdf.inc.php';
301 require_once 'user.func.inc.php';
302
303 session_write_close();
304
305 $sql = "SELECT a.alias
306 FROM aliases AS a
307 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
308 INNER JOIN contacts AS c ON ( a.id = c.contact )
309 WHERE c.uid = {?} AND a.type='a_vie'";
310 if ($arg0 == 'promo') {
311 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
312 } else {
313 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
314 }
315
316 $citer = $globals->xdb->iterRow($sql, Session::getInt('uid'));
317 $pdf = new ContactsPDF();
318
319 while (list($alias) = $citer->next()) {
320 $user = get_user_details($alias);
321 $pdf->addContact($user, $arg0 == 'photos' || $arg1 == 'photos');
322 }
323 $pdf->Output();
324
325 exit;
326 }
327
c9f82d49 328 function handler_rss(&$page, $user = null, $hash = null)
329 {
330 require_once 'rss.inc.php';
331 require_once 'notifs.inc.php';
0337d704 332
c9f82d49 333 $uid = init_rss('carnet/rss.tpl', $user, $hash);
334 $notifs = new Notifs($uid, false);
335 $page->assign('notifs', $notifs);
c9f82d49 336 }
fbfb06dc 337
338 function handler_ical(&$page, $user = null, $hash = null, $all = null)
339 {
340 global $globals;
341
342 new_nonhtml_page('carnet/calendar.tpl', AUTH_PUBLIC);
343
344 if ($alias && $hash) {
345 $res = $globals->xdb->query(
346 'SELECT a.id
347 FROM aliases AS a
348 INNER JOIN auth_user_quick AS q ON ( a.id = q.user_id AND q.core_rss_hash = {?} )
349 WHERE a.alias = {?} AND a.type != "homonyme"', $hash, $alias);
350 $uid = $res->fetchOneCell();
351 }
352
353 require_once 'notifs.inc.php';
354 $notifs = new Notifs($uid, true);
355
356 $annivcat = false;
357 foreach ($notifs->_cats as $cat) {
358 if (preg_match('/anniv/i', $cat['short']))
359 $annivcat = $cat['id'];
360 }
361
362 if ($annivcat !== false) {
363 $annivs = array();
364 foreach ($notifs->_data[$annivcat] as $promo) {
365 foreach ($promo as $notif) {
366 if ($all == 'all' || $notif['contact']) {
367 $annivs[] = array(
368 'timestamp' => $notif['known'],
369 'date' => strtotime($notif['date']),
370 'tomorrow' => strtotime("+1 day", strtotime($notif['date'])),
371 'bestalias' => $notif['bestalias'],
372 'summary' => 'Anniversaire de '.$notif['prenom']
373 .' '.$notif['nom'].' - x '.$notif['promo'],
374 );
375 }
376 }
377 }
378 $page->assign('events', $annivs);
379 }
380
381 header('Content-Type: text/calendar; charset=utf-8');
fbfb06dc 382 }
4da0b8d7 383}
c9f82d49 384
385?>