2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 interface ProfileSetting
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.
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
34 public function value(ProfilePage
&$page, $field, $value, &$success);
36 /** Save the new value for the given field.
38 public function save(ProfilePage
&$page, $field, $new_value);
40 /** Get text from the value.
42 public function getText($value);
45 abstract class ProfileNoSave
implements ProfileSetting
47 public function save(ProfilePage
&$page, $field, $new_value) { }
49 public function getText($value) {
54 class ProfileSettingWeb
extends ProfileNoSave
56 public function value(ProfilePage
&$page, $field, $value, &$success)
58 if (is_null($value)) {
59 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
61 $value = trim($value);
62 $success = empty($value) ||
preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
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');
71 class ProfileSettingEmail
extends ProfileNoSave
73 public function value(ProfilePage
&$page, $field, $value, &$success)
75 if (is_null($value)) {
76 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
78 $value = trim($value);
79 $success = empty($value) ||
isvalid_email($value);
81 Platal
::page()->trigError('Adresse Email invalide');
87 class ProfileSettingNumber
extends ProfileNoSave
89 public function value(ProfilePage
&$page, $field, $value, &$success)
91 if (is_null($value)) {
92 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
94 $value = trim($value);
95 $success = empty($value) ||
is_numeric($value);
97 Platal
::page()->trigError('Numéro invalide');
103 class ProfileSettingPhones
implements ProfileSetting
105 public function value(ProfilePage
&$page, $field, $value, &$success)
110 if (is_null($value)) {
111 $it = Phone
::iterate(array($page->pid()), array(Phone
::LINK_PROFILE
), array(0));
112 while ($phone = $it->next()) {
113 $success = ($phone->format() && $success);
114 $phones[] = $phone->toFormArray();
116 if (count($phones) == 0) {
117 $phone = new Phone();
118 $phones[] = $phone->toFormArray();
123 return Phone
::formatFormArray($value, $success);
126 public function save(ProfilePage
&$page, $field, $value)
128 Phone
::deletePhones($page->pid(), Phone
::LINK_PROFILE
);
129 Phone
::savePhones($value, $page->pid(), Phone
::LINK_PROFILE
);
132 public function getText($value) {
133 return Phone
::formArrayToString($value);
137 class ProfileSettingPub
extends ProfileNoSave
139 public function value(ProfilePage
&$page, $field, $value, &$success)
142 if (is_null($value)) {
143 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
147 } elseif ($value == 'on') { // Checkbox
153 public function getText($value) {
158 class ProfileSettingBool
extends ProfileNoSave
160 public function value(ProfilePage
&$page, $field, $value, &$success)
163 if (is_null($value)) {
164 $value = isset($page->values
[$field]) ?
$page->values
[$field] : null
;
166 return $value ?
"1" : "";
170 class ProfileSettingDate
extends ProfileNoSave
172 public function value(ProfilePage
&$page, $field, $value, &$success)
175 if (is_null($value)) {
176 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
178 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
180 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
182 $day = (int)$matches[1];
183 $month = (int)$matches[2];
184 $year = (int)$matches[3];
185 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
187 Platal
::page()->trigError("La date n'a pas une valeur valide");
195 abstract class ProfilePage
implements PlWizardPage
198 protected $pg_template;
199 protected $settings = array(); // A set ProfileSetting objects
200 protected $errors = array(); // A set of boolean with the value check errors
201 protected $changed = array(); // A set of boolean indicating wether the value has been changed
202 protected $watched = array(); // A set of boolean indicating the fields that are watched
204 public $orig = array();
205 public $values = array();
206 public $profile = null
;
207 public $owner = null
;
209 public function __construct(PlWizard
&$wiz)
211 $this->wizard
=& $wiz;
212 $this->profile
= $this->wizard
->getUserData('profile');
213 $this->owner
= $this->wizard
->getUserData('owner');
216 protected function _fetchData()
220 protected function fetchData()
222 if (count($this->orig
) > 0) {
223 $this->values
= $this->orig
;
228 foreach ($this->settings
as $field=>&$setting) {
230 if (!is_null($setting)) {
231 $this->values
[$field] = $setting->value($this, $field, null
, $success);
232 } else if (!isset($this->values
[$field])) {
233 $this->values
[$field] = S
::v($field);
235 $this->errors
[$field] = false
;
237 $this->orig
= $this->values
;
240 protected function _saveData()
244 protected function saveData()
246 require_once 'notifs.inc.php';
247 $changedFields = array();
248 foreach ($this->settings
as $field=>&$setting) {
249 if ($this->changed
[$field]) {
250 if (!is_null($setting)) {
251 $changedFields[$field] = array(
252 str_replace("\n", " - ", $setting->getText($this->orig
[$field])),
253 str_replace("\n", " - ", $setting->getText($this->values
[$field])),
256 $changedFields[$field] = array(
257 str_replace("\n", " - ", $this->orig
[$field]),
258 str_replace("\n", " - ", $this->values
[$field]),
261 if (!is_null($setting)) {
262 $setting->save($this, $field, $this->values
[$field]);
264 if (isset($this->watched
[$field]) && $this->watched
[$field]) {
265 WatchProfileUpdate
::register($this->profile
, $field);
271 // Update the last modification date
272 XDB
::execute('UPDATE profiles
273 SET last_change = NOW()
274 WHERE pid = {?}', $this->pid());
276 S
::logger()->log('profil', $platal->pl_self(2));
278 /** If the update was made by a third party and the profile corresponds
279 * to a registered user, stores both former and new text.
280 * This will be daily sent to the user.
282 $owner = $this->profile
->owner();
284 if ($owner->isActive() && $owner->id() != $user->id()) {
285 foreach ($changedFields as $field => $values) {
286 XDB
::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
287 VALUES ({?}, {?}, {?}, {?}, {?})',
288 $this->pid(), $user->id(), $field, $values[0], $values[1]);
293 protected function checkChanges()
295 $newvalues = $this->values
;
296 $this->values
= array();
298 $this->values
= $newvalues;
300 foreach ($this->settings
as $field=>&$setting) {
301 if ($this->orig
[$field] != $this->values
[$field]) {
302 $this->changed
[$field] = true
;
305 $this->changed
[$field] = false
;
311 protected function markChange()
315 public function template()
317 return 'profile/base.tpl';
320 public function pid()
322 return $this->profile
->id();
325 public function hrpid()
327 return $this->profile
->hrpid();
330 protected function _prepare(PlPage
&$page, $id)
334 public function prepare(PlPage
&$page, $id)
336 if (count($this->values
) == 0) {
339 foreach ($this->values
as $field=>&$value) {
340 $page->assign($field, $value);
342 $this->_prepare($page, $id);
343 $page->assign('profile', $this->profile
);
344 $page->assign('owner', $this->owner
);
345 $page->assign('profile_page', $this->pg_template
);
346 $page->assign('errors', $this->errors
);
349 public function process(&$global_success)
351 $global_success = true
;
353 foreach ($this->settings
as $field=>&$setting) {
355 if (!is_null($setting)) {
356 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
359 $this->values
[$field] = Post
::v($field, '');
361 $this->errors
[$field] = !$success;
362 $global_success = $global_success && $success;
364 if ($global_success) {
365 if ($this->checkChanges()) {
369 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
371 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
372 . "de ton profil et de revalider ta demande.");
373 return PlWizard
::CURRENT_PAGE
;
376 public function success()
378 return 'Ton profil a bien été mis à jour.';
382 require_once dirname(__FILE__
) . '/general.inc.php';
383 require_once dirname(__FILE__
) . '/addresses.inc.php';
384 require_once dirname(__FILE__
) . '/groups.inc.php';
385 require_once dirname(__FILE__
) . '/decos.inc.php';
386 require_once dirname(__FILE__
) . '/jobs.inc.php';
387 require_once dirname(__FILE__
) . '/skills.inc.php';
388 require_once dirname(__FILE__
) . '/mentor.inc.php';
390 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: