Refactoring of the wizard based profile edit with a new structure that
[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 ProfileFixed extends ProfileNoSave
47 {
48 public function value(ProfilePage &$page, $field, $value, &$success)
49 {
50 $success = true;
51 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
52 }
53 }
54
55 class ProfileWeb extends ProfileNoSave
56 {
57 public function value(ProfilePage &$page, $field, $value, &$success)
58 {
59 if (is_null($value)) {
60 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
61 }
62 $success = preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
63 if (!$success) {
64 global $page;
65 $page->trig('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
66 . ' et ne pas contenir de caractères interdits');
67 }
68 return $value;
69 }
70 }
71
72 class ProfileTel extends ProfileNoSave
73 {
74 public function value(ProfilePage &$page, $field, $value, &$success)
75 {
76 if (is_null($value)) {
77 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
78 }
79 $success = strlen(strtok($value, '<>{}@&#~\/:;?,!§*_`[]|%$^=')) < strlen($value);
80 if (!$success) {
81 global $page;
82 $page->trig('Le numéro de téléphone contient un caractère interdit.');
83 }
84 return $value;
85 }
86 }
87
88 abstract class ProfilePage implements PlWizardPage
89 {
90 protected $wizard;
91 protected $pg_template;
92 protected $settings = array(); // A set ProfileSetting objects
93
94 public $values = array();
95
96 public function __construct(PlWizard &$wiz)
97 {
98 $this->wizard =& $wiz;
99 }
100
101 protected function fetchData()
102 {
103 }
104
105 protected function saveData()
106 {
107 }
108
109 public function template()
110 {
111 return 'profile/base.tpl';
112 }
113
114 public function prepare(PlatalPage &$page)
115 {
116 if (count($this->values) == 0) {
117 $this->fetchData();
118 foreach ($this->settings as $field=>&$setting) {
119 $success = false;
120 $this->values[$field] = $setting->value($this, $field, null, $success);
121 }
122 }
123 foreach ($this->values as $field=>&$value) {
124 $page->assign($field, $value);
125 }
126 $page->assign('profile_page', $this->pg_template);
127 }
128
129 public function process()
130 {
131 $global_success = true;
132 $this->fetchData();
133 foreach ($this->settings as $field=>&$setting) {
134 $success = false;
135 $this->values[$field] = $setting->value($this, $field, Post::v($field), $success);
136 $global_success = $global_success && $success;
137 }
138 if ($global_success) {
139 foreach ($this->settings as $field=>&$setting) {
140 $setting->save($this, $field, $this->values[$field]);
141 }
142 $this->saveData();
143 return Post::has('valid_and_next') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
144 }
145 global $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;
149 }
150 }
151
152 require_once dirname(__FILE__) . '/general.inc.php';
153
154 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
155 ?>