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