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
122 protected $link_type;
125 public function __construct($type, $link_id)
127 $this->tel
= new ProfileTel();
128 $this->pub
= new ProfilePub();
129 $this->link_type
= $type;
130 $this->link_id
= $link_id;
133 public function value(ProfilePage
&$page, $field, $value, &$success)
136 if (is_null($value) ||
!is_array($value)) {
138 $res = XDB
::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
139 FROM profile_phones AS t
140 WHERE t.uid = {?} AND t.link_type = {?}
142 $page->pid(), $this->link_type
);
143 $value = $res->fetchAllAssoc();
145 foreach ($value as $key=>&$phone) {
146 if (@$phone['removed']) {
149 unset($phone['removed']);
150 $phone['pub'] = $this->pub
->value($page, 'pub', $phone['pub'], $s);
151 $phone['tel'] = $this->tel
->value($page, 'tel', $phone['tel'], $s);
152 if(!isset($phone['type']) ||
($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
153 $phone['type'] = 'fixed';
157 $phone['error'] = true
;
160 if (!isset($phone['comment'])) {
161 $phone['comment'] = '';
168 private function saveTel($pid, $telid, array &$phone)
170 if ($phone['tel'] != '') {
171 XDB
::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
172 search_tel, display_tel, pub, comment)
173 VALUES ({?}, {?}, {?}, {?}, {?},
174 {?}, {?}, {?}, {?})",
175 $pid, $this->link_type
, $this->link_id
, $telid, $phone['type'],
176 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
180 public function save(ProfilePage
&$page, $field, $value)
182 XDB
::execute("DELETE FROM profile_phones
183 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
184 $page->pid(), $this->link_type
, $this->link_id
);
185 $this->saveTels($page->pid(), $field, $value);
188 //Only saves phones without a delete operation
189 public function saveTels($pid, $field, $value)
191 foreach ($value as $telid=>&$phone) {
192 $this->saveTel($pid, $telid, $phone);
197 class ProfilePub
extends ProfileNoSave
199 public function value(ProfilePage
&$page, $field, $value, &$success)
202 if (is_null($value)) {
203 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
205 if (is_null($value) ||
!$value) {
207 } else if ($value == 'on') { // Checkbox
214 class ProfileBool
extends ProfileNoSave
216 public function value(ProfilePage
&$page, $field, $value, &$success)
219 if (is_null($value)) {
220 $value = @$page->values
[$field];
222 return $value ?
"1" : "";
226 class ProfileDate
extends ProfileNoSave
228 public function value(ProfilePage
&$page, $field, $value, &$success)
231 if (is_null($value)) {
232 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
234 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
236 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
238 $day = (int)$matches[1];
239 $month = (int)$matches[2];
240 $year = (int)$matches[3];
241 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
243 Platal
::page()->trigError("La date n'a pas une valeur valide");
251 abstract class ProfileGeoloc
implements ProfileSetting
253 protected function geolocAddress(array &$address, &$success)
255 require_once 'geoloc.inc.php';
257 unset($address['geoloc']);
258 unset($address['geoloc_cityid']);
259 if (@$address['parsevalid']
260 ||
(@$address['text'] && @$address['changed'])
261 ||
(@$address['text'] && !@$address['cityid'])) {
262 $address = array_merge($address, empty_address());
263 $new = get_address_infos(@$address['text']);
264 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
265 ||
(@$address['parsevalid'] && @$address['cityid'])) {
266 $address = array_merge($address, $new);
267 $address['checked'] = true
;
268 } else if (@$address['parsevalid']) {
269 $address = array_merge($address, cut_address(@$address['text']));
270 $address['checked'] = true
;
271 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
272 $mailer->assign('text', get_address_text($address));
273 $mailer->assign('geoloc', $geotxt);
275 } else if (@$address['changed'] ||
!@$address['checked']) {
277 $address = array_merge($address, cut_address(@$address['text']));
278 $address['checked'] = false
;
279 $address['geoloc'] = $geotxt;
280 $address['geoloc_cityid'] = $new['cityid'];
282 $address = array_merge($address, cut_address(@$address['text']));
283 $address['checked'] = true
;
286 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
287 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
288 $address['text'] = get_address_text($address);
293 abstract class ProfilePage
implements PlWizardPage
296 protected $pg_template;
297 protected $settings = array(); // A set ProfileSetting objects
298 protected $errors = array(); // A set of boolean with the value check errors
299 protected $changed = array(); // A set of boolean indicating wether the value has been changed
300 protected $watched = array(); // A set of boolean indicating the fields that are watched
302 public $orig = array();
303 public $values = array();
304 public $profile = null
;
305 public $owner = null
;
307 public function __construct(PlWizard
&$wiz)
309 $this->wizard
=& $wiz;
310 $this->profile
= $this->wizard
->getUserData('profile');
311 $this->owner
= $this->wizard
->getUserData('owner');
314 protected function _fetchData()
318 protected function fetchData()
320 if (count($this->orig
) > 0) {
321 $this->values
= $this->orig
;
326 foreach ($this->settings
as $field=>&$setting) {
328 if (!is_null($setting)) {
329 $this->values
[$field] = $setting->value($this, $field, null
, $success);
330 } else if (!isset($this->values
[$field])) {
331 $this->values
[$field] = S
::v($field);
333 $this->errors
[$field] = false
;
335 $this->orig
= $this->values
;
338 protected function _saveData()
342 protected function saveData()
344 require_once 'notifs.inc.php';
345 foreach ($this->settings
as $field=>&$setting) {
346 if (!is_null($setting) && $this->changed
[$field]) {
347 $setting->save($this, $field, $this->values
[$field]);
349 if ($this->changed
[$field] && @$this->watched
[$field]) {
350 WatchProfileUpdate
::register($this->profile
, $field);
355 // Update the last modification date
356 XDB
::execute('UPDATE profiles
357 SET last_change = NOW()
358 WHERE pid = {?}', $this->pid());
360 S
::logger()->log('profil', $platal->pl_self(2));
363 protected function checkChanges()
365 $newvalues = $this->values
;
366 $this->values
= array();
368 $this->values
= $newvalues;
370 foreach ($this->settings
as $field=>&$setting) {
371 if ($this->orig
[$field] != $this->values
[$field]) {
372 $this->changed
[$field] = true
;
375 $this->changed
[$field] = false
;
381 protected function markChange()
385 public function template()
387 return 'profile/base.tpl';
390 public function pid()
392 return $this->profile
->id();
395 public function hrpid()
397 return $this->profile
->hrpid();
400 protected function _prepare(PlPage
&$page, $id)
404 public function prepare(PlPage
&$page, $id)
406 if (count($this->values
) == 0) {
409 foreach ($this->values
as $field=>&$value) {
410 $page->assign($field, $value);
412 $this->_prepare($page, $id);
413 $page->assign('profile', $this->profile
);
414 $page->assign('owner', $this->owner
);
415 $page->assign('profile_page', $this->pg_template
);
416 $page->assign('errors', $this->errors
);
419 public function process()
421 $global_success = true
;
423 foreach ($this->settings
as $field=>&$setting) {
425 if (!is_null($setting)) {
426 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
429 $this->values
[$field] = Post
::v($field, '');
431 $this->errors
[$field] = !$success;
432 $global_success = $global_success && $success;
434 if ($global_success) {
435 if ($this->checkChanges()) {
439 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
441 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
442 . "de ton profil et de revalider ta demande");
443 return PlWizard
::CURRENT_PAGE
;
447 require_once dirname(__FILE__
) . '/general.inc.php';
448 require_once dirname(__FILE__
) . '/addresses.inc.php';
449 require_once dirname(__FILE__
) . '/groups.inc.php';
450 require_once dirname(__FILE__
) . '/decos.inc.php';
451 require_once dirname(__FILE__
) . '/jobs.inc.php';
452 require_once dirname(__FILE__
) . '/skills.inc.php';
453 require_once dirname(__FILE__
) . '/mentor.inc.php';
455 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: