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