Rewrite FlagSet in order to run flag operation in constant time.
[platal.git] / modules / profile / addresses.inc.php
CommitLineData
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 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 78 $current = 0;
2db7542d 79 $success = true;
9a1cb883
FB
80 foreach ($value as $key=>&$adr) {
81 if (@$adr['current']) {
82 $current++;
83 }
84 }
2db7542d
FB
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) {
9a1cb883 91 $success = false;
9a1cb883 92 }
7568a515 93 foreach ($value as $key=>&$adr) {
a619333a 94 $ls = true;
7568a515 95 $this->geolocAddress($adr, $s);
a619333a
FB
96 $ls = ($ls && $s);
97 $this->cleanAddress($page, $adr, $s);
98 $ls = ($ls && $s);
c6a7beb2
FB
99 if (!trim($adr['text'])) {
100 unset($value[$key]);
a619333a
FB
101 } else if (!$init) {
102 $success = ($success && $ls);
c6a7beb2 103 }
7568a515 104 }
0b14f91d
FB
105 return $value;
106 }
d5489b09
FB
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 {
113f6de8 120 $flags = new PlFlagSet();
d5489b09 121 if ($address['secondaire']) {
77e786e1 122 $flags->addFlag('res-secondaire');
d5489b09
FB
123 }
124 if ($address['mail']) {
77e786e1 125 $flags->addFlag('courrier');
d5489b09
FB
126 }
127 if ($address['temporary']) {
77e786e1 128 $flags->addFlag('temporaire');
d5489b09
FB
129 }
130 if ($address['current']) {
77e786e1 131 $flags->addFlag('active');
d5489b09 132 }
f93fb300 133 if ($address['checked']) {
77e786e1 134 $flags->addFlag('coord-checked');
f93fb300 135 }
c24a06aa 136 XDB::execute("INSERT INTO adresses (adr1, adr2, adr3,
d5489b09
FB
137 postcode, city, cityid,
138 country, region, regiontxt,
139 pub, datemaj, statut,
de0485fd 140 uid, adrid, glat, glng)
d5489b09
FB
141 VALUES ({?}, {?}, {?},
142 {?}, {?}, {?},
143 {?}, {?}, {?},
144 {?}, FROM_UNIXTIME({?}), {?},
de0485fd 145 {?}, {?}, {?}, {?})",
d5489b09
FB
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,
de0485fd 150 S::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon']);
d5489b09
FB
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();
a2a1c2f2 178 $this->watched['addresses'] = true;
0b14f91d
FB
179 }
180
7c2e0f0d 181 protected function _fetchData()
0b14f91d 182 {
0b14f91d 183 // Build the addresses tree
7568a515 184 $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
d5489b09 185 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
7568a515 186 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
d5489b09 187 a.pub, a.country, gp.pays AS countrytxt, gp.display,
f93fb300 188 FIND_IN_SET('coord-checked', a.statut) AS checked,
7568a515
FB
189 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
190 FIND_IN_SET('courrier', a.statut) AS mail,
d5489b09 191 FIND_IN_SET('temporaire', a.statut) AS temporary,
de0485fd
FB
192 FIND_IN_SET('active', a.statut) AS current,
193 a.glat AS precise_lat, a.glng AS precise_lon
7568a515 194 FROM adresses AS a
37d44b3b 195 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
7568a515 196 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
0b14f91d
FB
197 ORDER BY adrid",
198 S::i('uid'));
c24a06aa
FB
199 if ($res->numRows() == 0) {
200 $this->values['addresses'] = array();
201 } else {
202 $this->values['addresses'] = $res->fetchAllAssoc();
203 }
0b14f91d 204
7568a515
FB
205 $res = XDB::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
206 FROM tels
207 WHERE uid = {?}
208 ORDER BY adrid",
209 S::i('uid'));
0b14f91d 210 $i = 0;
c24a06aa 211 $adrNb = count($this->values['addresses']);
7568a515
FB
212 while ($tel = $res->next()) {
213 $adrid = $tel['adrid'];
214 unset($tel['adrid']);
c24a06aa 215 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
0b14f91d
FB
216 $i++;
217 }
c24a06aa
FB
218 if ($i >= $adrNb) {
219 break;
220 }
0b14f91d
FB
221 $address =& $this->values['addresses'][$i];
222 if (!isset($address['tel'])) {
223 $address['tel'] = array();
224 }
225 if ($address['id'] == $adrid) {
7568a515 226 $address['tel'][] = $tel;
0b14f91d
FB
227 }
228 }
7568a515 229 foreach ($this->values['addresses'] as $id=>&$address) {
c24a06aa
FB
230 if (!isset($address['tel'])) {
231 $address['tel'] = array();
232 }
7568a515
FB
233 unset($address['id']);
234 }
0b14f91d 235 }
0b14f91d
FB
236}
237
238// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
239?>