Working HTML part of the job tab
[platal.git] / modules / profile / page.inc.php
CommitLineData
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
22interface 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
41abstract class ProfileNoSave implements ProfileSetting
42{
43 public function save(ProfilePage &$page, $field, $new_value) { }
44}
45
fd38b30e
FB
46class 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 }
c24a06aa 53 $success = !trim($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
fd38b30e
FB
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
63class 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 }
a619333a 70 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
fd38b30e
FB
71 if (!$success) {
72 global $page;
a619333a 73 $page->trig('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
fd38b30e
FB
74 }
75 return $value;
76 }
77}
78
93553cea
FB
79class 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
576777d7
FB
96class ProfileBool extends ProfileNoSave
97{
98 public function value(ProfilePage &$page, $field, $value, &$success)
99 {
100 $success = true;
101 if (is_null($value)) {
c24a06aa 102 $value = @$page->values[$field];
576777d7
FB
103 }
104 return $value ? 1 : 0;
105 }
106}
107
fd38b30e 108abstract class ProfilePage implements PlWizardPage
f118f685
FB
109{
110 protected $wizard;
fd38b30e
FB
111 protected $pg_template;
112 protected $settings = array(); // A set ProfileSetting objects
93553cea 113 protected $errors = array(); // A set of boolean with the value check errors
576777d7 114 protected $changed = array(); // A set of boolean indicating wether the value has been changed
fd38b30e 115
93553cea 116 public $orig = array();
fd38b30e 117 public $values = array();
f118f685
FB
118
119 public function __construct(PlWizard &$wiz)
120 {
121 $this->wizard =& $wiz;
122 }
123
fd38b30e
FB
124 protected function fetchData()
125 {
93553cea
FB
126 if (count($this->orig) > 0) {
127 $this->values = $this->orig;
128 return;
129 }
130 foreach ($this->settings as $field=>&$setting) {
131 $success = false;
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);
136 }
137 $this->errors[$field] = false;
138 }
139 $this->orig = $this->values;
fd38b30e
FB
140 }
141
142 protected function saveData()
143 {
93553cea 144 foreach ($this->settings as $field=>&$setting) {
576777d7 145 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
146 $setting->save($this, $field, $this->values[$field]);
147 }
148 }
576777d7
FB
149
150 // Update the last modification date
151 XDB::execute('REPLACE INTO user_changes
152 SET user_id = {?}', S::v('uid'));
153 global $platal;
154 $log =& $_SESSION['log'];
155 $log->log('profil', $platal->pl_self(1));
93553cea
FB
156 }
157
158 protected function checkChanges()
159 {
160 $newvalues = $this->values;
161 $this->values = array();
162 $this->fetchData();
163 $this->values = $newvalues;
576777d7 164 $changes = false;
93553cea
FB
165 foreach ($this->settings as $field=>&$setting) {
166 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
167 $this->changed[$field] = true;
168 $changes = true;
169 } else {
170 $this->changed[$field] = false;
93553cea
FB
171 }
172 }
576777d7 173 return $changes;
93553cea
FB
174 }
175
176 protected function markChange()
177 {
fd38b30e
FB
178 }
179
f118f685
FB
180 public function template()
181 {
fd38b30e 182 return 'profile/base.tpl';
f118f685
FB
183 }
184
185 public function prepare(PlatalPage &$page)
186 {
fd38b30e
FB
187 if (count($this->values) == 0) {
188 $this->fetchData();
fd38b30e
FB
189 }
190 foreach ($this->values as $field=>&$value) {
191 $page->assign($field, $value);
192 }
193 $page->assign('profile_page', $this->pg_template);
93553cea 194 $page->assign('errors', $this->errors);
f118f685
FB
195 }
196
197 public function process()
198 {
fd38b30e
FB
199 $global_success = true;
200 $this->fetchData();
201 foreach ($this->settings as $field=>&$setting) {
202 $success = false;
93553cea 203 if (!is_null($setting)) {
85cc366b 204 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
205 } else {
206 $success = true;
85cc366b 207 $this->values[$field] = Post::v($field, '');
93553cea
FB
208 }
209 $this->errors[$field] = !$success;
fd38b30e
FB
210 $global_success = $global_success && $success;
211 }
212 if ($global_success) {
93553cea
FB
213 if ($this->checkChanges()) {
214 $this->saveData();
215 $this->markChange();
fd38b30e 216 }
93553cea 217 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e
FB
218 }
219 global $page;
93553cea 220 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations "
fd38b30e 221 . "de ton profil et de revalider ta demande");
f118f685
FB
222 return PlWizard::CURRENT_PAGE;
223 }
224}
225
fd38b30e 226require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 227require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 228require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 229require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 230require_once dirname(__FILE__) . '/jobs.inc.php';
f118f685
FB
231
232// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
233?>