pending commit, finished during MQ/S download ...
[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')) {
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'));
cab08090 72 $notifs = new Notifs(S::v('uid'), true);
fc12cbd1 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 {
b48a0758 113 $page->changeTpl('carnet/notifs.tpl');
114
115 require_once 'notifs.inc.php';
116
cab08090 117 $watch = new Watch(S::v('uid'));
b48a0758 118
08cce2ff 119 $res = XDB::query("SELECT promo_sortie
b48a0758 120 FROM auth_user_md5
121 WHERE user_id = {?}",
cab08090 122 S::v('uid', -1));
b48a0758 123 $promo_sortie = $res->fetchOneCell();
124 $page->assign('promo_sortie', $promo_sortie);
125
126 switch ($action) {
127 case 'add_promo':
128 case 'del_promo':
129 $this->_handler_notifs_promos($page, $watch, $action, $arg);
130 break;
131
132 case 'del_nonins':
133 $watch->_nonins->del($arg);
134 break;
135
136 case 'add_nonins':
137 $watch->_nonins->add($arg);
138 break;
139 }
140
141 if (Env::has('subs')) $watch->_subs->update('sub');
142 if (Env::has('flags_contacts')) {
143 $watch->watch_contacts = Env::getBool('contacts');
144 $watch->saveFlags();
145 }
146 if (Env::has('flags_mail')) {
147 $watch->watch_mail = Env::getBool('mail');
148 $watch->saveFlags();
149 }
150
151 $page->assign_by_ref('watch', $watch);
b48a0758 152 }
153
154 function _get_list($offset, $limit) {
cab08090 155 $uid = S::v('uid');
08cce2ff 156 $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
b48a0758 157 $total = $res->fetchOneCell();
158
159 $order = Get::get('order');
160 $orders = Array(
161 'nom' => 'nom DESC, u.prenom, u.promo',
162 'promo' => 'promo DESC, nom, u.prenom',
163 'last' => 'u.date DESC, nom, u.prenom, promo');
164 if ($order != 'promo' && $order != 'last')
165 $order = 'nom';
166 $order = $orders[$order];
167 if (Get::get('inv') == '')
168 $order = str_replace(" DESC,", ",", $order);
169
08cce2ff 170 $res = XDB::query("
b48a0758 171 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
172 FROM contacts AS c
173 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
174 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
175 WHERE c.uid = {?}
176 ORDER BY $order
177 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
178 $list = $res->fetchAllAssoc();
179
180 return Array($total, $list);
181 }
182
183 function handler_contacts(&$page, $action = null)
184 {
b48a0758 185 $page->changeTpl('carnet/mescontacts.tpl');
186 require_once("applis.func.inc.php");
187 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
188
cab08090 189 $uid = S::v('uid');
b48a0758 190 $user = Env::get('user');
191
192 switch (Env::get('action')) {
193 case 'retirer':
194 if (is_numeric($user)) {
08cce2ff 195 if (XDB::execute('DELETE FROM contacts
b48a0758 196 WHERE uid = {?} AND contact = {?}',
197 $uid, $user))
198 {
199 $page->trig("Contact retiré !");
200 }
201 } else {
08cce2ff 202 if (XDB::execute(
b48a0758 203 'DELETE FROM contacts
204 USING contacts AS c
205 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
206 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
207 {
208 $page->trig("Contact retiré !");
209 }
210 }
211 break;
212
213 case 'ajouter':
214 require_once('user.func.inc.php');
215 if (($login = get_user_login($user)) !== false) {
08cce2ff 216 if (XDB::execute(
b48a0758 217 'INSERT INTO contacts (uid, contact)
218 SELECT {?}, id
219 FROM aliases
220 WHERE alias = {?}', $uid, $login))
221 {
222 $page->trig('Contact ajouté !');
223 } else {
224 $page->trig('Contact déjà dans la liste !');
225 }
226 }
227 }
228
229 if ($action == 'trombi') {
230 require_once 'trombi.inc.php';
231
232 $trombi = new Trombi(array($this, '_get_list'));
233 $trombi->setNbRows(4);
234 $page->assign_by_ref('trombi',$trombi);
235
236 $order = Get::get('order');
237 if ($order != 'promo' && $order != 'last')
238 $order = 'nom';
239 $page->assign('order', $order);
240 $page->assign('inv', Get::get('inv'));
241
242 } else {
243
244 $order = Get::get('order');
245 $orders = Array(
246 'nom' => 'sortkey DESC, a.prenom, a.promo',
247 'promo' => 'promo DESC, sortkey, a.prenom',
248 'last' => 'a.date DESC, sortkey, a.prenom, promo');
249 if ($order != 'promo' && $order != 'last')
250 $order = 'nom';
251 $page->assign('order', $order);
252 $page->assign('inv', Get::get('inv'));
253 $order = $orders[$order];
254 if (Get::get('inv') == '')
255 $order = str_replace(" DESC,", ",", $order);
256
257 $sql = "SELECT contact AS id,
258 a.*, l.alias AS forlife,
259 1 AS inscrit,
260 a.perms != 'pending' AS wasinscrit,
261 a.deces != 0 AS dcd, a.deces, a.matricule_ax,
262 FIND_IN_SET('femme', a.flags) AS sexe,
263 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
264 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
265 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
266 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
267 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
268 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey
269 FROM contacts AS c
270 INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact)
271 INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie')
272 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id)
273 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
274 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
275 LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2)
276 LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0)
277 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
278 LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1)
279 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
280 LEFT JOIN adresses AS adr ON (a.user_id = adr.uid
281 AND FIND_IN_SET('active', adr.statut))
282 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
283 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
284 WHERE c.uid = $uid
285 ORDER BY ".$order;
286
08cce2ff 287 $page->assign_by_ref('citer', XDB::iterator($sql));
b48a0758 288 }
b48a0758 289 }
290
291 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
292 {
b48a0758 293 require_once 'contacts.pdf.inc.php';
294 require_once 'user.func.inc.php';
295
296 session_write_close();
297
298 $sql = "SELECT a.alias
299 FROM aliases AS a
300 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
301 INNER JOIN contacts AS c ON ( a.id = c.contact )
302 WHERE c.uid = {?} AND a.type='a_vie'";
303 if ($arg0 == 'promo') {
304 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
305 } else {
306 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
307 }
308
cab08090 309 $citer = XDB::iterRow($sql, S::v('uid'));
b48a0758 310 $pdf = new ContactsPDF();
311
312 while (list($alias) = $citer->next()) {
313 $user = get_user_details($alias);
314 $pdf->addContact($user, $arg0 == 'photos' || $arg1 == 'photos');
315 }
316 $pdf->Output();
317
318 exit;
319 }
320
c9f82d49 321 function handler_rss(&$page, $user = null, $hash = null)
322 {
323 require_once 'rss.inc.php';
324 require_once 'notifs.inc.php';
0337d704 325
c9f82d49 326 $uid = init_rss('carnet/rss.tpl', $user, $hash);
327 $notifs = new Notifs($uid, false);
328 $page->assign('notifs', $notifs);
c9f82d49 329 }
fbfb06dc 330
331 function handler_ical(&$page, $user = null, $hash = null, $all = null)
332 {
fbfb06dc 333 new_nonhtml_page('carnet/calendar.tpl', AUTH_PUBLIC);
334
335 if ($alias && $hash) {
08cce2ff 336 $res = XDB::query(
fbfb06dc 337 'SELECT a.id
338 FROM aliases AS a
339 INNER JOIN auth_user_quick AS q ON ( a.id = q.user_id AND q.core_rss_hash = {?} )
340 WHERE a.alias = {?} AND a.type != "homonyme"', $hash, $alias);
341 $uid = $res->fetchOneCell();
342 }
343
344 require_once 'notifs.inc.php';
345 $notifs = new Notifs($uid, true);
346
347 $annivcat = false;
348 foreach ($notifs->_cats as $cat) {
349 if (preg_match('/anniv/i', $cat['short']))
350 $annivcat = $cat['id'];
351 }
352
353 if ($annivcat !== false) {
354 $annivs = array();
355 foreach ($notifs->_data[$annivcat] as $promo) {
356 foreach ($promo as $notif) {
357 if ($all == 'all' || $notif['contact']) {
358 $annivs[] = array(
359 'timestamp' => $notif['known'],
360 'date' => strtotime($notif['date']),
361 'tomorrow' => strtotime("+1 day", strtotime($notif['date'])),
362 'bestalias' => $notif['bestalias'],
363 'summary' => 'Anniversaire de '.$notif['prenom']
364 .' '.$notif['nom'].' - x '.$notif['promo'],
365 );
366 }
367 }
368 }
369 $page->assign('events', $annivs);
370 }
371
372 header('Content-Type: text/calendar; charset=utf-8');
fbfb06dc 373 }
4da0b8d7 374}
c9f82d49 375
376?>