Start working on the medals page
[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 unset($address['geoloc']);
38 unset($address['geoloc_cityid']);
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 }
52 if (@$address['changed']) {
53 $address['datemaj'] = time();
54 }
55 $address['text'] = get_address_text($address);
56 }
57
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) {
64 if (@$tel['removed'] || !trim($tel['tel'])) {
65 unset($address['tel'][$t]);
66 } else {
67 $tel['pub'] = $this->pub->value($page, 'pub', $tel['pub'], $success);
68 }
69 unset($tel['removed']);
70 }
71 if (@$address['changed']) {
72 $address['datemaj'] = time();
73 }
74 $success = true;
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);
78 $address['current'] = $this->bool->value($page, 'current', @$address['current'], $success);
79 $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $success);
80 unset($address['parsevalid']);
81 unset($address['changed']);
82 unset($address['removed']);
83 unset($address['display']);
84 }
85
86 public function value(ProfilePage &$page, $field, $value, &$success)
87 {
88 $init = false;
89 if (is_null($value)) {
90 $value = $page->values['addresses'];
91 $init = true;
92 }
93 foreach ($value as $key=>&$adr) {
94 if (@$adr['removed']) {
95 unset($value[$key]);
96 }
97 }
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 }
109 foreach ($value as $key=>&$adr) {
110 $this->geolocAddress($adr, $s);
111 $this->cleanAddress($page, $adr);
112 if (!$init) {
113 $success = $success && $s;
114 }
115 if (!trim($adr['text'])) {
116 unset($value[$key]);
117 }
118 }
119 return $value;
120 }
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);
148 XDB::execute("INSERT INTO adresses (adr1, adr2, adr3,
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 }
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
199 $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
200 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
201 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
202 a.pub, a.country, gp.pays AS countrytxt, gp.display,
203 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
204 FIND_IN_SET('courrier', a.statut) AS mail,
205 FIND_IN_SET('temporaire', a.statut) AS temporary,
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)
210 ORDER BY adrid",
211 S::i('uid'));
212 if ($res->numRows() == 0) {
213 $this->values['addresses'] = array();
214 } else {
215 $this->values['addresses'] = $res->fetchAllAssoc();
216 }
217
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'));
223 $i = 0;
224 $adrNb = count($this->values['addresses']);
225 while ($tel = $res->next()) {
226 $adrid = $tel['adrid'];
227 unset($tel['adrid']);
228 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
229 $i++;
230 }
231 if ($i >= $adrNb) {
232 break;
233 }
234 $address =& $this->values['addresses'][$i];
235 if (!isset($address['tel'])) {
236 $address['tel'] = array();
237 }
238 if ($address['id'] == $adrid) {
239 $address['tel'][] = $tel;
240 }
241 }
242 foreach ($this->values['addresses'] as $id=>&$address) {
243 if (!isset($address['tel'])) {
244 $address['tel'] = array();
245 }
246 unset($address['id']);
247 }
248 parent::fetchData();
249 }
250 }
251
252 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
253 ?>