Convert source code to UTF-8
[platal.git] / include / profil / verif_adresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 global $adresses;
23
24 function 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
47 function 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 ++;
59 else
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
65 function replace_ifset_adr($varname, $i){
66 $tab = Env::v($varname, Array());
67 if (isset($tab[$i]))
68 $GLOBALS['adresses'][$i][$varname] = $tab[$i];
69 }
70
71 function replace_ifset_tel($varname, $i, $t){
72 $tab = Env::v($varname.$t, Array());
73 if (isset($tab[$i]))
74 $GLOBALS['adresses'][$i]['tels'][$t][$varname] = $tab[$i];
75 }
76
77 function get_adr_arg($varname, $i) {
78 $tab = Env::v($varname, Array());
79 return @$tab[$i];
80 }
81
82 function set_flag_adr($varname,$i){
83 $tab = Env::v($varname, Array());
84 if (isset($tab[$i])){
85 $GLOBALS['adresses'][$i][$varname] = 1;
86 }
87 else
88 $GLOBALS['adresses'][$i][$varname] = '0';
89 }
90
91 function 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 }
98
99 function replace_address($i){
100 global $adresses, $nb_tel_max;
101 if(!isset($adresses[$i])){
102 $adresses[$i]['nouvelle'] = 'ajout';
103 $adresses[$i]['adrid'] = $i;
104 }
105
106 replace_ifset_adr('secondaire', $i);
107 set_flag_adr('courrier', $i);
108 replace_ifset_adr('temporaire', $i);
109 if(Env::i('adrid_active', $i+1) == $i)
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);
121 replace_ifset_adr('pub', $i);
122
123 for ($telid = 0; $telid <= $nb_tel_max; $telid++) {
124 $tab = Env::v('telid'.$telid, Array());
125 if(isset($tab[$i])){ //ce telid etait donc present dans le formulaire
126 replace_tel($i, $telid);
127 }
128 }
129
130 if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i);
131 $tab = Env::v('numero_formulaire', Array());
132 if($tab[$i])
133 $adresses[$i]['numero_formulaire'] = $tab[$i];
134 else
135 $adresses[$i]['numero_formulaire'] = -1;
136 }
137
138 function geoloc_adresse($i) {
139 global $adresses;
140 $change = Env::v('change'.$i);
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']);
147 if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) || get_adr_arg('parsevalid', $i))
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 :
159 for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++) {
160 $tab = Env::v('adrid', Array());
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
168 if ($opened_tab == 'adresses' && Env::has('modifier')){ // on ne valide que qd on vient du formulaire
169 $adresses_principales = 0;
170 reset($adresses);
171 foreach($adresses as $adrid => $adr) {
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 {
176 $page->trig("Le champ '$description - Ligne 1' contient un caractère interdit.");
177 }
178 if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2']))
179 {
180 $page->trig("Le champ '$description - Ligne 2' contient un caractère interdit.");
181 }
182 if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3']))
183 {
184 $page->trig("Le champ '$description - Ligne 3' contient un caractère interdit.");
185 }
186 if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
187 {
188 $page->trig("Le champ '$description - Code Postal' contient un caractère interdit.");
189 }
190 foreach ($adr['tels'] as $tel) {
191 if (strlen(strtok($tel['tel'],"<>{}@&#~:;?,!§*_`[]|%$^=\"")) < strlen($tel['tel']))
192 {
193 $page->trig("Le champ '$description - ".$tel['tel_type']."' contient un caractère interdit.");
194 }
195 }
196 if(!$adr['secondaire']){
197 if($adresses_principales == 1){ //deja une adresse principale
198 $page->trig("Tu ne peux avoir qu'une résidence principale.");
199 $adresses_principales++;//pour eviter de repeter le message plusieurs fois
200 }
201 else $adresses_principales = 1;
202 }
203 }
204
205 }
206
207 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
208 ?>