Merge remote branch 'origin/platal-1.0.0'
[platal.git] / modules / profile / page.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 interface 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 /** Get text from the value.
41 */
42 public function getText($value);
43 }
44
45 abstract class ProfileNoSave implements ProfileSetting
46 {
47 public function save(ProfilePage &$page, $field, $new_value) { }
48
49 public function getText($value) {
50 return $value;
51 }
52 }
53
54 class ProfileSettingWeb extends ProfileNoSave
55 {
56 public function value(ProfilePage &$page, $field, $value, &$success)
57 {
58 if (is_null($value)) {
59 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
60 }
61 $value = trim($value);
62 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
63 if (!$success) {
64 Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
65 . ' et ne pas contenir de caractères interdits');
66 }
67 return $value;
68 }
69 }
70
71 class ProfileSettingEmail extends ProfileNoSave
72 {
73 public function value(ProfilePage &$page, $field, $value, &$success)
74 {
75 if (is_null($value)) {
76 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
77 }
78 $value = trim($value);
79 $success = empty($value) || isvalid_email($value);
80 if (!$success) {
81 Platal::page()->trigError('Adresse Email invalide');
82 }
83 return $value;
84 }
85 }
86
87 class ProfileSettingNumber extends ProfileNoSave
88 {
89 public function value(ProfilePage &$page, $field, $value, &$success)
90 {
91 if (is_null($value)) {
92 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
93 }
94 $value = trim($value);
95 $success = empty($value) || is_numeric($value);
96 if (!$success) {
97 Platal::page()->trigError('Numéro invalide');
98 }
99 return $value;
100 }
101 }
102
103
104 class ProfileSettingTel extends ProfileNoSave
105 {
106 public function value(ProfilePage &$page, $field, $value, &$success)
107 {
108 if (is_null($value)) {
109 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
110 }
111 require_once('profil.func.inc.php');
112 $value = format_phone_number($value);
113 if($value == '') {
114 $success = true;
115 return $value;
116 }
117 $value = format_display_number($value,$error);
118 $success = !$error;
119 if (!$success) {
120 Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
121 }
122 return $value;
123 }
124 }
125
126 class ProfileSettingPhones implements ProfileSetting
127 {
128 private $tel;
129 private $pub;
130 protected $link_type;
131 protected $link_id;
132
133 public function __construct($type, $link_id)
134 {
135 $this->tel = new ProfileSettingTel();
136 $this->pub = new ProfileSettingPub();
137 $this->link_type = $type;
138 $this->link_id = $link_id;
139 }
140
141 public function value(ProfilePage &$page, $field, $value, &$success)
142 {
143 $success = true;
144 if (is_null($value)) {
145 $value = array();
146 $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment
147 FROM profile_phones
148 WHERE pid = {?} AND link_type = {?}
149 ORDER BY tel_id',
150 $page->pid(), $this->link_type);
151 if ($res->numRows() > 0) {
152 $value = $res->fetchAllAssoc();
153 } else {
154 $value = array(
155 0 => array(
156 'type' => 'fixed',
157 'tel' => '',
158 'pub' => 'private',
159 'comment' => '',
160 )
161 );
162 }
163 }
164
165 foreach ($value as $key=>&$phone) {
166 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
167 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
168 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
169 $phone['type'] = 'fixed';
170 $s = false;
171 }
172 if (!$s) {
173 $phone['error'] = true;
174 $success = false;
175 }
176 if (!isset($phone['comment'])) {
177 $phone['comment'] = '';
178 }
179 }
180
181 return $value;
182 }
183
184 private function saveTel($pid, $telid, array &$phone)
185 {
186 if ($phone['tel'] != '') {
187 XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
188 search_tel, display_tel, pub, comment)
189 VALUES ({?}, {?}, {?}, {?}, {?},
190 {?}, {?}, {?}, {?})",
191 $pid, $this->link_type, $this->link_id, $telid, $phone['type'],
192 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
193 }
194 }
195
196 public function save(ProfilePage &$page, $field, $value)
197 {
198 XDB::execute("DELETE FROM profile_phones
199 WHERE pid = {?} AND link_type = {?} AND link_id = {?}",
200 $page->pid(), $this->link_type, $this->link_id);
201 $this->saveTels($page->pid(), $field, $value);
202 }
203
204 //Only saves phones without a delete operation
205 public function saveTels($pid, $field, $value)
206 {
207 foreach ($value as $telid=>&$phone) {
208 $this->saveTel($pid, $telid, $phone);
209 }
210 }
211
212 public function getText($value) {
213 $phones = array();
214 foreach ($value as $phone) {
215 if ($phone['tel'] != '') {
216 $phones[] = 'type : ' . $phone['type'] .', numéro : ' . $phone['tel']
217 . ', commentaire : « ' . $phone['comment'] . ' », affichage : ' . $phone['pub'];
218 }
219 }
220 return implode(' ; ' , $phones);
221 }
222 }
223
224 class ProfileSettingPub extends ProfileNoSave
225 {
226 public function value(ProfilePage &$page, $field, $value, &$success)
227 {
228 $success = true;
229 if (is_null($value)) {
230 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
231 }
232 if (!$value) {
233 $value = 'private';
234 } elseif ($value == 'on') { // Checkbox
235 $value = 'public';
236 }
237 return $value;
238 }
239
240 public function getText($value) {
241 return $value;
242 }
243 }
244
245 class ProfileSettingBool extends ProfileNoSave
246 {
247 public function value(ProfilePage &$page, $field, $value, &$success)
248 {
249 $success = true;
250 if (is_null($value)) {
251 $value = isset($page->values[$field]) ? $page->values[$field] : null;
252 }
253 return $value ? "1" : "";
254 }
255 }
256
257 class ProfileSettingDate extends ProfileNoSave
258 {
259 public function value(ProfilePage &$page, $field, $value, &$success)
260 {
261 $success = true;
262 if (is_null($value)) {
263 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
264 } else {
265 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
266 if (!$success) {
267 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
268 } else {
269 $day = (int)$matches[1];
270 $month = (int)$matches[2];
271 $year = (int)$matches[3];
272 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
273 if (!$success) {
274 Platal::page()->trigError("La date n'a pas une valeur valide");
275 }
276 }
277 }
278 return $value;
279 }
280 }
281
282 abstract class ProfileSettingGeocoding implements ProfileSetting
283 {
284 protected function geocodeAddress(array &$address, &$success)
285 {
286 require_once 'geocoding.inc.php';
287 $success = true;
288 if (isset($address['changed']) && $address['changed'] == 1) {
289 $gmapsGeocoder = new GMapsGeocoder();
290 $address = $gmapsGeocoder->getGeocodedAddress($address);
291 if (isset($address['geoloc'])) {
292 $success = false;
293 }
294 } elseif (@$address['changed'] && !@$address['text']) {
295 $address = empty_address();
296 $address['pub'] = 'private';
297 }
298 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
299 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
300 $mailer->assign('text', $address['text']);
301 $mailer->assign('geoloc', $address['geoloc']);
302 $mailer->send();
303 $gmapsGeocoder = new GMapsGeocoder();
304 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
305 }
306 }
307
308 public function getText($value) {
309 return $value;
310 }
311 }
312
313
314 abstract class ProfilePage implements PlWizardPage
315 {
316 protected $wizard;
317 protected $pg_template;
318 protected $settings = array(); // A set ProfileSetting objects
319 protected $errors = array(); // A set of boolean with the value check errors
320 protected $changed = array(); // A set of boolean indicating wether the value has been changed
321 protected $watched = array(); // A set of boolean indicating the fields that are watched
322
323 public $orig = array();
324 public $values = array();
325 public $profile = null;
326 public $owner = null;
327
328 public function __construct(PlWizard &$wiz)
329 {
330 $this->wizard =& $wiz;
331 $this->profile = $this->wizard->getUserData('profile');
332 $this->owner = $this->wizard->getUserData('owner');
333 }
334
335 protected function _fetchData()
336 {
337 }
338
339 protected function fetchData()
340 {
341 if (count($this->orig) > 0) {
342 $this->values = $this->orig;
343 return;
344 }
345
346 $this->_fetchData();
347 foreach ($this->settings as $field=>&$setting) {
348 $success = false;
349 if (!is_null($setting)) {
350 $this->values[$field] = $setting->value($this, $field, null, $success);
351 } else if (!isset($this->values[$field])) {
352 $this->values[$field] = S::v($field);
353 }
354 $this->errors[$field] = false;
355 }
356 $this->orig = $this->values;
357 }
358
359 protected function _saveData()
360 {
361 }
362
363 protected function saveData()
364 {
365 require_once 'notifs.inc.php';
366 $changedFields = array();
367 foreach ($this->settings as $field=>&$setting) {
368 if ($this->changed[$field]) {
369 if (!is_null($setting)) {
370 $changedFields[$field] = array(
371 str_replace("\n", " - ", $setting->getText($this->orig[$field])),
372 str_replace("\n", " - ", $setting->getText($this->values[$field])),
373 );
374 } else {
375 $changedFields[$field] = array(
376 str_replace("\n", " - ", $this->orig[$field]),
377 str_replace("\n", " - ", $this->values[$field]),
378 );
379 }
380 if (!is_null($setting)) {
381 $setting->save($this, $field, $this->values[$field]);
382 }
383 if (isset($this->watched[$field]) && $this->watched[$field]) {
384 WatchProfileUpdate::register($this->profile, $field);
385 }
386 }
387 }
388 $this->_saveData();
389
390 // Update the last modification date
391 XDB::execute('UPDATE profiles
392 SET last_change = NOW()
393 WHERE pid = {?}', $this->pid());
394 global $platal;
395 S::logger()->log('profil', $platal->pl_self(2));
396
397 /** If the update was made by a third party and the profile corresponds
398 * to a registered user, stores both former and new text.
399 * This will be daily sent to the user.
400 */
401 $owner = $this->profile->owner();
402 $user = S::user();
403 if ($owner->isActive() && $owner->id() != $user->id()) {
404 foreach ($changedFields as $field => $values) {
405 XDB::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
406 VALUES ({?}, {?}, {?}, {?}, {?})',
407 $this->pid(), $user->id(), $field, $values[0], $values[1]);
408 }
409 }
410 }
411
412 protected function checkChanges()
413 {
414 $newvalues = $this->values;
415 $this->values = array();
416 $this->fetchData();
417 $this->values = $newvalues;
418 $changes = false;
419 foreach ($this->settings as $field=>&$setting) {
420 if ($this->orig[$field] != $this->values[$field]) {
421 $this->changed[$field] = true;
422 $changes = true;
423 } else {
424 $this->changed[$field] = false;
425 }
426 }
427 return $changes;
428 }
429
430 protected function markChange()
431 {
432 }
433
434 public function template()
435 {
436 return 'profile/base.tpl';
437 }
438
439 public function pid()
440 {
441 return $this->profile->id();
442 }
443
444 public function hrpid()
445 {
446 return $this->profile->hrpid();
447 }
448
449 protected function _prepare(PlPage &$page, $id)
450 {
451 }
452
453 public function prepare(PlPage &$page, $id)
454 {
455 if (count($this->values) == 0) {
456 $this->fetchData();
457 }
458 foreach ($this->values as $field=>&$value) {
459 $page->assign($field, $value);
460 }
461 $this->_prepare($page, $id);
462 $page->assign('profile', $this->profile);
463 $page->assign('owner', $this->owner);
464 $page->assign('profile_page', $this->pg_template);
465 $page->assign('errors', $this->errors);
466 }
467
468 public function process(&$global_success)
469 {
470 $global_success = true;
471 $this->fetchData();
472 foreach ($this->settings as $field=>&$setting) {
473 $success = false;
474 if (!is_null($setting)) {
475 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
476 } else {
477 $success = true;
478 $this->values[$field] = Post::v($field, '');
479 }
480 $this->errors[$field] = !$success;
481 $global_success = $global_success && $success;
482 }
483 if ($global_success) {
484 if ($this->checkChanges()) {
485 $this->saveData();
486 $this->markChange();
487 }
488 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
489 }
490 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
491 . "de ton profil et de revalider ta demande.");
492 return PlWizard::CURRENT_PAGE;
493 }
494
495 public function success()
496 {
497 return 'Ton profil a bien été mis à jour.';
498 }
499 }
500
501 require_once dirname(__FILE__) . '/general.inc.php';
502 require_once dirname(__FILE__) . '/addresses.inc.php';
503 require_once dirname(__FILE__) . '/groups.inc.php';
504 require_once dirname(__FILE__) . '/decos.inc.php';
505 require_once dirname(__FILE__) . '/jobs.inc.php';
506 require_once dirname(__FILE__) . '/skills.inc.php';
507 require_once dirname(__FILE__) . '/mentor.inc.php';
508
509 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
510 ?>