93ee5cbfad3311648b54c193184819e7c70445ff
2 /***************************************************************************
3 * Copyright (C) 2003-2004 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 function generate_new_adrid(){
25 if(!isset($adresses)) //aucune adresses => retourne 1
29 $adrid_array = Array();
30 foreach($adresses as $numero => $adr){
31 $adrid_array[$i] = $numero;
34 sort($adrid_array,SORT_NUMERIC
); // classe les adrid dans l'ordre croissant
36 while(list($key,$current_adrid) = each($adrid_array)){
37 if($current_adrid == $new_adrid)
40 return $new_adrid; //s'ils sont differents, il y a un trou dans la liste des adrid donc new_adrid convient
42 //si aucun convient, on retourne le plus grand des adrid actuel + 1
46 function replace_ifset_adr($varname, $i){
47 $tab = Env
::getMixed($varname, Array());
49 $GLOBALS['adresses'][$i][$varname] = $tab[$i];
52 function get_adr_arg($varname, $i) {
53 $tab = Env
::getMixed($varname, Array());
57 function set_flag_adr($varname,$i){
58 $tab = Env
::getMixed($varname, Array());
60 $GLOBALS['adresses'][$i][$varname] = 1;
63 $GLOBALS['adresses'][$i][$varname] = '0';
67 function replace_address($i){
69 if(!isset($adresses[$i])){
70 $adresses[$i]['nouvelle'] = 'ajout';
71 $adresses[$i]['adrid'] = $i;
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;
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());
96 $adresses[$i]['numero_formulaire'] = $tab[$i];
98 $adresses[$i]['numero_formulaire'] = -1;
101 function geoloc_adresse($i) {
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);
113 $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt']));
114 $adresses[$i]['geoloc'] = $geotxt;
115 $adresses[$i]['geoloc_cityid'] = $new['cityid'];
118 $adresses[$i]['txt'] = get_address_text($adresses[$i]);
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);
127 if (isset($adresses[$adrid]['txt']))
128 geoloc_adresse($adrid);
131 if(Env
::get('old_tab', '') == 'adresses' && Env
::has('modifier')){ // on ne valide que qd on vient du formulaire
132 $adresses_principales = 0;
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']))
139 $str_error = $str_error."Le champ '$description - Ligne 1' contient un caractère interdit.<BR />";
141 if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2']))
143 $str_error = $str_error."Le champ '$description - Ligne 2' contient un caractère interdit.<BR />";
145 if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3']))
147 $str_error = $str_error."Le champ '$description - Ligne 3' contient un caractère interdit.<BR />";
149 if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
151 $str_error = $str_error."Le champ '$description - Code Postal' contient un caractère interdit.<BR />";
153 if (strlen(strtok($adr['city'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
155 $str_error = $str_error."Le champ '$description - Ville' contient un caractère interdit.<BR />";
157 if (strlen(strtok($adr['tel'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($adr['tel']))
159 $str_error = $str_error."Le champ '$description - Téléphone' contient un caractère interdit.<BR />";
161 if (strlen(strtok($adr['fax'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($adr['fax']))
163 $str_error = $str_error."Le champ '$description - Fax' contient un caractère interdit.<BR />";
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
170 else $adresses_principales = 1;
176 //on vire les adresses vides :
177 if(isset($adresses)){ // s'il y en a
179 foreach($adresses as $adrid => $adr){
180 if(is_adr_empty($adrid)){
181 delete_address($adrid);
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];
193 $adr['postcode'] = '';
195 $adr['country'] = '00';
199 $adr['secondaire'] = 1;
200 $adr['courrier'] = 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...)
215 foreach($adresses as $adrid_ => $adr_){
216 if(($adresses[$adrid_]['active']) && ($adr_['nouvelle'] != 'new')){
217 $ordre_des_adrid[$i] = $adrid_;
219 $est_attribuee[$adrid_] = 1;
222 $est_attribuee[$adrid_] = 0;
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_;
230 $est_attribuee[$adrid_] = 1;
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_;
239 $est_attribuee[$adrid_] = 1;
243 foreach($adresses as $adrid_ => $adr_){
244 if($est_attribuee[$adrid_] == 0){ // non attribuee
245 $ordre_des_adrid[$i] = $adrid_;
247 $est_attribuee[$adrid_] = 1;
252 $page->assign_by_ref('ordre_adrid',$ordre_des_adrid);
253 $page->assign('nb_adr',$nb_adr+
1);