Nearly finishes foreign keys.
[platal.git] / modules / profile / addresses.inc.php
CommitLineData
0b14f91d
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
0b14f91d
FB
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
12bcf04b 22class ProfileSettingAddress extends ProfileSettingGeocoding
0b14f91d 23{
d5489b09
FB
24 private $bool;
25 private $pub;
26
27 public function __construct()
28 {
12bcf04b
RB
29 $this->bool = new ProfileSettingBool();
30 $this->pub = new ProfileSettingPub();
d5489b09
FB
31 }
32
041a5cec 33 private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
d5489b09 34 {
0b6c8b36 35 $address['tel'] = Phone::formatFormArray($address['tel'], $s);
041a5cec 36 $address['current'] = $this->bool->value($page, 'current', $address['current'], $s);
a619333a 37 $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
041a5cec
SJ
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);
d5489b09
FB
41 }
42
0b14f91d
FB
43 public function value(ProfilePage &$page, $field, $value, &$success)
44 {
7568a515 45 $init = false;
0b14f91d 46 if (is_null($value)) {
7568a515 47 $value = $page->values['addresses'];
041a5cec 48 $init = true;
0b14f91d 49 }
041a5cec
SJ
50 foreach ($value as $key => &$address) {
51 if (isset($address['removed']) && $address['removed']) {
ed01acac 52 array_splice($value, $key, 1);
0b14f91d
FB
53 }
54 }
9a1cb883 55 $current = 0;
2db7542d 56 $success = true;
041a5cec
SJ
57 foreach ($value as $key => &$address) {
58 if (isset($address['current']) && $address['current']) {
9a1cb883
FB
59 $current++;
60 }
61 }
2db7542d 62 if ($current == 0 && count($value) > 0) {
a08dc64f 63 foreach ($value as &$address) {
041a5cec 64 $address['current'] = true;
2db7542d
FB
65 break;
66 }
041a5cec 67 } elseif ($current > 1) {
9a1cb883 68 $success = false;
9a1cb883 69 }
041a5cec
SJ
70 foreach ($value as $key => &$address) {
71 if (!trim($address['text'])) {
c6a7beb2 72 unset($value[$key]);
041a5cec 73 } elseif (!$init) {
73f6c165 74 $this->geocodeAddress($address, $s);
041a5cec 75 $success = $success && $s;
c6a7beb2 76 }
041a5cec 77 $this->cleanAddress($page, $key, $address);
7568a515 78 }
0b14f91d
FB
79 return $value;
80 }
d5489b09 81
a6483c12 82 public function saveAddress($pid, $addrid, array &$address, $type)
d5489b09 83 {
a6483c12 84 require_once 'geocoding.inc.php';
041a5cec 85
113f6de8 86 $flags = new PlFlagSet();
5b2c9987
FB
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'] =
041a5cec 92 (strpos(strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"),
5b2c9987 93 array("", "\n"), $address['text'])), 'CEDEX')) !== false);
4c906759
SJ
94 Geocoder::getAreaId($address, "administrativeArea");
95 Geocoder::getAreaId($address, "subAdministrativeArea");
96 Geocoder::getAreaId($address, "locality");
8d1e0ab3 97
041a5cec
SJ
98 XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
99 text, postalText, postalCode, localityId,
100 subAdministrativeAreaId, administrativeAreaId,
101 countryId, latitude, longitude, updateTime, pub, comment,
102 north, south, east, west)
541e8d03 103 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
041a5cec 104 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
a6391000 105 $pid, $type, $addrid, $flags, $address['accuracy'],
5a10ab14 106 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
041a5cec
SJ
107 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
108 $address['countryId'], $address['latitude'], $address['longitude'],
109 $address['updateTime'], $address['pub'], $address['comment'],
110 $address['north'], $address['south'], $address['east'], $address['west']);
d5489b09
FB
111 }
112
113 public function save(ProfilePage &$page, $field, $value)
114 {
041a5cec
SJ
115 XDB::execute("DELETE FROM profile_addresses
116 WHERE pid = {?} AND type = 'home'",
e5bcd851 117 $page->pid());
0b6c8b36 118 Phone::deletePhones($page->pid(), Phone::LINK_ADDRESS);
041a5cec 119 foreach ($value as $addrid => &$address) {
a6391000 120 $this->saveAddress($page->pid(), $addrid, $address, 'home');
0b6c8b36 121 Phone::savePhones($address['tel'], $page->pid(), Phone::LINK_ADDRESS, $addrid);
d5489b09
FB
122 }
123 }
a0fce0c6
SJ
124
125 public function getText($value) {
126 $addresses = array();
127 foreach ($value as $addrid => $address) {
0b6c8b36 128 $phones = Phone::formArrayToString($address['tel']);
a0fce0c6
SJ
129 $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub']
130 . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ? 'oui' : 'non')
131 . ', temporaire : ' . ($address['temporary'] ? 'oui' : 'non') . ', secondaire : '
132 . ($address['secondary'] ? 'oui' : 'non') . ', conctactable par courier : '
0b6c8b36 133 . ($address['mail'] ? 'oui' : 'non') . ($phones ? ', ' . $phones : '');
a0fce0c6
SJ
134 }
135 return implode(' ; ' , $addresses);
136 }
0b14f91d
FB
137}
138
12bcf04b 139class ProfileSettingAddresses extends ProfilePage
0b14f91d
FB
140{
141 protected $pg_template = 'profile/adresses.tpl';
142
143 public function __construct(PlWizard &$wiz)
144 {
145 parent::__construct($wiz);
12bcf04b 146 $this->settings['addresses'] = new ProfileSettingAddress();
041a5cec 147 $this->watched['addresses'] = true;
0b14f91d
FB
148 }
149
7c2e0f0d 150 protected function _fetchData()
0b14f91d 151 {
541e8d03 152 $res = XDB::query("SELECT id, accuracy, text, postalText,
041a5cec 153 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
eecbf7f5 154 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
041a5cec
SJ
155 north, south, east, west,
156 FIND_IN_SET('current', flags) AS current,
157 FIND_IN_SET('temporary', flags) AS temporary,
158 FIND_IN_SET('secondary', flags) AS secondary,
159 FIND_IN_SET('mail', flags) AS mail,
160 FIND_IN_SET('cedex', flags) AS cedex
161 FROM profile_addresses
162 WHERE pid = {?} AND type = 'home'
163 ORDER BY id",
e5bcd851 164 $this->pid());
c24a06aa
FB
165 if ($res->numRows() == 0) {
166 $this->values['addresses'] = array();
167 } else {
168 $this->values['addresses'] = $res->fetchAllAssoc();
169 }
0b14f91d 170
0b6c8b36
SJ
171 // Adds phones to addresses.
172 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_ADDRESS));
173 while ($phone = $it->next()) {
174 $this->values['addresses'][$phone->linkId()]['tel'][$phone->id()] = $phone->toFormArray();
0b14f91d 175 }
0b6c8b36
SJ
176
177 // Properly formats addresses.
041a5cec 178 foreach ($this->values['addresses'] as $id => &$address) {
0b6c8b36 179 $phone = new Phone();
c24a06aa 180 if (!isset($address['tel'])) {
0b6c8b36 181 $address['tel'] = array(0 => $phone->toFormArray());
c24a06aa 182 }
7568a515 183 unset($address['id']);
7f5418fd
SJ
184 $address['changed'] = 0;
185 $address['removed'] = 0;
7568a515 186 }
0b6c8b36 187 //var_dump($this->values['addresses']['tel']);
0b14f91d 188 }
0b14f91d
FB
189}
190
191// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
192?>