Notifies profile's owner when profile modified by a third party (Closes #1038).
[platal.git] / modules / profile / addresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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
22 class ProfileSettingAddress extends ProfileSettingGeocoding
23 {
24 private $bool;
25 private $pub;
26
27 public function __construct()
28 {
29 $this->bool = new ProfileSettingBool();
30 $this->pub = new ProfileSettingPub();
31 }
32
33 private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
34 {
35 if (!isset($address['tel'])) {
36 $address['tel'] = array();
37 }
38 $profiletel = new ProfileSettingPhones('address', $addrid);
39 $address['tel'] = $profiletel->value($page, 'tel', $address['tel'], $s);
40 $address['current'] = $this->bool->value($page, 'current', $address['current'], $s);
41 $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
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);
45 }
46
47 public function value(ProfilePage &$page, $field, $value, &$success)
48 {
49 $init = false;
50 if (is_null($value)) {
51 $value = $page->values['addresses'];
52 $init = true;
53 }
54 foreach ($value as $key => &$address) {
55 if (isset($address['removed']) && $address['removed']) {
56 array_splice($value, $key, 1);
57 }
58 }
59 $current = 0;
60 $success = true;
61 foreach ($value as $key => &$address) {
62 if (isset($address['current']) && $address['current']) {
63 $current++;
64 }
65 }
66 if ($current == 0 && count($value) > 0) {
67 foreach ($value as &$address) {
68 $address['current'] = true;
69 break;
70 }
71 } elseif ($current > 1) {
72 $success = false;
73 }
74 foreach ($value as $key => &$address) {
75 if (!trim($address['text'])) {
76 unset($value[$key]);
77 } elseif (!$init) {
78 $this->geocodeAddress($address, $s);
79 $success = $success && $s;
80 }
81 $this->cleanAddress($page, $key, $address);
82 }
83 return $value;
84 }
85
86 public function saveAddress($pid, $addrid, array &$address, $type)
87 {
88 require_once 'geocoding.inc.php';
89
90 $flags = new PlFlagSet();
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'] =
96 (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
97 array("", "\n"), $address['text'])), 'CEDEX')) !== false);
98 Geocoder::getAreaId($address, "administrativeArea");
99 Geocoder::getAreaId($address, "subAdministrativeArea");
100 Geocoder::getAreaId($address, "locality");
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)
106 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
107 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
108 $pid, $type, $addrid, $flags, $address['accuracy'],
109 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
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']);
114 }
115
116 public function save(ProfilePage &$page, $field, $value)
117 {
118 XDB::execute("DELETE FROM profile_addresses
119 WHERE pid = {?} AND type = 'home'",
120 $page->pid());
121 XDB::execute("DELETE FROM profile_phones
122 WHERE pid = {?} AND link_type = 'address'",
123 $page->pid());
124 foreach ($value as $addrid => &$address) {
125 $this->saveAddress($page->pid(), $addrid, $address, 'home');
126 $profiletel = new ProfileSettingPhones('address', $addrid);
127 $profiletel->saveTels($page->pid(), 'tel', $address['tel']);
128 }
129 }
130
131 public function getText($value) {
132 $addresses = array();
133 foreach ($value as $addrid => $address) {
134 $phones = new ProfileSettingPhones('address', $addrid);
135 $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub']
136 . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ? 'oui' : 'non')
137 . ', temporaire : ' . ($address['temporary'] ? 'oui' : 'non') . ', secondaire : '
138 . ($address['secondary'] ? 'oui' : 'non') . ', conctactable par courier : '
139 . ($address['mail'] ? 'oui' : 'non') . ', ' . $phones->getText($address['tel']);
140 }
141 return implode(' ; ' , $addresses);
142 }
143 }
144
145 class ProfileSettingAddresses extends ProfilePage
146 {
147 protected $pg_template = 'profile/adresses.tpl';
148
149 public function __construct(PlWizard &$wiz)
150 {
151 parent::__construct($wiz);
152 $this->settings['addresses'] = new ProfileSettingAddress();
153 $this->watched['addresses'] = true;
154 }
155
156 protected function _fetchData()
157 {
158 $res = XDB::query("SELECT id, accuracy, text, postalText,
159 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
160 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
161 north, south, east, west,
162 FIND_IN_SET('current', flags) AS current,
163 FIND_IN_SET('temporary', flags) AS temporary,
164 FIND_IN_SET('secondary', flags) AS secondary,
165 FIND_IN_SET('mail', flags) AS mail,
166 FIND_IN_SET('cedex', flags) AS cedex
167 FROM profile_addresses
168 WHERE pid = {?} AND type = 'home'
169 ORDER BY id",
170 $this->pid());
171 if ($res->numRows() == 0) {
172 $this->values['addresses'] = array();
173 } else {
174 $this->values['addresses'] = $res->fetchAllAssoc();
175 }
176
177 $res = XDB::iterator("SELECT link_id AS addrid, tel_type AS type, pub, display_tel AS tel, comment
178 FROM profile_phones
179 WHERE pid = {?} AND link_type = 'address'
180 ORDER BY link_id",
181 $this->pid());
182 $i = 0;
183 $adrNb = count($this->values['addresses']);
184 while ($tel = $res->next()) {
185 $addrid = $tel['addrid'];
186 unset($tel['addrid']);
187 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $addrid) {
188 $i++;
189 }
190 if ($i >= $adrNb) {
191 break;
192 }
193 $address =& $this->values['addresses'][$i];
194 if (!isset($address['tel'])) {
195 $address['tel'] = array();
196 }
197 if ($address['id'] == $addrid) {
198 $address['tel'][] = $tel;
199 }
200 }
201 foreach ($this->values['addresses'] as $id => &$address) {
202 if (!isset($address['tel'])) {
203 $address['tel'] = array(
204 0 => array(
205 'type' => 'fixed',
206 'tel' => '',
207 'pub' => 'private',
208 'comment' => '',
209 )
210 );
211 }
212 unset($address['id']);
213 $address['changed'] = 0;
214 $address['removed'] = 0;
215 }
216 }
217 }
218
219 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
220 ?>