Change default: show menu in xnet pages.
[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(
5e193297 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
5e193297 31 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE),
32 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE),
33 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_COOKIE),
34 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE),
b48a0758 35
5e193297 36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC),
c9f82d49 37 );
38 }
0337d704 39
8d8f7607 40 function on_subscribe($forlife, $uid, $promo, $password)
41 {
42 require_once 'notifs.inc.php';
43 register_watch_op($uid, WATCH_INSCR);
44 inscription_notifs_base($uid);
45 }
46
fc12cbd1 47 function _add_rss_link(&$page)
48 {
cab08090 49 if (!S::has('core_rss_hash')) {
fc12cbd1 50 return;
fd8f77de 51 }
fc12cbd1 52 $page->assign('xorg_rss',
53 array('title' => 'Polytechnique.org :: Carnet',
cab08090 54 'href' => '/carnet/rss/'.S::v('forlife')
55 .'/'.S::v('core_rss_hash').'/rss.xml')
fc12cbd1 56 );
57 }
58
b48a0758 59 function handler_index(&$page)
60 {
61 $page->changeTpl('carnet/index.tpl');
62 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
63 $this->_add_rss_link($page);
b48a0758 64 }
65
fc12cbd1 66 function handler_panel(&$page)
67 {
68 $page->changeTpl('carnet/panel.tpl');
69
70 if (Get::has('read')) {
5e2307dc 71 $_SESSION['watch_last'] = Get::v('read');
8b00e0e0 72 pl_redirect('carnet/panel');
fc12cbd1 73 }
74
75 require_once 'notifs.inc.php';
76
77 $page->assign('now',date('YmdHis'));
cab08090 78 $notifs = new Notifs(S::v('uid'), true);
fc12cbd1 79
80 $page->assign('notifs', $notifs);
81 $page->assign('today', date('Y-m-d'));
82 $this->_add_rss_link($page);
fc12cbd1 83 }
84
b48a0758 85 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
86 {
87 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
88 $p = intval($matches[1]);
89 if($p<1900 || $p>2100) {
089a5801 90 $page->trig("la promo entrée est invalide");
b48a0758 91 } else {
92 if ($action == 'add_promo') {
93 $watch->_promos->add($p);
94 } else {
95 $watch->_promos->del($p);
96 }
97 }
98 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
99 $p1 = intval($matches[1]);
100 $p2 = intval($matches[2]);
101 if($p1<1900 || $p1>2100) {
089a5801 102 $page->trig('la première promo de la plage entrée est invalide');
b48a0758 103 } elseif($p2<1900 || $p2>2100) {
089a5801 104 $page->trig('la seconde promo de la plage entrée est invalide');
b48a0758 105 } else {
106 if ($action == 'add_promo') {
107 $watch->_promos->addRange($p1, $p2);
108 } else {
109 $watch->_promos->delRange($p1, $p2);
110 }
111 }
112 } else {
089a5801 113 $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect.");
b48a0758 114 }
115 }
116
117 function handler_notifs(&$page, $action = null, $arg = null)
118 {
b48a0758 119 $page->changeTpl('carnet/notifs.tpl');
120
121 require_once 'notifs.inc.php';
122
cab08090 123 $watch = new Watch(S::v('uid'));
b48a0758 124
08cce2ff 125 $res = XDB::query("SELECT promo_sortie
b48a0758 126 FROM auth_user_md5
127 WHERE user_id = {?}",
cab08090 128 S::v('uid', -1));
b48a0758 129 $promo_sortie = $res->fetchOneCell();
130 $page->assign('promo_sortie', $promo_sortie);
131
132 switch ($action) {
133 case 'add_promo':
134 case 'del_promo':
135 $this->_handler_notifs_promos($page, $watch, $action, $arg);
136 break;
137
138 case 'del_nonins':
139 $watch->_nonins->del($arg);
140 break;
141
142 case 'add_nonins':
143 $watch->_nonins->add($arg);
144 break;
145 }
146
147 if (Env::has('subs')) $watch->_subs->update('sub');
148 if (Env::has('flags_contacts')) {
5e2307dc 149 $watch->watch_contacts = Env::b('contacts');
b48a0758 150 $watch->saveFlags();
151 }
152 if (Env::has('flags_mail')) {
5e2307dc 153 $watch->watch_mail = Env::b('mail');
b48a0758 154 $watch->saveFlags();
155 }
156
157 $page->assign_by_ref('watch', $watch);
b48a0758 158 }
159
160 function _get_list($offset, $limit) {
cab08090 161 $uid = S::v('uid');
08cce2ff 162 $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
b48a0758 163 $total = $res->fetchOneCell();
164
5e2307dc 165 $order = Get::v('order');
b48a0758 166 $orders = Array(
167 'nom' => 'nom DESC, u.prenom, u.promo',
168 'promo' => 'promo DESC, nom, u.prenom',
169 'last' => 'u.date DESC, nom, u.prenom, promo');
170 if ($order != 'promo' && $order != 'last')
171 $order = 'nom';
172 $order = $orders[$order];
5e2307dc 173 if (Get::v('inv') == '')
b48a0758 174 $order = str_replace(" DESC,", ",", $order);
175
08cce2ff 176 $res = XDB::query("
b48a0758 177 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
178 FROM contacts AS c
179 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
180 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
181 WHERE c.uid = {?}
182 ORDER BY $order
183 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
184 $list = $res->fetchAllAssoc();
185
186 return Array($total, $list);
187 }
188
189 function handler_contacts(&$page, $action = null)
190 {
b48a0758 191 $page->changeTpl('carnet/mescontacts.tpl');
192 require_once("applis.func.inc.php");
193 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
194
cab08090 195 $uid = S::v('uid');
5e2307dc 196 $user = Env::v('user');
b48a0758 197
5e2307dc 198 switch (Env::v('action')) {
b48a0758 199 case 'retirer':
200 if (is_numeric($user)) {
08cce2ff 201 if (XDB::execute('DELETE FROM contacts
b48a0758 202 WHERE uid = {?} AND contact = {?}',
203 $uid, $user))
204 {
089a5801 205 $page->trig("Contact retiré !");
b48a0758 206 }
207 } else {
08cce2ff 208 if (XDB::execute(
b48a0758 209 'DELETE FROM contacts
210 USING contacts AS c
211 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
212 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
213 {
089a5801 214 $page->trig("Contact retiré !");
b48a0758 215 }
216 }
217 break;
218
219 case 'ajouter':
220 require_once('user.func.inc.php');
221 if (($login = get_user_login($user)) !== false) {
08cce2ff 222 if (XDB::execute(
b48a0758 223 'INSERT INTO contacts (uid, contact)
224 SELECT {?}, id
225 FROM aliases
226 WHERE alias = {?}', $uid, $login))
227 {
089a5801 228 $page->trig('Contact ajouté !');
b48a0758 229 } else {
089a5801 230 $page->trig('Contact déjà dans la liste !');
b48a0758 231 }
232 }
233 }
234
235 if ($action == 'trombi') {
b48a0758 236 $trombi = new Trombi(array($this, '_get_list'));
237 $trombi->setNbRows(4);
238 $page->assign_by_ref('trombi',$trombi);
239
5e2307dc 240 $order = Get::v('order');
b48a0758 241 if ($order != 'promo' && $order != 'last')
242 $order = 'nom';
243 $page->assign('order', $order);
5e2307dc 244 $page->assign('inv', Get::v('inv'));
b48a0758 245
246 } else {
247
5e2307dc 248 $order = Get::v('order');
b48a0758 249 $orders = Array(
250 'nom' => 'sortkey DESC, a.prenom, a.promo',
251 'promo' => 'promo DESC, sortkey, a.prenom',
252 'last' => 'a.date DESC, sortkey, a.prenom, promo');
253 if ($order != 'promo' && $order != 'last')
254 $order = 'nom';
255 $page->assign('order', $order);
5e2307dc 256 $page->assign('inv', Get::v('inv'));
b48a0758 257 $order = $orders[$order];
5e2307dc 258 if (Get::v('inv') == '')
b48a0758 259 $order = str_replace(" DESC,", ",", $order);
260
261 $sql = "SELECT contact AS id,
262 a.*, l.alias AS forlife,
263 1 AS inscrit,
264 a.perms != 'pending' AS wasinscrit,
265 a.deces != 0 AS dcd, a.deces, a.matricule_ax,
266 FIND_IN_SET('femme', a.flags) AS sexe,
267 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
268 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
269 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
270 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
271 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
e0210288 272 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey,
273 COUNT(em.email) > 0 AS actif
b48a0758 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)
e0210288 289 LEFT JOIN emails AS em ON (em.uid = a.user_id AND em.flags = 'active')
b48a0758 290 WHERE c.uid = $uid
e0210288 291 GROUP BY a.user_id
b48a0758 292 ORDER BY ".$order;
293
cfa54940 294 $page->assign('citer', XDB::iterator($sql));
b48a0758 295 }
b48a0758 296 }
297
298 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
299 {
ee5990c7 300 require_once dirname(__FILE__).'/carnet/contacts.pdf.inc.php';
b48a0758 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
cab08090 316 $citer = XDB::iterRow($sql, S::v('uid'));
b48a0758 317 $pdf = new ContactsPDF();
318
319 while (list($alias) = $citer->next()) {
320 $user = get_user_details($alias);
93c099e1 321 $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos');
b48a0758 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
62e6d100 338 function handler_ical(&$page)
fbfb06dc 339 {
99544d53 340 require_once 'ical.inc.php';
801fcad8 341 $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
3585b0c8 342 $page->register_function('display_ical', 'display_ical');
fbfb06dc 343
f3b3f363 344 $res = XDB::iterRow(
345 'SELECT u.prenom,
346 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
347 u.promo,
348 u.naissance,
349 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
350 u.date_ins,
351 a.alias AS forlife
352 FROM contacts AS c
353 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
354 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
355 WHERE c.uid = {?}', S::v('uid'));
356
357 $annivs = Array();
358 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
359 $naissance = str_replace('-', '', $naissance);
360 $end = str_replace('-', '', $end);
361 $annivs[] = array(
362 'timestamp' => strtotime($ts),
363 'date' => $naissance,
364 'tomorrow' => $end,
365 'forlife' => $forlife,
366 'summary' => 'Anniversaire de '.$prenom
367 .' '.$nom.' - x '.$promo,
368 );
fbfb06dc 369 }
f3b3f363 370 $page->assign('events', $annivs);
fbfb06dc 371
372 header('Content-Type: text/calendar; charset=utf-8');
fbfb06dc 373 }
5e193297 374
917c4d11 375 function handler_vcard(&$page, $photos = null)
5e193297 376 {
377 $res = XDB::query('SELECT contact
378 FROM contacts
379 WHERE uid = {?}', S::v('uid'));
917c4d11 380
5e193297 381 require_once('vcard.inc.php');
917c4d11 382 $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
5e193297 383 $vcard->do_page(&$page);
384 }
4da0b8d7 385}
c9f82d49 386
387?>