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
123 protected $link_type;
126 public function __construct($type, $link_id, $id = 0)
131 $this->id
= S
::i('uid');
133 $this->tel
= new ProfileTel();
134 $this->pub
= new ProfilePub();
135 $this->link_type
= $type;
136 $this->link_id
= $link_id;
139 public function value(ProfilePage
&$page, $field, $value, &$success)
142 if (is_null($value) ||
!is_array($value)) {
144 $res = XDB
::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
145 FROM profile_phones AS t
146 WHERE t.uid = {?} AND t.link_type = {?}
148 $this->id
, $this->link_type
);
149 $value = $res->fetchAllAssoc();
151 foreach ($value as $key=>&$phone) {
152 if (@$phone['removed']) {
155 unset($phone['removed']);
156 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
157 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
158 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
159 $phone['type'] = 'fixed';
163 $phone['error'] = true
;
166 if (!isset($phone['comment'])) {
167 $phone['comment'] = '';
174 private function saveTel($telid, array &$phone)
176 if ($phone['tel'] != '') {
177 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
178 search_tel, display_tel, pub, comment)
179 VALUES ({?}, {?}, {?}, {?}, {?},
180 {?}, {?}, {?}, {?})",
181 $this->id
, $this->link_type
, $this->link_id
, $telid, $phone['type'],
182 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
186 public function save(ProfilePage
&$page, $field, $value)
188 XDB
::execute("DELETE FROM profile_phones
189 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
190 $this->id
, $this->link_type
, $this->link_id
);
191 $this->saveTels($field, $value);
194 //Only saves phones without a delete operation
195 public function saveTels($field, $value)
197 foreach ($value as $telid=>&$phone) {
198 $this->saveTel($telid, $phone);
203 class ProfilePub
extends ProfileNoSave
205 public function value(ProfilePage
&$page, $field, $value, &$success)
208 if (is_null($value)) {
209 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
211 if (is_null($value) ||
!$value) {
213 } else if ($value == 'on') { // Checkbox
220 class ProfileBool
extends ProfileNoSave
222 public function value(ProfilePage
&$page, $field, $value, &$success)
225 if (is_null($value)) {
226 $value = @$page->values
[$field];
228 return $value ?
"1" : "";
232 class ProfileDate
extends ProfileNoSave
234 public function value(ProfilePage
&$page, $field, $value, &$success)
237 if (is_null($value)) {
238 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
240 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
242 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
244 $day = (int)$matches[1];
245 $month = (int)$matches[2];
246 $year = (int)$matches[3];
247 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
249 Platal
::page()->trigError("La date n'a pas une valeur valide");
257 abstract class ProfileGeoloc
implements ProfileSetting
259 protected function geolocAddress(array &$address, &$success)
261 require_once 'geoloc.inc.php';
263 unset($address['geoloc']);
264 unset($address['geoloc_cityid']);
265 if (@$address['parsevalid']
266 ||
(@$address['text'] && @$address['changed'])
267 ||
(@$address['text'] && !@$address['cityid'])) {
268 $address = array_merge($address, empty_address());
269 $new = get_address_infos(@$address['text']);
270 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
271 ||
(@$address['parsevalid'] && @$address['cityid'])) {
272 $address = array_merge($address, $new);
273 $address['checked'] = true
;
274 } else if (@$address['parsevalid']) {
275 $address = array_merge($address, cut_address(@$address['text']));
276 $address['checked'] = true
;
277 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
278 $mailer->assign('text', get_address_text($address));
279 $mailer->assign('geoloc', $geotxt);
281 } else if (@$address['changed'] ||
!@$address['checked']) {
283 $address = array_merge($address, cut_address(@$address['text']));
284 $address['checked'] = false
;
285 $address['geoloc'] = $geotxt;
286 $address['geoloc_cityid'] = $new['cityid'];
288 $address = array_merge($address, cut_address(@$address['text']));
289 $address['checked'] = true
;
292 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
293 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
294 $address['text'] = get_address_text($address);
299 abstract class ProfilePage
implements PlWizardPage
302 protected $pg_template;
303 protected $settings = array(); // A set ProfileSetting objects
304 protected $errors = array(); // A set of boolean with the value check errors
305 protected $changed = array(); // A set of boolean indicating wether the value has been changed
306 protected $watched = array(); // A set of boolean indicating the fields that are watched
308 public $orig = array();
309 public $values = array();
311 public function __construct(PlWizard
&$wiz)
313 $this->wizard
=& $wiz;
316 protected function _fetchData()
320 protected function fetchData()
322 if (count($this->orig
) > 0) {
323 $this->values
= $this->orig
;
328 foreach ($this->settings
as $field=>&$setting) {
330 if (!is_null($setting)) {
331 $this->values
[$field] = $setting->value($this, $field, null
, $success);
332 } else if (!isset($this->values
[$field])) {
333 $this->values
[$field] = S
::v($field);
335 $this->errors
[$field] = false
;
337 $this->orig
= $this->values
;
340 protected function _saveData()
344 protected function saveData()
346 require_once 'notifs.inc.php';
347 foreach ($this->settings
as $field=>&$setting) {
348 if (!is_null($setting) && $this->changed
[$field]) {
349 $setting->save($this, $field, $this->values
[$field]);
351 if ($this->changed
[$field] && @$this->watched
[$field]) {
352 register_profile_update(S
::i('uid'), $field);
357 // Update the last modification date
358 XDB
::execute('REPLACE INTO user_changes
359 SET user_id = {?}', S
::v('uid'));
360 if (!S
::has('suid')) {
361 register_watch_op(S
::i('uid'), WATCH_FICHE
);
364 $log =& $_SESSION['log'];
365 S
::logger()->log('profil', $platal->pl_self(1));
368 protected function checkChanges()
370 $newvalues = $this->values
;
371 $this->values
= array();
373 $this->values
= $newvalues;
375 foreach ($this->settings
as $field=>&$setting) {
376 if ($this->orig
[$field] != $this->values
[$field]) {
377 $this->changed
[$field] = true
;
380 $this->changed
[$field] = false
;
386 protected function markChange()
390 public function template()
392 return 'profile/base.tpl';
395 protected function _prepare(PlPage
&$page, $id)
399 public function prepare(PlPage
&$page, $id)
401 if (count($this->values
) == 0) {
404 foreach ($this->values
as $field=>&$value) {
405 $page->assign($field, $value);
407 $this->_prepare($page, $id);
408 $page->assign('profile_page', $this->pg_template
);
409 $page->assign('errors', $this->errors
);
412 public function process()
414 $global_success = true
;
416 foreach ($this->settings
as $field=>&$setting) {
418 if (!is_null($setting)) {
419 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
422 $this->values
[$field] = Post
::v($field, '');
424 $this->errors
[$field] = !$success;
425 $global_success = $global_success && $success;
427 if ($global_success) {
428 if ($this->checkChanges()) {
432 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
434 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
435 . "de ton profil et de revalider ta demande");
436 return PlWizard
::CURRENT_PAGE
;
440 require_once dirname(__FILE__
) . '/general.inc.php';
441 require_once dirname(__FILE__
) . '/addresses.inc.php';
442 require_once dirname(__FILE__
) . '/groups.inc.php';
443 require_once dirname(__FILE__
) . '/decos.inc.php';
444 require_once dirname(__FILE__
) . '/jobs.inc.php';
445 require_once dirname(__FILE__
) . '/skills.inc.php';
446 require_once dirname(__FILE__
) . '/mentor.inc.php';
448 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: