Integrates new phone table in profile and other pages
[platal.git] / modules / profile / addresses.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ProfileAddress extends ProfileGeoloc
23 {
24 private $bool;
25 private $pub;
26 private $tel;
27
28 public function __construct()
29 {
30 $this->bool = new ProfileBool();
31 $this->pub = new ProfilePub();
32 $this->tel = new ProfileTel();
33 }
34
35 private function cleanAddress(ProfilePage &$page, array &$address, &$success)
36 {
37 if (@$address['changed']) {
38 $address['datemaj'] = time();
39 }
40 $success = true;
41 foreach ($address['tel'] as $t=>&$tel) {
42 if (@$tel['removed'] || !trim($tel['tel'])) {
43 unset($address['tel'][$t]);
44 } else {
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';
49 $s = false;
50 }
51 if (!$s) {
52 $tel['error'] = true;
53 $success = false;
54 }
55 }
56 unset($tel['removed']);
57 }
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']);
68 }
69
70 public function value(ProfilePage &$page, $field, $value, &$success)
71 {
72 $init = false;
73 if (is_null($value)) {
74 $value = $page->values['addresses'];
75 $init = true;
76 }
77 foreach ($value as $key=>&$adr) {
78 if (@$adr['removed']) {
79 unset($value[$key]);
80 }
81 }
82 $current = 0;
83 $success = true;
84 foreach ($value as $key=>&$adr) {
85 if (@$adr['current']) {
86 $current++;
87 }
88 }
89 if ($current == 0 && count($value) > 0) {
90 foreach ($value as $key=>&$adr) {
91 $adr['current'] = true;
92 break;
93 }
94 } else if ($current > 1) {
95 $success = false;
96 }
97 foreach ($value as $key=>&$adr) {
98 $ls = true;
99 $this->geolocAddress($adr, $s);
100 $ls = ($ls && $s);
101 $this->cleanAddress($page, $adr, $s);
102 $ls = ($ls && $s);
103 if (!trim($adr['text'])) {
104 unset($value[$key]);
105 } else if (!$init) {
106 $success = ($success && $ls);
107 }
108 }
109 return $value;
110 }
111
112 private function saveTel($adrid, $telid, array &$tel)
113 {
114 XDB::execute("INSERT INTO telephone (uid, link_type, link_id, tel_id, tel_type,
115 search_tel, display_tel, pub)
116 VALUES ({?}, 'address', {?}, {?}, {?},
117 {?}, {?}, {?})",
118 S::i('uid'), $adrid, $telid, $tel['type'],
119 format_phone_number($tel['tel']), $tel['tel'], $tel['pub']);
120 }
121
122 private function saveAddress($adrid, array &$address)
123 {
124 $flags = array();
125 if ($address['secondaire']) {
126 $flags[] = 'res-secondaire';
127 }
128 if ($address['mail']) {
129 $flags[] = 'courrier';
130 }
131 if ($address['temporary']) {
132 $flags[] = 'temporaire';
133 }
134 if ($address['current']) {
135 $flags[] = 'active';
136 }
137 if ($address['checked']) {
138 $flags[] = 'coord-checked';
139 }
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 ({?}, {?}, {?},
147 {?}, {?}, {?},
148 {?}, {?}, {?},
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);
158 }
159 }
160
161 public function save(ProfilePage &$page, $field, $value)
162 {
163 XDB::execute("DELETE FROM adresses
164 WHERE uid = {?}",
165 S::i('uid'));
166 XDB::execute("DELETE FROM telephone
167 WHERE uid = {?} AND link_type = 'address'",
168 S::i('uid'));
169 foreach ($value as $adrid=>&$address) {
170 $this->saveAddress($adrid, $address);
171 }
172 }
173 }
174
175 class ProfileAddresses extends ProfilePage
176 {
177 protected $pg_template = 'profile/adresses.tpl';
178
179 public function __construct(PlWizard &$wiz)
180 {
181 parent::__construct($wiz);
182 $this->settings['addresses'] = new ProfileAddress();
183 $this->watched['addresses'] = true;
184 }
185
186 protected function _fetchData()
187 {
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,
199 a.comment
200 FROM adresses AS a
201 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
202 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
203 ORDER BY adrid",
204 S::i('uid'));
205 if ($res->numRows() == 0) {
206 $this->values['addresses'] = array();
207 } else {
208 $this->values['addresses'] = $res->fetchAllAssoc();
209 }
210
211 $res = XDB::iterator("SELECT link_id AS adrid, tel_type AS type, pub, display_tel AS tel
212 FROM telephone
213 WHERE uid = {?} AND link_type = 'address'
214 ORDER BY link_id",
215 S::i('uid'));
216 $i = 0;
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) {
222 $i++;
223 }
224 if ($i >= $adrNb) {
225 break;
226 }
227 $address =& $this->values['addresses'][$i];
228 if (!isset($address['tel'])) {
229 $address['tel'] = array();
230 }
231 if ($address['id'] == $adrid) {
232 $address['tel'][] = $tel;
233 }
234 }
235 foreach ($this->values['addresses'] as $id=>&$address) {
236 if (!isset($address['tel'])) {
237 $address['tel'] = array();
238 }
239 unset($address['id']);
240 }
241 }
242 }
243
244 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
245 ?>