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