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);
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');
63 class ProfileEmail
extends ProfileNoSave
65 public function value(ProfilePage
&$page, $field, $value, &$success)
67 if (is_null($value)) {
68 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
70 $value = trim($value);
71 $success = empty($value) ||
isvalid_email($value);
73 Platal
::page()->trigError('Adresse Email invalide');
79 class ProfileNumber
extends ProfileNoSave
81 public function value(ProfilePage
&$page, $field, $value, &$success)
83 if (is_null($value)) {
84 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
86 $value = trim($value);
87 $success = empty($value) ||
is_numeric($value);
89 Platal
::page()->trigError('Numéro invalide');
96 class ProfileTel
extends ProfileNoSave
98 public function value(ProfilePage
&$page, $field, $value, &$success)
100 if (is_null($value)) {
101 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
103 require_once('profil.func.inc.php');
104 $value = format_phone_number($value);
109 $value = format_display_number($value,$error);
112 Platal
::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
118 class ProfilePhones
implements ProfileSetting
122 protected $link_type;
125 public function __construct($type, $id)
127 $this->tel
= new ProfileTel();
128 $this->pub
= new ProfilePub();
129 $this->link_type
= $type;
130 $this->link_id
= $id;
133 public function value(ProfilePage
&$page, $field, $value, &$success)
136 if (is_null($value)) {
137 $value = isset($page->values
[$field]) ?
$page->values
[$field] : array();
139 if (!is_array($value)) {
142 foreach ($value as $key=>&$phone) {
143 if (@$phone['removed']) {
146 unset($phone['removed']);
147 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
148 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
149 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
150 $phone['type'] = 'fixed';
154 $phone['error'] = true
;
157 if (!isset($phone['comment'])) {
158 $phone['comment'] = '';
165 private function saveTel($telid, array &$phone)
167 if ($phone['tel'] != '') {
168 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
169 search_tel, display_tel, pub, comment)
170 VALUES ({?}, {?}, {?}, {?}, {?},
171 {?}, {?}, {?}, {?})",
172 S
::i('uid'), $this->link_type
, $this->link_id
, $telid, $phone['type'],
173 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
177 public function save(ProfilePage
&$page, $field, $value)
179 XDB
::execute("DELETE FROM profile_phones
180 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
181 S
::i('uid'), $this->link_type
, $this->link_id
);
182 $this->saveTels($field, $value);
185 //Only saves phones without a delete operation
186 public function saveTels($field, $value)
188 foreach ($value as $telid=>&$phone) {
189 $this->saveTel($telid, $phone);
194 class ProfilePub
extends ProfileNoSave
196 public function value(ProfilePage
&$page, $field, $value, &$success)
199 if (is_null($value)) {
200 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
202 if (is_null($value) ||
!$value) {
204 } else if ($value == 'on') { // Checkbox
211 class ProfileBool
extends ProfileNoSave
213 public function value(ProfilePage
&$page, $field, $value, &$success)
216 if (is_null($value)) {
217 $value = @$page->values
[$field];
219 return $value ?
"1" : "";
223 class ProfileDate
extends ProfileNoSave
225 public function value(ProfilePage
&$page, $field, $value, &$success)
228 if (is_null($value)) {
229 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
231 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
233 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
235 $day = (int)$matches[1];
236 $month = (int)$matches[2];
237 $year = (int)$matches[3];
238 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
240 Platal
::page()->trigError("La date n'a pas une valeur valide");
248 abstract class ProfileGeoloc
implements ProfileSetting
250 protected function geolocAddress(array &$address, &$success)
252 require_once 'geoloc.inc.php';
254 unset($address['geoloc']);
255 unset($address['geoloc_cityid']);
256 if (@$address['parsevalid']
257 ||
(@$address['text'] && @$address['changed'])
258 ||
(@$address['text'] && !@$address['cityid'])) {
259 $address = array_merge($address, empty_address());
260 $new = get_address_infos(@$address['text']);
261 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
262 ||
(@$address['parsevalid'] && @$address['cityid'])) {
263 $address = array_merge($address, $new);
264 $address['checked'] = true
;
265 } else if (@$address['parsevalid']) {
266 $address = array_merge($address, cut_address(@$address['text']));
267 $address['checked'] = true
;
268 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
269 $mailer->assign('text', get_address_text($address));
270 $mailer->assign('geoloc', $geotxt);
272 } else if (@$address['changed'] ||
!@$address['checked']) {
274 $address = array_merge($address, cut_address(@$address['text']));
275 $address['checked'] = false
;
276 $address['geoloc'] = $geotxt;
277 $address['geoloc_cityid'] = $new['cityid'];
279 $address = array_merge($address, cut_address(@$address['text']));
280 $address['checked'] = true
;
283 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
284 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
285 $address['text'] = get_address_text($address);
290 abstract class ProfilePage
implements PlWizardPage
293 protected $pg_template;
294 protected $settings = array(); // A set ProfileSetting objects
295 protected $errors = array(); // A set of boolean with the value check errors
296 protected $changed = array(); // A set of boolean indicating wether the value has been changed
297 protected $watched = array(); // A set of boolean indicating the fields that are watched
299 public $orig = array();
300 public $values = array();
302 public function __construct(PlWizard
&$wiz)
304 $this->wizard
=& $wiz;
307 protected function _fetchData()
311 protected function fetchData()
313 if (count($this->orig
) > 0) {
314 $this->values
= $this->orig
;
319 foreach ($this->settings
as $field=>&$setting) {
321 if (!is_null($setting)) {
322 $this->values
[$field] = $setting->value($this, $field, null
, $success);
323 } else if (!isset($this->values
[$field])) {
324 $this->values
[$field] = S
::v($field);
326 $this->errors
[$field] = false
;
328 $this->orig
= $this->values
;
331 protected function _saveData()
335 protected function saveData()
337 require_once 'notifs.inc.php';
338 foreach ($this->settings
as $field=>&$setting) {
339 if (!is_null($setting) && $this->changed
[$field]) {
340 $setting->save($this, $field, $this->values
[$field]);
342 if ($this->changed
[$field] && @$this->watched
[$field]) {
343 register_profile_update(S
::i('uid'), $field);
348 // Update the last modification date
349 XDB
::execute('REPLACE INTO user_changes
350 SET user_id = {?}', S
::v('uid'));
351 if (!S
::has('suid')) {
352 register_watch_op(S
::i('uid'), WATCH_FICHE
);
355 $log =& $_SESSION['log'];
356 S
::logger()->log('profil', $platal->pl_self(1));
359 protected function checkChanges()
361 $newvalues = $this->values
;
362 $this->values
= array();
364 $this->values
= $newvalues;
366 foreach ($this->settings
as $field=>&$setting) {
367 if ($this->orig
[$field] != $this->values
[$field]) {
368 $this->changed
[$field] = true
;
371 $this->changed
[$field] = false
;
377 protected function markChange()
381 public function template()
383 return 'profile/base.tpl';
386 protected function _prepare(PlPage
&$page, $id)
390 public function prepare(PlPage
&$page, $id)
392 if (count($this->values
) == 0) {
395 foreach ($this->values
as $field=>&$value) {
396 $page->assign($field, $value);
398 $this->_prepare($page, $id);
399 $page->assign('profile_page', $this->pg_template
);
400 $page->assign('errors', $this->errors
);
403 public function process()
405 $global_success = true
;
407 foreach ($this->settings
as $field=>&$setting) {
409 if (!is_null($setting)) {
410 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
413 $this->values
[$field] = Post
::v($field, '');
415 $this->errors
[$field] = !$success;
416 $global_success = $global_success && $success;
418 if ($global_success) {
419 if ($this->checkChanges()) {
423 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
425 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
426 . "de ton profil et de revalider ta demande");
427 return PlWizard
::CURRENT_PAGE
;
431 require_once dirname(__FILE__
) . '/general.inc.php';
432 require_once dirname(__FILE__
) . '/addresses.inc.php';
433 require_once dirname(__FILE__
) . '/groups.inc.php';
434 require_once dirname(__FILE__
) . '/decos.inc.php';
435 require_once dirname(__FILE__
) . '/jobs.inc.php';
436 require_once dirname(__FILE__
) . '/skills.inc.php';
437 require_once dirname(__FILE__
) . '/mentor.inc.php';
439 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: