d1a1cfc2862b4a4df8a7bf5777b7e5ecbdeb5153
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();
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
);
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) {
163 class ProfileSettingBool
extends ProfileNoSave
165 public function value(ProfilePage
&$page, $field, $value, &$success)
168 if (is_null($value)) {
169 $value = isset($page->values
[$field]) ?
$page->values
[$field] : null
;
171 return $value ?
"1" : "";
175 class ProfileSettingDate
extends ProfileNoSave
179 public function __construct($allowEmpty = false
)
181 $this->allowEmpty
= $allowEmpty;
184 public function value(ProfilePage
&$page, $field, $value, &$success)
187 if (is_null($value)) {
188 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
190 $value = trim($value);
191 if (empty($value) && $this->allowEmpty
) {
194 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
196 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
198 $day = (int)$matches[1];
199 $month = (int)$matches[2];
200 $year = (int)$matches[3];
201 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
203 Platal
::page()->trigError("La date n'a pas une valeur valide");
210 public static function toSQLDate($value)
212 return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value);
216 abstract class ProfilePage
implements PlWizardPage
219 protected $pg_template;
220 protected $settings = array(); // A set ProfileSetting objects
221 protected $errors = array(); // A set of boolean with the value check errors
222 protected $changed = array(); // A set of boolean indicating wether the value has been changed
223 protected $watched = array(); // A set of boolean indicating the fields that are watched
225 public $orig = array();
226 public $values = array();
227 public $profile = null
;
228 public $owner = null
;
230 public function __construct(PlWizard
&$wiz)
232 $this->wizard
=& $wiz;
233 $this->profile
= $this->wizard
->getUserData('profile');
234 $this->owner
= $this->wizard
->getUserData('owner');
237 protected function _fetchData()
241 protected function fetchData()
243 if (count($this->orig
) > 0) {
244 $this->values
= $this->orig
;
249 foreach ($this->settings
as $field=>&$setting) {
251 if (!is_null($setting)) {
252 $this->values
[$field] = $setting->value($this, $field, null
, $success);
253 } else if (!isset($this->values
[$field])) {
254 $this->values
[$field] = S
::v($field);
256 $this->errors
[$field] = false
;
258 $this->orig
= $this->values
;
261 protected function _saveData()
265 public function saveData()
267 require_once 'notifs.inc.php';
268 $changedFields = array();
269 foreach ($this->settings
as $field=>&$setting) {
270 if ($this->changed
[$field]) {
271 if (!is_null($setting)) {
272 $changedFields[$field] = array(
273 str_replace("\n", " - ", $setting->getText($this->orig
[$field])),
274 str_replace("\n", " - ", $setting->getText($this->values
[$field])),
277 $changedFields[$field] = array(
278 str_replace("\n", " - ", $this->orig
[$field]),
279 str_replace("\n", " - ", $this->values
[$field]),
282 if (!is_null($setting)) {
283 $setting->save($this, $field, $this->values
[$field]);
285 if (isset($this->watched
[$field]) && $this->watched
[$field]) {
286 WatchProfileUpdate
::register($this->profile
, $field);
292 // Update the last modification date
293 XDB
::execute('UPDATE profiles
294 SET last_change = NOW()
295 WHERE pid = {?}', $this->pid());
297 S
::logger()->log('profil', $platal->pl_self(2));
299 /** If the update was made by a third party and the profile corresponds
300 * to a registered user, stores both former and new text.
301 * This will be daily sent to the user.
303 $owner = $this->profile
->owner();
305 if ($owner->isActive() && $owner->id() != $user->id()) {
306 foreach ($changedFields as $field => $values) {
307 XDB
::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
308 VALUES ({?}, {?}, {?}, {?}, {?})',
309 $this->pid(), $user->id(), $field, $values[0], $values[1]);
315 protected function checkChanges()
317 $newvalues = $this->values
;
318 $this->values
= array();
320 $this->values
= $newvalues;
322 foreach ($this->settings
as $field=>&$setting) {
323 if ($this->orig
[$field] != $this->values
[$field]) {
324 $this->changed
[$field] = true
;
327 $this->changed
[$field] = false
;
333 protected function markChange()
337 public function template()
339 return 'profile/base.tpl';
342 public function pid()
344 return $this->profile
->id();
347 public function hrpid()
349 return $this->profile
->hrpid();
352 protected function _prepare(PlPage
&$page, $id)
356 public function prepare(PlPage
&$page, $id)
358 if (count($this->values
) == 0) {
361 foreach ($this->values
as $field=>&$value) {
362 $page->assign($field, $value);
364 $this->_prepare($page, $id);
365 $page->assign('profile', $this->profile
);
366 $page->assign('owner', $this->owner
);
367 $page->assign('profile_page', $this->pg_template
);
368 $page->assign('errors', $this->errors
);
371 public function process(&$global_success)
373 $global_success = true
;
375 foreach ($this->settings
as $field=>&$setting) {
377 if (!is_null($setting)) {
378 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
381 $this->values
[$field] = Post
::v($field, '');
383 $this->errors
[$field] = !$success;
384 $global_success = $global_success && $success;
386 if ($global_success) {
387 if ($this->checkChanges()) {
388 /* Save changes atomically to avoid inconsistent state
391 if (!XDB
::runTransaction(array($this, 'saveData'))) {
392 $global_success = false
;
393 return PlWizard
::CURRENT_PAGE
;
397 // XXX: removes this code once all merge related issues have been fixed.
398 static $issues = array(0 => array('name', 'promo', 'phone', 'education'), 1 => array('address'), 2 => array('job'));
399 if (isset($issues[Post
::i('valid_page')])) {
400 foreach ($issues[Post
::i('valid_page')] as $issue) {
401 XDB
::execute("UPDATE profile_merge_issues
402 SET issues = REPLACE(issues, {?}, '')
404 $issue, $this->pid());
407 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
409 $text = "Certains champs n'ont pas pu être validés, merci de corriger les informations "
410 . (S
::user()->isMe($this->owner
) ?
"de ton profil et de revalider ta demande."
411 : "du profil et de revalider ta demande.");
412 Platal
::page()->trigError($text);
413 return PlWizard
::CURRENT_PAGE
;
416 public function success()
418 if (S
::user()->isMe($this->owner
)) {
419 return 'Ton profil a bien été mis à jour.';
421 return 'Le profil a bien été mis à jour.';
426 require_once dirname(__FILE__
) . '/general.inc.php';
427 require_once dirname(__FILE__
) . '/addresses.inc.php';
428 require_once dirname(__FILE__
) . '/groups.inc.php';
429 require_once dirname(__FILE__
) . '/decos.inc.php';
430 require_once dirname(__FILE__
) . '/jobs.inc.php';
431 require_once dirname(__FILE__
) . '/skills.inc.php';
432 require_once dirname(__FILE__
) . '/mentor.inc.php';
434 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: