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