Job page should work
[platal.git] / modules / profile / addresses.inc.php
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
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 (!$s) {
48 $tel['error'] = true;
49 $success = false;
50 }
51 }
52 unset($tel['removed']);
53 }
54 $address['secondaire'] = $this->bool->value($page, 'secondaire', $address['secondaire'], $s);
55 $address['mail'] = $this->bool->value($page, 'mail', $address['mail'], $s);
56 $address['temporary'] = $this->bool->value($page, 'temporary', $address['temporary'], $s);
57 $address['current'] = $this->bool->value($page, 'current', @$address['current'], $s);
58 $address['pub'] = $this->pub->value($page, 'pub', $address['pub'], $s);
59 unset($address['parsevalid']);
60 unset($address['changed']);
61 unset($address['removed']);
62 unset($address['display']);
63 }
64
65 public function value(ProfilePage &$page, $field, $value, &$success)
66 {
67 $init = false;
68 if (is_null($value)) {
69 $value = $page->values['addresses'];
70 $init = true;
71 }
72 foreach ($value as $key=>&$adr) {
73 if (@$adr['removed']) {
74 unset($value[$key]);
75 }
76 }
77 $current = 0;
78 foreach ($value as $key=>&$adr) {
79 if (@$adr['current']) {
80 $current++;
81 }
82 }
83 if (!$init && $current != 1 && count($value) > 0) {
84 $success = false;
85 } else {
86 $success = true;
87 }
88 foreach ($value as $key=>&$adr) {
89 $ls = true;
90 $this->geolocAddress($adr, $s);
91 $ls = ($ls && $s);
92 $this->cleanAddress($page, $adr, $s);
93 $ls = ($ls && $s);
94 if (!trim($adr['text'])) {
95 unset($value[$key]);
96 } else if (!$init) {
97 $success = ($success && $ls);
98 }
99 }
100 return $value;
101 }
102
103 private function saveTel($adrid, $telid, array &$tel)
104 {
105 XDB::execute("INSERT INTO tels (uid, adrid, telid,
106 tel_type, tel_pub, tel)
107 VALUES ({?}, {?}, {?},
108 {?}, {?}, {?})",
109 S::i('uid'), $adrid, $telid,
110 $tel['type'], $tel['pub'], $tel['tel']);
111 }
112
113 private function saveAddress($adrid, array &$address)
114 {
115 $flags = array();
116 if ($address['secondaire']) {
117 $flags[] = 'res-secondaire';
118 }
119 if ($address['mail']) {
120 $flags[] = 'courrier';
121 }
122 if ($address['temporary']) {
123 $flags[] = 'temporaire';
124 }
125 if ($address['current']) {
126 $flags[] = 'active';
127 }
128 $flags = implode(',', $flags);
129 XDB::execute("INSERT INTO adresses (adr1, adr2, adr3,
130 postcode, city, cityid,
131 country, region, regiontxt,
132 pub, datemaj, statut,
133 uid, adrid)
134 VALUES ({?}, {?}, {?},
135 {?}, {?}, {?},
136 {?}, {?}, {?},
137 {?}, FROM_UNIXTIME({?}), {?},
138 {?}, {?})",
139 $address['adr1'], $address['adr2'], $address['adr3'],
140 $address['postcode'], $address['city'], $address['cityid'],
141 $address['country'], $address['region'], $address['regiontxt'],
142 $address['pub'], $address['datemaj'], $flags,
143 S::i('uid'), $adrid);
144 foreach ($address['tel'] as $telid=>&$tel) {
145 $this->saveTel($adrid, $telid, $tel);
146 }
147 }
148
149 public function save(ProfilePage &$page, $field, $value)
150 {
151 XDB::execute("DELETE FROM adresses
152 WHERE uid = {?}",
153 S::i('uid'));
154 XDB::execute("DELETE FROM tels
155 WHERE uid = {?}",
156 S::i('uid'));
157 foreach ($value as $adrid=>&$address) {
158 $this->saveAddress($adrid, $address);
159 }
160 }
161 }
162
163 class ProfileAddresses extends ProfilePage
164 {
165 protected $pg_template = 'profile/adresses.tpl';
166
167 public function __construct(PlWizard &$wiz)
168 {
169 parent::__construct($wiz);
170 $this->settings['addresses'] = new ProfileAddress();
171 }
172
173 protected function fetchData()
174 {
175 if (count($this->orig) > 0) {
176 $this->values = $this->orig;
177 return;
178 }
179 // Build the addresses tree
180 $res = XDB::query("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
181 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
182 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
183 a.pub, a.country, gp.pays AS countrytxt, gp.display,
184 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
185 FIND_IN_SET('courrier', a.statut) AS mail,
186 FIND_IN_SET('temporaire', a.statut) AS temporary,
187 FIND_IN_SET('active', a.statut) AS current
188 FROM adresses AS a
189 INNER JOIN geoloc_pays AS gp ON(gp.a2 = a.country)
190 WHERE uid = {?} AND NOT FIND_IN_SET('pro', statut)
191 ORDER BY adrid",
192 S::i('uid'));
193 if ($res->numRows() == 0) {
194 $this->values['addresses'] = array();
195 } else {
196 $this->values['addresses'] = $res->fetchAllAssoc();
197 }
198
199 $res = XDB::iterator("SELECT adrid, tel_type AS type, tel_pub AS pub, tel
200 FROM tels
201 WHERE uid = {?}
202 ORDER BY adrid",
203 S::i('uid'));
204 $i = 0;
205 $adrNb = count($this->values['addresses']);
206 while ($tel = $res->next()) {
207 $adrid = $tel['adrid'];
208 unset($tel['adrid']);
209 while ($i < $adrNb && $this->values['addresses'][$i]['id'] < $adrid) {
210 $i++;
211 }
212 if ($i >= $adrNb) {
213 break;
214 }
215 $address =& $this->values['addresses'][$i];
216 if (!isset($address['tel'])) {
217 $address['tel'] = array();
218 }
219 if ($address['id'] == $adrid) {
220 $address['tel'][] = $tel;
221 }
222 }
223 foreach ($this->values['addresses'] as $id=>&$address) {
224 if (!isset($address['tel'])) {
225 $address['tel'] = array();
226 }
227 unset($address['id']);
228 }
229 parent::fetchData();
230 }
231 }
232
233 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
234 ?>