2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileAddress
extends ProfileGeoloc
28 public function __construct()
30 $this->bool
= new ProfileBool();
31 $this->pub
= new ProfilePub();
32 $this->tel
= new ProfileTel();
35 private function cleanAddress(ProfilePage
&$page, array &$address, &$success)
37 if (@$address['changed']) {
38 $address['datemaj'] = time();
41 foreach ($address['tel'] as $t=>&$tel) {
42 if (@$tel['removed'] ||
!trim($tel['tel'])) {
43 unset($address['tel'][$t]);
45 $tel['pub'] = $this->pub
->value($page, 'pub', $tel['pub'], $s);
46 $tel['tel'] = $this->tel
->value($page, 'tel', $tel['tel'], $s);
52 unset($tel['removed']);
54 $address['checked'] = $this->bool
->value($page, 'checked', $address['checked'], $s);
55 $address['secondaire'] = $this->bool
->value($page, 'secondaire', $address['secondaire'], $s);
56 $address['mail'] = $this->bool
->value($page, 'mail', $address['mail'], $s);
57 $address['temporary'] = $this->bool
->value($page, 'temporary', $address['temporary'], $s);
58 $address['current'] = $this->bool
->value($page, 'current', @$address['current'], $s);
59 $address['pub'] = $this->pub
->value($page, 'pub', $address['pub'], $s);
60 unset($address['parsevalid']);
61 unset($address['changed']);
62 unset($address['removed']);
63 unset($address['display']);
66 public function value(ProfilePage
&$page, $field, $value, &$success)
69 if (is_null($value)) {
70 $value = $page->values
['addresses'];
73 foreach ($value as $key=>&$adr) {
74 if (@$adr['removed']) {
80 foreach ($value as $key=>&$adr) {
81 if (@$adr['current']) {
85 if ($current == 0 && count($value) > 0) {
86 foreach ($value as $key=>&$adr) {
87 $adr['current'] = true
;
90 } else if ($current > 1) {
93 foreach ($value as $key=>&$adr) {
95 $this->geolocAddress($adr, $s);
97 $this->cleanAddress($page, $adr, $s);
99 if (!trim($adr['text'])) {
102 $success = ($success && $ls);
108 private function saveTel($adrid, $telid, array &$tel)
110 XDB
::execute("INSERT INTO tels (uid, adrid, telid,
111 tel_type, tel_pub, tel)
112 VALUES ({?}, {?}, {?},
114 S
::i('uid'), $adrid, $telid,
115 $tel['type'], $tel['pub'], $tel['tel']);
118 private function saveAddress($adrid, array &$address)
120 $flags = new PlFlagSet();
121 if ($address['secondaire']) {
122 $flags->addFlag('res-secondaire');
124 if ($address['mail']) {
125 $flags->addFlag('courrier');
127 if ($address['temporary']) {
128 $flags->addFlag('temporaire');
130 if ($address['current']) {
131 $flags->addFlag('active');
133 if ($address['checked']) {
134 $flags->addFlag('coord-checked');
136 XDB
::execute("INSERT INTO adresses (adr1, adr2, adr3,
137 postcode, city, cityid,
138 country, region, regiontxt,
139 pub, datemaj, statut,
140 uid, adrid, glat, glng)
141 VALUES ({?}, {?}, {?},
144 {?}, FROM_UNIXTIME({?}), {?},
145 {?}, {?}, {?}, {?})",
146 $address['adr1'], $address['adr2'], $address['adr3'],
147 $address['postcode'], $address['city'], $address['cityid'],
148 $address['country'], $address['region'], $address['regiontxt'],
149 $address['pub'], $address['datemaj'], $flags,
150 S
::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon']);
151 foreach ($address['tel'] as $telid=>&$tel) {
152 $this->saveTel($adrid, $telid, $tel);
156 public function save(ProfilePage
&$page, $field, $value)
158 XDB
::execute("DELETE FROM adresses
161 XDB
::execute("DELETE FROM tels
164 foreach ($value as $adrid=>&$address) {
165 $this->saveAddress($adrid, $address);
170 class ProfileAddresses
extends ProfilePage
172 protected $pg_template = 'profile/adresses.tpl';
174 public function __construct(PlWizard
&$wiz)
176 parent
::__construct($wiz);
177 $this->settings
['addresses'] = new ProfileAddress();
178 $this->watched
['addresses'] = true
;
181 protected function _fetchData()
183 // Build the addresses tree
184 $res = XDB
::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
185 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
186 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
187 a.pub, a.country, gp.pays AS countrytxt, gp.display,
188 FIND_IN_SET('coord-checked', a.statut) AS checked,
189 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
190 FIND_IN_SET('courrier', a.statut) AS mail,
191 FIND_IN_SET('temporaire', a.statut) AS temporary,
192 FIND_IN_SET('active', a.statut) AS current,
193 a.glat AS precise_lat, a.glng AS precise_lon
195 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
196 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
199 if ($res->numRows() == 0) {
200 $this->values
['addresses'] = array();
202 $this->values
['addresses'] = $res->fetchAllAssoc();
205 $res = XDB
::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
211 $adrNb = count($this->values
['addresses']);
212 while ($tel = $res->next()) {
213 $adrid = $tel['adrid'];
214 unset($tel['adrid']);
215 while ($i < $adrNb && $this->values
['addresses'][$i]['id'] < $adrid) {
221 $address =& $this->values
['addresses'][$i];
222 if (!isset($address['tel'])) {
223 $address['tel'] = array();
225 if ($address['id'] == $adrid) {
226 $address['tel'][] = $tel;
229 foreach ($this->values
['addresses'] as $id=>&$address) {
230 if (!isset($address['tel'])) {
231 $address['tel'] = array();
233 unset($address['id']);
238 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: