Merge branch 'master' into fusionax
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
fd38b30e
FB
46class ProfileWeb extends ProfileNoSave
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
37d44b3b
FB
63class ProfileEmail extends ProfileNoSave
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
92446a53
PC
79class ProfileNumber extends ProfileNoSave
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
fd38b30e
FB
96class ProfileTel extends ProfileNoSave
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
bde2be3b
GB
118class ProfilePhones implements ProfileSetting
119{
120 private $tel;
121 private $pub;
b814a8b8 122 protected $id;
bde2be3b
GB
123 protected $link_type;
124 protected $link_id;
125
b814a8b8 126 public function __construct($type, $link_id, $id = 0)
bde2be3b 127 {
b814a8b8
SJ
128 if ($id != 0) {
129 $this->id = $id;
130 } else {
131 $this->id = S::i('uid');
132 }
bde2be3b
GB
133 $this->tel = new ProfileTel();
134 $this->pub = new ProfilePub();
135 $this->link_type = $type;
b814a8b8 136 $this->link_id = $link_id;
bde2be3b
GB
137 }
138
139 public function value(ProfilePage &$page, $field, $value, &$success)
140 {
141 $success = true;
3cd815ca 142 if (is_null($value)) {
bde2be3b 143 $value = array();
b814a8b8
SJ
144 $res = XDB::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
145 FROM profile_phones AS t
146 WHERE t.uid = {?} AND t.link_type = {?}
147 ORDER BY t.tel_id",
148 $this->id, $this->link_type);
9b0d289d
SJ
149 if ($res->numRows() > 0) {
150 $value = $res->fetchAllAssoc();
151 } else {
152 $value = array(
153 0 => array(
154 'type' => 'fixed',
155 'tel' => '',
156 'pub' => 'private',
157 'comment' => '',
158 )
159 );
160 }
bde2be3b
GB
161 }
162 foreach ($value as $key=>&$phone) {
9b0d289d 163 if (isset($phone['removed']) && $phone['removed']) {
bde2be3b
GB
164 unset($value[$key]);
165 } else {
46cd7788 166 unset($phone['removed']);
bde2be3b
GB
167 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
168 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
169 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
170 $phone['type'] = 'fixed';
171 $s = false;
172 }
173 if (!$s) {
174 $phone['error'] = true;
175 $success = false;
176 }
177 if (!isset($phone['comment'])) {
178 $phone['comment'] = '';
179 }
180 }
181 }
182 return $value;
183 }
184
185 private function saveTel($telid, array &$phone)
186 {
187 if ($phone['tel'] != '') {
188 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
189 search_tel, display_tel, pub, comment)
190 VALUES ({?}, {?}, {?}, {?}, {?},
191 {?}, {?}, {?}, {?})",
b814a8b8
SJ
192 $this->id, $this->link_type, $this->link_id, $telid, $phone['type'],
193 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
bde2be3b
GB
194 }
195 }
196
197 public function save(ProfilePage &$page, $field, $value)
198 {
199 XDB::execute("DELETE FROM profile_phones
200 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
b814a8b8 201 $this->id, $this->link_type, $this->link_id);
bde2be3b
GB
202 $this->saveTels($field, $value);
203 }
204
205 //Only saves phones without a delete operation
206 public function saveTels($field, $value)
207 {
208 foreach ($value as $telid=>&$phone) {
209 $this->saveTel($telid, $phone);
210 }
211 }
212}
213
93553cea
FB
214class ProfilePub extends ProfileNoSave
215{
216 public function value(ProfilePage &$page, $field, $value, &$success)
217 {
218 $success = true;
219 if (is_null($value)) {
220 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
221 }
041a5cec 222 if (!$value) {
93553cea 223 $value = 'private';
041a5cec 224 } elseif ($value == 'on') { // Checkbox
93553cea
FB
225 $value = 'public';
226 }
227 return $value;
228 }
229}
230
576777d7
FB
231class ProfileBool extends ProfileNoSave
232{
233 public function value(ProfilePage &$page, $field, $value, &$success)
234 {
235 $success = true;
236 if (is_null($value)) {
041a5cec 237 $value = isset($page->values[$field]) ? $page->values[$field] : null;
576777d7 238 }
ee12da4e 239 return $value ? "1" : "";
576777d7
FB
240 }
241}
242
7bff4cb0
FB
243class ProfileDate extends ProfileNoSave
244{
245 public function value(ProfilePage &$page, $field, $value, &$success)
246 {
247 $success = true;
248 if (is_null($value)) {
249 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
250 } else {
251 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
252 if (!$success) {
d7610c35 253 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
254 } else {
255 $day = (int)$matches[1];
256 $month = (int)$matches[2];
257 $year = (int)$matches[3];
258 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
259 if (!$success) {
d7610c35 260 Platal::page()->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
261 }
262 }
263 }
264 return $value;
265 }
266}
267
73f6c165 268abstract class ProfileGeocoding implements ProfileSetting
37d44b3b 269{
73f6c165 270 protected function geocodeAddress(array &$address, &$success)
37d44b3b 271 {
4c906759 272 require_once 'geocoding.inc.php';
37d44b3b 273 $success = true;
afaa2cc7 274 if (isset($address['changed']) && $address['changed'] == 1) {
4c906759
SJ
275 $gmapsGeocoder = new GMapsGeocoder();
276 $address = $gmapsGeocoder->getGeocodedAddress($address);
041a5cec 277 if (isset($address['geoloc'])) {
37d44b3b 278 $success = false;
37d44b3b 279 }
041a5cec 280 }
42568423
SJ
281 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
282 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
283 $mailer->assign('text', $address['text']);
284 $mailer->assign('geoloc', $address['geoloc']);
285 $mailer->send();
73f6c165
SJ
286 $gmapsGeocoder = new GMapsGeocoder();
287 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
37d44b3b 288 }
37d44b3b
FB
289 }
290}
291
292
fd38b30e 293abstract class ProfilePage implements PlWizardPage
f118f685
FB
294{
295 protected $wizard;
fd38b30e
FB
296 protected $pg_template;
297 protected $settings = array(); // A set ProfileSetting objects
93553cea 298 protected $errors = array(); // A set of boolean with the value check errors
576777d7 299 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 300 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 301
93553cea 302 public $orig = array();
fd38b30e 303 public $values = array();
f118f685
FB
304
305 public function __construct(PlWizard &$wiz)
306 {
307 $this->wizard =& $wiz;
308 }
309
7c2e0f0d
FB
310 protected function _fetchData()
311 {
312 }
313
fd38b30e
FB
314 protected function fetchData()
315 {
93553cea
FB
316 if (count($this->orig) > 0) {
317 $this->values = $this->orig;
318 return;
319 }
7c2e0f0d
FB
320
321 $this->_fetchData();
93553cea
FB
322 foreach ($this->settings as $field=>&$setting) {
323 $success = false;
324 if (!is_null($setting)) {
325 $this->values[$field] = $setting->value($this, $field, null, $success);
326 } else if (!isset($this->values[$field])) {
327 $this->values[$field] = S::v($field);
328 }
329 $this->errors[$field] = false;
330 }
331 $this->orig = $this->values;
fd38b30e
FB
332 }
333
7c2e0f0d
FB
334 protected function _saveData()
335 {
336 }
337
fd38b30e
FB
338 protected function saveData()
339 {
a2a1c2f2 340 require_once 'notifs.inc.php';
93553cea 341 foreach ($this->settings as $field=>&$setting) {
576777d7 342 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
343 $setting->save($this, $field, $this->values[$field]);
344 }
a2a1c2f2
FB
345 if ($this->changed[$field] && @$this->watched[$field]) {
346 register_profile_update(S::i('uid'), $field);
347 }
93553cea 348 }
7c2e0f0d 349 $this->_saveData();
576777d7
FB
350
351 // Update the last modification date
352 XDB::execute('REPLACE INTO user_changes
353 SET user_id = {?}', S::v('uid'));
3ebe4a0a 354 if (!S::has('suid')) {
3ebe4a0a
FB
355 register_watch_op(S::i('uid'), WATCH_FICHE);
356 }
576777d7 357 global $platal;
732e5855 358 S::logger()->log('profil', $platal->pl_self(1));
93553cea
FB
359 }
360
361 protected function checkChanges()
362 {
363 $newvalues = $this->values;
364 $this->values = array();
365 $this->fetchData();
366 $this->values = $newvalues;
576777d7 367 $changes = false;
93553cea
FB
368 foreach ($this->settings as $field=>&$setting) {
369 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
370 $this->changed[$field] = true;
371 $changes = true;
372 } else {
373 $this->changed[$field] = false;
93553cea
FB
374 }
375 }
576777d7 376 return $changes;
93553cea
FB
377 }
378
379 protected function markChange()
380 {
fd38b30e
FB
381 }
382
f118f685
FB
383 public function template()
384 {
fd38b30e 385 return 'profile/base.tpl';
f118f685
FB
386 }
387
04334c61 388 protected function _prepare(PlPage &$page, $id)
7c2e0f0d
FB
389 {
390 }
391
04334c61 392 public function prepare(PlPage &$page, $id)
f118f685 393 {
fd38b30e
FB
394 if (count($this->values) == 0) {
395 $this->fetchData();
fd38b30e
FB
396 }
397 foreach ($this->values as $field=>&$value) {
398 $page->assign($field, $value);
399 }
7c2e0f0d 400 $this->_prepare($page, $id);
fd38b30e 401 $page->assign('profile_page', $this->pg_template);
93553cea 402 $page->assign('errors', $this->errors);
f118f685
FB
403 }
404
eb563236 405 public function process(&$global_success)
f118f685 406 {
fd38b30e
FB
407 $global_success = true;
408 $this->fetchData();
409 foreach ($this->settings as $field=>&$setting) {
410 $success = false;
93553cea 411 if (!is_null($setting)) {
85cc366b 412 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
413 } else {
414 $success = true;
85cc366b 415 $this->values[$field] = Post::v($field, '');
93553cea
FB
416 }
417 $this->errors[$field] = !$success;
fd38b30e
FB
418 $global_success = $global_success && $success;
419 }
420 if ($global_success) {
93553cea
FB
421 if ($this->checkChanges()) {
422 $this->saveData();
423 $this->markChange();
fd38b30e 424 }
93553cea 425 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e 426 }
d7610c35 427 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
eb563236 428 . "de ton profil et de revalider ta demande.");
f118f685
FB
429 return PlWizard::CURRENT_PAGE;
430 }
eb563236
SJ
431
432 public function success()
433 {
434 return 'Ton profil a bien été mis à jour.';
435 }
f118f685
FB
436}
437
fd38b30e 438require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 439require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 440require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 441require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 442require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 443require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 444require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
445
446// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
447?>