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