Add 'hidden' flag to text descriptions of the 'pub' flag
[platal.git] / modules / profile / general.inc.php
CommitLineData
fd38b30e
FB
1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
fd38b30e
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
12bcf04b 22class ProfileSettingSearchNames implements ProfileSetting
fd38b30e 23{
0e1dfbad
SJ
24 private function diff($pid, array $old, array $new)
25 {
26 $diff = false;
27 foreach ($old as $field => $name) {
28 $diff = $diff || ($name != $new[$field]);
29 }
b270577e 30
0e1dfbad 31 return $diff;
b270577e 32 }
c4b45374 33
e5bcd851
FB
34 private function matchWord($old, $new, $newLen)
35 {
93553cea 36 return ($i = strpos($old, $new)) !== false
fd38b30e
FB
37 && ($i == 0 || $old{$i-1} == ' ')
38 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
39 }
40
0e1dfbad 41 private function prepare(ProfilePage $page, array &$new_value)
fd38b30e 42 {
0e1dfbad
SJ
43 $initial_value = XDB::fetchOneAssoc('SELECT lastname_main, firstname_main
44 FROM profile_public_names
45 WHERE pid = {?}',
46 $page->pid());
fd38b30e 47
fd38b30e 48 $success = true;
0e1dfbad
SJ
49 foreach ($initial_value as $field => $name) {
50 $initial = name_to_basename($name);
51 $new = name_to_basename($new_value[$field]);
fd38b30e 52
0e1dfbad
SJ
53 if (!($this->matchWord($initial, $new, strlen($new))
54 || ($field == 'lastname_main' && $new == 'DE ' . $initial))) {
55 $new_value[$field . '_error'] = true;
56 $success = false;
57 Platal::page()->trigError('Le nom choisi (' . $new . ') est trop loin de sa valeur initiale (' . $initial . ').');
b270577e
SJ
58 }
59 }
60
0e1dfbad 61 return $success;
b270577e
SJ
62 }
63
26ba053e 64 public function value(ProfilePage $page, $field, $value, &$success)
fd38b30e 65 {
75e295ff 66 require_once 'name.func.inc.php';
0e1dfbad 67 $success = true;
09f9ebea 68
c4b45374 69 if (is_null($value)) {
0e1dfbad 70 $request = NamesReq::getPublicNames($page->pid());
c4b45374 71
0e1dfbad 72 if (!$request) {
99a1a08c 73 $value['public_names'] = XDB::fetchOneAssoc('SELECT lastname_main, lastname_marital, lastname_ordinary,
0e1dfbad
SJ
74 firstname_main, firstname_ordinary, pseudonym
75 FROM profile_public_names
76 WHERE pid = {?}',
77 $page->pid());
0e1dfbad
SJ
78 } else {
79 $value['public_names'] = $request;
bd3e755e
SJ
80 Platal::page()->assign('validation', true);
81 }
0e1dfbad
SJ
82
83 $value['private_names'] = XDB::fetchAllAssoc('SELECT type, name
84 FROM profile_private_names
85 WHERE pid = {?}
86 ORDER BY type, id',
87 $page->pid());
c4b45374 88 } else {
0e1dfbad 89 foreach ($value['public_names'] as $key => $name) {
75e295ff 90 $value['public_names'][$key] = capitalize_name(trim($name));
0e1dfbad 91 }
e0d6b3af
SJ
92 if (isset($value['private_names'])) {
93 foreach ($value['private_names'] as $key => $name) {
94 $value['private_names'][$key]['name'] = trim($name['name']);
95 if ($value['private_names'][$key]['name'] == '') {
96 unset($value['private_names'][$key]);
97 }
c4b45374
SJ
98 }
99 }
0e1dfbad
SJ
100
101 if (S::user()->isMe($page->owner)) {
102 $success = $this->prepare($page, $value['public_names']);
dced83b4 103 }
fd38b30e 104 }
09f9ebea 105
0e1dfbad 106 $public_name = build_first_name($value['public_names']) . ' ' . build_full_last_name($value['public_names'], $page->profile->isFemale());
e0d6b3af
SJ
107 if (isset($value['private_names'])) {
108 $private_name_end = build_private_name($value['private_names']);
109 $private_name = $public_name . $private_name_end;
110 } else {
111 $value['private_names'] = array();
112 $private_name = $public_name;
113 }
0e1dfbad
SJ
114
115 Platal::page()->assign('public_name', $public_name);
116 Platal::page()->assign('private_name', $private_name);
117
c4b45374 118 return $value;
fd38b30e
FB
119 }
120
26ba053e 121 public function save(ProfilePage $page, $field, $value)
fd38b30e 122 {
dced83b4 123 require_once 'name.func.inc.php';
024ec1e5 124
0e1dfbad
SJ
125 $old = XDB::fetchOneAssoc('SELECT lastname_main, lastname_marital, lastname_ordinary,
126 firstname_main, firstname_ordinary, pseudonym
127 FROM profile_public_names
128 WHERE pid = {?}',
129 $page->pid());
dced83b4 130
0e1dfbad
SJ
131 if ($has_diff = $this->diff($page->pid(), $old, $value['public_names'])) {
132 $new_names = new NamesReq(S::user(), $page->profile, $value['public_names'], $old);
dced83b4 133 $new_names->submit();
0e1dfbad
SJ
134 Platal::page()->assign('validation', true);
135 Platal::page()->trigWarning('La demande de modification des noms a bien été prise en compte.' .
136 ' Un email sera envoyé dès que ces changements auront été effectués.');
137 }
138
139 XDB::execute('DELETE FROM profile_private_names
140 WHERE pid = {?}',
141 $page->pid());
142 $values = array();
143 $nickname = $lastname = $firstname = 0;
e0d6b3af
SJ
144 if (isset($value['private_names'])) {
145 foreach ($value['private_names'] as $name) {
146 $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
147 }
0e1dfbad
SJ
148 }
149 if (count($values)) {
150 XDB::rawExecute('INSERT INTO profile_private_names (pid, type, id, name)
151 VALUES ' . implode(',', $values));
152 }
153
154 if ($has_diff) {
155 update_display_names($page->profile, $old, $value['private_names']);
dced83b4 156 } else {
e0d6b3af
SJ
157 update_display_names($page->profile,
158 $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
c4b45374 159 }
fd38b30e 160 }
a0fce0c6
SJ
161
162 public function getText($value) {
0e1dfbad
SJ
163 $public_names = array();
164 foreach ($value['public_names'] as $name) {
165 if ($name != '') {
166 $public_names[] = $name;
a0fce0c6
SJ
167 }
168 }
0e1dfbad 169
e0d6b3af 170 if (isset($value['private_names']) && count($value['private_names'])) {
0e1dfbad
SJ
171 $private_names = array();
172 foreach ($value['private_names'] as $name) {
173 $private_names[] = $name['name'];
174 }
175 return 'noms publics : ' . implode(', ' , $public_names) . ', noms privés : ' . implode(', ' , $private_names);;
176 }
177
178 return 'noms publics : ' . implode(', ' , $public_names);
a0fce0c6 179 }
fd38b30e
FB
180}
181
12bcf04b 182class ProfileSettingEdu implements ProfileSetting
576777d7 183{
e5bcd851
FB
184 public function __construct() {
185 }
043bbacf 186
5ba8842e 187 static function sortByGradYear($line1, $line2) {
be6806fa
SJ
188 $a = (isset($line1['grad_year'])) ? (int) $line1['grad_year'] : 0;
189 $b = (isset($line2['grad_year'])) ? (int) $line2['grad_year'] : 0;
22f0a0a8 190 if ($a == $b) {
5ba8842e
SJ
191 return 0;
192 }
22f0a0a8 193 return ($a < $b) ? -1 : 1;
5ba8842e
SJ
194 }
195
26ba053e 196 public function value(ProfilePage $page, $field, $value, &$success)
576777d7
FB
197 {
198 $success = true;
885fa398 199 if (is_null($value)) {
043bbacf 200 $value = array();
111b2736
FB
201 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
202 FROM profile_education
fb052bde 203 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
111b2736
FB
204 ORDER BY id",
205 $page->pid());
885fa398
SJ
206 } else if (!is_array($value)) {
207 $value = null;
043bbacf
SJ
208 } else {
209 $i = 0;
210 foreach ($value as $key=>&$edu) {
3ede4850
SJ
211 if ($edu['eduid'] < 1 || !isset($edu['degreeid']) || $edu['degreeid'] < 1) {
212 Platal::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
213 $success = false;
214 }
043bbacf 215 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
be6806fa 216 Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
83065276 217 $edu['grad_year'] = null;
be6806fa 218 $edu['warning'] = true;
043bbacf
SJ
219 }
220 if ($key != $i) {
221 $value[$i] = $edu;
222 unset($value[$key]);
223 }
224 $i++;
225 }
12bcf04b 226 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
576777d7
FB
227 }
228 return $value;
229 }
230
26ba053e 231 public function save(ProfilePage $page, $field, $value)
576777d7 232 {
043bbacf 233 XDB::execute("DELETE FROM profile_education
fb052bde 234 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))",
e5bcd851 235 $page->pid());
fb052bde 236 $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
043bbacf 237 foreach ($value as $eduid=>&$edu) {
fb052bde 238 if ($edu['eduid'] != '' && $schoolsList[$edu['eduid']] != Profile::EDU_X) {
8768e5af 239 $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
043bbacf 240 XDB::execute("INSERT INTO profile_education
ce0b2c6f 241 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
1504ac45 242 fieldid = {?}, grad_year = {?}, program = {?}",
e5bcd851 243 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
8768e5af 244 $fieldId, $edu['grad_year'], $edu['program']);
043bbacf 245 }
576777d7
FB
246 }
247 }
a0fce0c6
SJ
248
249 public function getText($value) {
250 $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
251 $degreesList = DirEnum::getOptions(DirEnum::EDUDEGREES);
252 $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS);
253 $educations = array();
14aba233
SJ
254 foreach ($value as $id => $education) {
255 // XXX: the following condition should be removed once there are no more incomplete educations.
256 if (is_null($education['eduid']) || is_null($education['degreeid'])) {
257 if (is_null($education['eduid']) && is_null($education['degreeid'])) {
258 $educations[$id] = 'formation manquante';
259 } else {
260 $educations[$id] = (is_null($education['eduid']) ? 'université manquante' : $schoolsList[$education['eduid']]) . ', '
261 . (is_null($education['degreeid']) ? 'diplôme manquant' : $degreesList[$education['degreeid']]);
262 }
263 } else {
264 $educations[$id] = $schoolsList[$education['eduid']] . ', ' . $degreesList[$education['degreeid']];
265 }
266
267 $details = array();
268 if ($education['grad_year']) {
269 $details[] = $education['grad_year'];
270 }
271 if ($education['program']) {
272 $details[] = '« ' . $education['program'] . ' »';
273 }
274 if ($education['fieldid']) {
275 $details[] = $fieldsList[$education['fieldid']];
276 }
277 if (count($details)) {
278 $educations[$id] .= ' (' . implode(', ', $details) . ')';
279 }
a0fce0c6
SJ
280 }
281 return implode(', ', $educations);
282 }
576777d7
FB
283}
284
d7de04af
SJ
285class ProfileSettingMainEdu implements ProfileSetting
286{
287 private $cycles;
288
289 public function __construct()
290 {
291 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
292 $eduDegrees = array_flip($eduDegrees);
293 $this->cycles = array(
294 $eduDegrees[Profile::DEGREE_X] => 'Cycle polytechnicien',
295 $eduDegrees[Profile::DEGREE_M] => 'Cycle master',
296 $eduDegrees[Profile::DEGREE_D] => 'Cycle doctoral'
297 );
298 }
299
300 public function value(ProfilePage $page, $field, $value, &$success)
301 {
302 $success = true;
303 if (is_null($value)) {
304 $value = array();
305 $value = XDB::fetchAllAssoc("SELECT degreeid, fieldid, promo_year, program
306 FROM profile_education
fb052bde
SJ
307 WHERE pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
308 ORDER BY NOT FIND_IN_SET('primary', flags), degreeid",
d7de04af
SJ
309 $page->pid());
310
311 foreach ($value as &$item) {
312 $item['cycle'] = $this->cycles[$item['degreeid']];
313 }
314 } elseif (!is_array($value)) {
315 $value = array();
316 } else {
317 foreach ($value as $key => $item) {
318 if (!isset($item['degreeid'])) {
319 unset($value[$key]);
320 }
321 }
322 }
323
324 return $value;
325 }
326
327 public function save(ProfilePage $page, $field, $value)
328 {
329 foreach ($value as $item) {
330 $fieldId = ($item['fieldid'] == 0) ? null : $item['fieldid'];
331 XDB::execute("UPDATE profile_education
332 SET fieldid = {?}, program = {?}
fb052bde 333 WHERE pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags)) AND degreeid = {?}",
d7de04af
SJ
334 $fieldId, $item['program'], $page->pid(), $item['degreeid']);
335 }
336 }
337
338 public function getText($value) {
339 $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS);
340 $educations = array();
341 foreach ($value as $item) {
342 $details = array($this->cycles[$item['degreeid']]);
8fcfc71f
SJ
343 if ($item['program']) {
344 $details[] = '« ' . $item['program'] . ' »';
d7de04af 345 }
8fcfc71f
SJ
346 if ($item['fieldid']) {
347 $details[] = $fieldsList[$item['fieldid']];
d7de04af
SJ
348 }
349 }
350 return implode(', ', $educations);
351 }
352}
353
12bcf04b 354class ProfileSettingEmailDirectory implements ProfileSetting
b715c1e1 355{
b814a8b8 356 public function __construct(){}
26ba053e 357 public function save(ProfilePage $page, $field, $value){}
b715c1e1 358
26ba053e 359 public function value(ProfilePage $page, $field, $value, &$success)
b715c1e1 360 {
ad3fee9d 361 $p = Platal::page();
b715c1e1
SJ
362
363 $success = true;
364 if (!is_null($value)) {
365 $email_stripped = strtolower(trim($value));
ad3fee9d
SJ
366 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
367 $p->assign('email_error', '1');
368 $p->assign('email_directory_error', $email_stripped);
369 $p->trigError('Adresse Email invalide');
b715c1e1
SJ
370 $success = false;
371 } else {
ad3fee9d 372 $p->assign('email_error', '0');
b715c1e1
SJ
373 }
374 }
375 return $value;
376 }
a0fce0c6
SJ
377
378 public function getText($value) {
379 return $value;
380 }
b715c1e1
SJ
381}
382
12bcf04b 383class ProfileSettingNetworking implements ProfileSetting
d1a2252a
GB
384{
385 private $email;
386 private $pub;
387 private $web;
92446a53 388 private $number;
d1a2252a
GB
389
390 public function __construct()
391 {
12bcf04b
RB
392 $this->email = new ProfileSettingEmail();
393 $this->pub = new ProfileSettingPub();
394 $this->web = new ProfileSettingWeb();
395 $this->number = new ProfileSettingNumber();
d1a2252a
GB
396 }
397
26ba053e 398 public function value(ProfilePage $page, $field, $value, &$success)
d1a2252a
GB
399 {
400 if (is_null($value)) {
1f5cd004 401 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
111b2736 402 FROM profile_networking AS n
ce0b2c6f 403 WHERE n.pid = {?}",
111b2736 404 $page->pid());
d1a2252a
GB
405 }
406 if (!is_array($value)) {
407 $value = array();
408 }
1f5cd004 409 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
111b2736 410 FROM profile_networking_enum;');
d1a2252a
GB
411 $success = true;
412 foreach($value as $i=>&$network) {
92c3f9e5
GB
413 if (!trim($network['address'])) {
414 unset($value[$i]);
415 } else {
416 if (!isset($network['pub'])) {
417 $network['pub'] = 'private';
418 }
419 $network['error'] = false;
420 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
421 $s = true;
1f5cd004
PC
422 $network['name'] = $filters[$network['type']]['name'];
423 if ($filters[$network['type']]['filter'] == 'web') {
92c3f9e5 424 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
1f5cd004 425 } elseif ($filters[$network['type']]['filter'] == 'email') {
92c3f9e5 426 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
1f5cd004 427 } elseif ($filters[$network['type']]['filter'] == 'number') {
92c3f9e5
GB
428 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
429 }
430 if (!$s) {
431 $success = false;
432 $network['error'] = true;
433 }
d1a2252a
GB
434 }
435 }
436 return $value;
437 }
438
26ba053e 439 public function save(ProfilePage $page, $field, $value)
d1a2252a
GB
440 {
441 XDB::execute("DELETE FROM profile_networking
ce0b2c6f 442 WHERE pid = {?}",
e5bcd851 443 $page->pid());
d1a2252a
GB
444 if (!count($value)) {
445 return;
446 }
447 $insert = array();
448 foreach ($value as $id=>$network) {
1f5cd004 449 XDB::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
d1a2252a 450 VALUES ({?}, {?}, {?}, {?}, {?})",
e5bcd851 451 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
d1a2252a
GB
452 }
453 }
a0fce0c6
SJ
454
455 public function getText($value) {
b49e98ca 456 static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
a0fce0c6
SJ
457 $networkings = array();
458 foreach ($value as $network) {
14aba233 459 $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
a0fce0c6 460 }
14aba233 461 return implode(', ' , $networkings);
a0fce0c6 462 }
d1a2252a
GB
463}
464
8fcfc71f
SJ
465class ProfileSettingHobby implements ProfileSetting
466{
467 private $pub;
468 static private $type = array('Sport', 'Loisir', 'Hobby');
469
470 public function __construct()
471 {
472 $this->pub = new ProfileSettingPub();
473 }
474
475 public function value(ProfilePage $page, $field, $value, &$success)
476 {
80cc7d28 477 $success = true;
8fcfc71f
SJ
478 if (is_null($value)) {
479 $value = XDB::fetchAllAssoc('SELECT type, text, pub
480 FROM profile_hobby
481 WHERE pid = {?}',
482 $page->pid());
483 }
484 if (!is_array($value)) {
485 return array();
486 }
8fcfc71f
SJ
487 foreach($value as $i => &$hobby) {
488 $hobby['text'] = trim($hobby['text']);
489 if (!$hobby['text'] ||!in_array($hobby['type'], self::$type)) {
490 unset($value[$i]);
491 } else {
492 if (!isset($hobby['pub'])) {
493 $hobby['pub'] = 'private';
494 }
495 $s = true;
496 $hobby['pub'] = $this->pub->value($page, 'pub', $hobby['pub'], $s);
497 $success = $success && $s;
498 }
499 }
500 return $value;
501 }
502
503 public function save(ProfilePage $page, $field, $value)
504 {
505 XDB::execute('DELETE FROM profile_hobby
506 WHERE pid = {?}',
507 $page->pid());
508 if (!count($value)) {
509 return;
510 }
511 foreach ($value as $id => $hobby) {
512 XDB::execute("INSERT INTO profile_hobby (pid, id, type, text, pub)
513 VALUES ({?}, {?}, {?}, {?}, {?})",
514 $page->pid(), $id, $hobby['type'], $hobby['text'], $hobby['pub']);
515 }
516 }
517
518 public function getText($value) {
b49e98ca 519 static $pubs = array('public' => 'publique', 'ax' => 'annuaire papier', 'private' => 'privé', 'hidden' => 'administrateurs');
8fcfc71f
SJ
520 $hobbies = array();
521 foreach (self::$type as $type) {
522 $hobbies[$type] = array();
523 }
524 foreach ($value as $hobby) {
525 $hobbies[$hobby['type']][] = $hobby['text'] . ' (affichage ' . $pubs[$hobby['pub']] . ')';
526 }
527 $text = array();
528 foreach (self::$type as $type) {
529 if (!empty($hobbies[$type])) {
530 $text[] = $hobbies[$type] . ' : ' . implode(', ' , $hobbies[$type]);
531 }
532 }
533 return implode(', ' , $text);
534 }
535}
536
537
7e233317
SJ
538class ProfileSettingPromo implements ProfileSetting
539{
540 public function __construct(){}
541
26ba053e 542 public function save(ProfilePage $page, $field, $value)
7e233317
SJ
543 {
544 $gradYearNew = $value;
545 if ($page->profile->mainEducation() == 'X') {
546 $gradYearNew += $page->profile->mainEducationDuration();
547 }
86ced4d4
SJ
548 if (($page->profile->mainEducation() != 'X'
549 && $value == $page->profile->entry_year + $page->profile->mainEducationDuration())
550 || ($page->profile->mainEducation() == 'X' && $value == $page->profile->entry_year)) {
7e233317
SJ
551 XDB::execute('UPDATE profile_display
552 SET promo = {?}
553 WHERE pid = {?}',
554 $page->profile->mainEducation() . strval($value), $page->profile->id());
555 XDB::execute('UPDATE profile_education
556 SET grad_year = {?}
557 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
558 $gradYearNew, $page->profile->id());
86ced4d4 559 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
7e233317 560 } else {
0b981fbe 561 $myorange = new OrangeReq(S::user(), $page->profile, $gradYearNew);
7e233317
SJ
562 $myorange->submit();
563 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
564 }
565 }
566
26ba053e 567 public function value(ProfilePage $page, $field, $value, &$success)
7e233317
SJ
568 {
569 $entryYear = $page->profile->entry_year;
570 $gradYear = $page->profile->grad_year;
7733ade1 571 $yearpromo = $page->profile->yearpromo();
7e233317 572 $success = true;
86ced4d4 573 if (is_null($value) || $value == $yearpromo) {
7e233317
SJ
574 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
575 $promoChoice = array();
86ced4d4
SJ
576 for ($i = $entryYear; $i <= $gradYear - $page->profile->mainEducationDuration(); ++$i) {
577 if ($page->profile->mainEducation() == 'X') {
578 $promoChoice[] = $page->profile->mainEducation() . strval($i);
579 } else {
580 $promoChoice[] = $page->profile->mainEducation() . strval($i + $page->profile->mainEducationDuration());
581 }
7e233317
SJ
582 }
583 Platal::page()->assign('promo_choice', $promoChoice);
584 }
67b4d856
SJ
585
586 // If this profile belongs to an X, return grad year minus education duration.
587 if ($page->profile->mainEducation() == 'X') {
588 return $gradYear - $page->profile->mainEducationDuration();
589 }
590
591 return $gradYear;
7e233317
SJ
592 }
593
594 // If this profile belongs to an X, $promoNew needs to be changed to
595 // the graduation year.
596 $gradYearNew = $value;
597 if ($page->profile->mainEducation() == 'X') {
598 $gradYearNew += $page->profile->mainEducationDuration();
599 }
600
601 if ($value < 1000 || $value > 9999) {
602 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
603 $success = false;
604 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
605 Platal::page()->trigError('Trop tôt&nbsp;!');
606 $success = false;
607 }
608 return intval($value);
609 }
a0fce0c6
SJ
610
611 public function getText($value) {
612 return $value;
613 }
7e233317
SJ
614}
615
616
66c4bdaf 617class ProfilePageGeneral extends ProfilePage
fd38b30e
FB
618{
619 protected $pg_template = 'profile/general.tpl';
620
26ba053e 621 public function __construct(PlWizard $wiz)
fd38b30e
FB
622 {
623 parent::__construct($wiz);
e77c0699
SJ
624 $this->settings['search_names'] = new ProfileSettingSearchNames();
625 $this->settings['nationality1'] = $this->settings['nationality2']
626 = $this->settings['nationality3']
627 = $this->settings['promo_display']
89aba52d 628 = $this->settings['profile_title']
e77c0699
SJ
629 = null;
630 $this->settings['email_directory'] = new ProfileSettingEmail();
631 $this->settings['email_directory_new'] = new ProfileSettingEmailDirectory();
632 $this->settings['tels'] = new ProfileSettingPhones();
633 $this->settings['edus'] = new ProfileSettingEdu();
d7de04af 634 $this->settings['main_edus'] = new ProfileSettingMainEdu();
7e233317 635 $this->settings['promo'] = new ProfileSettingPromo();
79d0b9af 636 $this->settings['networking'] = new ProfileSettingNetworking();
8fcfc71f 637 $this->settings['hobbies'] = new ProfileSettingHobby();
e77c0699
SJ
638 $this->watched = array('tels' => true,
639 'networking' => true, 'edus' => true,
640 'nationality1' => true, 'nationality2' => true,
641 'nationality3' => true, 'search_names' => true);
42ec0fe2
FB
642
643 /* Some fields editable under condition */
644 if (!S::user()->isMe($this->owner)) {
645 $this->settings['deathdate'] = new ProfileSettingDate(true);
c56a253e 646 $this->settings['birthdate'] = new ProfileSettingDate(true);
e1805873 647 $this->settings['birthdate_ref'] = new ProfileSettingDate(true);
9ede5056 648 $this->settings['axfreetext'] = null;
c56a253e 649 } else {
1447ed1a 650 $this->settings['yourself'] = null;
c56a253e 651 $this->settings['birthdate'] = new ProfileSettingDate();
1447ed1a 652 }
42ec0fe2
FB
653 if (S::user()->checkPerms('directory_private')
654 || S::user()->isMyProfile($this->owner)) {
1447ed1a 655 $this->settings['freetext'] = null;
e77c0699
SJ
656 $this->settings['freetext_pub'] = $this->settings['photo_pub']
657 = new ProfileSettingPub();
42ec0fe2
FB
658 $this->watched['freetext'] = true;
659 }
660
26198a04 661 Platal::page()->assign('is_registered', ($this->owner->perms ? true : false));
93553cea
FB
662 }
663
7c2e0f0d 664 protected function _fetchData()
93553cea 665 {
576777d7 666 // Checkout all data...
c56a253e 667 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
7e233317 668 p.email_directory as email_directory, pd.promo AS promo_display,
9ede5056 669 p.freetext, p.freetext_pub, p.axfreetext, p.ax_id AS matricule_ax, pd.yourself,
89aba52d
SJ
670 p.deathdate, IF(p.birthdate_ref = 0, '', p.birthdate_ref) AS birthdate_ref,
671 p.title AS profile_title
7e233317
SJ
672 FROM profiles AS p
673 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
7e233317 674 WHERE p.pid = {?}", $this->pid());
93553cea 675 $this->values = $res->fetchOneAssoc();
576777d7 676
576777d7
FB
677 // Retreive photo informations
678 $res = XDB::query("SELECT pub
5c4ea53f
FB
679 FROM profile_photos
680 WHERE pid = {?}", $this->pid());
bf9202ed
SJ
681 if ($res->numRows() == 0) {
682 $this->values['photo_pub'] = 'private';
683 } else {
684 $this->values['photo_pub'] = $res->fetchOneCell();
685 }
576777d7 686
e5bcd851
FB
687 if ($this->owner) {
688 $res = XDB::query("SELECT COUNT(*)
689 FROM requests
58bb2d9c 690 WHERE type = 'photo' AND pid = {?}",
e5bcd851
FB
691 $this->owner->id());
692 $this->values['nouvellephoto'] = $res->fetchOneCell();
693 } else {
694 $this->values['nouvellephoto'] = 0;
695 }
93553cea
FB
696 }
697
7c2e0f0d 698 protected function _saveData()
93553cea 699 {
e5bcd851 700 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
ce0b2c6f 701 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
9ede5056 702 || $this->changed['axfreetext'] || $this->changed['email_directory'] || $this->changed['profile_title']) {
e5bcd851
FB
703 if ($this->values['nationality3'] == "") {
704 $this->values['nationality3'] = NULL;
8450c2aa 705 }
e5bcd851
FB
706 if ($this->values['nationality2'] == "") {
707 $this->values['nationality2'] = $this->values['nationality3'];
708 $this->values['nationality3'] = NULL;
8450c2aa 709 }
e5bcd851 710 if ($this->values['nationality1'] == "") {
48a4525c 711 $this->values['nationality1'] = $this->values['nationality2'];
e5bcd851
FB
712 $this->values['nationality2'] = $this->values['nationality3'];
713 $this->values['nationality3'] = NULL;
8450c2aa 714 }
48a4525c
SJ
715 if ($this->values['nationality1'] == $this->values['nationality2']
716 && $this->values['nationality2'] == $this->values['nationality3']) {
717 $this->values['nationality2'] = NULL;
718 $this->values['nationality3'] = NULL;
719 } else if ($this->values['nationality1'] == $this->values['nationality2']) {
720 $this->values['nationality2'] = $this->values['nationality3'];
721 $this->values['nationality3'] = NULL;
722 } else if ($this->values['nationality2'] == $this->values['nationality3']
723 || $this->values['nationality1'] == $this->values['nationality3']) {
724 $this->values['nationality3'] = NULL;
725 }
726
ce0b2c6f
FB
727 $new_email = ($this->values['email_directory'] == "new@example.org") ?
728 $this->values['email_directory_new'] : $this->values['email_directory'];
729 if ($new_email == "") {
730 $new_email = NULL;
731 }
8450c2aa 732
e5bcd851
FB
733 XDB::execute("UPDATE profiles
734 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
9ede5056 735 freetext = {?}, freetext_pub = {?}, axfreetext = {?}, email_directory = {?}, title = {?}
d1e61677 736 WHERE pid = {?}",
e5bcd851 737 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
87db81e7 738 ProfileSettingDate::toSQLDate($this->values['birthdate']),
9ede5056 739 $this->values['freetext'], $this->values['freetext_pub'], $this->values['axfreetext'], $new_email,
89aba52d 740 $this->values['profile_title'], $this->pid());
b715c1e1 741 }
a7c28fff 742 if ($this->changed['photo_pub']) {
5c4ea53f 743 XDB::execute("UPDATE profile_photos
a7c28fff 744 SET pub = {?}
5c4ea53f 745 WHERE pid = {?}",
e5bcd851 746 $this->values['photo_pub'], $this->pid());
a7c28fff 747 }
9241c897 748 if (S::user()->isMe($this->owner) && $this->changed['yourself']) {
cb0af6e5
FB
749 if ($this->owner) {
750 XDB::execute('UPDATE accounts
751 SET display_name = {?}
752 WHERE uid = {?}',
753 $this->values['yourself'], $this->owner->id());
754 }
755 XDB::execute('UPDATE profile_display
756 SET yourself = {?}
757 WHERE pid = {?}', $this->values['yourself'],
758 $this->pid());
fb2c09c9 759 }
7e233317 760 if ($this->changed['promo_display']) {
86ced4d4 761 if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
7733ade1
SJ
762 $yearpromo = intval(substr($this->values['promo_display'], 1, 4));
763 if (($this->profile->mainEducation() == 'X' && $yearpromo >= $this->profile->entry_year)
86ced4d4 764 || ($this->profile->mainEducation() != 'X'
7733ade1 765 && $yearpromo >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
86ced4d4
SJ
766 XDB::execute('UPDATE profile_display
767 SET promo = {?}
768 WHERE pid = {?}',
769 $this->values['promo_display'], $this->pid());
7733ade1
SJ
770 XDB::execute('UPDATE profile_education
771 SET promo_year = {?}
772 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
773 $yearpromo, $this->pid());
86ced4d4 774 }
7e233317 775 }
b04882ff 776 }
26198a04 777 if ($this->changed['birthdate_ref'] && S::admin() && !$this->owner->perms) {
e1805873
SJ
778 XDB::execute('UPDATE profiles
779 SET birthdate_ref = {?}
780 WHERE pid = {?}',
781 ProfileSettingDate::toSQLDate($this->values['birthdate_ref']), $this->pid());
782 }
f7190088 783 if (!S::user()->isMe($this->owner) && $this->changed['deathdate']) {
87db81e7
FB
784 XDB::execute('UPDATE profiles
785 SET deathdate = {?}, deathdate_rec = NOW()
786 WHERE pid = {?} AND deathdate_rec IS NULL',
787 ProfileSettingDate::toSQLDate($this->values['deathdate']), $this->pid());
788 if (XDB::affectedRows() > 0) {
789 $this->profile->clear();
790 if ($this->owner) {
791 $this->owner->clear(true);
792 }
793 } else {
794 /* deathdate_rec was not NULL, this is just an update of the death date
795 */
796 XDB::execute('UPDATE profiles
797 SET deathdate = {?}
798 WHERE pid = {?}',
799 ProfileSettingDate::toSQLDate($this->values['deathdate']), $this->pid());
800 }
801 }
fd38b30e
FB
802 }
803
26ba053e 804 public function _prepare(PlPage $page, $id)
fd38b30e 805 {
f711b03f 806 require_once "education.func.inc.php";
d1a2252a 807
111b2736
FB
808 $res = XDB::query("SELECT id, field
809 FROM profile_education_field_enum
810 ORDER BY field");
043bbacf
SJ
811 $page->assign('edu_fields', $res->fetchAllAssoc());
812
b715c1e1 813 require_once "emails.combobox.inc.php";
17c6e7bb 814 fill_email_combobox($page, array('source', 'redirect', 'job', 'directory'), $this->owner);
b715c1e1 815
1f5cd004 816 $res = XDB::query("SELECT nw.nwid AS type, nw.name
111b2736
FB
817 FROM profile_networking_enum AS nw
818 ORDER BY name");
d1a2252a 819 $page->assign('network_list', $res->fetchAllAssoc());
c4b45374 820
0e1dfbad
SJ
821 $page->assign('lastnames', array('main' => 'Nom patronymique', 'marital' => 'Nom marital', 'ordinary' => 'Nom usuel'));
822 $page->assign('firstnames', array('firstname_main' => 'Prénom', 'firstname_ordinary' => 'Prénom usuel', 'pseudonym' => 'Pseudonyme (nom de plume)'));
823 $page->assign('other_names', array('nickname' => 'Surnom', 'firstname' => 'Autre prénom', 'lastname' => 'Autre nom'));
e8a7cf31 824 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
fd38b30e
FB
825 }
826}
827
448c8cdc 828// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
fd38b30e 829?>