2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 require_once('applis.func.inc.php');
25 function replace_ifset(&$var,$req) {
31 function replace_ifset_i(&$var,$req,$i) {
32 if (isset($_REQUEST[$req][$i])){
33 $var[$i] = $_REQUEST[$req][$i];
37 function replace_ifset_i_j(&$var,$req,$i,$j) {
38 if (isset($_REQUEST[$req][$j])){
39 $var[$i] = $_REQUEST[$req][$j];
43 //pour rentrer qqchose dans la base
44 function put_in_db($string){
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)
53 // if (!isset($a) || !$a || !is_array($a))
57 foreach ($b as $val => $bvar) {
58 if (isset($a[$val])) {
59 if ($a[$val] == $bvar)
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 'tels' : if (!($c['tels'] = diff_user_tels($a[$val], $bvar, $view))) unset($c['tels']); break;
70 // don't modify freetext if you don't have the right
71 if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext']))
72 unset($c['freetext']);
78 function same_tel(&$a, &$b) {
79 $numbera = format_phone_number((string) $a);
80 $numberb = format_phone_number((string) $b);
81 return $numbera === $numberb;
83 function same_address(&$a, &$b) {
85 (same_field($a['adr1'],$b['adr1'])) &&
86 (same_field($a['adr2'],$b['adr2'])) &&
87 (same_field($a['adr3'],$b['adr3'])) &&
88 (same_field($a['postcode'],$b['postcode'])) &&
89 (same_field($a['city'],$b['city'])) &&
90 (same_field($a['countrytxt'],$b['countrytxt'])) &&
93 function same_pro(&$a, &$b) {
95 (same_field($a['entreprise'],$b['entreprise'])) &&
96 (same_field($a['fonction'],$b['fonction'])) &&
99 function same_field(&$a, &$b) {
100 if ($a == $b) return true
;
102 if (!is_array($b) ||
count($a) != count($b)) return false
;
103 foreach ($a as $val => $avar)
104 if (!isset($b[$val]) ||
!same_field($avar, $b[$val])) return false
;
106 } elseif (is_string($a))
107 return (strtoupper($a) == strtoupper($b));
109 function diff_user_tel(&$a, &$b) {
111 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
112 $c['tel_pub'] = $b['tel_pub'];
113 foreach ($b as $val => $bvar) {
114 if (isset($a[$val])) {
115 if ($a[$val] == $bvar)
121 $c['telid'] = $a['telid'];
125 function diff_user_tels(&$a, &$b)
129 foreach ($b as $i => $telb) $telids_b[$telb['telid']] = $i;
131 foreach ($a as $j => $tela) {
132 if (isset($tela['telid'])) {
133 // if b has a tel with the same telid, compute diff
134 if (isset($telids_b[$tela['telid']])) {
135 if (!($c[$j] = diff_user_tel($tela, $b[$telids_b[$tela['adrid']]]))) {
138 unset($telids_b[$tela['telid']]);
141 // try to find a match in b
142 foreach ($b as $i => $telb) {
143 if (same_tel($tela['tel'], $telb['tel'])) {
144 $tela['telid'] = $telb['telid'];
145 if (!($c[$j] = diff_user_tel($tela, $telb))) {
148 unset($telids_b[$tela['telid']]);
155 foreach ($telids_b as $telidb => $i)
156 $c[] = array('telid' => $telidb, 'remove' => 1);
160 function diff_user_address($a, $b) {
161 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
162 $a['pub'] = $b['pub'];
163 if (isset($b['tels'])) {
164 if (isset($a['tels'])) {
169 $ctels = diff_user_tels($avar, $b['tels']);
171 if (!count($ctels)) {
178 foreach ($a as $val => $avar) {
179 if (!isset($b[$val]) ||
!same_field($avar,$b[$val])) {
186 // $b need to use adrids
187 function diff_user_addresses(&$a, &$b) {
190 foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i;
192 foreach ($a as $j => $adra) {
193 if (isset($adra['adrid'])) {
194 // if b has an address with the same adrid, compute diff
195 if (isset($adrids_b[$adra['adrid']])) {
196 if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]])))
198 unset($adrids_b[$adra['adrid']]);
201 // try to find a match in b
202 foreach ($b as $i => $adrb) {
203 if (same_address($adra, $adrb)) {
204 $adra['adrid'] = $adrb['adrid'];
205 if (!($c[$j] = diff_user_address($adra, $adrb)))
207 if ($c[$j]) $c[$j]['adrid'] = $adra['adrid'];
208 unset($adrids_b[$adra['adrid']]);
215 foreach ($adrids_b as $adridb => $i)
216 $c[] = array('adrid' => $adridb, 'remove' => 1);
218 if (!count($c)) return false
;
222 function diff_user_pro($a, &$b, $view = 'private') {
223 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
224 $a['pub'] = $b['pub'];
225 if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) {
229 unset($a['postcode']);
231 unset($a['countrytxt']);
234 if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub']))
235 $a['adr_pub'] = $b['adr_pub'];
236 if (isset($b['tels'])) {
237 if (isset($a['tels']))
241 $ctels = diff_user_tels($avar, $b['tels']);
243 if (!count($ctels)) {
248 if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view))
250 if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub']))
251 $a['email_pub'] = $b['email_pub'];
252 foreach ($a as $val => $avar) {
253 if (($avar && !isset($b[$val])) ||
!same_field($avar,$b[$val])) {
260 // $b need to use entrids
261 function diff_user_pros(&$a, &$b, $view = 'private') {
263 $entrids_b = array();
264 foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i;
266 foreach ($a as $j => $proa) {
267 if (isset($proa['entrid'])) {
268 // if b has an address with the same adrid, compute diff
269 if (isset($entrids_b[$proa['entrid']])) {
270 if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view)))
272 unset($entrids_b[$proa['entrid']]);
275 // try to find a match in b
276 foreach ($b as $i => $prob) {
277 if (same_pro($proa, $prob)) {
278 $proa['entrid'] = $prob['entrid'];
279 if (!($c[$j] = diff_user_pro($proa, $prob, $view)))
281 if ($c[$j]) $c[$j]['entrid'] = $proa['entrid'];
282 unset($entrids_b[$proa['entrid']]);
289 foreach ($entrids_b as $entridb => $i)
290 $c[] = array('entrid' => $entridb, 'remove' => 1);
292 if (!count($c)) return false
;
296 function format_phone_number($tel)
299 if (substr($tel, 0, 3) === '(0)') {
302 $tel = preg_replace('/\(0\)/', '', $tel);
303 $tel = preg_replace('/[^0-9]/', '', $tel);
304 if (substr($tel, 0, 2) === '00') {
305 $tel = substr($tel, 2);
306 } else if(substr($tel, 0, 1) === '0') {
307 $tel = '33' . substr($tel, 1);
312 function format_display_number($tel, &$error, $format = array('format'=>'','phoneprf'=>''))
316 $tel_length = strlen($tel);
317 if((!isset($format['phoneprf'])) ||
($format['phoneprf'] == '')) {
318 $res = XDB
::query("SELECT phoneprf, phoneformat AS format
320 WHERE phoneprf = {?} OR phoneprf = {?} OR phoneprf = {?}
322 substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
323 if ($res->numRows() == 0) {
327 $format = $res->fetchOneAssoc();
329 if ($format['format'] == '') {
330 $format['format'] = '+p';
333 $i = strlen($format['phoneprf']);
334 $length_format = strlen($format['format']);
335 while (($i < $tel_length) && ($j < $length_format)){
336 if ($format['format'][$j] == '#'){
339 } else if ($format['format'][$j] == 'p') {
340 $ret .= $format['phoneprf'];
342 $ret .= $format['format'][$j];
346 for (; $i < $tel_length - 1; $i +
= 2) {
347 $ret .= ' ' . substr($tel, $i, 2);
349 //appends last alone number to the last block
350 if ($i < $tel_length) {
351 $ret .= substr($tel, $i);
356 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: