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 ***************************************************************************/
24 function replace_ifset(&$var,$req) {
30 function replace_ifset_i(&$var,$req,$i) {
31 if (isset($_REQUEST[$req][$i])){
32 $var[$i] = $_REQUEST[$req][$i];
36 function replace_ifset_i_j(&$var,$req,$i,$j) {
37 if (isset($_REQUEST[$req][$j])){
38 $var[$i] = $_REQUEST[$req][$j];
42 //pour rentrer qqchose dans la base
43 function put_in_db($string){
47 // example of use for diff_user_details : get $b from database, $a from other site
48 // calculate diff $c and add $c in database (with set_user_details)
49 function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $b
50 // if (!isset($b) || !$b || !is_array($b) || count($b) == 0)
52 // if (!isset($a) || !$a || !is_array($a))
56 foreach ($b as $val => $bvar) {
57 if (isset($a[$val])) {
58 if ($a[$val] == $bvar)
62 case 'adr' : if (!($c['adr'] = diff_user_addresses($a[$val], $bvar, $view))) unset($c['adr']); break;
63 case 'adr_pro' : if (!($c['adr_pro'] = diff_user_pros($a[$val], $bvar, $view))) unset($c['adr_pro']); break;
64 case 'tels' : if (!($c['tels'] = diff_user_tels($a[$val], $bvar, $view))) unset($c['tels']); break;
69 // don't modify freetext if you don't have the right
70 if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext']))
71 unset($c['freetext']);
77 function same_tel(&$a, &$b) {
78 $numbera = format_phone_number((string) $a);
79 $numberb = format_phone_number((string) $b);
80 return $numbera === $numberb;
82 function same_address(&$a, &$b) {
84 (same_field($a['adr1'],$b['adr1'])) &&
85 (same_field($a['adr2'],$b['adr2'])) &&
86 (same_field($a['adr3'],$b['adr3'])) &&
87 (same_field($a['postcode'],$b['postcode'])) &&
88 (same_field($a['city'],$b['city'])) &&
89 (same_field($a['countrytxt'],$b['countrytxt'])) &&
92 function same_pro(&$a, &$b) {
94 (same_field($a['entreprise'],$b['entreprise'])) &&
95 (same_field($a['fonction'],$b['fonction'])) &&
98 function same_field(&$a, &$b) {
99 if ($a == $b) return true
;
101 if (!is_array($b) ||
count($a) != count($b)) return false
;
102 foreach ($a as $val => $avar)
103 if (!isset($b[$val]) ||
!same_field($avar, $b[$val])) return false
;
105 } elseif (is_string($a))
106 return (strtoupper($a) == strtoupper($b));
108 function diff_user_tel(&$a, &$b) {
110 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
111 $c['tel_pub'] = $b['tel_pub'];
112 foreach ($b as $val => $bvar) {
113 if (isset($a[$val])) {
114 if ($a[$val] == $bvar)
120 $c['telid'] = $a['telid'];
124 function diff_user_tels(&$a, &$b)
128 foreach ($b as $i => $telb) $telids_b[$telb['telid']] = $i;
130 foreach ($a as $j => $tela) {
131 if (isset($tela['telid'])) {
132 // if b has a tel with the same telid, compute diff
133 if (isset($telids_b[$tela['telid']])) {
134 if (!($c[$j] = diff_user_tel($tela, $b[$telids_b[$tela['adrid']]]))) {
137 unset($telids_b[$tela['telid']]);
140 // try to find a match in b
141 foreach ($b as $i => $telb) {
142 if (same_tel($tela['tel'], $telb['tel'])) {
143 $tela['telid'] = $telb['telid'];
144 if (!($c[$j] = diff_user_tel($tela, $telb))) {
147 unset($telids_b[$tela['telid']]);
154 foreach ($telids_b as $telidb => $i)
155 $c[] = array('telid' => $telidb, 'remove' => 1);
159 function diff_user_address($a, $b) {
160 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
161 $a['pub'] = $b['pub'];
162 if (isset($b['tels'])) {
163 if (isset($a['tels'])) {
168 $ctels = diff_user_tels($avar, $b['tels']);
170 if (!count($ctels)) {
177 foreach ($a as $val => $avar) {
178 if (!isset($b[$val]) ||
!same_field($avar,$b[$val])) {
185 // $b need to use adrids
186 function diff_user_addresses(&$a, &$b) {
189 foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i;
191 foreach ($a as $j => $adra) {
192 if (isset($adra['adrid'])) {
193 // if b has an address with the same adrid, compute diff
194 if (isset($adrids_b[$adra['adrid']])) {
195 if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]])))
197 unset($adrids_b[$adra['adrid']]);
200 // try to find a match in b
201 foreach ($b as $i => $adrb) {
202 if (same_address($adra, $adrb)) {
203 $adra['adrid'] = $adrb['adrid'];
204 if (!($c[$j] = diff_user_address($adra, $adrb)))
206 if ($c[$j]) $c[$j]['adrid'] = $adra['adrid'];
207 unset($adrids_b[$adra['adrid']]);
214 foreach ($adrids_b as $adridb => $i)
215 $c[] = array('adrid' => $adridb, 'remove' => 1);
217 if (!count($c)) return false
;
221 function diff_user_pro($a, &$b, $view = 'private') {
222 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
223 $a['pub'] = $b['pub'];
224 if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) {
228 unset($a['postcode']);
230 unset($a['countrytxt']);
233 if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub']))
234 $a['adr_pub'] = $b['adr_pub'];
235 if (isset($b['tels'])) {
236 if (isset($a['tels']))
240 $ctels = diff_user_tels($avar, $b['tels']);
242 if (!count($ctels)) {
247 if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view))
249 if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub']))
250 $a['email_pub'] = $b['email_pub'];
251 foreach ($a as $val => $avar) {
252 if (($avar && !isset($b[$val])) ||
!same_field($avar,$b[$val])) {
259 // $b need to use entrids
260 function diff_user_pros(&$a, &$b, $view = 'private') {
262 $entrids_b = array();
263 foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i;
265 foreach ($a as $j => $proa) {
266 if (isset($proa['entrid'])) {
267 // if b has an address with the same adrid, compute diff
268 if (isset($entrids_b[$proa['entrid']])) {
269 if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view)))
271 unset($entrids_b[$proa['entrid']]);
274 // try to find a match in b
275 foreach ($b as $i => $prob) {
276 if (same_pro($proa, $prob)) {
277 $proa['entrid'] = $prob['entrid'];
278 if (!($c[$j] = diff_user_pro($proa, $prob, $view)))
280 if ($c[$j]) $c[$j]['entrid'] = $proa['entrid'];
281 unset($entrids_b[$proa['entrid']]);
288 foreach ($entrids_b as $entridb => $i)
289 $c[] = array('entrid' => $entridb, 'remove' => 1);
291 if (!count($c)) return false
;
295 function format_phone_number($tel)
298 if (substr($tel, 0, 3) === '(0)') {
301 $tel = preg_replace('/\(0\)/', '', $tel);
302 $tel = preg_replace('/[^0-9]/', '', $tel);
303 if (substr($tel, 0, 2) === '00') {
304 $tel = substr($tel, 2);
305 } else if(substr($tel, 0, 1) === '0') {
306 $tel = '33' . substr($tel, 1);
311 function format_display_number($tel, &$error, $format = array('format'=>'','phoneprf'=>''))
315 $tel_length = strlen($tel);
316 if((!isset($format['phoneprf'])) ||
($format['phoneprf'] == '')) {
317 $res = XDB
::query("SELECT phoneprf, phoneformat AS format
319 WHERE phoneprf = {?} OR phoneprf = {?} OR phoneprf = {?}
321 substr($tel, 0, 1), substr($tel, 0, 2), substr($tel, 0, 3));
322 if ($res->numRows() == 0) {
326 $format = $res->fetchOneAssoc();
328 if ($format['format'] == '') {
329 $format['format'] = '+p';
332 $i = strlen($format['phoneprf']);
333 $length_format = strlen($format['format']);
334 while (($i < $tel_length) && ($j < $length_format)){
335 if ($format['format'][$j] == '#'){
338 } else if ($format['format'][$j] == 'p') {
339 $ret .= $format['phoneprf'];
341 $ret .= $format['format'][$j];
345 for (; $i < $tel_length - 1; $i +
= 2) {
346 $ret .= ' ' . substr($tel, $i, 2);
348 //appends last alone number to the last block
349 if ($i < $tel_length) {
350 $ret .= substr($tel, $i);
355 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: