c11abf4451813d11078c76724ba89dc3cbab9817
[platal.git] / modules / profile / general.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class ProfileSettingSearchNames implements ProfileSetting
23 {
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 }
30
31 return $diff;
32 }
33
34 private function matchWord($old, $new, $newLen)
35 {
36 return ($i = strpos($old, $new)) !== false
37 && ($i == 0 || $old{$i-1} == ' ')
38 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
39 }
40
41 private function prepare(ProfilePage $page, array &$new_value)
42 {
43 $initial_value = XDB::fetchOneAssoc('SELECT lastname_main, firstname_main
44 FROM profile_public_names
45 WHERE pid = {?}',
46 $page->pid());
47
48 $success = true;
49 foreach ($initial_value as $field => $name) {
50 $initial = name_to_basename($name);
51 $new = name_to_basename($new_value[$field]);
52
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 . ').');
58 }
59 }
60
61 return $success;
62 }
63
64 public function value(ProfilePage $page, $field, $value, &$success)
65 {
66 require_once 'name.func.inc.php';
67 $success = true;
68
69 if (is_null($value)) {
70 $request = NamesReq::getPublicNames($page->pid());
71
72 if (!$request) {
73 $value['public_names'] = XDB::fetchOneAssoc('SELECT lastname_main, lastname_marital, lastname_ordinary,
74 firstname_main, firstname_ordinary, pseudonym
75 FROM profile_public_names
76 WHERE pid = {?}',
77 $page->pid());
78 } else {
79 $value['public_names'] = $request;
80 Platal::page()->assign('validation', true);
81 }
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());
88 } else {
89 foreach ($value['public_names'] as $key => $name) {
90 $value['public_names'][$key] = capitalize_name(trim($name));
91 }
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 }
98 }
99 }
100
101 if (S::user()->isMe($page->owner)) {
102 $success = $this->prepare($page, $value['public_names']);
103 }
104 }
105
106 $public_name = build_first_name($value['public_names']) . ' ' . build_full_last_name($value['public_names'], $page->profile->isFemale());
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 }
114
115 Platal::page()->assign('public_name', $public_name);
116 Platal::page()->assign('private_name', $private_name);
117
118 return $value;
119 }
120
121 public function save(ProfilePage $page, $field, $value)
122 {
123 require_once 'name.func.inc.php';
124
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());
130
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);
133 $new_names->submit();
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;
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 }
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']);
156 } else {
157 update_display_names($page->profile,
158 $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
159 }
160 }
161
162 public function getText($value) {
163 $public_names = array();
164 foreach ($value['public_names'] as $name) {
165 if ($name != '') {
166 $public_names[] = $name;
167 }
168 }
169
170 if (isset($value['private_names']) && count($value['private_names'])) {
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);
179 }
180 }
181
182 class ProfileSettingEdu implements ProfileSetting
183 {
184 public function __construct() {
185 }
186
187 static function sortByGradYear($line1, $line2) {
188 $a = (isset($line1['grad_year'])) ? (int) $line1['grad_year'] : 0;
189 $b = (isset($line2['grad_year'])) ? (int) $line2['grad_year'] : 0;
190 if ($a == $b) {
191 return 0;
192 }
193 return ($a < $b) ? -1 : 1;
194 }
195
196 public function value(ProfilePage $page, $field, $value, &$success)
197 {
198 $success = true;
199 if (is_null($value)) {
200 $value = array();
201 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
202 FROM profile_education
203 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
204 ORDER BY id",
205 $page->pid());
206 } else if (!is_array($value)) {
207 $value = null;
208 } else {
209 $i = 0;
210 foreach ($value as $key=>&$edu) {
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 }
215 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
216 Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
217 $edu['grad_year'] = null;
218 $edu['warning'] = true;
219 }
220 if ($key != $i) {
221 $value[$i] = $edu;
222 unset($value[$key]);
223 }
224 $i++;
225 }
226 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
227 }
228 return $value;
229 }
230
231 public function save(ProfilePage $page, $field, $value)
232 {
233 XDB::execute("DELETE FROM profile_education
234 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))",
235 $page->pid());
236 $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
237 foreach ($value as $eduid=>&$edu) {
238 if ($edu['eduid'] != '' && $schoolsList[$edu['eduid']] != Profile::EDU_X) {
239 $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
240 XDB::execute("INSERT INTO profile_education
241 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
242 fieldid = {?}, grad_year = {?}, program = {?}",
243 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
244 $fieldId, $edu['grad_year'], $edu['program']);
245 }
246 }
247 }
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();
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 }
280 }
281 return implode(', ', $educations);
282 }
283 }
284
285 class 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
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",
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 = {?}
333 WHERE pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags)) AND degreeid = {?}",
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']]);
343 if ($item['program']) {
344 $details[] = '« ' . $item['program'] . ' »';
345 }
346 if ($item['fieldid']) {
347 $details[] = $fieldsList[$item['fieldid']];
348 }
349 }
350 return implode(', ', $educations);
351 }
352 }
353
354 class ProfileSettingEmailDirectory implements ProfileSetting
355 {
356 public function __construct(){}
357 public function save(ProfilePage $page, $field, $value){}
358
359 public function value(ProfilePage $page, $field, $value, &$success)
360 {
361 $p = Platal::page();
362
363 $success = true;
364 if (!is_null($value)) {
365 $email_stripped = strtolower(trim($value));
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');
370 $success = false;
371 } else {
372 $p->assign('email_error', '0');
373 }
374 }
375 return $value;
376 }
377
378 public function getText($value) {
379 return $value;
380 }
381 }
382
383 class ProfileSettingNetworking implements ProfileSetting
384 {
385 private $email;
386 private $pub;
387 private $web;
388 private $number;
389
390 public function __construct()
391 {
392 $this->email = new ProfileSettingEmail();
393 $this->pub = new ProfileSettingPub();
394 $this->web = new ProfileSettingWeb();
395 $this->number = new ProfileSettingNumber();
396 }
397
398 public function value(ProfilePage $page, $field, $value, &$success)
399 {
400 if (is_null($value)) {
401 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
402 FROM profile_networking AS n
403 WHERE n.pid = {?}",
404 $page->pid());
405 }
406 if (!is_array($value)) {
407 $value = array();
408 }
409 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
410 FROM profile_networking_enum;');
411 $success = true;
412 foreach($value as $i=>&$network) {
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;
422 $network['name'] = $filters[$network['type']]['name'];
423 if ($filters[$network['type']]['filter'] == 'web') {
424 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
425 } elseif ($filters[$network['type']]['filter'] == 'email') {
426 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
427 } elseif ($filters[$network['type']]['filter'] == 'number') {
428 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
429 }
430 if (!$s) {
431 $success = false;
432 $network['error'] = true;
433 }
434 }
435 }
436 return $value;
437 }
438
439 public function save(ProfilePage $page, $field, $value)
440 {
441 XDB::execute("DELETE FROM profile_networking
442 WHERE pid = {?}",
443 $page->pid());
444 if (!count($value)) {
445 return;
446 }
447 $insert = array();
448 foreach ($value as $id=>$network) {
449 XDB::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
450 VALUES ({?}, {?}, {?}, {?}, {?})",
451 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
452 }
453 }
454
455 public function getText($value) {
456 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
457 $networkings = array();
458 foreach ($value as $network) {
459 $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
460 }
461 return implode(', ' , $networkings);
462 }
463 }
464
465 class 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 {
477 $success = true;
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 }
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) {
519 static $pubs = array('public' => 'publique', 'private' => 'privé');
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
538 class ProfileSettingPromo implements ProfileSetting
539 {
540 public function __construct(){}
541
542 public function save(ProfilePage $page, $field, $value)
543 {
544 $gradYearNew = $value;
545 if ($page->profile->mainEducation() == 'X') {
546 $gradYearNew += $page->profile->mainEducationDuration();
547 }
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)) {
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());
559 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
560 } else {
561 $myorange = new OrangeReq(S::user(), $page->profile, $gradYearNew);
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
567 public function value(ProfilePage $page, $field, $value, &$success)
568 {
569 $entryYear = $page->profile->entry_year;
570 $gradYear = $page->profile->grad_year;
571 $yearpromo = $page->profile->yearpromo();
572 $success = true;
573 if (is_null($value) || $value == $yearpromo) {
574 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
575 $promoChoice = array();
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 }
582 }
583 Platal::page()->assign('promo_choice', $promoChoice);
584 }
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;
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 }
610
611 public function getText($value) {
612 return $value;
613 }
614 }
615
616
617 class ProfilePageGeneral extends ProfilePage
618 {
619 protected $pg_template = 'profile/general.tpl';
620
621 public function __construct(PlWizard $wiz)
622 {
623 parent::__construct($wiz);
624 $this->settings['search_names'] = new ProfileSettingSearchNames();
625 $this->settings['nationality1'] = $this->settings['nationality2']
626 = $this->settings['nationality3']
627 = $this->settings['promo_display']
628 = $this->settings['profile_title']
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();
634 $this->settings['main_edus'] = new ProfileSettingMainEdu();
635 $this->settings['promo'] = new ProfileSettingPromo();
636 $this->settings['networking'] = new ProfileSettingNetworking();
637 $this->settings['hobbies'] = new ProfileSettingHobby();
638 $this->watched = array('tels' => true,
639 'networking' => true, 'edus' => true,
640 'nationality1' => true, 'nationality2' => true,
641 'nationality3' => true, 'search_names' => true);
642
643 /* Some fields editable under condition */
644 if (!S::user()->isMe($this->owner)) {
645 $this->settings['deathdate'] = new ProfileSettingDate(true);
646 $this->settings['birthdate'] = new ProfileSettingDate(true);
647 $this->settings['birthdate_ref'] = new ProfileSettingDate(true);
648 $this->settings['axfreetext'] = null;
649 } else {
650 $this->settings['yourself'] = null;
651 $this->settings['birthdate'] = new ProfileSettingDate();
652 }
653 if (S::user()->checkPerms('directory_private')
654 || S::user()->isMyProfile($this->owner)) {
655 $this->settings['freetext'] = null;
656 $this->settings['freetext_pub'] = $this->settings['photo_pub']
657 = new ProfileSettingPub();
658 $this->watched['freetext'] = true;
659 }
660
661 Platal::page()->assign('is_registered', ($this->owner->perms ? true : false));
662 }
663
664 protected function _fetchData()
665 {
666 // Checkout all data...
667 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
668 p.email_directory as email_directory, pd.promo AS promo_display,
669 p.freetext, p.freetext_pub, p.axfreetext, p.ax_id AS matricule_ax, pd.yourself,
670 p.deathdate, IF(p.birthdate_ref = 0, '', p.birthdate_ref) AS birthdate_ref,
671 p.title AS profile_title
672 FROM profiles AS p
673 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
674 WHERE p.pid = {?}", $this->pid());
675 $this->values = $res->fetchOneAssoc();
676
677 // Retreive photo informations
678 $res = XDB::query("SELECT pub
679 FROM profile_photos
680 WHERE pid = {?}", $this->pid());
681 if ($res->numRows() == 0) {
682 $this->values['photo_pub'] = 'private';
683 } else {
684 $this->values['photo_pub'] = $res->fetchOneCell();
685 }
686
687 if ($this->owner) {
688 $res = XDB::query("SELECT COUNT(*)
689 FROM requests
690 WHERE type = 'photo' AND pid = {?}",
691 $this->owner->id());
692 $this->values['nouvellephoto'] = $res->fetchOneCell();
693 } else {
694 $this->values['nouvellephoto'] = 0;
695 }
696 }
697
698 protected function _saveData()
699 {
700 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
701 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
702 || $this->changed['axfreetext'] || $this->changed['email_directory'] || $this->changed['profile_title']) {
703 if ($this->values['nationality3'] == "") {
704 $this->values['nationality3'] = NULL;
705 }
706 if ($this->values['nationality2'] == "") {
707 $this->values['nationality2'] = $this->values['nationality3'];
708 $this->values['nationality3'] = NULL;
709 }
710 if ($this->values['nationality1'] == "") {
711 $this->values['nationality1'] = $this->values['nationality2'];
712 $this->values['nationality2'] = $this->values['nationality3'];
713 $this->values['nationality3'] = NULL;
714 }
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
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 }
732
733 XDB::execute("UPDATE profiles
734 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
735 freetext = {?}, freetext_pub = {?}, axfreetext = {?}, email_directory = {?}, title = {?}
736 WHERE pid = {?}",
737 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
738 ProfileSettingDate::toSQLDate($this->values['birthdate']),
739 $this->values['freetext'], $this->values['freetext_pub'], $this->values['axfreetext'], $new_email,
740 $this->values['profile_title'], $this->pid());
741 }
742 if ($this->changed['photo_pub']) {
743 XDB::execute("UPDATE profile_photos
744 SET pub = {?}
745 WHERE pid = {?}",
746 $this->values['photo_pub'], $this->pid());
747 }
748 if (S::user()->isMe($this->owner) && $this->changed['yourself']) {
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());
759 }
760 if ($this->changed['promo_display']) {
761 if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
762 $yearpromo = intval(substr($this->values['promo_display'], 1, 4));
763 if (($this->profile->mainEducation() == 'X' && $yearpromo >= $this->profile->entry_year)
764 || ($this->profile->mainEducation() != 'X'
765 && $yearpromo >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
766 XDB::execute('UPDATE profile_display
767 SET promo = {?}
768 WHERE pid = {?}',
769 $this->values['promo_display'], $this->pid());
770 XDB::execute('UPDATE profile_education
771 SET promo_year = {?}
772 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
773 $yearpromo, $this->pid());
774 }
775 }
776 }
777 if ($this->changed['birthdate_ref'] && S::admin() && !$this->owner->perms) {
778 XDB::execute('UPDATE profiles
779 SET birthdate_ref = {?}
780 WHERE pid = {?}',
781 ProfileSettingDate::toSQLDate($this->values['birthdate_ref']), $this->pid());
782 }
783 if (!S::user()->isMe($this->owner) && $this->changed['deathdate']) {
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 }
802 }
803
804 public function _prepare(PlPage $page, $id)
805 {
806 require_once "education.func.inc.php";
807
808 $res = XDB::query("SELECT id, field
809 FROM profile_education_field_enum
810 ORDER BY field");
811 $page->assign('edu_fields', $res->fetchAllAssoc());
812
813 require_once "emails.combobox.inc.php";
814 fill_email_combobox($page, array('source', 'redirect', 'job', 'directory'), $this->owner);
815
816 $res = XDB::query("SELECT nw.nwid AS type, nw.name
817 FROM profile_networking_enum AS nw
818 ORDER BY name");
819 $page->assign('network_list', $res->fetchAllAssoc());
820
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'));
824 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
825 }
826 }
827
828 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
829 ?>