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