Informs user that her name change is awaiting validation (Closes #1083).
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
f118f685
FB
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
fd38b30e
FB
22interface ProfileSetting
23{
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.
27 *
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
30 *
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
33 */
34 public function value(ProfilePage &$page, $field, $value, &$success);
35
36 /** Save the new value for the given field.
37 */
38 public function save(ProfilePage &$page, $field, $new_value);
39}
40
41abstract class ProfileNoSave implements ProfileSetting
42{
43 public function save(ProfilePage &$page, $field, $new_value) { }
44}
45
12bcf04b 46class ProfileSettingWeb extends ProfileNoSave
fd38b30e
FB
47{
48 public function value(ProfilePage &$page, $field, $value, &$success)
49 {
50 if (is_null($value)) {
51 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
52 }
37d44b3b
FB
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
fd38b30e 55 if (!$success) {
d7610c35
FB
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');
fd38b30e
FB
58 }
59 return $value;
60 }
61}
62
12bcf04b 63class ProfileSettingEmail extends ProfileNoSave
37d44b3b
FB
64{
65 public function value(ProfilePage &$page, $field, $value, &$success)
66 {
67 if (is_null($value)) {
68 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
69 }
70 $value = trim($value);
37d44b3b
FB
71 $success = empty($value) || isvalid_email($value);
72 if (!$success) {
d7610c35 73 Platal::page()->trigError('Adresse Email invalide');
37d44b3b
FB
74 }
75 return $value;
76 }
77}
78
12bcf04b 79class ProfileSettingNumber extends ProfileNoSave
92446a53
PC
80{
81 public function value(ProfilePage &$page, $field, $value, &$success)
82 {
83 if (is_null($value)) {
84 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
85 }
86 $value = trim($value);
87 $success = empty($value) || is_numeric($value);
88 if (!$success) {
ad3fee9d 89 Platal::page()->trigError('Numéro invalide');
92446a53
PC
90 }
91 return $value;
92 }
93}
94
37d44b3b 95
12bcf04b 96class ProfileSettingTel extends ProfileNoSave
fd38b30e
FB
97{
98 public function value(ProfilePage &$page, $field, $value, &$success)
99 {
100 if (is_null($value)) {
101 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
102 }
5e4417a9
GB
103 require_once('profil.func.inc.php');
104 $value = format_phone_number($value);
105 if($value == '') {
106 $success = true;
107 return $value;
108 }
109 $value = format_display_number($value,$error);
110 $success = !$error;
fd38b30e 111 if (!$success) {
ad3fee9d 112 Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
fd38b30e
FB
113 }
114 return $value;
115 }
116}
117
12bcf04b 118class ProfileSettingPhones implements ProfileSetting
bde2be3b
GB
119{
120 private $tel;
121 private $pub;
122 protected $link_type;
123 protected $link_id;
124
e5bcd851 125 public function __construct($type, $link_id)
bde2be3b 126 {
12bcf04b
RB
127 $this->tel = new ProfileSettingTel();
128 $this->pub = new ProfileSettingPub();
bde2be3b 129 $this->link_type = $type;
b814a8b8 130 $this->link_id = $link_id;
bde2be3b
GB
131 }
132
133 public function value(ProfilePage &$page, $field, $value, &$success)
134 {
135 $success = true;
3cd815ca 136 if (is_null($value)) {
bde2be3b 137 $value = array();
9acf2251
SJ
138 $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment
139 FROM profile_phones
ce0b2c6f 140 WHERE pid = {?} AND link_type = {?}
9acf2251
SJ
141 ORDER BY tel_id',
142 $page->pid(), $this->link_type);
9b0d289d
SJ
143 if ($res->numRows() > 0) {
144 $value = $res->fetchAllAssoc();
145 } else {
146 $value = array(
147 0 => array(
148 'type' => 'fixed',
149 'tel' => '',
150 'pub' => 'private',
151 'comment' => '',
152 )
153 );
154 }
bde2be3b 155 }
ed01acac 156
bde2be3b 157 foreach ($value as $key=>&$phone) {
ed01acac
SJ
158 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
159 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
160 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
161 $phone['type'] = 'fixed';
162 $s = false;
163 }
164 if (!$s) {
165 $phone['error'] = true;
166 $success = false;
167 }
168 if (!isset($phone['comment'])) {
169 $phone['comment'] = '';
bde2be3b
GB
170 }
171 }
ed01acac 172
bde2be3b
GB
173 return $value;
174 }
175
e5bcd851 176 private function saveTel($pid, $telid, array &$phone)
bde2be3b
GB
177 {
178 if ($phone['tel'] != '') {
ce0b2c6f 179 XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
bde2be3b
GB
180 search_tel, display_tel, pub, comment)
181 VALUES ({?}, {?}, {?}, {?}, {?},
182 {?}, {?}, {?}, {?})",
e5bcd851 183 $pid, $this->link_type, $this->link_id, $telid, $phone['type'],
b814a8b8 184 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
bde2be3b
GB
185 }
186 }
187
188 public function save(ProfilePage &$page, $field, $value)
189 {
190 XDB::execute("DELETE FROM profile_phones
ce0b2c6f 191 WHERE pid = {?} AND link_type = {?} AND link_id = {?}",
e5bcd851
FB
192 $page->pid(), $this->link_type, $this->link_id);
193 $this->saveTels($page->pid(), $field, $value);
bde2be3b
GB
194 }
195
196 //Only saves phones without a delete operation
e5bcd851 197 public function saveTels($pid, $field, $value)
bde2be3b
GB
198 {
199 foreach ($value as $telid=>&$phone) {
e5bcd851 200 $this->saveTel($pid, $telid, $phone);
bde2be3b
GB
201 }
202 }
203}
204
12bcf04b 205class ProfileSettingPub extends ProfileNoSave
93553cea
FB
206{
207 public function value(ProfilePage &$page, $field, $value, &$success)
208 {
209 $success = true;
210 if (is_null($value)) {
211 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
212 }
041a5cec 213 if (!$value) {
93553cea 214 $value = 'private';
041a5cec 215 } elseif ($value == 'on') { // Checkbox
93553cea
FB
216 $value = 'public';
217 }
218 return $value;
219 }
220}
221
12bcf04b 222class ProfileSettingBool extends ProfileNoSave
576777d7
FB
223{
224 public function value(ProfilePage &$page, $field, $value, &$success)
225 {
226 $success = true;
227 if (is_null($value)) {
041a5cec 228 $value = isset($page->values[$field]) ? $page->values[$field] : null;
576777d7 229 }
ee12da4e 230 return $value ? "1" : "";
576777d7
FB
231 }
232}
233
12bcf04b 234class ProfileSettingDate extends ProfileNoSave
7bff4cb0
FB
235{
236 public function value(ProfilePage &$page, $field, $value, &$success)
237 {
238 $success = true;
239 if (is_null($value)) {
240 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
241 } else {
242 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
243 if (!$success) {
d7610c35 244 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
245 } else {
246 $day = (int)$matches[1];
247 $month = (int)$matches[2];
248 $year = (int)$matches[3];
249 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
250 if (!$success) {
d7610c35 251 Platal::page()->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
252 }
253 }
254 }
255 return $value;
256 }
257}
258
12bcf04b 259abstract class ProfileSettingGeocoding implements ProfileSetting
37d44b3b 260{
73f6c165 261 protected function geocodeAddress(array &$address, &$success)
37d44b3b 262 {
4c906759 263 require_once 'geocoding.inc.php';
37d44b3b 264 $success = true;
afaa2cc7 265 if (isset($address['changed']) && $address['changed'] == 1) {
4c906759
SJ
266 $gmapsGeocoder = new GMapsGeocoder();
267 $address = $gmapsGeocoder->getGeocodedAddress($address);
041a5cec 268 if (isset($address['geoloc'])) {
37d44b3b 269 $success = false;
37d44b3b 270 }
4279d3bc
SJ
271 } elseif (@$address['changed'] && !@$address['text']) {
272 $address = empty_address();
273 $address['pub'] = 'private';
041a5cec 274 }
42568423
SJ
275 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
276 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
277 $mailer->assign('text', $address['text']);
278 $mailer->assign('geoloc', $address['geoloc']);
279 $mailer->send();
73f6c165
SJ
280 $gmapsGeocoder = new GMapsGeocoder();
281 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
37d44b3b 282 }
37d44b3b
FB
283 }
284}
285
286
fd38b30e 287abstract class ProfilePage implements PlWizardPage
f118f685
FB
288{
289 protected $wizard;
fd38b30e
FB
290 protected $pg_template;
291 protected $settings = array(); // A set ProfileSetting objects
93553cea 292 protected $errors = array(); // A set of boolean with the value check errors
576777d7 293 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 294 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 295
93553cea 296 public $orig = array();
fd38b30e 297 public $values = array();
e5bcd851
FB
298 public $profile = null;
299 public $owner = null;
f118f685
FB
300
301 public function __construct(PlWizard &$wiz)
302 {
303 $this->wizard =& $wiz;
e5bcd851
FB
304 $this->profile = $this->wizard->getUserData('profile');
305 $this->owner = $this->wizard->getUserData('owner');
f118f685
FB
306 }
307
7c2e0f0d
FB
308 protected function _fetchData()
309 {
310 }
311
fd38b30e
FB
312 protected function fetchData()
313 {
93553cea
FB
314 if (count($this->orig) > 0) {
315 $this->values = $this->orig;
316 return;
317 }
7c2e0f0d
FB
318
319 $this->_fetchData();
93553cea
FB
320 foreach ($this->settings as $field=>&$setting) {
321 $success = false;
322 if (!is_null($setting)) {
323 $this->values[$field] = $setting->value($this, $field, null, $success);
324 } else if (!isset($this->values[$field])) {
325 $this->values[$field] = S::v($field);
326 }
327 $this->errors[$field] = false;
328 }
329 $this->orig = $this->values;
fd38b30e
FB
330 }
331
7c2e0f0d
FB
332 protected function _saveData()
333 {
334 }
335
fd38b30e
FB
336 protected function saveData()
337 {
a2a1c2f2 338 require_once 'notifs.inc.php';
93553cea 339 foreach ($this->settings as $field=>&$setting) {
576777d7 340 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
341 $setting->save($this, $field, $this->values[$field]);
342 }
a2a1c2f2 343 if ($this->changed[$field] && @$this->watched[$field]) {
4e7bf1e0 344 WatchProfileUpdate::register($this->profile, $field);
a2a1c2f2 345 }
93553cea 346 }
7c2e0f0d 347 $this->_saveData();
576777d7
FB
348
349 // Update the last modification date
4e7bf1e0
FB
350 XDB::execute('UPDATE profiles
351 SET last_change = NOW()
352 WHERE pid = {?}', $this->pid());
576777d7 353 global $platal;
e5bcd851 354 S::logger()->log('profil', $platal->pl_self(2));
93553cea
FB
355 }
356
357 protected function checkChanges()
358 {
359 $newvalues = $this->values;
360 $this->values = array();
361 $this->fetchData();
362 $this->values = $newvalues;
576777d7 363 $changes = false;
93553cea
FB
364 foreach ($this->settings as $field=>&$setting) {
365 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
366 $this->changed[$field] = true;
367 $changes = true;
368 } else {
369 $this->changed[$field] = false;
93553cea
FB
370 }
371 }
576777d7 372 return $changes;
93553cea
FB
373 }
374
375 protected function markChange()
376 {
fd38b30e
FB
377 }
378
f118f685
FB
379 public function template()
380 {
fd38b30e 381 return 'profile/base.tpl';
f118f685
FB
382 }
383
e5bcd851
FB
384 public function pid()
385 {
386 return $this->profile->id();
387 }
388
389 public function hrpid()
390 {
391 return $this->profile->hrpid();
392 }
393
04334c61 394 protected function _prepare(PlPage &$page, $id)
7c2e0f0d
FB
395 {
396 }
397
04334c61 398 public function prepare(PlPage &$page, $id)
f118f685 399 {
fd38b30e
FB
400 if (count($this->values) == 0) {
401 $this->fetchData();
fd38b30e
FB
402 }
403 foreach ($this->values as $field=>&$value) {
404 $page->assign($field, $value);
405 }
7c2e0f0d 406 $this->_prepare($page, $id);
e5bcd851
FB
407 $page->assign('profile', $this->profile);
408 $page->assign('owner', $this->owner);
fd38b30e 409 $page->assign('profile_page', $this->pg_template);
93553cea 410 $page->assign('errors', $this->errors);
f118f685
FB
411 }
412
eb563236 413 public function process(&$global_success)
f118f685 414 {
fd38b30e
FB
415 $global_success = true;
416 $this->fetchData();
417 foreach ($this->settings as $field=>&$setting) {
418 $success = false;
93553cea 419 if (!is_null($setting)) {
85cc366b 420 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
421 } else {
422 $success = true;
85cc366b 423 $this->values[$field] = Post::v($field, '');
93553cea
FB
424 }
425 $this->errors[$field] = !$success;
fd38b30e
FB
426 $global_success = $global_success && $success;
427 }
428 if ($global_success) {
93553cea
FB
429 if ($this->checkChanges()) {
430 $this->saveData();
431 $this->markChange();
fd38b30e 432 }
93553cea 433 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e 434 }
d7610c35 435 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
eb563236 436 . "de ton profil et de revalider ta demande.");
f118f685
FB
437 return PlWizard::CURRENT_PAGE;
438 }
eb563236
SJ
439
440 public function success()
441 {
442 return 'Ton profil a bien été mis à jour.';
443 }
f118f685
FB
444}
445
fd38b30e 446require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 447require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 448require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 449require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 450require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 451require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 452require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
453
454// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
455?>