Ooops, cf ad75c91.
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
f118f685
FB
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 }
37d44b3b
FB
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
fd38b30e 55 if (!$success) {
d7610c35
FB
56 Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
57 . ' et ne pas contenir de caractères interdits');
fd38b30e
FB
58 }
59 return $value;
60 }
61}
62
37d44b3b
FB
63class ProfileEmail 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 }
70 $value = trim($value);
37d44b3b
FB
71 $success = empty($value) || isvalid_email($value);
72 if (!$success) {
d7610c35 73 Platal::page()->trigError('Adresse Email invalide');
37d44b3b
FB
74 }
75 return $value;
76 }
77}
78
79
fd38b30e
FB
80class ProfileTel extends ProfileNoSave
81{
82 public function value(ProfilePage &$page, $field, $value, &$success)
83 {
84 if (is_null($value)) {
85 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
86 }
a619333a 87 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
fd38b30e 88 if (!$success) {
d7610c35 89 Platal::page()->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
fd38b30e
FB
90 }
91 return $value;
92 }
93}
94
93553cea
FB
95class ProfilePub extends ProfileNoSave
96{
97 public function value(ProfilePage &$page, $field, $value, &$success)
98 {
99 $success = true;
100 if (is_null($value)) {
101 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
102 }
103 if (is_null($value) || !$value) {
104 $value = 'private';
105 } else if ($value == 'on') { // Checkbox
106 $value = 'public';
107 }
108 return $value;
109 }
110}
111
576777d7
FB
112class ProfileBool extends ProfileNoSave
113{
114 public function value(ProfilePage &$page, $field, $value, &$success)
115 {
116 $success = true;
117 if (is_null($value)) {
c24a06aa 118 $value = @$page->values[$field];
576777d7 119 }
ee12da4e 120 return $value ? "1" : "";
576777d7
FB
121 }
122}
123
7bff4cb0
FB
124class ProfileDate extends ProfileNoSave
125{
126 public function value(ProfilePage &$page, $field, $value, &$success)
127 {
128 $success = true;
129 if (is_null($value)) {
130 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
131 } else {
132 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
133 if (!$success) {
d7610c35 134 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
135 } else {
136 $day = (int)$matches[1];
137 $month = (int)$matches[2];
138 $year = (int)$matches[3];
139 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
140 if (!$success) {
d7610c35 141 Platal::page()->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
142 }
143 }
144 }
145 return $value;
146 }
147}
148
37d44b3b
FB
149abstract class ProfileGeoloc implements ProfileSetting
150{
151 protected function geolocAddress(array &$address, &$success)
152 {
153 require_once 'geoloc.inc.php';
154 $success = true;
155 unset($address['geoloc']);
156 unset($address['geoloc_cityid']);
157 if (@$address['parsevalid']
158 || (@$address['text'] && @$address['changed'])
159 || (@$address['text'] && !@$address['cityid'])) {
160 $address = array_merge($address, empty_address());
161 $new = get_address_infos(@$address['text']);
1879c7b2
FB
162 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
163 || (@$address['parsevalid'] && @$address['cityid'])) {
37d44b3b 164 $address = array_merge($address, $new);
f93fb300 165 $address['checked'] = true;
1879c7b2
FB
166 } else if (@$address['parsevalid']) {
167 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 168 $address['checked'] = true;
b71f7275 169 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
f93fb300
FB
170 $mailer->assign('text', get_address_text($address));
171 $mailer->assign('geoloc', $geotxt);
172 $mailer->send();
173 } else if (@$address['changed'] || !@$address['checked']) {
37d44b3b
FB
174 $success = false;
175 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 176 $address['checked'] = false;
37d44b3b
FB
177 $address['geoloc'] = $geotxt;
178 $address['geoloc_cityid'] = $new['cityid'];
f93fb300
FB
179 } else {
180 $address = array_merge($address, cut_address(@$address['text']));
181 $address['checked'] = true;
37d44b3b
FB
182 }
183 }
de0485fd
FB
184 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
185 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
37d44b3b
FB
186 $address['text'] = get_address_text($address);
187 }
188}
189
190
fd38b30e 191abstract class ProfilePage implements PlWizardPage
f118f685
FB
192{
193 protected $wizard;
fd38b30e
FB
194 protected $pg_template;
195 protected $settings = array(); // A set ProfileSetting objects
93553cea 196 protected $errors = array(); // A set of boolean with the value check errors
576777d7 197 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 198 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 199
93553cea 200 public $orig = array();
fd38b30e 201 public $values = array();
f118f685
FB
202
203 public function __construct(PlWizard &$wiz)
204 {
205 $this->wizard =& $wiz;
206 }
207
7c2e0f0d
FB
208 protected function _fetchData()
209 {
210 }
211
fd38b30e
FB
212 protected function fetchData()
213 {
93553cea
FB
214 if (count($this->orig) > 0) {
215 $this->values = $this->orig;
216 return;
217 }
7c2e0f0d
FB
218
219 $this->_fetchData();
93553cea
FB
220 foreach ($this->settings as $field=>&$setting) {
221 $success = false;
222 if (!is_null($setting)) {
223 $this->values[$field] = $setting->value($this, $field, null, $success);
224 } else if (!isset($this->values[$field])) {
225 $this->values[$field] = S::v($field);
226 }
227 $this->errors[$field] = false;
228 }
229 $this->orig = $this->values;
fd38b30e
FB
230 }
231
7c2e0f0d
FB
232 protected function _saveData()
233 {
234 }
235
fd38b30e
FB
236 protected function saveData()
237 {
a2a1c2f2 238 require_once 'notifs.inc.php';
93553cea 239 foreach ($this->settings as $field=>&$setting) {
576777d7 240 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
241 $setting->save($this, $field, $this->values[$field]);
242 }
a2a1c2f2
FB
243 if ($this->changed[$field] && @$this->watched[$field]) {
244 register_profile_update(S::i('uid'), $field);
245 }
93553cea 246 }
7c2e0f0d 247 $this->_saveData();
576777d7
FB
248
249 // Update the last modification date
250 XDB::execute('REPLACE INTO user_changes
251 SET user_id = {?}', S::v('uid'));
3ebe4a0a 252 if (!S::has('suid')) {
3ebe4a0a
FB
253 register_watch_op(S::i('uid'), WATCH_FICHE);
254 }
576777d7 255 global $platal;
732e5855 256 S::logger()->log('profil', $platal->pl_self(1));
93553cea
FB
257 }
258
259 protected function checkChanges()
260 {
261 $newvalues = $this->values;
262 $this->values = array();
263 $this->fetchData();
264 $this->values = $newvalues;
576777d7 265 $changes = false;
93553cea
FB
266 foreach ($this->settings as $field=>&$setting) {
267 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
268 $this->changed[$field] = true;
269 $changes = true;
270 } else {
271 $this->changed[$field] = false;
93553cea
FB
272 }
273 }
576777d7 274 return $changes;
93553cea
FB
275 }
276
277 protected function markChange()
278 {
fd38b30e
FB
279 }
280
f118f685
FB
281 public function template()
282 {
fd38b30e 283 return 'profile/base.tpl';
f118f685
FB
284 }
285
04334c61 286 protected function _prepare(PlPage &$page, $id)
7c2e0f0d
FB
287 {
288 }
289
04334c61 290 public function prepare(PlPage &$page, $id)
f118f685 291 {
fd38b30e
FB
292 if (count($this->values) == 0) {
293 $this->fetchData();
fd38b30e
FB
294 }
295 foreach ($this->values as $field=>&$value) {
296 $page->assign($field, $value);
297 }
7c2e0f0d 298 $this->_prepare($page, $id);
fd38b30e 299 $page->assign('profile_page', $this->pg_template);
93553cea 300 $page->assign('errors', $this->errors);
f118f685
FB
301 }
302
303 public function process()
304 {
fd38b30e
FB
305 $global_success = true;
306 $this->fetchData();
307 foreach ($this->settings as $field=>&$setting) {
308 $success = false;
93553cea 309 if (!is_null($setting)) {
85cc366b 310 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
311 } else {
312 $success = true;
85cc366b 313 $this->values[$field] = Post::v($field, '');
93553cea
FB
314 }
315 $this->errors[$field] = !$success;
fd38b30e
FB
316 $global_success = $global_success && $success;
317 }
318 if ($global_success) {
93553cea
FB
319 if ($this->checkChanges()) {
320 $this->saveData();
321 $this->markChange();
fd38b30e 322 }
93553cea 323 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e 324 }
d7610c35
FB
325 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
326 . "de ton profil et de revalider ta demande");
f118f685
FB
327 return PlWizard::CURRENT_PAGE;
328 }
329}
330
fd38b30e 331require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 332require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 333require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 334require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 335require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 336require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 337require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
338
339// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
340?>