2 /***************************************************************************
3 * Copyright (C) 2003-2006 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
),
34 'carnet/rss' => $this->make_hook('rss', AUTH_PUBLIC
),
35 'carnet/ical' => $this->make_hook('ical', AUTH_PUBLIC
),
39 function on_subscribe($forlife, $uid, $promo, $password)
41 require_once 'notifs.inc.php';
42 register_watch_op($uid, WATCH_INSCR
);
43 inscription_notifs_base($uid);
46 function _add_rss_link(&$page)
48 if (!S
::has('core_rss_hash')) {
51 $page->assign('xorg_rss',
52 array('title' => 'Polytechnique.org :: Carnet',
53 'href' => '/carnet/rss/'.S
::v('forlife')
54 .'/'.S
::v('core_rss_hash').'/rss.xml')
58 function handler_index(&$page)
60 $page->changeTpl('carnet/index.tpl');
61 $page->assign('xorg_title','Polytechnique.org - Mon carnet');
62 $this->_add_rss_link($page);
65 function handler_panel(&$page)
67 $page->changeTpl('carnet/panel.tpl');
69 if (Get
::has('read')) {
70 $_SESSION['watch_last'] = Get
::v('read');
71 pl_redirect('carnet/panel');
74 require_once 'notifs.inc.php';
76 $page->assign('now',date('YmdHis'));
77 $notifs = new Notifs(S
::v('uid'), true
);
79 $page->assign('notifs', $notifs);
80 $page->assign('today', date('Y-m-d'));
81 $this->_add_rss_link($page);
84 function _handler_notifs_promos(&$page, &$watch, $action, $arg)
86 if(preg_match('!^ *(\d{4}) *$!', $arg, $matches)) {
87 $p = intval($matches[1]);
88 if($p<1900 ||
$p>2100) {
89 $page->trig("la promo entrée est invalide");
91 if ($action == 'add_promo') {
92 $watch->_promos
->add($p);
94 $watch->_promos
->del($p);
97 } elseif (preg_match('!^ *(\d{4}) *- *(\d{4}) *$!', $arg, $matches)) {
98 $p1 = intval($matches[1]);
99 $p2 = intval($matches[2]);
100 if($p1<1900 ||
$p1>2100) {
101 $page->trig('la première promo de la plage entrée est invalide');
102 } elseif($p2<1900 ||
$p2>2100) {
103 $page->trig('la seconde promo de la plage entrée est invalide');
105 if ($action == 'add_promo') {
106 $watch->_promos
->addRange($p1, $p2);
108 $watch->_promos
->delRange($p1, $p2);
112 $page->trig("La promo (ou la plage de promo) entrée est dans un format incorrect.");
116 function handler_notifs(&$page, $action = null
, $arg = null
)
118 $page->changeTpl('carnet/notifs.tpl');
120 require_once 'notifs.inc.php';
122 $watch = new Watch(S
::v('uid'));
124 $res = XDB
::query("SELECT promo_sortie
126 WHERE user_id = {?}",
128 $promo_sortie = $res->fetchOneCell();
129 $page->assign('promo_sortie', $promo_sortie);
134 $this->_handler_notifs_promos($page, $watch, $action, $arg);
138 $watch->_nonins
->del($arg);
142 $watch->_nonins
->add($arg);
146 if (Env
::has('subs')) $watch->_subs
->update('sub');
147 if (Env
::has('flags_contacts')) {
148 $watch->watch_contacts
= Env
::b('contacts');
151 if (Env
::has('flags_mail')) {
152 $watch->watch_mail
= Env
::b('mail');
156 $page->assign_by_ref('watch', $watch);
159 function _get_list($offset, $limit) {
161 $res = XDB
::query("SELECT COUNT(*) FROM contacts WHERE uid = {?}", $uid);
162 $total = $res->fetchOneCell();
164 $order = Get
::v('order');
166 'nom' => 'nom DESC, u.prenom, u.promo',
167 'promo' => 'promo DESC, nom, u.prenom',
168 'last' => 'u.date DESC, nom, u.prenom, promo');
169 if ($order != 'promo' && $order != 'last')
171 $order = $orders[$order];
172 if (Get
::v('inv') == '')
173 $order = str_replace(" DESC,", ",", $order);
176 SELECT u.prenom, IF(u.nom_usage='',u.nom,u.nom_usage) AS nom, a.alias AS forlife, u.promo
178 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
179 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type='a_vie')
182 LIMIT {?}, {?}", $uid, $offset*$limit, $limit);
183 $list = $res->fetchAllAssoc();
185 return Array($total, $list);
188 function handler_contacts(&$page, $action = null
)
190 $page->changeTpl('carnet/mescontacts.tpl');
191 require_once("applis.func.inc.php");
192 $page->assign('xorg_title','Polytechnique.org - Mes contacts');
195 $user = Env
::v('user');
197 switch (Env
::v('action')) {
199 if (is_numeric($user)) {
200 if (XDB
::execute('DELETE FROM contacts
201 WHERE uid = {?} AND contact = {?}',
204 $page->trig("Contact retiré !");
208 'DELETE FROM contacts
210 INNER JOIN aliases AS a ON (c.contact=a.id and a.type!="homonyme")
211 WHERE c.uid = {?} AND a.alias={?}', $uid, $user))
213 $page->trig("Contact retiré !");
219 require_once('user.func.inc.php');
220 if (($login = get_user_login($user)) !== false
) {
222 'INSERT INTO contacts (uid, contact)
225 WHERE alias = {?}', $uid, $login))
227 $page->trig('Contact ajouté !');
229 $page->trig('Contact déjà dans la liste !');
234 if ($action == 'trombi') {
235 require_once 'trombi.inc.php';
237 $trombi = new Trombi(array($this, '_get_list'));
238 $trombi->setNbRows(4);
239 $page->assign_by_ref('trombi',$trombi);
241 $order = Get
::v('order');
242 if ($order != 'promo' && $order != 'last')
244 $page->assign('order', $order);
245 $page->assign('inv', Get
::v('inv'));
249 $order = Get
::v('order');
251 'nom' => 'sortkey DESC, a.prenom, a.promo',
252 'promo' => 'promo DESC, sortkey, a.prenom',
253 'last' => 'a.date DESC, sortkey, a.prenom, promo');
254 if ($order != 'promo' && $order != 'last')
256 $page->assign('order', $order);
257 $page->assign('inv', Get
::v('inv'));
258 $order = $orders[$order];
259 if (Get
::v('inv') == '')
260 $order = str_replace(" DESC,", ",", $order);
262 $sql = "SELECT contact AS id,
263 a.*, l.alias AS forlife,
265 a.perms != 'pending' AS wasinscrit,
266 a.deces != 0 AS dcd, a.deces, a.matricule_ax,
267 FIND_IN_SET('femme', a.flags) AS sexe,
268 e.entreprise, es.label AS secteur, ef.fonction_fr AS fonction,
269 IF(n.nat='',n.pays,n.nat) AS nat, n.a2 AS iso3166,
270 ad0.text AS app0text, ad0.url AS app0url, ai0.type AS app0type,
271 ad1.text AS app1text, ad1.url AS app1url, ai1.type AS app1type,
272 adr.city, gp.a2, gp.pays AS countrytxt, gr.name AS region,
273 IF(a.nom_usage<>'',a.nom_usage,a.nom) AS sortkey,
274 COUNT(em.email) > 0 AS actif
276 INNER JOIN auth_user_md5 AS a ON (a.user_id = c.contact)
277 INNER JOIN aliases AS l ON (a.user_id = l.id AND l.type='a_vie')
278 LEFT JOIN entreprises AS e ON (e.entrid = 0 AND e.uid = a.user_id)
279 LEFT JOIN emploi_secteur AS es ON (e.secteur = es.id)
280 LEFT JOIN fonctions_def AS ef ON (e.fonction = ef.id)
281 LEFT JOIN geoloc_pays AS n ON (a.nationalite = n.a2)
282 LEFT JOIN applis_ins AS ai0 ON (a.user_id = ai0.uid AND ai0.ordre = 0)
283 LEFT JOIN applis_def AS ad0 ON (ad0.id = ai0.aid)
284 LEFT JOIN applis_ins AS ai1 ON (a.user_id = ai1.uid AND ai1.ordre = 1)
285 LEFT JOIN applis_def AS ad1 ON (ad1.id = ai1.aid)
286 LEFT JOIN adresses AS adr ON (a.user_id = adr.uid
287 AND FIND_IN_SET('active', adr.statut))
288 LEFT JOIN geoloc_pays AS gp ON (adr.country = gp.a2)
289 LEFT JOIN geoloc_region AS gr ON (adr.country = gr.a2 AND adr.region = gr.region)
290 LEFT JOIN emails AS em ON (em.uid = a.user_id AND em.flags = 'active')
295 $page->assign('citer', XDB
::iterator($sql));
299 function handler_pdf(&$page, $arg0 = null
, $arg1 = null
)
301 require_once dirname(__FILE__
).'/carnet/contacts.pdf.inc.php';
302 require_once 'user.func.inc.php';
304 session_write_close();
306 $sql = "SELECT a.alias
308 INNER JOIN auth_user_md5 AS u ON ( a.id = u.user_id )
309 INNER JOIN contacts AS c ON ( a.id = c.contact )
310 WHERE c.uid = {?} AND a.type='a_vie'";
311 if ($arg0 == 'promo') {
312 $sql .= ' ORDER BY u.promo, u.nom, u.prenom';
314 $sql .= ' ORDER BY u.nom, u.prenom, u.promo';
317 $citer = XDB
::iterRow($sql, S
::v('uid'));
318 $pdf = new ContactsPDF();
320 while (list($alias) = $citer->next()) {
321 $user = get_user_details($alias);
322 $pdf->addContact($user, $arg0 == 'photos' ||
$arg1 == 'photos');
329 function handler_rss(&$page, $user = null
, $hash = null
)
331 require_once 'rss.inc.php';
332 require_once 'notifs.inc.php';
334 $uid = init_rss('carnet/rss.tpl', $user, $hash);
335 $notifs = new Notifs($uid, false
);
336 $page->assign('notifs', $notifs);
339 function handler_ical(&$page)
341 require_once dirname(__FILE__
).'/carnet/smarty.php';
342 $page->changeTpl('carnet/calendar.tpl', NO_SKIN
);
343 $page->register_function('display_ical', 'display_ical');
347 IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
350 DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
354 INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
355 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
356 WHERE c.uid = {?}', S
::v('uid'));
359 while (list($prenom, $nom, $promo, $naissance, $end, $ts, $forlife) = $res->next()) {
360 $naissance = str_replace('-', '', $naissance);
361 $end = str_replace('-', '', $end);
363 'timestamp' => strtotime($ts),
364 'date' => $naissance,
366 'forlife' => $forlife,
367 'summary' => 'Anniversaire de '.$prenom
368 .' '.$nom.' - x '.$promo,
371 $page->assign('events', $annivs);
373 header('Content-Type: text/calendar; charset=utf-8');