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
27 public function __construct()
29 $this->bool
= new ProfileBool();
30 $this->pub
= new ProfilePub();
33 private function cleanAddress(ProfilePage
&$page, $adrid, array &$address, &$success)
35 if (@$address['changed']) {
36 $address['datemaj'] = time();
39 if (!isset($address['tel'])) {
40 $address['tel'] = array();
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']);
56 public function value(ProfilePage
&$page, $field, $value, &$success)
59 if (is_null($value)) {
60 $value = $page->values
['addresses'];
63 foreach ($value as $key=>&$adr) {
64 if (@$adr['removed']) {
70 foreach ($value as $key=>&$adr) {
71 if (@$adr['current']) {
75 if ($current == 0 && count($value) > 0) {
76 foreach ($value as $key=>&$adr) {
77 $adr['current'] = true
;
80 } else if ($current > 1) {
83 foreach ($value as $key=>&$adr) {
85 $this->geolocAddress($adr, $s);
87 $this->cleanAddress($page, $key, $adr, $s);
89 if (!trim($adr['text'])) {
92 $success = ($success && $ls);
98 private function saveTel($adrid, $telid, array &$tel)
100 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
101 search_tel, display_tel, pub)
102 VALUES ({?}, 'address', {?}, {?}, {?},
104 S
::i('uid'), $adrid, $telid, $tel['type'],
105 format_phone_number($tel['tel']), $tel['tel'], $tel['pub']);
108 private function saveAddress($adrid, array &$address)
110 $flags = new PlFlagSet();
111 if ($address['secondaire']) {
112 $flags->addFlag('res-secondaire');
114 if ($address['mail']) {
115 $flags->addFlag('courrier');
117 if ($address['temporary']) {
118 $flags->addFlag('temporaire');
120 if ($address['current']) {
121 $flags->addFlag('active');
123 if ($address['checked']) {
124 $flags->addFlag('coord-checked');
126 XDB
::execute("INSERT INTO adresses (adr1, adr2, adr3,
127 postcode, city, cityid,
128 country, region, regiontxt,
129 pub, datemaj, statut,
130 uid, adrid, glat, glng, comment)
131 VALUES ({?}, {?}, {?},
134 {?}, FROM_UNIXTIME({?}), {?},
135 {?}, {?}, {?}, {?}, {?})",
136 $address['adr1'], $address['adr2'], $address['adr3'],
137 $address['postcode'], $address['city'], $address['cityid'],
138 $address['country'], $address['region'], $address['regiontxt'],
139 $address['pub'], $address['datemaj'], $flags,
140 S
::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon'], $address['comment']);
143 public function save(ProfilePage
&$page, $field, $value)
145 XDB
::execute("DELETE FROM adresses
148 XDB
::execute("DELETE FROM profile_phones
149 WHERE uid = {?} AND link_type = 'address'",
151 foreach ($value as $adrid=>&$address) {
152 $this->saveAddress($adrid, $address);
153 $profiletel = new ProfilePhones('address', $adrid);
154 $profiletel->saveTels('tel', $address['tel']);
159 class ProfileAddresses
extends ProfilePage
161 protected $pg_template = 'profile/adresses.tpl';
163 public function __construct(PlWizard
&$wiz)
165 parent
::__construct($wiz);
166 $this->settings
['addresses'] = new ProfileAddress();
167 $this->watched
['addresses'] = true
;
170 protected function _fetchData()
172 // Build the addresses tree
173 $res = XDB
::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
174 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
175 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
176 a.pub, a.country, gp.pays AS countrytxt, gp.display,
177 FIND_IN_SET('coord-checked', a.statut) AS checked,
178 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
179 FIND_IN_SET('courrier', a.statut) AS mail,
180 FIND_IN_SET('temporaire', a.statut) AS temporary,
181 FIND_IN_SET('active', a.statut) AS current,
182 a.glat AS precise_lat, a.glng AS precise_lon,
185 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
186 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
189 if ($res->numRows() == 0) {
190 $this->values
['addresses'] = array();
192 $this->values
['addresses'] = $res->fetchAllAssoc();
195 $res = XDB
::iterator("SELECT link_id AS adrid, tel_type AS type, pub, display_tel AS tel, comment
197 WHERE uid = {?} AND link_type = 'address'
201 $adrNb = count($this->values
['addresses']);
202 while ($tel = $res->next()) {
203 $adrid = $tel['adrid'];
204 unset($tel['adrid']);
205 while ($i < $adrNb && $this->values
['addresses'][$i]['id'] < $adrid) {
211 $address =& $this->values
['addresses'][$i];
212 if (!isset($address['tel'])) {
213 $address['tel'] = array();
215 if ($address['id'] == $adrid) {
216 $address['tel'][] = $tel;
219 foreach ($this->values
['addresses'] as $id=>&$address) {
220 if (!isset($address['tel'])) {
221 $address['tel'] = array();
223 unset($address['id']);
228 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: