Phone class: new class to access profile_phones.
[platal.git] / modules / profile / addresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 class ProfileSettingAddress extends ProfileSettingGeocoding
23 {
24 private $bool;
25 private $pub;
26
27 public function __construct()
28 {
29 $this->bool = new ProfileSettingBool();
30 $this->pub = new ProfileSettingPub();
31 }
32
33 private function cleanAddress(ProfilePage &$page, $addrid, array &$address)
34 {
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);
41 }
42
43 public function value(ProfilePage &$page, $field, $value, &$success)
44 {
45 $init = false;
46 if (is_null($value)) {
47 $value = $page->values['addresses'];
48 $init = true;
49 }
50 foreach ($value as $key => &$address) {
51 if (isset($address['removed']) && $address['removed']) {
52 array_splice($value, $key, 1);
53 }
54 }
55 $current = 0;
56 $success = true;
57 foreach ($value as $key => &$address) {
58 if (isset($address['current']) && $address['current']) {
59 $current++;
60 }
61 }
62 if ($current == 0 && count($value) > 0) {
63 foreach ($value as &$address) {
64 $address['current'] = true;
65 break;
66 }
67 } elseif ($current > 1) {
68 $success = false;
69 }
70 foreach ($value as $key => &$address) {
71 if (!trim($address['text'])) {
72 unset($value[$key]);
73 } elseif (!$init) {
74 $this->geocodeAddress($address, $s);
75 $success = $success && $s;
76 }
77 $this->cleanAddress($page, $key, $address);
78 }
79 return $value;
80 }
81
82 public function saveAddress($pid, $addrid, array &$address, $type)
83 {
84 require_once 'geocoding.inc.php';
85
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");
97 XDB::execute("INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
98 text, postalText, postalCode, localityId,
99 subAdministrativeAreaId, administrativeAreaId,
100 countryId, latitude, longitude, updateTime, pub, comment,
101 north, south, east, west)
102 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
103 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})",
104 $pid, $type, $addrid, $flags, $address['accuracy'],
105 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
106 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
107 $address['countryId'], $address['latitude'], $address['longitude'],
108 $address['updateTime'], $address['pub'], $address['comment'],
109 $address['north'], $address['south'], $address['east'], $address['west']);
110 }
111
112 public function save(ProfilePage &$page, $field, $value)
113 {
114 XDB::execute("DELETE FROM profile_addresses
115 WHERE pid = {?} AND type = 'home'",
116 $page->pid());
117 Phone::deletePhones($page->pid(), Phone::LINK_ADDRESS);
118 foreach ($value as $addrid => &$address) {
119 $this->saveAddress($page->pid(), $addrid, $address, 'home');
120 Phone::savePhones($address['tel'], $page->pid(), Phone::LINK_ADDRESS, $addrid);
121 }
122 }
123
124 public function getText($value) {
125 $addresses = array();
126 foreach ($value as $addrid => $address) {
127 $phones = Phone::formArrayToString($address['tel']);
128 $addresses[] = 'Adresse : ' . $address['text'] . ', affichage : ' . $address['pub']
129 . ', commentaire : ' . $address['comment'] . ', actuelle : ' . ($address['current'] ? 'oui' : 'non')
130 . ', temporaire : ' . ($address['temporary'] ? 'oui' : 'non') . ', secondaire : '
131 . ($address['secondary'] ? 'oui' : 'non') . ', conctactable par courier : '
132 . ($address['mail'] ? 'oui' : 'non') . ($phones ? ', ' . $phones : '');
133 }
134 return implode(' ; ' , $addresses);
135 }
136 }
137
138 class ProfileSettingAddresses extends ProfilePage
139 {
140 protected $pg_template = 'profile/adresses.tpl';
141
142 public function __construct(PlWizard &$wiz)
143 {
144 parent::__construct($wiz);
145 $this->settings['addresses'] = new ProfileSettingAddress();
146 $this->watched['addresses'] = true;
147 }
148
149 protected function _fetchData()
150 {
151 $res = XDB::query("SELECT id, accuracy, text, postalText,
152 postalCode, localityId, subAdministrativeAreaId, administrativeAreaId,
153 countryId, latitude, longitude, pub, comment, UNIX_TIMESTAMP(updateTime) AS updateTime,
154 north, south, east, west,
155 FIND_IN_SET('current', flags) AS current,
156 FIND_IN_SET('temporary', flags) AS temporary,
157 FIND_IN_SET('secondary', flags) AS secondary,
158 FIND_IN_SET('mail', flags) AS mail,
159 FIND_IN_SET('cedex', flags) AS cedex
160 FROM profile_addresses
161 WHERE pid = {?} AND type = 'home'
162 ORDER BY id",
163 $this->pid());
164 if ($res->numRows() == 0) {
165 $this->values['addresses'] = array();
166 } else {
167 $this->values['addresses'] = $res->fetchAllAssoc();
168 }
169
170 // Adds phones to addresses.
171 $it = Phone::iterate(array($this->pid()), array(Phone::LINK_ADDRESS));
172 while ($phone = $it->next()) {
173 $this->values['addresses'][$phone->linkId()]['tel'][$phone->id()] = $phone->toFormArray();
174 }
175
176 // Properly formats addresses.
177 foreach ($this->values['addresses'] as $id => &$address) {
178 $phone = new Phone();
179 if (!isset($address['tel'])) {
180 $address['tel'] = array(0 => $phone->toFormArray());
181 }
182 unset($address['id']);
183 $address['changed'] = 0;
184 $address['removed'] = 0;
185 }
186 //var_dump($this->values['addresses']['tel']);
187 }
188 }
189
190 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
191 ?>