Working HTML part of the job tab
[platal.git] / modules / profile / addresses.inc.php
CommitLineData
0b14f91d
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 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
d5489b09 22class ProfileAddress
0b14f91d 23{
d5489b09
FB
24 private $bool;
25 private $pub;
a619333a 26 private $tel;
d5489b09
FB
27
28 public function __construct()
29 {
30 $this->bool = new ProfileBool();
31 $this->pub = new ProfilePub();
a619333a 32 $this->tel = new ProfileTel();
d5489b09
FB
33 }
34
35 private function geolocAddress(array &$address, &$success)
7568a515
FB
36 {
37 require_once 'geoloc.inc.php';
c24a06aa 38 $success = true;
32346370
FB
39 unset($address['geoloc']);
40 unset($address['geoloc_cityid']);
7568a515
FB
41 if (@$address['parsevalid'] || (@$address['text'] && @$address['changed']) || !@$address['cityid']) {
42 $address = array_merge($address, empty_address());
43 $new = get_address_infos(@$address['text']);
44 if (compare_addresses_text(@$adress['text'], $geotxt = get_address_text($new))
45 || @$address['parsevalid']) {
46 $address = array_merge($address, $new);
47 } else {
48 $success = false;
49 $address = array_merge($address, cut_address(@$address['text']));
50 $address['geoloc'] = $geotxt;
51 $address['geoloc_cityid'] = $new['cityid'];
52 }
53 }
d5489b09
FB
54 if (@$address['changed']) {
55 $address['datemaj'] = time();
56 }
7568a515 57 $address['text'] = get_address_text($address);
7568a515
FB
58 }
59
a619333a 60 private function cleanAddress(ProfilePage &$page, array &$address, &$success)
d5489b09
FB
61 {
62 if (@$address['changed']) {
63 $address['datemaj'] = time();
64 }
a619333a 65 $success = true;
d5489b09 66 foreach ($address['tel'] as $t=>&$tel) {
c6a7beb2 67 if (@$tel['removed'] || !trim($tel['tel'])) {
d5489b09
FB
68 unset($address['tel'][$t]);
69 } else {
a619333a
FB
70 $tel['pub'] = $this->pub->value($page, 'pub', $tel['pub'], $s);
71 $tel['tel'] = $this->tel->value($page, 'tel', $tel['tel'], $s);
72 if (!$s) {
73 $tel['error'] = true;
74 $success = false;
75 }
d5489b09 76 }
c24a06aa 77 unset($tel['removed']);
d5489b09 78 }
c24a06aa
FB
79 if (@$address['changed']) {
80 $address['datemaj'] = time();
81 }
a619333a
FB
82 $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $s);
83 $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s);
84 $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
85 $address['current'] = $this->bool->value($page, 'current', @$address['current'], $s);
86 $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s);
c24a06aa
FB
87 unset($address['parsevalid']);
88 unset($address['changed']);
89 unset($address['removed']);
90 unset($address['display']);
d5489b09
FB
91 }
92
0b14f91d
FB
93 public function value(ProfilePage &$page, $field, $value, &$success)
94 {
7568a515 95 $init = false;
0b14f91d 96 if (is_null($value)) {
7568a515
FB
97 $value = $page->values['addresses'];
98 $init = true;
0b14f91d
FB
99 }
100 foreach ($value as $key=>&$adr) {
7568a515 101 if (@$adr['removed']) {
0b14f91d
FB
102 unset($value[$key]);
103 }
104 }
9a1cb883
FB
105 $current = 0;
106 foreach ($value as $key=>&$adr) {
107 if (@$adr['current']) {
108 $current++;
109 }
110 }
111 if (!$init && $current != 1 && count($value) > 0) {
112 $success = false;
113 } else {
114 $success = true;
115 }
7568a515 116 foreach ($value as $key=>&$adr) {
a619333a 117 $ls = true;
7568a515 118 $this->geolocAddress($adr, $s);
a619333a
FB
119 $ls = ($ls && $s);
120 $this->cleanAddress($page, $adr, $s);
121 $ls = ($ls && $s);
c6a7beb2
FB
122 if (!trim($adr['text'])) {
123 unset($value[$key]);
a619333a
FB
124 } else if (!$init) {
125 $success = ($success && $ls);
c6a7beb2 126 }
7568a515 127 }
0b14f91d
FB
128 return $value;
129 }
d5489b09
FB
130
131 private function saveTel($adrid, $telid, array &$tel)
132 {
133 XDB::execute("INSERT INTO tels (uid, adrid, telid,
134 tel_type, tel_pub, tel)
135 VALUES ({?}, {?}, {?},
136 {?}, {?}, {?})",
137 S::i('uid'), $adrid, $telid,
138 $tel['type'], $tel['pub'], $tel['tel']);
139 }
140
141 private function saveAddress($adrid, array &$address)
142 {
143 $flags = array();
144 if ($address['secondaire']) {
145 $flags[] = 'res-secondaire';
146 }
147 if ($address['mail']) {
148 $flags[] = 'courrier';
149 }
150 if ($address['temporary']) {
151 $flags[] = 'temporaire';
152 }
153 if ($address['current']) {
154 $flags[] = 'active';
155 }
156 $flags = implode(',', $flags);
c24a06aa 157 XDB::execute("INSERT INTO adresses (adr1, adr2, adr3,
d5489b09
FB
158 postcode, city, cityid,
159 country, region, regiontxt,
160 pub, datemaj, statut,
161 uid, adrid)
162 VALUES ({?}, {?}, {?},
163 {?}, {?}, {?},
164 {?}, {?}, {?},
165 {?}, FROM_UNIXTIME({?}), {?},
166 {?}, {?})",
167 $address['adr1'], $address['adr2'], $address['adr3'],
168 $address['postcode'], $address['city'], $address['cityid'],
169 $address['country'], $address['region'], $address['regiontxt'],
170 $address['pub'], $address['datemaj'], $flags,
171 S::i('uid'), $adrid);
172 foreach ($address['tel'] as $telid=>&$tel) {
173 $this->saveTel($adrid, $telid, $tel);
174 }
175 }
176
177 public function save(ProfilePage &$page, $field, $value)
178 {
179 XDB::execute("DELETE FROM adresses
180 WHERE uid = {?}",
181 S::i('uid'));
182 XDB::execute("DELETE FROM tels
183 WHERE uid = {?}",
184 S::i('uid'));
185 foreach ($value as $adrid=>&$address) {
186 $this->saveAddress($adrid, $address);
187 }
188 }
0b14f91d
FB
189}
190
191class ProfileAddresses extends ProfilePage
192{
193 protected $pg_template = 'profile/adresses.tpl';
194
195 public function __construct(PlWizard &$wiz)
196 {
197 parent::__construct($wiz);
198 $this->settings['addresses'] = new ProfileAddress();
199 }
200
201 protected function fetchData()
202 {
203 if (count($this->orig) > 0) {
204 $this->values = $this->orig;
205 return;
206 }
207 // Build the addresses tree
7568a515 208 $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
d5489b09 209 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
7568a515 210 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
d5489b09 211 a.pub, a.country, gp.pays AS countrytxt, gp.display,
7568a515
FB
212 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
213 FIND_IN_SET('courrier', a.statut) AS mail,
d5489b09 214 FIND_IN_SET('temporaire', a.statut) AS temporary,
7568a515
FB
215 FIND_IN_SET('active', a.statut) AS current
216 FROM adresses AS a
217 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
218 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
0b14f91d
FB
219 ORDER BY adrid",
220 S::i('uid'));
c24a06aa
FB
221 if ($res->numRows() == 0) {
222 $this->values['addresses'] = array();
223 } else {
224 $this->values['addresses'] = $res->fetchAllAssoc();
225 }
0b14f91d 226
7568a515
FB
227 $res = XDB::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
228 FROM tels
229 WHERE uid = {?}
230 ORDER BY adrid",
231 S::i('uid'));
0b14f91d 232 $i = 0;
c24a06aa 233 $adrNb = count($this->values['addresses']);
7568a515
FB
234 while ($tel = $res->next()) {
235 $adrid = $tel['adrid'];
236 unset($tel['adrid']);
c24a06aa 237 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
0b14f91d
FB
238 $i++;
239 }
c24a06aa
FB
240 if ($i >= $adrNb) {
241 break;
242 }
0b14f91d
FB
243 $address =& $this->values['addresses'][$i];
244 if (!isset($address['tel'])) {
245 $address['tel'] = array();
246 }
247 if ($address['id'] == $adrid) {
7568a515 248 $address['tel'][] = $tel;
0b14f91d
FB
249 }
250 }
7568a515 251 foreach ($this->values['addresses'] as $id=>&$address) {
c24a06aa
FB
252 if (!isset($address['tel'])) {
253 $address['tel'] = array();
254 }
7568a515
FB
255 unset($address['id']);
256 }
0b14f91d
FB
257 parent::fetchData();
258 }
0b14f91d
FB
259}
260
261// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
262?>