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