2 /***************************************************************************
3 * Copyright (C) 2003-2011 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), Visibility
::defaultForEdit());
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();
122 $phones = Phone
::formatFormArray($value, $success);
124 Platal
::page()->trigError('Numéro de téléphone invalide');
130 public function save(ProfilePage
$page, $field, $value)
132 Phone
::deletePhones($page->pid(), Phone
::LINK_PROFILE
, null
, S
::user()->isMe($page->owner
) || S
::admin());
133 Phone
::savePhones($value, $page->pid(), Phone
::LINK_PROFILE
);
136 public function getText($value)
138 return Phone
::formArrayToString($value);
142 class ProfileSettingPub
extends ProfileNoSave
144 public function value(ProfilePage
$page, $field, $value, &$success)
147 if (is_null($value)) {
148 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
152 } elseif ($value == 'on') { // Checkbox
158 public function getText($value) {
159 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
160 return $pubs[$value];
164 class ProfileSettingBool
extends ProfileNoSave
166 public function value(ProfilePage
$page, $field, $value, &$success)
169 if (is_null($value)) {
170 $value = isset($page->values
[$field]) ?
$page->values
[$field] : null
;
172 return $value ?
"1" : "";
176 class ProfileSettingDate
extends ProfileNoSave
180 public function __construct($allowEmpty = false
)
182 $this->allowEmpty
= $allowEmpty;
185 public function value(ProfilePage
$page, $field, $value, &$success)
188 if (is_null($value)) {
189 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
191 $value = trim($value);
192 if (empty($value) && $this->allowEmpty
) {
195 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
197 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
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);
204 Platal
::page()->trigError("La date n'a pas une valeur valide");
211 public static function toSQLDate($value)
213 return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value);
217 abstract class ProfilePage
implements PlWizardPage
220 protected $pg_template;
221 protected $settings = array(); // A set ProfileSetting objects
222 protected $errors = array(); // A set of boolean with the value check errors
223 protected $changed = array(); // A set of boolean indicating wether the value has been changed
224 protected $watched = array(); // A set of boolean indicating the fields that are watched
226 public $orig = array();
227 public $values = array();
228 public $profile = null
;
229 public $owner = null
;
231 public function __construct(PlWizard
$wiz)
233 $this->wizard
=& $wiz;
234 $this->profile
= $this->wizard
->getUserData('profile');
235 $this->owner
= $this->wizard
->getUserData('owner');
238 protected function _fetchData()
242 protected function fetchData()
244 if (count($this->orig
) > 0) {
245 $this->values
= $this->orig
;
250 foreach ($this->settings
as $field=>&$setting) {
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);
257 $this->errors
[$field] = false
;
259 $this->orig
= $this->values
;
262 protected function _saveData()
266 public function saveData()
268 require_once 'notifs.inc.php';
269 $changedFields = array();
270 foreach ($this->settings
as $field=>&$setting) {
271 if ($this->changed
[$field]) {
272 if (!is_null($setting)) {
273 $changedFields[$field] = array(
274 preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->orig
[$field])),
275 preg_replace('/(\r\n|\n|\r)/', ' - ', $setting->getText($this->values
[$field])),
278 $changedFields[$field] = array(
279 preg_replace('/(\r\n|\n|\r)/', ' - ', $this->orig
[$field]),
280 preg_replace('/(\r\n|\n|\r)/', ' - ', $this->values
[$field]),
283 if (!is_null($setting)) {
284 $setting->save($this, $field, $this->values
[$field]);
286 if (isset($this->watched
[$field]) && $this->watched
[$field]) {
287 WatchProfileUpdate
::register($this->profile
, $field);
293 // Update the last modification date
294 XDB
::execute('UPDATE profiles
295 SET last_change = NOW()
296 WHERE pid = {?}', $this->pid());
298 S
::logger()->log('profil', $platal->pl_self(2));
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.
305 $owner = $this->profile
->owner();
307 if ($owner->isActive()) {
308 foreach ($changedFields as $field => $values) {
309 if (in_array($field, Profile
::$descriptions)) {
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');
322 protected function checkChanges()
324 $newvalues = $this->values
;
325 $this->values
= array();
327 $this->values
= $newvalues;
329 foreach ($this->settings
as $field=>&$setting) {
330 if ($this->orig
[$field] != $this->values
[$field]) {
331 $this->changed
[$field] = true
;
334 $this->changed
[$field] = false
;
340 protected function markChange()
344 public function template()
346 return 'profile/base.tpl';
349 public function pid()
351 return $this->profile
->id();
354 public function hrpid()
356 return $this->profile
->hrpid();
359 protected function _prepare(PlPage
$page, $id)
363 public function prepare(PlPage
$page, $id)
365 if (count($this->values
) == 0) {
368 foreach ($this->values
as $field=>&$value) {
369 $page->assign($field, $value);
371 $this->_prepare($page, $id);
372 $page->assign('profile', $this->profile
);
373 $page->assign('owner', $this->owner
);
374 $page->assign('profile_page', $this->pg_template
);
375 $page->assign('errors', $this->errors
);
378 public function process(&$global_success)
380 $global_success = true
;
382 foreach ($this->settings
as $field=>&$setting) {
384 if (!is_null($setting)) {
385 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
388 $this->values
[$field] = Post
::v($field, '');
390 $this->errors
[$field] = !$success;
391 $global_success = $global_success && $success;
393 if ($global_success) {
394 if ($this->checkChanges()) {
395 /* Save changes atomically to avoid inconsistent state
398 if (!XDB
::runTransaction(array($this, 'saveData'))) {
399 $global_success = false
;
400 return PlWizard
::CURRENT_PAGE
;
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')])) {
407 foreach ($issues[Post
::i('valid_page')] as $issue) {
408 XDB
::execute("UPDATE profile_merge_issues
409 SET issues = REPLACE(issues, {?}, '')
411 $issue, $this->pid());
414 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
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);
420 return PlWizard
::CURRENT_PAGE
;
423 public function success()
425 if (S
::user()->isMe($this->owner
)) {
426 return 'Ton profil a bien été mis à jour.';
428 return 'Le profil a bien été mis à jour.';
433 require_once dirname(__FILE__
) . '/general.inc.php';
434 require_once dirname(__FILE__
) . '/addresses.inc.php';
435 require_once dirname(__FILE__
) . '/groups.inc.php';
436 require_once dirname(__FILE__
) . '/decos.inc.php';
437 require_once dirname(__FILE__
) . '/jobs.inc.php';
438 require_once dirname(__FILE__
) . '/skills.inc.php';
439 require_once dirname(__FILE__
) . '/mentor.inc.php';
440 require_once dirname(__FILE__
) . '/deltaten.inc.php';
442 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: