Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / modules / profile / verif_adresses.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
8303b851 22global $adresses;
0337d704 23
24function generate_new_adrid(){
25 global $adresses;
26 if(!isset($adresses)) //aucune adresses => retourne 1
27 return 1;
28 reset($adresses);
29 $i = 0;
30 $adrid_array = Array();
31 foreach($adresses as $numero => $adr){
32 $adrid_array[$i] = $numero;
33 $i++;
34 }
35 sort($adrid_array,SORT_NUMERIC); // classe les adrid dans l'ordre croissant
36 $new_adrid = 1;
37 while(list($key,$current_adrid) = each($adrid_array)){
38 if($current_adrid == $new_adrid)
39 $new_adrid++;
40 else
41 return $new_adrid; //s'ils sont differents, il y a un trou dans la liste des adrid donc new_adrid convient
42 }
43 //si aucun convient, on retourne le plus grand des adrid actuel + 1
44 return $new_adrid;
45}
46
79a5acea 47function generate_new_telid($adr){
48 $i = 0;
49 $telid_array = Array();
50 foreach($adr['tels'] as $tel){
51 $telid_array[$i] = $tel['telid'];
52 $i++;
53 }
54 sort($telid_array,SORT_NUMERIC); // classe les adrid dans l'ordre croissant
55 $new_telid = 0;
56 foreach($telid_array as $current_telid)
57 if ($current_telid == $new_telid)
58 $new_telid ++;
787bb3d7 59 else
79a5acea 60 return $new_telid;//s'ils sont differents, il y a un trou dans la liste des telid donc new_telid convient
61 //si aucun convient, on retourne le plus grand des telid actuel + 1
62 return $new_telid;
63}
64
0337d704 65function replace_ifset_adr($varname, $i){
5e2307dc 66 $tab = Env::v($varname, Array());
0337d704 67 if (isset($tab[$i]))
68 $GLOBALS['adresses'][$i][$varname] = $tab[$i];
69}
70
79a5acea 71function replace_ifset_tel($varname, $i, $t){
5e2307dc 72 $tab = Env::v($varname.$t, Array());
79a5acea 73 if (isset($tab[$i]))
787bb3d7 74 $GLOBALS['adresses'][$i]['tels'][$t][$varname] = $tab[$i];
79a5acea 75}
76
0337d704 77function get_adr_arg($varname, $i) {
5e2307dc 78 $tab = Env::v($varname, Array());
1a013db7 79 return @$tab[$i];
0337d704 80}
81
82function set_flag_adr($varname,$i){
5e2307dc 83 $tab = Env::v($varname, Array());
0337d704 84 if (isset($tab[$i])){
85 $GLOBALS['adresses'][$i][$varname] = 1;
86 }
87 else
88 $GLOBALS['adresses'][$i][$varname] = '0';
89}
90
79a5acea 91function replace_tel($i, $t){
92 replace_ifset_tel('telid', $i, $t);
93 replace_ifset_tel('tel', $i, $t);
94 replace_ifset_tel('tel_pub', $i, $t);
95 replace_ifset_tel('tel_type', $i, $t);
96 replace_ifset_tel('new_tel', $i, $t);
97}
0337d704 98
99function replace_address($i){
79a5acea 100 global $adresses, $nb_tel_max;
0337d704 101 if(!isset($adresses[$i])){
102 $adresses[$i]['nouvelle'] = 'ajout';
103 $adresses[$i]['adrid'] = $i;
104 }
787bb3d7 105
0337d704 106 replace_ifset_adr('secondaire', $i);
107 set_flag_adr('courrier', $i);
108 replace_ifset_adr('temporaire', $i);
5e2307dc 109 if(Env::i('adrid_active', $i+1) == $i)
0337d704 110 $adresses[$i]['active'] = 1;
111 else
112 $adresses[$i]['active'] = 0;
113 replace_ifset_adr('adr1', $i);
114 replace_ifset_adr('adr2', $i);
115 replace_ifset_adr('adr3', $i);
116 replace_ifset_adr('postcode', $i);
117 replace_ifset_adr('city', $i);
118 replace_ifset_adr('cityid', $i);
119 replace_ifset_adr('country', $i);
120 replace_ifset_adr('region', $i);
0337d704 121 replace_ifset_adr('pub', $i);
787bb3d7 122
79a5acea 123 for ($telid = 0; $telid <= $nb_tel_max; $telid++) {
5e2307dc 124 $tab = Env::v('telid'.$telid, Array());
79a5acea 125 if(isset($tab[$i])){ //ce telid etait donc present dans le formulaire
126 replace_tel($i, $telid);
127 }
128 }
787bb3d7 129
0337d704 130 if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i);
5e2307dc 131 $tab = Env::v('numero_formulaire', Array());
0337d704 132 if($tab[$i])
133 $adresses[$i]['numero_formulaire'] = $tab[$i];
134 else
135 $adresses[$i]['numero_formulaire'] = -1;
136}
137
138function geoloc_adresse($i) {
139 global $adresses;
5e2307dc 140 $change = Env::v('change'.$i);
0337d704 141 if (get_adr_arg('parsevalid', $i) || ($adresses[$i]['txt'] && $change) || (!$adresses[$i]['cityid'])) {
142 require_once('geoloc.inc.php');
143 // erases the previous address (but not the phone or pub)
144 $adresses[$i] = array_merge($adresses[$i], empty_address());
145 // localize new address
146 $new = get_address_infos($adresses[$i]['txt']);
787bb3d7 147 if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) || get_adr_arg('parsevalid', $i))
0337d704 148 $adresses[$i] = array_merge($adresses[$i], $new);
149 else {
150 $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt']));
151 $adresses[$i]['geoloc'] = $geotxt;
152 $adresses[$i]['geoloc_cityid'] = $new['cityid'];
153 }
154 }
155 $adresses[$i]['txt'] = get_address_text($adresses[$i]);
156}
157
158//remplace par les eventuelles nouvelles valeurs :
159for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++) {
5e2307dc 160 $tab = Env::v('adrid', Array());
0337d704 161 if(isset($tab[$adrid])){ //cet adrid etait donc present dans le formulaire
162 replace_address($adrid);
163 }
164 if (isset($adresses[$adrid]['txt']))
165 geoloc_adresse($adrid);
166}
167
2f678da1 168if ($opened_tab == 'adresses' && Env::has('modifier')){ // on ne valide que qd on vient du formulaire
0337d704 169$adresses_principales = 0;
170reset($adresses);
171foreach($adresses as $adrid => $adr) {
e0a21127 172 //validité de chaque adresse
173 $description = (($adr['numero_formulaire'] > 0)?"Adresse n°{$adr['numero_formulaire']}":"Nouvelle adresse");
174 if (strlen(strtok($adr['adr1'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr1'])) {
175 $page->trig("Le champ '$description - Ligne 1' contient un caractère interdit.");
0337d704 176 }
e0a21127 177 if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2'])) {
178 $page->trig("Le champ '$description - Ligne 2' contient un caractère interdit.");
0337d704 179 }
e0a21127 180 if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3'])) {
181 $page->trig("Le champ '$description - Ligne 3' contient un caractère interdit.");
0337d704 182 }
e0a21127 183 if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode'])) {
184 $page->trig("Le champ '$description - Code Postal' contient un caractère interdit.");
0337d704 185 }
e0a21127 186 if (is_array($adr['tels'])) {
187 foreach ($adr['tels'] as $tel) {
188 if (strlen(strtok($tel['tel'],"<>{}@&#~:;?,!§*_`[]|%$^=\"")) < strlen($tel['tel'])) {
189 $page->trig("Le champ '$description - ".$tel['tel_type']."' contient un caractère interdit.");
190 }
191 }
0337d704 192 }
e0a21127 193 if(!$adr['secondaire']) {
194 if($adresses_principales == 1) { //deja une adresse principale
195 $page->trig("Tu ne peux avoir qu'une résidence principale.");
196 $adresses_principales++; //pour eviter de repeter le message plusieurs fois
197 } else {
198 $adresses_principales = 1;
199 }
0337d704 200 }
0337d704 201}
202
203}
204
a7de4ef7 205// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 206?>