Merge branch 'master' into fusionax
[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
73f6c165 22class ProfileAddress extends ProfileGeocoding
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
041a5cec 33 private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
d5489b09 34 {
bde2be3b
GB
35 if (!isset($address['tel'])) {
36 $address['tel'] = array();
d5489b09 37 }
041a5cec
SJ
38 $profiletel = new ProfilePhones('address', $addrid);
39 $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s);
40 $address['current'] = $this->bool->value($page, 'current', $address['current'], $s);
a619333a 41 $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
041a5cec
SJ
42 $address['secondary'] = $this->bool->value($page, 'secondary', $address['secondary'], $s);
43 $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s);
44 $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s);
d5489b09
FB
45 }
46
0b14f91d
FB
47 public function value(ProfilePage &$page, $field, $value, &$success)
48 {
7568a515 49 $init = false;
0b14f91d 50 if (is_null($value)) {
7568a515 51 $value = $page->values['addresses'];
041a5cec 52 $init = true;
0b14f91d 53 }
041a5cec
SJ
54 foreach ($value as $key => &$address) {
55 if (isset($address['removed']) && $address['removed']) {
0b14f91d
FB
56 unset($value[$key]);
57 }
58 }
9a1cb883 59 $current = 0;
2db7542d 60 $success = true;
041a5cec
SJ
61 foreach ($value as $key => &$address) {
62 if (isset($address['current']) && $address['current']) {
9a1cb883
FB
63 $current++;
64 }
65 }
2db7542d 66 if ($current == 0 && count($value) > 0) {
041a5cec
SJ
67 foreach ($value as $address) {
68 $address['current'] = true;
2db7542d
FB
69 break;
70 }
041a5cec 71 } elseif ($current > 1) {
9a1cb883 72 $success = false;
9a1cb883 73 }
041a5cec
SJ
74 foreach ($value as $key => &$address) {
75 if (!trim($address['text'])) {
c6a7beb2 76 unset($value[$key]);
041a5cec 77 } elseif (!$init) {
73f6c165 78 $this->geocodeAddress($address, $s);
041a5cec 79 $success = $success && $s;
c6a7beb2 80 }
041a5cec 81 $this->cleanAddress($page, $key, $address);
7568a515 82 }
0b14f91d
FB
83 return $value;
84 }
d5489b09 85
041a5cec 86 private function saveTel($addrid, $telid, array &$tel)
d5489b09 87 {
b235d980 88 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
041a5cec 89 search_tel, display_tel, pub)
5e4417a9 90 VALUES ({?}, 'address', {?}, {?}, {?},
d5489b09 91 {?}, {?}, {?})",
041a5cec
SJ
92 S::i('uid'), $addrid, $telid, $tel['type'],
93 format_phone_number($tel['tel']), $tel['tel'], $tel['pub']);
d5489b09
FB
94 }
95
541e8d03 96 public function saveAddress($addrid, array &$address, $type)
d5489b09 97 {
4c906759 98 require_once "geocoding.inc.php";
041a5cec 99
113f6de8 100 $flags = new PlFlagSet();
041a5cec
SJ
101 if ($address['current']) {
102 $flags->addFlag('current');
d5489b09
FB
103 }
104 if ($address['temporary']) {
041a5cec 105 $flags->addFlag('temporary');
d5489b09 106 }
041a5cec
SJ
107 if ($address['secondary']) {
108 $flags->addFlag('secondary');
f93fb300 109 }
041a5cec
SJ
110 if ($address['mail']) {
111 $flags->addFlag('mail');
112 }
113 if ($address['cedex'] =
114 (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
115 array("", "\n"), $address['text'])), 'CEDEX')) !== false) {
116 $flags->addFlag('cedex');
117 }
4c906759
SJ
118 Geocoder::getAreaId($address, "administrativeArea");
119 Geocoder::getAreaId($address, "subAdministrativeArea");
120 Geocoder::getAreaId($address, "locality");
041a5cec
SJ
121 XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
122 text, postalText, postalCode, localityId,
123 subAdministrativeAreaId, administrativeAreaId,
124 countryId, latitude, longitude, updateTime, pub, comment,
125 north, south, east, west)
541e8d03 126 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
041a5cec 127 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
541e8d03 128 S::i('uid'), $type, $addrid, $flags, $address['accuracy'],
5a10ab14 129 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
041a5cec
SJ
130 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
131 $address['countryId'], $address['latitude'], $address['longitude'],
132 $address['updateTime'], $address['pub'], $address['comment'],
133 $address['north'], $address['south'], $address['east'], $address['west']);
d5489b09
FB
134 }
135
136 public function save(ProfilePage &$page, $field, $value)
137 {
041a5cec
SJ
138 XDB::execute("DELETE FROM profile_addresses
139 WHERE pid = {?} AND type = 'home'",
d5489b09 140 S::i('uid'));
b235d980 141 XDB::execute("DELETE FROM profile_phones
5e4417a9 142 WHERE uid = {?} AND link_type = 'address'",
d5489b09 143 S::i('uid'));
041a5cec 144 foreach ($value as $addrid => &$address) {
541e8d03 145 $this->saveAddress($addrid, $address, 'home');
041a5cec 146 $profiletel = new ProfilePhones('address', $addrid);
bde2be3b 147 $profiletel->saveTels('tel', $address['tel']);
d5489b09
FB
148 }
149 }
0b14f91d
FB
150}
151
152class ProfileAddresses extends ProfilePage
153{
154 protected $pg_template = 'profile/adresses.tpl';
155
156 public function __construct(PlWizard &$wiz)
157 {
158 parent::__construct($wiz);
159 $this->settings['addresses'] = new ProfileAddress();
041a5cec 160 $this->watched['addresses'] = true;
0b14f91d
FB
161 }
162
7c2e0f0d 163 protected function _fetchData()
0b14f91d 164 {
541e8d03 165 $res = XDB::query("SELECT id, accuracy, text, postalText,
041a5cec 166 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
eecbf7f5 167 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
041a5cec
SJ
168 north, south, east, west,
169 FIND_IN_SET('current', flags) AS current,
170 FIND_IN_SET('temporary', flags) AS temporary,
171 FIND_IN_SET('secondary', flags) AS secondary,
172 FIND_IN_SET('mail', flags) AS mail,
173 FIND_IN_SET('cedex', flags) AS cedex
174 FROM profile_addresses
175 WHERE pid = {?} AND type = 'home'
176 ORDER BY id",
0b14f91d 177 S::i('uid'));
c24a06aa
FB
178 if ($res->numRows() == 0) {
179 $this->values['addresses'] = array();
180 } else {
181 $this->values['addresses'] = $res->fetchAllAssoc();
182 }
0b14f91d 183
041a5cec 184 $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment
b235d980 185 FROM profile_phones
5e4417a9
GB
186 WHERE uid = {?} AND link_type = 'address'
187 ORDER BY link_id",
7568a515 188 S::i('uid'));
0b14f91d 189 $i = 0;
c24a06aa 190 $adrNb = count($this->values['addresses']);
7568a515 191 while ($tel = $res->next()) {
041a5cec
SJ
192 $adrid = $tel['addrid'];
193 unset($tel['addrid']);
c24a06aa 194 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
0b14f91d
FB
195 $i++;
196 }
c24a06aa
FB
197 if ($i >= $adrNb) {
198 break;
199 }
0b14f91d
FB
200 $address =& $this->values['addresses'][$i];
201 if (!isset($address['tel'])) {
202 $address['tel'] = array();
203 }
204 if ($address['id'] == $adrid) {
7568a515 205 $address['tel'][] = $tel;
0b14f91d
FB
206 }
207 }
041a5cec 208 foreach ($this->values['addresses'] as $id => &$address) {
c24a06aa 209 if (!isset($address['tel'])) {
7f5418fd
SJ
210 $address['tel'] = array(
211 0 => array(
212 'type' => 'fixed',
213 'tel' => '',
214 'pub' => 'private',
215 'comment' => '',
216 )
217 );
c24a06aa 218 }
7568a515 219 unset($address['id']);
7f5418fd
SJ
220 $address['changed'] = 0;
221 $address['removed'] = 0;
7568a515 222 }
0b14f91d 223 }
0b14f91d
FB
224}
225
226// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
227?>