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