e30023d186c1094199cea686763081e53778527f
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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->trig('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->trig('Adresse Email invalide');
83 class ProfileTel
extends ProfileNoSave
85 public function value(ProfilePage
&$page, $field, $value, &$success)
87 if (is_null($value)) {
88 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
90 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
93 $page->trig('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
99 class ProfilePub
extends ProfileNoSave
101 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 if (is_null($value) ||
!$value) {
109 } else if ($value == 'on') { // Checkbox
116 class ProfileBool
extends ProfileNoSave
118 public function value(ProfilePage
&$page, $field, $value, &$success)
121 if (is_null($value)) {
122 $value = @$page->values
[$field];
124 return $value ?
1 : 0;
128 abstract class ProfileGeoloc
implements ProfileSetting
130 protected function geolocAddress(array &$address, &$success)
132 require_once 'geoloc.inc.php';
134 unset($address['geoloc']);
135 unset($address['geoloc_cityid']);
136 if (@$address['parsevalid']
137 ||
(@$address['text'] && @$address['changed'])
138 ||
(@$address['text'] && !@$address['cityid'])) {
139 $address = array_merge($address, empty_address());
140 $new = get_address_infos(@$address['text']);
141 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
142 ||
(@$address['parsevalid'] && @$address['cityid'])) {
143 $address = array_merge($address, $new);
144 $address['checked'] = true
;
145 } else if (@$address['parsevalid']) {
146 $address = array_merge($address, cut_address(@$address['text']));
147 $address['checked'] = true
;
148 $mailer = new PlMailer('geoloc/mail_geoloc.tpl');
149 $mailer->assign('text', get_address_text($address));
150 $mailer->assign('geoloc', $geotxt);
152 } else if (@$address['changed'] ||
!@$address['checked']) {
154 $address = array_merge($address, cut_address(@$address['text']));
155 $address['checked'] = false
;
156 $address['geoloc'] = $geotxt;
157 $address['geoloc_cityid'] = $new['cityid'];
159 $address = array_merge($address, cut_address(@$address['text']));
160 $address['checked'] = true
;
163 $address['text'] = get_address_text($address);
168 abstract class ProfilePage
implements PlWizardPage
171 protected $pg_template;
172 protected $settings = array(); // A set ProfileSetting objects
173 protected $errors = array(); // A set of boolean with the value check errors
174 protected $changed = array(); // A set of boolean indicating wether the value has been changed
176 public $orig = array();
177 public $values = array();
179 public function __construct(PlWizard
&$wiz)
181 $this->wizard
=& $wiz;
184 protected function fetchData()
186 if (count($this->orig
) > 0) {
187 $this->values
= $this->orig
;
190 foreach ($this->settings
as $field=>&$setting) {
192 if (!is_null($setting)) {
193 $this->values
[$field] = $setting->value($this, $field, null
, $success);
194 } else if (!isset($this->values
[$field])) {
195 $this->values
[$field] = S
::v($field);
197 $this->errors
[$field] = false
;
199 $this->orig
= $this->values
;
202 protected function saveData()
204 foreach ($this->settings
as $field=>&$setting) {
205 if (!is_null($setting) && $this->changed
[$field]) {
206 $setting->save($this, $field, $this->values
[$field]);
210 // Update the last modification date
211 XDB
::execute('REPLACE INTO user_changes
212 SET user_id = {?}', S
::v('uid'));
214 $log =& $_SESSION['log'];
215 $log->log('profil', $platal->pl_self(1));
218 protected function checkChanges()
220 $newvalues = $this->values
;
221 $this->values
= array();
223 $this->values
= $newvalues;
225 foreach ($this->settings
as $field=>&$setting) {
226 if ($this->orig
[$field] != $this->values
[$field]) {
227 $this->changed
[$field] = true
;
230 $this->changed
[$field] = false
;
236 protected function markChange()
240 public function template()
242 return 'profile/base.tpl';
245 public function prepare(PlatalPage
&$page, $id)
247 if (count($this->values
) == 0) {
250 foreach ($this->values
as $field=>&$value) {
251 $page->assign($field, $value);
253 $page->assign('profile_page', $this->pg_template
);
254 $page->assign('errors', $this->errors
);
257 public function process()
259 $global_success = true
;
261 foreach ($this->settings
as $field=>&$setting) {
263 if (!is_null($setting)) {
264 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
267 $this->values
[$field] = Post
::v($field, '');
269 $this->errors
[$field] = !$success;
270 $global_success = $global_success && $success;
272 if ($global_success) {
273 if ($this->checkChanges()) {
277 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
280 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations "
281 . "de ton profil et de revalider ta demande");
282 return PlWizard
::CURRENT_PAGE
;
286 require_once dirname(__FILE__
) . '/general.inc.php';
287 require_once dirname(__FILE__
) . '/addresses.inc.php';
288 require_once dirname(__FILE__
) . '/groups.inc.php';
289 require_once dirname(__FILE__
) . '/decos.inc.php';
290 require_once dirname(__FILE__
) . '/jobs.inc.php';
291 require_once dirname(__FILE__
) . '/skills.inc.php';
292 require_once dirname(__FILE__
) . '/mentor.inc.php';
294 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: