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