Phone class: new class to access profile_phones.
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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);
a0fce0c6
SJ
39
40 /** Get text from the value.
41 */
42 public function getText($value);
fd38b30e
FB
43}
44
45abstract class ProfileNoSave implements ProfileSetting
46{
47 public function save(ProfilePage &$page, $field, $new_value) { }
a0fce0c6
SJ
48
49 public function getText($value) {
50 return $value;
51 }
fd38b30e
FB
52}
53
12bcf04b 54class ProfileSettingWeb extends ProfileNoSave
fd38b30e
FB
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 }
37d44b3b
FB
61 $value = trim($value);
62 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
fd38b30e 63 if (!$success) {
d7610c35
FB
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');
fd38b30e
FB
66 }
67 return $value;
68 }
69}
70
12bcf04b 71class ProfileSettingEmail extends ProfileNoSave
37d44b3b
FB
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);
37d44b3b
FB
79 $success = empty($value) || isvalid_email($value);
80 if (!$success) {
d7610c35 81 Platal::page()->trigError('Adresse Email invalide');
37d44b3b
FB
82 }
83 return $value;
84 }
85}
86
12bcf04b 87class ProfileSettingNumber extends ProfileNoSave
92446a53
PC
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) {
ad3fee9d 97 Platal::page()->trigError('Numéro invalide');
92446a53
PC
98 }
99 return $value;
100 }
101}
102
12bcf04b 103class ProfileSettingPhones implements ProfileSetting
bde2be3b 104{
bde2be3b
GB
105 public function value(ProfilePage &$page, $field, $value, &$success)
106 {
107 $success = true;
0b6c8b36 108 $phones = array();
ed01acac 109
0b6c8b36
SJ
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();
ed01acac 115 }
0b6c8b36
SJ
116 if (count($phones) == 0) {
117 $phone = new Phone();
118 $phones[] = $phone->toFormArray();
bde2be3b 119 }
0b6c8b36 120 return $phones;
bde2be3b 121 }
ed01acac 122
0b6c8b36 123 return Phone::formatFormArray($value, $success);
bde2be3b
GB
124 }
125
126 public function save(ProfilePage &$page, $field, $value)
127 {
0b6c8b36
SJ
128 Phone::deletePhones($page->pid(), Phone::LINK_PROFILE);
129 Phone::savePhones($value, $page->pid(), Phone::LINK_PROFILE);
bde2be3b 130 }
a0fce0c6
SJ
131
132 public function getText($value) {
0b6c8b36 133 return Phone::formArrayToString($value);
a0fce0c6 134 }
bde2be3b
GB
135}
136
12bcf04b 137class ProfileSettingPub extends ProfileNoSave
93553cea
FB
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 }
041a5cec 145 if (!$value) {
93553cea 146 $value = 'private';
041a5cec 147 } elseif ($value == 'on') { // Checkbox
93553cea
FB
148 $value = 'public';
149 }
150 return $value;
151 }
a0fce0c6
SJ
152
153 public function getText($value) {
154 return $value;
155 }
93553cea
FB
156}
157
12bcf04b 158class ProfileSettingBool extends ProfileNoSave
576777d7
FB
159{
160 public function value(ProfilePage &$page, $field, $value, &$success)
161 {
162 $success = true;
163 if (is_null($value)) {
041a5cec 164 $value = isset($page->values[$field]) ? $page->values[$field] : null;
576777d7 165 }
ee12da4e 166 return $value ? "1" : "";
576777d7
FB
167 }
168}
169
12bcf04b 170class ProfileSettingDate extends ProfileNoSave
7bff4cb0
FB
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) {
d7610c35 180 Platal::page()->trigError("Les dates doivent être au format jj/mm/aaaa");
7bff4cb0
FB
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) {
d7610c35 187 Platal::page()->trigError("La date n'a pas une valeur valide");
7bff4cb0
FB
188 }
189 }
190 }
191 return $value;
192 }
193}
194
12bcf04b 195abstract class ProfileSettingGeocoding implements ProfileSetting
37d44b3b 196{
73f6c165 197 protected function geocodeAddress(array &$address, &$success)
37d44b3b 198 {
4c906759 199 require_once 'geocoding.inc.php';
37d44b3b 200 $success = true;
afaa2cc7 201 if (isset($address['changed']) && $address['changed'] == 1) {
4c906759
SJ
202 $gmapsGeocoder = new GMapsGeocoder();
203 $address = $gmapsGeocoder->getGeocodedAddress($address);
041a5cec 204 if (isset($address['geoloc'])) {
37d44b3b 205 $success = false;
37d44b3b 206 }
4279d3bc
SJ
207 } elseif (@$address['changed'] && !@$address['text']) {
208 $address = empty_address();
209 $address['pub'] = 'private';
041a5cec 210 }
42568423
SJ
211 if (isset($address['geoloc_choice']) && ($address['geoloc_choice'] == 0)) {
212 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
213 $mailer->assign('text', $address['text']);
214 $mailer->assign('geoloc', $address['geoloc']);
215 $mailer->send();
73f6c165
SJ
216 $gmapsGeocoder = new GMapsGeocoder();
217 $address = $gmapsGeocoder->stripGeocodingFromAddress($address);
37d44b3b 218 }
37d44b3b 219 }
a0fce0c6
SJ
220
221 public function getText($value) {
222 return $value;
223 }
37d44b3b
FB
224}
225
226
fd38b30e 227abstract class ProfilePage implements PlWizardPage
f118f685
FB
228{
229 protected $wizard;
fd38b30e
FB
230 protected $pg_template;
231 protected $settings = array(); // A set ProfileSetting objects
93553cea 232 protected $errors = array(); // A set of boolean with the value check errors
576777d7 233 protected $changed = array(); // A set of boolean indicating wether the value has been changed
a2a1c2f2 234 protected $watched = array(); // A set of boolean indicating the fields that are watched
fd38b30e 235
93553cea 236 public $orig = array();
fd38b30e 237 public $values = array();
e5bcd851
FB
238 public $profile = null;
239 public $owner = null;
f118f685
FB
240
241 public function __construct(PlWizard &$wiz)
242 {
243 $this->wizard =& $wiz;
e5bcd851
FB
244 $this->profile = $this->wizard->getUserData('profile');
245 $this->owner = $this->wizard->getUserData('owner');
f118f685
FB
246 }
247
7c2e0f0d
FB
248 protected function _fetchData()
249 {
250 }
251
fd38b30e
FB
252 protected function fetchData()
253 {
93553cea
FB
254 if (count($this->orig) > 0) {
255 $this->values = $this->orig;
256 return;
257 }
7c2e0f0d
FB
258
259 $this->_fetchData();
93553cea
FB
260 foreach ($this->settings as $field=>&$setting) {
261 $success = false;
262 if (!is_null($setting)) {
263 $this->values[$field] = $setting->value($this, $field, null, $success);
264 } else if (!isset($this->values[$field])) {
265 $this->values[$field] = S::v($field);
266 }
267 $this->errors[$field] = false;
268 }
269 $this->orig = $this->values;
fd38b30e
FB
270 }
271
7c2e0f0d
FB
272 protected function _saveData()
273 {
274 }
275
fd38b30e
FB
276 protected function saveData()
277 {
a2a1c2f2 278 require_once 'notifs.inc.php';
a0fce0c6 279 $changedFields = array();
93553cea 280 foreach ($this->settings as $field=>&$setting) {
a0fce0c6
SJ
281 if ($this->changed[$field]) {
282 if (!is_null($setting)) {
283 $changedFields[$field] = array(
284 str_replace("\n", " - ", $setting->getText($this->orig[$field])),
285 str_replace("\n", " - ", $setting->getText($this->values[$field])),
286 );
287 } else {
288 $changedFields[$field] = array(
289 str_replace("\n", " - ", $this->orig[$field]),
290 str_replace("\n", " - ", $this->values[$field]),
291 );
292 }
293 if (!is_null($setting)) {
294 $setting->save($this, $field, $this->values[$field]);
295 }
296 if (isset($this->watched[$field]) && $this->watched[$field]) {
297 WatchProfileUpdate::register($this->profile, $field);
298 }
a2a1c2f2 299 }
93553cea 300 }
7c2e0f0d 301 $this->_saveData();
576777d7
FB
302
303 // Update the last modification date
4e7bf1e0
FB
304 XDB::execute('UPDATE profiles
305 SET last_change = NOW()
306 WHERE pid = {?}', $this->pid());
576777d7 307 global $platal;
e5bcd851 308 S::logger()->log('profil', $platal->pl_self(2));
a0fce0c6
SJ
309
310 /** If the update was made by a third party and the profile corresponds
311 * to a registered user, stores both former and new text.
312 * This will be daily sent to the user.
313 */
314 $owner = $this->profile->owner();
315 $user = S::user();
316 if ($owner->isActive() && $owner->id() != $user->id()) {
317 foreach ($changedFields as $field => $values) {
318 XDB::execute('REPLACE INTO profile_modifications (pid, uid, field, oldText, newText)
319 VALUES ({?}, {?}, {?}, {?}, {?})',
320 $this->pid(), $user->id(), $field, $values[0], $values[1]);
321 }
322 }
93553cea
FB
323 }
324
325 protected function checkChanges()
326 {
327 $newvalues = $this->values;
328 $this->values = array();
329 $this->fetchData();
330 $this->values = $newvalues;
576777d7 331 $changes = false;
93553cea
FB
332 foreach ($this->settings as $field=>&$setting) {
333 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
334 $this->changed[$field] = true;
335 $changes = true;
336 } else {
337 $this->changed[$field] = false;
93553cea
FB
338 }
339 }
576777d7 340 return $changes;
93553cea
FB
341 }
342
343 protected function markChange()
344 {
fd38b30e
FB
345 }
346
f118f685
FB
347 public function template()
348 {
fd38b30e 349 return 'profile/base.tpl';
f118f685
FB
350 }
351
e5bcd851
FB
352 public function pid()
353 {
354 return $this->profile->id();
355 }
356
357 public function hrpid()
358 {
359 return $this->profile->hrpid();
360 }
361
04334c61 362 protected function _prepare(PlPage &$page, $id)
7c2e0f0d
FB
363 {
364 }
365
04334c61 366 public function prepare(PlPage &$page, $id)
f118f685 367 {
fd38b30e
FB
368 if (count($this->values) == 0) {
369 $this->fetchData();
fd38b30e
FB
370 }
371 foreach ($this->values as $field=>&$value) {
372 $page->assign($field, $value);
373 }
7c2e0f0d 374 $this->_prepare($page, $id);
e5bcd851
FB
375 $page->assign('profile', $this->profile);
376 $page->assign('owner', $this->owner);
fd38b30e 377 $page->assign('profile_page', $this->pg_template);
93553cea 378 $page->assign('errors', $this->errors);
f118f685
FB
379 }
380
eb563236 381 public function process(&$global_success)
f118f685 382 {
fd38b30e
FB
383 $global_success = true;
384 $this->fetchData();
385 foreach ($this->settings as $field=>&$setting) {
386 $success = false;
93553cea 387 if (!is_null($setting)) {
85cc366b 388 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
389 } else {
390 $success = true;
85cc366b 391 $this->values[$field] = Post::v($field, '');
93553cea
FB
392 }
393 $this->errors[$field] = !$success;
fd38b30e
FB
394 $global_success = $global_success && $success;
395 }
396 if ($global_success) {
93553cea
FB
397 if ($this->checkChanges()) {
398 $this->saveData();
399 $this->markChange();
fd38b30e 400 }
93553cea 401 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e 402 }
d7610c35 403 Platal::page()->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
eb563236 404 . "de ton profil et de revalider ta demande.");
f118f685
FB
405 return PlWizard::CURRENT_PAGE;
406 }
eb563236
SJ
407
408 public function success()
409 {
410 return 'Ton profil a bien été mis à jour.';
411 }
f118f685
FB
412}
413
fd38b30e 414require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 415require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 416require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 417require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 418require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 419require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 420require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
421
422// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
423?>