+ private function prepare(ProfilePage $page, array &$new_value)
+ {
+ $initial_value = XDB::fetchOneAssoc('SELECT lastname_main, firstname_main
+ FROM profile_public_names
+ WHERE pid = {?}',
+ $page->pid());
+
+ $success = true;
+ foreach ($initial_value as $field => $name) {
+ $initial = name_to_basename($name);
+ $new = name_to_basename($new_value[$field]);
+
+ if (!($this->matchWord($initial, $new, strlen($new))
+ || ($field == 'lastname_main' && $new == 'DE ' . $initial))) {
+ $new_value[$field . '_error'] = true;
+ $success = false;
+ Platal::page()->trigError('Le nom choisi (' . $new . ') est trop loin de sa valeur initiale (' . $initial . ').');
+ }
+ }
+
+ return $success;
+ }
+
+ public function value(ProfilePage $page, $field, $value, &$success)
+ {
+ require_once 'name.func.inc.php';
+ $success = true;
+
+ if (is_null($value)) {
+ $request = NamesReq::getPublicNames($page->pid());
+
+ if (!$request) {
+ $value['public_names'] = XDB::fetchOneAssoc('SELECT lastname_main, lastname_marital, lastname_ordinary,
+ firstname_main, firstname_ordinary, pseudonym
+ FROM profile_public_names
+ WHERE pid = {?}',
+ $page->pid());
+ } else {
+ $value['public_names'] = $request;
+ Platal::page()->assign('validation', true);
+ }
+
+ $value['private_names'] = XDB::fetchAllAssoc('SELECT type, name
+ FROM profile_private_names
+ WHERE pid = {?}
+ ORDER BY type, id',
+ $page->pid());
+ } else {
+ foreach ($value['public_names'] as $key => $name) {
+ $value['public_names'][$key] = capitalize_name(trim($name));
+ }
+ if (isset($value['private_names'])) {
+ foreach ($value['private_names'] as $key => $name) {
+ $value['private_names'][$key]['name'] = trim($name['name']);
+ if ($value['private_names'][$key]['name'] == '') {
+ unset($value['private_names'][$key]);
+ }
+ }
+ }
+
+ if (S::user()->isMe($page->owner)) {
+ $success = $this->prepare($page, $value['public_names']);
+ }
+ }
+
+ $public_name = build_first_name($value['public_names']) . ' ' . build_full_last_name($value['public_names'], $page->profile->isFemale());
+ if (isset($value['private_names'])) {
+ $private_name_end = build_private_name($value['private_names']);
+ $private_name = $public_name . $private_name_end;
+ } else {
+ $value['private_names'] = array();
+ $private_name = $public_name;
+ }
+
+ Platal::page()->assign('public_name', $public_name);
+ Platal::page()->assign('private_name', $private_name);
+
+ return $value;
+ }
+
+ public function save(ProfilePage $page, $field, $value)
+ {
+ require_once 'name.func.inc.php';
+
+ $old = XDB::fetchOneAssoc('SELECT lastname_main, lastname_marital, lastname_ordinary,
+ firstname_main, firstname_ordinary, pseudonym
+ FROM profile_public_names
+ WHERE pid = {?}',
+ $page->pid());
+
+ if ($has_diff = $this->diff($page->pid(), $old, $value['public_names'])) {
+ $new_names = new NamesReq(S::user(), $page->profile, $value['public_names'], $old);
+ $new_names->submit();
+ Platal::page()->assign('validation', true);
+ Platal::page()->trigWarning('La demande de modification des noms a bien été prise en compte.' .
+ ' Un email sera envoyé dès que ces changements auront été effectués.');
+ }
+
+ XDB::execute('DELETE FROM profile_private_names
+ WHERE pid = {?}',
+ $page->pid());
+ $values = array();
+ $nickname = $lastname = $firstname = 0;
+ if (isset($value['private_names'])) {
+ foreach ($value['private_names'] as $name) {
+ $values[] = XDB::format('({?}, {?}, {?}, {?})', $page->pid(), $name['type'], $$name['type']++, $name['name']);
+ }
+ }
+ if (count($values)) {
+ XDB::rawExecute('INSERT INTO profile_private_names (pid, type, id, name)
+ VALUES ' . implode(',', $values));
+ }
+
+ if ($has_diff) {
+ update_display_names($page->profile, $old, $value['private_names']);
+ } else {
+ update_display_names($page->profile,
+ $value['public_names'], (isset($value['private_names']) ? $value['private_names'] : null));
+ }
+ }
+
+ public function getText($value) {
+ $public_names = array();
+ foreach ($value['public_names'] as $name) {
+ if ($name != '') {
+ $public_names[] = $name;
+ }
+ }
+
+ if (isset($value['private_names']) && count($value['private_names'])) {
+ $private_names = array();
+ foreach ($value['private_names'] as $name) {
+ $private_names[] = $name['name'];
+ }
+ return 'noms publics : ' . implode(', ' , $public_names) . ', noms privés : ' . implode(', ' , $private_names);;
+ }
+
+ return 'noms publics : ' . implode(', ' , $public_names);
+ }
+}
+
+class ProfileSettingEdu implements ProfileSetting
+{
+ public function __construct() {
+ }
+
+ static function sortByGradYear($line1, $line2) {
+ $a = (isset($line1['grad_year'])) ? (int) $line1['grad_year'] : 0;
+ $b = (isset($line2['grad_year'])) ? (int) $line2['grad_year'] : 0;
+ if ($a == $b) {
+ return 0;
+ }
+ return ($a < $b) ? -1 : 1;
+ }
+
+ public function value(ProfilePage $page, $field, $value, &$success)
+ {
+ $success = true;
+ if (is_null($value)) {
+ $value = array();
+ $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
+ FROM profile_education
+ WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
+ ORDER BY id",
+ $page->pid());
+ } else if (!is_array($value)) {
+ $value = null;
+ } else {
+ $i = 0;
+ foreach ($value as $key=>&$edu) {
+ if ($edu['eduid'] < 1 || !isset($edu['degreeid']) || $edu['degreeid'] < 1) {
+ Platal::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
+ $success = false;
+ }
+ if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
+ Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
+ $edu['grad_year'] = null;
+ $edu['warning'] = true;
+ }
+ if ($key != $i) {
+ $value[$i] = $edu;
+ unset($value[$key]);
+ }
+ $i++;
+ }
+ usort($value, array("ProfileSettingEdu", "sortByGradYear"));
+ }
+ return $value;
+ }
+
+ public function save(ProfilePage $page, $field, $value)
+ {
+ XDB::execute("DELETE FROM profile_education
+ WHERE pid = {?} AND !(FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))",
+ $page->pid());
+ $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
+ foreach ($value as $eduid=>&$edu) {
+ if ($edu['eduid'] != '' && $schoolsList[$edu['eduid']] != Profile::EDU_X) {
+ $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
+ XDB::execute("INSERT INTO profile_education
+ SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
+ fieldid = {?}, grad_year = {?}, program = {?}",
+ $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
+ $fieldId, $edu['grad_year'], $edu['program']);
+ }
+ }
+ }
+
+ public function getText($value) {
+ $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
+ $degreesList = DirEnum::getOptions(DirEnum::EDUDEGREES);
+ $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS);
+ $educations = array();
+ foreach ($value as $id => $education) {
+ // XXX: the following condition should be removed once there are no more incomplete educations.
+ if (is_null($education['eduid']) || is_null($education['degreeid'])) {
+ if (is_null($education['eduid']) && is_null($education['degreeid'])) {
+ $educations[$id] = 'formation manquante';
+ } else {
+ $educations[$id] = (is_null($education['eduid']) ? 'université manquante' : $schoolsList[$education['eduid']]) . ', '
+ . (is_null($education['degreeid']) ? 'diplôme manquant' : $degreesList[$education['degreeid']]);
+ }
+ } else {
+ $educations[$id] = $schoolsList[$education['eduid']] . ', ' . $degreesList[$education['degreeid']];
+ }
+
+ $details = array();
+ if ($education['grad_year']) {
+ $details[] = $education['grad_year'];
+ }
+ if ($education['program']) {
+ $details[] = '« ' . $education['program'] . ' »';
+ }
+ if ($education['fieldid']) {
+ $details[] = $fieldsList[$education['fieldid']];
+ }
+ if (count($details)) {
+ $educations[$id] .= ' (' . implode(', ', $details) . ')';
+ }
+ }
+ return implode(', ', $educations);
+ }
+}
+
+class ProfileSettingMainEdu implements ProfileSetting
+{
+ private $cycles;
+
+ public function __construct()
+ {
+ $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
+ $eduDegrees = array_flip($eduDegrees);
+ $this->cycles = array(
+ $eduDegrees[Profile::DEGREE_X] => 'Cycle polytechnicien',
+ $eduDegrees[Profile::DEGREE_M] => 'Cycle master',
+ $eduDegrees[Profile::DEGREE_D] => 'Cycle doctoral'
+ );
+ }
+
+ public function value(ProfilePage $page, $field, $value, &$success)
+ {
+ $success = true;
+ if (is_null($value)) {
+ $value = array();
+ $value = XDB::fetchAllAssoc("SELECT degreeid, fieldid, promo_year, program
+ FROM profile_education
+ WHERE pid = {?} AND (FIND_IN_SET('primary', flags) OR FIND_IN_SET('secondary', flags))
+ ORDER BY NOT FIND_IN_SET('primary', flags), degreeid",
+ $page->pid());
+
+ foreach ($value as &$item) {
+ $item['cycle'] = $this->cycles[$item['degreeid']];
+ }
+ } elseif (!is_array($value)) {
+ $value = array();
+ } else {
+ foreach ($value as $key => $item) {
+ if (!isset($item['degreeid'])) {
+ unset($value[$key]);
+ }
+ }
+ }
+
+ return $value;
+ }
+
+ public function save(ProfilePage $page, $field, $value)