Commit | Line | Data |
---|---|---|
f118f685 FB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2007 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
fd38b30e FB |
22 | interface ProfileSetting |
23 | { | |
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. | |
27 | * | |
28 | * If value is null, the default value should be returned. | |
29 | * TODO: check this does not conflict with some possible values. | |
30 | * | |
31 | * Whatever happen, this function must always returns the function to | |
32 | * show on the page to the user. | |
33 | */ | |
34 | public function value(ProfilePage &$page, $field, $value, &$success); | |
35 | ||
36 | /** Save the new value for the given field. | |
37 | */ | |
38 | public function save(ProfilePage &$page, $field, $new_value); | |
39 | } | |
40 | ||
41 | abstract class ProfileNoSave implements ProfileSetting | |
42 | { | |
43 | public function save(ProfilePage &$page, $field, $new_value) { } | |
44 | } | |
45 | ||
fd38b30e FB |
46 | class ProfileWeb extends ProfileNoSave |
47 | { | |
48 | public function value(ProfilePage &$page, $field, $value, &$success) | |
49 | { | |
50 | if (is_null($value)) { | |
51 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
52 | } | |
53 | $success = preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value); | |
54 | if (!$success) { | |
55 | global $page; | |
56 | $page->trig('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://' | |
57 | . ' et ne pas contenir de caractères interdits'); | |
58 | } | |
59 | return $value; | |
60 | } | |
61 | } | |
62 | ||
63 | class ProfileTel extends ProfileNoSave | |
64 | { | |
65 | public function value(ProfilePage &$page, $field, $value, &$success) | |
66 | { | |
67 | if (is_null($value)) { | |
68 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
69 | } | |
93553cea | 70 | $success = strlen(strtok($value, '<>{}@&#~\/:;?,!§*_`[]|%$^=')) == strlen($value); |
fd38b30e FB |
71 | if (!$success) { |
72 | global $page; | |
73 | $page->trig('Le numéro de téléphone contient un caractère interdit.'); | |
74 | } | |
75 | return $value; | |
76 | } | |
77 | } | |
78 | ||
93553cea FB |
79 | class ProfilePub extends ProfileNoSave |
80 | { | |
81 | public function value(ProfilePage &$page, $field, $value, &$success) | |
82 | { | |
83 | $success = true; | |
84 | if (is_null($value)) { | |
85 | return isset($page->values[$field]) ? $page->values[$field] : S::v($field); | |
86 | } | |
87 | if (is_null($value) || !$value) { | |
88 | $value = 'private'; | |
89 | } else if ($value == 'on') { // Checkbox | |
90 | $value = 'public'; | |
91 | } | |
92 | return $value; | |
93 | } | |
94 | } | |
95 | ||
fd38b30e | 96 | abstract class ProfilePage implements PlWizardPage |
f118f685 FB |
97 | { |
98 | protected $wizard; | |
fd38b30e FB |
99 | protected $pg_template; |
100 | protected $settings = array(); // A set ProfileSetting objects | |
93553cea | 101 | protected $errors = array(); // A set of boolean with the value check errors |
fd38b30e | 102 | |
93553cea | 103 | public $orig = array(); |
fd38b30e | 104 | public $values = array(); |
f118f685 FB |
105 | |
106 | public function __construct(PlWizard &$wiz) | |
107 | { | |
108 | $this->wizard =& $wiz; | |
109 | } | |
110 | ||
fd38b30e FB |
111 | protected function fetchData() |
112 | { | |
93553cea FB |
113 | if (count($this->orig) > 0) { |
114 | $this->values = $this->orig; | |
115 | return; | |
116 | } | |
117 | foreach ($this->settings as $field=>&$setting) { | |
118 | $success = false; | |
119 | if (!is_null($setting)) { | |
120 | $this->values[$field] = $setting->value($this, $field, null, $success); | |
121 | } else if (!isset($this->values[$field])) { | |
122 | $this->values[$field] = S::v($field); | |
123 | } | |
124 | $this->errors[$field] = false; | |
125 | } | |
126 | $this->orig = $this->values; | |
fd38b30e FB |
127 | } |
128 | ||
129 | protected function saveData() | |
130 | { | |
93553cea FB |
131 | foreach ($this->settings as $field=>&$setting) { |
132 | if (!is_null($setting)) { | |
133 | $setting->save($this, $field, $this->values[$field]); | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | protected function checkChanges() | |
139 | { | |
140 | $newvalues = $this->values; | |
141 | $this->values = array(); | |
142 | $this->fetchData(); | |
143 | $this->values = $newvalues; | |
144 | foreach ($this->settings as $field=>&$setting) { | |
145 | if ($this->orig[$field] != $this->values[$field]) { | |
146 | return true; | |
147 | } | |
148 | } | |
149 | return false; | |
150 | } | |
151 | ||
152 | protected function markChange() | |
153 | { | |
fd38b30e FB |
154 | } |
155 | ||
f118f685 FB |
156 | public function template() |
157 | { | |
fd38b30e | 158 | return 'profile/base.tpl'; |
f118f685 FB |
159 | } |
160 | ||
161 | public function prepare(PlatalPage &$page) | |
162 | { | |
fd38b30e FB |
163 | if (count($this->values) == 0) { |
164 | $this->fetchData(); | |
fd38b30e FB |
165 | } |
166 | foreach ($this->values as $field=>&$value) { | |
167 | $page->assign($field, $value); | |
168 | } | |
169 | $page->assign('profile_page', $this->pg_template); | |
93553cea | 170 | $page->assign('errors', $this->errors); |
f118f685 FB |
171 | } |
172 | ||
173 | public function process() | |
174 | { | |
fd38b30e FB |
175 | $global_success = true; |
176 | $this->fetchData(); | |
177 | foreach ($this->settings as $field=>&$setting) { | |
178 | $success = false; | |
93553cea FB |
179 | if (!is_null($setting)) { |
180 | $this->values[$field] = $setting->value($this, $field, Post::v($field), $success); | |
181 | } else { | |
182 | $success = true; | |
183 | $this->values[$field] = Post::v($field); | |
184 | } | |
185 | $this->errors[$field] = !$success; | |
fd38b30e FB |
186 | $global_success = $global_success && $success; |
187 | } | |
188 | if ($global_success) { | |
93553cea FB |
189 | if ($this->checkChanges()) { |
190 | $this->saveData(); | |
191 | $this->markChange(); | |
fd38b30e | 192 | } |
93553cea | 193 | return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE; |
fd38b30e FB |
194 | } |
195 | global $page; | |
93553cea | 196 | $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations " |
fd38b30e | 197 | . "de ton profil et de revalider ta demande"); |
f118f685 FB |
198 | return PlWizard::CURRENT_PAGE; |
199 | } | |
200 | } | |
201 | ||
fd38b30e | 202 | require_once dirname(__FILE__) . '/general.inc.php'; |
f118f685 FB |
203 | |
204 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
205 | ?> |