Better that way
[platal.git] / include / user.func.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8b10988d 3 * Copyright (C) 2003-2006 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
22// {{{ function user_clear_all_subs()
23/** kills the inscription of a user.
24 * we still keep his birthdate, adresses, and personnal stuff
25 * kills the entreprises, mentor, emails and lists subscription stuff
26 */
27function user_clear_all_subs($user_id, $really_del=true)
28{
dd502514 29 // keep datas in : aliases, adresses, tels, applis_ins, binets_ins, contacts, groupesx_ins, homonymes, identification_ax, photo
0337d704 30 // delete in : auth_user_md5, auth_user_quick, competences_ins, emails, entreprises, langues_ins, mentor,
31 // mentor_pays, mentor_secteurs, newsletter_ins, perte_pass, requests, user_changes, virtual_redirect, watch_sub
32 // + delete maillists
33
34 global $globals;
35 $uid = intval($user_id);
08cce2ff 36 $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $uid);
0337d704 37 $alias = $res->fetchOneCell();
38
39 if ($really_del) {
08cce2ff 40 XDB::execute("DELETE FROM emails WHERE uid={?}", $uid);
41 XDB::execute("DELETE FROM newsletter_ins WHERE user_id={?}", $uid);
0337d704 42 }
43
08cce2ff 44 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain);
45 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain2);
46
47 XDB::execute("UPDATE auth_user_md5 SET password='',smtppass='' WHERE user_id={?}", $uid);
48 XDB::execute("UPDATE auth_user_quick SET watch_flags='' WHERE user_id={?}", $uid);
49
50 XDB::execute("DELETE FROM competences_ins WHERE uid={?}", $uid);
51 XDB::execute("DELETE FROM entreprises WHERE uid={?}", $uid);
52 XDB::execute("DELETE FROM langues_ins WHERE uid={?}", $uid);
53 XDB::execute("DELETE FROM mentor_pays WHERE uid={?}", $uid);
54 XDB::execute("DELETE FROM mentor_secteur WHERE uid={?}", $uid);
55 XDB::execute("DELETE FROM mentor WHERE uid={?}", $uid);
56 XDB::execute("DELETE FROM perte_pass WHERE uid={?}", $uid);
57 XDB::execute("DELETE FROM requests WHERE user_id={?}", $uid);
58 XDB::execute("DELETE FROM user_changes WHERE user_id={?}", $uid);
59 XDB::execute("DELETE FROM watch_sub WHERE uid={?}", $uid);
9bb8bf21 60
61 $mmlist = new MMList(S::v('id'), S::v('password'));
62 $mmlist->kill($alias, $really_del);
0337d704 63}
64
65// }}}
66// {{{ function get_user_login()
67
68function get_user_login($data, $get_forlife = false) {
69 global $globals, $page;
70
71 if (preg_match(',^[0-9]*$,', $data)) {
08cce2ff 72 $res = XDB::query("SELECT alias FROM aliases WHERE type='a_vie' AND id={?}", $data);
0337d704 73 if ($res->numRows()) {
74 return $res->fetchOneCell();
75 } else {
76 $page->trig("il n'y a pas d'utilisateur avec cet id");
77 return false;
78 }
79 }
80
81 $data = trim(strtolower($data));
82
83 if (strstr($data, '@')===false) {
84 $data = $data.'@'.$globals->mail->domain;
85 }
86
575dd9be 87 list($mbox, $fqdn) = explode('@', $data);
0337d704 88 if ($fqdn == $globals->mail->domain || $fqdn == $globals->mail->domain2) {
89
08cce2ff 90 $res = XDB::query("SELECT a.alias
0337d704 91 FROM aliases AS a
92 INNER JOIN aliases AS b ON (a.id = b.id AND b.type IN ('alias', 'a_vie') AND b.alias={?})
93 WHERE a.type = 'a_vie'", $mbox);
94 if ($res->numRows()) {
95 return $get_forlife ? $res->fetchOneCell() : $mbox;
96 } else {
97 $page->trig("il n'y a pas d'utilisateur avec ce login");
98 return false;
99 }
100
101 } elseif ($fqdn == $globals->mail->alias_dom || $fqdn == $globals->mail->alias_dom2) {
102
08cce2ff 103 $res = XDB::query("SELECT redirect
0337d704 104 FROM virtual_redirect
105 INNER JOIN virtual USING(vid)
106 WHERE alias={?}", $mbox.'@'.$globals->mail->alias_dom);
107 if ($redir = $res->fetchOneCell()) {
575dd9be 108 list($alias) = explode('@', $redir);
0337d704 109 } else {
110 $page->trig("il n'y a pas d'utilisateur avec cet alias");
111 $alias = false;
112 }
113 return $alias;
114
115 } else {
116
08cce2ff 117 $res = XDB::query("SELECT alias
0337d704 118 FROM aliases AS a
119 INNER JOIN emails AS e ON e.uid=a.id
120 WHERE e.email={?} AND a.type='a_vie'", $data);
121 switch ($i = $res->numRows()) {
122 case 0:
123 $page->trig("il n'y a pas d'utilisateur avec cette addresse mail");
124 return false;
125
126 case 1:
127 return $res->fetchOneCell();
128
129 default:
cab08090 130 if (S::has_perms()) {
0337d704 131 $aliases = $res->fetchColumn();
132 $page->trig("Il y a $i utilisateurs avec cette adresse mail : ".join(', ', $aliases));
133 } else {
134 $res->free();
135 }
136 }
137 }
138
139 return false;
140}
141
142// }}}
143// {{{ function get_user_forlife()
144
145function get_user_forlife($data) {
146 return get_user_login($data, true);
147}
148
149// }}}
e7545178 150// {{{ function get_users_forlife_list()
151
152function get_users_forlife_list($members, $strict = false)
153{
154 if (strlen(trim($members)) == 0) {
155 return null;
156 }
157 $members = explode(' ', $members);
158 if ($members) {
159 $list = array();
160 foreach ($members as $i => $alias) {
161 if (($login = get_user_forlife($alias)) !== false) {
162 $list[$i] = $login;
163 } else if(!$strict) {
164 $list[$i] = $alias;
165 }
166 }
167 return $list;
168 }
169 return null;
170}
171
172// }}}
8b10988d 173// {{{ function has_user_right()
174function has_user_right($pub, $view = 'private') {
175 if ($pub == $view) return true;
176 // all infos available for private
177 if ($view == 'private') return true;
178 // public infos available for all
179 if ($pub == 'public') return true;
180 // here we have view = ax or public, and pub = ax or private, and pub != view
181 return false;
182}
183// }}}
9039c94d 184// {{{ function get_user_details_pro()
185
8b10988d 186function get_user_details_pro($uid, $view = 'private')
9039c94d 187{
9039c94d 188 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
dd502514 189 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city, e.entrid,
9039c94d 190 gp.pays AS countrytxt, gr.name AS region, e.tel, e.fax, e.mobile, e.entrid,
191 e.pub, e.adr_pub, e.tel_pub, e.email, e.email_pub, e.web
192 FROM entreprises AS e
193 LEFT JOIN emploi_secteur AS s ON(e.secteur = s.id)
194 LEFT JOIN emploi_ss_secteur AS ss ON(e.ss_secteur = ss.id AND e.secteur = ss.secteur)
195 LEFT JOIN fonctions_def AS f ON(e.fonction = f.id)
196 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
197 LEFT JOIN geoloc_region AS gr ON (gr.a2 = e.country and gr.region = e.region)
198 WHERE e.uid = {?}
199 ORDER BY e.entrid";
08cce2ff 200 $res = XDB::query($sql, $uid);
8b10988d 201 $all_pro = $res->fetchAllAssoc();
202 foreach ($all_pro as $i => $pro) {
203 if (!has_user_right($pro['pub'], $view))
204 unset($all_pro[$i]);
205 else {
206 if (!has_user_right($pro['adr_pub'], $view)) {
dd502514 207 if ($pro['adr1'] == '' &&
208 $pro['adr2'] == '' &&
209 $pro['adr3'] == '' &&
210 $pro['postcode'] == '' &&
211 $pro['city'] == '' &&
212 $pro['countrytxt'] == '' &&
213 $pro['region'] == '') {
214 $all_pro[$i]['adr_pub'] = $view;
215 } else {
216 $all_pro[$i]['adr1'] = '';
217 $all_pro[$i]['adr2'] = '';
218 $all_pro[$i]['adr3'] = '';
219 $all_pro[$i]['postcode'] = '';
220 $all_pro[$i]['city'] = '';
221 $all_pro[$i]['countrytxt'] = '';
222 $all_pro[$i]['region'] = '';
223 }
8b10988d 224 }
225 if (!has_user_right($pro['tel_pub'], $view)) {
dd502514 226 // if no tel was defined, then the viewer will be able to write it
227 if ($pro['tel'] == '' &&
228 $pro['fax'] == '' &&
229 $pro['mobile'] == '') {
230 $all_pro[$i]['tel_pub'] = $view;
231 } else {
232 $all_pro[$i]['tel'] = '';
233 $all_pro[$i]['fax'] = '';
234 $all_pro[$i]['mobile'] = '';
235 }
8b10988d 236 }
237 if (!has_user_right($pro['email_pub'], $view)) {
dd502514 238 if ($pro['email'] == '')
239 $all_pro[$i]['email_pub'] = $view;
240 else
241 $all_pro[$i]['email'] = '';
8b10988d 242 }
243 if ($all_pro[$i]['adr1'] == '' &&
244 $all_pro[$i]['adr2'] == '' &&
245 $all_pro[$i]['adr3'] == '' &&
246 $all_pro[$i]['postcode'] == '' &&
247 $all_pro[$i]['city'] == '' &&
248 $all_pro[$i]['countrytxt'] == '' &&
249 $all_pro[$i]['region'] == '' &&
250 $all_pro[$i]['entreprise'] == '' &&
251 $all_pro[$i]['fonction'] == '' &&
252 $all_pro[$i]['secteur'] == '' &&
253 $all_pro[$i]['poste'] == '' &&
254 $all_pro[$i]['tel'] == '' &&
255 $all_pro[$i]['fax'] == '' &&
256 $all_pro[$i]['mobile'] == '' &&
257 $all_pro[$i]['email'] == '')
258 unset($all_pro[$i]);
259 }
260 }
261 if (!count($all_pro)) return false;
262 return $all_pro;
9039c94d 263}
264
265// }}}
8b10988d 266function get_user_details_adr($uid, $view = 'private') {
8b10988d 267 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
268 gp.pays AS countrytxt,a.region, a.regiontxt,
269 FIND_IN_SET('active', a.statut) AS active, a.adrid,
270 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
271 a.pub, gp.display
272 FROM adresses AS a
273 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
274 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
275 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
08cce2ff 276 $res = XDB::query($sql, $uid);
8b10988d 277 $all_adr = $res->fetchAllAssoc();
278 $adrid_index = array();
279 foreach ($all_adr as $i => $adr) {
280 if (!has_user_right($adr['pub'], $view))
281 unset($all_adr[$i]);
282 else
283 $adrid_index[$adr['adrid']] = $i;
284 }
285
286 $sql = "SELECT t.adrid, t.tel_pub, t.tel_type, t.tel, t.telid
287 FROM tels AS t
288 INNER JOIN adresses AS a ON (a.uid = t.uid) AND (a.adrid = t.adrid)
289 WHERE t.uid = {?} AND NOT FIND_IN_SET('pro',a.statut)
290 ORDER BY t.adrid, t.tel_type DESC, t.telid";
08cce2ff 291 $restel = XDB::iterator($sql, $uid);
8b10988d 292 while ($nexttel = $restel->next()) {
293 if (has_user_right($nexttel['tel_pub'], $view)) {
294 $adrid = $nexttel['adrid'];
295 unset($nexttel['adrid']);
296 if (isset($adrid_index[$adrid])) {
297 if (!isset($all_adr[$adrid_index[$adrid]]['tels']))
298 $all_adr[$adrid_index[$adrid]]['tels'] = array($nexttel);
299 else
300 $all_adr[$adrid_index[$adrid]]['tels'][] = $nexttel;
301 }
302 }
303 }
304 return $all_adr;
305}
0337d704 306// {{{ function get_user_details()
307
8b10988d 308function &get_user_details($login, $from_uid = '', $view = 'private')
0337d704 309{
0337d704 310 $reqsql = "SELECT u.user_id, u.promo, u.promo_sortie, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
311 u.perms IN ('admin','user') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
312 q.profile_nick AS nickname, q.profile_from_ax, q.profile_mobile AS mobile, q.profile_web AS web, q.profile_freetext AS freetext,
313 q.profile_mobile_pub AS mobile_pub, q.profile_web_pub AS web_pub, q.profile_freetext_pub AS freetext_pub,
314 q.profile_medals_pub AS medals_pub,
315 IF(gp.nat='',gp.pays,gp.nat) AS nationalite, gp.a2 AS iso3166,
316 a.alias AS forlife, a2.alias AS bestalias,
317 c.uid IS NOT NULL AS is_contact,
318 s.text AS section, p.x, p.y, p.pub AS photo_pub,
dd502514 319 u.matricule_ax,
0337d704 320 m.expertise != '' AS is_referent
321
322 FROM auth_user_md5 AS u
323 INNER JOIN auth_user_quick AS q USING(user_id)
324 INNER JOIN aliases AS a ON (u.user_id=a.id AND a.type='a_vie')
325 INNER JOIN aliases AS a2 ON (u.user_id=a2.id AND FIND_IN_SET('bestalias',a2.flags))
326 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
327 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = u.nationalite)
328 INNER JOIN sections AS s ON (s.id = u.section)
329 LEFT JOIN photo AS p ON (p.uid = u.user_id)
330 LEFT JOIN mentor AS m ON (m.uid = u.user_id)
331 WHERE a.alias = {?}";
08cce2ff 332 $res = XDB::query($reqsql, $from_uid, $login);
0337d704 333 $user = $res->fetchOneAssoc();
334 $uid = $user['user_id'];
8b10988d 335 // hide orange status, cv, nickname, section
336 if (!has_user_right('private', $view)) {
337 $user['promo_sortie'] = $user['promo'] + 3;
338 $user['cv'] = '';
339 $user['nickname'] = '';
340 $user['section'] = '';
341 }
342 // hide mobile
dd502514 343 if (!has_user_right($user['mobile_pub'], $view)) {
344 if ($user['mobile'] == '')
345 $user['mobile_pub'] = $view;
346 else
347 $user['mobile'] = '';
348 }
8b10988d 349 // hide web
dd502514 350 if (!has_user_right($user['web_pub'], $view)) {
351 if ($user['web'] == '')
352 $user['web_pub'] = $view;
353 else
354 $user['web'] = '';
355 }
8b10988d 356 // hide freetext
dd502514 357 if (!has_user_right($user['freetext_pub'], $view)) {
358 if ($user['freetext'] == '')
359 $user['freetext_pub'] = $view;
360 else
361 $user['freetext'] = '';
362 }
0337d704 363
8b10988d 364 $user['adr_pro'] = get_user_details_pro($uid, $view);
365 $user['adr'] = get_user_details_adr($uid, $view);
0337d704 366
8b10988d 367 if (has_user_right('private', $view)) {
368 $sql = "SELECT text
369 FROM binets_ins
370 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
371 WHERE user_id = {?}";
08cce2ff 372 $res = XDB::query($sql, $uid);
8b10988d 373 $user['binets'] = $res->fetchColumn();
374 $user['binets_join'] = join(', ', $user['binets']);
79a5acea 375
08cce2ff 376 $res = XDB::iterRow("SELECT text, url
8b10988d 377 FROM groupesx_ins
378 LEFT JOIN groupesx_def ON groupesx_ins.gid = groupesx_def.id
379 WHERE guid = {?}", $uid);
380 $user['gpxs'] = Array();
381 while (list($gxt, $gxu) = $res->next()) {
382 $user['gpxs'][] = $gxu ? "<a href=\"$gxu\">$gxt</a>" : $gxt;
383 }
384 $user['gpxs_join'] = join(', ', $user['gpxs']);
79a5acea 385 }
0337d704 386
08cce2ff 387 $res = XDB::iterRow("SELECT applis_def.text, applis_def.url, applis_ins.type
0337d704 388 FROM applis_ins
389 INNER JOIN applis_def ON applis_def.id = applis_ins.aid
390 WHERE uid={?}
391 ORDER BY ordre", $uid);
392
393 $user['applis_fmt'] = Array();
dd502514 394 $user['formation'] = Array();
0337d704 395 while (list($txt, $url, $type) = $res->next()) {
dd502514 396 $user['formation'][] = $txt." ".$type;
0337d704 397 require_once('applis.func.inc.php');
398 $user['applis_fmt'][] = applis_fmt($type, $txt, $url);
399 }
400 $user['applis_join'] = join(', ', $user['applis_fmt']);
401
8b10988d 402 if (has_user_right($user['medals_pub'], $view)) {
08cce2ff 403 $res = XDB::iterator("SELECT m.id, m.text AS medal, m.type, m.img, s.gid, g.text AS grade
8b10988d 404 FROM profile_medals_sub AS s
405 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
406 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
407 WHERE s.uid = {?}", $uid);
408 $user['medals'] = Array();
409 while ($tmp = $res->next()) {
410 $user['medals'][] = $tmp;
411 }
0337d704 412 }
413
414 return $user;
415}
dd502514 416// }}}
417// {{{ function add_user_address()
418function add_user_address($uid, $adrid, $adr) {
08cce2ff 419 XDB::execute(
dd502514 420 "INSERT INTO adresses (`uid`, `adrid`, `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `datemaj`, `pub`) (
421 SELECT u.user_id, {?}, {?}, {?}, {?}, {?}, {?}, gp.a2, NOW(), {?}
422 FROM auth_user_md5 AS u
423 LEFT JOIN geoloc_pays AS gp ON (gp.pays LIKE {?} OR gp.country LIKE {?} OR gp.a2 LIKE {?})
424 WHERE u.user_id = {?}
425 LIMIT 1)",
426 $adrid, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['postcode'], $adr['city'], $adr['pub'], $adr['countrytxt'], $adr['countrytxt'], $adr['countrytxt'], $uid);
427 if (isset($adr['tels']) && is_array($adr['tels'])) {
428 $telid = 0;
429 foreach ($adr['tels'] as $tel) if ($tel['tel']) {
430 add_user_tel($uid, $adrid, $telid, $tel);
431 $telid ++;
432 }
433 }
434}
435// }}}
436// {{{ function update_user_address()
437function update_user_address($uid, $adrid, $adr) {
dd502514 438 // update address
08cce2ff 439 XDB::execute(
dd502514 440 "UPDATE adresses AS a LEFT JOIN geoloc_pays AS gp ON (gp.pays = {?})
441 SET `adr1` = {?}, `adr2` = {?}, `adr3` = {?},
442 `postcode` = {?}, `city` = {?}, a.`country` = gp.a2, `datemaj` = NOW(), `pub` = {?}
443 WHERE adrid = {?} AND uid = {?}",
444 $adr['country_txt'],
445 $adr['adr1'], $adr['adr2'], $adr['adr3'],
446 $adr['postcode'], $adr['city'], $adr['pub'], $adrid, $uid);
447 if (isset($adr['tels']) && is_array($adr['tels'])) {
08cce2ff 448 $res = XDB::query("SELECT telid FROM tels WHERE uid = {?} AND adrid = {?} ORDER BY telid", $uid, $adrid);
dd502514 449 $telids = $res->fetchColumn();
450 foreach ($adr['tels'] as $tel) {
451 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
452 remove_user_tel($uid, $adrid, $tel['telid']);
453 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
454 } else if (isset($tel['telid'])) {
455 update_user_tel($uid, $adrid, $tel['telid'], $tel);
456 } else {
457 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
458 add_user_tel($uid, $adrid, $telid, $tel);
459 }
460 }
461 }
462}
463// }}}
464// {{{ function remove_user_address()
465function remove_user_address($uid, $adrid) {
08cce2ff 466 XDB::execute("DELETE FROM adresses WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
467 XDB::execute("DELETE FROM tels WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
dd502514 468}
469// }}}
470// {{{ function add_user_tel()
471function add_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 472 XDB::execute(
dd502514 473 "INSERT INTO tels SET uid = {?}, adrid = {?}, telid = {?}, tel = {?}, tel_type = {?}, tel_pub = {?}",
474 $uid, $adrid, $telid, $tel['tel'], $tel['tel_type'], $tel['tel_pub']);
475}
476// }}}
477// {{{ function update_user_tel()
478function update_user_tel($uid, $adrid, $telid, $tel) {
08cce2ff 479 XDB::execute(
dd502514 480 "UPDATE tels SET tel = {?}, tel_type = {?}, tel_pub = {?}
481 WHERE telid = {?} AND adrid = {?} AND uid = {?}",
482 $tel['tel'], $tel['tel_type'], $tel['tel_pub'],
483 $telid, $adrid, $uid);
484}
485// }}}
486// {{{ function remove_user_tel()
487function remove_user_tel($uid, $adrid, $telid) {
a3a049fc 488 XDB::execute("DELETE FROM tels WHERE telid = {?} AND adrid = {?} AND uid = {?}",
489 $telid, $adrid, $uid);
dd502514 490}
491// }}}
492// {{{ function add_user_pro()
493function add_user_pro($uid, $entrid, $pro) {
08cce2ff 494 XDB::execute(
dd502514 495 "INSERT INTO entreprises (`uid`, `entrid`, `entreprise`, `poste`, `secteur`, `ss_secteur`, `fonction`,
496 `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `region`, `tel`, `fax`, `mobile`, `email`, `web`, `pub`, `adr_pub`, `tel_pub`, `email_pub`)
497 SELECT u.user_id, {?}, {?}, {?}, s.id, ss.id, f.id,
498 {?}, {?}, {?}, {?}, {?}, gp.a2, gr.region, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}
499 FROM auth_user_md5 AS u
500 LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
501 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
502 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})
503 LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
504 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})
505 WHERE u.user_id = {?}
506 LIMIT 1",
507 $entrid, $pro['entreprise'], $pro['poste'],
508 $pro['adr1'], $pro['adr2'], $pro['adr3'], $pro['postcode'], $pro['city'], $pro['tel'], $pro['fax'], $pro['mobile'], $pro['email'], $pro['web'], $pro['pub'], $pro['adr_pub'], $pro['tel_pub'], $pro['email_pub'],
509 $pro['secteur'], $pro['sous_secteur'], $pro['fonction'], $pro['fonction'],
510 $pro['countrytxt'], $pro['countrytxt'], $pro['region'],
511 $uid);
512}
513// }}}
514// {{{ function update_user_pro()
515function update_user_pro($uid, $entrid, $pro) {
dd502514 516 $join = "";
517 $set = "";
518 $args_join = array();
519 $args_set = array();
520
521 $join .= "LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
522 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
523 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})";
524 $args_join[] = $pro['secteur'];
525 $args_join[] = $pro['sous_secteur'];
526 $args_join[] = $pro['fonction'];
527 $args_join[] = $pro['fonction'];
528 $set .= ", e.`entreprise` = {?}, e.`secteur` = s.id, e.`ss_secteur` = ss.id, e.`fonction` = f.id, e.`poste`= {?}, e.`web` = {?}, e.`pub` = {?}";
529 $args_set[] = $pro['entreprise'];
530 $args_set[] = $pro['poste'];
531 $args_set[] = $pro['web'];
532 $args_set[] = $pro['pub'];
533
534 if (isset($pro['adr1'])) {
535 $join .= "LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
536 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})";
537 $args_join[] = $pro['countrytxt'];
538 $args_join[] = $pro['countrytxt'];
539 $args_join[] = $pro['region'];
540 $set .= ", e.`adr1` = {?}, e.`adr2` = {?}, e.`adr3` = {?}, e.`postcode` = {?}, e.`city` = {?}, e.`country` = gp.a2, e.`region` = gr.region, e.`adr_pub` = {?}";
541 $args_set[] = $pro['adr1'];
542 $args_set[] = $pro['adr2'];
543 $args_set[] = $pro['adr3'];
544 $args_set[] = $pro['postcode'];
545 $args_set[] = $pro['city'];
546 $args_set[] = $pro['adr_pub'];
547 }
548
549 if (isset($pro['tel'])) {
550 $set .= ", e.`tel` = {?}, e.`fax` = {?}, e.`mobile` = {?}, e.tel_pub = {?}";
551 $args_set[] = $pro['tel'];
552 $args_set[] = $pro['fax'];
553 $args_set[] = $pro['mobile'];
554 $args_set[] = $pro['tel_pub'];
555 }
556 if (isset($pro['email'])) {
557 $set .= ", e.`email` = {?}, e.`email_pub` = {?}";
558 $args_set[] = $pro['email'];
559 $args_set[] = $pro['email_pub'];
560 }
561 $query = "UPDATE entreprises AS e ".$join." SET ".substr($set,1)." WHERE e.uid = {?} AND e.entrid = {?}";
562 $args_where = array($uid, $entrid);
563 $args = array_merge(array($query), $args_join, $args_set, $args_where);
a3a049fc 564 call_user_func_array(array('XDB', 'execute'), $args);
dd502514 565}
566// }}}
567// {{{ function remove_user_pro()
568function remove_user_pro($uid, $entrid) {
08cce2ff 569 XDB::execute("DELETE FROM entreprises WHERE entrid = {?} AND uid = {?}", $entrid, $uid);
dd502514 570}
571// }}}
572// {{{ function set_user_details()
573function set_user_details_addresses($uid, $adrs) {
08cce2ff 574 $res = XDB::query("SELECT adrid FROM adresses WHERE uid = {?} AND adrid >= 1 ORDER BY adrid", $uid);
dd502514 575 $adrids = $res->fetchColumn();
576 foreach ($adrs as $adr) {
577 if (isset($adr['adrid']) && isset($adr['remove']) && $adr['remove']) {
578 remove_user_address($uid, $adr['adrid']);
579 if (isset($adrids[$adr['adrid']])) unset($adrids[$adr['adrid']]);
580 } else if (isset($adr['adrid'])) {
581 update_user_address($uid, $adr['adrid'], $adr);
582 } else {
583 for ($adrid = 1; isset($adrids[$adrid-1]) && ($adrids[$adrid-1] == $adrid); $adrid++);
584 add_user_address($uid, $adrid, $adr);
585 $adrids[$adrid-1] = $adrid;
586 }
587 }
588 require_once 'geoloc.inc.php';
589 localize_addresses($uid);
590}
591// }}}
592// {{{ function set_user_details_pro()
593
594function set_user_details_pro($uid, $pros)
595{
08cce2ff 596 $res = XDB::query("SELECT entrid FROM entreprises WHERE uid = {?} ORDER BY entrid", $uid);
dd502514 597 $entrids = $res->fetchColumn();
598 foreach ($pros as $pro) {
599 if (isset($pro['entrid']) && isset($pro['remove']) && $pro['remove']) {
600 remove_user_pro($uid, $pro['entrid']);
601 if (isset($entrids[$pro['entrid']])) unset($entrids[$pro['entrid']]);
602 } else if (isset($pro['entrid'])) {
603 update_user_pro($uid, $pro['entrid'], $pro);
604 } else {
605 for ($entrid = 0; isset($entrids[$entrid]) && ($entrids[$entrid] == $entrid); $entrid++);
606 add_user_pro($uid, $entrid, $pro);
607 }
608 }
609}
0337d704 610
611// }}}
dd502514 612// {{{ function set_user_details()
613function set_user_details($uid, $details) {
dd502514 614 if (isset($details['nom_usage'])) {
08cce2ff 615 XDB::execute("UPDATE auth_user_md5 SET nom_usage = {?} WHERE user_id = {?}", strtoupper($details['nom_usage']), $uid);
dd502514 616 }
617 if (isset($details['mobile'])) {
08cce2ff 618 XDB::execute("UPDATE auth_user_quick SET profile_mobile = {?} WHERE user_id = {?}", $details['mobile'], $uid);
dd502514 619 }
620 if (isset($details['nationalite'])) {
08cce2ff 621 XDB::execute(
dd502514 622 "UPDATE auth_user_md5 AS u
623 INNER JOIN geoloc_pays AS gp
624 SET u.nationalite = gp.a2
625 WHERE (gp.a2 = {?} OR gp.nat = {?})
626 AND u.user_id = {?}", $details['nationalite'], $details['nationalite'], $uid);
627 }
628 if (isset($details['adr']) && is_array($details['adr']))
629 set_user_details_addresses($uid, $details['adr']);
630 if (isset($details['adr_pro']) && is_array($details['adr_pro']))
631 set_user_details_pro($uid, $details['adr_pro']);
632 if (isset($details['binets']) && is_array($details['binets'])) {
08cce2ff 633 XDB::execute("DELETE FROM binets_ins WHERE user_id = {?}", $uid);
dd502514 634 foreach ($details['binets'] as $binet)
08cce2ff 635 XDB::execute(
dd502514 636 "INSERT INTO binets_ins (`user_id`, `binet_id`)
637 SELECT {?}, id FROM binets_def WHERE text = {?} LIMIT 1",
638 $uid, $binet);
639 }
640 if (isset($details['gpxs']) && is_array($details['gpxs'])) {
08cce2ff 641 XDB::execute("DELETE FROM groupesx_ins WHERE user_id = {?}", $uid);
dd502514 642 foreach ($details['gpxs'] as $groupex) {
643 if (preg_match('/<a href="[^"]*">([^<]+)</a>/', $groupex, $a)) $groupex = $a[1];
08cce2ff 644 XDB::execute(
dd502514 645 "INSERT INTO groupesx_ins (`user_id`, `binet_id`)
646 SELECT {?}, id FROM groupesx_def WHERE text = {?} LIMIT 1",
647 $uid, $groupex);
648 }
649 }
650 // applis
651 // medals
652}
653// }}}
0337d704 654// {{{ function _user_reindex
655
656function _user_reindex($uid, $keys, $muls) {
0337d704 657 foreach ($keys as $i => $key) {
658 if ($key == '') {
659 continue;
660 }
661 $toks = preg_split('/[ \'\-]+/', $key);
662 $token = "";
663 $first = 5;
664 while ($toks) {
665 $token = strtolower(replace_accent(array_pop($toks) . $token));
666 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
667 mysql_query("REPLACE INTO search_name (token, uid, score) VALUES('$token',$uid,$score)");
668 $first = 0;
669 }
670 }
671}
672
673// }}}
674// {{{ function user_reindex
675
676function user_reindex($uid) {
08cce2ff 677 XDB::execute("DELETE FROM search_name WHERE uid={?}", $uid);
678 $res = XDB::query("SELECT prenom, nom, nom_usage, profile_nick FROM auth_user_md5 INNER JOIN auth_user_quick USING(user_id) WHERE auth_user_md5.user_id = {?}", $uid);
0337d704 679 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2));
680}
681
682// }}}
74c55fd6 683
684function set_new_usage($uid, $usage, $alias=false) {
685 XDB::execute("UPDATE auth_user_md5 set nom_usage={?} WHERE user_id={?}",$usage ,$uid);
686 XDB::execute("DELETE FROM aliases WHERE FIND_IN_SET('usage',flags) AND id={?}", $uid);
687 if ($alias && $usage) {
688 XDB::execute("UPDATE aliases SET flags=flags & 255-1 WHERE id={?}", $uid);
689 XDB::execute("INSERT INTO aliases VALUES({?}, 'alias', 'usage,bestalias', {?}, null)",
690 $alias, $uid);
691 }
692 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
693 if ($r->fetchOneCell() == "") {
694 XDB::execute("UPDATE aliases SET flags = 1 | flags WHERE id = {?} LIMIT 1", $uid);
695 }
696 require_once 'user.func.inc.php';
697 user_reindex($uid);
698}
699
0337d704 700// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
701?>