Merge commit 'origin/master' into hruid
[platal.git] / modules / carnet.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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),
8fc4efa3 32 'carnet/contacts/pdf' => $this->make_hook('pdf', AUTH_COOKIE, 'user', NO_HTTPS),
33 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_PUBLIC, 'user', NO_HTTPS),
34 'carnet/contacts/vcard' => $this->make_hook('vcard', AUTH_COOKIE, 'user', NO_HTTPS),
b48a0758 35
8fc4efa3 36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC, 'user', NO_HTTPS),
c9f82d49 37 );
38 }
0337d704 39
fc12cbd1 40 function _add_rss_link(&$page)
41 {
cab08090 42 if (!S::has('core_rss_hash')) {
fc12cbd1 43 return;
fd8f77de 44 }
162370e7 45 $page->setRssLink('Polytechnique.org :: Carnet',
46 '/carnet/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml');
fc12cbd1 47 }
48
b48a0758 49 function handler_index(&$page)
50 {
51 $page->changeTpl('carnet/index.tpl');
46f272fe 52 $page->setTitle('Mon carnet');
b48a0758 53 $this->_add_rss_link($page);
b48a0758 54 }
55
fc12cbd1 56 function handler_panel(&$page)
57 {
58 $page->changeTpl('carnet/panel.tpl');
59
60 if (Get::has('read')) {
ebfdf077
FB
61 S::set('watch_last', Get::v('read'));
62 Platal::session()->updateNbNotifs();
8b00e0e0 63 pl_redirect('carnet/panel');
fc12cbd1 64 }
65
66 require_once 'notifs.inc.php';
67
68 $page->assign('now',date('YmdHis'));
cab08090 69 $notifs = new Notifs(S::v('uid'), true);
fc12cbd1 70
71 $page->assign('notifs', $notifs);
72 $page->assign('today', date('Y-m-d'));
73 $this->_add_rss_link($page);
fc12cbd1 74 }
75
b48a0758 76 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
77 {
78 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
79 $p = intval($matches[1]);
80 if($p<1900 || $p>2100) {
a7d35093 81 $page->trigError("la promo entrée est invalide");
b48a0758 82 } else {
83 if ($action == 'add_promo') {
84 $watch->_promos->add($p);
85 } else {
86 $watch->_promos->del($p);
87 }
88 }
89 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
90 $p1 = intval($matches[1]);
91 $p2 = intval($matches[2]);
92 if($p1<1900 || $p1>2100) {
a7d35093 93 $page->trigError('la première promo de la plage entrée est invalide');
b48a0758 94 } elseif($p2<1900 || $p2>2100) {
a7d35093 95 $page->trigError('la seconde promo de la plage entrée est invalide');
b48a0758 96 } else {
97 if ($action == 'add_promo') {
98 $watch->_promos->addRange($p1, $p2);
99 } else {
100 $watch->_promos->delRange($p1, $p2);
101 }
102 }
103 } else {
a7d35093 104 $page->trigError("La promo (ou la plage de promo) entrée est dans un format incorrect.");
b48a0758 105 }
106 }
107
108 function handler_notifs(&$page, $action = null, $arg = null)
109 {
b48a0758 110 $page->changeTpl('carnet/notifs.tpl');
111
112 require_once 'notifs.inc.php';
113
cab08090 114 $watch = new Watch(S::v('uid'));
b48a0758 115
08cce2ff 116 $res = XDB::query("SELECT promo_sortie
b48a0758 117 FROM auth_user_md5
118 WHERE user_id = {?}",
cab08090 119 S::v('uid', -1));
b48a0758 120 $promo_sortie = $res->fetchOneCell();
121 $page->assign('promo_sortie', $promo_sortie);
122
40d428d8
VZ
123 if ($action) {
124 S::assert_xsrf_token();
e6bf9216 125 }
b48a0758 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
40d428d8
VZ
141 if (Env::has('subs')) {
142 S::assert_xsrf_token();
e6bf9216 143 $watch->_subs->update('sub');
e6bf9216
VZ
144 }
145
40d428d8
VZ
146 if (Env::has('flags_contacts')) {
147 S::assert_xsrf_token();
5e2307dc 148 $watch->watch_contacts = Env::b('contacts');
b48a0758 149 $watch->saveFlags();
150 }
e6bf9216 151
40d428d8
VZ
152 if (Env::has('flags_mail')) {
153 S::assert_xsrf_token();
e6bf9216 154 $watch->watch_mail = Env::b('mail');
b48a0758 155 $watch->saveFlags();
156 }
157
158 $page->assign_by_ref('watch', $watch);
b48a0758 159 }
160
161 function _get_list($offset, $limit) {
cab08090 162 $uid = S::v('uid');
08cce2ff 163 $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
b48a0758 164 $total = $res->fetchOneCell();
165
5e2307dc 166 $order = Get::v('order');
b48a0758 167 $orders = Array(
168 'nom' => 'nom DESC, u.prenom, u.promo',
169 'promo' => 'promo DESC, nom, u.prenom',
170 'last' => 'u.date DESC, nom, u.prenom, promo');
171 if ($order != 'promo' && $order != 'last')
172 $order = 'nom';
173 $order = $orders[$order];
5e2307dc 174 if (Get::v('inv') == '')
b48a0758 175 $order = str_replace(" DESC,", ",", $order);
176
08cce2ff 177 $res = XDB::query("
b48a0758 178 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
179 FROM contacts AS c
180 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
181 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
182 WHERE c.uid = {?}
183 ORDER BY $order
184 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
185 $list = $res->fetchAllAssoc();
186
187 return Array($total, $list);
188 }
189
3b2f9d11 190 function searchErrorHandler($explain) {
d7610c35 191 $page =& Platal::page();
a7d35093 192 $page->trigError($explain);
3b2f9d11 193 $this->handler_contacts($page);
194 }
195
a2aa8436 196 function handler_contacts(&$page, $action = null, $subaction = null, $ssaction = null)
b48a0758 197 {
46f272fe 198 $page->setTitle('Mes contacts');
59a61432 199 $this->_add_rss_link($page);
b48a0758 200
cab08090 201 $uid = S::v('uid');
5e2307dc 202 $user = Env::v('user');
b48a0758 203
e6bf9216
VZ
204 // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
205 // It allows direct linking to contact adding in the RSS feed.
40d428d8
VZ
206 if (Env::v('action') && Env::v('token') !== S::v('core_rss_hash')) {
207 S::assert_xsrf_token();
208 }
209 switch (Env::v('action')) {
210 case 'retirer':
d6460651
VZ
211 if (($user = User::get(Env::v('user')))) {
212 if (XDB::execute("DELETE FROM contacts
213 WHERE uid = {?} AND contact = {?}", $uid, $user->id())) {
a7d35093 214 $page->trigSuccess("Contact retiré !");
b48a0758 215 }
216 }
217 break;
218
40d428d8 219 case 'ajouter':
d6460651
VZ
220 if (($user = User::get(Env::v('user')))) {
221 if (XDB::execute("REPLACE INTO contacts (uid, contact)
222 VALUES ({?}, {?})", $uid, $user->id())) {
a7d35093 223 $page->trigSuccess('Contact ajouté !');
b48a0758 224 } else {
a7d35093 225 $page->trigWarning('Contact déjà dans la liste !');
b48a0758 226 }
227 }
d6460651 228 break;
b48a0758 229 }
230
a2aa8436 231 $search = false;
232 if ($action == 'search') {
233 $action = $subaction;
234 $subaction = $ssaction;
235 $search = true;
236 }
237 if ($search && trim(Env::v('quick'))) {
238 require_once 'userset.inc.php';
239 $base = 'carnet/contacts/search';
3b2f9d11 240
adb07f6f 241 Platal::load('search', 'classes.inc.php');
3b2f9d11 242 ThrowError::$throwHook = array($this, 'searchErrorHandler');
243 $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
a2aa8436 244 } else {
245 $base = 'carnet/contacts';
246 $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
247 }
1fae7605 248 $view->addMod('minifiche', 'Mini-fiches', true);
8c4a0c30 249 $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true));
a2aa8436 250 $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
251 $view->apply($base, $page, $action, $subaction);
252 if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
8c4a0c30 253 $page->changeTpl('carnet/mescontacts.tpl');
b48a0758 254 }
b48a0758 255 }
256
257 function handler_pdf(&$page, $arg0 = null, $arg1 = null)
258 {
460d8f55 259 $this->load('contacts.pdf.inc.php');
b48a0758 260 require_once 'user.func.inc.php';
261
732e5855 262 Platal::session()->close();
b48a0758 263
264 $sql = "SELECT a.alias
265 FROM aliases AS a
266 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
267 INNER JOIN contacts AS c ON ( a.id = c.contact )
268 WHERE c.uid = {?} AND a.type='a_vie'";
269 if ($arg0 == 'promo') {
270 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
271 } else {
272 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
273 }
274
cab08090 275 $citer = XDB::iterRow($sql, S::v('uid'));
b48a0758 276 $pdf = new ContactsPDF();
277
278 while (list($alias) = $citer->next()) {
279 $user = get_user_details($alias);
ac73e294 280 foreach ($user as &$value) {
281 if (is_utf8($value)) {
282 $value = utf8_decode($value);
283 }
284 }
93c099e1 285 $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos');
b48a0758 286 }
287 $pdf->Output();
288
289 exit;
290 }
291
c9f82d49 292 function handler_rss(&$page, $user = null, $hash = null)
293 {
460d8f55 294 $this->load('feed.inc.php');
4e9a7a6d
FB
295 $feed = new CarnetFeed();
296 return $feed->run($page, $user, $hash);
c9f82d49 297 }
fbfb06dc 298
963c0b2e 299 function handler_ical(&$page, $alias = null, $hash = null)
fbfb06dc 300 {
963c0b2e 301 require_once 'rss.inc.php';
302 $uid = init_rss(null, $alias, $hash, false);
303 if (S::logged()) {
304 if (!$uid) {
305 $uid = S::i('uid');
306 } else if ($uid != S::i('uid')) {
963c0b2e 307 send_warning_email("Récupération d\'un autre utilisateur ($uid)");
308 }
309 } else if (!$uid) {
310 exit;
311 }
99544d53 312 require_once 'ical.inc.php';
801fcad8 313 $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
3585b0c8 314 $page->register_function('display_ical', 'display_ical');
fbfb06dc 315
f3b3f363 316 $res = XDB::iterRow(
317 'SELECT u.prenom,
318 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
319 u.promo,
320 u.naissance,
321 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
322 u.date_ins,
323 a.alias AS forlife
324 FROM contacts AS c
325 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
326 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
963c0b2e 327 WHERE c.uid = {?}', $uid);
f3b3f363 328
329 $annivs = Array();
330 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
331 $naissance = str_replace('-', '', $naissance);
332 $end = str_replace('-', '', $end);
333 $annivs[] = array(
334 'timestamp' => strtotime($ts),
335 'date' => $naissance,
336 'tomorrow' => $end,
337 'forlife' => $forlife,
338 'summary' => 'Anniversaire de '.$prenom
339 .' '.$nom.' - x '.$promo,
340 );
fbfb06dc 341 }
f3b3f363 342 $page->assign('events', $annivs);
fbfb06dc 343
344 header('Content-Type: text/calendar; charset=utf-8');
fbfb06dc 345 }
5e193297 346
917c4d11 347 function handler_vcard(&$page, $photos = null)
5e193297 348 {
349 $res = XDB::query('SELECT contact
350 FROM contacts
351 WHERE uid = {?}', S::v('uid'));
5d42c993
FB
352 $vcard = new VCard($photos == 'photos');
353 $vcard->addUsers($res->fetchColumn());
354 $vcard->show();
5e193297 355 }
4da0b8d7 356}
c9f82d49 357
a7de4ef7 358// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
c9f82d49 359?>