2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileSettingSearchNames
implements ProfileSetting
24 private $private_name_end;
25 private $search_names;
28 public function __construct() {
29 $this->name_types
= DirEnum
::getOptions(DirEnum
::NAMES
);
32 private function matchWord($old, $new, $newLen)
34 return ($i = strpos($old, $new)) !== false
35 && ($i == 0 ||
$old{$i-1} == ' ')
36 && ($i +
$newLen == strlen($old) ||
$old{$i +
$newLen} == ' ');
39 private function prepareField($value)
41 return name_to_basename($value);
44 private function prepare(ProfilePage
$page, $field, $value, $init, &$success)
47 $ini = $this->prepareField($init);
48 $new = $this->prepareField($value);
49 $newLen = strlen($new);
50 $success = $this->matchWord($ini, $new, $newLen)
51 ||
($field == 'lastname' && $new == 'DE ' . $ini);
53 $field = strtolower($field);
54 Platal
::page()->trigError("Le " . $this->name_types
[$field] . " que tu as choisi (" . $value .
55 ") est trop loin de ton " . $this->name_types
[$field] . " initial (" . $init . ").");
57 return $success ?
$value : $init;
60 /* Removes duplicated entries for the fields that do not allow them. */
61 private function clean($value)
63 $single_types = XDB
::fetchAllAssoc('id',
65 FROM profile_name_enum
66 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
68 foreach ($value as $key => $item) {
69 if (isset($single_types[$item['typeid']])) {
70 if ($single_types[$item['typeid']] === true
) {
73 $single_types[$item['typeid']] = true
;
81 public function value(ProfilePage
$page, $field, $value, &$success)
86 if (is_null($value)) {
87 $sn_all = XDB
::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
88 sn.particle, sn.typeid, e.type, e.name AS type_name,
89 FIND_IN_SET('has_particle', e.flags) AS has_particle,
90 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
91 FIND_IN_SET('public', e.flags) AS pub
92 FROM profile_name AS sn
93 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
94 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
95 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
98 $sn_types = XDB
::iterator("SELECT id, type, name,
99 FIND_IN_SET('has_particle', flags) AS has_particle
100 FROM profile_name_enum
101 WHERE NOT FIND_IN_SET('not_displayed', flags)
102 AND FIND_IN_SET('always_displayed', flags)
106 $sn = $sn_all->next();
107 while ($sn_type = $sn_types->next()) {
108 if ($sn_type['id'] == $sn['typeid']) {
111 $sn = $sn_all->next();
114 $value[] = array('name' => '',
116 'typeid' => $sn_type['id'],
117 'type' => $sn_type['type'],
118 'type_name' => $sn_type['name'],
119 'has_particle' => $sn_type['has_particle'],
120 'always_displayed' => 1,
127 } while ($sn = $sn_all->next());
129 $namesRequest = ProfileValidate
::get_typed_requests($page->pid(), 'usage');
130 if (count($namesRequest) > 0) {
131 Platal
::page()->assign('validation', true
);
133 $value = $this->clean($value);
135 require_once 'name.func.inc.php';
137 $value = $this->clean($value);
138 $res = XDB
::query("SELECT s.particle, s.name
139 FROM profile_name AS s
140 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
141 WHERE s.pid = {?} AND (e.type = 'lastname' OR e.type = 'firstname')
142 ORDER BY e.type = 'firstname'",
144 $res = $res->fetchAllAssoc();
146 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
147 $initial['firstname'] = $res[1]['name'];
148 $sn_types = build_types();
149 $this->search_names
= array();
150 foreach ($value as &$sn) {
151 $sn['name'] = trim($sn['name']);
152 if (S
::user()->isMe($page->owner
) && ($sn['type'] == 'firstname' ||
$sn['type'] == 'lastname')) {
153 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
154 $initial[$sn['type']], $success_tmp);
155 $success = $success && $success_tmp;
158 if (isset($sn['particle']) && ($sn['particle'] != '')) {
159 // particle is before first blank
160 list($particle, $name) = explode(' ', $sn['name'], 2);
161 $particle = trim($particle) . ' ';
163 // particle is before first quote
164 list($particle, $name) = explode('\'', $sn['name'], 2);
165 $particle = trim($particle);
167 // actually there is no particle
177 if ($sn['name'] != '') {
179 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
181 'particle' => $particle,
182 'pub' => $sn['pub']);
184 if (isset($this->search_names
[$sn['typeid']])) {
185 $this->search_names
[$sn['typeid']][] = $sn['name'];
187 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
189 $sn['type_name'] = $sn_types[$sn['typeid']];
193 $res = XDB
::query("SELECT public_name, private_name
197 list($public_name, $private_name) = $res->fetchOneRow();
199 $sn_types_private = build_types('private');
200 $this->private_name_end
= build_private_name($this->search_names
, $sn_types_private);
201 $private_name = $public_name . $this->private_name_end
;
203 Platal
::page()->assign('public_name', $public_name);
204 Platal
::page()->assign('private_name', $private_name);
210 public function save(ProfilePage
$page, $field, $value)
212 require_once 'name.func.inc.php';
214 $sn_old = build_sn_pub($page->pid());
215 XDB
::execute("DELETE FROM s
216 USING profile_name AS s
217 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
218 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
220 $has_new = set_alias_names($this->search_names
, $sn_old, $page->pid(), $page->owner
);
222 // Only requires validation if modification in public names
224 $new_names = new NamesReq(S
::user(), $page->profile
, $this->search_names
, $this->private_name_end
);
225 $new_names->submit();
226 Platal
::page()->trigWarning('La demande de modification de tes noms a bien été prise en compte.' .
227 ' Tu recevras un email dès que ces changements auront été effectués.');
229 $display_names = array();
230 build_display_names($display_names, $this->search_names
,
231 $page->profile
->isFemale(), $this->private_name_end
);
232 set_profile_display($display_names, $page->profile
);
236 public function getText($value) {
238 foreach ($value as $name) {
239 if ($name['name'] != '') {
240 $names[] = mb_strtolower($name['type_name']) . ' : ' . $name['name'];
243 return implode(', ' , $names);
247 class ProfileSettingEdu
implements ProfileSetting
249 public function __construct() {
252 static function sortByGradYear($line1, $line2) {
253 $a = (isset($line1['grad_year'])) ?
(int) $line1['grad_year'] : 0;
254 $b = (isset($line2['grad_year'])) ?
(int) $line2['grad_year'] : 0;
258 return ($a < $b) ?
-1 : 1;
261 public function value(ProfilePage
$page, $field, $value, &$success)
264 if (is_null($value)) {
266 $value = XDB
::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
267 FROM profile_education
268 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
271 } else if (!is_array($value)) {
275 foreach ($value as $key=>&$edu) {
276 if ($edu['eduid'] < 1 ||
!isset($edu['degreeid']) ||
$edu['degreeid'] < 1) {
277 Platal
::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
280 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
281 Platal
::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
282 $edu['grad_year'] = null
;
283 $edu['warning'] = true
;
291 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
296 public function save(ProfilePage
$page, $field, $value)
298 XDB
::execute("DELETE FROM profile_education
299 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
301 foreach ($value as $eduid=>&$edu) {
302 if ($edu['eduid'] != '') {
303 $fieldId = ($edu['fieldid'] == 0) ? null
: $edu['fieldid'];
304 XDB
::execute("INSERT INTO profile_education
305 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
306 fieldid = {?}, grad_year = {?}, program = {?}",
307 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
308 $fieldId, $edu['grad_year'], $edu['program']);
313 public function getText($value) {
314 $schoolsList = DirEnum
::getOptions(DirEnum
::EDUSCHOOLS
);
315 $degreesList = DirEnum
::getOptions(DirEnum
::EDUDEGREES
);
316 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
317 $educations = array();
318 foreach ($value as $id => $education) {
319 // XXX: the following condition should be removed once there are no more incomplete educations.
320 if (is_null($education['eduid']) ||
is_null($education['degreeid'])) {
321 if (is_null($education['eduid']) && is_null($education['degreeid'])) {
322 $educations[$id] = 'formation manquante';
324 $educations[$id] = (is_null($education['eduid']) ?
'université manquante' : $schoolsList[$education['eduid']]) . ', '
325 . (is_null($education['degreeid']) ?
'diplôme manquant' : $degreesList[$education['degreeid']]);
328 $educations[$id] = $schoolsList[$education['eduid']] . ', ' . $degreesList[$education['degreeid']];
332 if ($education['grad_year']) {
333 $details[] = $education['grad_year'];
335 if ($education['program']) {
336 $details[] = '« ' . $education['program'] . ' »';
338 if ($education['fieldid']) {
339 $details[] = $fieldsList[$education['fieldid']];
341 if (count($details)) {
342 $educations[$id] .= ' (' . implode(', ', $details) . ')';
345 return implode(', ', $educations);
349 class ProfileSettingEmailDirectory
implements ProfileSetting
351 public function __construct(){}
352 public function save(ProfilePage
$page, $field, $value){}
354 public function value(ProfilePage
$page, $field, $value, &$success)
359 if (!is_null($value)) {
360 $email_stripped = strtolower(trim($value));
361 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
362 $p->assign('email_error', '1');
363 $p->assign('email_directory_error', $email_stripped);
364 $p->trigError('Adresse Email invalide');
367 $p->assign('email_error', '0');
373 public function getText($value) {
378 class ProfileSettingNetworking
implements ProfileSetting
385 public function __construct()
387 $this->email
= new ProfileSettingEmail();
388 $this->pub
= new ProfileSettingPub();
389 $this->web
= new ProfileSettingWeb();
390 $this->number
= new ProfileSettingNumber();
393 public function value(ProfilePage
$page, $field, $value, &$success)
395 if (is_null($value)) {
396 $value = XDB
::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
397 FROM profile_networking AS n
401 if (!is_array($value)) {
404 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
405 FROM profile_networking_enum;');
407 foreach($value as $i=>&$network) {
408 if (!trim($network['address'])) {
411 if (!isset($network['pub'])) {
412 $network['pub'] = 'private';
414 $network['error'] = false
;
415 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
417 $network['name'] = $filters[$network['type']]['name'];
418 if ($filters[$network['type']]['filter'] == 'web') {
419 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
420 } elseif ($filters[$network['type']]['filter'] == 'email') {
421 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
422 } elseif ($filters[$network['type']]['filter'] == 'number') {
423 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
427 $network['error'] = true
;
434 public function save(ProfilePage
$page, $field, $value)
436 XDB
::execute("DELETE FROM profile_networking
439 if (!count($value)) {
443 foreach ($value as $id=>$network) {
444 XDB
::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
445 VALUES ({?}, {?}, {?}, {?}, {?})",
446 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
450 public function getText($value) {
451 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
452 $networkings = array();
453 foreach ($value as $network) {
454 $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
456 return implode(', ' , $networkings);
460 class ProfileSettingPromo
implements ProfileSetting
462 public function __construct(){}
464 public function save(ProfilePage
$page, $field, $value)
466 $gradYearNew = $value;
467 if ($page->profile
->mainEducation() == 'X') {
468 $gradYearNew +
= $page->profile
->mainEducationDuration();
470 if (($page->profile
->mainEducation() != 'X'
471 && $value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration())
472 ||
($page->profile
->mainEducation() == 'X' && $value == $page->profile
->entry_year
)) {
473 XDB
::execute('UPDATE profile_display
476 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
477 XDB
::execute('UPDATE profile_education
479 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
480 $gradYearNew, $page->profile
->id());
481 Platal
::page()->trigSuccess('Ton statut « orange » a été supprimé.');
483 $myorange = new OrangeReq(S
::user(), $page->profile
, $gradYearNew);
485 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
489 public function value(ProfilePage
$page, $field, $value, &$success)
491 $entryYear = $page->profile
->entry_year
;
492 $gradYear = $page->profile
->grad_year
;
493 $yearpromo = $page->profile
->grad_year
;
494 if ($page->profile
->mainEducation() == 'X') {
495 $yearpromo -= $page->profile
->mainEducationDuration();
498 if (is_null($value) ||
$value == $yearpromo) {
499 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
500 $promoChoice = array();
501 for ($i = $entryYear; $i <= $gradYear - $page->profile
->mainEducationDuration(); ++
$i) {
502 if ($page->profile
->mainEducation() == 'X') {
503 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
505 $promoChoice[] = $page->profile
->mainEducation() . strval($i +
$page->profile
->mainEducationDuration());
508 Platal
::page()->assign('promo_choice', $promoChoice);
513 // If this profile belongs to an X, $promoNew needs to be changed to
514 // the graduation year.
515 $gradYearNew = $value;
516 if ($page->profile
->mainEducation() == 'X') {
517 $gradYearNew +
= $page->profile
->mainEducationDuration();
520 if ($value < 1000 ||
$value > 9999) {
521 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
523 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
524 Platal
::page()->trigError('Trop tôt !');
527 return intval($value);
530 public function getText($value) {
536 class ProfilePageGeneral
extends ProfilePage
538 protected $pg_template = 'profile/general.tpl';
540 public function __construct(PlWizard
$wiz)
542 parent
::__construct($wiz);
543 $this->settings
['search_names']
544 = new ProfileSettingSearchNames();
545 $this->settings
['nationality1']
546 = $this->settings
['nationality2']
547 = $this->settings
['nationality3']
548 = $this->settings
['promo_display']
550 $this->settings
['email_directory']
551 = new ProfileSettingEmail();
552 $this->settings
['email_directory_new']
553 = new ProfileSettingEmailDirectory();
554 $this->settings
['networking'] = new ProfileSettingNetworking();
555 $this->settings
['tels'] = new ProfileSettingPhones();
556 $this->settings
['edus'] = new ProfileSettingEdu();
557 $this->settings
['promo'] = new ProfileSettingPromo();
558 $this->watched
= array('tels' => true
,
559 'networking' => true
, 'edus' => true
,
560 'nationality1' => true
, 'nationality2' => true
,
561 'nationality3' => true
, 'search_names' => true
);
563 /* Some fields editable under condition */
564 if (!S
::user()->isMe($this->owner
)) {
565 $this->settings
['deathdate'] = new ProfileSettingDate(true
);
566 $this->settings
['birthdate'] = new ProfileSettingDate(true
);
568 $this->settings
['yourself'] = null
;
569 $this->settings
['birthdate'] = new ProfileSettingDate();
571 if (S
::user()->checkPerms('directory_private')
572 || S
::user()->isMyProfile($this->owner
)) {
573 $this->settings
['freetext'] = null
;
574 $this->settings
['freetext_pub']
575 = $this->settings
['photo_pub']
576 = new ProfileSettingPub();
577 $this->watched
['freetext'] = true
;
582 protected function _fetchData()
584 // Checkout all data...
585 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
586 p.email_directory as email_directory, pd.promo AS promo_display,
587 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself,
590 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
591 WHERE p.pid = {?}", $this->pid());
592 $this->values
= $res->fetchOneAssoc();
594 // Retreive photo informations
595 $res = XDB
::query("SELECT pub
597 WHERE pid = {?}", $this->pid());
598 if ($res->numRows() == 0) {
599 $this->values
['photo_pub'] = 'private';
601 $this->values
['photo_pub'] = $res->fetchOneCell();
605 $res = XDB
::query("SELECT COUNT(*)
607 WHERE type = 'photo' AND pid = {?}",
609 $this->values
['nouvellephoto'] = $res->fetchOneCell();
611 $this->values
['nouvellephoto'] = 0;
615 protected function _saveData()
617 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
618 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
619 ||
$this->changed
['email_directory']) {
620 if ($this->values
['nationality3'] == "") {
621 $this->values
['nationality3'] = NULL
;
623 if ($this->values
['nationality2'] == "") {
624 $this->values
['nationality2'] = $this->values
['nationality3'];
625 $this->values
['nationality3'] = NULL
;
627 if ($this->values
['nationality1'] == "") {
628 $this->values
['nationality1'] = $this->values
['nationality2'];
629 $this->values
['nationality2'] = $this->values
['nationality3'];
630 $this->values
['nationality3'] = NULL
;
632 if ($this->values
['nationality1'] == $this->values
['nationality2']
633 && $this->values
['nationality2'] == $this->values
['nationality3']) {
634 $this->values
['nationality2'] = NULL
;
635 $this->values
['nationality3'] = NULL
;
636 } else if ($this->values
['nationality1'] == $this->values
['nationality2']) {
637 $this->values
['nationality2'] = $this->values
['nationality3'];
638 $this->values
['nationality3'] = NULL
;
639 } else if ($this->values
['nationality2'] == $this->values
['nationality3']
640 ||
$this->values
['nationality1'] == $this->values
['nationality3']) {
641 $this->values
['nationality3'] = NULL
;
644 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
645 $this->values
['email_directory_new'] : $this->values
['email_directory'];
646 if ($new_email == "") {
650 XDB
::execute("UPDATE profiles
651 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
652 freetext = {?}, freetext_pub = {?}, email_directory = {?}
654 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
655 ProfileSettingDate
::toSQLDate($this->values
['birthdate']),
656 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
658 if ($this->changed
['photo_pub']) {
659 XDB
::execute("UPDATE profile_photos
662 $this->values
['photo_pub'], $this->pid());
664 if (S
::user()->isMe($this->owner
) && $this->changed
['yourself']) {
666 XDB
::execute('UPDATE accounts
667 SET display_name = {?}
669 $this->values
['yourself'], $this->owner
->id());
671 XDB
::execute('UPDATE profile_display
673 WHERE pid = {?}', $this->values
['yourself'],
676 if ($this->changed
['promo_display']) {
677 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()) {
678 if (($this->profile
->mainEducation() == 'X'
679 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year
)
680 ||
($this->profile
->mainEducation() != 'X'
681 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration())) {
682 XDB
::execute('UPDATE profile_display
685 $this->values
['promo_display'], $this->pid());
689 if (!S
::user()->isMe($this->owner
) && $this->changed
['deathdate']) {
690 XDB
::execute('UPDATE profiles
691 SET deathdate = {?}, deathdate_rec = NOW()
692 WHERE pid = {?} AND deathdate_rec IS NULL',
693 ProfileSettingDate
::toSQLDate($this->values
['deathdate']), $this->pid());
694 if (XDB
::affectedRows() > 0) {
695 $this->profile
->clear();
697 $this->owner
->clear(true
);
700 /* deathdate_rec was not NULL, this is just an update of the death date
702 XDB
::execute('UPDATE profiles
705 ProfileSettingDate
::toSQLDate($this->values
['deathdate']), $this->pid());
710 public function _prepare(PlPage
$page, $id)
712 require_once "education.func.inc.php";
714 $res = XDB
::query("SELECT id, field
715 FROM profile_education_field_enum
717 $page->assign('edu_fields', $res->fetchAllAssoc());
719 require_once "emails.combobox.inc.php";
720 fill_email_combobox($page, $this->owner
);
722 $res = XDB
::query("SELECT nw.nwid AS type, nw.name
723 FROM profile_networking_enum AS nw
725 $page->assign('network_list', $res->fetchAllAssoc());
727 $res = XDB
::query("SELECT public_name, private_name
731 $res = $res->fetchOneRow();
732 $page->assign('public_name', $res[0]);
733 $page->assign('private_name', $res[1]);
734 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
738 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: