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