Adapts mentoring to the new sectors.
[platal.git] / include / user.func.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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{
043bbacf 29 // keep datas in : aliases, adresses, tels, profile_education, 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;
d56cb887
VZ
35 $uid = intval($user_id);
36 $user = User::getSilent($uid);
37 list($alias) = explode('@', $user->forlifeEmail());
0337d704 38
5fecdf6d
SJ
39 $tables_to_clear = array('uid' => array('competences_ins', 'entreprises', 'langues_ins', 'profile_mentor_country',
40 'profile_mentor_sector', 'profile_mentor', 'perte_pass', 'watch_sub'),
425f432d 41 'user_id' => array('requests', 'user_changes'));
42
0337d704 43 if ($really_del) {
b235d980 44 array_push($tables_to_clear['uid'], 'emails', 'groupex.membres', 'contacts', 'adresses', 'profile_phones',
425f432d 45 'photo', 'perte_pass', 'langues_ins', 'forums.abos', 'forums.profils');
46 array_push($tables_to_clear['user_id'], 'newsletter_ins', 'auth_user_quick', 'binets_ins');
787bb3d7 47 $tables_to_clear['id'] = array('aliases');
425f432d 48 $tables_to_clear['contact'] = array('contacts');
425f432d 49 XDB::execute("UPDATE auth_user_md5
50 SET date_ins = 0, promo_sortie = 0, nom_usage = '', password = '', perms = 'pending',
eeb2ec0f
SJ
51 nationalite = '', nationalite2 = '', nationalite3 = '', cv = '', section = 0,
52 date = 0, smtppass = '', mail_storage = ''
425f432d 53 WHERE user_id = {?}", $uid);
54 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
55 $alias.'@'.$globals->mail->domain);
56 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
57 $alias.'@'.$globals->mail->domain2);
58 } else {
59 XDB::execute("UPDATE auth_user_md5 SET password='',smtppass='' WHERE user_id={?}", $uid);
60 XDB::execute("UPDATE auth_user_quick SET watch_flags='' WHERE user_id={?}", $uid);
0337d704 61 }
62
08cce2ff 63 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain);
64 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain2);
65
425f432d 66 foreach ($tables_to_clear as $key=>&$tables) {
67 foreach ($tables as $table) {
68 XDB::execute("DELETE FROM $table WHERE $key={?}", $uid);
69 }
70 }
9bb8bf21 71
c53b5964 72 $mmlist = new MMList(S::v('uid'), S::v('password'));
9bb8bf21 73 $mmlist->kill($alias, $really_del);
84270653
VZ
74
75 // Deactivates, when available, the Google Apps account of the user.
76 if ($globals->mailstorage->googleapps_domain) {
77 require_once 'googleapps.inc.php';
14870e88 78 if (GoogleAppsAccount::account_status($uid)) {
d56cb887 79 $account = new GoogleAppsAccount($user);
14870e88
VZ
80 $account->suspend();
81 }
84270653 82 }
0337d704 83}
84
85// }}}
8b10988d 86// {{{ function has_user_right()
87function has_user_right($pub, $view = 'private') {
88 if ($pub == $view) return true;
787bb3d7 89 // all infos available for private
8b10988d 90 if ($view == 'private') return true;
787bb3d7 91 // public infos available for all
8b10988d 92 if ($pub == 'public') return true;
93 // here we have view = ax or public, and pub = ax or private, and pub != view
787bb3d7 94 return false;
8b10988d 95}
96// }}}
0baf0741 97// {{{ function get_not_registered_user()
98
99function get_not_registered_user($login, $iterator = false)
100{
101 global $globals;
102 @list($login, $domain) = explode('@', $login);
103 if ($domain && $domain != $globals->mail->domain && $domain != $globals->mail->domain2) {
104 return null;
105 }
106 @list($prenom, $nom, $promo) = explode('.', $login);
107 $where = 'REPLACE(REPLACE(REPLACE(nom, " ", ""), "-", ""), "\'", "") LIKE CONCAT("%", {?}, "%")
108 AND REPLACE(REPLACE(REPLACE(prenom, " ", ""), "-", ""), "\'", "") LIKE CONCAT("%", {?}, "%")';
109 if ($promo) {
110 if (preg_match('/^[0-9]{2}$/', $promo)) {
111 $where .= 'AND MOD(promo, 100) = {?}';
112 } elseif (preg_match('/^[0-9]{4}$/', $promo)) {
113 $where .= 'AND promo = {?}';
114 }
115 }
116 $sql = "SELECT user_id, nom, prenom, promo
117 FROM auth_user_md5
1555e55e 118 WHERE $where AND perms = 'pending'
787bb3d7 119 ORDER BY promo, nom, prenom";
0baf0741 120 if ($iterator) {
121 return XDB::iterator($sql, $nom, $prenom, $promo);
122 } else {
123 $res = XDB::query($sql, $nom, $prenom, $promo);
124 return $res->fetchAllAssoc();
125 }
126}
127
128// }}}
9039c94d 129// {{{ function get_user_details_pro()
130
8b10988d 131function get_user_details_pro($uid, $view = 'private')
9039c94d 132{
9039c94d 133 $sql = "SELECT e.entreprise, s.label as secteur , ss.label as sous_secteur , f.fonction_fr as fonction,
dd502514 134 e.poste, e.adr1, e.adr2, e.adr3, e.postcode, e.city, e.entrid,
bde2be3b
GB
135 gp.pays AS countrytxt, gr.name AS region, e.entrid,
136 e.pub, e.adr_pub, e.email, e.email_pub, e.web
9039c94d 137 FROM entreprises AS e
138 LEFT JOIN emploi_secteur AS s ON(e.secteur = s.id)
139 LEFT JOIN emploi_ss_secteur AS ss ON(e.ss_secteur = ss.id AND e.secteur = ss.secteur)
140 LEFT JOIN fonctions_def AS f ON(e.fonction = f.id)
141 LEFT JOIN geoloc_pays AS gp ON (gp.a2 = e.country)
142 LEFT JOIN geoloc_region AS gr ON (gr.a2 = e.country and gr.region = e.region)
143 WHERE e.uid = {?}
144 ORDER BY e.entrid";
08cce2ff 145 $res = XDB::query($sql, $uid);
8b10988d 146 $all_pro = $res->fetchAllAssoc();
147 foreach ($all_pro as $i => $pro) {
148 if (!has_user_right($pro['pub'], $view))
149 unset($all_pro[$i]);
150 else {
151 if (!has_user_right($pro['adr_pub'], $view)) {
dd502514 152 if ($pro['adr1'] == '' &&
153 $pro['adr2'] == '' &&
154 $pro['adr3'] == '' &&
155 $pro['postcode'] == '' &&
156 $pro['city'] == '' &&
157 $pro['countrytxt'] == '' &&
158 $pro['region'] == '') {
159 $all_pro[$i]['adr_pub'] = $view;
160 } else {
161 $all_pro[$i]['adr1'] = '';
162 $all_pro[$i]['adr2'] = '';
163 $all_pro[$i]['adr3'] = '';
164 $all_pro[$i]['postcode'] = '';
165 $all_pro[$i]['city'] = '';
166 $all_pro[$i]['countrytxt'] = '';
167 $all_pro[$i]['region'] = '';
168 }
8b10988d 169 }
bde2be3b
GB
170 $sql = "SELECT pub AS tel_pub, tel_type, display_tel AS tel, comment
171 FROM profile_phones AS t
172 WHERE uid = {?} AND link_type = 'pro' AND link_id = {?}
173 ORDER BY link_id, tel_type DESC, tel_id";
174 $restel = XDB::iterator($sql, $uid, $pro['entrid']);
175 while ($nexttel = $restel->next()) {
176 if (has_user_right($nexttel['tel_pub'], $view)) {
177 if (!isset($all_pro[$i]['tels'])) {
178 $all_pro[$i]['tels'] = array($nexttel);
179 } else {
180 $all_pro[$i]['tels'][] = $nexttel;
181 }
dd502514 182 }
8b10988d 183 }
184 if (!has_user_right($pro['email_pub'], $view)) {
dd502514 185 if ($pro['email'] == '')
186 $all_pro[$i]['email_pub'] = $view;
187 else
188 $all_pro[$i]['email'] = '';
8b10988d 189 }
190 if ($all_pro[$i]['adr1'] == '' &&
191 $all_pro[$i]['adr2'] == '' &&
192 $all_pro[$i]['adr3'] == '' &&
193 $all_pro[$i]['postcode'] == '' &&
194 $all_pro[$i]['city'] == '' &&
195 $all_pro[$i]['countrytxt'] == '' &&
196 $all_pro[$i]['region'] == '' &&
197 $all_pro[$i]['entreprise'] == '' &&
198 $all_pro[$i]['fonction'] == '' &&
199 $all_pro[$i]['secteur'] == '' &&
200 $all_pro[$i]['poste'] == '' &&
bde2be3b 201 (!isset($all_pro[$i]['tels'])) &&
8b10988d 202 $all_pro[$i]['email'] == '')
203 unset($all_pro[$i]);
204 }
205 }
206 if (!count($all_pro)) return false;
207 return $all_pro;
9039c94d 208}
209
210// }}}
433336f3 211// {{{ function get_user_details_adr()
212
8b10988d 213function get_user_details_adr($uid, $view = 'private') {
8b10988d 214 $sql = "SELECT a.adrid, a.adr1,a.adr2,a.adr3,a.postcode,a.city,
215 gp.pays AS countrytxt,a.region, a.regiontxt,
216 FIND_IN_SET('active', a.statut) AS active, a.adrid,
217 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
5d42c993 218 FIND_IN_SET('courrier', a.statut) AS courier,
646a4582 219 a.pub, gp.display, a.comment
8b10988d 220 FROM adresses AS a
221 LEFT JOIN geoloc_pays AS gp ON (gp.a2=a.country)
222 WHERE uid= {?} AND NOT FIND_IN_SET('pro',a.statut)
223 ORDER BY NOT FIND_IN_SET('active',a.statut), FIND_IN_SET('temporaire',a.statut), FIND_IN_SET('res-secondaire',a.statut)";
08cce2ff 224 $res = XDB::query($sql, $uid);
8b10988d 225 $all_adr = $res->fetchAllAssoc();
226 $adrid_index = array();
227 foreach ($all_adr as $i => $adr) {
228 if (!has_user_right($adr['pub'], $view))
229 unset($all_adr[$i]);
230 else
231 $adrid_index[$adr['adrid']] = $i;
232 }
787bb3d7 233
bde2be3b 234 $sql = "SELECT link_id AS adrid, pub AS tel_pub, tel_type, display_tel AS tel, tel_id AS telid, comment
b235d980 235 FROM profile_phones AS t
5e4417a9
GB
236 WHERE uid = {?} AND link_type = 'address'
237 ORDER BY link_id, tel_type DESC, tel_id";
08cce2ff 238 $restel = XDB::iterator($sql, $uid);
8b10988d 239 while ($nexttel = $restel->next()) {
240 if (has_user_right($nexttel['tel_pub'], $view)) {
241 $adrid = $nexttel['adrid'];
242 unset($nexttel['adrid']);
243 if (isset($adrid_index[$adrid])) {
787bb3d7 244 if (!isset($all_adr[$adrid_index[$adrid]]['tels']))
8b10988d 245 $all_adr[$adrid_index[$adrid]]['tels'] = array($nexttel);
246 else
247 $all_adr[$adrid_index[$adrid]]['tels'][] = $nexttel;
248 }
249 }
250 }
251 return $all_adr;
252}
433336f3 253
254// }}}
0337d704 255// {{{ function get_user_details()
256
8b10988d 257function &get_user_details($login, $from_uid = '', $view = 'private')
0337d704 258{
fb2c09c9 259 $reqsql = "SELECT u.user_id, d.promo_display, u.prenom, u.nom, u.nom_usage, u.date, u.cv,
f6c71069 260 u.perms IN ('admin','user','disabled') AS inscrit, FIND_IN_SET('femme', u.flags) AS sexe, u.deces != 0 AS dcd, u.deces,
bde2be3b
GB
261 q.profile_nick AS nickname, q.profile_from_ax, q.profile_freetext AS freetext,
262 q.profile_freetext_pub AS freetext_pub,
078dc884 263 q.profile_medals_pub AS medals_pub, co.corps_pub AS corps_pub,
eeb2ec0f
SJ
264 IF(gp1.nat='',gp1.pays,gp1.nat) AS nationalite, gp1.a2 AS iso3166_1,
265 IF(gp2.nat='',gp2.pays,gp2.nat) AS nationalite2, gp2.a2 AS iso3166_2,
266 IF(gp3.nat='',gp3.pays,gp3.nat) AS nationalite3, gp3.a2 AS iso3166_3,
0337d704 267 a.alias AS forlife, a2.alias AS bestalias,
268 c.uid IS NOT NULL AS is_contact,
269 s.text AS section, p.x, p.y, p.pub AS photo_pub,
dd502514 270 u.matricule_ax,
d9caac6f 271 m.expertise != '' AS is_referent,
cd5bd7dc
PC
272 (COUNT(e.email) > 0 OR FIND_IN_SET('googleapps', u.mail_storage) > 0) AS actif,
273 nd.display AS name_display, nd.tooltip AS name_tooltip
fb2c09c9
SJ
274 FROM auth_user_md5 AS u
275 INNER JOIN auth_user_quick AS q USING(user_id)
276 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = 'a_vie')
277 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET('bestalias', a2.flags))
278 LEFT JOIN contacts AS c ON (c.uid = {?} and c.contact = u.user_id)
279 LEFT JOIN profile_corps AS co ON (co.uid = u.user_id)
280 LEFT JOIN geoloc_pays AS gp1 ON (gp1.a2 = u.nationalite)
281 LEFT JOIN geoloc_pays AS gp2 ON (gp2.a2 = u.nationalite2)
282 LEFT JOIN geoloc_pays AS gp3 ON (gp3.a2 = u.nationalite3)
283 INNER JOIN sections AS s ON (s.id = u.section)
284 LEFT JOIN photo AS p ON (p.uid = u.user_id)
5fecdf6d 285 LEFT JOIN profile_mentor AS m ON (m.uid = u.user_id)
fb2c09c9
SJ
286 LEFT JOIN emails AS e ON (e.uid = u.user_id AND e.flags='active')
287 INNER JOIN profile_names_display AS nd ON (nd.user_id = u.user_id)
288 INNER JOIN profile_display AS d ON (d.uid = u.user_id)
d9caac6f 289 WHERE a.alias = {?}
290 GROUP BY u.user_id";
08cce2ff 291 $res = XDB::query($reqsql, $from_uid, $login);
0337d704 292 $user = $res->fetchOneAssoc();
293 $uid = $user['user_id'];
8b10988d 294 // hide orange status, cv, nickname, section
295 if (!has_user_right('private', $view)) {
8b10988d 296 $user['cv'] = '';
297 $user['nickname'] = '';
298 $user['section'] = '';
299 }
1052148d 300
8b10988d 301 // hide freetext
dd502514 302 if (!has_user_right($user['freetext_pub'], $view)) {
303 if ($user['freetext'] == '')
304 $user['freetext_pub'] = $view;
305 else
306 $user['freetext'] = '';
307 }
0337d704 308
bde2be3b
GB
309 $sql = "SELECT pub AS tel_pub, tel_type, display_tel AS tel, comment
310 FROM profile_phones AS t
311 WHERE uid = {?} AND link_type = 'user'
312 ORDER BY tel_type DESC, tel_id";
313 $restel = XDB::iterator($sql, $uid);
314 while ($nexttel = $restel->next()) {
315 if (has_user_right($nexttel['tel_pub'], $view)) {
316 if (!isset($user['tels'])) {
317 $user['tels'] = array($nexttel);
318 } else {
319 $user['tels'][] = $nexttel;
320 }
321 }
322 }
323
8b10988d 324 $user['adr_pro'] = get_user_details_pro($uid, $view);
325 $user['adr'] = get_user_details_adr($uid, $view);
0337d704 326
8b10988d 327 if (has_user_right('private', $view)) {
328 $sql = "SELECT text
329 FROM binets_ins
330 LEFT JOIN binets_def ON binets_ins.binet_id = binets_def.id
331 WHERE user_id = {?}";
08cce2ff 332 $res = XDB::query($sql, $uid);
8b10988d 333 $user['binets'] = $res->fetchColumn();
334 $user['binets_join'] = join(', ', $user['binets']);
787bb3d7 335
b37aacd9
FB
336 $res = XDB::iterRow("SELECT a.diminutif, a.nom, a.site
337 FROM groupex.asso AS a
338 LEFT JOIN groupex.membres AS m ON (m.asso_id = a.id)
339 WHERE m.uid = {?} AND (a.cat = 'GroupesX' OR a.cat = 'Institutions')
340 AND pub = 'public'", $uid);
8b10988d 341 $user['gpxs'] = Array();
ae2f9e35 342 $user['gpxs_name'] = Array();
b37aacd9
FB
343 while (list($gxd, $gxt, $gxu) = $res->next()) {
344 if (!$gxu) {
345 $gxu = 'http://www.polytechnique.net/' . $gxd;
346 }
347 $user['gpxs'][] = '<span title="' . pl_entities($gxt) . "\"><a href=\"$gxu\">$gxd</a></span>";
ae2f9e35 348 $user['gpxs_name'][] = $gxt;
787bb3d7 349 }
8b10988d 350 $user['gpxs_join'] = join(', ', $user['gpxs']);
79a5acea 351 }
0337d704 352
1504ac45
SJ
353 $res = XDB::iterRow("SELECT en.name AS name, en.url AS url, d.degree AS degree,
354 ed.grad_year AS grad_year, f.field AS field, ed.program AS program
043bbacf 355 FROM profile_education AS ed
c7eac294
SJ
356 LEFT JOIN profile_education_enum AS en ON (en.id = ed.eduid)
357 LEFT JOIN profile_education_degree_enum AS d ON (d.id = ed.degreeid)
358 LEFT JOIN profile_education_field_enum AS f ON (f.id = ed.fieldid)
ae2f9e35 359 WHERE uid={?}
043bbacf
SJ
360 ORDER BY ed.grad_year", $uid);
361
1504ac45 362 if (list($name, $url, $degree, $grad_year, $field, $program) = $res->next()) {
c7eac294
SJ
363 require_once('education.func.inc.php');
364 $user['education'][] = education_fmt($name, $url, $degree, $grad_year, $field, $program, $user['sexe'], true);
043bbacf 365 }
1504ac45 366 while (list($name, $url, $degree, $grad_year, $field, $program) = $res->next()) {
c7eac294 367 $user['education'][] = education_fmt($name, $url, $degree, $grad_year, $field, $program, $user['sexe'], true);
0337d704 368 }
0337d704 369
078dc884
SJ
370 if (has_user_right($user['corps_pub'], $view)) {
371 $res = XDB::query("SELECT e1.name AS original, e2.name AS current, r.name AS rank
372 FROM profile_corps AS c
373 LEFT JOIN profile_corps_enum AS e1 ON (c.original_corpsid = e1.id)
374 LEFT JOIN profile_corps_enum AS e2 ON (c.current_corpsid = e2.id)
375 LEFT JOIN profile_corps_rank_enum AS r ON (c.rankid = r.id)
376 WHERE c.uid = {?} AND c.original_corpsid != 1", $uid);
377 if ($res = $res->fetchOneRow()) {
378 list($original, $current, $rank) = $res;
379 $user['corps'] = "Corps d'origine : " . $original . ", corps actuel : " . $current . ", grade : " . $rank;
380 }
381 }
382
8b10988d 383 if (has_user_right($user['medals_pub'], $view)) {
5e70bf24 384 $res = XDB::iterator("SELECT m.id, m.text AS medal, m.type, s.gid, g.text AS grade
ae2f9e35 385 FROM profile_medals_sub AS s
386 INNER JOIN profile_medals AS m ON ( s.mid = m.id )
387 LEFT JOIN profile_medals_grades AS g ON ( s.mid = g.mid AND s.gid = g.gid )
388 WHERE s.uid = {?}", $uid);
8b10988d 389 $user['medals'] = Array();
390 while ($tmp = $res->next()) {
391 $user['medals'][] = $tmp;
392 }
0337d704 393 }
394
d1a2252a 395 $user['networking'] = Array();
dc6378df 396 $res = XDB::iterator("SELECT n.address, n.pub, m.network_type AS type, m.name, m.filter, m.link
d1a2252a
GB
397 FROM profile_networking AS n
398 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
399 WHERE n.uid = {?}", $uid);
400 while($network = $res->next())
401 {
402 if (has_user_right($network['pub'], $view)) {
dc6378df 403 $network['link'] = str_replace('%s', $network['address'], $network['link']);
d1a2252a
GB
404 $user['networking'][] = $network;
405 }
406 }
407
0337d704 408 return $user;
409}
dd502514 410// }}}
411// {{{ function add_user_address()
412function add_user_address($uid, $adrid, $adr) {
08cce2ff 413 XDB::execute(
dd502514 414 "INSERT INTO adresses (`uid`, `adrid`, `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `datemaj`, `pub`) (
415 SELECT u.user_id, {?}, {?}, {?}, {?}, {?}, {?}, gp.a2, NOW(), {?}
416 FROM auth_user_md5 AS u
417 LEFT JOIN geoloc_pays AS gp ON (gp.pays LIKE {?} OR gp.country LIKE {?} OR gp.a2 LIKE {?})
418 WHERE u.user_id = {?}
419 LIMIT 1)",
420 $adrid, $adr['adr1'], $adr['adr2'], $adr['adr3'], $adr['postcode'], $adr['city'], $adr['pub'], $adr['countrytxt'], $adr['countrytxt'], $adr['countrytxt'], $uid);
421 if (isset($adr['tels']) && is_array($adr['tels'])) {
422 $telid = 0;
423 foreach ($adr['tels'] as $tel) if ($tel['tel']) {
b235d980 424 add_user_tel($uid, 'address', $adrid, $telid, $tel);
dd502514 425 $telid ++;
426 }
427 }
428}
429// }}}
430// {{{ function update_user_address()
431function update_user_address($uid, $adrid, $adr) {
dd502514 432 // update address
08cce2ff 433 XDB::execute(
787bb3d7 434 "UPDATE adresses AS a LEFT JOIN geoloc_pays AS gp ON (gp.pays = {?})
dd502514 435 SET `adr1` = {?}, `adr2` = {?}, `adr3` = {?},
436 `postcode` = {?}, `city` = {?}, a.`country` = gp.a2, `datemaj` = NOW(), `pub` = {?}
437 WHERE adrid = {?} AND uid = {?}",
438 $adr['country_txt'],
439 $adr['adr1'], $adr['adr2'], $adr['adr3'],
440 $adr['postcode'], $adr['city'], $adr['pub'], $adrid, $uid);
441 if (isset($adr['tels']) && is_array($adr['tels'])) {
b235d980 442 $res = XDB::query("SELECT tel_id FROM profile_phones WHERE uid = {?} AND link_type = 'address' AND link_id = {?} ORDER BY tel_id", $uid, $adrid);
dd502514 443 $telids = $res->fetchColumn();
444 foreach ($adr['tels'] as $tel) {
445 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
b235d980 446 remove_user_tel($uid, 'address', $adrid, $tel['telid']);
dd502514 447 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
448 } else if (isset($tel['telid'])) {
b235d980 449 update_user_tel($uid, 'address', $adrid, $tel['telid'], $tel);
dd502514 450 } else {
451 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
b235d980 452 add_user_tel($uid, 'address', $adrid, $telid, $tel);
dd502514 453 }
454 }
455 }
456}
457// }}}
458// {{{ function remove_user_address()
459function remove_user_address($uid, $adrid) {
08cce2ff 460 XDB::execute("DELETE FROM adresses WHERE adrid = {?} AND uid = {?}", $adrid, $uid);
b235d980 461 XDB::execute("DELETE FROM profile_phones WHERE link_id = {?} AND uid = {?} AND link_type = 'address'", $adrid, $uid);
dd502514 462}
463// }}}
464// {{{ function add_user_tel()
b235d980 465function add_user_tel($uid, $link_type, $link_id, $telid, $tel) {
5e4417a9
GB
466 require('profil.func.inc.php');
467 $fmt_phone = format_phone_number($tel['tel']);
468 $disp_phone = format_display_number($fmt_phone, $error);
b235d980
GB
469 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, search_tel, display_tel, pub)
470 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
471 $uid, $link_type, $link_id, $telid, $tel['tel_type'], $fmt_phone, $disp_phone, $tel['tel_pub']);
dd502514 472}
473// }}}
474// {{{ function update_user_tel()
b235d980 475function update_user_tel($uid, $link_type, $link_id, $telid, $tel) {
5e4417a9
GB
476 require('profil.func.inc.php');
477 $fmt_phone = format_phone_number($tel['tel']);
478 $disp_phone = format_display_number($fmt_phone, $error);
b235d980
GB
479 XDB::execute("UPDATE profile_phones SET search_tel = {?}, display_tel = {?}, tel_type = {?}, pub = {?}
480 WHERE link_type = {?} AND tel_id = {?} AND link_id = {?} AND uid = {?}",
5e4417a9 481 $fmt_phone, $disp_phone, $tel['tel_type'], $tel['tel_pub'],
b235d980 482 $link_type, $telid, $link_id, $uid);
dd502514 483}
484// }}}
485// {{{ function remove_user_tel()
b235d980
GB
486function remove_user_tel($uid, $link_type, $link_id, $telid) {
487 XDB::execute("DELETE FROM profile_phones WHERE tel_id = {?} AND link_id = {?} AND uid = {?} AND link_type = {?}",
488 $telid, $link_id, $uid, $link_type);
dd502514 489}
490// }}}
491// {{{ function add_user_pro()
492function add_user_pro($uid, $entrid, $pro) {
08cce2ff 493 XDB::execute(
dd502514 494 "INSERT INTO entreprises (`uid`, `entrid`, `entreprise`, `poste`, `secteur`, `ss_secteur`, `fonction`,
b235d980 495 `adr1`, `adr2`, `adr3`, `postcode`, `city`, `country`, `region`, `email`, `web`, `pub`, `adr_pub`, `email_pub`)
dd502514 496 SELECT u.user_id, {?}, {?}, {?}, s.id, ss.id, f.id,
497 {?}, {?}, {?}, {?}, {?}, gp.a2, gr.region, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}
498 FROM auth_user_md5 AS u
499 LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
500 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
501 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})
502 LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
503 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})
504 WHERE u.user_id = {?}
505 LIMIT 1",
506 $entrid, $pro['entreprise'], $pro['poste'],
b235d980 507 $pro['adr1'], $pro['adr2'], $pro['adr3'], $pro['postcode'], $pro['city'], $pro['email'], $pro['web'], $pro['pub'], $pro['adr_pub'], $pro['email_pub'],
dd502514 508 $pro['secteur'], $pro['sous_secteur'], $pro['fonction'], $pro['fonction'],
509 $pro['countrytxt'], $pro['countrytxt'], $pro['region'],
510 $uid);
b235d980
GB
511 if (isset($pro['tels']) && is_array($pro['tels'])) {
512 $telid = 0;
513 foreach ($pro['tels'] as $tel) {
514 if ($pro['tel']) {
515 add_user_tel($uid, 'pro', $entrid, $telid, $tel);
516 $telid ++;
517 }
518 }
519 }
dd502514 520}
521// }}}
522// {{{ function update_user_pro()
523function update_user_pro($uid, $entrid, $pro) {
dd502514 524 $join = "";
525 $set = "";
526 $args_join = array();
527 $args_set = array();
787bb3d7 528
dd502514 529 $join .= "LEFT JOIN emploi_secteur AS s ON(s.label LIKE {?})
530 LEFT JOIN emploi_ss_secteur AS ss ON(s.id = ss.secteur AND ss.label LIKE {?})
531 LEFT JOIN fonctions_def AS f ON(f.fonction_fr LIKE {?} OR f.fonction_en LIKE {?})";
532 $args_join[] = $pro['secteur'];
533 $args_join[] = $pro['sous_secteur'];
534 $args_join[] = $pro['fonction'];
535 $args_join[] = $pro['fonction'];
536 $set .= ", e.`entreprise` = {?}, e.`secteur` = s.id, e.`ss_secteur` = ss.id, e.`fonction` = f.id, e.`poste`= {?}, e.`web` = {?}, e.`pub` = {?}";
537 $args_set[] = $pro['entreprise'];
538 $args_set[] = $pro['poste'];
539 $args_set[] = $pro['web'];
540 $args_set[] = $pro['pub'];
787bb3d7 541
dd502514 542 if (isset($pro['adr1'])) {
543 $join .= "LEFT JOIN geoloc_pays AS gp ON (gp.country LIKE {?} OR gp.pays LIKE {?})
544 LEFT JOIN geoloc_region AS gr ON (gr.a2 = gp.a2 AND gr.name LIKE {?})";
545 $args_join[] = $pro['countrytxt'];
546 $args_join[] = $pro['countrytxt'];
547 $args_join[] = $pro['region'];
548 $set .= ", e.`adr1` = {?}, e.`adr2` = {?}, e.`adr3` = {?}, e.`postcode` = {?}, e.`city` = {?}, e.`country` = gp.a2, e.`region` = gr.region, e.`adr_pub` = {?}";
549 $args_set[] = $pro['adr1'];
550 $args_set[] = $pro['adr2'];
551 $args_set[] = $pro['adr3'];
552 $args_set[] = $pro['postcode'];
553 $args_set[] = $pro['city'];
554 $args_set[] = $pro['adr_pub'];
555 }
787bb3d7 556
dd502514 557 if (isset($pro['email'])) {
558 $set .= ", e.`email` = {?}, e.`email_pub` = {?}";
559 $args_set[] = $pro['email'];
560 $args_set[] = $pro['email_pub'];
787bb3d7 561 }
dd502514 562 $query = "UPDATE entreprises AS e ".$join." SET ".substr($set,1)." WHERE e.uid = {?} AND e.entrid = {?}";
563 $args_where = array($uid, $entrid);
564 $args = array_merge(array($query), $args_join, $args_set, $args_where);
a3a049fc 565 call_user_func_array(array('XDB', 'execute'), $args);
b235d980
GB
566
567
568 if (isset($pro['tels']) && is_array($pro['tels'])) {
569 $res = XDB::query("SELECT tel_id FROM profile_phones WHERE uid = {?} AND link_type = 'pro' AND link_id = {?} ORDER BY tel_id", $uid, $entrid);
570 $telids = $res->fetchColumn();
571 foreach ($pro['tels'] as $tel) {
572 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
573 remove_user_tel($uid, 'pro', $entrid, $tel['telid']);
574 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
575 } else if (isset($tel['telid'])) {
576 update_user_tel($uid, 'pro', $entrid, $tel['telid'], $tel);
577 } else {
578 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
579 add_user_tel($uid, 'pro', $entrid, $telid, $tel);
580 }
581 }
582 }
dd502514 583}
584// }}}
585// {{{ function remove_user_pro()
586function remove_user_pro($uid, $entrid) {
08cce2ff 587 XDB::execute("DELETE FROM entreprises WHERE entrid = {?} AND uid = {?}", $entrid, $uid);
b235d980 588 XDB::execute("DELETE FROM profile_phones WHERE link_id = {?} AND uid = {?} AND link_type = 'pro'", $entrid, $uid);
dd502514 589}
590// }}}
b235d980 591// {{{ function set_user_details_addresses()
dd502514 592function set_user_details_addresses($uid, $adrs) {
08cce2ff 593 $res = XDB::query("SELECT adrid FROM adresses WHERE uid = {?} AND adrid >= 1 ORDER BY adrid", $uid);
dd502514 594 $adrids = $res->fetchColumn();
595 foreach ($adrs as $adr) {
596 if (isset($adr['adrid']) && isset($adr['remove']) && $adr['remove']) {
597 remove_user_address($uid, $adr['adrid']);
598 if (isset($adrids[$adr['adrid']])) unset($adrids[$adr['adrid']]);
599 } else if (isset($adr['adrid'])) {
600 update_user_address($uid, $adr['adrid'], $adr);
601 } else {
602 for ($adrid = 1; isset($adrids[$adrid-1]) && ($adrids[$adrid-1] == $adrid); $adrid++);
603 add_user_address($uid, $adrid, $adr);
604 $adrids[$adrid-1] = $adrid;
605 }
606 }
607 require_once 'geoloc.inc.php';
608 localize_addresses($uid);
609}
610// }}}
611// {{{ function set_user_details_pro()
612
613function set_user_details_pro($uid, $pros)
614{
08cce2ff 615 $res = XDB::query("SELECT entrid FROM entreprises WHERE uid = {?} ORDER BY entrid", $uid);
dd502514 616 $entrids = $res->fetchColumn();
617 foreach ($pros as $pro) {
618 if (isset($pro['entrid']) && isset($pro['remove']) && $pro['remove']) {
619 remove_user_pro($uid, $pro['entrid']);
620 if (isset($entrids[$pro['entrid']])) unset($entrids[$pro['entrid']]);
621 } else if (isset($pro['entrid'])) {
622 update_user_pro($uid, $pro['entrid'], $pro);
623 } else {
624 for ($entrid = 0; isset($entrids[$entrid]) && ($entrids[$entrid] == $entrid); $entrid++);
625 add_user_pro($uid, $entrid, $pro);
626 }
627 }
628}
0337d704 629
630// }}}
dd502514 631// {{{ function set_user_details()
632function set_user_details($uid, $details) {
dd502514 633 if (isset($details['nom_usage'])) {
08cce2ff 634 XDB::execute("UPDATE auth_user_md5 SET nom_usage = {?} WHERE user_id = {?}", strtoupper($details['nom_usage']), $uid);
dd502514 635 }
dd502514 636 if (isset($details['nationalite'])) {
08cce2ff 637 XDB::execute(
dd502514 638 "UPDATE auth_user_md5 AS u
639 INNER JOIN geoloc_pays AS gp
640 SET u.nationalite = gp.a2
641 WHERE (gp.a2 = {?} OR gp.nat = {?})
642 AND u.user_id = {?}", $details['nationalite'], $details['nationalite'], $uid);
643 }
644 if (isset($details['adr']) && is_array($details['adr']))
645 set_user_details_addresses($uid, $details['adr']);
646 if (isset($details['adr_pro']) && is_array($details['adr_pro']))
647 set_user_details_pro($uid, $details['adr_pro']);
648 if (isset($details['binets']) && is_array($details['binets'])) {
08cce2ff 649 XDB::execute("DELETE FROM binets_ins WHERE user_id = {?}", $uid);
dd502514 650 foreach ($details['binets'] as $binet)
08cce2ff 651 XDB::execute(
dd502514 652 "INSERT INTO binets_ins (`user_id`, `binet_id`)
653 SELECT {?}, id FROM binets_def WHERE text = {?} LIMIT 1",
787bb3d7 654 $uid, $binet);
dd502514 655 }
656 if (isset($details['gpxs']) && is_array($details['gpxs'])) {
08cce2ff 657 XDB::execute("DELETE FROM groupesx_ins WHERE user_id = {?}", $uid);
dd502514 658 foreach ($details['gpxs'] as $groupex) {
a14159bf 659 if (preg_match('/<a href="[^"]*">([^<]+)</a>/u', $groupex, $a)) $groupex = $a[1];
08cce2ff 660 XDB::execute(
dd502514 661 "INSERT INTO groupesx_ins (`user_id`, `binet_id`)
662 SELECT {?}, id FROM groupesx_def WHERE text = {?} LIMIT 1",
663 $uid, $groupex);
787bb3d7 664 }
dd502514 665 }
b235d980
GB
666 if (isset($details['tels']) && is_array($details['tels'])) {
667 $res = XDB::query("SELECT tel_id FROM profile_phones WHERE uid = {?} AND link_type = 'user' ORDER BY tel_id", $uid);
668 $telids = $res->fetchColumn();
669 foreach ($details['tels'] as $tel) {
670 if (isset($tel['telid']) && isset($tel['remove']) && $tel['remove']) {
671 remove_user_tel($uid, 'user', 0, $tel['telid']);
672 if (isset($telids[$tel['telid']])) unset($telids[$tel['telid']]);
673 } else if (isset($tel['telid'])) {
674 update_user_tel($uid, 'user', 0, $tel['telid'], $tel);
675 } else {
676 for ($telid = 0; isset($telids[$telid]) && ($telids[$telid] == $telid); $telid++);
677 add_user_tel($uid, 'user', 0, $telid, $tel);
678 }
679 }
680 }
681
f711b03f 682 // education
dd502514 683 // medals
684}
685// }}}
0337d704 686// {{{ function _user_reindex
687
c0c9f772 688function _user_reindex($uid, $keys, $muls, $pubs)
94f6c381 689{
0337d704 690 foreach ($keys as $i => $key) {
691 if ($key == '') {
692 continue;
693 }
694 $toks = preg_split('/[ \'\-]+/', $key);
695 $token = "";
696 $first = 5;
697 while ($toks) {
698 $token = strtolower(replace_accent(array_pop($toks) . $token));
699 $score = ($toks ? 0 : 10 + $first) * $muls[$i];
c0c9f772 700 XDB::execute("REPLACE INTO search_name (token, uid, soundex, score, flags)
701 VALUES ({?}, {?}, {?}, {?}, {?})",
702 $token, $uid, soundex_fr($token), $score, $pubs[$i] ? 'public' : '');
0337d704 703 $first = 0;
704 }
705 }
94f6c381 706 $res = XDB::query("SELECT nom_ini, nom, nom_usage, prenom_ini, prenom, promo, matricule
707 FROM auth_user_md5
708 WHERE user_id = {?}", $uid);
709 if (!$res->numRows()) {
710 unset($res);
711 return;
712 }
713 $array = $res->fetchOneRow();
714 $promo = intval(array_pop($array));
715 $mat = array_shift($array);
716 array_walk($array, 'soundex_fr');
717 XDB::execute("REPLACE INTO recherche_soundex
718 SET matricule = {?}, nom1_soundex = {?}, nom2_soundex= {?}, nom3_soundex = {?},
719 prenom1_soundex = {?}, prenom2_soundex= {?}, promo = {?}",
720 $mat, $array[0], $array[1], $array[2], $array[3], $array[4], $promo);
721 unset($res);
722 unset($array);
0337d704 723}
724
725// }}}
726// {{{ function user_reindex
727
728function user_reindex($uid) {
08cce2ff 729 XDB::execute("DELETE FROM search_name WHERE uid={?}", $uid);
730 $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);
36f58fcf 731 if ($res->numRows()) {
c0c9f772 732 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1,0.2), array(true, true, true, false));
36f58fcf 733 } else { // not in auth_user_quick => still "pending"
734 $res = XDB::query("SELECT prenom, nom, nom_usage FROM auth_user_md5 WHERE auth_user_md5.user_id = {?}", $uid);
735 if ($res->numRows()) {
c0c9f772 736 _user_reindex($uid, $res->fetchOneRow(), array(1,1,1), array(true, true, true));
36f58fcf 737 }
738 }
0337d704 739}
740
741// }}}
433336f3 742// {{{ function set_new_usage()
74c55fd6 743
a11b5424 744function set_new_usage($uid, $usage, $alias=false)
787bb3d7 745{
74c55fd6 746 XDB::execute("UPDATE auth_user_md5 set nom_usage={?} WHERE user_id={?}",$usage ,$uid);
747 XDB::execute("DELETE FROM aliases WHERE FIND_IN_SET('usage',flags) AND id={?}", $uid);
748 if ($alias && $usage) {
749 XDB::execute("UPDATE aliases SET flags=flags & 255-1 WHERE id={?}", $uid);
750 XDB::execute("INSERT INTO aliases VALUES({?}, 'alias', 'usage,bestalias', {?}, null)",
751 $alias, $uid);
752 }
753 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
a11b5424 754 if ($r->numRows() == "") {
74c55fd6 755 XDB::execute("UPDATE aliases SET flags = 1 | flags WHERE id = {?} LIMIT 1", $uid);
a11b5424 756 $r = XDB::query("SELECT alias FROM aliases WHERE FIND_IN_SET('bestalias', flags) AND id = {?}", $uid);
74c55fd6 757 }
74c55fd6 758 user_reindex($uid);
a11b5424 759 return $r->fetchOneCell();
74c55fd6 760}
761
433336f3 762// }}}
763// {{{ function get_X_mat
764function get_X_mat($ourmat)
765{
787bb3d7 766 if (!preg_match('/^[0-9]{8}$/', $ourmat)) {
433336f3 767 // le matricule de notre base doit comporter 8 chiffres
768 return 0;
787bb3d7
FB
769 }
770
433336f3 771 $year = intval(substr($ourmat, 0, 4));
772 $rang = intval(substr($ourmat, 5, 3));
773 if ($year < 1996) {
787bb3d7 774 return;
433336f3 775 } elseif ($year < 2000) {
776 $year = intval(substr(1900 - $year, 1, 3));
777 return sprintf('%02u0%03u', $year, $rang);
778 } else {
779 $year = intval(substr(1900 - $year, 1, 3));
780 return sprintf('%03u%03u', $year, $rang);
781 }
787bb3d7
FB
782}
783
433336f3 784// }}}
785
786
a7de4ef7 787// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 788?>