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