775af6d8afda56d78efcf37c1fdb5e20650a40fa
[platal.git] / include / profil / update_adresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 reset($adresses);
24
25 function insert_new_tel($adrid, $tel) {
26 if ($tel['tel'] == "")
27 return;
28 $r = XDB::query("SELECT MAX(telid+1) FROM tels WHERE uid = {?} AND adrid = {?}", S::v('uid',-1), $adrid);
29 $newid = $r->fetchOneCell();
30 if (!$newid) $newid = 0;
31 XDB::execute( "INSERT INTO tels SET tel_type = {?}, tel_pub = {?},
32 tel = {?}, uid = {?}, adrid = {?}, telid = {?}",
33 $tel['tel_type'], $tel['tel_pub'], $tel['tel'],
34 S::v('uid', -1), $adrid, $newid);
35 }
36
37 foreach ($adresses as $adrid => $adr) {
38 if ($adr['nouvelle'] != 'new') {
39 // test si on vient de creer cette adresse dans verif_adresse.inc.php
40
41 //construction des bits
42 $statut = "";
43 if ($adr["secondaire"]) $statut .= 'res-secondaire,';
44 if ($adr["courrier"]) $statut .= 'courrier,';
45 if ($adr["active"]) $statut .= 'active,';
46 if ($adr["temporaire"]) $statut .= 'temporaire,';
47 if (! empty($statut)) $statut = substr($statut, 0, -1);
48 $precise_coords = "";
49 if (isset($adr['precise_lat']) && isset($adr['precise_lon'])) {
50 $precise_coords = ", glat = '".$adr['precise_lat']."'";
51 $precise_coords .= ", glng = '".$adr['precise_lon']."'";
52 }
53
54 if ($adr["nouvelle"] == 'ajout') {
55 //nouvelle adresse
56 if (is_adr_empty($adrid)) {
57 unset($adresses[$adrid]);
58 continue;
59 }
60 echo "Nouveau pas vide";
61 XDB::execute("INSERT INTO adresses SET adr1 = {?}, adr2 = {?},
62 adr3 = {?}, postcode = {?}, city = {?}, cityid = {?},
63 country = {?}, region = {?}, regiontxt = {?},
64 pub = {?}, datemaj = NOW(), statut = {?}, uid = {?},
65 adrid = {?}".$precise_coords, $adr['adr1'], $adr['adr2'],
66 $adr['adr3'], $adr['postcode'], $adr['city'],
67 $adr['cityid'], $adr['country'], $adr['region'],
68 $adr['regiontxt'], $adr['pub'], $statut,
69 S::v('uid', -1), $adrid);
70 $telsvalues = "";
71 foreach ($adr['tels'] as $tel) {
72 insert_new_tel($adrid, $tel);
73 }
74 } else {
75 //c'est une mise à jour
76 XDB::execute("UPDATE adresses SET adr1 = {?}, adr2 = {?},
77 adr3 = {?}, postcode = {?}, city = {?}, cityid = {?},
78 country = {?}, region = {?}, regiontxt = {?},
79 pub = {?}, datemaj = NOW(), statut = {?}".$precise_coords."
80 WHERE uid = {?} AND adrid = {?}", $adr['adr1'],
81 $adr['adr2'], $adr['adr3'], $adr['postcode'],
82 $adr['city'], $adr['cityid'], $adr['country'],
83 $adr['region'], $adr['regiontxt'], $adr['pub'],
84 $statut, S::v('uid', -1), $adrid);
85 foreach ($adr['tels'] as $tel) {
86 if (isset($tel['new_tel'])) {
87 if ($tel['new_tel'])
88 insert_new_tel($adrid, $tel);
89 } else {
90 if ($tel['tel'] != "") {
91 XDB::execute(
92 "UPDATE tels SET
93 tel_type = {?},
94 tel_pub = {?},
95 tel = {?}
96 WHERE
97 uid = {?} AND
98 adrid = {?} AND
99 telid = {?}",
100 $tel['tel_type'],
101 $tel['tel_pub'],
102 $tel['tel'],
103 S::v('uid', -1),
104 $adrid,
105 $tel['telid']);
106 } else {
107 XDB::execute(
108 "DELETE FROM tels WHERE
109 uid = {?} AND
110 adrid = {?} AND
111 telid = {?}",
112 S::v('uid', -1),
113 $adrid,
114 $tel['telid']);
115 }
116 }
117 }
118 }// fin nouvelle / ancienne adresse
119 }//fin if nouvellement crée
120 }//fin foreach
121
122
123 //on vire les adresses vides :
124 if(isset($adresses)){ // s'il y en a
125 reset($adresses);
126 foreach($adresses as $adrid => $adr){
127 // on vire les tels vides
128 foreach ($adr['tels'] as $telid => $tel) {
129 if ($tel['tel'] == '') unset($adresses[$adrid]['tels'][$telid]);
130 }
131 if(is_adr_empty($adrid)){
132 delete_address($adrid);
133 }
134 }
135 }
136
137 ?>