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');
80 class ProfileTel
extends ProfileNoSave
82 public function value(ProfilePage
&$page, $field, $value, &$success)
84 if (is_null($value)) {
85 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
87 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
89 Platal
::page()->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
95 class ProfilePub
extends ProfileNoSave
97 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 if (is_null($value) ||
!$value) {
105 } else if ($value == 'on') { // Checkbox
112 class ProfileBool
extends ProfileNoSave
114 public function value(ProfilePage
&$page, $field, $value, &$success)
117 if (is_null($value)) {
118 $value = @$page->values
[$field];
120 return $value ?
"1" : "";
124 class ProfileDate
extends ProfileNoSave
126 public function value(ProfilePage
&$page, $field, $value, &$success)
129 if (is_null($value)) {
130 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
132 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
134 Platal
::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
136 $day = (int)$matches[1];
137 $month = (int)$matches[2];
138 $year = (int)$matches[3];
139 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
141 Platal
::page()->trigError("La date n'a pas une valeur valide");
149 abstract class ProfileGeoloc
implements ProfileSetting
151 protected function geolocAddress(array &$address, &$success)
153 require_once 'geoloc.inc.php';
155 unset($address['geoloc']);
156 unset($address['geoloc_cityid']);
157 if (@$address['parsevalid']
158 ||
(@$address['text'] && @$address['changed'])
159 ||
(@$address['text'] && !@$address['cityid'])) {
160 $address = array_merge($address, empty_address());
161 $new = get_address_infos(@$address['text']);
162 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
163 ||
(@$address['parsevalid'] && @$address['cityid'])) {
164 $address = array_merge($address, $new);
165 $address['checked'] = true
;
166 } else if (@$address['parsevalid']) {
167 $address = array_merge($address, cut_address(@$address['text']));
168 $address['checked'] = true
;
169 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
170 $mailer->assign('text', get_address_text($address));
171 $mailer->assign('geoloc', $geotxt);
173 } else if (@$address['changed'] ||
!@$address['checked']) {
175 $address = array_merge($address, cut_address(@$address['text']));
176 $address['checked'] = false
;
177 $address['geoloc'] = $geotxt;
178 $address['geoloc_cityid'] = $new['cityid'];
180 $address = array_merge($address, cut_address(@$address['text']));
181 $address['checked'] = true
;
183 } elseif (@$address['changed'] && !@$address['text']) {
184 $address = empty_address();
185 $address['pub'] = 'private';
187 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
188 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
189 $address['text'] = get_address_text($address);
194 abstract class ProfilePage
implements PlWizardPage
197 protected $pg_template;
198 protected $settings = array(); // A set ProfileSetting objects
199 protected $errors = array(); // A set of boolean with the value check errors
200 protected $changed = array(); // A set of boolean indicating wether the value has been changed
201 protected $watched = array(); // A set of boolean indicating the fields that are watched
203 public $orig = array();
204 public $values = array();
206 public function __construct(PlWizard
&$wiz)
208 $this->wizard
=& $wiz;
211 protected function _fetchData()
215 protected function fetchData()
217 if (count($this->orig
) > 0) {
218 $this->values
= $this->orig
;
223 foreach ($this->settings
as $field=>&$setting) {
225 if (!is_null($setting)) {
226 $this->values
[$field] = $setting->value($this, $field, null
, $success);
227 } else if (!isset($this->values
[$field])) {
228 $this->values
[$field] = S
::v($field);
230 $this->errors
[$field] = false
;
232 $this->orig
= $this->values
;
235 protected function _saveData()
239 protected function saveData()
241 require_once 'notifs.inc.php';
242 foreach ($this->settings
as $field=>&$setting) {
243 if (!is_null($setting) && $this->changed
[$field]) {
244 $setting->save($this, $field, $this->values
[$field]);
246 if ($this->changed
[$field] && @$this->watched
[$field]) {
247 register_profile_update(S
::i('uid'), $field);
252 // Update the last modification date
253 XDB
::execute('REPLACE INTO user_changes
254 SET user_id = {?}', S
::v('uid'));
255 if (!S
::has('suid')) {
256 register_watch_op(S
::i('uid'), WATCH_FICHE
);
259 S
::logger()->log('profil', $platal->pl_self(1));
262 protected function checkChanges()
264 $newvalues = $this->values
;
265 $this->values
= array();
267 $this->values
= $newvalues;
269 foreach ($this->settings
as $field=>&$setting) {
270 if ($this->orig
[$field] != $this->values
[$field]) {
271 $this->changed
[$field] = true
;
274 $this->changed
[$field] = false
;
280 protected function markChange()
284 public function template()
286 return 'profile/base.tpl';
289 protected function _prepare(PlPage
&$page, $id)
293 public function prepare(PlPage
&$page, $id)
295 if (count($this->values
) == 0) {
298 foreach ($this->values
as $field=>&$value) {
299 $page->assign($field, $value);
301 $this->_prepare($page, $id);
302 $page->assign('profile_page', $this->pg_template
);
303 $page->assign('errors', $this->errors
);
306 public function process(&$global_success)
308 $global_success = true
;
310 foreach ($this->settings
as $field=>&$setting) {
312 if (!is_null($setting)) {
313 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
316 $this->values
[$field] = Post
::v($field, '');
318 $this->errors
[$field] = !$success;
319 $global_success = $global_success && $success;
321 if ($global_success) {
322 if ($this->checkChanges()) {
326 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_PAGE
;
328 Platal
::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
329 . "de ton profil et de revalider ta demande.");
330 return PlWizard
::CURRENT_PAGE
;
333 public function success()
335 return 'Ton profil a bien été mis à jour.';
339 require_once dirname(__FILE__
) . '/general.inc.php';
340 require_once dirname(__FILE__
) . '/addresses.inc.php';
341 require_once dirname(__FILE__
) . '/groups.inc.php';
342 require_once dirname(__FILE__
) . '/decos.inc.php';
343 require_once dirname(__FILE__
) . '/jobs.inc.php';
344 require_once dirname(__FILE__
) . '/skills.inc.php';
345 require_once dirname(__FILE__
) . '/mentor.inc.php';
347 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: