Merge branch 'platal-0.10.2'
[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
41 abstract class ProfileNoSave implements ProfileSetting
42 {
43 public function save(ProfilePage &$page, $field, $new_value) { }
44 }
45
46 class ProfileSettingWeb 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 }
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
55 if (!$success) {
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');
58 }
59 return $value;
60 }
61 }
62
63 class ProfileSettingEmail 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);
71 $success = empty($value) || isvalid_email($value);
72 if (!$success) {
73 Platal::page()->trigError('Adresse Email invalide');
74 }
75 return $value;
76 }
77 }
78
79 class ProfileSettingNumber 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) {
89 Platal::page()->trigError('Numéro invalide');
90 }
91 return $value;
92 }
93 }
94
95
96 class ProfileSettingTel 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 }
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;
111 if (!$success) {
112 Platal::page()->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
113 }
114 return $value;
115 }
116 }
117
118 class ProfileSettingPhones implements ProfileSetting
119 {
120 private $tel;
121 private $pub;
122 protected $link_type;
123 protected $link_id;
124
125 public function __construct($type, $link_id)
126 {
127 $this->tel = new ProfileSettingTel();
128 $this->pub = new ProfileSettingPub();
129 $this->link_type = $type;
130 $this->link_id = $link_id;
131 }
132
133 public function value(ProfilePage &$page, $field, $value, &$success)
134 {
135 $success = true;
136 if (is_null($value)) {
137 $value = array();
138 $res = XDB::iterator('SELECT display_tel AS tel, tel_type AS type, pub, comment
139 FROM profile_phones
140 WHERE pid = {?} AND link_type = {?}
141 ORDER BY tel_id',
142 $page->pid(), $this->link_type);
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 }
155 }
156 foreach ($value as $key=>&$phone) {
157 if (isset($phone['removed']) && $phone['removed']) {
158 unset($value[$key]);
159 } else {
160 unset($phone['removed']);
161 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
162 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
163 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
164 $phone['type'] = 'fixed';
165 $s = false;
166 }
167 if (!$s) {
168 $phone['error'] = true;
169 $success = false;
170 }
171 if (!isset($phone['comment'])) {
172 $phone['comment'] = '';
173 }
174 }
175 }
176 return $value;
177 }
178
179 private function saveTel($pid, $telid, array &$phone)
180 {
181 if ($phone['tel'] != '') {
182 XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
183 search_tel, display_tel, pub, comment)
184 VALUES ({?}, {?}, {?}, {?}, {?},
185 {?}, {?}, {?}, {?})",
186 $pid, $this->link_type, $this->link_id, $telid, $phone['type'],
187 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
188 }
189 }
190
191 public function save(ProfilePage &$page, $field, $value)
192 {
193 XDB::execute("DELETE FROM profile_phones
194 WHERE pid = {?} AND link_type = {?} AND link_id = {?}",
195 $page->pid(), $this->link_type, $this->link_id);
196 $this->saveTels($page->pid(), $field, $value);
197 }
198
199 //Only saves phones without a delete operation
200 public function saveTels($pid, $field, $value)
201 {
202 foreach ($value as $telid=>&$phone) {
203 $this->saveTel($pid, $telid, $phone);
204 }
205 }
206 }
207
208 class ProfileSettingPub extends ProfileNoSave
209 {
210 public function value(ProfilePage &$page, $field, $value, &$success)
211 {
212 $success = true;
213 if (is_null($value)) {
214 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
215 }
216 if (!$value) {
217 $value = 'private';
218 } elseif ($value == 'on') { // Checkbox
219 $value = 'public';
220 }
221 return $value;
222 }
223 }
224
225 class ProfileSettingBool extends ProfileNoSave
226 {
227 public function value(ProfilePage &$page, $field, $value, &$success)
228 {
229 $success = true;
230 if (is_null($value)) {
231 $value = isset($page->values[$field]) ? $page->values[$field] : null;
232 }
233 return $value ? "1" : "";
234 }
235 }
236
237 class ProfileSettingDate extends ProfileNoSave
238 {
239 public function value(ProfilePage &$page, $field, $value, &$success)
240 {
241 $success = true;
242 if (is_null($value)) {
243 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
244 } else {
245 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
246 if (!$success) {
247 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
248 } else {
249 $day = (int)$matches[1];
250 $month = (int)$matches[2];
251 $year = (int)$matches[3];
252 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
253 if (!$success) {
254 Platal::page()->trigError("La date n'a pas une valeur valide");
255 }
256 }
257 }
258 return $value;
259 }
260 }
261
262 abstract class ProfileSettingGeocoding implements ProfileSetting
263 {
264 protected function geocodeAddress(array &$address, &$success)
265 {
266 require_once 'geocoding.inc.php';
267 $success = true;
268 if (isset($address['changed']) && $address['changed'] == 1) {
269 $gmapsGeocoder = new GMapsGeocoder();
270 $address = $gmapsGeocoder->getGeocodedAddress($address);
271 if (isset($address['geoloc'])) {
272 $success = false;
273 }
274 } elseif (@$address['changed'] && !@$address['text']) {
275 $address = empty_address();
276 $address['pub'] = 'private';
277 }
278 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
279 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
280 $mailer->assign('text', $address['text']);
281 $mailer->assign('geoloc', $address['geoloc']);
282 $mailer->send();
283 $gmapsGeocoder = new GMapsGeocoder();
284 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
285 }
286 }
287 }
288
289
290 abstract class ProfilePage implements PlWizardPage
291 {
292 protected $wizard;
293 protected $pg_template;
294 protected $settings = array(); // A set ProfileSetting objects
295 protected $errors = array(); // A set of boolean with the value check errors
296 protected $changed = array(); // A set of boolean indicating wether the value has been changed
297 protected $watched = array(); // A set of boolean indicating the fields that are watched
298
299 public $orig = array();
300 public $values = array();
301 public $profile = null;
302 public $owner = null;
303
304 public function __construct(PlWizard &$wiz)
305 {
306 $this->wizard =& $wiz;
307 $this->profile = $this->wizard->getUserData('profile');
308 $this->owner = $this->wizard->getUserData('owner');
309 }
310
311 protected function _fetchData()
312 {
313 }
314
315 protected function fetchData()
316 {
317 if (count($this->orig) > 0) {
318 $this->values = $this->orig;
319 return;
320 }
321
322 $this->_fetchData();
323 foreach ($this->settings as $field=>&$setting) {
324 $success = false;
325 if (!is_null($setting)) {
326 $this->values[$field] = $setting->value($this, $field, null, $success);
327 } else if (!isset($this->values[$field])) {
328 $this->values[$field] = S::v($field);
329 }
330 $this->errors[$field] = false;
331 }
332 $this->orig = $this->values;
333 }
334
335 protected function _saveData()
336 {
337 }
338
339 protected function saveData()
340 {
341 require_once 'notifs.inc.php';
342 foreach ($this->settings as $field=>&$setting) {
343 if (!is_null($setting) && $this->changed[$field]) {
344 $setting->save($this, $field, $this->values[$field]);
345 }
346 if ($this->changed[$field] && @$this->watched[$field]) {
347 WatchProfileUpdate::register($this->profile, $field);
348 }
349 }
350 $this->_saveData();
351
352 // Update the last modification date
353 XDB::execute('UPDATE profiles
354 SET last_change = NOW()
355 WHERE pid = {?}', $this->pid());
356 global $platal;
357 S::logger()->log('profil', $platal->pl_self(2));
358 }
359
360 protected function checkChanges()
361 {
362 $newvalues = $this->values;
363 $this->values = array();
364 $this->fetchData();
365 $this->values = $newvalues;
366 $changes = false;
367 foreach ($this->settings as $field=>&$setting) {
368 if ($this->orig[$field] != $this->values[$field]) {
369 $this->changed[$field] = true;
370 $changes = true;
371 } else {
372 $this->changed[$field] = false;
373 }
374 }
375 return $changes;
376 }
377
378 protected function markChange()
379 {
380 }
381
382 public function template()
383 {
384 return 'profile/base.tpl';
385 }
386
387 public function pid()
388 {
389 return $this->profile->id();
390 }
391
392 public function hrpid()
393 {
394 return $this->profile->hrpid();
395 }
396
397 protected function _prepare(PlPage &$page, $id)
398 {
399 }
400
401 public function prepare(PlPage &$page, $id)
402 {
403 if (count($this->values) == 0) {
404 $this->fetchData();
405 }
406 foreach ($this->values as $field=>&$value) {
407 $page->assign($field, $value);
408 }
409 $this->_prepare($page, $id);
410 $page->assign('profile', $this->profile);
411 $page->assign('owner', $this->owner);
412 $page->assign('profile_page', $this->pg_template);
413 $page->assign('errors', $this->errors);
414 }
415
416 public function process(&$global_success)
417 {
418 $global_success = true;
419 $this->fetchData();
420 foreach ($this->settings as $field=>&$setting) {
421 $success = false;
422 if (!is_null($setting)) {
423 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
424 } else {
425 $success = true;
426 $this->values[$field] = Post::v($field, '');
427 }
428 $this->errors[$field] = !$success;
429 $global_success = $global_success && $success;
430 }
431 if ($global_success) {
432 if ($this->checkChanges()) {
433 $this->saveData();
434 $this->markChange();
435 }
436 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
437 }
438 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
439 . "de ton profil et de revalider ta demande.");
440 return PlWizard::CURRENT_PAGE;
441 }
442
443 public function success()
444 {
445 return 'Ton profil a bien été mis à jour.';
446 }
447 }
448
449 require_once dirname(__FILE__) . '/general.inc.php';
450 require_once dirname(__FILE__) . '/addresses.inc.php';
451 require_once dirname(__FILE__) . '/groups.inc.php';
452 require_once dirname(__FILE__) . '/decos.inc.php';
453 require_once dirname(__FILE__) . '/jobs.inc.php';
454 require_once dirname(__FILE__) . '/skills.inc.php';
455 require_once dirname(__FILE__) . '/mentor.inc.php';
456
457 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
458 ?>