1edfbc108f9c96f193c46da6570589c098775237
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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['secondaire'] = $this->bool
->value($page, 'secondaire', $address['secondaire'], $s);
55 $address['mail'] = $this->bool
->value($page, 'mail', $address['mail'], $s);
56 $address['temporary'] = $this->bool
->value($page, 'temporary', $address['temporary'], $s);
57 $address['current'] = $this->bool
->value($page, 'current', @$address['current'], $s);
58 $address['pub'] = $this->pub
->value($page, 'pub', $address['pub'], $s);
59 unset($address['parsevalid']);
60 unset($address['changed']);
61 unset($address['removed']);
62 unset($address['display']);
65 public function value(ProfilePage
&$page, $field, $value, &$success)
68 if (is_null($value)) {
69 $value = $page->values
['addresses'];
72 foreach ($value as $key=>&$adr) {
73 if (@$adr['removed']) {
78 foreach ($value as $key=>&$adr) {
79 if (@$adr['current']) {
83 if (!$init && $current != 1 && count($value) > 0) {
88 foreach ($value as $key=>&$adr) {
90 $this->geolocAddress($adr, $s);
92 $this->cleanAddress($page, $adr, $s);
94 if (!trim($adr['text'])) {
97 $success = ($success && $ls);
103 private function saveTel($adrid, $telid, array &$tel)
105 XDB
::execute("INSERT INTO tels (uid, adrid, telid,
106 tel_type, tel_pub, tel)
107 VALUES ({?}, {?}, {?},
109 S
::i('uid'), $adrid, $telid,
110 $tel['type'], $tel['pub'], $tel['tel']);
113 private function saveAddress($adrid, array &$address)
116 if ($address['secondaire']) {
117 $flags[] = 'res-secondaire';
119 if ($address['mail']) {
120 $flags[] = 'courrier';
122 if ($address['temporary']) {
123 $flags[] = 'temporaire';
125 if ($address['current']) {
128 $flags = implode(',', $flags);
129 XDB
::execute("INSERT INTO adresses (adr1, adr2, adr3,
130 postcode, city, cityid,
131 country, region, regiontxt,
132 pub, datemaj, statut,
134 VALUES ({?}, {?}, {?},
137 {?}, FROM_UNIXTIME({?}), {?},
139 $address['adr1'], $address['adr2'], $address['adr3'],
140 $address['postcode'], $address['city'], $address['cityid'],
141 $address['country'], $address['region'], $address['regiontxt'],
142 $address['pub'], $address['datemaj'], $flags,
143 S
::i('uid'), $adrid);
144 foreach ($address['tel'] as $telid=>&$tel) {
145 $this->saveTel($adrid, $telid, $tel);
149 public function save(ProfilePage
&$page, $field, $value)
151 XDB
::execute("DELETE FROM adresses
154 XDB
::execute("DELETE FROM tels
157 foreach ($value as $adrid=>&$address) {
158 $this->saveAddress($adrid, $address);
163 class ProfileAddresses
extends ProfilePage
165 protected $pg_template = 'profile/adresses.tpl';
167 public function __construct(PlWizard
&$wiz)
169 parent
::__construct($wiz);
170 $this->settings
['addresses'] = new ProfileAddress();
173 protected function fetchData()
175 if (count($this->orig
) > 0) {
176 $this->values
= $this->orig
;
179 // Build the addresses tree
180 $res = XDB
::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
181 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
182 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
183 a.pub, a.country, gp.pays AS countrytxt, gp.display,
184 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
185 FIND_IN_SET('courrier', a.statut) AS mail,
186 FIND_IN_SET('temporaire', a.statut) AS temporary,
187 FIND_IN_SET('active', a.statut) AS current
189 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
190 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
193 if ($res->numRows() == 0) {
194 $this->values
['addresses'] = array();
196 $this->values
['addresses'] = $res->fetchAllAssoc();
199 $res = XDB
::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
205 $adrNb = count($this->values
['addresses']);
206 while ($tel = $res->next()) {
207 $adrid = $tel['adrid'];
208 unset($tel['adrid']);
209 while ($i < $adrNb && $this->values
['addresses'][$i]['id'] < $adrid) {
215 $address =& $this->values
['addresses'][$i];
216 if (!isset($address['tel'])) {
217 $address['tel'] = array();
219 if ($address['id'] == $adrid) {
220 $address['tel'][] = $tel;
223 foreach ($this->values
['addresses'] as $id=>&$address) {
224 if (!isset($address['tel'])) {
225 $address['tel'] = array();
227 unset($address['id']);
233 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: