2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 'mobile' : if (same_tel($a[$val], $bvar)) unset($c['mobile']); break;
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']))
73 if (isset($b['web_pub']) && !has_user_right($b['web_pub'], $view) && isset($c['web']))
75 if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext']))
76 unset($c['freetext']);
82 function same_tel(&$a, &$b) {
83 $numbera = preg_replace('/[^0-9]/', '', (string) $a);
84 $numberb = preg_replace('/[^0-9]/', '', (string) $b);
85 return $numbera === $numberb;
87 function same_address(&$a, &$b) {
89 (same_field($a['adr1'],$b['adr1'])) &&
90 (same_field($a['adr1'],$b['adr1'])) &&
91 (same_field($a['adr1'],$b['adr1'])) &&
92 (same_field($a['postcode'],$b['postcode'])) &&
93 (same_field($a['city'],$b['city'])) &&
94 (same_field($a['countrytxt'],$b['countrytxt'])) &&
97 function same_pro(&$a, &$b) {
99 (same_field($a['entreprise'],$b['entreprise'])) &&
100 (same_field($a['fonction'],$b['fonction'])) &&
103 function same_field(&$a, &$b) {
104 if ($a == $b) return true
;
106 if (!is_array($b) ||
count($a) != count($b)) return false
;
107 foreach ($a as $val => $avar)
108 if (!isset($b[$val]) ||
!same_field($avar, $b[$val])) return false
;
110 } elseif (is_string($a))
111 return (mb_strtoupper($a) == mb_strtoupper($b));
113 function diff_user_tel(&$a, &$b) {
115 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
116 $c['tel_pub'] = $b['tel_pub'];
117 foreach ($b as $val => $bvar) {
118 if (isset($a[$val])) {
119 if ($a[$val] == $bvar)
125 $c['telid'] = $a['telid'];
129 function diff_user_address($a, $b) {
130 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
131 $a['pub'] = $b['pub'];
132 if (isset($b['tels'])) {
136 foreach ($bvar as $i => $telb) $telids_b[$telb['telid']] = $i;
138 if (isset($a['tels']))
143 foreach ($avar as $j => $tela) {
144 if (isset($tela['telid'])) {
145 // if b has a tel with the same telid, compute diff
146 if (isset($telids_b[$tela['telid']])) {
147 if (!($ctels[$j] = diff_user_tel($tela, $varb[$telids_b[$tela['adrid']]])))
149 unset($telids_b[$tela['telid']]);
152 // try to find a match in b
153 foreach ($bvar as $i => $telb) {
154 if (same_tel($tela['tel'], $telb['tel'])) {
155 $tela['telid'] = $telb['telid'];
156 if (!($ctels[$j] = diff_user_tel($tela, $telb)))
158 unset($telids_b[$tela['telid']]);
165 foreach ($telids_b as $telidb => $i)
166 $ctels[] = array('telid' => $telidb, 'remove' => 1);
168 if (!count($ctels)) {
174 foreach ($a as $val => $avar) {
175 if (!isset($b[$val]) ||
!same_field($avar,$b[$val])) {
182 // $b need to use adrids
183 function diff_user_addresses(&$a, &$b) {
186 foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i;
188 foreach ($a as $j => $adra) {
189 if (isset($adra['adrid'])) {
190 // if b has an address with the same adrid, compute diff
191 if (isset($adrids_b[$adra['adrid']])) {
192 if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]])))
194 unset($adrids_b[$adra['adrid']]);
197 // try to find a match in b
198 foreach ($b as $i => $adrb) {
199 if (same_address($adra, $adrb)) {
200 $adra['adrid'] = $adrb['adrid'];
201 if (!($c[$j] = diff_user_address($adra, $adrb)))
203 if ($c[$j]) $c[$j]['adrid'] = $adra['adrid'];
204 unset($adrids_b[$adra['adrid']]);
211 foreach ($adrids_b as $adridb => $i)
212 $c[] = array('adrid' => $adridb, 'remove' => 1);
214 if (!count($c)) return false
;
218 function diff_user_pro($a, &$b, $view = 'private') {
219 if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub']))
220 $a['pub'] = $b['pub'];
221 if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) {
225 unset($a['postcode']);
227 unset($a['countrytxt']);
230 if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub']))
231 $a['adr_pub'] = $b['adr_pub'];
232 if (isset($b['tel_pub']) && !has_user_right($b['tel_pub'], $view)) {
237 if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub']))
238 $a['tel_pub'] = $b['tel_pub'];
239 if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view))
241 if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub']))
242 $a['email_pub'] = $b['email_pub'];
243 foreach ($a as $val => $avar) {
244 if (($avar && !isset($b[$val])) ||
!same_field($avar,$b[$val])) {
251 // $b need to use entrids
252 function diff_user_pros(&$a, &$b, $view = 'private') {
254 $entrids_b = array();
255 foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i;
257 foreach ($a as $j => $proa) {
258 if (isset($proa['entrid'])) {
259 // if b has an address with the same adrid, compute diff
260 if (isset($entrids_b[$proa['entrid']])) {
261 if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view)))
263 unset($entrids_b[$proa['entrid']]);
266 // try to find a match in b
267 foreach ($b as $i => $prob) {
268 if (same_pro($proa, $prob)) {
269 $proa['entrid'] = $prob['entrid'];
270 if (!($c[$j] = diff_user_pro($proa, $prob, $view)))
272 if ($c[$j]) $c[$j]['entrid'] = $proa['entrid'];
273 unset($entrids_b[$proa['entrid']]);
280 foreach ($entrids_b as $entridb => $i)
281 $c[] = array('entrid' => $entridb, 'remove' => 1);
283 if (!count($c)) return false
;
287 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: