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);
47 if(!isset($tel['type']) ||
($tel['type'] != 'fixed' && $tel['type'] != 'mobile' && $tel['type'] != 'fax')) {
48 $tel['type'] = 'fixed';
56 unset($tel['removed']);
58 $address['checked'] = $this->bool
->value($page, 'checked', $address['checked'], $s);
59 $address['secondaire'] = $this->bool
->value($page, 'secondaire', $address['secondaire'], $s);
60 $address['mail'] = $this->bool
->value($page, 'mail', $address['mail'], $s);
61 $address['temporary'] = $this->bool
->value($page, 'temporary', $address['temporary'], $s);
62 $address['current'] = $this->bool
->value($page, 'current', @$address['current'], $s);
63 $address['pub'] = $this->pub
->value($page, 'pub', $address['pub'], $s);
64 unset($address['parsevalid']);
65 unset($address['changed']);
66 unset($address['removed']);
67 unset($address['display']);
70 public function value(ProfilePage
&$page, $field, $value, &$success)
73 if (is_null($value)) {
74 $value = $page->values
['addresses'];
77 foreach ($value as $key=>&$adr) {
78 if (@$adr['removed']) {
84 foreach ($value as $key=>&$adr) {
85 if (@$adr['current']) {
89 if ($current == 0 && count($value) > 0) {
90 foreach ($value as $key=>&$adr) {
91 $adr['current'] = true
;
94 } else if ($current > 1) {
97 foreach ($value as $key=>&$adr) {
99 $this->geolocAddress($adr, $s);
101 $this->cleanAddress($page, $adr, $s);
103 if (!trim($adr['text'])) {
106 $success = ($success && $ls);
112 private function saveTel($adrid, $telid, array &$tel)
114 XDB
::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id, tel_type,
115 search_tel, display_tel, pub)
116 VALUES ({?}, 'address', {?}, {?}, {?},
118 S
::i('uid'), $adrid, $telid, $tel['type'],
119 format_phone_number($tel['tel']), $tel['tel'], $tel['pub']);
122 private function saveAddress($adrid, array &$address)
125 if ($address['secondaire']) {
126 $flags[] = 'res-secondaire';
128 if ($address['mail']) {
129 $flags[] = 'courrier';
131 if ($address['temporary']) {
132 $flags[] = 'temporaire';
134 if ($address['current']) {
137 if ($address['checked']) {
138 $flags[] = 'coord-checked';
140 $flags = implode(',', $flags);
141 XDB
::execute("INSERT INTO adresses (adr1, adr2, adr3,
142 postcode, city, cityid,
143 country, region, regiontxt,
144 pub, datemaj, statut,
145 uid, adrid, glat, glng, comment)
146 VALUES ({?}, {?}, {?},
149 {?}, FROM_UNIXTIME({?}), {?},
150 {?}, {?}, {?}, {?}, {?})",
151 $address['adr1'], $address['adr2'], $address['adr3'],
152 $address['postcode'], $address['city'], $address['cityid'],
153 $address['country'], $address['region'], $address['regiontxt'],
154 $address['pub'], $address['datemaj'], $flags,
155 S
::i('uid'), $adrid, $address['precise_lat'], $address['precise_lon'], $address['comment']);
156 foreach ($address['tel'] as $telid=>&$tel) {
157 $this->saveTel($adrid, $telid, $tel);
161 public function save(ProfilePage
&$page, $field, $value)
163 XDB
::execute("DELETE FROM adresses
166 XDB
::execute("DELETE FROM telephone
167 WHERE uid = {?} AND link_type = 'address'",
169 foreach ($value as $adrid=>&$address) {
170 $this->saveAddress($adrid, $address);
175 class ProfileAddresses
extends ProfilePage
177 protected $pg_template = 'profile/adresses.tpl';
179 public function __construct(PlWizard
&$wiz)
181 parent
::__construct($wiz);
182 $this->settings
['addresses'] = new ProfileAddress();
183 $this->watched
['addresses'] = true
;
186 protected function _fetchData()
188 // Build the addresses tree
189 $res = XDB
::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
190 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
191 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
192 a.pub, a.country, gp.pays AS countrytxt, gp.display,
193 FIND_IN_SET('coord-checked', a.statut) AS checked,
194 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
195 FIND_IN_SET('courrier', a.statut) AS mail,
196 FIND_IN_SET('temporaire', a.statut) AS temporary,
197 FIND_IN_SET('active', a.statut) AS current,
198 a.glat AS precise_lat, a.glng AS precise_lon,
201 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
202 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
205 if ($res->numRows() == 0) {
206 $this->values
['addresses'] = array();
208 $this->values
['addresses'] = $res->fetchAllAssoc();
211 $res = XDB
::iterator("SELECT link_id AS adrid, tel_type AS type, pub, display_tel AS tel
213 WHERE uid = {?} AND link_type = 'address'
217 $adrNb = count($this->values
['addresses']);
218 while ($tel = $res->next()) {
219 $adrid = $tel['adrid'];
220 unset($tel['adrid']);
221 while ($i < $adrNb && $this->values
['addresses'][$i]['id'] < $adrid) {
227 $address =& $this->values
['addresses'][$i];
228 if (!isset($address['tel'])) {
229 $address['tel'] = array();
231 if ($address['id'] == $adrid) {
232 $address['tel'][] = $tel;
235 foreach ($this->values
['addresses'] as $id=>&$address) {
236 if (!isset($address['tel'])) {
237 $address['tel'] = array();
239 unset($address['id']);
244 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: