first reimport from platal
[platal.git] / include / profil / verif_adresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2004 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 function generate_new_adrid(){
24 global $adresses;
25 if(!isset($adresses)) //aucune adresses => retourne 1
26 return 1;
27 reset($adresses);
28 $i = 0;
29 $adrid_array = Array();
30 foreach($adresses as $numero => $adr){
31 $adrid_array[$i] = $numero;
32 $i++;
33 }
34 sort($adrid_array,SORT_NUMERIC); // classe les adrid dans l'ordre croissant
35 $new_adrid = 1;
36 while(list($key,$current_adrid) = each($adrid_array)){
37 if($current_adrid == $new_adrid)
38 $new_adrid++;
39 else
40 return $new_adrid; //s'ils sont differents, il y a un trou dans la liste des adrid donc new_adrid convient
41 }
42 //si aucun convient, on retourne le plus grand des adrid actuel + 1
43 return $new_adrid;
44 }
45
46 function replace_ifset_adr($varname, $i){
47 $tab = Env::getMixed($varname, Array());
48 if (isset($tab[$i]))
49 $GLOBALS['adresses'][$i][$varname] = $tab[$i];
50 }
51
52 function get_adr_arg($varname, $i) {
53 $tab = Env::getMixed($varname, Array());
54 return $tab[$i];
55 }
56
57 function set_flag_adr($varname,$i){
58 $tab = Env::getMixed($varname, Array());
59 if (isset($tab[$i])){
60 $GLOBALS['adresses'][$i][$varname] = 1;
61 }
62 else
63 $GLOBALS['adresses'][$i][$varname] = '0';
64 }
65
66
67 function replace_address($i){
68 global $adresses;
69 if(!isset($adresses[$i])){
70 $adresses[$i]['nouvelle'] = 'ajout';
71 $adresses[$i]['adrid'] = $i;
72 }
73
74 replace_ifset_adr('secondaire', $i);
75 set_flag_adr('courrier', $i);
76 replace_ifset_adr('temporaire', $i);
77 if(Env::getInt('adrid_active', $i+1) == $i)
78 $adresses[$i]['active'] = 1;
79 else
80 $adresses[$i]['active'] = 0;
81 replace_ifset_adr('adr1', $i);
82 replace_ifset_adr('adr2', $i);
83 replace_ifset_adr('adr3', $i);
84 replace_ifset_adr('postcode', $i);
85 replace_ifset_adr('city', $i);
86 replace_ifset_adr('cityid', $i);
87 replace_ifset_adr('country', $i);
88 replace_ifset_adr('region', $i);
89 replace_ifset_adr('tel', $i);
90 replace_ifset_adr('fax', $i);
91 replace_ifset_adr('pub', $i);
92 replace_ifset_adr('tel_pub', $i);
93 if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i);
94 $tab = Env::getMixed('numero_formulaire', Array());
95 if($tab[$i])
96 $adresses[$i]['numero_formulaire'] = $tab[$i];
97 else
98 $adresses[$i]['numero_formulaire'] = -1;
99 }
100
101 function geoloc_adresse($i) {
102 global $adresses;
103 $change = Env::get('change'.$i);
104 if (get_adr_arg('parsevalid', $i) || ($adresses[$i]['txt'] && $change) || (!$adresses[$i]['cityid'])) {
105 require_once('geoloc.inc.php');
106 // erases the previous address (but not the phone or pub)
107 $adresses[$i] = array_merge($adresses[$i], empty_address());
108 // localize new address
109 $new = get_address_infos($adresses[$i]['txt']);
110 if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) || get_adr_arg('parsevalid', $i))
111 $adresses[$i] = array_merge($adresses[$i], $new);
112 else {
113 $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt']));
114 $adresses[$i]['geoloc'] = $geotxt;
115 $adresses[$i]['geoloc_cityid'] = $new['cityid'];
116 }
117 }
118 $adresses[$i]['txt'] = get_address_text($adresses[$i]);
119 }
120
121 //remplace par les eventuelles nouvelles valeurs :
122 for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++) {
123 $tab = Env::getMixed('adrid', Array());
124 if(isset($tab[$adrid])){ //cet adrid etait donc present dans le formulaire
125 replace_address($adrid);
126 }
127 if (isset($adresses[$adrid]['txt']))
128 geoloc_adresse($adrid);
129 }
130
131 if(Env::get('old_tab', '') == 'adresses' && Env::has('modifier')){ // on ne valide que qd on vient du formulaire
132 $adresses_principales = 0;
133 reset($adresses);
134 foreach($adresses as $adrid => $adr) {
135 //validité de chaque adresse
136 $description = (($adr['numero_formulaire'] > 0)?"Adresse n°{$adr['numero_formulaire']}":"Nouvelle adresse");
137 if (strlen(strtok($adr['adr1'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr1']))
138 {
139 $str_error = $str_error."Le champ '$description - Ligne 1' contient un caractère interdit.<BR />";
140 }
141 if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2']))
142 {
143 $str_error = $str_error."Le champ '$description - Ligne 2' contient un caractère interdit.<BR />";
144 }
145 if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3']))
146 {
147 $str_error = $str_error."Le champ '$description - Ligne 3' contient un caractère interdit.<BR />";
148 }
149 if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
150 {
151 $str_error = $str_error."Le champ '$description - Code Postal' contient un caractère interdit.<BR />";
152 }
153 if (strlen(strtok($adr['city'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
154 {
155 $str_error = $str_error."Le champ '$description - Ville' contient un caractère interdit.<BR />";
156 }
157 if (strlen(strtok($adr['tel'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($adr['tel']))
158 {
159 $str_error = $str_error."Le champ '$description - Téléphone' contient un caractère interdit.<BR />";
160 }
161 if (strlen(strtok($adr['fax'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($adr['fax']))
162 {
163 $str_error = $str_error."Le champ '$description - Fax' contient un caractère interdit.<BR />";
164 }
165 if(!$adr['secondaire']){
166 if($adresses_principales == 1){ //deja une adresse principale
167 $str_error = $str_error."Tu ne peux avoir qu'une résidence principale.<BR />";
168 $adresses_principales++;//pour eviter de repeter le message plusieurs fois
169 }
170 else $adresses_principales = 1;
171 }
172 }
173
174 }
175
176 //on vire les adresses vides :
177 if(isset($adresses)){ // s'il y en a
178 reset($adresses);
179 foreach($adresses as $adrid => $adr){
180 if(is_adr_empty($adrid)){
181 delete_address($adrid);
182 }
183 }
184 }
185 //on génère une éventuelle nouvelle adresse
186 if (!isset($adresses) || (count($adresses) < $nb_adr_max)){
187 $adrid = generate_new_adrid();
188 $adresses[$adrid]['adrid'] = $adrid;
189 $adr = &$adresses[$adrid];
190 $adr['adr1'] = '';
191 $adr['adr2'] = '';
192 $adr['adr3'] = '';
193 $adr['postcode'] = '';
194 $adr['city'] = '';
195 $adr['country'] = '00';
196 $adr['region'] = '';
197 $adr['tel'] = '';
198 $adr['fax'] = '';
199 $adr['secondaire'] = 1;
200 $adr['courrier'] = 0;
201 $adr['active'] = 0;
202 $adr['temporaire'] = 1;
203 $adr['pub'] = 'private';
204 $adr['tel_pub'] = 'private';
205 $adr['nouvelle'] = 'new'; //n'est pas issue d'un formulaire (sert dans update_adresses...)
206 }
207
208 unset($adr);
209 unset($adrid);
210
211 //tri des adresses :
212
213 reset($adresses);
214 $i = 1;
215 foreach($adresses as $adrid_ => $adr_){
216 if(($adresses[$adrid_]['active']) && ($adr_['nouvelle'] != 'new')){
217 $ordre_des_adrid[$i] = $adrid_;
218 $i++;
219 $est_attribuee[$adrid_] = 1;
220 }
221 else
222 $est_attribuee[$adrid_] = 0;
223 }
224
225 reset($adresses);
226 foreach($adresses as $adrid_ => $adr_){
227 if(($adresses[$adrid_]['secondaire'] == 0) && ($est_attribuee[$adrid_] == 0) && ($adr_['nouvelle'] != 'new')){ // principale et non attribuee
228 $ordre_des_adrid[$i] = $adrid_;
229 $i++;
230 $est_attribuee[$adrid_] = 1;
231 }
232 }
233
234 reset($adresses);
235 foreach($adresses as $adrid_ => $adr_){
236 if(($adresses[$adrid_]['temporaire'] == 0) && ($est_attribuee[$adrid_] == 0) && ($adr_['nouvelle'] != 'new')){ // permanente et non attribuee
237 $ordre_des_adrid[$i] = $adrid_;
238 $i++;
239 $est_attribuee[$adrid_] = 1;
240 }
241 }
242 reset($adresses);
243 foreach($adresses as $adrid_ => $adr_){
244 if($est_attribuee[$adrid_] == 0){ // non attribuee
245 $ordre_des_adrid[$i] = $adrid_;
246 $i++;
247 $est_attribuee[$adrid_] = 1;
248 }
249 }
250
251 $nb_adr = $i - 1;
252 $page->assign_by_ref('ordre_adrid',$ordre_des_adrid);
253 $page->assign('nb_adr',$nb_adr+1);
254
255 ?>