Telephone validation was triggering an invalid error when the profile
[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 /** Get text from the value.
41 */
42 public function getText($value);
43 }
44
45 abstract class ProfileNoSave implements ProfileSetting
46 {
47 public function save(ProfilePage &$page, $field, $new_value) { }
48
49 public function getText($value) {
50 return $value;
51 }
52 }
53
54 class ProfileSettingWeb extends ProfileNoSave
55 {
56 public function value(ProfilePage &$page, $field, $value, &$success)
57 {
58 if (is_null($value)) {
59 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
60 }
61 $value = trim($value);
62 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
63 if (!$success) {
64 Platal::page()->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
65 . ' et ne pas contenir de caractères interdits');
66 }
67 return $value;
68 }
69 }
70
71 class ProfileSettingEmail extends ProfileNoSave
72 {
73 public function value(ProfilePage &$page, $field, $value, &$success)
74 {
75 if (is_null($value)) {
76 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
77 }
78 $value = trim($value);
79 $success = empty($value) || isvalid_email($value);
80 if (!$success) {
81 Platal::page()->trigError('Adresse Email invalide');
82 }
83 return $value;
84 }
85 }
86
87 class ProfileSettingNumber extends ProfileNoSave
88 {
89 public function value(ProfilePage &$page, $field, $value, &$success)
90 {
91 if (is_null($value)) {
92 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
93 }
94 $value = trim($value);
95 $success = empty($value) || is_numeric($value);
96 if (!$success) {
97 Platal::page()->trigError('Numéro invalide');
98 }
99 return $value;
100 }
101 }
102
103 class ProfileSettingPhones implements ProfileSetting
104 {
105 public function value(ProfilePage &$page, $field, $value, &$success)
106 {
107 $success = true;
108 $phones = array();
109
110 if (is_null($value)) {
111 $it = Phone::iterate(array($page->pid()), array(Phone::LINK_PROFILE), array(0));
112 while ($phone = $it->next()) {
113 $success = ($phone->format() && $success);
114 $phones[] = $phone->toFormArray();
115 }
116 if (count($phones) == 0) {
117 $phone = new Phone();
118 $phones[] = $phone->toFormArray();
119 }
120 return $phones;
121 }
122
123 return Phone::formatFormArray($value, $success);
124 }
125
126 public function save(ProfilePage &$page, $field, $value)
127 {
128 Phone::deletePhones($page->pid(), Phone::LINK_PROFILE);
129 Phone::savePhones($value, $page->pid(), Phone::LINK_PROFILE);
130 }
131
132 public function getText($value)
133 {
134 return Phone::formArrayToString($value);
135 }
136 }
137
138 class ProfileSettingPub extends ProfileNoSave
139 {
140 public function value(ProfilePage &$page, $field, $value, &$success)
141 {
142 $success = true;
143 if (is_null($value)) {
144 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
145 }
146 if (!$value) {
147 $value = 'private';
148 } elseif ($value == 'on') { // Checkbox
149 $value = 'public';
150 }
151 return $value;
152 }
153
154 public function getText($value) {
155 return $value;
156 }
157 }
158
159 class ProfileSettingBool extends ProfileNoSave
160 {
161 public function value(ProfilePage &$page, $field, $value, &$success)
162 {
163 $success = true;
164 if (is_null($value)) {
165 $value = isset($page->values[$field]) ? $page->values[$field] : null;
166 }
167 return $value ? "1" : "";
168 }
169 }
170
171 class ProfileSettingDate extends ProfileNoSave
172 {
173 public function value(ProfilePage &$page, $field, $value, &$success)
174 {
175 $success = true;
176 if (is_null($value)) {
177 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
178 } else {
179 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
180 if (!$success) {
181 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
182 } else {
183 $day = (int)$matches[1];
184 $month = (int)$matches[2];
185 $year = (int)$matches[3];
186 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
187 if (!$success) {
188 Platal::page()->trigError("La date n'a pas une valeur valide");
189 }
190 }
191 }
192 return $value;
193 }
194
195 public static function toSQLDate($value)
196 {
197 return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value);
198 }
199 }
200
201 abstract class ProfilePage implements PlWizardPage
202 {
203 protected $wizard;
204 protected $pg_template;
205 protected $settings = array(); // A set ProfileSetting objects
206 protected $errors = array(); // A set of boolean with the value check errors
207 protected $changed = array(); // A set of boolean indicating wether the value has been changed
208 protected $watched = array(); // A set of boolean indicating the fields that are watched
209
210 public $orig = array();
211 public $values = array();
212 public $profile = null;
213 public $owner = null;
214
215 public function __construct(PlWizard &$wiz)
216 {
217 $this->wizard =& $wiz;
218 $this->profile = $this->wizard->getUserData('profile');
219 $this->owner = $this->wizard->getUserData('owner');
220 }
221
222 protected function _fetchData()
223 {
224 }
225
226 protected function fetchData()
227 {
228 if (count($this->orig) > 0) {
229 $this->values = $this->orig;
230 return;
231 }
232
233 $this->_fetchData();
234 foreach ($this->settings as $field=>&$setting) {
235 $success = false;
236 if (!is_null($setting)) {
237 $this->values[$field] = $setting->value($this, $field, null, $success);
238 } else if (!isset($this->values[$field])) {
239 $this->values[$field] = S::v($field);
240 }
241 $this->errors[$field] = false;
242 }
243 $this->orig = $this->values;
244 }
245
246 protected function _saveData()
247 {
248 }
249
250 protected function saveData()
251 {
252 require_once 'notifs.inc.php';
253 $changedFields = array();
254 foreach ($this->settings as $field=>&$setting) {
255 if ($this->changed[$field]) {
256 if (!is_null($setting)) {
257 $changedFields[$field] = array(
258 str_replace("\n", " - ", $setting->getText($this->orig[$field])),
259 str_replace("\n", " - ", $setting->getText($this->values[$field])),
260 );
261 } else {
262 $changedFields[$field] = array(
263 str_replace("\n", " - ", $this->orig[$field]),
264 str_replace("\n", " - ", $this->values[$field]),
265 );
266 }
267 if (!is_null($setting)) {
268 $setting->save($this, $field, $this->values[$field]);
269 }
270 if (isset($this->watched[$field]) && $this->watched[$field]) {
271 WatchProfileUpdate::register($this->profile, $field);
272 }
273 }
274 }
275 $this->_saveData();
276
277 // Update the last modification date
278 XDB::execute('UPDATE profiles
279 SET last_change = NOW()
280 WHERE pid = {?}', $this->pid());
281 global $platal;
282 S::logger()->log('profil', $platal->pl_self(2));
283
284 /** If the update was made by a third party and the profile corresponds
285 * to a registered user, stores both former and new text.
286 * This will be daily sent to the user.
287 */
288 $owner = $this->profile->owner();
289 $user = S::user();
290 if ($owner->isActive() && $owner->id() != $user->id()) {
291 foreach ($changedFields as $field => $values) {
292 XDB::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
293 VALUES ({?}, {?}, {?}, {?}, {?})',
294 $this->pid(), $user->id(), $field, $values[0], $values[1]);
295 }
296 }
297 }
298
299 protected function checkChanges()
300 {
301 $newvalues = $this->values;
302 $this->values = array();
303 $this->fetchData();
304 $this->values = $newvalues;
305 $changes = false;
306 foreach ($this->settings as $field=>&$setting) {
307 if ($this->orig[$field] != $this->values[$field]) {
308 $this->changed[$field] = true;
309 $changes = true;
310 } else {
311 $this->changed[$field] = false;
312 }
313 }
314 return $changes;
315 }
316
317 protected function markChange()
318 {
319 }
320
321 public function template()
322 {
323 return 'profile/base.tpl';
324 }
325
326 public function pid()
327 {
328 return $this->profile->id();
329 }
330
331 public function hrpid()
332 {
333 return $this->profile->hrpid();
334 }
335
336 protected function _prepare(PlPage &$page, $id)
337 {
338 }
339
340 public function prepare(PlPage &$page, $id)
341 {
342 if (count($this->values) == 0) {
343 $this->fetchData();
344 }
345 foreach ($this->values as $field=>&$value) {
346 $page->assign($field, $value);
347 }
348 $this->_prepare($page, $id);
349 $page->assign('profile', $this->profile);
350 $page->assign('owner', $this->owner);
351 $page->assign('profile_page', $this->pg_template);
352 $page->assign('errors', $this->errors);
353 }
354
355 public function process(&$global_success)
356 {
357 $global_success = true;
358 $this->fetchData();
359 foreach ($this->settings as $field=>&$setting) {
360 $success = false;
361 if (!is_null($setting)) {
362 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
363 } else {
364 $success = true;
365 $this->values[$field] = Post::v($field, '');
366 }
367 $this->errors[$field] = !$success;
368 $global_success = $global_success && $success;
369 }
370 if ($global_success) {
371 if ($this->checkChanges()) {
372 $this->saveData();
373 $this->markChange();
374 }
375 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
376 }
377 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
378 . "de ton profil et de revalider ta demande.");
379 return PlWizard::CURRENT_PAGE;
380 }
381
382 public function success()
383 {
384 return 'Ton profil a bien été mis à jour.';
385 }
386 }
387
388 require_once dirname(__FILE__) . '/general.inc.php';
389 require_once dirname(__FILE__) . '/addresses.inc.php';
390 require_once dirname(__FILE__) . '/groups.inc.php';
391 require_once dirname(__FILE__) . '/decos.inc.php';
392 require_once dirname(__FILE__) . '/jobs.inc.php';
393 require_once dirname(__FILE__) . '/skills.inc.php';
394 require_once dirname(__FILE__) . '/mentor.inc.php';
395
396 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
397 ?>