Convert source code to UTF-8
[platal.git] / modules / carnet.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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
22 class CarnetModule extends PLModule
23 {
24 function handlers()
25 {
26 return array(
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),
30
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),
35
36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC),
37 );
38 }
39
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
47 function _add_rss_link(&$page)
48 {
49 if (!S::has('core_rss_hash')) {
50 return;
51 }
52 $page->setRssLink('Polytechnique.org :: Carnet',
53 '/carnet/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
54 }
55
56 function handler_index(&$page)
57 {
58 $page->changeTpl('carnet/index.tpl');
59 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
60 $this->_add_rss_link($page);
61 }
62
63 function handler_panel(&$page)
64 {
65 $page->changeTpl('carnet/panel.tpl');
66
67 if (Get::has('read')) {
68 $_SESSION['watch_last'] = Get::v('read');
69 pl_redirect('carnet/panel');
70 }
71
72 require_once 'notifs.inc.php';
73
74 $page->assign('now',date('YmdHis'));
75 $notifs = new Notifs(S::v('uid'), true);
76
77 $page->assign('notifs', $notifs);
78 $page->assign('today', date('Y-m-d'));
79 $this->_add_rss_link($page);
80 }
81
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 $page->changeTpl('carnet/notifs.tpl');
117
118 require_once 'notifs.inc.php';
119
120 $watch = new Watch(S::v('uid'));
121
122 $res = XDB::query("SELECT promo_sortie
123 FROM auth_user_md5
124 WHERE user_id = {?}",
125 S::v('uid', -1));
126 $promo_sortie = $res->fetchOneCell();
127 $page->assign('promo_sortie', $promo_sortie);
128
129 switch ($action) {
130 case 'add_promo':
131 case 'del_promo':
132 $this->_handler_notifs_promos($page, $watch, $action, $arg);
133 break;
134
135 case 'del_nonins':
136 $watch->_nonins->del($arg);
137 break;
138
139 case 'add_nonins':
140 $watch->_nonins->add($arg);
141 break;
142 }
143
144 if (Env::has('subs')) $watch->_subs->update('sub');
145 if (Env::has('flags_contacts')) {
146 $watch->watch_contacts = Env::b('contacts');
147 $watch->saveFlags();
148 }
149 if (Env::has('flags_mail')) {
150 $watch->watch_mail = Env::b('mail');
151 $watch->saveFlags();
152 }
153
154 $page->assign_by_ref('watch', $watch);
155 }
156
157 function _get_list($offset, $limit) {
158 $uid = S::v('uid');
159 $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
160 $total = $res->fetchOneCell();
161
162 $order = Get::v('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::v('inv') == '')
171 $order = str_replace(" DESC,", ",", $order);
172
173 $res = 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 $page->changeTpl('carnet/mescontacts.tpl');
189 require_once("applis.func.inc.php");
190 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
191 $this->_add_rss_link($page);
192
193 $uid = S::v('uid');
194 $user = Env::v('user');
195
196 switch (Env::v('action')) {
197 case 'retirer':
198 if (is_numeric($user)) {
199 if (XDB::execute('DELETE FROM contacts
200 WHERE uid = {?} AND contact = {?}',
201 $uid, $user))
202 {
203 $page->trig("Contact retiré !");
204 }
205 } else {
206 if (XDB::execute(
207 'DELETE FROM contacts
208 USING contacts AS c
209 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
210 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
211 {
212 $page->trig("Contact retiré !");
213 }
214 }
215 break;
216
217 case 'ajouter':
218 require_once('user.func.inc.php');
219 if (($login = get_user_login($user)) !== false) {
220 if (XDB::execute(
221 'INSERT INTO contacts (uid, contact)
222 SELECT {?}, id
223 FROM aliases
224 WHERE alias = {?}', $uid, $login))
225 {
226 $page->trig('Contact ajouté !');
227 } else {
228 $page->trig('Contact déjà dans la liste !');
229 }
230 }
231 }
232
233 if ($action == 'trombi') {
234 $trombi = new Trombi(array($this, '_get_list'));
235 $trombi->setNbRows(4);
236 $page->assign_by_ref('trombi',$trombi);
237
238 $order = Get::v('order');
239 if ($order != 'promo' && $order != 'last')
240 $order = 'nom';
241 $page->assign('order', $order);
242 $page->assign('inv', Get::v('inv'));
243
244 } else {
245
246 $order = Get::v('order');
247 $orders = Array(
248 'nom' => 'sortkey DESC, a.prenom, a.promo',
249 'promo' => 'promo DESC, sortkey, a.prenom',
250 'last' => 'a.date DESC, sortkey, a.prenom, promo');
251 if ($order != 'promo' && $order != 'last')
252 $order = 'nom';
253 $page->assign('order', $order);
254 $page->assign('inv', Get::v('inv'));
255 $order = $orders[$order];
256 if (Get::v('inv') == '')
257 $order = str_replace(" DESC,", ",", $order);
258
259 $sql = "SELECT contact AS id,
260 a.*, l.alias AS forlife,
261 1 AS inscrit,
262 a.perms != 'pending' AS wasinscrit,
263 a.deces != 0 AS dcd, a.deces, a.matricule_ax,
264 FIND_IN_SET('femme', a.flags) AS sexe,
265 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
266 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
267 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
268 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
269 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
270 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey,
271 COUNT(em.email) > 0 AS actif
272 FROM contacts AS c
273 INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact)
274 INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie')
275 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id)
276 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
277 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
278 LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2)
279 LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0)
280 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
281 LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1)
282 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
283 LEFT JOIN adresses AS adr ON (a.user_id = adr.uid
284 AND FIND_IN_SET('active', adr.statut))
285 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
286 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
287 LEFT JOIN emails AS em ON (em.uid = a.user_id AND em.flags = 'active')
288 WHERE c.uid = $uid
289 GROUP BY a.user_id
290 ORDER BY ".$order;
291
292 $page->assign('citer', XDB::iterator($sql));
293 }
294 }
295
296 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
297 {
298 require_once dirname(__FILE__).'/carnet/contacts.pdf.inc.php';
299 require_once 'user.func.inc.php';
300
301 session_write_close();
302
303 $sql = "SELECT a.alias
304 FROM aliases AS a
305 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
306 INNER JOIN contacts AS c ON ( a.id = c.contact )
307 WHERE c.uid = {?} AND a.type='a_vie'";
308 if ($arg0 == 'promo') {
309 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
310 } else {
311 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
312 }
313
314 $citer = XDB::iterRow($sql, S::v('uid'));
315 $pdf = new ContactsPDF();
316
317 while (list($alias) = $citer->next()) {
318 $user = get_user_details($alias);
319 $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos');
320 }
321 $pdf->Output();
322
323 exit;
324 }
325
326 function handler_rss(&$page, $user = null, $hash = null)
327 {
328 require_once 'rss.inc.php';
329 require_once 'notifs.inc.php';
330
331 $uid = init_rss('carnet/rss.tpl', $user, $hash);
332 $notifs = new Notifs($uid, false);
333 $page->assign('notifs', $notifs);
334 }
335
336 function handler_ical(&$page)
337 {
338 require_once 'ical.inc.php';
339 $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
340 $page->register_function('display_ical', 'display_ical');
341
342 $res = XDB::iterRow(
343 'SELECT u.prenom,
344 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
345 u.promo,
346 u.naissance,
347 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
348 u.date_ins,
349 a.alias AS forlife
350 FROM contacts AS c
351 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
352 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
353 WHERE c.uid = {?}', S::v('uid'));
354
355 $annivs = Array();
356 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
357 $naissance = str_replace('-', '', $naissance);
358 $end = str_replace('-', '', $end);
359 $annivs[] = array(
360 'timestamp' => strtotime($ts),
361 'date' => $naissance,
362 'tomorrow' => $end,
363 'forlife' => $forlife,
364 'summary' => 'Anniversaire de '.$prenom
365 .' '.$nom.' - x '.$promo,
366 );
367 }
368 $page->assign('events', $annivs);
369
370 header('Content-Type: text/calendar; charset=utf-8');
371 }
372
373 function handler_vcard(&$page, $photos = null)
374 {
375 $res = XDB::query('SELECT contact
376 FROM contacts
377 WHERE uid = {?}', S::v('uid'));
378
379 require_once('vcard.inc.php');
380 $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
381 $vcard->do_page(&$page);
382 }
383 }
384
385 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
386 ?>