6decf819af396d0d09a05edcb202e5156fcf8a64
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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);
41 abstract class ProfileNoSave
implements ProfileSetting
43 public function save(ProfilePage
&$page, $field, $new_value) { }
46 class ProfileWeb
extends ProfileNoSave
48 public function value(ProfilePage
&$page, $field, $value, &$success)
50 if (is_null($value)) {
51 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
53 $success = !trim($value) ||
preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
56 $page->trig('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
57 . ' et ne pas contenir de caractères interdits');
63 class ProfileTel
extends ProfileNoSave
65 public function value(ProfilePage
&$page, $field, $value, &$success)
67 if (is_null($value)) {
68 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
70 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
73 $page->trig('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
79 class ProfilePub
extends ProfileNoSave
81 public function value(ProfilePage
&$page, $field, $value, &$success)
84 if (is_null($value)) {
85 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
87 if (is_null($value) ||
!$value) {
89 } else if ($value == 'on') { // Checkbox
96 class ProfileBool
extends ProfileNoSave
98 public function value(ProfilePage
&$page, $field, $value, &$success)
101 if (is_null($value)) {
102 $value = @$page->values
[$field];
104 return $value ?
1 : 0;
108 abstract class ProfilePage
implements PlWizardPage
111 protected $pg_template;
112 protected $settings = array(); // A set ProfileSetting objects
113 protected $errors = array(); // A set of boolean with the value check errors
114 protected $changed = array(); // A set of boolean indicating wether the value has been changed
116 public $orig = array();
117 public $values = array();
119 public function __construct(PlWizard
&$wiz)
121 $this->wizard
=& $wiz;
124 protected function fetchData()
126 if (count($this->orig
) > 0) {
127 $this->values
= $this->orig
;
130 foreach ($this->settings
as $field=>&$setting) {
132 if (!is_null($setting)) {
133 $this->values
[$field] = $setting->value($this, $field, null
, $success);
134 } else if (!isset($this->values
[$field])) {
135 $this->values
[$field] = S
::v($field);
137 $this->errors
[$field] = false
;
139 $this->orig
= $this->values
;
142 protected function saveData()
144 foreach ($this->settings
as $field=>&$setting) {
145 if (!is_null($setting) && $this->changed
[$field]) {
146 $setting->save($this, $field, $this->values
[$field]);
150 // Update the last modification date
151 XDB
::execute('REPLACE INTO user_changes
152 SET user_id = {?}', S
::v('uid'));
154 $log =& $_SESSION['log'];
155 $log->log('profil', $platal->pl_self(1));
158 protected function checkChanges()
160 $newvalues = $this->values
;
161 $this->values
= array();
163 $this->values
= $newvalues;
165 foreach ($this->settings
as $field=>&$setting) {
166 if ($this->orig
[$field] != $this->values
[$field]) {
167 $this->changed
[$field] = true
;
170 $this->changed
[$field] = false
;
176 protected function markChange()
180 public function template()
182 return 'profile/base.tpl';
185 public function prepare(PlatalPage
&$page)
187 if (count($this->values
) == 0) {
190 foreach ($this->values
as $field=>&$value) {
191 $page->assign($field, $value);
193 $page->assign('profile_page', $this->pg_template
);
194 $page->assign('errors', $this->errors
);
197 public function process()
199 $global_success = true
;
201 foreach ($this->settings
as $field=>&$setting) {
203 if (!is_null($setting)) {
204 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
207 $this->values
[$field] = Post
::v($field, '');
209 $this->errors
[$field] = !$success;
210 $global_success = $global_success && $success;
212 if ($global_success) {
213 if ($this->checkChanges()) {
217 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
220 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations "
221 . "de ton profil et de revalider ta demande");
222 return PlWizard
::CURRENT_PAGE
;
226 require_once dirname(__FILE__
) . '/general.inc.php';
227 require_once dirname(__FILE__
) . '/addresses.inc.php';
228 require_once dirname(__FILE__
) . '/groups.inc.php';
229 require_once dirname(__FILE__
) . '/decos.inc.php';
230 require_once dirname(__FILE__
) . '/jobs.inc.php';
232 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: