Informs user that her name change is awaiting validation (Closes #1083).
[platal.git] / modules / profile / addresses.inc.php
CommitLineData
0b14f91d
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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
12bcf04b 22class ProfileSettingAddress extends ProfileSettingGeocoding
0b14f91d 23{
d5489b09
FB
24 private $bool;
25 private $pub;
26
27 public function __construct()
28 {
12bcf04b
RB
29 $this->bool = new ProfileSettingBool();
30 $this->pub = new ProfileSettingPub();
d5489b09
FB
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 }
12bcf04b 38 $profiletel = new ProfileSettingPhones('address', $addrid);
041a5cec
SJ
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']) {
ed01acac 56 array_splice($value, $key, 1);
0b14f91d
FB
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) {
a08dc64f 67 foreach ($value as &$address) {
041a5cec 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
a6483c12 86 public function saveAddress($pid, $addrid, array &$address, $type)
d5489b09 87 {
a6483c12 88 require_once 'geocoding.inc.php';
041a5cec 89
113f6de8 90 $flags = new PlFlagSet();
5b2c9987
FB
91 $flags->addFlag('current', $address['current']);
92 $flags->addFlag('temporary', $address['temporary']);
93 $flags->addFlag('secondary', $address['secondary']);
94 $flags->addFlag('mail', $address['mail']);
95 $flags->addFlag('cedex', $address['cedex'] =
041a5cec 96 (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
5b2c9987 97 array("", "\n"), $address['text'])), 'CEDEX')) !== false);
4c906759
SJ
98 Geocoder::getAreaId($address, "administrativeArea");
99 Geocoder::getAreaId($address, "subAdministrativeArea");
100 Geocoder::getAreaId($address, "locality");
041a5cec
SJ
101 XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
102 text, postalText, postalCode, localityId,
103 subAdministrativeAreaId, administrativeAreaId,
104 countryId, latitude, longitude, updateTime, pub, comment,
105 north, south, east, west)
541e8d03 106 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
041a5cec 107 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
a6391000 108 $pid, $type, $addrid, $flags, $address['accuracy'],
5a10ab14 109 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
041a5cec
SJ
110 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
111 $address['countryId'], $address['latitude'], $address['longitude'],
112 $address['updateTime'], $address['pub'], $address['comment'],
113 $address['north'], $address['south'], $address['east'], $address['west']);
d5489b09
FB
114 }
115
116 public function save(ProfilePage &$page, $field, $value)
117 {
041a5cec
SJ
118 XDB::execute("DELETE FROM profile_addresses
119 WHERE pid = {?} AND type = 'home'",
e5bcd851 120 $page->pid());
b235d980 121 XDB::execute("DELETE FROM profile_phones
ce0b2c6f 122 WHERE pid = {?} AND link_type = 'address'",
e5bcd851 123 $page->pid());
041a5cec 124 foreach ($value as $addrid => &$address) {
a6391000 125 $this->saveAddress($page->pid(), $addrid, $address, 'home');
12bcf04b 126 $profiletel = new ProfileSettingPhones('address', $addrid);
e5bcd851 127 $profiletel->saveTels($page->pid(), 'tel', $address['tel']);
d5489b09
FB
128 }
129 }
0b14f91d
FB
130}
131
12bcf04b 132class ProfileSettingAddresses extends ProfilePage
0b14f91d
FB
133{
134 protected $pg_template = 'profile/adresses.tpl';
135
136 public function __construct(PlWizard &$wiz)
137 {
138 parent::__construct($wiz);
12bcf04b 139 $this->settings['addresses'] = new ProfileSettingAddress();
041a5cec 140 $this->watched['addresses'] = true;
0b14f91d
FB
141 }
142
7c2e0f0d 143 protected function _fetchData()
0b14f91d 144 {
541e8d03 145 $res = XDB::query("SELECT id, accuracy, text, postalText,
041a5cec 146 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
eecbf7f5 147 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
041a5cec
SJ
148 north, south, east, west,
149 FIND_IN_SET('current', flags) AS current,
150 FIND_IN_SET('temporary', flags) AS temporary,
151 FIND_IN_SET('secondary', flags) AS secondary,
152 FIND_IN_SET('mail', flags) AS mail,
153 FIND_IN_SET('cedex', flags) AS cedex
154 FROM profile_addresses
155 WHERE pid = {?} AND type = 'home'
156 ORDER BY id",
e5bcd851 157 $this->pid());
c24a06aa
FB
158 if ($res->numRows() == 0) {
159 $this->values['addresses'] = array();
160 } else {
161 $this->values['addresses'] = $res->fetchAllAssoc();
162 }
0b14f91d 163
041a5cec 164 $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment
b235d980 165 FROM profile_phones
ce0b2c6f 166 WHERE pid = {?} AND link_type = 'address'
5e4417a9 167 ORDER BY link_id",
e5bcd851 168 $this->pid());
0b14f91d 169 $i = 0;
c24a06aa 170 $adrNb = count($this->values['addresses']);
7568a515 171 while ($tel = $res->next()) {
383ee0d0 172 $addrid = $tel['addrid'];
041a5cec 173 unset($tel['addrid']);
383ee0d0 174 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $addrid) {
0b14f91d
FB
175 $i++;
176 }
c24a06aa
FB
177 if ($i >= $adrNb) {
178 break;
179 }
0b14f91d
FB
180 $address =& $this->values['addresses'][$i];
181 if (!isset($address['tel'])) {
182 $address['tel'] = array();
183 }
383ee0d0 184 if ($address['id'] == $addrid) {
7568a515 185 $address['tel'][] = $tel;
0b14f91d
FB
186 }
187 }
041a5cec 188 foreach ($this->values['addresses'] as $id => &$address) {
c24a06aa 189 if (!isset($address['tel'])) {
7f5418fd
SJ
190 $address['tel'] = array(
191 0 => array(
192 'type' => 'fixed',
193 'tel' => '',
194 'pub' => 'private',
195 'comment' => '',
196 )
197 );
c24a06aa 198 }
7568a515 199 unset($address['id']);
7f5418fd
SJ
200 $address['changed'] = 0;
201 $address['removed'] = 0;
7568a515 202 }
0b14f91d 203 }
0b14f91d
FB
204}
205
206// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
207?>