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