migrate fiche_referent -> profile/referent/${username}
[platal.git] / modules / profile.php
CommitLineData
7d8b17cb 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
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
22class ProfileModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
28e16d4d 27 'photo' => $this->make_hook('photo', AUTH_PUBLIC),
28 'photo/change' => $this->make_hook('photo_change', AUTH_MDP),
e49018a7 29
28e16d4d 30 'profile/orange' => $this->make_hook('p_orange', AUTH_MDP),
31 'profile/referent' => $this->make_hook('p_referent', AUTH_MDP),
32 'profile/usage' => $this->make_hook('p_usage', AUTH_MDP),
e49018a7 33
28e16d4d 34 'trombi' => $this->make_hook('trombi', AUTH_COOKIE),
926f16d7 35
28e16d4d 36 'vcard' => $this->make_hook('vcard', AUTH_COOKIE),
7d8b17cb 37 );
38 }
39
40 function _trombi_getlist($offset, $limit)
41 {
42 global $globals;
43
44 $where = ( $this->promo > 0 ? "WHERE promo='{$this->promo}'" : "" );
45
46 $res = $globals->xdb->query(
47 "SELECT COUNT(*)
48 FROM auth_user_md5 AS u
49 RIGHT JOIN photo AS p ON u.user_id=p.uid
50 $where");
51 $pnb = $res->fetchOneCell();
52
53 $res = $globals->xdb->query(
54 "SELECT promo,user_id,a.alias AS forlife,IF(nom_usage='', nom, nom_usage) AS nom,prenom
55 FROM photo AS p
56 INNER JOIN auth_user_md5 AS u ON u.user_id=p.uid
57 INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' )
58 $where
59 ORDER BY promo,nom,prenom LIMIT {?}, {?}", $offset*$limit, $limit);
60
61 return array($pnb, $res->fetchAllAssoc());
62 }
63
adbdf493 64 function handler_photo(&$page, $x = null, $req = null)
65 {
66 if (is_null($x)) {
67 return PL_NOT_FOUND;
68 }
69
70 global $globals;
71
72 $res = $globals->xdb->query("SELECT id, pub FROM aliases
73 LEFT JOIN photo ON(id = uid)
74 WHERE alias = {?}", $x);
75 list($uid, $photo_pub) = $res->fetchOneRow();
76
77 if ($req && logged()) {
78 include 'validations.inc.php';
79 $myphoto = PhotoReq::get_request($uid);
80 Header('Content-type: image/'.$myphoto->mimetype);
81 echo $myphoto->data;
82 } else {
83 $res = $globals->xdb->query(
84 "SELECT attachmime, attach
85 FROM photo
86 WHERE uid={?}", $uid);
87
88 if ((list($type,$data) = $res->fetchOneRow()) && ($photo_pub == 'public' || logged())) {
89 Header("Content-type: image/$type");
90 echo $data;
91 } else {
92 Header('Content-type: image/png');
fb9a56cb 93 echo file_get_contents(dirname(__FILE__).'/../htdocs/images/none.png');
adbdf493 94 }
95 }
96 exit;
97 }
98
fb9a56cb 99 function handler_photo_change(&$page)
100 {
101 global $globals;
102
103 $page->changeTpl('trombino.tpl');
104
105 require_once('validations.inc.php');
106
107 $trombi_x = '/home/web/trombino/photos'.Session::get('promo')
108 .'/'.Session::get('forlife').'.jpg';
109
110 if (Env::has('upload')) {
111 $file = isset($_FILES['userfile']['tmp_name'])
112 ? $_FILES['userfile']['tmp_name']
113 : Env::get('photo');
114 if ($data = file_get_contents($file)) {
115 if ($myphoto = new PhotoReq(Session::getInt('uid'), $data)) {
116 $myphoto->submit();
117 }
118 } else {
119 $page->trig('Fichier inexistant ou vide');
120 }
121 } elseif (Env::has('trombi')) {
122 $myphoto = new PhotoReq(Session::getInt('uid'),
123 file_get_contents($trombi_x));
124 if ($myphoto) {
125 $myphoto->commit();
126 $myphoto->clean();
127 }
128 } elseif (Env::get('suppr')) {
129 $globals->xdb->execute('DELETE FROM photo WHERE uid = {?}',
130 Session::getInt('uid'));
131 $globals->xdb->execute('DELETE FROM requests
132 WHERE user_id = {?} AND type="photo"',
133 Session::getInt('uid'));
134 } elseif (Env::get('cancel')) {
135 $sql = $globals->xdb->query('DELETE FROM requests
136 WHERE user_id={?} AND type="photo"',
137 Session::getInt('uid'));
138 }
139
140 $sql = $globals->xdb->query('SELECT COUNT(*) FROM requests
141 WHERE user_id={?} AND type="photo"',
142 Session::getInt('uid'));
143 $page->assign('submited', $sql->fetchOneCell());
144 $page->assign('has_trombi_x', file_exists($trombi_x));
145
146 return PL_OK;
147 }
148
9b0fa329 149 function handler_p_orange(&$page)
150 {
151 global $globals;
152
153 $page->changeTpl('orange.tpl');
154
155 require_once 'validations.inc.php';
156 require_once 'xorg.misc.inc.php';
157
158 $res = $globals->xdb->query(
159 "SELECT u.promo,u.promo_sortie
160 FROM auth_user_md5 AS u
161 WHERE user_id={?}", Session::getInt('uid'));
162
163 list($promo,$promo_sortie_old) = $res->fetchOneRow();
164 $page->assign('promo_sortie_old', $promo_sortie_old);
165 $page->assign('promo', $promo);
166
167 if (!Env::has('promo_sortie')) {
168 return PL_OK;
169 }
170
171 $promo_sortie = Env::getInt('promo_sortie');
172
173 if ($promo_sortie < 1000 || $promo_sortie > 9999) {
174 $page->trig('L\'année de sortie doit être un nombre de quatre chiffres');
175 }
176 elseif ($promo_sortie < $promo + 3) {
177 $page->trig('Trop tôt');
178 }
179 elseif ($promo_sortie == $promo_sortie_old) {
180 $page->trig('Tu appartiens déjà à la promotion correspondante à cette année de sortie.');
181 }
182 elseif ($promo_sortie == $promo + 3) {
183 $globals->xdb->execute(
184 "UPDATE auth_user_md5 set promo_sortie={?}
185 WHERE user_id={?}",$promo_sortie,Session::getInt('uid'));
186 $page->trig('Ton statut "orange" a été supprimé.');
187 $page->assign('promo_sortie_old', $promo_sortie);
188 }
189 else {
190 $page->assign('promo_sortie', $promo_sortie);
191
192 if (Env::has('submit')) {
193 $myorange = new OrangeReq(Session::getInt('uid'),
194 $promo_sortie);
195 $myorange->submit();
196 $page->assign('myorange', $myorange);
197 }
198 }
199
200 return PL_OK;
201 }
202
28e16d4d 203 function handler_p_referent(&$page, $x = null)
204 {
205 global $globals;
206
207 require_once 'user.func.inc.php';
208
209 if (is_null($x)) {
210 return PL_NOT_FOUND;
211 }
212
213 $page->changeTpl('fiche_referent.tpl');
214 $page->assign('simple', true);
215
216 $res = $globals->xdb->query(
217 "SELECT prenom, nom, user_id, promo, cv, a.alias AS bestalias
218 FROM auth_user_md5 AS u
219 INNER JOIN aliases AS a ON (u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
220 INNER JOIN aliases AS a1 ON (u.user_id=a1.id
221 AND a1.alias = {?}
222 AND a1.type!='homonyme')", $x);
223
224 if ($res->numRows() != 1) {
225 return PL_NOT_FOUND;
226 }
227
228 list($prenom, $nom, $user_id, $promo, $cv, $bestalias) = $res->fetchOneRow();
229
230 $page->assign('prenom', $prenom);
231 $page->assign('nom', $nom);
232 $page->assign('promo', $promo);
233 $page->assign('cv', $cv);
234 $page->assign('bestalias', $bestalias);
235 $page->assign('adr_pro', get_user_details_pro($user_id));
236
237 ///// recuperations infos referent
238
239 //expertise
240 $res = $globals->xdb->query("SELECT expertise FROM mentor WHERE uid = {?}", $user_id);
241 $page->assign('expertise', $res->fetchOneCell());
242
243 //secteurs
244 $secteurs = $ss_secteurs = Array();
245 $res = $globals->xdb->iterRow(
246 "SELECT s.label, ss.label
247 FROM mentor_secteurs AS m
248 LEFT JOIN emploi_secteur AS s ON(m.secteur = s.id)
249 LEFT JOIN emploi_ss_secteur AS ss ON(m.secteur = ss.secteur AND m.ss_secteur = ss.id)
250 WHERE uid = {?}", $user_id);
251 while (list($sec, $ssec) = $res->next()) {
252 $secteurs[] = $sec;
253 $ss_secteurs[] = $ssec;
254 }
255 $page->assign_by_ref('secteurs', $secteurs);
256 $page->assign_by_ref('ss_secteurs', $ss_secteurs);
257
258 //pays
259 $res = $globals->xdb->query(
260 "SELECT gp.pays
261 FROM mentor_pays AS m
262 LEFT JOIN geoloc_pays AS gp ON(m.pid = gp.a2)
263 WHERE uid = {?}", $user_id);
264 $page->assign('pays', $res->fetchColumn());
265
266 $page->addJsLink('javascript/close_on_esc.js');
267 return PL_OK;
268 }
269
926f16d7 270 function handler_p_usage(&$page)
271 {
272 global $globals;
273
274 $page->changeTpl('nomusage.tpl');
275
276 require_once 'validations.inc.php';
277 require_once 'xorg.misc.inc.php';
278
279 $res = $globals->xdb->query(
280 "SELECT u.nom,u.nom_usage,u.flags,e.alias
281 FROM auth_user_md5 AS u
282 LEFT JOIN aliases AS e ON(u.user_id = e.id AND FIND_IN_SET('usage',e.flags))
283 WHERE user_id={?}", Session::getInt('uid'));
284
285 list($nom,$usage_old,$flags,$alias_old) = $res->fetchOneRow();
286 $flags = new flagset($flags);
287 $page->assign('usage_old', $usage_old);
288 $page->assign('alias_old', $alias_old);
289
290 $nom_usage = replace_accent(trim(Env::get('nom_usage')));
291 $nom_usage = strtoupper($nom_usage);
292 $page->assign('usage_req', $nom_usage);
293
294 if (Env::has('submit') && ($nom_usage != $usage_old)) {
295 // on vient de recevoir une requete, differente de l'ancien nom d'usage
296 if ($nom_usage == $nom) {
297 $page->assign('same', true);
9b0fa329 298 } else { // le nom de mariage est distinct du nom à l'X
926f16d7 299 // on calcule l'alias pour l'afficher
300 $reason = Env::get('reason');
301 if ($reason == 'other') {
302 $reason = Env::get('other_reason');
303 }
304 $myusage = new UsageReq(Session::getInt('uid'), $nom_usage, $reason);
305 $myusage->submit();
306 $page->assign('myusage', $myusage);
307 }
308 }
309
310 return PL_OK;
311 }
312
7d8b17cb 313 function handler_trombi(&$page, $promo = null)
314 {
315 require_once 'trombi.inc.php';
316
317 $page->changeTpl('trombipromo.tpl');
318 $page->assign('xorg_title','Polytechnique.org - Trombi Promo');
319
320 if (is_null($promo)) {
321 return PL_OK;
322 }
323
324 $this->promo = $promo = intval($promo);
325
326 if ($promo >= 1900 && $promo < intval(date('Y'))
327 || ($promo == -1 && has_perms()))
328 {
329 $trombi = new Trombi(array($this, '_trombi_getlist'));
330 $trombi->hidePromo();
331 $trombi->setAdmin();
332 $page->assign_by_ref('trombi', $trombi);
333 } else {
334 $page->trig('Promotion incorrecte (saisir au format YYYY). Recommence.');
335 }
336
337 return PL_OK;
338 }
e49018a7 339
340 function format_adr($params, &$smarty)
341 {
342 // $adr1, $adr2, $adr3, $postcode, $city, $region, $country
343 extract($params['adr']);
344 $adr = $adr1;
345 $adr = trim("$adr\n$adr2");
346 $adr = trim("$adr\n$adr3");
347 return quoted_printable_encode(";;$adr;$city;$region;$postcode;$country");
348 }
349
350 function handler_vcard(&$page, $x = null)
351 {
352 if (is_null($x)) {
353 return PL_NOT_FOUND;
354 }
355
356 global $globals;
357
358 if (substr($x, -4) == '.vcf') {
359 $x = substr($x, 0, strlen($x) - 4);
360 }
361
362 new_nonhtml_page('vcard.tpl', AUTH_COOKIE);
363 require_once 'xorg.misc.inc.php';
364 require_once 'user.func.inc.php';
365
366 $page->register_modifier('qp_enc', 'quoted_printable_encode');
367 $page->register_function('format_adr', array($this, 'format_adr'));
368
369 $login = get_user_forlife($x);
370 $user = get_user_details($login);
371
372 // alias virtual
373 $res = $globals->xdb->query(
374 "SELECT alias
375 FROM virtual
376 INNER JOIN virtual_redirect USING(vid)
377 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
378 WHERE ( redirect={?} OR redirect={?} )
379 AND alias LIKE '%@{$globals->mail->alias_dom}'",
380 Session::getInt('uid'),
381 $user['forlife'].'@'.$globals->mail->domain,
382 $user['forlife'].'@'.$globals->mail->domain2);
383
384 $user['virtualalias'] = $res->fetchOneCell();
385
386 $page->assign_by_ref('vcard', $user);
387
388 header("Pragma: ");
389 header("Cache-Control: ");
390 header("Content-type: text/x-vcard\n");
391 header("Content-Transfer-Encoding: Quoted-Printable\n");
392
393 return PL_OK;
394 }
7d8b17cb 395}
396
397?>