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