0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 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 |
22 | class 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), |
963c0b2e |
33 | 'carnet/contacts/ical' => $this->make_hook('ical', AUTH_PUBLIC), |
5e193297 |
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 | } |
162370e7 |
52 | $page->setRssLink('Polytechnique.org :: Carnet', |
53 | '/carnet/rss/'.S::v('forlife') .'/'.S::v('core_rss_hash').'/rss.xml'); |
fc12cbd1 |
54 | } |
55 | |
b48a0758 |
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); |
b48a0758 |
61 | } |
62 | |
fc12cbd1 |
63 | function handler_panel(&$page) |
64 | { |
65 | $page->changeTpl('carnet/panel.tpl'); |
66 | |
67 | if (Get::has('read')) { |
5e2307dc |
68 | $_SESSION['watch_last'] = Get::v('read'); |
8b00e0e0 |
69 | pl_redirect('carnet/panel'); |
fc12cbd1 |
70 | } |
71 | |
72 | require_once 'notifs.inc.php'; |
73 | |
74 | $page->assign('now',date('YmdHis')); |
cab08090 |
75 | $notifs = new Notifs(S::v('uid'), true); |
fc12cbd1 |
76 | |
77 | $page->assign('notifs', $notifs); |
78 | $page->assign('today', date('Y-m-d')); |
79 | $this->_add_rss_link($page); |
fc12cbd1 |
80 | } |
81 | |
b48a0758 |
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) { |
a7de4ef7 |
87 | $page->trig("la promo entrée est invalide"); |
b48a0758 |
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) { |
a7de4ef7 |
99 | $page->trig('la première promo de la plage entrée est invalide'); |
b48a0758 |
100 | } elseif($p2<1900 || $p2>2100) { |
a7de4ef7 |
101 | $page->trig('la seconde promo de la plage entrée est invalide'); |
b48a0758 |
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 { |
a7de4ef7 |
110 | $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect."); |
b48a0758 |
111 | } |
112 | } |
113 | |
114 | function handler_notifs(&$page, $action = null, $arg = null) |
115 | { |
b48a0758 |
116 | $page->changeTpl('carnet/notifs.tpl'); |
117 | |
118 | require_once 'notifs.inc.php'; |
119 | |
cab08090 |
120 | $watch = new Watch(S::v('uid')); |
b48a0758 |
121 | |
08cce2ff |
122 | $res = XDB::query("SELECT promo_sortie |
b48a0758 |
123 | FROM auth_user_md5 |
124 | WHERE user_id = {?}", |
cab08090 |
125 | S::v('uid', -1)); |
b48a0758 |
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')) { |
5e2307dc |
146 | $watch->watch_contacts = Env::b('contacts'); |
b48a0758 |
147 | $watch->saveFlags(); |
148 | } |
149 | if (Env::has('flags_mail')) { |
5e2307dc |
150 | $watch->watch_mail = Env::b('mail'); |
b48a0758 |
151 | $watch->saveFlags(); |
152 | } |
153 | |
154 | $page->assign_by_ref('watch', $watch); |
b48a0758 |
155 | } |
156 | |
157 | function _get_list($offset, $limit) { |
cab08090 |
158 | $uid = S::v('uid'); |
08cce2ff |
159 | $res = XDB::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid); |
b48a0758 |
160 | $total = $res->fetchOneCell(); |
161 | |
5e2307dc |
162 | $order = Get::v('order'); |
b48a0758 |
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]; |
5e2307dc |
170 | if (Get::v('inv') == '') |
b48a0758 |
171 | $order = str_replace(" DESC,", ",", $order); |
172 | |
08cce2ff |
173 | $res = XDB::query(" |
b48a0758 |
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 | |
8c4a0c30 |
186 | function handler_contacts(&$page, $action = null, $subaction = null) |
b48a0758 |
187 | { |
b48a0758 |
188 | $page->assign('xorg_title','Polytechnique.org - Mes contacts'); |
59a61432 |
189 | $this->_add_rss_link($page); |
b48a0758 |
190 | |
cab08090 |
191 | $uid = S::v('uid'); |
5e2307dc |
192 | $user = Env::v('user'); |
b48a0758 |
193 | |
5e2307dc |
194 | switch (Env::v('action')) { |
b48a0758 |
195 | case 'retirer': |
196 | if (is_numeric($user)) { |
08cce2ff |
197 | if (XDB::execute('DELETE FROM contacts |
8c4a0c30 |
198 | WHERE uid = {?} AND contact = {?}', |
199 | $uid, $user)) |
b48a0758 |
200 | { |
a7de4ef7 |
201 | $page->trig("Contact retiré !"); |
b48a0758 |
202 | } |
203 | } else { |
08cce2ff |
204 | if (XDB::execute( |
e2f609cd |
205 | 'DELETE FROM c |
b48a0758 |
206 | USING contacts AS c |
207 | INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme") |
208 | WHERE c.uid = {?} AND a.alias={?}', $uid, $user)) |
209 | { |
a7de4ef7 |
210 | $page->trig("Contact retiré !"); |
b48a0758 |
211 | } |
212 | } |
213 | break; |
214 | |
215 | case 'ajouter': |
216 | require_once('user.func.inc.php'); |
217 | if (($login = get_user_login($user)) !== false) { |
08cce2ff |
218 | if (XDB::execute( |
51e10229 |
219 | 'REPLACE INTO contacts (uid, contact) |
220 | SELECT {?}, id |
221 | FROM aliases |
222 | WHERE alias = {?}', $uid, $login)) |
b48a0758 |
223 | { |
a7de4ef7 |
224 | $page->trig('Contact ajouté !'); |
b48a0758 |
225 | } else { |
a7de4ef7 |
226 | $page->trig('Contact déjà dans la liste !'); |
b48a0758 |
227 | } |
228 | } |
229 | } |
230 | |
8c4a0c30 |
231 | $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid "); |
232 | $view->addMod('minifiche', 'Mini-Fiches', true); |
233 | $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true)); |
234 | $view->addMod('geoloc', 'Planisphère'); |
235 | $view->apply('carnet/contacts', $page, $action, $subaction); |
236 | if ($action != 'geoloc' || !$subaction) { |
237 | $page->changeTpl('carnet/mescontacts.tpl'); |
b48a0758 |
238 | } |
b48a0758 |
239 | } |
240 | |
241 | function handler_pdf(&$page, $arg0 = null, $arg1 = null) |
242 | { |
ee5990c7 |
243 | require_once dirname(__FILE__).'/carnet/contacts.pdf.inc.php'; |
b48a0758 |
244 | require_once 'user.func.inc.php'; |
245 | |
246 | session_write_close(); |
247 | |
248 | $sql = "SELECT a.alias |
249 | FROM aliases AS a |
250 | INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id ) |
251 | INNER JOIN contacts AS c ON ( a.id = c.contact ) |
252 | WHERE c.uid = {?} AND a.type='a_vie'"; |
253 | if ($arg0 == 'promo') { |
254 | $sql .= ' ORDER BY u.promo, u.nom, u.prenom'; |
255 | } else { |
256 | $sql .= ' ORDER BY u.nom, u.prenom, u.promo'; |
257 | } |
258 | |
cab08090 |
259 | $citer = XDB::iterRow($sql, S::v('uid')); |
b48a0758 |
260 | $pdf = new ContactsPDF(); |
261 | |
262 | while (list($alias) = $citer->next()) { |
263 | $user = get_user_details($alias); |
ac73e294 |
264 | foreach ($user as &$value) { |
265 | if (is_utf8($value)) { |
266 | $value = utf8_decode($value); |
267 | } |
268 | } |
93c099e1 |
269 | $pdf = ContactsPDF::addContact($pdf, $user, $arg0 == 'photos' || $arg1 == 'photos'); |
b48a0758 |
270 | } |
271 | $pdf->Output(); |
272 | |
273 | exit; |
274 | } |
275 | |
c9f82d49 |
276 | function handler_rss(&$page, $user = null, $hash = null) |
277 | { |
278 | require_once 'rss.inc.php'; |
279 | require_once 'notifs.inc.php'; |
0337d704 |
280 | |
c9f82d49 |
281 | $uid = init_rss('carnet/rss.tpl', $user, $hash); |
282 | $notifs = new Notifs($uid, false); |
283 | $page->assign('notifs', $notifs); |
c9f82d49 |
284 | } |
fbfb06dc |
285 | |
963c0b2e |
286 | function handler_ical(&$page, $alias = null, $hash = null) |
fbfb06dc |
287 | { |
963c0b2e |
288 | require_once 'rss.inc.php'; |
289 | $uid = init_rss(null, $alias, $hash, false); |
290 | if (S::logged()) { |
291 | if (!$uid) { |
292 | $uid = S::i('uid'); |
293 | } else if ($uid != S::i('uid')) { |
294 | require_once 'xorg.misc.inc.php'; |
295 | send_warning_email("Récupération d\'un autre utilisateur ($uid)"); |
296 | } |
297 | } else if (!$uid) { |
298 | exit; |
299 | } |
99544d53 |
300 | require_once 'ical.inc.php'; |
801fcad8 |
301 | $page->changeTpl('carnet/calendar.tpl', NO_SKIN); |
3585b0c8 |
302 | $page->register_function('display_ical', 'display_ical'); |
fbfb06dc |
303 | |
f3b3f363 |
304 | $res = XDB::iterRow( |
305 | 'SELECT u.prenom, |
306 | IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom, |
307 | u.promo, |
308 | u.naissance, |
309 | DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end, |
310 | u.date_ins, |
311 | a.alias AS forlife |
312 | FROM contacts AS c |
313 | INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact) |
314 | INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\') |
963c0b2e |
315 | WHERE c.uid = {?}', $uid); |
f3b3f363 |
316 | |
317 | $annivs = Array(); |
318 | while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) { |
319 | $naissance = str_replace('-', '', $naissance); |
320 | $end = str_replace('-', '', $end); |
321 | $annivs[] = array( |
322 | 'timestamp' => strtotime($ts), |
323 | 'date' => $naissance, |
324 | 'tomorrow' => $end, |
325 | 'forlife' => $forlife, |
326 | 'summary' => 'Anniversaire de '.$prenom |
327 | .' '.$nom.' - x '.$promo, |
328 | ); |
fbfb06dc |
329 | } |
f3b3f363 |
330 | $page->assign('events', $annivs); |
fbfb06dc |
331 | |
332 | header('Content-Type: text/calendar; charset=utf-8'); |
fbfb06dc |
333 | } |
5e193297 |
334 | |
917c4d11 |
335 | function handler_vcard(&$page, $photos = null) |
5e193297 |
336 | { |
337 | $res = XDB::query('SELECT contact |
338 | FROM contacts |
339 | WHERE uid = {?}', S::v('uid')); |
917c4d11 |
340 | $vcard = new VCard($res->fetchColumn(), $photos == 'photos'); |
5e193297 |
341 | $vcard->do_page(&$page); |
342 | } |
4da0b8d7 |
343 | } |
c9f82d49 |
344 | |
a7de4ef7 |
345 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
c9f82d49 |
346 | ?> |