2 /***************************************************************************
3 * Copyright (C) 2003-2010 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);
40 /** Get text from the value.
42 public function getText($value);
45 abstract class ProfileNoSave
implements ProfileSetting
47 public function save(ProfilePage
&$page, $field, $new_value) { }
49 public function getText($value) {
54 class ProfileSettingWeb
extends ProfileNoSave
56 public function value(ProfilePage
&$page, $field, $value, &$success)
58 if (is_null($value)) {
59 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
61 $value = trim($value);
62 $success = empty($value) ||
preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
64 Platal
::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
65 . ' et ne pas contenir de caractères interdits');
71 class ProfileSettingEmail
extends ProfileNoSave
73 public function value(ProfilePage
&$page, $field, $value, &$success)
75 if (is_null($value)) {
76 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
78 $value = trim($value);
79 $success = empty($value) ||
isvalid_email($value);
81 Platal
::page()->trigError('Adresse Email invalide');
87 class ProfileSettingNumber
extends ProfileNoSave
89 public function value(ProfilePage
&$page, $field, $value, &$success)
91 if (is_null($value)) {
92 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
94 $value = trim($value);
95 $success = empty($value) ||
is_numeric($value);
97 Platal
::page()->trigError('Numéro invalide');
104 class ProfileSettingTel
extends ProfileNoSave
106 public function value(ProfilePage
&$page, $field, $value, &$success)
108 if (is_null($value)) {
109 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
111 require_once('profil.func.inc.php');
112 $value = format_phone_number($value);
117 $value = format_display_number($value,$error);
120 Platal
::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
126 class ProfileSettingPhones
implements ProfileSetting
130 protected $link_type;
133 public function __construct($type, $link_id)
135 $this->tel
= new ProfileSettingTel();
136 $this->pub
= new ProfileSettingPub();
137 $this->link_type
= $type;
138 $this->link_id
= $link_id;
141 public function value(ProfilePage
&$page, $field, $value, &$success)
144 if (is_null($value)) {
146 $res = XDB
::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment
148 WHERE pid = {?} AND link_type = {?}
150 $page->pid(), $this->link_type
);
151 if ($res->numRows() > 0) {
152 $value = $res->fetchAllAssoc();
165 foreach ($value as $key=>&$phone) {
166 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
167 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
168 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
169 $phone['type'] = 'fixed';
173 $phone['error'] = true
;
176 if (!isset($phone['comment'])) {
177 $phone['comment'] = '';
184 private function saveTel($pid, $telid, array &$phone)
186 if ($phone['tel'] != '') {
187 XDB
::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
188 search_tel, display_tel, pub, comment)
189 VALUES ({?}, {?}, {?}, {?}, {?},
190 {?}, {?}, {?}, {?})",
191 $pid, $this->link_type
, $this->link_id
, $telid, $phone['type'],
192 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
196 public function save(ProfilePage
&$page, $field, $value)
198 XDB
::execute("DELETE FROM profile_phones
199 WHERE pid = {?} AND link_type = {?} AND link_id = {?}",
200 $page->pid(), $this->link_type
, $this->link_id
);
201 $this->saveTels($page->pid(), $field, $value);
204 //Only saves phones without a delete operation
205 public function saveTels($pid, $field, $value)
207 foreach ($value as $telid=>&$phone) {
208 $this->saveTel($pid, $telid, $phone);
212 public function getText($value) {
214 foreach ($value as $phone) {
215 if ($phone['tel'] != '') {
216 $phones[] = 'type : ' . $phone['type'] .', numéro : ' . $phone['tel']
217 . ', commentaire : « ' . $phone['comment'] . ' », affichage : ' . $phone['pub'];
220 return implode(' ; ' , $phones);
224 class ProfileSettingPub
extends ProfileNoSave
226 public function value(ProfilePage
&$page, $field, $value, &$success)
229 if (is_null($value)) {
230 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
234 } elseif ($value == 'on') { // Checkbox
240 public function getText($value) {
245 class ProfileSettingBool
extends ProfileNoSave
247 public function value(ProfilePage
&$page, $field, $value, &$success)
250 if (is_null($value)) {
251 $value = isset($page->values
[$field]) ?
$page->values
[$field] : null
;
253 return $value ?
"1" : "";
257 class ProfileSettingDate
extends ProfileNoSave
259 public function value(ProfilePage
&$page, $field, $value, &$success)
262 if (is_null($value)) {
263 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
265 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
267 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
269 $day = (int)$matches[1];
270 $month = (int)$matches[2];
271 $year = (int)$matches[3];
272 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
274 Platal
::page()->trigError("La date n'a pas une valeur valide");
282 abstract class ProfileSettingGeocoding
implements ProfileSetting
284 protected function geocodeAddress(array &$address, &$success)
286 require_once 'geocoding.inc.php';
288 if (isset($address['changed']) && $address['changed'] == 1) {
289 $gmapsGeocoder = new GMapsGeocoder();
290 $address = $gmapsGeocoder->getGeocodedAddress($address);
291 if (isset($address['geoloc'])) {
294 } elseif (@$address['changed'] && !@$address['text']) {
295 $address = empty_address();
296 $address['pub'] = 'private';
298 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
299 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
300 $mailer->assign('text', $address['text']);
301 $mailer->assign('geoloc', $address['geoloc']);
303 $gmapsGeocoder = new GMapsGeocoder();
304 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
308 public function getText($value) {
314 abstract class ProfilePage
implements PlWizardPage
317 protected $pg_template;
318 protected $settings = array(); // A set ProfileSetting objects
319 protected $errors = array(); // A set of boolean with the value check errors
320 protected $changed = array(); // A set of boolean indicating wether the value has been changed
321 protected $watched = array(); // A set of boolean indicating the fields that are watched
323 public $orig = array();
324 public $values = array();
325 public $profile = null
;
326 public $owner = null
;
328 public function __construct(PlWizard
&$wiz)
330 $this->wizard
=& $wiz;
331 $this->profile
= $this->wizard
->getUserData('profile');
332 $this->owner
= $this->wizard
->getUserData('owner');
335 protected function _fetchData()
339 protected function fetchData()
341 if (count($this->orig
) > 0) {
342 $this->values
= $this->orig
;
347 foreach ($this->settings
as $field=>&$setting) {
349 if (!is_null($setting)) {
350 $this->values
[$field] = $setting->value($this, $field, null
, $success);
351 } else if (!isset($this->values
[$field])) {
352 $this->values
[$field] = S
::v($field);
354 $this->errors
[$field] = false
;
356 $this->orig
= $this->values
;
359 protected function _saveData()
363 protected function saveData()
365 require_once 'notifs.inc.php';
366 $changedFields = array();
367 foreach ($this->settings
as $field=>&$setting) {
368 if ($this->changed
[$field]) {
369 if (!is_null($setting)) {
370 $changedFields[$field] = array(
371 str_replace("\n", " - ", $setting->getText($this->orig
[$field])),
372 str_replace("\n", " - ", $setting->getText($this->values
[$field])),
375 $changedFields[$field] = array(
376 str_replace("\n", " - ", $this->orig
[$field]),
377 str_replace("\n", " - ", $this->values
[$field]),
380 if (!is_null($setting)) {
381 $setting->save($this, $field, $this->values
[$field]);
383 if (isset($this->watched
[$field]) && $this->watched
[$field]) {
384 WatchProfileUpdate
::register($this->profile
, $field);
390 // Update the last modification date
391 XDB
::execute('UPDATE profiles
392 SET last_change = NOW()
393 WHERE pid = {?}', $this->pid());
395 S
::logger()->log('profil', $platal->pl_self(2));
397 /** If the update was made by a third party and the profile corresponds
398 * to a registered user, stores both former and new text.
399 * This will be daily sent to the user.
401 $owner = $this->profile
->owner();
403 if ($owner->isActive() && $owner->id() != $user->id()) {
404 foreach ($changedFields as $field => $values) {
405 XDB
::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
406 VALUES ({?}, {?}, {?}, {?}, {?})',
407 $this->pid(), $user->id(), $field, $values[0], $values[1]);
412 protected function checkChanges()
414 $newvalues = $this->values
;
415 $this->values
= array();
417 $this->values
= $newvalues;
419 foreach ($this->settings
as $field=>&$setting) {
420 if ($this->orig
[$field] != $this->values
[$field]) {
421 $this->changed
[$field] = true
;
424 $this->changed
[$field] = false
;
430 protected function markChange()
434 public function template()
436 return 'profile/base.tpl';
439 public function pid()
441 return $this->profile
->id();
444 public function hrpid()
446 return $this->profile
->hrpid();
449 protected function _prepare(PlPage
&$page, $id)
453 public function prepare(PlPage
&$page, $id)
455 if (count($this->values
) == 0) {
458 foreach ($this->values
as $field=>&$value) {
459 $page->assign($field, $value);
461 $this->_prepare($page, $id);
462 $page->assign('profile', $this->profile
);
463 $page->assign('owner', $this->owner
);
464 $page->assign('profile_page', $this->pg_template
);
465 $page->assign('errors', $this->errors
);
468 public function process(&$global_success)
470 $global_success = true
;
472 foreach ($this->settings
as $field=>&$setting) {
474 if (!is_null($setting)) {
475 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
478 $this->values
[$field] = Post
::v($field, '');
480 $this->errors
[$field] = !$success;
481 $global_success = $global_success && $success;
483 if ($global_success) {
484 if ($this->checkChanges()) {
488 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
490 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
491 . "de ton profil et de revalider ta demande.");
492 return PlWizard
::CURRENT_PAGE
;
495 public function success()
497 return 'Ton profil a bien été mis à jour.';
501 require_once dirname(__FILE__
) . '/general.inc.php';
502 require_once dirname(__FILE__
) . '/addresses.inc.php';
503 require_once dirname(__FILE__
) . '/groups.inc.php';
504 require_once dirname(__FILE__
) . '/decos.inc.php';
505 require_once dirname(__FILE__
) . '/jobs.inc.php';
506 require_once dirname(__FILE__
) . '/skills.inc.php';
507 require_once dirname(__FILE__
) . '/mentor.inc.php';
509 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: