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 ProfileFixed
extends ProfileNoSave
48 public function value(ProfilePage
&$page, $field, $value, &$success)
51 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
55 class ProfileWeb
extends ProfileNoSave
57 public function value(ProfilePage
&$page, $field, $value, &$success)
59 if (is_null($value)) {
60 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
62 $success = preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
65 $page->trig('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
66 . ' et ne pas contenir de caractères interdits');
72 class ProfileTel
extends ProfileNoSave
74 public function value(ProfilePage
&$page, $field, $value, &$success)
76 if (is_null($value)) {
77 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
79 $success = strlen(strtok($value, '<>{}@&#~\/:;?,!§*_`[]|%$^=')) < strlen($value);
82 $page->trig('Le numéro de téléphone contient un caractère interdit.');
88 abstract class ProfilePage
implements PlWizardPage
91 protected $pg_template;
92 protected $settings = array(); // A set ProfileSetting objects
94 public $values = array();
96 public function __construct(PlWizard
&$wiz)
98 $this->wizard
=& $wiz;
101 protected function fetchData()
105 protected function saveData()
109 public function template()
111 return 'profile/base.tpl';
114 public function prepare(PlatalPage
&$page)
116 if (count($this->values
) == 0) {
118 foreach ($this->settings
as $field=>&$setting) {
120 $this->values
[$field] = $setting->value($this, $field, null
, $success);
123 foreach ($this->values
as $field=>&$value) {
124 $page->assign($field, $value);
126 $page->assign('profile_page', $this->pg_template
);
129 public function process()
131 $global_success = true
;
133 foreach ($this->settings
as $field=>&$setting) {
135 $this->values
[$field] = $setting->value($this, $field, Post
::v($field), $success);
136 $global_success = $global_success && $success;
138 if ($global_success) {
139 foreach ($this->settings
as $field=>&$setting) {
140 $setting->save($this, $field, $this->values
[$field]);
143 return Post
::has('valid_and_next') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
146 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les infos "
147 . "de ton profil et de revalider ta demande");
148 return PlWizard
::CURRENT_PAGE
;
152 require_once dirname(__FILE__
) . '/general.inc.php';
154 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: