Commit | Line | Data |
---|---|---|
f118f685 FB |
1 | <?php |
2 | /*************************************************************************** | |
12262f13 | 3 | * Copyright (C) 2003-2011 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 | */ | |
26ba053e | 34 | public function value(ProfilePage $page, $field, $value, &$success); |
fd38b30e FB |
35 | |
36 | /** Save the new value for the given field. | |
37 | */ | |
26ba053e | 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 | { | |
26ba053e | 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 | 55 | { |
26ba053e | 56 | public function value(ProfilePage $page, $field, $value, &$success) |
fd38b30e FB |
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 | 72 | { |
26ba053e | 73 | public function value(ProfilePage $page, $field, $value, &$success) |
37d44b3b FB |
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 | 88 | { |
26ba053e | 89 | public function value(ProfilePage $page, $field, $value, &$success) |
92446a53 PC |
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 | ||
12bcf04b | 103 | class ProfileSettingPhones implements ProfileSetting |
bde2be3b | 104 | { |
26ba053e | 105 | public function value(ProfilePage $page, $field, $value, &$success) |
bde2be3b GB |
106 | { |
107 | $success = true; | |
0b6c8b36 | 108 | $phones = array(); |
ed01acac | 109 | |
0b6c8b36 | 110 | if (is_null($value)) { |
22771578 | 111 | $it = Phone::iterate(array($page->pid()), array(Phone::LINK_PROFILE), array(0), Visibility::defaultForEdit()); |
0b6c8b36 SJ |
112 | while ($phone = $it->next()) { |
113 | $success = ($phone->format() && $success); | |
114 | $phones[] = $phone->toFormArray(); | |
ed01acac | 115 | } |
0b6c8b36 SJ |
116 | if (count($phones) == 0) { |
117 | $phone = new Phone(); | |
118 | $phones[] = $phone->toFormArray(); | |
bde2be3b | 119 | } |
0b6c8b36 | 120 | return $phones; |
31dede92 FB |
121 | } else { |
122 | $phones = Phone::formatFormArray($value, $success); | |
123 | if (!$success) { | |
124 | Platal::page()->trigError('Numéro de téléphone invalide'); | |
125 | } | |
42ec0fe2 | 126 | return $phones; |
bde2be3b | 127 | } |
bde2be3b GB |
128 | } |
129 | ||
26ba053e | 130 | public function save(ProfilePage $page, $field, $value) |
bde2be3b | 131 | { |
6592a264 | 132 | Phone::deletePhones($page->pid(), Phone::LINK_PROFILE, null, S::user()->isMe($page->owner) || S::admin()); |
0b6c8b36 | 133 | Phone::savePhones($value, $page->pid(), Phone::LINK_PROFILE); |
bde2be3b | 134 | } |
a0fce0c6 | 135 | |
e45ad94e FB |
136 | public function getText($value) |
137 | { | |
0b6c8b36 | 138 | return Phone::formArrayToString($value); |
a0fce0c6 | 139 | } |
bde2be3b GB |
140 | } |
141 | ||
12bcf04b | 142 | class ProfileSettingPub extends ProfileNoSave |
93553cea | 143 | { |
26ba053e | 144 | public function value(ProfilePage $page, $field, $value, &$success) |
93553cea FB |
145 | { |
146 | $success = true; | |
147 | if (is_null($value)) { | |
148 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
149 | } | |
041a5cec | 150 | if (!$value) { |
93553cea | 151 | $value = 'private'; |
041a5cec | 152 | } elseif ($value == 'on') { // Checkbox |
93553cea FB |
153 | $value = 'public'; |
154 | } | |
155 | return $value; | |
156 | } | |
a0fce0c6 SJ |
157 | |
158 | public function getText($value) { | |
14aba233 SJ |
159 | static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé'); |
160 | return $pubs[$value]; | |
a0fce0c6 | 161 | } |
93553cea FB |
162 | } |
163 | ||
12bcf04b | 164 | class ProfileSettingBool extends ProfileNoSave |
576777d7 | 165 | { |
26ba053e | 166 | public function value(ProfilePage $page, $field, $value, &$success) |
576777d7 FB |
167 | { |
168 | $success = true; | |
169 | if (is_null($value)) { | |
041a5cec | 170 | $value = isset($page->values[$field]) ? $page->values[$field] : null; |
576777d7 | 171 | } |
ee12da4e | 172 | return $value ? "1" : ""; |
576777d7 FB |
173 | } |
174 | } | |
175 | ||
12bcf04b | 176 | class ProfileSettingDate extends ProfileNoSave |
7bff4cb0 | 177 | { |
42ec0fe2 FB |
178 | private $allowEmpty; |
179 | ||
180 | public function __construct($allowEmpty = false) | |
181 | { | |
182 | $this->allowEmpty = $allowEmpty; | |
183 | } | |
184 | ||
26ba053e | 185 | public function value(ProfilePage $page, $field, $value, &$success) |
7bff4cb0 FB |
186 | { |
187 | $success = true; | |
188 | if (is_null($value)) { | |
189 | $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]); | |
190 | } else { | |
42ec0fe2 FB |
191 | $value = trim($value); |
192 | if (empty($value) && $this->allowEmpty) { | |
193 | return null; | |
194 | } | |
7bff4cb0 FB |
195 | $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches); |
196 | if (!$success) { | |
d7610c35 | 197 | Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa"); |
4fc3b113 | 198 | } else { |
7bff4cb0 FB |
199 | $day = (int)$matches[1]; |
200 | $month = (int)$matches[2]; | |
201 | $year = (int)$matches[3]; | |
202 | $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020); | |
203 | if (!$success) { | |
d7610c35 | 204 | Platal::page()->trigError("La date n'a pas une valeur valide"); |
7bff4cb0 FB |
205 | } |
206 | } | |
207 | } | |
208 | return $value; | |
209 | } | |
87db81e7 FB |
210 | |
211 | public static function toSQLDate($value) | |
212 | { | |
213 | return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value); | |
214 | } | |
7bff4cb0 FB |
215 | } |
216 | ||
fd38b30e | 217 | abstract class ProfilePage implements PlWizardPage |
f118f685 FB |
218 | { |
219 | protected $wizard; | |
fd38b30e FB |
220 | protected $pg_template; |
221 | protected $settings = array(); // A set ProfileSetting objects | |
93553cea | 222 | protected $errors = array(); // A set of boolean with the value check errors |
576777d7 | 223 | protected $changed = array(); // A set of boolean indicating wether the value has been changed |
a2a1c2f2 | 224 | protected $watched = array(); // A set of boolean indicating the fields that are watched |
fd38b30e | 225 | |
93553cea | 226 | public $orig = array(); |
fd38b30e | 227 | public $values = array(); |
e5bcd851 FB |
228 | public $profile = null; |
229 | public $owner = null; | |
f118f685 | 230 | |
26ba053e | 231 | public function __construct(PlWizard $wiz) |
f118f685 FB |
232 | { |
233 | $this->wizard =& $wiz; | |
e5bcd851 FB |
234 | $this->profile = $this->wizard->getUserData('profile'); |
235 | $this->owner = $this->wizard->getUserData('owner'); | |
f118f685 FB |
236 | } |
237 | ||
7c2e0f0d FB |
238 | protected function _fetchData() |
239 | { | |
240 | } | |
241 | ||
fd38b30e FB |
242 | protected function fetchData() |
243 | { | |
93553cea FB |
244 | if (count($this->orig) > 0) { |
245 | $this->values = $this->orig; | |
246 | return; | |
247 | } | |
7c2e0f0d FB |
248 | |
249 | $this->_fetchData(); | |
93553cea FB |
250 | foreach ($this->settings as $field=>&$setting) { |
251 | $success = false; | |
252 | if (!is_null($setting)) { | |
253 | $this->values[$field] = $setting->value($this, $field, null, $success); | |
254 | } else if (!isset($this->values[$field])) { | |
255 | $this->values[$field] = S::v($field); | |
256 | } | |
257 | $this->errors[$field] = false; | |
258 | } | |
259 | $this->orig = $this->values; | |
fd38b30e FB |
260 | } |
261 | ||
7c2e0f0d FB |
262 | protected function _saveData() |
263 | { | |
264 | } | |
265 | ||
31cbd100 | 266 | public function saveData() |
fd38b30e | 267 | { |
a2a1c2f2 | 268 | require_once 'notifs.inc.php'; |
a0fce0c6 | 269 | $changedFields = array(); |
93553cea | 270 | foreach ($this->settings as $field=>&$setting) { |
a0fce0c6 SJ |
271 | if ($this->changed[$field]) { |
272 | if (!is_null($setting)) { | |
273 | $changedFields[$field] = array( | |
f5751460 SJ |
274 | preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->orig[$field])), |
275 | preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->values[$field])), | |
a0fce0c6 SJ |
276 | ); |
277 | } else { | |
278 | $changedFields[$field] = array( | |
f5751460 SJ |
279 | preg_replace('/(\r\n|\n|\r)/', ' - ', $this->orig[$field]), |
280 | preg_replace('/(\r\n|\n|\r)/', ' - ', $this->values[$field]), | |
a0fce0c6 SJ |
281 | ); |
282 | } | |
283 | if (!is_null($setting)) { | |
284 | $setting->save($this, $field, $this->values[$field]); | |
285 | } | |
286 | if (isset($this->watched[$field]) && $this->watched[$field]) { | |
287 | WatchProfileUpdate::register($this->profile, $field); | |
288 | } | |
a2a1c2f2 | 289 | } |
93553cea | 290 | } |
7c2e0f0d | 291 | $this->_saveData(); |
576777d7 FB |
292 | |
293 | // Update the last modification date | |
4e7bf1e0 FB |
294 | XDB::execute('UPDATE profiles |
295 | SET last_change = NOW() | |
296 | WHERE pid = {?}', $this->pid()); | |
576777d7 | 297 | global $platal; |
e5bcd851 | 298 | S::logger()->log('profil', $platal->pl_self(2)); |
a0fce0c6 | 299 | |
f5751460 SJ |
300 | /** Stores all profile modifications for active users in order to: |
301 | * -daily notify the user in case of third party edition, | |
302 | * -display the modification to the secretaries for verification in | |
303 | * case of an edition made by the user. | |
a0fce0c6 SJ |
304 | */ |
305 | $owner = $this->profile->owner(); | |
306 | $user = S::user(); | |
f5751460 | 307 | if ($owner->isActive()) { |
a0fce0c6 | 308 | foreach ($changedFields as $field => $values) { |
6ccad32e | 309 | if (in_array($field, Profile::$descriptions)) { |
39cb9bf3 SJ |
310 | XDB::execute('INSERT INTO profile_modifications (pid, uid, field, oldText, newText, type, timestamp) |
311 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, NOW()) | |
312 | ON DUPLICATE KEY UPDATE uid = VALUES(uid), oldText = IF(VALUES(type) != type, VALUES(oldText), oldText), | |
313 | newText = VALUES(newText), type = VALUES(type), timestamp = NOW()', | |
314 | $this->pid(), $user->id(), Profile::$descriptions[$field], $values[0], $values[1], | |
315 | ($owner->id() == $user->id()) ? 'self' : 'third_party'); | |
316 | } | |
a0fce0c6 SJ |
317 | } |
318 | } | |
31cbd100 | 319 | return true; |
93553cea FB |
320 | } |
321 | ||
322 | protected function checkChanges() | |
323 | { | |
324 | $newvalues = $this->values; | |
325 | $this->values = array(); | |
326 | $this->fetchData(); | |
327 | $this->values = $newvalues; | |
576777d7 | 328 | $changes = false; |
93553cea FB |
329 | foreach ($this->settings as $field=>&$setting) { |
330 | if ($this->orig[$field] != $this->values[$field]) { | |
576777d7 FB |
331 | $this->changed[$field] = true; |
332 | $changes = true; | |
333 | } else { | |
334 | $this->changed[$field] = false; | |
93553cea FB |
335 | } |
336 | } | |
576777d7 | 337 | return $changes; |
93553cea FB |
338 | } |
339 | ||
340 | protected function markChange() | |
341 | { | |
fd38b30e FB |
342 | } |
343 | ||
f118f685 FB |
344 | public function template() |
345 | { | |
fd38b30e | 346 | return 'profile/base.tpl'; |
f118f685 FB |
347 | } |
348 | ||
e5bcd851 FB |
349 | public function pid() |
350 | { | |
351 | return $this->profile->id(); | |
352 | } | |
353 | ||
354 | public function hrpid() | |
355 | { | |
356 | return $this->profile->hrpid(); | |
357 | } | |
358 | ||
26ba053e | 359 | protected function _prepare(PlPage $page, $id) |
7c2e0f0d FB |
360 | { |
361 | } | |
362 | ||
26ba053e | 363 | public function prepare(PlPage $page, $id) |
f118f685 | 364 | { |
fd38b30e FB |
365 | if (count($this->values) == 0) { |
366 | $this->fetchData(); | |
fd38b30e FB |
367 | } |
368 | foreach ($this->values as $field=>&$value) { | |
369 | $page->assign($field, $value); | |
370 | } | |
7c2e0f0d | 371 | $this->_prepare($page, $id); |
e5bcd851 FB |
372 | $page->assign('profile', $this->profile); |
373 | $page->assign('owner', $this->owner); | |
fd38b30e | 374 | $page->assign('profile_page', $this->pg_template); |
93553cea | 375 | $page->assign('errors', $this->errors); |
f118f685 FB |
376 | } |
377 | ||
eb563236 | 378 | public function process(&$global_success) |
f118f685 | 379 | { |
fd38b30e FB |
380 | $global_success = true; |
381 | $this->fetchData(); | |
382 | foreach ($this->settings as $field=>&$setting) { | |
383 | $success = false; | |
93553cea | 384 | if (!is_null($setting)) { |
85cc366b | 385 | $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success); |
93553cea FB |
386 | } else { |
387 | $success = true; | |
85cc366b | 388 | $this->values[$field] = Post::v($field, ''); |
93553cea FB |
389 | } |
390 | $this->errors[$field] = !$success; | |
fd38b30e FB |
391 | $global_success = $global_success && $success; |
392 | } | |
393 | if ($global_success) { | |
93553cea | 394 | if ($this->checkChanges()) { |
31cbd100 FB |
395 | /* Save changes atomically to avoid inconsistent state |
396 | * in case of error. | |
397 | */ | |
398 | if (!XDB::runTransaction(array($this, 'saveData'))) { | |
399 | $global_success = false; | |
400 | return PlWizard::CURRENT_PAGE; | |
401 | } | |
93553cea | 402 | $this->markChange(); |
fd38b30e | 403 | } |
3f09ff52 SJ |
404 | // XXX: removes this code once all merge related issues have been fixed. |
405 | static $issues = array(0 => array('name', 'promo', 'phone', 'education'), 1 => array('address'), 2 => array('job')); | |
406 | if (isset($issues[Post::i('valid_page')])) { | |
aadf2259 | 407 | foreach ($issues[Post::i('valid_page')] as $issue) { |
3f09ff52 SJ |
408 | XDB::execute("UPDATE profile_merge_issues |
409 | SET issues = REPLACE(issues, {?}, '') | |
410 | WHERE pid = {?}", | |
411 | $issue, $this->pid()); | |
412 | } | |
413 | } | |
93553cea | 414 | return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE; |
fd38b30e | 415 | } |
42ec0fe2 FB |
416 | $text = "Certains champs n'ont pas pu être validés, merci de corriger les informations " |
417 | . (S::user()->isMe($this->owner) ? "de ton profil et de revalider ta demande." | |
418 | : "du profil et de revalider ta demande."); | |
419 | Platal::page()->trigError($text); | |
f118f685 FB |
420 | return PlWizard::CURRENT_PAGE; |
421 | } | |
eb563236 SJ |
422 | |
423 | public function success() | |
424 | { | |
42ec0fe2 FB |
425 | if (S::user()->isMe($this->owner)) { |
426 | return 'Ton profil a bien été mis à jour.'; | |
427 | } else { | |
428 | return 'Le profil a bien été mis à jour.'; | |
429 | } | |
eb563236 | 430 | } |
f118f685 FB |
431 | } |
432 | ||
fd38b30e | 433 | require_once dirname(__FILE__) . '/general.inc.php'; |
0b14f91d | 434 | require_once dirname(__FILE__) . '/addresses.inc.php'; |
92412b28 | 435 | require_once dirname(__FILE__) . '/groups.inc.php'; |
a7c28fff | 436 | require_once dirname(__FILE__) . '/decos.inc.php'; |
3950bc21 | 437 | require_once dirname(__FILE__) . '/jobs.inc.php'; |
6457b5e4 | 438 | require_once dirname(__FILE__) . '/mentor.inc.php'; |
080632c7 | 439 | require_once dirname(__FILE__) . '/deltaten.inc.php'; |
f118f685 FB |
440 | |
441 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
442 | ?> |