6fe6f75c374a41527e926735e7f5a2fa63af860c
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 ProfileSettingAddress
extends ProfileSettingGeocoding
27 public function __construct()
29 $this->bool
= new ProfileSettingBool();
30 $this->pub
= new ProfileSettingPub();
33 private function cleanAddress(ProfilePage
&$page, $addrid, array &$address)
35 $address['tel'] = Phone
::formatFormArray($address['tel'], $s);
36 $address['current'] = $this->bool
->value($page, 'current', $address['current'], $s);
37 $address['temporary'] = $this->bool
->value($page, 'temporary', $address['temporary'], $s);
38 $address['secondary'] = $this->bool
->value($page, 'secondary', $address['secondary'], $s);
39 $address['mail'] = $this->bool
->value($page, 'mail', $address['mail'], $s);
40 $address['pub'] = $this->pub
->value($page, 'pub', $address['pub'], $s);
43 public function value(ProfilePage
&$page, $field, $value, &$success)
46 if (is_null($value)) {
47 $value = $page->values
['addresses'];
50 foreach ($value as $key => &$address) {
51 if (isset($address['removed']) && $address['removed']) {
52 array_splice($value, $key, 1);
57 foreach ($value as $key => &$address) {
58 if (isset($address['current']) && $address['current']) {
62 if ($current == 0 && count($value) > 0) {
63 foreach ($value as &$address) {
64 $address['current'] = true
;
67 } elseif ($current > 1) {
70 foreach ($value as $key => &$address) {
71 if (!trim($address['text'])) {
74 $this->geocodeAddress($address, $s);
75 $success = $success && $s;
77 $this->cleanAddress($page, $key, $address);
82 public function saveAddress($pid, $addrid, array &$address, $type)
84 require_once 'geocoding.inc.php';
86 $flags = new PlFlagSet();
87 $flags->addFlag('current', $address['current']);
88 $flags->addFlag('temporary', $address['temporary']);
89 $flags->addFlag('secondary', $address['secondary']);
90 $flags->addFlag('mail', $address['mail']);
91 $flags->addFlag('cedex', $address['cedex'] =
92 (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
93 array("", "\n"), $address['text'])), 'CEDEX')) !== false
);
94 Geocoder
::getAreaId($address, "administrativeArea");
95 Geocoder
::getAreaId($address, "subAdministrativeArea");
96 Geocoder
::getAreaId($address, "locality");
98 // Cleanup foreign keys
99 $foreign_keys = array('localityId', 'subAdministrativeAreaId', 'administrativeAreaId', 'countryId');
100 foreach ($foreign_keys as $key) {
101 if ($address[$key] == '') {
102 $address[$key] = null
;
106 XDB
::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
107 text, postalText, postalCode, localityId,
108 subAdministrativeAreaId, administrativeAreaId,
109 countryId, latitude, longitude, updateTime, pub, comment,
110 north, south, east, west)
111 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
112 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
113 $pid, $type, $addrid, $flags, $address['accuracy'],
114 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
115 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
116 $address['countryId'], $address['latitude'], $address['longitude'],
117 $address['updateTime'], $address['pub'], $address['comment'],
118 $address['north'], $address['south'], $address['east'], $address['west']);
121 public function save(ProfilePage
&$page, $field, $value)
123 XDB
::execute("DELETE FROM profile_addresses
124 WHERE pid = {?} AND type = 'home'",
126 Phone
::deletePhones($page->pid(), Phone
::LINK_ADDRESS
);
127 foreach ($value as $addrid => &$address) {
128 $this->saveAddress($page->pid(), $addrid, $address, 'home');
129 Phone
::savePhones($address['tel'], $page->pid(), Phone
::LINK_ADDRESS
, $addrid);
133 public function getText($value) {
134 $addresses = array();
135 foreach ($value as $addrid => $address) {
136 $phones = Phone
::formArrayToString($address['tel']);
137 $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub']
138 . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ?
'oui' : 'non')
139 . ', temporaire : ' . ($address['temporary'] ?
'oui' : 'non') . ', secondaire : '
140 . ($address['secondary'] ?
'oui' : 'non') . ', conctactable par courier : '
141 . ($address['mail'] ?
'oui' : 'non') . ($phones ?
', ' . $phones : '');
143 return implode(' ; ' , $addresses);
147 class ProfileSettingAddresses
extends ProfilePage
149 protected $pg_template = 'profile/adresses.tpl';
151 public function __construct(PlWizard
&$wiz)
153 parent
::__construct($wiz);
154 $this->settings
['addresses'] = new ProfileSettingAddress();
155 $this->watched
['addresses'] = true
;
158 protected function _fetchData()
160 $res = XDB
::query("SELECT id, accuracy, text, postalText,
161 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
162 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
163 north, south, east, west,
164 FIND_IN_SET('current', flags) AS current,
165 FIND_IN_SET('temporary', flags) AS temporary,
166 FIND_IN_SET('secondary', flags) AS secondary,
167 FIND_IN_SET('mail', flags) AS mail,
168 FIND_IN_SET('cedex', flags) AS cedex
169 FROM profile_addresses
170 WHERE pid = {?} AND type = 'home'
173 if ($res->numRows() == 0) {
174 $this->values
['addresses'] = array();
176 $this->values
['addresses'] = $res->fetchAllAssoc();
179 // Adds phones to addresses.
180 $it = Phone
::iterate(array($this->pid()), array(Phone
::LINK_ADDRESS
));
181 while ($phone = $it->next()) {
182 $this->values
['addresses'][$phone->linkId()]['tel'][$phone->id()] = $phone->toFormArray();
185 // Properly formats addresses.
186 foreach ($this->values
['addresses'] as $id => &$address) {
187 $phone = new Phone();
188 if (!isset($address['tel'])) {
189 $address['tel'] = array(0 => $phone->toFormArray());
191 unset($address['id']);
192 $address['changed'] = 0;
193 $address['removed'] = 0;
195 //var_dump($this->values['addresses']['tel']);
199 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: