Adds a common template to add phone numbers in the different pages of the profile
[platal.git] / modules / profile / page.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 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 }
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
55 if (!$success) {
56 global $page;
57 $page->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
58 . ' et ne pas contenir de caractères interdits');
59 }
60 return $value;
61 }
62 }
63
64 class ProfileEmail extends ProfileNoSave
65 {
66 public function value(ProfilePage &$page, $field, $value, &$success)
67 {
68 if (is_null($value)) {
69 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
70 }
71 $value = trim($value);
72 require_once 'xorg.misc.inc.php';
73 $success = empty($value) || isvalid_email($value);
74 if (!$success) {
75 global $page;
76 $page->trigError('Adresse Email invalide');
77 }
78 return $value;
79 }
80 }
81
82 class ProfileNumber extends ProfileNoSave
83 {
84 public function value(ProfilePage &$page, $field, $value, &$success)
85 {
86 if (is_null($value)) {
87 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
88 }
89 $value = trim($value);
90 $success = empty($value) || is_numeric($value);
91 if (!$success) {
92 global $page;
93 $page->trigError('Numéro invalide');
94 }
95 return $value;
96 }
97 }
98
99
100 class ProfileTel extends ProfileNoSave
101 {
102 public function value(ProfilePage &$page, $field, $value, &$success)
103 {
104 if (is_null($value)) {
105 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
106 }
107 require_once('profil.func.inc.php');
108 $value = format_phone_number($value);
109 if($value == '') {
110 $success = true;
111 return $value;
112 }
113 $value = format_display_number($value,$error);
114 $success = !$error;
115 if (!$success) {
116 global $page;
117 $page->trigError('Le préfixe international du numéro de téléphone est inconnu. ');
118 }
119 return $value;
120 }
121 }
122
123 class ProfilePhones implements ProfileSetting
124 {
125 private $tel;
126 private $pub;
127 protected $link_type;
128 protected $link_id;
129
130 public function __construct($type, $id)
131 {
132 $this->tel = new ProfileTel();
133 $this->pub = new ProfilePub();
134 $this->link_type = $type;
135 $this->link_id = $id;
136 }
137
138 public function value(ProfilePage &$page, $field, $value, &$success)
139 {
140 $success = true;
141 if (is_null($value)) {
142 $value = isset($page->values[$field]) ? $page->values[$field] : array();
143 }
144 if (!is_array($value)) {
145 $value = array();
146 }
147 foreach ($value as $key=>&$phone) {
148 if (@$phone['removed']) {
149 unset($value[$key]);
150 } else {
151 $phone['pub'] = $this->pub->value($page, 'pub', $phone['pub'], $s);
152 $phone['tel'] = $this->tel->value($page, 'tel', $phone['tel'], $s);
153 if(!isset($phone['type']) || ($phone['type'] != 'fixed' && $phone['type'] != 'mobile' && $phone['type'] != 'fax')) {
154 $phone['type'] = 'fixed';
155 $s = false;
156 }
157 if (!$s) {
158 $phone['error'] = true;
159 $success = false;
160 }
161 if (!isset($phone['comment'])) {
162 $phone['comment'] = '';
163 }
164 }
165 }
166 return $value;
167 }
168
169 private function saveTel($telid, array &$phone)
170 {
171 if ($phone['tel'] != '') {
172 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id, tel_type,
173 search_tel, display_tel, pub, comment)
174 VALUES ({?}, {?}, {?}, {?}, {?},
175 {?}, {?}, {?}, {?})",
176 S::i('uid'), $this->link_type, $this->link_id, $telid, $phone['type'],
177 format_phone_number($phone['tel']), $phone['tel'], $phone['pub'], $phone['comment']);
178 }
179 }
180
181 public function save(ProfilePage &$page, $field, $value)
182 {
183 XDB::execute("DELETE FROM profile_phones
184 WHERE uid = {?} AND link_type = {?} AND link_id = {?}",
185 S::i('uid'), $this->link_type, $this->link_id);
186 $this->saveTels($field, $value);
187 }
188
189 //Only saves phones without a delete operation
190 public function saveTels($field, $value)
191 {
192 foreach ($value as $telid=>&$phone) {
193 $this->saveTel($telid, $phone);
194 }
195 }
196 }
197
198 class ProfilePub extends ProfileNoSave
199 {
200 public function value(ProfilePage &$page, $field, $value, &$success)
201 {
202 $success = true;
203 if (is_null($value)) {
204 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
205 }
206 if (is_null($value) || !$value) {
207 $value = 'private';
208 } else if ($value == 'on') { // Checkbox
209 $value = 'public';
210 }
211 return $value;
212 }
213 }
214
215 class ProfileBool extends ProfileNoSave
216 {
217 public function value(ProfilePage &$page, $field, $value, &$success)
218 {
219 $success = true;
220 if (is_null($value)) {
221 $value = @$page->values[$field];
222 }
223 return $value ? "1" : "";
224 }
225 }
226
227 class ProfileDate extends ProfileNoSave
228 {
229 public function value(ProfilePage &$page, $field, $value, &$success)
230 {
231 $success = true;
232 if (is_null($value)) {
233 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
234 } else {
235 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
236 if (!$success) {
237 global $page;
238 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
239 } else {
240 $day = (int)$matches[1];
241 $month = (int)$matches[2];
242 $year = (int)$matches[3];
243 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
244 if (!$success) {
245 global $page;
246 $page->trigError("La date n'a pas une valeur valide");
247 }
248 }
249 }
250 return $value;
251 }
252 }
253
254 abstract class ProfileGeoloc implements ProfileSetting
255 {
256 protected function geolocAddress(array &$address, &$success)
257 {
258 require_once 'geoloc.inc.php';
259 $success = true;
260 unset($address['geoloc']);
261 unset($address['geoloc_cityid']);
262 if (@$address['parsevalid']
263 || (@$address['text'] && @$address['changed'])
264 || (@$address['text'] && !@$address['cityid'])) {
265 $address = array_merge($address, empty_address());
266 $new = get_address_infos(@$address['text']);
267 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
268 || (@$address['parsevalid'] && @$address['cityid'])) {
269 $address = array_merge($address, $new);
270 $address['checked'] = true;
271 } else if (@$address['parsevalid']) {
272 $address = array_merge($address, cut_address(@$address['text']));
273 $address['checked'] = true;
274 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
275 $mailer->assign('text', get_address_text($address));
276 $mailer->assign('geoloc', $geotxt);
277 $mailer->send();
278 } else if (@$address['changed'] || !@$address['checked']) {
279 $success = false;
280 $address = array_merge($address, cut_address(@$address['text']));
281 $address['checked'] = false;
282 $address['geoloc'] = $geotxt;
283 $address['geoloc_cityid'] = $new['cityid'];
284 } else {
285 $address = array_merge($address, cut_address(@$address['text']));
286 $address['checked'] = true;
287 }
288 }
289 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
290 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
291 $address['text'] = get_address_text($address);
292 }
293 }
294
295
296 abstract class ProfilePage implements PlWizardPage
297 {
298 protected $wizard;
299 protected $pg_template;
300 protected $settings = array(); // A set ProfileSetting objects
301 protected $errors = array(); // A set of boolean with the value check errors
302 protected $changed = array(); // A set of boolean indicating wether the value has been changed
303 protected $watched = array(); // A set of boolean indicating the fields that are watched
304
305 public $orig = array();
306 public $values = array();
307
308 public function __construct(PlWizard &$wiz)
309 {
310 $this->wizard =& $wiz;
311 }
312
313 protected function _fetchData()
314 {
315 }
316
317 protected function fetchData()
318 {
319 if (count($this->orig) > 0) {
320 $this->values = $this->orig;
321 return;
322 }
323
324 $this->_fetchData();
325 foreach ($this->settings as $field=>&$setting) {
326 $success = false;
327 if (!is_null($setting)) {
328 $this->values[$field] = $setting->value($this, $field, null, $success);
329 } else if (!isset($this->values[$field])) {
330 $this->values[$field] = S::v($field);
331 }
332 $this->errors[$field] = false;
333 }
334 $this->orig = $this->values;
335 }
336
337 protected function _saveData()
338 {
339 }
340
341 protected function saveData()
342 {
343 require_once 'notifs.inc.php';
344 foreach ($this->settings as $field=>&$setting) {
345 if (!is_null($setting) && $this->changed[$field]) {
346 $setting->save($this, $field, $this->values[$field]);
347 }
348 if ($this->changed[$field] && @$this->watched[$field]) {
349 register_profile_update(S::i('uid'), $field);
350 }
351 }
352 $this->_saveData();
353
354 // Update the last modification date
355 XDB::execute('REPLACE INTO user_changes
356 SET user_id = {?}', S::v('uid'));
357 if (!S::has('suid')) {
358 register_watch_op(S::i('uid'), WATCH_FICHE);
359 }
360 global $platal;
361 $log =& $_SESSION['log'];
362 $log->log('profil', $platal->pl_self(1));
363 }
364
365 protected function checkChanges()
366 {
367 $newvalues = $this->values;
368 $this->values = array();
369 $this->fetchData();
370 $this->values = $newvalues;
371 $changes = false;
372 foreach ($this->settings as $field=>&$setting) {
373 if ($this->orig[$field] != $this->values[$field]) {
374 $this->changed[$field] = true;
375 $changes = true;
376 } else {
377 $this->changed[$field] = false;
378 }
379 }
380 return $changes;
381 }
382
383 protected function markChange()
384 {
385 }
386
387 public function template()
388 {
389 return 'profile/base.tpl';
390 }
391
392 protected function _prepare(PlatalPage &$page, $id)
393 {
394 }
395
396 public function prepare(PlatalPage &$page, $id)
397 {
398 if (count($this->values) == 0) {
399 $this->fetchData();
400 }
401 foreach ($this->values as $field=>&$value) {
402 $page->assign($field, $value);
403 }
404 $this->_prepare($page, $id);
405 $page->assign('profile_page', $this->pg_template);
406 $page->assign('errors', $this->errors);
407 }
408
409 public function process()
410 {
411 $global_success = true;
412 $this->fetchData();
413 foreach ($this->settings as $field=>&$setting) {
414 $success = false;
415 if (!is_null($setting)) {
416 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
417 } else {
418 $success = true;
419 $this->values[$field] = Post::v($field, '');
420 }
421 $this->errors[$field] = !$success;
422 $global_success = $global_success && $success;
423 }
424 if ($global_success) {
425 if ($this->checkChanges()) {
426 $this->saveData();
427 $this->markChange();
428 }
429 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
430 }
431 global $page;
432 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
433 . "de ton profil et de revalider ta demande");
434 return PlWizard::CURRENT_PAGE;
435 }
436 }
437
438 require_once dirname(__FILE__) . '/general.inc.php';
439 require_once dirname(__FILE__) . '/addresses.inc.php';
440 require_once dirname(__FILE__) . '/groups.inc.php';
441 require_once dirname(__FILE__) . '/decos.inc.php';
442 require_once dirname(__FILE__) . '/jobs.inc.php';
443 require_once dirname(__FILE__) . '/skills.inc.php';
444 require_once dirname(__FILE__) . '/mentor.inc.php';
445
446 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
447 ?>