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