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