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