Commit | Line | Data |
---|---|---|
f118f685 FB |
1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 Polytechnique.org * |
f118f685 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 | ||
fd38b30e FB |
22 | interface ProfileSetting |
23 | { | |
24 | /** Get a field and a value, check that the given value is | |
25 | * valid, if not, return a corrected value. If no valid value can be | |
26 | * computed from the input data, the success flag is set to false. | |
27 | * | |
28 | * If value is null, the default value should be returned. | |
29 | * TODO: check this does not conflict with some possible values. | |
30 | * | |
31 | * Whatever happen, this function must always returns the function to | |
32 | * show on the page to the user. | |
33 | */ | |
34 | public function value(ProfilePage &$page, $field, $value, &$success); | |
35 | ||
36 | /** Save the new value for the given field. | |
37 | */ | |
38 | public function save(ProfilePage &$page, $field, $new_value); | |
39 | } | |
40 | ||
41 | abstract class ProfileNoSave implements ProfileSetting | |
42 | { | |
43 | public function save(ProfilePage &$page, $field, $new_value) { } | |
44 | } | |
45 | ||
fd38b30e FB |
46 | class ProfileWeb extends ProfileNoSave |
47 | { | |
48 | public function value(ProfilePage &$page, $field, $value, &$success) | |
49 | { | |
50 | if (is_null($value)) { | |
51 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
52 | } | |
37d44b3b FB |
53 | $value = trim($value); |
54 | $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value); | |
fd38b30e | 55 | if (!$success) { |
d7610c35 FB |
56 | Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://' |
57 | . ' et ne pas contenir de caractères interdits'); | |
fd38b30e FB |
58 | } |
59 | return $value; | |
60 | } | |
61 | } | |
62 | ||
37d44b3b FB |
63 | class ProfileEmail extends ProfileNoSave |
64 | { | |
65 | public function value(ProfilePage &$page, $field, $value, &$success) | |
66 | { | |
67 | if (is_null($value)) { | |
68 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
69 | } | |
70 | $value = trim($value); | |
37d44b3b FB |
71 | $success = empty($value) || isvalid_email($value); |
72 | if (!$success) { | |
d7610c35 | 73 | Platal::page()->trigError('Adresse Email invalide'); |
37d44b3b FB |
74 | } |
75 | return $value; | |
76 | } | |
77 | } | |
78 | ||
92446a53 PC |
79 | class ProfileNumber extends ProfileNoSave |
80 | { | |
81 | public function value(ProfilePage &$page, $field, $value, &$success) | |
82 | { | |
83 | if (is_null($value)) { | |
84 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
85 | } | |
86 | $value = trim($value); | |
87 | $success = empty($value) || is_numeric($value); | |
88 | if (!$success) { | |
ad3fee9d | 89 | Platal::page()->trigError('Numéro invalide'); |
92446a53 PC |
90 | } |
91 | return $value; | |
92 | } | |
93 | } | |
94 | ||
37d44b3b | 95 | |
fd38b30e FB |
96 | class ProfileTel extends ProfileNoSave |
97 | { | |
98 | public function value(ProfilePage &$page, $field, $value, &$success) | |
99 | { | |
100 | if (is_null($value)) { | |
101 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
102 | } | |
5e4417a9 GB |
103 | require_once('profil.func.inc.php'); |
104 | $value = format_phone_number($value); | |
105 | if($value == '') { | |
106 | $success = true; | |
107 | return $value; | |
108 | } | |
109 | $value = format_display_number($value,$error); | |
110 | $success = !$error; | |
fd38b30e | 111 | if (!$success) { |
ad3fee9d | 112 | Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. '); |
fd38b30e FB |
113 | } |
114 | return $value; | |
115 | } | |
116 | } | |
117 | ||
bde2be3b GB |
118 | class ProfilePhones implements ProfileSetting |
119 | { | |
120 | private $tel; | |
121 | private $pub; | |
b814a8b8 | 122 | protected $id; |
bde2be3b GB |
123 | protected $link_type; |
124 | protected $link_id; | |
125 | ||
b814a8b8 | 126 | public function __construct($type, $link_id, $id = 0) |
bde2be3b | 127 | { |
b814a8b8 SJ |
128 | if ($id != 0) { |
129 | $this->id = $id; | |
130 | } else { | |
131 | $this->id = S::i('uid'); | |
132 | } | |
bde2be3b GB |
133 | $this->tel = new ProfileTel(); |
134 | $this->pub = new ProfilePub(); | |
135 | $this->link_type = $type; | |
b814a8b8 | 136 | $this->link_id = $link_id; |
bde2be3b GB |
137 | } |
138 | ||
139 | public function value(ProfilePage &$page, $field, $value, &$success) | |
140 | { | |
141 | $success = true; | |
c94023bb | 142 | if (is_null($value) || !is_array($value)) { |
bde2be3b | 143 | $value = array(); |
b814a8b8 SJ |
144 | $res = XDB::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment |
145 | FROM profile_phones AS t | |
146 | WHERE t.uid = {?} AND t.link_type = {?} | |
147 | ORDER BY t.tel_id", | |
148 | $this->id, $this->link_type); | |
c94023bb | 149 | $value = $res->fetchAllAssoc(); |
bde2be3b GB |
150 | } |
151 | foreach ($value as $key=>&$phone) { | |
152 | if (@$phone['removed']) { | |
153 | unset($value[$key]); | |
154 | } else { | |
46cd7788 | 155 | unset($phone['removed']); |
bde2be3b GB |
156 | $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s); |
157 | $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s); | |
158 | if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) { | |
159 | $phone['type'] = 'fixed'; | |
160 | $s = false; | |
161 | } | |
162 | if (!$s) { | |
163 | $phone['error'] = true; | |
164 | $success = false; | |
165 | } | |
166 | if (!isset($phone['comment'])) { | |
167 | $phone['comment'] = ''; | |
168 | } | |
169 | } | |
170 | } | |
171 | return $value; | |
172 | } | |
173 | ||
174 | private function saveTel($telid, array &$phone) | |
175 | { | |
176 | if ($phone['tel'] != '') { | |
177 | XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type, | |
178 | search_tel, display_tel, pub, comment) | |
179 | VALUES ({?}, {?}, {?}, {?}, {?}, | |
180 | {?}, {?}, {?}, {?})", | |
b814a8b8 SJ |
181 | $this->id, $this->link_type, $this->link_id, $telid, $phone['type'], |
182 | format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']); | |
bde2be3b GB |
183 | } |
184 | } | |
185 | ||
186 | public function save(ProfilePage &$page, $field, $value) | |
187 | { | |
188 | XDB::execute("DELETE FROM profile_phones | |
189 | WHERE uid = {?} AND link_type = {?} AND link_id = {?}", | |
b814a8b8 | 190 | $this->id, $this->link_type, $this->link_id); |
bde2be3b GB |
191 | $this->saveTels($field, $value); |
192 | } | |
193 | ||
194 | //Only saves phones without a delete operation | |
195 | public function saveTels($field, $value) | |
196 | { | |
197 | foreach ($value as $telid=>&$phone) { | |
198 | $this->saveTel($telid, $phone); | |
199 | } | |
200 | } | |
201 | } | |
202 | ||
93553cea FB |
203 | class ProfilePub extends ProfileNoSave |
204 | { | |
205 | public function value(ProfilePage &$page, $field, $value, &$success) | |
206 | { | |
207 | $success = true; | |
208 | if (is_null($value)) { | |
209 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
210 | } | |
211 | if (is_null($value) || !$value) { | |
212 | $value = 'private'; | |
213 | } else if ($value == 'on') { // Checkbox | |
214 | $value = 'public'; | |
215 | } | |
216 | return $value; | |
217 | } | |
218 | } | |
219 | ||
576777d7 FB |
220 | class ProfileBool extends ProfileNoSave |
221 | { | |
222 | public function value(ProfilePage &$page, $field, $value, &$success) | |
223 | { | |
224 | $success = true; | |
225 | if (is_null($value)) { | |
c24a06aa | 226 | $value = @$page->values[$field]; |
576777d7 | 227 | } |
ee12da4e | 228 | return $value ? "1" : ""; |
576777d7 FB |
229 | } |
230 | } | |
231 | ||
7bff4cb0 FB |
232 | class ProfileDate extends ProfileNoSave |
233 | { | |
234 | public function value(ProfilePage &$page, $field, $value, &$success) | |
235 | { | |
236 | $success = true; | |
237 | if (is_null($value)) { | |
238 | $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]); | |
239 | } else { | |
240 | $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches); | |
241 | if (!$success) { | |
d7610c35 | 242 | Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa"); |
7bff4cb0 FB |
243 | } else { |
244 | $day = (int)$matches[1]; | |
245 | $month = (int)$matches[2]; | |
246 | $year = (int)$matches[3]; | |
247 | $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020); | |
248 | if (!$success) { | |
d7610c35 | 249 | Platal::page()->trigError("La date n'a pas une valeur valide"); |
7bff4cb0 FB |
250 | } |
251 | } | |
252 | } | |
253 | return $value; | |
254 | } | |
255 | } | |
256 | ||
37d44b3b FB |
257 | abstract class ProfileGeoloc implements ProfileSetting |
258 | { | |
259 | protected function geolocAddress(array &$address, &$success) | |
260 | { | |
261 | require_once 'geoloc.inc.php'; | |
262 | $success = true; | |
263 | unset($address['geoloc']); | |
264 | unset($address['geoloc_cityid']); | |
265 | if (@$address['parsevalid'] | |
266 | || (@$address['text'] && @$address['changed']) | |
267 | || (@$address['text'] && !@$address['cityid'])) { | |
268 | $address = array_merge($address, empty_address()); | |
269 | $new = get_address_infos(@$address['text']); | |
1879c7b2 FB |
270 | if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new)) |
271 | || (@$address['parsevalid'] && @$address['cityid'])) { | |
37d44b3b | 272 | $address = array_merge($address, $new); |
f93fb300 | 273 | $address['checked'] = true; |
1879c7b2 FB |
274 | } else if (@$address['parsevalid']) { |
275 | $address = array_merge($address, cut_address(@$address['text'])); | |
f93fb300 | 276 | $address['checked'] = true; |
b71f7275 | 277 | $mailer = new PlMailer('geoloc/geoloc.mail.tpl'); |
f93fb300 FB |
278 | $mailer->assign('text', get_address_text($address)); |
279 | $mailer->assign('geoloc', $geotxt); | |
280 | $mailer->send(); | |
281 | } else if (@$address['changed'] || !@$address['checked']) { | |
37d44b3b FB |
282 | $success = false; |
283 | $address = array_merge($address, cut_address(@$address['text'])); | |
f93fb300 | 284 | $address['checked'] = false; |
37d44b3b FB |
285 | $address['geoloc'] = $geotxt; |
286 | $address['geoloc_cityid'] = $new['cityid']; | |
f93fb300 FB |
287 | } else { |
288 | $address = array_merge($address, cut_address(@$address['text'])); | |
289 | $address['checked'] = true; | |
37d44b3b FB |
290 | } |
291 | } | |
de0485fd FB |
292 | $address['precise_lat'] = rtrim($address['precise_lat'], '.0'); |
293 | $address['precise_lon'] = rtrim($address['precise_lon'], '.0'); | |
37d44b3b FB |
294 | $address['text'] = get_address_text($address); |
295 | } | |
296 | } | |
297 | ||
298 | ||
fd38b30e | 299 | abstract class ProfilePage implements PlWizardPage |
f118f685 FB |
300 | { |
301 | protected $wizard; | |
fd38b30e FB |
302 | protected $pg_template; |
303 | protected $settings = array(); // A set ProfileSetting objects | |
93553cea | 304 | protected $errors = array(); // A set of boolean with the value check errors |
576777d7 | 305 | protected $changed = array(); // A set of boolean indicating wether the value has been changed |
a2a1c2f2 | 306 | protected $watched = array(); // A set of boolean indicating the fields that are watched |
fd38b30e | 307 | |
93553cea | 308 | public $orig = array(); |
fd38b30e | 309 | public $values = array(); |
f118f685 FB |
310 | |
311 | public function __construct(PlWizard &$wiz) | |
312 | { | |
313 | $this->wizard =& $wiz; | |
314 | } | |
315 | ||
7c2e0f0d FB |
316 | protected function _fetchData() |
317 | { | |
318 | } | |
319 | ||
fd38b30e FB |
320 | protected function fetchData() |
321 | { | |
93553cea FB |
322 | if (count($this->orig) > 0) { |
323 | $this->values = $this->orig; | |
324 | return; | |
325 | } | |
7c2e0f0d FB |
326 | |
327 | $this->_fetchData(); | |
93553cea FB |
328 | foreach ($this->settings as $field=>&$setting) { |
329 | $success = false; | |
330 | if (!is_null($setting)) { | |
331 | $this->values[$field] = $setting->value($this, $field, null, $success); | |
332 | } else if (!isset($this->values[$field])) { | |
333 | $this->values[$field] = S::v($field); | |
334 | } | |
335 | $this->errors[$field] = false; | |
336 | } | |
337 | $this->orig = $this->values; | |
fd38b30e FB |
338 | } |
339 | ||
7c2e0f0d FB |
340 | protected function _saveData() |
341 | { | |
342 | } | |
343 | ||
fd38b30e FB |
344 | protected function saveData() |
345 | { | |
a2a1c2f2 | 346 | require_once 'notifs.inc.php'; |
93553cea | 347 | foreach ($this->settings as $field=>&$setting) { |
576777d7 | 348 | if (!is_null($setting) && $this->changed[$field]) { |
93553cea FB |
349 | $setting->save($this, $field, $this->values[$field]); |
350 | } | |
a2a1c2f2 FB |
351 | if ($this->changed[$field] && @$this->watched[$field]) { |
352 | register_profile_update(S::i('uid'), $field); | |
353 | } | |
93553cea | 354 | } |
7c2e0f0d | 355 | $this->_saveData(); |
576777d7 FB |
356 | |
357 | // Update the last modification date | |
358 | XDB::execute('REPLACE INTO user_changes | |
359 | SET user_id = {?}', S::v('uid')); | |
3ebe4a0a | 360 | if (!S::has('suid')) { |
3ebe4a0a FB |
361 | register_watch_op(S::i('uid'), WATCH_FICHE); |
362 | } | |
576777d7 FB |
363 | global $platal; |
364 | $log =& $_SESSION['log']; | |
732e5855 | 365 | S::logger()->log('profil', $platal->pl_self(1)); |
93553cea FB |
366 | } |
367 | ||
368 | protected function checkChanges() | |
369 | { | |
370 | $newvalues = $this->values; | |
371 | $this->values = array(); | |
372 | $this->fetchData(); | |
373 | $this->values = $newvalues; | |
576777d7 | 374 | $changes = false; |
93553cea FB |
375 | foreach ($this->settings as $field=>&$setting) { |
376 | if ($this->orig[$field] != $this->values[$field]) { | |
576777d7 FB |
377 | $this->changed[$field] = true; |
378 | $changes = true; | |
379 | } else { | |
380 | $this->changed[$field] = false; | |
93553cea FB |
381 | } |
382 | } | |
576777d7 | 383 | return $changes; |
93553cea FB |
384 | } |
385 | ||
386 | protected function markChange() | |
387 | { | |
fd38b30e FB |
388 | } |
389 | ||
f118f685 FB |
390 | public function template() |
391 | { | |
fd38b30e | 392 | return 'profile/base.tpl'; |
f118f685 FB |
393 | } |
394 | ||
04334c61 | 395 | protected function _prepare(PlPage &$page, $id) |
7c2e0f0d FB |
396 | { |
397 | } | |
398 | ||
04334c61 | 399 | public function prepare(PlPage &$page, $id) |
f118f685 | 400 | { |
fd38b30e FB |
401 | if (count($this->values) == 0) { |
402 | $this->fetchData(); | |
fd38b30e FB |
403 | } |
404 | foreach ($this->values as $field=>&$value) { | |
405 | $page->assign($field, $value); | |
406 | } | |
7c2e0f0d | 407 | $this->_prepare($page, $id); |
fd38b30e | 408 | $page->assign('profile_page', $this->pg_template); |
93553cea | 409 | $page->assign('errors', $this->errors); |
f118f685 FB |
410 | } |
411 | ||
412 | public function process() | |
413 | { | |
fd38b30e FB |
414 | $global_success = true; |
415 | $this->fetchData(); | |
416 | foreach ($this->settings as $field=>&$setting) { | |
417 | $success = false; | |
93553cea | 418 | if (!is_null($setting)) { |
85cc366b | 419 | $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success); |
93553cea FB |
420 | } else { |
421 | $success = true; | |
85cc366b | 422 | $this->values[$field] = Post::v($field, ''); |
93553cea FB |
423 | } |
424 | $this->errors[$field] = !$success; | |
fd38b30e FB |
425 | $global_success = $global_success && $success; |
426 | } | |
427 | if ($global_success) { | |
93553cea FB |
428 | if ($this->checkChanges()) { |
429 | $this->saveData(); | |
430 | $this->markChange(); | |
fd38b30e | 431 | } |
93553cea | 432 | return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE; |
fd38b30e | 433 | } |
d7610c35 FB |
434 | Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations " |
435 | . "de ton profil et de revalider ta demande"); | |
f118f685 FB |
436 | return PlWizard::CURRENT_PAGE; |
437 | } | |
438 | } | |
439 | ||
fd38b30e | 440 | require_once dirname(__FILE__) . '/general.inc.php'; |
0b14f91d | 441 | require_once dirname(__FILE__) . '/addresses.inc.php'; |
92412b28 | 442 | require_once dirname(__FILE__) . '/groups.inc.php'; |
a7c28fff | 443 | require_once dirname(__FILE__) . '/decos.inc.php'; |
3950bc21 | 444 | require_once dirname(__FILE__) . '/jobs.inc.php'; |
f25e1a56 | 445 | require_once dirname(__FILE__) . '/skills.inc.php'; |
6457b5e4 | 446 | require_once dirname(__FILE__) . '/mentor.inc.php'; |
f118f685 FB |
447 | |
448 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
449 | ?> |