Uses PlPage instead of PlatalPage (again).
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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
92446a53
PC
79class ProfileNumber extends ProfileNoSave
80{
81 public function value(ProfilePage &$page, $field, $value, &$success)
82 {
83 if (is_null($value)) {
84 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
85 }
86 $value = trim($value);
87 $success = empty($value) || is_numeric($value);
88 if (!$success) {
89 global $page;
90 $page->trigError('Numéro invalide');
91 }
92 return $value;
93 }
94}
95
37d44b3b 96
fd38b30e
FB
97class ProfileTel extends ProfileNoSave
98{
99 public function value(ProfilePage &$page, $field, $value, &$success)
100 {
101 if (is_null($value)) {
102 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
103 }
5e4417a9
GB
104 require_once('profil.func.inc.php');
105 $value = format_phone_number($value);
106 if($value == '') {
107 $success = true;
108 return $value;
109 }
110 $value = format_display_number($value,$error);
111 $success = !$error;
fd38b30e
FB
112 if (!$success) {
113 global $page;
5e4417a9 114 $page->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
fd38b30e
FB
115 }
116 return $value;
117 }
118}
119
bde2be3b
GB
120class ProfilePhones implements ProfileSetting
121{
122 private $tel;
123 private $pub;
124 protected $link_type;
125 protected $link_id;
126
127 public function __construct($type, $id)
128 {
129 $this->tel = new ProfileTel();
130 $this->pub = new ProfilePub();
131 $this->link_type = $type;
132 $this->link_id = $id;
133 }
134
135 public function value(ProfilePage &$page, $field, $value, &$success)
136 {
137 $success = true;
138 if (is_null($value)) {
139 $value = isset($page->values[$field]) ? $page->values[$field] : array();
140 }
141 if (!is_array($value)) {
142 $value = array();
143 }
144 foreach ($value as $key=>&$phone) {
145 if (@$phone['removed']) {
146 unset($value[$key]);
147 } else {
148 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
149 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
150 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
151 $phone['type'] = 'fixed';
152 $s = false;
153 }
154 if (!$s) {
155 $phone['error'] = true;
156 $success = false;
157 }
158 if (!isset($phone['comment'])) {
159 $phone['comment'] = '';
160 }
161 }
162 }
163 return $value;
164 }
165
166 private function saveTel($telid, array &$phone)
167 {
168 if ($phone['tel'] != '') {
169 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
170 search_tel, display_tel, pub, comment)
171 VALUES ({?}, {?}, {?}, {?}, {?},
172 {?}, {?}, {?}, {?})",
173 S::i('uid'), $this->link_type, $this->link_id, $telid, $phone['type'],
174 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
175 }
176 }
177
178 public function save(ProfilePage &$page, $field, $value)
179 {
180 XDB::execute("DELETE FROM profile_phones
181 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
182 S::i('uid'), $this->link_type, $this->link_id);
183 $this->saveTels($field, $value);
184 }
185
186 //Only saves phones without a delete operation
187 public function saveTels($field, $value)
188 {
189 foreach ($value as $telid=>&$phone) {
190 $this->saveTel($telid, $phone);
191 }
192 }
193}
194
93553cea
FB
195class ProfilePub extends ProfileNoSave
196{
197 public function value(ProfilePage &$page, $field, $value, &$success)
198 {
199 $success = true;
200 if (is_null($value)) {
201 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
202 }
203 if (is_null($value) || !$value) {
204 $value = 'private';
205 } else if ($value == 'on') { // Checkbox
206 $value = 'public';
207 }
208 return $value;
209 }
210}
211
576777d7
FB
212class ProfileBool extends ProfileNoSave
213{
214 public function value(ProfilePage &$page, $field, $value, &$success)
215 {
216 $success = true;
217 if (is_null($value)) {
c24a06aa 218 $value = @$page->values[$field];
576777d7 219 }
ee12da4e 220 return $value ? "1" : "";
576777d7
FB
221 }
222}
223
7bff4cb0
FB
224class ProfileDate extends ProfileNoSave
225{
226 public function value(ProfilePage &$page, $field, $value, &$success)
227 {
228 $success = true;
229 if (is_null($value)) {
230 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
231 } else {
232 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
233 if (!$success) {
d7610c35 234 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
235 } else {
236 $day = (int)$matches[1];
237 $month = (int)$matches[2];
238 $year = (int)$matches[3];
239 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
240 if (!$success) {
d7610c35 241 Platal::page()->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
242 }
243 }
244 }
245 return $value;
246 }
247}
248
37d44b3b
FB
249abstract class ProfileGeoloc implements ProfileSetting
250{
251 protected function geolocAddress(array &$address, &$success)
252 {
253 require_once 'geoloc.inc.php';
254 $success = true;
255 unset($address['geoloc']);
256 unset($address['geoloc_cityid']);
257 if (@$address['parsevalid']
258 || (@$address['text'] && @$address['changed'])
259 || (@$address['text'] && !@$address['cityid'])) {
260 $address = array_merge($address, empty_address());
261 $new = get_address_infos(@$address['text']);
1879c7b2
FB
262 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
263 || (@$address['parsevalid'] && @$address['cityid'])) {
37d44b3b 264 $address = array_merge($address, $new);
f93fb300 265 $address['checked'] = true;
1879c7b2
FB
266 } else if (@$address['parsevalid']) {
267 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 268 $address['checked'] = true;
b71f7275 269 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
f93fb300
FB
270 $mailer->assign('text', get_address_text($address));
271 $mailer->assign('geoloc', $geotxt);
272 $mailer->send();
273 } else if (@$address['changed'] || !@$address['checked']) {
37d44b3b
FB
274 $success = false;
275 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 276 $address['checked'] = false;
37d44b3b
FB
277 $address['geoloc'] = $geotxt;
278 $address['geoloc_cityid'] = $new['cityid'];
f93fb300
FB
279 } else {
280 $address = array_merge($address, cut_address(@$address['text']));
281 $address['checked'] = true;
37d44b3b
FB
282 }
283 }
de0485fd
FB
284 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
285 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
37d44b3b
FB
286 $address['text'] = get_address_text($address);
287 }
288}
289
290
fd38b30e 291abstract class ProfilePage implements PlWizardPage
f118f685
FB
292{
293 protected $wizard;
fd38b30e
FB
294 protected $pg_template;
295 protected $settings = array(); // A set ProfileSetting objects
93553cea 296 protected $errors = array(); // A set of boolean with the value check errors
576777d7 297 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 298 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 299
93553cea 300 public $orig = array();
fd38b30e 301 public $values = array();
f118f685
FB
302
303 public function __construct(PlWizard &$wiz)
304 {
305 $this->wizard =& $wiz;
306 }
307
7c2e0f0d
FB
308 protected function _fetchData()
309 {
310 }
311
fd38b30e
FB
312 protected function fetchData()
313 {
93553cea
FB
314 if (count($this->orig) > 0) {
315 $this->values = $this->orig;
316 return;
317 }
7c2e0f0d
FB
318
319 $this->_fetchData();
93553cea
FB
320 foreach ($this->settings as $field=>&$setting) {
321 $success = false;
322 if (!is_null($setting)) {
323 $this->values[$field] = $setting->value($this, $field, null, $success);
324 } else if (!isset($this->values[$field])) {
325 $this->values[$field] = S::v($field);
326 }
327 $this->errors[$field] = false;
328 }
329 $this->orig = $this->values;
fd38b30e
FB
330 }
331
7c2e0f0d
FB
332 protected function _saveData()
333 {
334 }
335
fd38b30e
FB
336 protected function saveData()
337 {
a2a1c2f2 338 require_once 'notifs.inc.php';
93553cea 339 foreach ($this->settings as $field=>&$setting) {
576777d7 340 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
341 $setting->save($this, $field, $this->values[$field]);
342 }
a2a1c2f2
FB
343 if ($this->changed[$field] && @$this->watched[$field]) {
344 register_profile_update(S::i('uid'), $field);
345 }
93553cea 346 }
7c2e0f0d 347 $this->_saveData();
576777d7
FB
348
349 // Update the last modification date
350 XDB::execute('REPLACE INTO user_changes
351 SET user_id = {?}', S::v('uid'));
3ebe4a0a 352 if (!S::has('suid')) {
3ebe4a0a
FB
353 register_watch_op(S::i('uid'), WATCH_FICHE);
354 }
576777d7
FB
355 global $platal;
356 $log =& $_SESSION['log'];
732e5855 357 S::logger()->log('profil', $platal->pl_self(1));
93553cea
FB
358 }
359
360 protected function checkChanges()
361 {
362 $newvalues = $this->values;
363 $this->values = array();
364 $this->fetchData();
365 $this->values = $newvalues;
576777d7 366 $changes = false;
93553cea
FB
367 foreach ($this->settings as $field=>&$setting) {
368 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
369 $this->changed[$field] = true;
370 $changes = true;
371 } else {
372 $this->changed[$field] = false;
93553cea
FB
373 }
374 }
576777d7 375 return $changes;
93553cea
FB
376 }
377
378 protected function markChange()
379 {
fd38b30e
FB
380 }
381
f118f685
FB
382 public function template()
383 {
fd38b30e 384 return 'profile/base.tpl';
f118f685
FB
385 }
386
04334c61 387 protected function _prepare(PlPage &$page, $id)
7c2e0f0d
FB
388 {
389 }
390
04334c61 391 public function prepare(PlPage &$page, $id)
f118f685 392 {
fd38b30e
FB
393 if (count($this->values) == 0) {
394 $this->fetchData();
fd38b30e
FB
395 }
396 foreach ($this->values as $field=>&$value) {
397 $page->assign($field, $value);
398 }
7c2e0f0d 399 $this->_prepare($page, $id);
fd38b30e 400 $page->assign('profile_page', $this->pg_template);
93553cea 401 $page->assign('errors', $this->errors);
f118f685
FB
402 }
403
404 public function process()
405 {
fd38b30e
FB
406 $global_success = true;
407 $this->fetchData();
408 foreach ($this->settings as $field=>&$setting) {
409 $success = false;
93553cea 410 if (!is_null($setting)) {
85cc366b 411 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
412 } else {
413 $success = true;
85cc366b 414 $this->values[$field] = Post::v($field, '');
93553cea
FB
415 }
416 $this->errors[$field] = !$success;
fd38b30e
FB
417 $global_success = $global_success && $success;
418 }
419 if ($global_success) {
93553cea
FB
420 if ($this->checkChanges()) {
421 $this->saveData();
422 $this->markChange();
fd38b30e 423 }
93553cea 424 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e 425 }
d7610c35
FB
426 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
427 . "de ton profil et de revalider ta demande");
f118f685
FB
428 return PlWizard::CURRENT_PAGE;
429 }
430}
431
fd38b30e 432require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 433require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 434require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 435require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 436require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 437require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 438require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
439
440// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
441?>