2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 interface ProfileSetting
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.
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
34 public function value(ProfilePage
&$page, $field, $value, &$success);
36 /** Save the new value for the given field.
38 public function save(ProfilePage
&$page, $field, $new_value);
41 abstract class ProfileNoSave
implements ProfileSetting
43 public function save(ProfilePage
&$page, $field, $new_value) { }
46 class ProfileWeb
extends ProfileNoSave
48 public function value(ProfilePage
&$page, $field, $value, &$success)
50 if (is_null($value)) {
51 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
53 $value = trim($value);
54 $success = empty($value) ||
preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
57 $page->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
58 . ' et ne pas contenir de caractères interdits');
64 class ProfileEmail
extends ProfileNoSave
66 public function value(ProfilePage
&$page, $field, $value, &$success)
68 if (is_null($value)) {
69 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
71 $value = trim($value);
72 require_once 'xorg.misc.inc.php';
73 $success = empty($value) ||
isvalid_email($value);
76 $page->trigError('Adresse Email invalide');
82 class ProfileNumber
extends ProfileNoSave
84 public function value(ProfilePage
&$page, $field, $value, &$success)
86 if (is_null($value)) {
87 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
89 $value = trim($value);
90 $success = empty($value) ||
is_numeric($value);
93 $page->trigError('Numéro invalide');
100 class ProfileTel
extends ProfileNoSave
102 public function value(ProfilePage
&$page, $field, $value, &$success)
104 if (is_null($value)) {
105 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
107 require_once('profil.func.inc.php');
108 $value = format_phone_number($value);
113 $value = format_display_number($value,$error);
117 $page->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
123 class ProfilePhones
implements ProfileSetting
127 protected $link_type;
130 public function __construct($type, $id)
132 $this->tel
= new ProfileTel();
133 $this->pub
= new ProfilePub();
134 $this->link_type
= $type;
135 $this->link_id
= $id;
138 public function value(ProfilePage
&$page, $field, $value, &$success)
141 if (is_null($value)) {
142 $value = isset($page->values
[$field]) ?
$page->values
[$field] : array();
144 if (!is_array($value)) {
147 foreach ($value as $key=>&$phone) {
148 if (@$phone['removed']) {
151 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
152 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
153 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
154 $phone['type'] = 'fixed';
158 $phone['error'] = true
;
161 if (!isset($phone['comment'])) {
162 $phone['comment'] = '';
169 private function saveTel($telid, array &$phone)
171 if ($phone['tel'] != '') {
172 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
173 search_tel, display_tel, pub, comment)
174 VALUES ({?}, {?}, {?}, {?}, {?},
175 {?}, {?}, {?}, {?})",
176 S
::i('uid'), $this->link_type
, $this->link_id
, $telid, $phone['type'],
177 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
181 public function save(ProfilePage
&$page, $field, $value)
183 XDB
::execute("DELETE FROM profile_phones
184 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
185 S
::i('uid'), $this->link_type
, $this->link_id
);
186 $this->saveTels($field, $value);
189 //Only saves phones without a delete operation
190 public function saveTels($field, $value)
192 foreach ($value as $telid=>&$phone) {
193 $this->saveTel($telid, $phone);
198 class ProfilePub
extends ProfileNoSave
200 public function value(ProfilePage
&$page, $field, $value, &$success)
203 if (is_null($value)) {
204 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
206 if (is_null($value) ||
!$value) {
208 } else if ($value == 'on') { // Checkbox
215 class ProfileBool
extends ProfileNoSave
217 public function value(ProfilePage
&$page, $field, $value, &$success)
220 if (is_null($value)) {
221 $value = @$page->values
[$field];
223 return $value ?
"1" : "";
227 class ProfileDate
extends ProfileNoSave
229 public function value(ProfilePage
&$page, $field, $value, &$success)
232 if (is_null($value)) {
233 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
235 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
238 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
240 $day = (int)$matches[1];
241 $month = (int)$matches[2];
242 $year = (int)$matches[3];
243 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
246 $page->trigError("La date n'a pas une valeur valide");
254 abstract class ProfileGeoloc
implements ProfileSetting
256 protected function geolocAddress(array &$address, &$success)
258 require_once 'geoloc.inc.php';
260 unset($address['geoloc']);
261 unset($address['geoloc_cityid']);
262 if (@$address['parsevalid']
263 ||
(@$address['text'] && @$address['changed'])
264 ||
(@$address['text'] && !@$address['cityid'])) {
265 $address = array_merge($address, empty_address());
266 $new = get_address_infos(@$address['text']);
267 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
268 ||
(@$address['parsevalid'] && @$address['cityid'])) {
269 $address = array_merge($address, $new);
270 $address['checked'] = true
;
271 } else if (@$address['parsevalid']) {
272 $address = array_merge($address, cut_address(@$address['text']));
273 $address['checked'] = true
;
274 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
275 $mailer->assign('text', get_address_text($address));
276 $mailer->assign('geoloc', $geotxt);
278 } else if (@$address['changed'] ||
!@$address['checked']) {
280 $address = array_merge($address, cut_address(@$address['text']));
281 $address['checked'] = false
;
282 $address['geoloc'] = $geotxt;
283 $address['geoloc_cityid'] = $new['cityid'];
285 $address = array_merge($address, cut_address(@$address['text']));
286 $address['checked'] = true
;
289 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
290 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
291 $address['text'] = get_address_text($address);
296 abstract class ProfilePage
implements PlWizardPage
299 protected $pg_template;
300 protected $settings = array(); // A set ProfileSetting objects
301 protected $errors = array(); // A set of boolean with the value check errors
302 protected $changed = array(); // A set of boolean indicating wether the value has been changed
303 protected $watched = array(); // A set of boolean indicating the fields that are watched
305 public $orig = array();
306 public $values = array();
308 public function __construct(PlWizard
&$wiz)
310 $this->wizard
=& $wiz;
313 protected function _fetchData()
317 protected function fetchData()
319 if (count($this->orig
) > 0) {
320 $this->values
= $this->orig
;
325 foreach ($this->settings
as $field=>&$setting) {
327 if (!is_null($setting)) {
328 $this->values
[$field] = $setting->value($this, $field, null
, $success);
329 } else if (!isset($this->values
[$field])) {
330 $this->values
[$field] = S
::v($field);
332 $this->errors
[$field] = false
;
334 $this->orig
= $this->values
;
337 protected function _saveData()
341 protected function saveData()
343 require_once 'notifs.inc.php';
344 foreach ($this->settings
as $field=>&$setting) {
345 if (!is_null($setting) && $this->changed
[$field]) {
346 $setting->save($this, $field, $this->values
[$field]);
348 if ($this->changed
[$field] && @$this->watched
[$field]) {
349 register_profile_update(S
::i('uid'), $field);
354 // Update the last modification date
355 XDB
::execute('REPLACE INTO user_changes
356 SET user_id = {?}', S
::v('uid'));
357 if (!S
::has('suid')) {
358 register_watch_op(S
::i('uid'), WATCH_FICHE
);
361 $log =& $_SESSION['log'];
362 $log->log('profil', $platal->pl_self(1));
365 protected function checkChanges()
367 $newvalues = $this->values
;
368 $this->values
= array();
370 $this->values
= $newvalues;
372 foreach ($this->settings
as $field=>&$setting) {
373 if ($this->orig
[$field] != $this->values
[$field]) {
374 $this->changed
[$field] = true
;
377 $this->changed
[$field] = false
;
383 protected function markChange()
387 public function template()
389 return 'profile/base.tpl';
392 protected function _prepare(PlatalPage
&$page, $id)
396 public function prepare(PlatalPage
&$page, $id)
398 if (count($this->values
) == 0) {
401 foreach ($this->values
as $field=>&$value) {
402 $page->assign($field, $value);
404 $this->_prepare($page, $id);
405 $page->assign('profile_page', $this->pg_template
);
406 $page->assign('errors', $this->errors
);
409 public function process()
411 $global_success = true
;
413 foreach ($this->settings
as $field=>&$setting) {
415 if (!is_null($setting)) {
416 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
419 $this->values
[$field] = Post
::v($field, '');
421 $this->errors
[$field] = !$success;
422 $global_success = $global_success && $success;
424 if ($global_success) {
425 if ($this->checkChanges()) {
429 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
432 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
433 . "de ton profil et de revalider ta demande");
434 return PlWizard
::CURRENT_PAGE
;
438 require_once dirname(__FILE__
) . '/general.inc.php';
439 require_once dirname(__FILE__
) . '/addresses.inc.php';
440 require_once dirname(__FILE__
) . '/groups.inc.php';
441 require_once dirname(__FILE__
) . '/decos.inc.php';
442 require_once dirname(__FILE__
) . '/jobs.inc.php';
443 require_once dirname(__FILE__
) . '/skills.inc.php';
444 require_once dirname(__FILE__
) . '/mentor.inc.php';
446 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: