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