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 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 }
105 }
106 }
107
108 if (S::user()->isMe($page->owner)) {
109 $success = $this->prepare($page, $value['public_names']);
110 }
111 }
112
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());
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 }
122
123 Platal::page()->assign('public_name', $public_name);
124 Platal::page()->assign('private_name', $private_name);
125
126 return $value;
127 }
128
129 public function save(ProfilePage $page, $field, $value)
130 {
131 require_once 'name.func.inc.php';
132
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());
138
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);
141 $new_names->submit();
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;
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 }
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']);
164 } else {
165 update_display_names($page->profile,
166 $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
167 }
168 }
169
170 public function getText($value) {
171 $public_names = array();
172 foreach ($value['public_names'] as $name) {
173 if ($name != '') {
174 $public_names[] = $name;
175 }
176 }
177
178 if (isset($value['private_names']) && count($value['private_names'])) {
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);
187 }
188 }
189
190 class ProfileSettingEdu implements ProfileSetting
191 {
192 public function __construct() {
193 }
194
195 static function sortByGradYear($line1, $line2) {
196 $a = (isset($line1['grad_year'])) ? (int) $line1['grad_year'] : 0;
197 $b = (isset($line2['grad_year'])) ? (int) $line2['grad_year'] : 0;
198 if ($a == $b) {
199 return 0;
200 }
201 return ($a < $b) ? -1 : 1;
202 }
203
204 public function value(ProfilePage $page, $field, $value, &$success)
205 {
206 $success = true;
207 if (is_null($value)) {
208 $value = array();
209 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
210 FROM profile_education
211 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
212 ORDER BY id",
213 $page->pid());
214 } else if (!is_array($value)) {
215 $value = null;
216 } else {
217 $i = 0;
218 foreach ($value as $key=>&$edu) {
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 }
223 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
224 Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
225 $edu['grad_year'] = null;
226 $edu['warning'] = true;
227 }
228 if ($key != $i) {
229 $value[$i] = $edu;
230 unset($value[$key]);
231 }
232 $i++;
233 }
234 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
235 }
236 return $value;
237 }
238
239 public function save(ProfilePage $page, $field, $value)
240 {
241 XDB::execute("DELETE FROM profile_education
242 WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))",
243 $page->pid());
244 $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
245 foreach ($value as $eduid=>&$edu) {
246 if ($edu['eduid'] != '' && $schoolsList[$edu['eduid']] != Profile::EDU_X) {
247 $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
248 XDB::execute("INSERT INTO profile_education
249 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
250 fieldid = {?}, grad_year = {?}, program = {?}",
251 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
252 $fieldId, $edu['grad_year'], $edu['program']);
253 }
254 }
255 }
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();
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 }
288 }
289 return implode(', ', $educations);
290 }
291 }
292
293 class 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
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",
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 = {?}
341 WHERE pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags)) AND degreeid = {?}",
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']]);
351 if ($item['program']) {
352 $details[] = '« ' . $item['program'] . ' »';
353 }
354 if ($item['fieldid']) {
355 $details[] = $fieldsList[$item['fieldid']];
356 }
357 }
358 return implode(', ', $educations);
359 }
360 }
361
362 class ProfileSettingEmailDirectory implements ProfileSetting
363 {
364 public function __construct(){}
365 public function save(ProfilePage $page, $field, $value){}
366
367 public function value(ProfilePage $page, $field, $value, &$success)
368 {
369 $p = Platal::page();
370
371 $success = true;
372 if (!is_null($value)) {
373 $email_stripped = strtolower(trim($value));
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');
378 $success = false;
379 } else {
380 $p->assign('email_error', '0');
381 }
382 }
383 return $value;
384 }
385
386 public function getText($value) {
387 return $value;
388 }
389 }
390
391 class ProfileSettingNetworking implements ProfileSetting
392 {
393 private $email;
394 private $pub;
395 private $web;
396 private $number;
397
398 public function __construct()
399 {
400 $this->email = new ProfileSettingEmail();
401 $this->pub = new ProfileSettingPub();
402 $this->web = new ProfileSettingWeb();
403 $this->number = new ProfileSettingNumber();
404 }
405
406 public function value(ProfilePage $page, $field, $value, &$success)
407 {
408 if (is_null($value)) {
409 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
410 FROM profile_networking AS n
411 WHERE n.pid = {?}",
412 $page->pid());
413 }
414 if (!is_array($value)) {
415 $value = array();
416 }
417 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
418 FROM profile_networking_enum;');
419 $success = true;
420 foreach($value as $i=>&$network) {
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;
430 $network['name'] = $filters[$network['type']]['name'];
431 if ($filters[$network['type']]['filter'] == 'web') {
432 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
433 } elseif ($filters[$network['type']]['filter'] == 'email') {
434 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
435 } elseif ($filters[$network['type']]['filter'] == 'number') {
436 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
437 }
438 if (!$s) {
439 $success = false;
440 $network['error'] = true;
441 }
442 }
443 }
444 return $value;
445 }
446
447 public function save(ProfilePage $page, $field, $value)
448 {
449 XDB::execute("DELETE FROM profile_networking
450 WHERE pid = {?}",
451 $page->pid());
452 if (!count($value)) {
453 return;
454 }
455 $insert = array();
456 foreach ($value as $id=>$network) {
457 XDB::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
458 VALUES ({?}, {?}, {?}, {?}, {?})",
459 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
460 }
461 }
462
463 public function getText($value) {
464 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
465 $networkings = array();
466 foreach ($value as $network) {
467 $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
468 }
469 return implode(', ' , $networkings);
470 }
471 }
472
473 class 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 {
485 $success = true;
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 }
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
546 class ProfileSettingPromo implements ProfileSetting
547 {
548 public function __construct(){}
549
550 public function save(ProfilePage $page, $field, $value)
551 {
552 $gradYearNew = $value;
553 if ($page->profile->mainEducation() == 'X') {
554 $gradYearNew += $page->profile->mainEducationDuration();
555 }
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)) {
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());
567 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
568 } else {
569 $myorange = new OrangeReq(S::user(), $page->profile, $gradYearNew);
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
575 public function value(ProfilePage $page, $field, $value, &$success)
576 {
577 $entryYear = $page->profile->entry_year;
578 $gradYear = $page->profile->grad_year;
579 $yearpromo = $page->profile->yearpromo();
580 $success = true;
581 if (is_null($value) || $value == $yearpromo) {
582 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
583 $promoChoice = array();
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 }
590 }
591 Platal::page()->assign('promo_choice', $promoChoice);
592 }
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;
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 }
618
619 public function getText($value) {
620 return $value;
621 }
622 }
623
624
625 class ProfilePageGeneral extends ProfilePage
626 {
627 protected $pg_template = 'profile/general.tpl';
628
629 public function __construct(PlWizard $wiz)
630 {
631 parent::__construct($wiz);
632 $this->settings['search_names'] = new ProfileSettingSearchNames();
633 $this->settings['nationality1'] = $this->settings['nationality2']
634 = $this->settings['nationality3']
635 = $this->settings['promo_display']
636 = $this->settings['profile_title']
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();
642 $this->settings['main_edus'] = new ProfileSettingMainEdu();
643 $this->settings['promo'] = new ProfileSettingPromo();
644 $this->settings['networking'] = new ProfileSettingNetworking();
645 $this->settings['hobbies'] = new ProfileSettingHobby();
646 $this->watched = array('tels' => true,
647 'networking' => true, 'edus' => true,
648 'nationality1' => true, 'nationality2' => true,
649 'nationality3' => true, 'search_names' => true);
650
651 /* Some fields editable under condition */
652 if (!S::user()->isMe($this->owner)) {
653 $this->settings['deathdate'] = new ProfileSettingDate(true);
654 $this->settings['birthdate'] = new ProfileSettingDate(true);
655 $this->settings['birthdate_ref'] = new ProfileSettingDate(true);
656 } else {
657 $this->settings['yourself'] = null;
658 $this->settings['birthdate'] = new ProfileSettingDate();
659 }
660 if (S::user()->checkPerms('directory_private')
661 || S::user()->isMyProfile($this->owner)) {
662 $this->settings['freetext'] = null;
663 $this->settings['freetext_pub'] = $this->settings['photo_pub']
664 = new ProfileSettingPub();
665 $this->watched['freetext'] = true;
666 }
667
668 Platal::page()->assign('is_registered', ($this->owner->perms ? true : false));
669 }
670
671 protected function _fetchData()
672 {
673 // Checkout all data...
674 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
675 p.email_directory as email_directory, pd.promo AS promo_display,
676 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself,
677 p.deathdate, IF(p.birthdate_ref = 0, '', p.birthdate_ref) AS birthdate_ref,
678 p.title AS profile_title
679 FROM profiles AS p
680 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
681 WHERE p.pid = {?}", $this->pid());
682 $this->values = $res->fetchOneAssoc();
683
684 // Retreive photo informations
685 $res = XDB::query("SELECT pub
686 FROM profile_photos
687 WHERE pid = {?}", $this->pid());
688 if ($res->numRows() == 0) {
689 $this->values['photo_pub'] = 'private';
690 } else {
691 $this->values['photo_pub'] = $res->fetchOneCell();
692 }
693
694 if ($this->owner) {
695 $res = XDB::query("SELECT COUNT(*)
696 FROM requests
697 WHERE type = 'photo' AND pid = {?}",
698 $this->owner->id());
699 $this->values['nouvellephoto'] = $res->fetchOneCell();
700 } else {
701 $this->values['nouvellephoto'] = 0;
702 }
703 }
704
705 protected function _saveData()
706 {
707 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
708 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
709 || $this->changed['email_directory'] || $this->changed['profile_title']) {
710 if ($this->values['nationality3'] == "") {
711 $this->values['nationality3'] = NULL;
712 }
713 if ($this->values['nationality2'] == "") {
714 $this->values['nationality2'] = $this->values['nationality3'];
715 $this->values['nationality3'] = NULL;
716 }
717 if ($this->values['nationality1'] == "") {
718 $this->values['nationality1'] = $this->values['nationality2'];
719 $this->values['nationality2'] = $this->values['nationality3'];
720 $this->values['nationality3'] = NULL;
721 }
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
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 }
739
740 XDB::execute("UPDATE profiles
741 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
742 freetext = {?}, freetext_pub = {?}, email_directory = {?}, title = {?}
743 WHERE pid = {?}",
744 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
745 ProfileSettingDate::toSQLDate($this->values['birthdate']),
746 $this->values['freetext'], $this->values['freetext_pub'], $new_email,
747 $this->values['profile_title'], $this->pid());
748 }
749 if ($this->changed['photo_pub']) {
750 XDB::execute("UPDATE profile_photos
751 SET pub = {?}
752 WHERE pid = {?}",
753 $this->values['photo_pub'], $this->pid());
754 }
755 if (S::user()->isMe($this->owner) && $this->changed['yourself']) {
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());
766 }
767 if ($this->changed['promo_display']) {
768 if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
769 $yearpromo = intval(substr($this->values['promo_display'], 1, 4));
770 if (($this->profile->mainEducation() == 'X' && $yearpromo >= $this->profile->entry_year)
771 || ($this->profile->mainEducation() != 'X'
772 && $yearpromo >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
773 XDB::execute('UPDATE profile_display
774 SET promo = {?}
775 WHERE pid = {?}',
776 $this->values['promo_display'], $this->pid());
777 XDB::execute('UPDATE profile_education
778 SET promo_year = {?}
779 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
780 $yearpromo, $this->pid());
781 }
782 }
783 }
784 if ($this->changed['birthdate_ref'] && S::admin() && !$this->owner->perms) {
785 XDB::execute('UPDATE profiles
786 SET birthdate_ref = {?}
787 WHERE pid = {?}',
788 ProfileSettingDate::toSQLDate($this->values['birthdate_ref']), $this->pid());
789 }
790 if (!S::user()->isMe($this->owner) && $this->changed['deathdate']) {
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 }
809 }
810
811 public function _prepare(PlPage $page, $id)
812 {
813 require_once "education.func.inc.php";
814
815 $res = XDB::query("SELECT id, field
816 FROM profile_education_field_enum
817 ORDER BY field");
818 $page->assign('edu_fields', $res->fetchAllAssoc());
819
820 require_once "emails.combobox.inc.php";
821 fill_email_combobox($page, array('source', 'redirect', 'job', 'directory'), $this->owner);
822
823 $res = XDB::query("SELECT nw.nwid AS type, nw.name
824 FROM profile_networking_enum AS nw
825 ORDER BY name");
826 $page->assign('network_list', $res->fetchAllAssoc());
827
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'));
831 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
832 }
833 }
834
835 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
836 ?>