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