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