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 ProfilePub
extends ProfileNoSave
125 public function value(ProfilePage
&$page, $field, $value, &$success)
128 if (is_null($value)) {
129 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
131 if (is_null($value) ||
!$value) {
133 } else if ($value == 'on') { // Checkbox
140 class ProfileBool
extends ProfileNoSave
142 public function value(ProfilePage
&$page, $field, $value, &$success)
145 if (is_null($value)) {
146 $value = @$page->values
[$field];
148 return $value ?
"1" : "";
152 class ProfileDate
extends ProfileNoSave
154 public function value(ProfilePage
&$page, $field, $value, &$success)
157 if (is_null($value)) {
158 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
160 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
163 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
165 $day = (int)$matches[1];
166 $month = (int)$matches[2];
167 $year = (int)$matches[3];
168 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
171 $page->trigError("La date n'a pas une valeur valide");
179 abstract class ProfileGeoloc
implements ProfileSetting
181 protected function geolocAddress(array &$address, &$success)
183 require_once 'geoloc.inc.php';
185 unset($address['geoloc']);
186 unset($address['geoloc_cityid']);
187 if (@$address['parsevalid']
188 ||
(@$address['text'] && @$address['changed'])
189 ||
(@$address['text'] && !@$address['cityid'])) {
190 $address = array_merge($address, empty_address());
191 $new = get_address_infos(@$address['text']);
192 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
193 ||
(@$address['parsevalid'] && @$address['cityid'])) {
194 $address = array_merge($address, $new);
195 $address['checked'] = true
;
196 } else if (@$address['parsevalid']) {
197 $address = array_merge($address, cut_address(@$address['text']));
198 $address['checked'] = true
;
199 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
200 $mailer->assign('text', get_address_text($address));
201 $mailer->assign('geoloc', $geotxt);
203 } else if (@$address['changed'] ||
!@$address['checked']) {
205 $address = array_merge($address, cut_address(@$address['text']));
206 $address['checked'] = false
;
207 $address['geoloc'] = $geotxt;
208 $address['geoloc_cityid'] = $new['cityid'];
210 $address = array_merge($address, cut_address(@$address['text']));
211 $address['checked'] = true
;
214 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
215 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
216 $address['text'] = get_address_text($address);
221 abstract class ProfilePage
implements PlWizardPage
224 protected $pg_template;
225 protected $settings = array(); // A set ProfileSetting objects
226 protected $errors = array(); // A set of boolean with the value check errors
227 protected $changed = array(); // A set of boolean indicating wether the value has been changed
228 protected $watched = array(); // A set of boolean indicating the fields that are watched
230 public $orig = array();
231 public $values = array();
233 public function __construct(PlWizard
&$wiz)
235 $this->wizard
=& $wiz;
238 protected function _fetchData()
242 protected function fetchData()
244 if (count($this->orig
) > 0) {
245 $this->values
= $this->orig
;
250 foreach ($this->settings
as $field=>&$setting) {
252 if (!is_null($setting)) {
253 $this->values
[$field] = $setting->value($this, $field, null
, $success);
254 } else if (!isset($this->values
[$field])) {
255 $this->values
[$field] = S
::v($field);
257 $this->errors
[$field] = false
;
259 $this->orig
= $this->values
;
262 protected function _saveData()
266 protected function saveData()
268 require_once 'notifs.inc.php';
269 foreach ($this->settings
as $field=>&$setting) {
270 if (!is_null($setting) && $this->changed
[$field]) {
271 $setting->save($this, $field, $this->values
[$field]);
273 if ($this->changed
[$field] && @$this->watched
[$field]) {
274 register_profile_update(S
::i('uid'), $field);
279 // Update the last modification date
280 XDB
::execute('REPLACE INTO user_changes
281 SET user_id = {?}', S
::v('uid'));
282 if (!S
::has('suid')) {
283 register_watch_op(S
::i('uid'), WATCH_FICHE
);
286 $log =& $_SESSION['log'];
287 $log->log('profil', $platal->pl_self(1));
290 protected function checkChanges()
292 $newvalues = $this->values
;
293 $this->values
= array();
295 $this->values
= $newvalues;
297 foreach ($this->settings
as $field=>&$setting) {
298 if ($this->orig
[$field] != $this->values
[$field]) {
299 $this->changed
[$field] = true
;
302 $this->changed
[$field] = false
;
308 protected function markChange()
312 public function template()
314 return 'profile/base.tpl';
317 protected function _prepare(PlatalPage
&$page, $id)
321 public function prepare(PlatalPage
&$page, $id)
323 if (count($this->values
) == 0) {
326 foreach ($this->values
as $field=>&$value) {
327 $page->assign($field, $value);
329 $this->_prepare($page, $id);
330 $page->assign('profile_page', $this->pg_template
);
331 $page->assign('errors', $this->errors
);
334 public function process()
336 $global_success = true
;
338 foreach ($this->settings
as $field=>&$setting) {
340 if (!is_null($setting)) {
341 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
344 $this->values
[$field] = Post
::v($field, '');
346 $this->errors
[$field] = !$success;
347 $global_success = $global_success && $success;
349 if ($global_success) {
350 if ($this->checkChanges()) {
354 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
357 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
358 . "de ton profil et de revalider ta demande");
359 return PlWizard
::CURRENT_PAGE
;
363 require_once dirname(__FILE__
) . '/general.inc.php';
364 require_once dirname(__FILE__
) . '/addresses.inc.php';
365 require_once dirname(__FILE__
) . '/groups.inc.php';
366 require_once dirname(__FILE__
) . '/decos.inc.php';
367 require_once dirname(__FILE__
) . '/jobs.inc.php';
368 require_once dirname(__FILE__
) . '/skills.inc.php';
369 require_once dirname(__FILE__
) . '/mentor.inc.php';
371 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: