2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class CarnetModule
extends PLModule
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
),
31 'carnet/contacts' => $this->make_hook('contacts', AUTH_COOKIE
),
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
),
36 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC
, 'user', NO_HTTPS
),
40 function on_subscribe($forlife, $uid, $promo, $password)
42 require_once 'notifs.inc.php';
43 register_watch_op($uid, WATCH_INSCR
);
44 inscription_notifs_base($uid);
47 function _add_rss_link(&$page)
49 if (!S
::has('core_rss_hash')) {
52 $page->setRssLink('Polytechnique.org :: Carnet',
53 '/carnet/rss/'.S
::v('forlife') .'/'.S
::v('core_rss_hash').'/rss.xml');
56 function handler_index(&$page)
58 $page->changeTpl('carnet/index.tpl');
59 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
60 $this->_add_rss_link($page);
63 function handler_panel(&$page)
65 $page->changeTpl('carnet/panel.tpl');
67 if (Get
::has('read')) {
68 $_SESSION['watch_last'] = Get
::v('read');
70 pl_redirect('carnet/panel');
73 require_once 'notifs.inc.php';
75 $page->assign('now',date('YmdHis'));
76 $notifs = new Notifs(S
::v('uid'), true
);
78 $page->assign('notifs', $notifs);
79 $page->assign('today', date('Y-m-d'));
80 $this->_add_rss_link($page);
83 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
85 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
86 $p = intval($matches[1]);
87 if($p<1900 ||
$p>2100) {
88 $page->trig("la promo entrée est invalide");
90 if ($action == 'add_promo') {
91 $watch->_promos
->add($p);
93 $watch->_promos
->del($p);
96 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
97 $p1 = intval($matches[1]);
98 $p2 = intval($matches[2]);
99 if($p1<1900 ||
$p1>2100) {
100 $page->trig('la première promo de la plage entrée est invalide');
101 } elseif($p2<1900 ||
$p2>2100) {
102 $page->trig('la seconde promo de la plage entrée est invalide');
104 if ($action == 'add_promo') {
105 $watch->_promos
->addRange($p1, $p2);
107 $watch->_promos
->delRange($p1, $p2);
111 $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect.");
115 function handler_notifs(&$page, $action = null
, $arg = null
)
117 $page->changeTpl('carnet/notifs.tpl');
119 require_once 'notifs.inc.php';
121 $watch = new Watch(S
::v('uid'));
123 $res = XDB
::query("SELECT promo_sortie
125 WHERE user_id = {?}",
127 $promo_sortie = $res->fetchOneCell();
128 $page->assign('promo_sortie', $promo_sortie);
133 $this->_handler_notifs_promos($page, $watch, $action, $arg);
137 $watch->_nonins
->del($arg);
141 $watch->_nonins
->add($arg);
145 if (Env
::has('subs')) $watch->_subs
->update('sub');
146 if (Env
::has('flags_contacts')) {
147 $watch->watch_contacts
= Env
::b('contacts');
150 if (Env
::has('flags_mail')) {
151 $watch->watch_mail
= Env
::b('mail');
155 $page->assign_by_ref('watch', $watch);
158 function _get_list($offset, $limit) {
160 $res = XDB
::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
161 $total = $res->fetchOneCell();
163 $order = Get
::v('order');
165 'nom' => 'nom DESC, u.prenom, u.promo',
166 'promo' => 'promo DESC, nom, u.prenom',
167 'last' => 'u.date DESC, nom, u.prenom, promo');
168 if ($order != 'promo' && $order != 'last')
170 $order = $orders[$order];
171 if (Get
::v('inv') == '')
172 $order = str_replace(" DESC,", ",", $order);
175 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
177 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
178 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
181 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
182 $list = $res->fetchAllAssoc();
184 return Array($total, $list);
187 function searchErrorHandler($explain) {
189 $page->trig($explain);
190 $this->handler_contacts($page);
193 function handler_contacts(&$page, $action = null
, $subaction = null
, $ssaction = null
)
195 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
196 $this->_add_rss_link($page);
199 $user = Env
::v('user');
201 switch (Env
::v('action')) {
203 if (is_numeric($user)) {
204 if (XDB
::execute('DELETE FROM contacts
205 WHERE uid = {?} AND contact = {?}',
208 $page->trig("Contact retiré !");
214 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
215 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
217 $page->trig("Contact retiré !");
223 require_once('user.func.inc.php');
224 if (($login = get_user_login($user)) !== false
) {
226 'REPLACE INTO contacts (uid, contact)
229 WHERE alias = {?}', $uid, $login))
231 $page->trig('Contact ajouté !');
233 $page->trig('Contact déjà dans la liste !');
239 if ($action == 'search') {
240 $action = $subaction;
241 $subaction = $ssaction;
244 if ($search && trim(Env
::v('quick'))) {
245 require_once 'userset.inc.php';
246 $base = 'carnet/contacts/search';
248 require_once(dirname(__FILE__
) . '/search/classes.inc.php');
249 ThrowError
::$throwHook = array($this, 'searchErrorHandler');
250 $view = new SearchSet(true
, false
, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
252 $base = 'carnet/contacts';
253 $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
255 $view->addMod('minifiche', 'Mini-Fiches', true
);
256 $view->addMod('trombi', 'Trombinoscope', false
, array('with_admin' => false
, 'with_promo' => true
));
257 $view->addMod('geoloc', 'Planisphère', false
, array('with_annu' => 'carnet/contacts/search'));
258 $view->apply($base, $page, $action, $subaction);
259 if ($action != 'geoloc' ||
($search && !$ssaction) ||
(!$search && !$subaction)) {
260 $page->changeTpl('carnet/mescontacts.tpl');
264 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
266 require_once dirname(__FILE__
).'/carnet/contacts.pdf.inc.php';
267 require_once 'user.func.inc.php';
269 session_write_close();
271 $sql = "SELECT a.alias
273 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
274 INNER JOIN contacts AS c ON ( a.id = c.contact )
275 WHERE c.uid = {?} AND a.type='a_vie'";
276 if ($arg0 == 'promo') {
277 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
279 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
282 $citer = XDB
::iterRow($sql, S
::v('uid'));
283 $pdf = new ContactsPDF();
285 while (list($alias) = $citer->next()) {
286 $user = get_user_details($alias);
287 foreach ($user as &$value) {
288 if (is_utf8($value)) {
289 $value = utf8_decode($value);
292 $pdf = ContactsPDF
::addContact($pdf, $user, $arg0 == 'photos' ||
$arg1 == 'photos');
299 function handler_rss(&$page, $user = null
, $hash = null
)
301 require_once 'rss.inc.php';
302 require_once 'notifs.inc.php';
304 $uid = init_rss('carnet/rss.tpl', $user, $hash);
305 $notifs = new Notifs($uid, false
);
306 $page->assign('notifs', $notifs);
309 function handler_ical(&$page, $alias = null
, $hash = null
)
311 require_once 'rss.inc.php';
312 $uid = init_rss(null
, $alias, $hash, false
);
316 } else if ($uid != S
::i('uid')) {
317 require_once 'xorg.misc.inc.php';
318 send_warning_email("Récupération d\'un autre utilisateur ($uid)");
323 require_once 'ical.inc.php';
324 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
325 $page->register_function('display_ical', 'display_ical');
329 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
332 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
336 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
337 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
338 WHERE c.uid = {?}', $uid);
341 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
342 $naissance = str_replace('-', '', $naissance);
343 $end = str_replace('-', '', $end);
345 'timestamp' => strtotime($ts),
346 'date' => $naissance,
348 'forlife' => $forlife,
349 'summary' => 'Anniversaire de '.$prenom
350 .' '.$nom.' - x '.$promo,
353 $page->assign('events', $annivs);
355 header('Content-Type: text/calendar; charset=utf-8');
358 function handler_vcard(&$page, $photos = null
)
360 $res = XDB
::query('SELECT contact
362 WHERE uid = {?}', S
::v('uid'));
363 $vcard = new VCard($res->fetchColumn(), $photos == 'photos');
364 $vcard->do_page(&$page);
368 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: