Commit | Line | Data |
---|---|---|
f118f685 FB |
1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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); | |
a0fce0c6 SJ |
39 | |
40 | /** Get text from the value. | |
41 | */ | |
42 | public function getText($value); | |
fd38b30e FB |
43 | } |
44 | ||
45 | abstract class ProfileNoSave implements ProfileSetting | |
46 | { | |
47 | public function save(ProfilePage &$page, $field, $new_value) { } | |
a0fce0c6 SJ |
48 | |
49 | public function getText($value) { | |
50 | return $value; | |
51 | } | |
fd38b30e FB |
52 | } |
53 | ||
12bcf04b | 54 | class ProfileSettingWeb extends ProfileNoSave |
fd38b30e FB |
55 | { |
56 | public function value(ProfilePage &$page, $field, $value, &$success) | |
57 | { | |
58 | if (is_null($value)) { | |
59 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
60 | } | |
37d44b3b FB |
61 | $value = trim($value); |
62 | $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value); | |
fd38b30e | 63 | if (!$success) { |
d7610c35 FB |
64 | Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://' |
65 | . ' et ne pas contenir de caractères interdits'); | |
fd38b30e FB |
66 | } |
67 | return $value; | |
68 | } | |
69 | } | |
70 | ||
12bcf04b | 71 | class ProfileSettingEmail extends ProfileNoSave |
37d44b3b FB |
72 | { |
73 | public function value(ProfilePage &$page, $field, $value, &$success) | |
74 | { | |
75 | if (is_null($value)) { | |
76 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
77 | } | |
78 | $value = trim($value); | |
37d44b3b FB |
79 | $success = empty($value) || isvalid_email($value); |
80 | if (!$success) { | |
d7610c35 | 81 | Platal::page()->trigError('Adresse Email invalide'); |
37d44b3b FB |
82 | } |
83 | return $value; | |
84 | } | |
85 | } | |
86 | ||
12bcf04b | 87 | class ProfileSettingNumber extends ProfileNoSave |
92446a53 PC |
88 | { |
89 | public function value(ProfilePage &$page, $field, $value, &$success) | |
90 | { | |
91 | if (is_null($value)) { | |
92 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
93 | } | |
94 | $value = trim($value); | |
95 | $success = empty($value) || is_numeric($value); | |
96 | if (!$success) { | |
ad3fee9d | 97 | Platal::page()->trigError('Numéro invalide'); |
92446a53 PC |
98 | } |
99 | return $value; | |
100 | } | |
101 | } | |
102 | ||
37d44b3b | 103 | |
12bcf04b | 104 | class ProfileSettingTel extends ProfileNoSave |
fd38b30e FB |
105 | { |
106 | public function value(ProfilePage &$page, $field, $value, &$success) | |
107 | { | |
108 | if (is_null($value)) { | |
109 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
110 | } | |
5e4417a9 GB |
111 | require_once('profil.func.inc.php'); |
112 | $value = format_phone_number($value); | |
113 | if($value == '') { | |
114 | $success = true; | |
115 | return $value; | |
116 | } | |
117 | $value = format_display_number($value,$error); | |
118 | $success = !$error; | |
fd38b30e | 119 | if (!$success) { |
ad3fee9d | 120 | Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. '); |
fd38b30e FB |
121 | } |
122 | return $value; | |
123 | } | |
124 | } | |
125 | ||
12bcf04b | 126 | class ProfileSettingPhones implements ProfileSetting |
bde2be3b GB |
127 | { |
128 | private $tel; | |
129 | private $pub; | |
130 | protected $link_type; | |
131 | protected $link_id; | |
132 | ||
e5bcd851 | 133 | public function __construct($type, $link_id) |
bde2be3b | 134 | { |
12bcf04b RB |
135 | $this->tel = new ProfileSettingTel(); |
136 | $this->pub = new ProfileSettingPub(); | |
bde2be3b | 137 | $this->link_type = $type; |
b814a8b8 | 138 | $this->link_id = $link_id; |
bde2be3b GB |
139 | } |
140 | ||
141 | public function value(ProfilePage &$page, $field, $value, &$success) | |
142 | { | |
143 | $success = true; | |
3cd815ca | 144 | if (is_null($value)) { |
bde2be3b | 145 | $value = array(); |
9acf2251 SJ |
146 | $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment |
147 | FROM profile_phones | |
ce0b2c6f | 148 | WHERE pid = {?} AND link_type = {?} |
9acf2251 SJ |
149 | ORDER BY tel_id', |
150 | $page->pid(), $this->link_type); | |
9b0d289d SJ |
151 | if ($res->numRows() > 0) { |
152 | $value = $res->fetchAllAssoc(); | |
153 | } else { | |
154 | $value = array( | |
155 | 0 => array( | |
156 | 'type' => 'fixed', | |
157 | 'tel' => '', | |
158 | 'pub' => 'private', | |
159 | 'comment' => '', | |
160 | ) | |
161 | ); | |
162 | } | |
bde2be3b | 163 | } |
ed01acac | 164 | |
bde2be3b | 165 | foreach ($value as $key=>&$phone) { |
ed01acac SJ |
166 | $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s); |
167 | $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s); | |
168 | if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) { | |
169 | $phone['type'] = 'fixed'; | |
170 | $s = false; | |
171 | } | |
172 | if (!$s) { | |
173 | $phone['error'] = true; | |
174 | $success = false; | |
175 | } | |
176 | if (!isset($phone['comment'])) { | |
177 | $phone['comment'] = ''; | |
bde2be3b GB |
178 | } |
179 | } | |
ed01acac | 180 | |
bde2be3b GB |
181 | return $value; |
182 | } | |
183 | ||
e5bcd851 | 184 | private function saveTel($pid, $telid, array &$phone) |
bde2be3b GB |
185 | { |
186 | if ($phone['tel'] != '') { | |
ce0b2c6f | 187 | XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type, |
bde2be3b GB |
188 | search_tel, display_tel, pub, comment) |
189 | VALUES ({?}, {?}, {?}, {?}, {?}, | |
190 | {?}, {?}, {?}, {?})", | |
e5bcd851 | 191 | $pid, $this->link_type, $this->link_id, $telid, $phone['type'], |
b814a8b8 | 192 | format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']); |
bde2be3b GB |
193 | } |
194 | } | |
195 | ||
196 | public function save(ProfilePage &$page, $field, $value) | |
197 | { | |
198 | XDB::execute("DELETE FROM profile_phones | |
ce0b2c6f | 199 | WHERE pid = {?} AND link_type = {?} AND link_id = {?}", |
e5bcd851 FB |
200 | $page->pid(), $this->link_type, $this->link_id); |
201 | $this->saveTels($page->pid(), $field, $value); | |
bde2be3b GB |
202 | } |
203 | ||
204 | //Only saves phones without a delete operation | |
e5bcd851 | 205 | public function saveTels($pid, $field, $value) |
bde2be3b GB |
206 | { |
207 | foreach ($value as $telid=>&$phone) { | |
e5bcd851 | 208 | $this->saveTel($pid, $telid, $phone); |
bde2be3b GB |
209 | } |
210 | } | |
a0fce0c6 SJ |
211 | |
212 | public function getText($value) { | |
213 | $phones = array(); | |
214 | foreach ($value as $phone) { | |
215 | if ($phone['tel'] != '') { | |
216 | $phones[] = 'type : ' . $phone['type'] .', numéro : ' . $phone['tel'] | |
217 | . ', commentaire : « ' . $phone['comment'] . ' », affichage : ' . $phone['pub']; | |
218 | } | |
219 | } | |
220 | return implode(' ; ' , $phones); | |
221 | } | |
bde2be3b GB |
222 | } |
223 | ||
12bcf04b | 224 | class ProfileSettingPub extends ProfileNoSave |
93553cea FB |
225 | { |
226 | public function value(ProfilePage &$page, $field, $value, &$success) | |
227 | { | |
228 | $success = true; | |
229 | if (is_null($value)) { | |
230 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
231 | } | |
041a5cec | 232 | if (!$value) { |
93553cea | 233 | $value = 'private'; |
041a5cec | 234 | } elseif ($value == 'on') { // Checkbox |
93553cea FB |
235 | $value = 'public'; |
236 | } | |
237 | return $value; | |
238 | } | |
a0fce0c6 SJ |
239 | |
240 | public function getText($value) { | |
241 | return $value; | |
242 | } | |
93553cea FB |
243 | } |
244 | ||
12bcf04b | 245 | class ProfileSettingBool extends ProfileNoSave |
576777d7 FB |
246 | { |
247 | public function value(ProfilePage &$page, $field, $value, &$success) | |
248 | { | |
249 | $success = true; | |
250 | if (is_null($value)) { | |
041a5cec | 251 | $value = isset($page->values[$field]) ? $page->values[$field] : null; |
576777d7 | 252 | } |
ee12da4e | 253 | return $value ? "1" : ""; |
576777d7 FB |
254 | } |
255 | } | |
256 | ||
12bcf04b | 257 | class ProfileSettingDate extends ProfileNoSave |
7bff4cb0 FB |
258 | { |
259 | public function value(ProfilePage &$page, $field, $value, &$success) | |
260 | { | |
261 | $success = true; | |
262 | if (is_null($value)) { | |
263 | $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]); | |
264 | } else { | |
265 | $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches); | |
266 | if (!$success) { | |
d7610c35 | 267 | Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa"); |
7bff4cb0 FB |
268 | } else { |
269 | $day = (int)$matches[1]; | |
270 | $month = (int)$matches[2]; | |
271 | $year = (int)$matches[3]; | |
272 | $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020); | |
273 | if (!$success) { | |
d7610c35 | 274 | Platal::page()->trigError("La date n'a pas une valeur valide"); |
7bff4cb0 FB |
275 | } |
276 | } | |
277 | } | |
278 | return $value; | |
279 | } | |
280 | } | |
281 | ||
12bcf04b | 282 | abstract class ProfileSettingGeocoding implements ProfileSetting |
37d44b3b | 283 | { |
73f6c165 | 284 | protected function geocodeAddress(array &$address, &$success) |
37d44b3b | 285 | { |
4c906759 | 286 | require_once 'geocoding.inc.php'; |
37d44b3b | 287 | $success = true; |
afaa2cc7 | 288 | if (isset($address['changed']) && $address['changed'] == 1) { |
4c906759 SJ |
289 | $gmapsGeocoder = new GMapsGeocoder(); |
290 | $address = $gmapsGeocoder->getGeocodedAddress($address); | |
041a5cec | 291 | if (isset($address['geoloc'])) { |
37d44b3b | 292 | $success = false; |
37d44b3b | 293 | } |
4279d3bc SJ |
294 | } elseif (@$address['changed'] && !@$address['text']) { |
295 | $address = empty_address(); | |
296 | $address['pub'] = 'private'; | |
041a5cec | 297 | } |
42568423 SJ |
298 | if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) { |
299 | $mailer = new PlMailer('geoloc/geoloc.mail.tpl'); | |
300 | $mailer->assign('text', $address['text']); | |
301 | $mailer->assign('geoloc', $address['geoloc']); | |
302 | $mailer->send(); | |
73f6c165 SJ |
303 | $gmapsGeocoder = new GMapsGeocoder(); |
304 | $address = $gmapsGeocoder->stripGeocodingFromAddress($address); | |
37d44b3b | 305 | } |
37d44b3b | 306 | } |
a0fce0c6 SJ |
307 | |
308 | public function getText($value) { | |
309 | return $value; | |
310 | } | |
37d44b3b FB |
311 | } |
312 | ||
313 | ||
fd38b30e | 314 | abstract class ProfilePage implements PlWizardPage |
f118f685 FB |
315 | { |
316 | protected $wizard; | |
fd38b30e FB |
317 | protected $pg_template; |
318 | protected $settings = array(); // A set ProfileSetting objects | |
93553cea | 319 | protected $errors = array(); // A set of boolean with the value check errors |
576777d7 | 320 | protected $changed = array(); // A set of boolean indicating wether the value has been changed |
a2a1c2f2 | 321 | protected $watched = array(); // A set of boolean indicating the fields that are watched |
fd38b30e | 322 | |
93553cea | 323 | public $orig = array(); |
fd38b30e | 324 | public $values = array(); |
e5bcd851 FB |
325 | public $profile = null; |
326 | public $owner = null; | |
f118f685 FB |
327 | |
328 | public function __construct(PlWizard &$wiz) | |
329 | { | |
330 | $this->wizard =& $wiz; | |
e5bcd851 FB |
331 | $this->profile = $this->wizard->getUserData('profile'); |
332 | $this->owner = $this->wizard->getUserData('owner'); | |
f118f685 FB |
333 | } |
334 | ||
7c2e0f0d FB |
335 | protected function _fetchData() |
336 | { | |
337 | } | |
338 | ||
fd38b30e FB |
339 | protected function fetchData() |
340 | { | |
93553cea FB |
341 | if (count($this->orig) > 0) { |
342 | $this->values = $this->orig; | |
343 | return; | |
344 | } | |
7c2e0f0d FB |
345 | |
346 | $this->_fetchData(); | |
93553cea FB |
347 | foreach ($this->settings as $field=>&$setting) { |
348 | $success = false; | |
349 | if (!is_null($setting)) { | |
350 | $this->values[$field] = $setting->value($this, $field, null, $success); | |
351 | } else if (!isset($this->values[$field])) { | |
352 | $this->values[$field] = S::v($field); | |
353 | } | |
354 | $this->errors[$field] = false; | |
355 | } | |
356 | $this->orig = $this->values; | |
fd38b30e FB |
357 | } |
358 | ||
7c2e0f0d FB |
359 | protected function _saveData() |
360 | { | |
361 | } | |
362 | ||
fd38b30e FB |
363 | protected function saveData() |
364 | { | |
a2a1c2f2 | 365 | require_once 'notifs.inc.php'; |
a0fce0c6 | 366 | $changedFields = array(); |
93553cea | 367 | foreach ($this->settings as $field=>&$setting) { |
a0fce0c6 SJ |
368 | if ($this->changed[$field]) { |
369 | if (!is_null($setting)) { | |
370 | $changedFields[$field] = array( | |
371 | str_replace("\n", " - ", $setting->getText($this->orig[$field])), | |
372 | str_replace("\n", " - ", $setting->getText($this->values[$field])), | |
373 | ); | |
374 | } else { | |
375 | $changedFields[$field] = array( | |
376 | str_replace("\n", " - ", $this->orig[$field]), | |
377 | str_replace("\n", " - ", $this->values[$field]), | |
378 | ); | |
379 | } | |
380 | if (!is_null($setting)) { | |
381 | $setting->save($this, $field, $this->values[$field]); | |
382 | } | |
383 | if (isset($this->watched[$field]) && $this->watched[$field]) { | |
384 | WatchProfileUpdate::register($this->profile, $field); | |
385 | } | |
a2a1c2f2 | 386 | } |
93553cea | 387 | } |
7c2e0f0d | 388 | $this->_saveData(); |
576777d7 FB |
389 | |
390 | // Update the last modification date | |
4e7bf1e0 FB |
391 | XDB::execute('UPDATE profiles |
392 | SET last_change = NOW() | |
393 | WHERE pid = {?}', $this->pid()); | |
576777d7 | 394 | global $platal; |
e5bcd851 | 395 | S::logger()->log('profil', $platal->pl_self(2)); |
a0fce0c6 SJ |
396 | |
397 | /** If the update was made by a third party and the profile corresponds | |
398 | * to a registered user, stores both former and new text. | |
399 | * This will be daily sent to the user. | |
400 | */ | |
401 | $owner = $this->profile->owner(); | |
402 | $user = S::user(); | |
403 | if ($owner->isActive() && $owner->id() != $user->id()) { | |
404 | foreach ($changedFields as $field => $values) { | |
405 | XDB::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText) | |
406 | VALUES ({?}, {?}, {?}, {?}, {?})', | |
407 | $this->pid(), $user->id(), $field, $values[0], $values[1]); | |
408 | } | |
409 | } | |
93553cea FB |
410 | } |
411 | ||
412 | protected function checkChanges() | |
413 | { | |
414 | $newvalues = $this->values; | |
415 | $this->values = array(); | |
416 | $this->fetchData(); | |
417 | $this->values = $newvalues; | |
576777d7 | 418 | $changes = false; |
93553cea FB |
419 | foreach ($this->settings as $field=>&$setting) { |
420 | if ($this->orig[$field] != $this->values[$field]) { | |
576777d7 FB |
421 | $this->changed[$field] = true; |
422 | $changes = true; | |
423 | } else { | |
424 | $this->changed[$field] = false; | |
93553cea FB |
425 | } |
426 | } | |
576777d7 | 427 | return $changes; |
93553cea FB |
428 | } |
429 | ||
430 | protected function markChange() | |
431 | { | |
fd38b30e FB |
432 | } |
433 | ||
f118f685 FB |
434 | public function template() |
435 | { | |
fd38b30e | 436 | return 'profile/base.tpl'; |
f118f685 FB |
437 | } |
438 | ||
e5bcd851 FB |
439 | public function pid() |
440 | { | |
441 | return $this->profile->id(); | |
442 | } | |
443 | ||
444 | public function hrpid() | |
445 | { | |
446 | return $this->profile->hrpid(); | |
447 | } | |
448 | ||
04334c61 | 449 | protected function _prepare(PlPage &$page, $id) |
7c2e0f0d FB |
450 | { |
451 | } | |
452 | ||
04334c61 | 453 | public function prepare(PlPage &$page, $id) |
f118f685 | 454 | { |
fd38b30e FB |
455 | if (count($this->values) == 0) { |
456 | $this->fetchData(); | |
fd38b30e FB |
457 | } |
458 | foreach ($this->values as $field=>&$value) { | |
459 | $page->assign($field, $value); | |
460 | } | |
7c2e0f0d | 461 | $this->_prepare($page, $id); |
e5bcd851 FB |
462 | $page->assign('profile', $this->profile); |
463 | $page->assign('owner', $this->owner); | |
fd38b30e | 464 | $page->assign('profile_page', $this->pg_template); |
93553cea | 465 | $page->assign('errors', $this->errors); |
f118f685 FB |
466 | } |
467 | ||
eb563236 | 468 | public function process(&$global_success) |
f118f685 | 469 | { |
fd38b30e FB |
470 | $global_success = true; |
471 | $this->fetchData(); | |
472 | foreach ($this->settings as $field=>&$setting) { | |
473 | $success = false; | |
93553cea | 474 | if (!is_null($setting)) { |
85cc366b | 475 | $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success); |
93553cea FB |
476 | } else { |
477 | $success = true; | |
85cc366b | 478 | $this->values[$field] = Post::v($field, ''); |
93553cea FB |
479 | } |
480 | $this->errors[$field] = !$success; | |
fd38b30e FB |
481 | $global_success = $global_success && $success; |
482 | } | |
483 | if ($global_success) { | |
93553cea FB |
484 | if ($this->checkChanges()) { |
485 | $this->saveData(); | |
486 | $this->markChange(); | |
fd38b30e | 487 | } |
93553cea | 488 | return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE; |
fd38b30e | 489 | } |
d7610c35 | 490 | Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations " |
eb563236 | 491 | . "de ton profil et de revalider ta demande."); |
f118f685 FB |
492 | return PlWizard::CURRENT_PAGE; |
493 | } | |
eb563236 SJ |
494 | |
495 | public function success() | |
496 | { | |
497 | return 'Ton profil a bien été mis à jour.'; | |
498 | } | |
f118f685 FB |
499 | } |
500 | ||
fd38b30e | 501 | require_once dirname(__FILE__) . '/general.inc.php'; |
0b14f91d | 502 | require_once dirname(__FILE__) . '/addresses.inc.php'; |
92412b28 | 503 | require_once dirname(__FILE__) . '/groups.inc.php'; |
a7c28fff | 504 | require_once dirname(__FILE__) . '/decos.inc.php'; |
3950bc21 | 505 | require_once dirname(__FILE__) . '/jobs.inc.php'; |
f25e1a56 | 506 | require_once dirname(__FILE__) . '/skills.inc.php'; |
6457b5e4 | 507 | require_once dirname(__FILE__) . '/mentor.inc.php'; |
f118f685 FB |
508 | |
509 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
510 | ?> |