Commit | Line | Data |
---|---|---|
0b14f91d FB |
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 | ||
d5489b09 | 22 | class ProfileAddress |
0b14f91d | 23 | { |
d5489b09 FB |
24 | private $bool; |
25 | private $pub; | |
26 | ||
27 | public function __construct() | |
28 | { | |
29 | $this->bool = new ProfileBool(); | |
30 | $this->pub = new ProfilePub(); | |
31 | } | |
32 | ||
33 | private function geolocAddress(array &$address, &$success) | |
7568a515 FB |
34 | { |
35 | require_once 'geoloc.inc.php'; | |
c24a06aa | 36 | $success = true; |
32346370 FB |
37 | unset($address['geoloc']); |
38 | unset($address['geoloc_cityid']); | |
7568a515 FB |
39 | if (@$address['parsevalid'] || (@$address['text'] && @$address['changed']) || !@$address['cityid']) { |
40 | $address = array_merge($address, empty_address()); | |
41 | $new = get_address_infos(@$address['text']); | |
42 | if (compare_addresses_text(@$adress['text'], $geotxt = get_address_text($new)) | |
43 | || @$address['parsevalid']) { | |
44 | $address = array_merge($address, $new); | |
45 | } else { | |
46 | $success = false; | |
47 | $address = array_merge($address, cut_address(@$address['text'])); | |
48 | $address['geoloc'] = $geotxt; | |
49 | $address['geoloc_cityid'] = $new['cityid']; | |
50 | } | |
51 | } | |
d5489b09 FB |
52 | if (@$address['changed']) { |
53 | $address['datemaj'] = time(); | |
54 | } | |
7568a515 | 55 | $address['text'] = get_address_text($address); |
7568a515 FB |
56 | } |
57 | ||
d5489b09 FB |
58 | private function cleanAddress(ProfilePage &$page, array &$address) |
59 | { | |
60 | if (@$address['changed']) { | |
61 | $address['datemaj'] = time(); | |
62 | } | |
63 | foreach ($address['tel'] as $t=>&$tel) { | |
c6a7beb2 | 64 | if (@$tel['removed'] || !trim($tel['tel'])) { |
d5489b09 FB |
65 | unset($address['tel'][$t]); |
66 | } else { | |
67 | $tel['pub'] = $this->pub->value($page, 'pub', $tel['pub'], $success); | |
68 | } | |
c24a06aa | 69 | unset($tel['removed']); |
d5489b09 | 70 | } |
c24a06aa FB |
71 | if (@$address['changed']) { |
72 | $address['datemaj'] = time(); | |
73 | } | |
74 | $success = true; | |
d5489b09 FB |
75 | $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $success); |
76 | $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $success); | |
77 | $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $success); | |
c24a06aa | 78 | $address['current'] = $this->bool->value($page, 'current', @$address['current'], $success); |
d5489b09 | 79 | $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $success); |
c24a06aa FB |
80 | unset($address['parsevalid']); |
81 | unset($address['changed']); | |
82 | unset($address['removed']); | |
83 | unset($address['display']); | |
d5489b09 FB |
84 | } |
85 | ||
0b14f91d FB |
86 | public function value(ProfilePage &$page, $field, $value, &$success) |
87 | { | |
7568a515 | 88 | $init = false; |
0b14f91d | 89 | if (is_null($value)) { |
7568a515 FB |
90 | $value = $page->values['addresses']; |
91 | $init = true; | |
0b14f91d FB |
92 | } |
93 | foreach ($value as $key=>&$adr) { | |
7568a515 | 94 | if (@$adr['removed']) { |
0b14f91d FB |
95 | unset($value[$key]); |
96 | } | |
97 | } | |
9a1cb883 FB |
98 | $current = 0; |
99 | foreach ($value as $key=>&$adr) { | |
100 | if (@$adr['current']) { | |
101 | $current++; | |
102 | } | |
103 | } | |
104 | if (!$init && $current != 1 && count($value) > 0) { | |
105 | $success = false; | |
106 | } else { | |
107 | $success = true; | |
108 | } | |
7568a515 FB |
109 | foreach ($value as $key=>&$adr) { |
110 | $this->geolocAddress($adr, $s); | |
d5489b09 | 111 | $this->cleanAddress($page, $adr); |
7568a515 FB |
112 | if (!$init) { |
113 | $success = $success && $s; | |
114 | } | |
c6a7beb2 FB |
115 | if (!trim($adr['text'])) { |
116 | unset($value[$key]); | |
117 | } | |
7568a515 | 118 | } |
0b14f91d FB |
119 | return $value; |
120 | } | |
d5489b09 FB |
121 | |
122 | private function saveTel($adrid, $telid, array &$tel) | |
123 | { | |
124 | XDB::execute("INSERT INTO tels (uid, adrid, telid, | |
125 | tel_type, tel_pub, tel) | |
126 | VALUES ({?}, {?}, {?}, | |
127 | {?}, {?}, {?})", | |
128 | S::i('uid'), $adrid, $telid, | |
129 | $tel['type'], $tel['pub'], $tel['tel']); | |
130 | } | |
131 | ||
132 | private function saveAddress($adrid, array &$address) | |
133 | { | |
134 | $flags = array(); | |
135 | if ($address['secondaire']) { | |
136 | $flags[] = 'res-secondaire'; | |
137 | } | |
138 | if ($address['mail']) { | |
139 | $flags[] = 'courrier'; | |
140 | } | |
141 | if ($address['temporary']) { | |
142 | $flags[] = 'temporaire'; | |
143 | } | |
144 | if ($address['current']) { | |
145 | $flags[] = 'active'; | |
146 | } | |
147 | $flags = implode(',', $flags); | |
c24a06aa | 148 | XDB::execute("INSERT INTO adresses (adr1, adr2, adr3, |
d5489b09 FB |
149 | postcode, city, cityid, |
150 | country, region, regiontxt, | |
151 | pub, datemaj, statut, | |
152 | uid, adrid) | |
153 | VALUES ({?}, {?}, {?}, | |
154 | {?}, {?}, {?}, | |
155 | {?}, {?}, {?}, | |
156 | {?}, FROM_UNIXTIME({?}), {?}, | |
157 | {?}, {?})", | |
158 | $address['adr1'], $address['adr2'], $address['adr3'], | |
159 | $address['postcode'], $address['city'], $address['cityid'], | |
160 | $address['country'], $address['region'], $address['regiontxt'], | |
161 | $address['pub'], $address['datemaj'], $flags, | |
162 | S::i('uid'), $adrid); | |
163 | foreach ($address['tel'] as $telid=>&$tel) { | |
164 | $this->saveTel($adrid, $telid, $tel); | |
165 | } | |
166 | } | |
167 | ||
168 | public function save(ProfilePage &$page, $field, $value) | |
169 | { | |
170 | XDB::execute("DELETE FROM adresses | |
171 | WHERE uid = {?}", | |
172 | S::i('uid')); | |
173 | XDB::execute("DELETE FROM tels | |
174 | WHERE uid = {?}", | |
175 | S::i('uid')); | |
176 | foreach ($value as $adrid=>&$address) { | |
177 | $this->saveAddress($adrid, $address); | |
178 | } | |
179 | } | |
0b14f91d FB |
180 | } |
181 | ||
182 | class ProfileAddresses extends ProfilePage | |
183 | { | |
184 | protected $pg_template = 'profile/adresses.tpl'; | |
185 | ||
186 | public function __construct(PlWizard &$wiz) | |
187 | { | |
188 | parent::__construct($wiz); | |
189 | $this->settings['addresses'] = new ProfileAddress(); | |
190 | } | |
191 | ||
192 | protected function fetchData() | |
193 | { | |
194 | if (count($this->orig) > 0) { | |
195 | $this->values = $this->orig; | |
196 | return; | |
197 | } | |
198 | // Build the addresses tree | |
7568a515 | 199 | $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3, |
d5489b09 | 200 | UNIX_TIMESTAMP(a.datemaj) AS datemaj, |
7568a515 | 201 | a.postcode, a.city, a.cityid, a.region, a.regiontxt, |
d5489b09 | 202 | a.pub, a.country, gp.pays AS countrytxt, gp.display, |
7568a515 FB |
203 | FIND_IN_SET('res-secondaire', a.statut) AS secondaire, |
204 | FIND_IN_SET('courrier', a.statut) AS mail, | |
d5489b09 | 205 | FIND_IN_SET('temporaire', a.statut) AS temporary, |
7568a515 FB |
206 | FIND_IN_SET('active', a.statut) AS current |
207 | FROM adresses AS a | |
208 | INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country) | |
209 | WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut) | |
0b14f91d FB |
210 | ORDER BY adrid", |
211 | S::i('uid')); | |
c24a06aa FB |
212 | if ($res->numRows() == 0) { |
213 | $this->values['addresses'] = array(); | |
214 | } else { | |
215 | $this->values['addresses'] = $res->fetchAllAssoc(); | |
216 | } | |
0b14f91d | 217 | |
7568a515 FB |
218 | $res = XDB::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel |
219 | FROM tels | |
220 | WHERE uid = {?} | |
221 | ORDER BY adrid", | |
222 | S::i('uid')); | |
0b14f91d | 223 | $i = 0; |
c24a06aa | 224 | $adrNb = count($this->values['addresses']); |
7568a515 FB |
225 | while ($tel = $res->next()) { |
226 | $adrid = $tel['adrid']; | |
227 | unset($tel['adrid']); | |
c24a06aa | 228 | while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) { |
0b14f91d FB |
229 | $i++; |
230 | } | |
c24a06aa FB |
231 | if ($i >= $adrNb) { |
232 | break; | |
233 | } | |
0b14f91d FB |
234 | $address =& $this->values['addresses'][$i]; |
235 | if (!isset($address['tel'])) { | |
236 | $address['tel'] = array(); | |
237 | } | |
238 | if ($address['id'] == $adrid) { | |
7568a515 | 239 | $address['tel'][] = $tel; |
0b14f91d FB |
240 | } |
241 | } | |
7568a515 | 242 | foreach ($this->values['addresses'] as $id=>&$address) { |
c24a06aa FB |
243 | if (!isset($address['tel'])) { |
244 | $address['tel'] = array(); | |
245 | } | |
7568a515 FB |
246 | unset($address['id']); |
247 | } | |
0b14f91d FB |
248 | parent::fetchData(); |
249 | } | |
0b14f91d FB |
250 | } |
251 | ||
252 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
253 | ?> |