c8334b1507910376b315a555770c617c8b0dc37c
[platal.git] / include / profil.func.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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
22
23 require_once('applis.func.inc.php');
24
25 function replace_ifset(&$var,$req) {
26 if (Env::has($req)){
27 $var = Env::v($req);
28 }
29 }
30
31 function replace_ifset_i(&$var,$req,$i) {
32 if (isset($_REQUEST[$req][$i])){
33 $var[$i] = $_REQUEST[$req][$i];
34 }
35 }
36
37 function replace_ifset_i_j(&$var,$req,$i,$j) {
38 if (isset($_REQUEST[$req][$j])){
39 $var[$i] = $_REQUEST[$req][$j];
40 }
41 }
42
43 //pour rentrer qqchose dans la base
44 function put_in_db($string){
45 return trim($string);
46 }
47
48 // example of use for diff_user_details : get $b from database, $a from other site
49 // calculate diff $c and add $c in database (with set_user_details)
50 function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $b
51 // if (!isset($b) || !$b || !is_array($b) || count($b) == 0)
52 // return $a;
53 // if (!isset($a) || !$a || !is_array($a))
54 // $c = array();
55 // else
56 $c = $a;
57 foreach ($b as $val => $bvar) {
58 if (isset($a[$val])) {
59 if ($a[$val] == $bvar)
60 unset($c[$val]);
61 else {
62 switch ($val) {
63 case 'adr' : if (!($c['adr'] = diff_user_addresses($a[$val], $bvar, $view))) unset($c['adr']); break;
64 case 'adr_pro' : if (!($c['adr_pro'] = diff_user_pros($a[$val], $bvar, $view))) unset($c['adr_pro']); break;
65 case 'mobile' : if (same_tel($a[$val], $bvar)) unset($c['mobile']); break;
66 }
67 }
68 }
69 }
70 // don't modify mobile if you don't have the right
71 if (isset($b['mobile_pub']) && !has_user_right($b['mobile_pub'], $view) && isset($c['mobile']))
72 unset($c['mobile']);
73 if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext']))
74 unset($c['freetext']);
75 if (!count($c))
76 return false;
77 return $c;
78 }
79
80 function same_tel(&$a, &$b) {
81 $numbera = preg_replace('/[^0-9]/', '', (string) $a);
82 $numberb = preg_replace('/[^0-9]/', '', (string) $b);
83 return $numbera === $numberb;
84 }
85 function same_address(&$a, &$b) {
86 return
87 (same_field($a['adr1'],$b['adr1'])) &&
88 (same_field($a['adr1'],$b['adr1'])) &&
89 (same_field($a['adr1'],$b['adr1'])) &&
90 (same_field($a['postcode'],$b['postcode'])) &&
91 (same_field($a['city'],$b['city'])) &&
92 (same_field($a['countrytxt'],$b['countrytxt'])) &&
93 true;
94 }
95 function same_pro(&$a, &$b) {
96 return
97 (same_field($a['entreprise'],$b['entreprise'])) &&
98 (same_field($a['fonction'],$b['fonction'])) &&
99 true;
100 }
101 function same_field(&$a, &$b) {
102 if ($a == $b) return true;
103 if (is_array($a)) {
104 if (!is_array($b) || count($a) != count($b)) return false;
105 foreach ($a as $val => $avar)
106 if (!isset($b[$val]) || !same_field($avar, $b[$val])) return false;
107 return true;
108 } elseif (is_string($a))
109 return (strtoupper($a) == strtoupper($b));
110 }
111 function diff_user_tel(&$a, &$b) {
112 $c = $a;
113 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
114 $c['tel_pub'] = $b['tel_pub'];
115 foreach ($b as $val => $bvar) {
116 if (isset($a[$val])) {
117 if ($a[$val] == $bvar)
118 unset($c[$val]);
119 }
120 }
121 if (!count($c))
122 return false;
123 $c['telid'] = $a['telid'];
124 return $c;
125 }
126
127 function diff_user_address($a, $b) {
128 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
129 $a['pub'] = $b['pub'];
130 if (isset($b['tels'])) {
131 $bvar = $b['tels'];
132
133 $telids_b = array();
134 foreach ($bvar as $i => $telb) $telids_b[$telb['telid']] = $i;
135
136 if (isset($a['tels']))
137 $avar = $a['tels'];
138 else
139 $avar = array();
140 $ctels = $avar;
141 foreach ($avar as $j => $tela) {
142 if (isset($tela['telid'])) {
143 // if b has a tel with the same telid, compute diff
144 if (isset($telids_b[$tela['telid']])) {
145 if (!($ctels[$j] = diff_user_tel($tela, $varb[$telids_b[$tela['adrid']]])))
146 unset($ctels[$j]);
147 unset($telids_b[$tela['telid']]);
148 }
149 } else {
150 // try to find a match in b
151 foreach ($bvar as $i => $telb) {
152 if (same_tel($tela['tel'], $telb['tel'])) {
153 $tela['telid'] = $telb['telid'];
154 if (!($ctels[$j] = diff_user_tel($tela, $telb)))
155 unset($ctels[$j]);
156 unset($telids_b[$tela['telid']]);
157 break;
158 }
159 }
160 }
161 }
162
163 foreach ($telids_b as $telidb => $i)
164 $ctels[] = array('telid' => $telidb, 'remove' => 1);
165
166 if (!count($ctels)) {
167 $b['tels'] = $avar;
168 } else
169 $a['tels'] = $ctels;
170 }
171
172 foreach ($a as $val => $avar) {
173 if (!isset($b[$val]) || !same_field($avar,$b[$val])) {
174 return $a;
175 }
176 }
177 return false;
178 }
179
180 // $b need to use adrids
181 function diff_user_addresses(&$a, &$b) {
182 $c = $a;
183 $adrids_b = array();
184 foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i;
185
186 foreach ($a as $j => $adra) {
187 if (isset($adra['adrid'])) {
188 // if b has an address with the same adrid, compute diff
189 if (isset($adrids_b[$adra['adrid']])) {
190 if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]])))
191 unset($c[$j]);
192 unset($adrids_b[$adra['adrid']]);
193 }
194 } else {
195 // try to find a match in b
196 foreach ($b as $i => $adrb) {
197 if (same_address($adra, $adrb)) {
198 $adra['adrid'] = $adrb['adrid'];
199 if (!($c[$j] = diff_user_address($adra, $adrb)))
200 unset($c[$j]);
201 if ($c[$j]) $c[$j]['adrid'] = $adra['adrid'];
202 unset($adrids_b[$adra['adrid']]);
203 break;
204 }
205 }
206 }
207 }
208
209 foreach ($adrids_b as $adridb => $i)
210 $c[] = array('adrid' => $adridb, 'remove' => 1);
211
212 if (!count($c)) return false;
213 return $c;
214 }
215
216 function diff_user_pro($a, &$b, $view = 'private') {
217 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
218 $a['pub'] = $b['pub'];
219 if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) {
220 unset($a['adr1']);
221 unset($a['adr2']);
222 unset($a['adr3']);
223 unset($a['postcode']);
224 unset($a['city']);
225 unset($a['countrytxt']);
226 unset($a['region']);
227 }
228 if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub']))
229 $a['adr_pub'] = $b['adr_pub'];
230 if (isset($b['tel_pub']) && !has_user_right($b['tel_pub'], $view)) {
231 unset($a['tel']);
232 unset($a['fax']);
233 unset($a['mobile']);
234 }
235 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
236 $a['tel_pub'] = $b['tel_pub'];
237 if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view))
238 unset($a['email']);
239 if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub']))
240 $a['email_pub'] = $b['email_pub'];
241 foreach ($a as $val => $avar) {
242 if (($avar && !isset($b[$val])) || !same_field($avar,$b[$val])) {
243 return $a;
244 }
245 }
246 return false;
247 }
248
249 // $b need to use entrids
250 function diff_user_pros(&$a, &$b, $view = 'private') {
251 $c = $a;
252 $entrids_b = array();
253 foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i;
254
255 foreach ($a as $j => $proa) {
256 if (isset($proa['entrid'])) {
257 // if b has an address with the same adrid, compute diff
258 if (isset($entrids_b[$proa['entrid']])) {
259 if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view)))
260 unset($c[$j]);
261 unset($entrids_b[$proa['entrid']]);
262 }
263 } else {
264 // try to find a match in b
265 foreach ($b as $i => $prob) {
266 if (same_pro($proa, $prob)) {
267 $proa['entrid'] = $prob['entrid'];
268 if (!($c[$j] = diff_user_pro($proa, $prob, $view)))
269 unset($c[$j]);
270 if ($c[$j]) $c[$j]['entrid'] = $proa['entrid'];
271 unset($entrids_b[$proa['entrid']]);
272 break;
273 }
274 }
275 }
276 }
277
278 foreach ($entrids_b as $entridb => $i)
279 $c[] = array('entrid' => $entridb, 'remove' => 1);
280
281 if (!count($c)) return false;
282 return $c;
283 }
284
285 function format_phone_number($tel)
286 {
287 $tel = trim($tel);
288 if (substr($tel, 0, 3) === '(0)') {
289 $tel = '33' . $tel;
290 }
291 $tel = preg_replace('/\(0\)/', '', $tel);
292 $tel = preg_replace('/[^0-9]/', '', $tel);
293 if (substr($tel, 0, 2) === '00') {
294 $tel = substr($tel, 2);
295 } else if(substr($tel, 0, 1) === '0') {
296 $tel = '33' . substr($tel, 1);
297 }
298 return $tel;
299 }
300
301 function format_display_number($tel, &$error)
302 {
303 $error = false;
304 $ret = '';
305 $tel_length = strlen($tel);
306 $res = XDB::query("SELECT phoneprf, format
307 FROM phone_formats
308 WHERE phoneprf = {?} OR phoneprf = {?} OR phoneprf = {?}",
309 substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
310 if ($res->numRows() == 0) {
311 $error = true;
312 return '*+' . $tel;
313 }
314 $format = $res->fetchOneAssoc();
315 if ($format['format'] == '') {
316 $format['format'] = '+p';
317 }
318 $j = 0;
319 $i = strlen($format['phoneprf']);
320 $length_format = strlen($format['format']);
321 while (($i < $tel_length) && ($j < $length_format)){
322 if ($format['format'][$j] == '#'){
323 $ret .= $tel[$i];
324 $i++;
325 } else if ($format['format'][$j] == 'p') {
326 $ret .= $format['phoneprf'];
327 } else {
328 $ret .= $format['format'][$j];
329 }
330 $j++;
331 }
332 for (; $i < $tel_length - 1; $i += 2) {
333 $ret .= ' ' . substr($tel, $i, 2);
334 }
335 //appends last alone number to the last block
336 if ($i < $tel_length) {
337 $ret .= substr($tel, $i);
338 }
339 return $ret;
340 }
341
342 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
343 ?>