6c4495dba2e34a8d1804fea100a0580594ace3b4
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 generate_new_telid($adr){
48 $telid_array = Array();
49 foreach($adr['tels'] as $tel){
50 $telid_array[$i] = $tel['telid'];
53 sort($telid_array,SORT_NUMERIC
); // classe les adrid dans l'ordre croissant
55 foreach($telid_array as $current_telid)
56 if ($current_telid == $new_telid)
59 return $new_telid;//s'ils sont differents, il y a un trou dans la liste des telid donc new_telid convient
60 //si aucun convient, on retourne le plus grand des telid actuel + 1
64 function replace_ifset_adr($varname, $i){
65 $tab = Env
::v($varname, Array());
67 $GLOBALS['adresses'][$i][$varname] = $tab[$i];
70 function replace_ifset_tel($varname, $i, $t){
71 $tab = Env
::v($varname.$t, Array());
73 $GLOBALS['adresses'][$i]['tels'][$t][$varname] = $tab[$i];
76 function get_adr_arg($varname, $i) {
77 $tab = Env
::v($varname, Array());
81 function set_flag_adr($varname,$i){
82 $tab = Env
::v($varname, Array());
84 $GLOBALS['adresses'][$i][$varname] = 1;
87 $GLOBALS['adresses'][$i][$varname] = '0';
90 function replace_tel($i, $t){
91 replace_ifset_tel('telid', $i, $t);
92 replace_ifset_tel('tel', $i, $t);
93 replace_ifset_tel('tel_pub', $i, $t);
94 replace_ifset_tel('tel_type', $i, $t);
95 replace_ifset_tel('new_tel', $i, $t);
98 function replace_address($i){
99 global $adresses, $nb_tel_max;
100 if(!isset($adresses[$i])){
101 $adresses[$i]['nouvelle'] = 'ajout';
102 $adresses[$i]['adrid'] = $i;
105 replace_ifset_adr('secondaire', $i);
106 set_flag_adr('courrier', $i);
107 replace_ifset_adr('temporaire', $i);
108 if(Env
::i('adrid_active', $i+
1) == $i)
109 $adresses[$i]['active'] = 1;
111 $adresses[$i]['active'] = 0;
112 replace_ifset_adr('adr1', $i);
113 replace_ifset_adr('adr2', $i);
114 replace_ifset_adr('adr3', $i);
115 replace_ifset_adr('postcode', $i);
116 replace_ifset_adr('city', $i);
117 replace_ifset_adr('cityid', $i);
118 replace_ifset_adr('country', $i);
119 replace_ifset_adr('region', $i);
120 replace_ifset_adr('pub', $i);
122 for ($telid = 0; $telid <= $nb_tel_max; $telid++
) {
123 $tab = Env
::v('telid'.$telid, Array());
124 if(isset($tab[$i])){ //ce telid etait donc present dans le formulaire
125 replace_tel($i, $telid);
129 if (!get_adr_arg('parsevalid', $i)) replace_ifset_adr('txt', $i);
130 $tab = Env
::v('numero_formulaire', Array());
132 $adresses[$i]['numero_formulaire'] = $tab[$i];
134 $adresses[$i]['numero_formulaire'] = -1;
137 function geoloc_adresse($i) {
139 $change = Env
::v('change'.$i);
140 if (get_adr_arg('parsevalid', $i) ||
($adresses[$i]['txt'] && $change) ||
(!$adresses[$i]['cityid'])) {
141 require_once('geoloc.inc.php');
142 // erases the previous address (but not the phone or pub)
143 $adresses[$i] = array_merge($adresses[$i], empty_address());
144 // localize new address
145 $new = get_address_infos($adresses[$i]['txt']);
146 if (compare_addresses_text($adresses[$i]['txt'], $geotxt = get_address_text($new)) ||
get_adr_arg('parsevalid', $i))
147 $adresses[$i] = array_merge($adresses[$i], $new);
149 $adresses[$i] = array_merge($adresses[$i], cut_address($adresses[$i]['txt']));
150 $adresses[$i]['geoloc'] = $geotxt;
151 $adresses[$i]['geoloc_cityid'] = $new['cityid'];
154 $adresses[$i]['txt'] = get_address_text($adresses[$i]);
157 //remplace par les eventuelles nouvelles valeurs :
158 for ($adrid = 1; $adrid <= $nb_adr_max; $adrid++
) {
159 $tab = Env
::v('adrid', Array());
160 if(isset($tab[$adrid])){ //cet adrid etait donc present dans le formulaire
161 replace_address($adrid);
163 if (isset($adresses[$adrid]['txt']))
164 geoloc_adresse($adrid);
167 if ($opened_tab == 'adresses' && Env
::has('modifier')){ // on ne valide que qd on vient du formulaire
168 $adresses_principales = 0;
170 foreach($adresses as $adrid => $adr) {
171 //validité de chaque adresse
172 $description = (($adr['numero_formulaire'] > 0)?
"Adresse n°{$adr['numero_formulaire']}":"Nouvelle adresse");
173 if (strlen(strtok($adr['adr1'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr1']))
175 $page->trig("Le champ '$description - Ligne 1' contient un caractère interdit.");
177 if (strlen(strtok($adr['adr2'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr2']))
179 $page->trig("Le champ '$description - Ligne 2' contient un caractère interdit.");
181 if (strlen(strtok($adr['adr3'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['adr3']))
183 $page->trig("Le champ '$description - Ligne 3' contient un caractère interdit.");
185 if (strlen(strtok($adr['postcode'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
187 $page->trig("Le champ '$description - Code Postal' contient un caractère interdit.");
189 if (strlen(strtok($adr['city'],"<>{}@~?!§*`|%$^=+")) < strlen($adr['postcode']))
191 $page->trig("Le champ '$description - Ville' contient un caractère interdit.");
193 foreach ($adr['tels'] as $tel) {
194 if (strlen(strtok($tel['tel'],"<>{}@&#~\/:;?,!§*_`[]|%$^=\"")) < strlen($tel['tel']))
196 $page->trig("Le champ '$description - ".$tel['tel_type']."' contient un caractère interdit.");
199 if(!$adr['secondaire']){
200 if($adresses_principales == 1){ //deja une adresse principale
201 $page->trig("Tu ne peux avoir qu'une résidence principale.");
202 $adresses_principales++
;//pour eviter de repeter le message plusieurs fois
204 else $adresses_principales = 1;
210 //on vire les adresses vides :
211 if(isset($adresses)){ // s'il y en a
213 foreach($adresses as $adrid => $adr){
214 // on vire les tels vides
215 foreach ($adr['tels'] as $telid => $tel) {
216 if ($tel['tel'] == '') unset($adresses[$adrid]['tels'][$telid]);
218 if(is_adr_empty($adrid)){
219 delete_address($adrid);