Remove duplicated code
[platal.git] / modules / profile / page.inc.php
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
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
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 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
55 if (!$success) {
56 global $page;
57 $page->trig('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
58 . ' et ne pas contenir de caractères interdits');
59 }
60 return $value;
61 }
62 }
63
64 class ProfileEmail extends ProfileNoSave
65 {
66 public function value(ProfilePage &$page, $field, $value, &$success)
67 {
68 if (is_null($value)) {
69 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
70 }
71 $value = trim($value);
72 require_once 'xorg.misc.inc.php';
73 $success = empty($value) || isvalid_email($value);
74 if (!$success) {
75 global $page;
76 $page->trig('Adresse Email invalide');
77 }
78 return $value;
79 }
80 }
81
82
83 class ProfileTel extends ProfileNoSave
84 {
85 public function value(ProfilePage &$page, $field, $value, &$success)
86 {
87 if (is_null($value)) {
88 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
89 }
90 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
91 if (!$success) {
92 global $page;
93 $page->trig('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
94 }
95 return $value;
96 }
97 }
98
99 class ProfilePub extends ProfileNoSave
100 {
101 public function value(ProfilePage &$page, $field, $value, &$success)
102 {
103 $success = true;
104 if (is_null($value)) {
105 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
106 }
107 if (is_null($value) || !$value) {
108 $value = 'private';
109 } else if ($value == 'on') { // Checkbox
110 $value = 'public';
111 }
112 return $value;
113 }
114 }
115
116 class ProfileBool extends ProfileNoSave
117 {
118 public function value(ProfilePage &$page, $field, $value, &$success)
119 {
120 $success = true;
121 if (is_null($value)) {
122 $value = @$page->values[$field];
123 }
124 return $value ? 1 : 0;
125 }
126 }
127
128 abstract class ProfileGeoloc implements ProfileSetting
129 {
130 protected function geolocAddress(array &$address, &$success)
131 {
132 require_once 'geoloc.inc.php';
133 $success = true;
134 unset($address['geoloc']);
135 unset($address['geoloc_cityid']);
136 if (@$address['parsevalid']
137 || (@$address['text'] && @$address['changed'])
138 || (@$address['text'] && !@$address['cityid'])) {
139 $address = array_merge($address, empty_address());
140 $new = get_address_infos(@$address['text']);
141 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
142 || (@$address['parsevalid'] && @$address['cityid'])) {
143 $address = array_merge($address, $new);
144 $address['checked'] = true;
145 } else if (@$address['parsevalid']) {
146 $address = array_merge($address, cut_address(@$address['text']));
147 $address['checked'] = true;
148 $mailer = new PlMailer('geoloc/mail_geoloc.tpl');
149 $mailer->assign('text', get_address_text($address));
150 $mailer->assign('geoloc', $geotxt);
151 $mailer->send();
152 } else if (@$address['changed'] || !@$address['checked']) {
153 $success = false;
154 $address = array_merge($address, cut_address(@$address['text']));
155 $address['checked'] = false;
156 $address['geoloc'] = $geotxt;
157 $address['geoloc_cityid'] = $new['cityid'];
158 } else {
159 $address = array_merge($address, cut_address(@$address['text']));
160 $address['checked'] = true;
161 }
162 }
163 $address['text'] = get_address_text($address);
164 }
165 }
166
167
168 abstract class ProfilePage implements PlWizardPage
169 {
170 protected $wizard;
171 protected $pg_template;
172 protected $settings = array(); // A set ProfileSetting objects
173 protected $errors = array(); // A set of boolean with the value check errors
174 protected $changed = array(); // A set of boolean indicating wether the value has been changed
175
176 public $orig = array();
177 public $values = array();
178
179 public function __construct(PlWizard &$wiz)
180 {
181 $this->wizard =& $wiz;
182 }
183
184 protected function _fetchData()
185 {
186 }
187
188 protected function fetchData()
189 {
190 if (count($this->orig) > 0) {
191 $this->values = $this->orig;
192 return;
193 }
194
195 $this->_fetchData();
196 foreach ($this->settings as $field=>&$setting) {
197 $success = false;
198 if (!is_null($setting)) {
199 $this->values[$field] = $setting->value($this, $field, null, $success);
200 } else if (!isset($this->values[$field])) {
201 $this->values[$field] = S::v($field);
202 }
203 $this->errors[$field] = false;
204 }
205 $this->orig = $this->values;
206 }
207
208 protected function _saveData()
209 {
210 }
211
212 protected function saveData()
213 {
214 foreach ($this->settings as $field=>&$setting) {
215 if (!is_null($setting) && $this->changed[$field]) {
216 $setting->save($this, $field, $this->values[$field]);
217 }
218 }
219 $this->_saveData();
220
221 // Update the last modification date
222 XDB::execute('REPLACE INTO user_changes
223 SET user_id = {?}', S::v('uid'));
224 global $platal;
225 $log =& $_SESSION['log'];
226 $log->log('profil', $platal->pl_self(1));
227 }
228
229 protected function checkChanges()
230 {
231 $newvalues = $this->values;
232 $this->values = array();
233 $this->fetchData();
234 $this->values = $newvalues;
235 $changes = false;
236 foreach ($this->settings as $field=>&$setting) {
237 if ($this->orig[$field] != $this->values[$field]) {
238 $this->changed[$field] = true;
239 $changes = true;
240 } else {
241 $this->changed[$field] = false;
242 }
243 }
244 return $changes;
245 }
246
247 protected function markChange()
248 {
249 }
250
251 public function template()
252 {
253 return 'profile/base.tpl';
254 }
255
256 protected function _prepare(PlatalPage &$page, $id)
257 {
258 }
259
260 public function prepare(PlatalPage &$page, $id)
261 {
262 if (count($this->values) == 0) {
263 $this->fetchData();
264 }
265 foreach ($this->values as $field=>&$value) {
266 $page->assign($field, $value);
267 }
268 $this->_prepare($page, $id);
269 $page->assign('profile_page', $this->pg_template);
270 $page->assign('errors', $this->errors);
271 }
272
273 public function process()
274 {
275 $global_success = true;
276 $this->fetchData();
277 foreach ($this->settings as $field=>&$setting) {
278 $success = false;
279 if (!is_null($setting)) {
280 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
281 } else {
282 $success = true;
283 $this->values[$field] = Post::v($field, '');
284 }
285 $this->errors[$field] = !$success;
286 $global_success = $global_success && $success;
287 }
288 if ($global_success) {
289 if ($this->checkChanges()) {
290 $this->saveData();
291 $this->markChange();
292 }
293 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
294 }
295 global $page;
296 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations "
297 . "de ton profil et de revalider ta demande");
298 return PlWizard::CURRENT_PAGE;
299 }
300 }
301
302 require_once dirname(__FILE__) . '/general.inc.php';
303 require_once dirname(__FILE__) . '/addresses.inc.php';
304 require_once dirname(__FILE__) . '/groups.inc.php';
305 require_once dirname(__FILE__) . '/decos.inc.php';
306 require_once dirname(__FILE__) . '/jobs.inc.php';
307 require_once dirname(__FILE__) . '/skills.inc.php';
308 require_once dirname(__FILE__) . '/mentor.inc.php';
309
310 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
311 ?>