Allow edition of the death date in the profile edition page.
[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 return Phone::formArrayToString($value);
134 }
135 }
136
137 class ProfileSettingPub extends ProfileNoSave
138 {
139 public function value(ProfilePage &$page, $field, $value, &$success)
140 {
141 $success = true;
142 if (is_null($value)) {
143 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
144 }
145 if (!$value) {
146 $value = 'private';
147 } elseif ($value == 'on') { // Checkbox
148 $value = 'public';
149 }
150 return $value;
151 }
152
153 public function getText($value) {
154 return $value;
155 }
156 }
157
158 class ProfileSettingBool extends ProfileNoSave
159 {
160 public function value(ProfilePage &$page, $field, $value, &$success)
161 {
162 $success = true;
163 if (is_null($value)) {
164 $value = isset($page->values[$field]) ? $page->values[$field] : null;
165 }
166 return $value ? "1" : "";
167 }
168 }
169
170 class ProfileSettingDate extends ProfileNoSave
171 {
172 public function value(ProfilePage &$page, $field, $value, &$success)
173 {
174 $success = true;
175 if (is_null($value)) {
176 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
177 } else {
178 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
179 if (!$success) {
180 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
181 } else {
182 $day = (int)$matches[1];
183 $month = (int)$matches[2];
184 $year = (int)$matches[3];
185 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
186 if (!$success) {
187 Platal::page()->trigError("La date n'a pas une valeur valide");
188 }
189 }
190 }
191 return $value;
192 }
193
194 public static function toSQLDate($value)
195 {
196 return preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $value);
197 }
198 }
199
200 abstract class ProfilePage implements PlWizardPage
201 {
202 protected $wizard;
203 protected $pg_template;
204 protected $settings = array(); // A set ProfileSetting objects
205 protected $errors = array(); // A set of boolean with the value check errors
206 protected $changed = array(); // A set of boolean indicating wether the value has been changed
207 protected $watched = array(); // A set of boolean indicating the fields that are watched
208
209 public $orig = array();
210 public $values = array();
211 public $profile = null;
212 public $owner = null;
213
214 public function __construct(PlWizard &$wiz)
215 {
216 $this->wizard =& $wiz;
217 $this->profile = $this->wizard->getUserData('profile');
218 $this->owner = $this->wizard->getUserData('owner');
219 }
220
221 protected function _fetchData()
222 {
223 }
224
225 protected function fetchData()
226 {
227 if (count($this->orig) > 0) {
228 $this->values = $this->orig;
229 return;
230 }
231
232 $this->_fetchData();
233 foreach ($this->settings as $field=>&$setting) {
234 $success = false;
235 if (!is_null($setting)) {
236 $this->values[$field] = $setting->value($this, $field, null, $success);
237 } else if (!isset($this->values[$field])) {
238 $this->values[$field] = S::v($field);
239 }
240 $this->errors[$field] = false;
241 }
242 $this->orig = $this->values;
243 }
244
245 protected function _saveData()
246 {
247 }
248
249 protected function saveData()
250 {
251 require_once 'notifs.inc.php';
252 $changedFields = array();
253 foreach ($this->settings as $field=>&$setting) {
254 if ($this->changed[$field]) {
255 if (!is_null($setting)) {
256 $changedFields[$field] = array(
257 str_replace("\n", " - ", $setting->getText($this->orig[$field])),
258 str_replace("\n", " - ", $setting->getText($this->values[$field])),
259 );
260 } else {
261 $changedFields[$field] = array(
262 str_replace("\n", " - ", $this->orig[$field]),
263 str_replace("\n", " - ", $this->values[$field]),
264 );
265 }
266 if (!is_null($setting)) {
267 $setting->save($this, $field, $this->values[$field]);
268 }
269 if (isset($this->watched[$field]) && $this->watched[$field]) {
270 WatchProfileUpdate::register($this->profile, $field);
271 }
272 }
273 }
274 $this->_saveData();
275
276 // Update the last modification date
277 XDB::execute('UPDATE profiles
278 SET last_change = NOW()
279 WHERE pid = {?}', $this->pid());
280 global $platal;
281 S::logger()->log('profil', $platal->pl_self(2));
282
283 /** If the update was made by a third party and the profile corresponds
284 * to a registered user, stores both former and new text.
285 * This will be daily sent to the user.
286 */
287 $owner = $this->profile->owner();
288 $user = S::user();
289 if ($owner->isActive() && $owner->id() != $user->id()) {
290 foreach ($changedFields as $field => $values) {
291 XDB::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
292 VALUES ({?}, {?}, {?}, {?}, {?})',
293 $this->pid(), $user->id(), $field, $values[0], $values[1]);
294 }
295 }
296 }
297
298 protected function checkChanges()
299 {
300 $newvalues = $this->values;
301 $this->values = array();
302 $this->fetchData();
303 $this->values = $newvalues;
304 $changes = false;
305 foreach ($this->settings as $field=>&$setting) {
306 if ($this->orig[$field] != $this->values[$field]) {
307 $this->changed[$field] = true;
308 $changes = true;
309 } else {
310 $this->changed[$field] = false;
311 }
312 }
313 return $changes;
314 }
315
316 protected function markChange()
317 {
318 }
319
320 public function template()
321 {
322 return 'profile/base.tpl';
323 }
324
325 public function pid()
326 {
327 return $this->profile->id();
328 }
329
330 public function hrpid()
331 {
332 return $this->profile->hrpid();
333 }
334
335 protected function _prepare(PlPage &$page, $id)
336 {
337 }
338
339 public function prepare(PlPage &$page, $id)
340 {
341 if (count($this->values) == 0) {
342 $this->fetchData();
343 }
344 foreach ($this->values as $field=>&$value) {
345 $page->assign($field, $value);
346 }
347 $this->_prepare($page, $id);
348 $page->assign('profile', $this->profile);
349 $page->assign('owner', $this->owner);
350 $page->assign('profile_page', $this->pg_template);
351 $page->assign('errors', $this->errors);
352 }
353
354 public function process(&$global_success)
355 {
356 $global_success = true;
357 $this->fetchData();
358 foreach ($this->settings as $field=>&$setting) {
359 $success = false;
360 if (!is_null($setting)) {
361 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
362 } else {
363 $success = true;
364 $this->values[$field] = Post::v($field, '');
365 }
366 $this->errors[$field] = !$success;
367 $global_success = $global_success && $success;
368 }
369 if ($global_success) {
370 if ($this->checkChanges()) {
371 $this->saveData();
372 $this->markChange();
373 }
374 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
375 }
376 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
377 . "de ton profil et de revalider ta demande.");
378 return PlWizard::CURRENT_PAGE;
379 }
380
381 public function success()
382 {
383 return 'Ton profil a bien été mis à jour.';
384 }
385 }
386
387 require_once dirname(__FILE__) . '/general.inc.php';
388 require_once dirname(__FILE__) . '/addresses.inc.php';
389 require_once dirname(__FILE__) . '/groups.inc.php';
390 require_once dirname(__FILE__) . '/decos.inc.php';
391 require_once dirname(__FILE__) . '/jobs.inc.php';
392 require_once dirname(__FILE__) . '/skills.inc.php';
393 require_once dirname(__FILE__) . '/mentor.inc.php';
394
395 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
396 ?>