2 /***************************************************************************
3 * Copyright (C) 2003-2009 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)) {
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 if ($res->numRows() > 0) {
150 $value = $res->fetchAllAssoc();
162 foreach ($value as $key=>&$phone) {
163 if (isset($phone['removed']) && $phone['removed']) {
166 unset($phone['removed']);
167 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
168 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
169 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
170 $phone['type'] = 'fixed';
174 $phone['error'] = true
;
177 if (!isset($phone['comment'])) {
178 $phone['comment'] = '';
185 private function saveTel($telid, array &$phone)
187 if ($phone['tel'] != '') {
188 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
189 search_tel, display_tel, pub, comment)
190 VALUES ({?}, {?}, {?}, {?}, {?},
191 {?}, {?}, {?}, {?})",
192 $this->id
, $this->link_type
, $this->link_id
, $telid, $phone['type'],
193 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
197 public function save(ProfilePage
&$page, $field, $value)
199 XDB
::execute("DELETE FROM profile_phones
200 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
201 $this->id
, $this->link_type
, $this->link_id
);
202 $this->saveTels($field, $value);
205 //Only saves phones without a delete operation
206 public function saveTels($field, $value)
208 foreach ($value as $telid=>&$phone) {
209 $this->saveTel($telid, $phone);
214 class ProfilePub
extends ProfileNoSave
216 public function value(ProfilePage
&$page, $field, $value, &$success)
219 if (is_null($value)) {
220 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
224 } elseif ($value == 'on') { // Checkbox
231 class ProfileBool
extends ProfileNoSave
233 public function value(ProfilePage
&$page, $field, $value, &$success)
236 if (is_null($value)) {
237 $value = isset($page->values
[$field]) ?
$page->values
[$field] : null
;
239 return $value ?
"1" : "";
243 class ProfileDate
extends ProfileNoSave
245 public function value(ProfilePage
&$page, $field, $value, &$success)
248 if (is_null($value)) {
249 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
251 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
253 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
255 $day = (int)$matches[1];
256 $month = (int)$matches[2];
257 $year = (int)$matches[3];
258 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
260 Platal
::page()->trigError("La date n'a pas une valeur valide");
268 abstract class ProfileGeocoding
implements ProfileSetting
270 protected function geocodeAddress(array &$address, &$success)
272 require_once 'geocoding.inc.php';
274 if (isset($address['changed']) && $address['changed'] == 1) {
275 $gmapsGeocoder = new GMapsGeocoder();
276 $address = $gmapsGeocoder->getGeocodedAddress($address);
277 if (isset($address['geoloc'])) {
280 } elseif (@$address['changed'] && !@$address['text']) {
281 $address = empty_address();
282 $address['pub'] = 'private';
284 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
285 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
286 $mailer->assign('text', $address['text']);
287 $mailer->assign('geoloc', $address['geoloc']);
289 $gmapsGeocoder = new GMapsGeocoder();
290 $address = $gmapsGeocoder->stripGeocodingFromAddress($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 S
::logger()->log('profil', $platal->pl_self(1));
364 protected function checkChanges()
366 $newvalues = $this->values
;
367 $this->values
= array();
369 $this->values
= $newvalues;
371 foreach ($this->settings
as $field=>&$setting) {
372 if ($this->orig
[$field] != $this->values
[$field]) {
373 $this->changed
[$field] = true
;
376 $this->changed
[$field] = false
;
382 protected function markChange()
386 public function template()
388 return 'profile/base.tpl';
391 protected function _prepare(PlPage
&$page, $id)
395 public function prepare(PlPage
&$page, $id)
397 if (count($this->values
) == 0) {
400 foreach ($this->values
as $field=>&$value) {
401 $page->assign($field, $value);
403 $this->_prepare($page, $id);
404 $page->assign('profile_page', $this->pg_template
);
405 $page->assign('errors', $this->errors
);
408 public function process(&$global_success)
410 $global_success = true
;
412 foreach ($this->settings
as $field=>&$setting) {
414 if (!is_null($setting)) {
415 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
418 $this->values
[$field] = Post
::v($field, '');
420 $this->errors
[$field] = !$success;
421 $global_success = $global_success && $success;
423 if ($global_success) {
424 if ($this->checkChanges()) {
428 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
430 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
431 . "de ton profil et de revalider ta demande.");
432 return PlWizard
::CURRENT_PAGE
;
435 public function success()
437 return 'Ton profil a bien été mis à jour.';
441 require_once dirname(__FILE__
) . '/general.inc.php';
442 require_once dirname(__FILE__
) . '/addresses.inc.php';
443 require_once dirname(__FILE__
) . '/groups.inc.php';
444 require_once dirname(__FILE__
) . '/decos.inc.php';
445 require_once dirname(__FILE__
) . '/jobs.inc.php';
446 require_once dirname(__FILE__
) . '/skills.inc.php';
447 require_once dirname(__FILE__
) . '/mentor.inc.php';
449 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: