Adds phone number search
[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
FB
55 if (!$success) {
56 global $page;
a7d35093 57 $page->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
fd38b30e
FB
58 . ' et ne pas contenir de caractères interdits');
59 }
60 return $value;
61 }
62}
63
37d44b3b
FB
64class 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;
a7d35093 76 $page->trigError('Adresse Email invalide');
37d44b3b
FB
77 }
78 return $value;
79 }
80}
81
92446a53
PC
82class 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
37d44b3b 99
fd38b30e
FB
100class 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 }
a619333a 107 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
fd38b30e
FB
108 if (!$success) {
109 global $page;
a7d35093 110 $page->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
fd38b30e
FB
111 }
112 return $value;
113 }
114}
115
93553cea
FB
116class ProfilePub extends ProfileNoSave
117{
118 public function value(ProfilePage &$page, $field, $value, &$success)
119 {
120 $success = true;
121 if (is_null($value)) {
122 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
123 }
124 if (is_null($value) || !$value) {
125 $value = 'private';
126 } else if ($value == 'on') { // Checkbox
127 $value = 'public';
128 }
129 return $value;
130 }
131}
132
576777d7
FB
133class ProfileBool extends ProfileNoSave
134{
135 public function value(ProfilePage &$page, $field, $value, &$success)
136 {
137 $success = true;
138 if (is_null($value)) {
c24a06aa 139 $value = @$page->values[$field];
576777d7 140 }
ee12da4e 141 return $value ? "1" : "";
576777d7
FB
142 }
143}
144
7bff4cb0
FB
145class ProfileDate extends ProfileNoSave
146{
147 public function value(ProfilePage &$page, $field, $value, &$success)
148 {
149 $success = true;
150 if (is_null($value)) {
151 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
152 } else {
153 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
154 if (!$success) {
155 global $page;
a7d35093 156 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
157 } else {
158 $day = (int)$matches[1];
159 $month = (int)$matches[2];
160 $year = (int)$matches[3];
161 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
162 if (!$success) {
163 global $page;
a7d35093 164 $page->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
165 }
166 }
167 }
168 return $value;
169 }
170}
171
37d44b3b
FB
172abstract class ProfileGeoloc implements ProfileSetting
173{
174 protected function geolocAddress(array &$address, &$success)
175 {
176 require_once 'geoloc.inc.php';
177 $success = true;
178 unset($address['geoloc']);
179 unset($address['geoloc_cityid']);
180 if (@$address['parsevalid']
181 || (@$address['text'] && @$address['changed'])
182 || (@$address['text'] && !@$address['cityid'])) {
183 $address = array_merge($address, empty_address());
184 $new = get_address_infos(@$address['text']);
1879c7b2
FB
185 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
186 || (@$address['parsevalid'] && @$address['cityid'])) {
37d44b3b 187 $address = array_merge($address, $new);
f93fb300 188 $address['checked'] = true;
1879c7b2
FB
189 } else if (@$address['parsevalid']) {
190 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 191 $address['checked'] = true;
b71f7275 192 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
f93fb300
FB
193 $mailer->assign('text', get_address_text($address));
194 $mailer->assign('geoloc', $geotxt);
195 $mailer->send();
196 } else if (@$address['changed'] || !@$address['checked']) {
37d44b3b
FB
197 $success = false;
198 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 199 $address['checked'] = false;
37d44b3b
FB
200 $address['geoloc'] = $geotxt;
201 $address['geoloc_cityid'] = $new['cityid'];
f93fb300
FB
202 } else {
203 $address = array_merge($address, cut_address(@$address['text']));
204 $address['checked'] = true;
37d44b3b
FB
205 }
206 }
de0485fd
FB
207 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
208 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
37d44b3b
FB
209 $address['text'] = get_address_text($address);
210 }
211}
212
213
fd38b30e 214abstract class ProfilePage implements PlWizardPage
f118f685
FB
215{
216 protected $wizard;
fd38b30e
FB
217 protected $pg_template;
218 protected $settings = array(); // A set ProfileSetting objects
93553cea 219 protected $errors = array(); // A set of boolean with the value check errors
576777d7 220 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 221 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 222
93553cea 223 public $orig = array();
fd38b30e 224 public $values = array();
f118f685
FB
225
226 public function __construct(PlWizard &$wiz)
227 {
228 $this->wizard =& $wiz;
229 }
230
7c2e0f0d
FB
231 protected function _fetchData()
232 {
233 }
234
fd38b30e
FB
235 protected function fetchData()
236 {
93553cea
FB
237 if (count($this->orig) > 0) {
238 $this->values = $this->orig;
239 return;
240 }
7c2e0f0d
FB
241
242 $this->_fetchData();
93553cea
FB
243 foreach ($this->settings as $field=>&$setting) {
244 $success = false;
245 if (!is_null($setting)) {
246 $this->values[$field] = $setting->value($this, $field, null, $success);
247 } else if (!isset($this->values[$field])) {
248 $this->values[$field] = S::v($field);
249 }
250 $this->errors[$field] = false;
251 }
252 $this->orig = $this->values;
fd38b30e
FB
253 }
254
7c2e0f0d
FB
255 protected function _saveData()
256 {
257 }
258
fd38b30e
FB
259 protected function saveData()
260 {
a2a1c2f2 261 require_once 'notifs.inc.php';
93553cea 262 foreach ($this->settings as $field=>&$setting) {
576777d7 263 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
264 $setting->save($this, $field, $this->values[$field]);
265 }
a2a1c2f2
FB
266 if ($this->changed[$field] && @$this->watched[$field]) {
267 register_profile_update(S::i('uid'), $field);
268 }
93553cea 269 }
7c2e0f0d 270 $this->_saveData();
576777d7
FB
271
272 // Update the last modification date
273 XDB::execute('REPLACE INTO user_changes
274 SET user_id = {?}', S::v('uid'));
3ebe4a0a 275 if (!S::has('suid')) {
3ebe4a0a
FB
276 register_watch_op(S::i('uid'), WATCH_FICHE);
277 }
576777d7
FB
278 global $platal;
279 $log =& $_SESSION['log'];
280 $log->log('profil', $platal->pl_self(1));
93553cea
FB
281 }
282
283 protected function checkChanges()
284 {
285 $newvalues = $this->values;
286 $this->values = array();
287 $this->fetchData();
288 $this->values = $newvalues;
576777d7 289 $changes = false;
93553cea
FB
290 foreach ($this->settings as $field=>&$setting) {
291 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
292 $this->changed[$field] = true;
293 $changes = true;
294 } else {
295 $this->changed[$field] = false;
93553cea
FB
296 }
297 }
576777d7 298 return $changes;
93553cea
FB
299 }
300
301 protected function markChange()
302 {
fd38b30e
FB
303 }
304
f118f685
FB
305 public function template()
306 {
fd38b30e 307 return 'profile/base.tpl';
f118f685
FB
308 }
309
7c2e0f0d
FB
310 protected function _prepare(PlatalPage &$page, $id)
311 {
312 }
313
ddb64990 314 public function prepare(PlatalPage &$page, $id)
f118f685 315 {
fd38b30e
FB
316 if (count($this->values) == 0) {
317 $this->fetchData();
fd38b30e
FB
318 }
319 foreach ($this->values as $field=>&$value) {
320 $page->assign($field, $value);
321 }
7c2e0f0d 322 $this->_prepare($page, $id);
fd38b30e 323 $page->assign('profile_page', $this->pg_template);
93553cea 324 $page->assign('errors', $this->errors);
f118f685
FB
325 }
326
327 public function process()
328 {
fd38b30e
FB
329 $global_success = true;
330 $this->fetchData();
331 foreach ($this->settings as $field=>&$setting) {
332 $success = false;
93553cea 333 if (!is_null($setting)) {
85cc366b 334 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
335 } else {
336 $success = true;
85cc366b 337 $this->values[$field] = Post::v($field, '');
93553cea
FB
338 }
339 $this->errors[$field] = !$success;
fd38b30e
FB
340 $global_success = $global_success && $success;
341 }
342 if ($global_success) {
93553cea
FB
343 if ($this->checkChanges()) {
344 $this->saveData();
345 $this->markChange();
fd38b30e 346 }
93553cea 347 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e
FB
348 }
349 global $page;
a7d35093 350 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
fd38b30e 351 . "de ton profil et de revalider ta demande");
f118f685
FB
352 return PlWizard::CURRENT_PAGE;
353 }
354}
355
fd38b30e 356require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 357require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 358require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 359require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 360require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 361require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 362require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
363
364// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
365?>