Integrates new phone table in profile and other pages
[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 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
140 class ProfileBool extends ProfileNoSave
141 {
142 public function value(ProfilePage &$page, $field, $value, &$success)
143 {
144 $success = true;
145 if (is_null($value)) {
146 $value = @$page->values[$field];
147 }
148 return $value ? "1" : "";
149 }
150 }
151
152 class 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;
163 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
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;
171 $page->trigError("La date n'a pas une valeur valide");
172 }
173 }
174 }
175 return $value;
176 }
177 }
178
179 abstract 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']);
192 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
193 || (@$address['parsevalid'] && @$address['cityid'])) {
194 $address = array_merge($address, $new);
195 $address['checked'] = true;
196 } else if (@$address['parsevalid']) {
197 $address = array_merge($address, cut_address(@$address['text']));
198 $address['checked'] = true;
199 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
200 $mailer->assign('text', get_address_text($address));
201 $mailer->assign('geoloc', $geotxt);
202 $mailer->send();
203 } else if (@$address['changed'] || !@$address['checked']) {
204 $success = false;
205 $address = array_merge($address, cut_address(@$address['text']));
206 $address['checked'] = false;
207 $address['geoloc'] = $geotxt;
208 $address['geoloc_cityid'] = $new['cityid'];
209 } else {
210 $address = array_merge($address, cut_address(@$address['text']));
211 $address['checked'] = true;
212 }
213 }
214 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
215 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
216 $address['text'] = get_address_text($address);
217 }
218 }
219
220
221 abstract class ProfilePage implements PlWizardPage
222 {
223 protected $wizard;
224 protected $pg_template;
225 protected $settings = array(); // A set ProfileSetting objects
226 protected $errors = array(); // A set of boolean with the value check errors
227 protected $changed = array(); // A set of boolean indicating wether the value has been changed
228 protected $watched = array(); // A set of boolean indicating the fields that are watched
229
230 public $orig = array();
231 public $values = array();
232
233 public function __construct(PlWizard &$wiz)
234 {
235 $this->wizard =& $wiz;
236 }
237
238 protected function _fetchData()
239 {
240 }
241
242 protected function fetchData()
243 {
244 if (count($this->orig) > 0) {
245 $this->values = $this->orig;
246 return;
247 }
248
249 $this->_fetchData();
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;
260 }
261
262 protected function _saveData()
263 {
264 }
265
266 protected function saveData()
267 {
268 require_once 'notifs.inc.php';
269 foreach ($this->settings as $field=>&$setting) {
270 if (!is_null($setting) && $this->changed[$field]) {
271 $setting->save($this, $field, $this->values[$field]);
272 }
273 if ($this->changed[$field] && @$this->watched[$field]) {
274 register_profile_update(S::i('uid'), $field);
275 }
276 }
277 $this->_saveData();
278
279 // Update the last modification date
280 XDB::execute('REPLACE INTO user_changes
281 SET user_id = {?}', S::v('uid'));
282 if (!S::has('suid')) {
283 register_watch_op(S::i('uid'), WATCH_FICHE);
284 }
285 global $platal;
286 $log =& $_SESSION['log'];
287 $log->log('profil', $platal->pl_self(1));
288 }
289
290 protected function checkChanges()
291 {
292 $newvalues = $this->values;
293 $this->values = array();
294 $this->fetchData();
295 $this->values = $newvalues;
296 $changes = false;
297 foreach ($this->settings as $field=>&$setting) {
298 if ($this->orig[$field] != $this->values[$field]) {
299 $this->changed[$field] = true;
300 $changes = true;
301 } else {
302 $this->changed[$field] = false;
303 }
304 }
305 return $changes;
306 }
307
308 protected function markChange()
309 {
310 }
311
312 public function template()
313 {
314 return 'profile/base.tpl';
315 }
316
317 protected function _prepare(PlatalPage &$page, $id)
318 {
319 }
320
321 public function prepare(PlatalPage &$page, $id)
322 {
323 if (count($this->values) == 0) {
324 $this->fetchData();
325 }
326 foreach ($this->values as $field=>&$value) {
327 $page->assign($field, $value);
328 }
329 $this->_prepare($page, $id);
330 $page->assign('profile_page', $this->pg_template);
331 $page->assign('errors', $this->errors);
332 }
333
334 public function process()
335 {
336 $global_success = true;
337 $this->fetchData();
338 foreach ($this->settings as $field=>&$setting) {
339 $success = false;
340 if (!is_null($setting)) {
341 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
342 } else {
343 $success = true;
344 $this->values[$field] = Post::v($field, '');
345 }
346 $this->errors[$field] = !$success;
347 $global_success = $global_success && $success;
348 }
349 if ($global_success) {
350 if ($this->checkChanges()) {
351 $this->saveData();
352 $this->markChange();
353 }
354 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
355 }
356 global $page;
357 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
358 . "de ton profil et de revalider ta demande");
359 return PlWizard::CURRENT_PAGE;
360 }
361 }
362
363 require_once dirname(__FILE__) . '/general.inc.php';
364 require_once dirname(__FILE__) . '/addresses.inc.php';
365 require_once dirname(__FILE__) . '/groups.inc.php';
366 require_once dirname(__FILE__) . '/decos.inc.php';
367 require_once dirname(__FILE__) . '/jobs.inc.php';
368 require_once dirname(__FILE__) . '/skills.inc.php';
369 require_once dirname(__FILE__) . '/mentor.inc.php';
370
371 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
372 ?>