2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 require_once 'validations.inc.php';
130 $namesRequest = ProfileValidate
::get_typed_requests($page->pid(), 'usage');
131 if (count($namesRequest) > 0) {
132 Platal
::page()->assign('validation', true
);
134 $value = $this->clean($value);
136 require_once 'name.func.inc.php';
138 $value = $this->clean($value);
139 $res = XDB
::query("SELECT s.particle, s.name
140 FROM profile_name AS s
141 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
142 WHERE s.pid = {?} AND (e.type = 'lastname' OR e.type = 'firstname')
143 ORDER BY e.type = 'firstname'",
145 $res = $res->fetchAllAssoc();
147 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
148 $initial['firstname'] = $res[1]['name'];
149 $sn_types = build_types();
150 $this->search_names
= array();
151 foreach ($value as &$sn) {
152 $sn['name'] = trim($sn['name']);
153 if ($sn['type'] == 'firstname' ||
$sn['type'] == 'lastname') {
154 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
155 $initial[$sn['type']], $success_tmp);
156 $success = $success && $success_tmp;
159 if (isset($sn['particle']) && ($sn['particle'] != '')) {
160 // particle is before first blank
161 list($particle, $name) = explode(' ', $sn['name'], 2);
162 $particle = trim($particle) . ' ';
164 // particle is before first quote
165 list($particle, $name) = explode('\'', $sn['name'], 2);
166 $particle = trim($particle);
168 // actually there is no particle
178 if ($sn['name'] != '') {
180 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
182 'particle' => $particle,
183 'pub' => $sn['pub']);
185 if (isset($this->search_names
[$sn['typeid']])) {
186 $this->search_names
[$sn['typeid']][] = $sn['name'];
188 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
190 $sn['type_name'] = $sn_types[$sn['typeid']];
194 $res = XDB
::query("SELECT public_name, private_name
198 list($public_name, $private_name) = $res->fetchOneRow();
200 $sn_types_private = build_types('private');
201 $this->private_name_end
= build_private_name($this->search_names
, $sn_types_private);
202 $private_name = $public_name . $this->private_name_end
;
204 Platal
::page()->assign('public_name', $public_name);
205 Platal
::page()->assign('private_name', $private_name);
211 public function save(ProfilePage
&$page, $field, $value)
213 require_once 'name.func.inc.php';
214 require_once 'validations.inc.php';
216 $sn_old = build_sn_pub($page->pid());
217 XDB
::execute("DELETE FROM s
218 USING profile_name AS s
219 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
220 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
222 $has_new = set_alias_names($this->search_names
, $sn_old, $page->pid(), $page->owner
->id());
224 // Only requires validation if modification in public names
226 $new_names = new NamesReq(S
::user(), $page->profile
, $this->search_names
, $this->private_name_end
);
227 $new_names->submit();
228 Platal
::page()->trigWarning('La demande de modification de tes noms a bien été prise en compte.' .
229 ' Tu recevras un email dès que ces changements auront été effectués.');
231 $display_names = array();
232 build_display_names($display_names, $this->search_names
,
233 $page->profile
->isFemale(), $this->private_name_end
);
234 set_profile_display($display_names, $page->profile
);
238 public function getText($value) {
240 foreach ($value as $name) {
241 if ($name['name'] != '') {
242 $names[] = $name['type_name'] . ' : ' . $name['name'];
245 return implode(', ' , $names);
249 class ProfileSettingEdu
implements ProfileSetting
251 public function __construct() {
254 static function sortByGradYear($line1, $line2) {
255 $a = (isset($line1['grad_year'])) ?
(int) $line1['grad_year'] : 0;
256 $b = (isset($line2['grad_year'])) ?
(int) $line2['grad_year'] : 0;
260 return ($a < $b) ?
-1 : 1;
263 public function value(ProfilePage
&$page, $field, $value, &$success)
266 if (is_null($value)) {
268 $value = XDB
::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
269 FROM profile_education
270 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
273 } else if (!is_array($value)) {
277 foreach ($value as $key=>&$edu) {
278 if ($edu['eduid'] < 1 ||
!isset($edu['degreeid']) ||
$edu['degreeid'] < 1) {
279 Platal
::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
282 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
283 Platal
::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
284 $edu['grad_year'] = null
;
285 $edu['warning'] = true
;
293 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
298 public function save(ProfilePage
&$page, $field, $value)
300 XDB
::execute("DELETE FROM profile_education
301 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
303 foreach ($value as $eduid=>&$edu) {
304 if ($edu['eduid'] != '') {
305 $fieldId = ($edu['fieldid'] == 0) ? null
: $edu['fieldid'];
306 XDB
::execute("INSERT INTO profile_education
307 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
308 fieldid = {?}, grad_year = {?}, program = {?}",
309 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
310 $fieldId, $edu['grad_year'], $edu['program']);
315 public function getText($value) {
316 $schoolsList = DirEnum
::getOptions(DirEnum
::EDUSCHOOLS
);
317 $degreesList = DirEnum
::getOptions(DirEnum
::EDUDEGREES
);
318 $fieldsList = DirEnum
::getOptions(DirEnum
::EDUFIELDS
);
319 $educations = array();
320 foreach ($value as $education) {
321 $educations[] = 'Université : ' . $schoolsList[$education['eduid']]
322 . ', diplôme : ' . $degreesList[$education['degreeid']]
323 . ', domaine : ' . $fieldsList[$education['fieldid']]
324 . ', année d\'obtention : ' . $education['grad_year']
325 . ', intitulé : ' . $education['program'];
327 return implode(', ', $educations);
331 class ProfileSettingEmailDirectory
implements ProfileSetting
333 public function __construct(){}
334 public function save(ProfilePage
&$page, $field, $value){}
336 public function value(ProfilePage
&$page, $field, $value, &$success)
341 if (!is_null($value)) {
342 $email_stripped = strtolower(trim($value));
343 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
344 $p->assign('email_error', '1');
345 $p->assign('email_directory_error', $email_stripped);
346 $p->trigError('Adresse Email invalide');
349 $p->assign('email_error', '0');
355 public function getText($value) {
360 class ProfileSettingNetworking
implements ProfileSetting
367 public function __construct()
369 $this->email
= new ProfileSettingEmail();
370 $this->pub
= new ProfileSettingPub();
371 $this->web
= new ProfileSettingWeb();
372 $this->number
= new ProfileSettingNumber();
375 public function value(ProfilePage
&$page, $field, $value, &$success)
377 if (is_null($value)) {
378 $value = XDB
::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
379 FROM profile_networking AS n
383 if (!is_array($value)) {
386 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
387 FROM profile_networking_enum;');
389 foreach($value as $i=>&$network) {
390 if (!trim($network['address'])) {
393 if (!isset($network['pub'])) {
394 $network['pub'] = 'private';
396 $network['error'] = false
;
397 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
399 $network['name'] = $filters[$network['type']]['name'];
400 if ($filters[$network['type']]['filter'] == 'web') {
401 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
402 } elseif ($filters[$network['type']]['filter'] == 'email') {
403 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
404 } elseif ($filters[$network['type']]['filter'] == 'number') {
405 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
409 $network['error'] = true
;
416 public function save(ProfilePage
&$page, $field, $value)
418 XDB
::execute("DELETE FROM profile_networking
421 if (!count($value)) {
425 foreach ($value as $id=>$network) {
426 XDB
::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
427 VALUES ({?}, {?}, {?}, {?}, {?})",
428 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
432 public function getText($value) {
433 $networkings = array();
434 foreach ($value as $network) {
435 $networkings[] = 'nom : ' . $network['name'] . ', adresse : ' . $network['address']
436 . ', affichage : ' . $network['pub'];
438 return implode(' ; ' , $networkings);
442 class ProfileSettingPromo
implements ProfileSetting
444 public function __construct(){}
446 public function save(ProfilePage
&$page, $field, $value)
448 $gradYearNew = $value;
449 if ($page->profile
->mainEducation() == 'X') {
450 $gradYearNew +
= $page->profile
->mainEducationDuration();
452 if (($page->profile
->mainEducation() != 'X'
453 && $value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration())
454 ||
($page->profile
->mainEducation() == 'X' && $value == $page->profile
->entry_year
)) {
455 XDB
::execute('UPDATE profile_display
458 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
459 XDB
::execute('UPDATE profile_education
461 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
462 $gradYearNew, $page->profile
->id());
463 Platal
::page()->trigSuccess('Ton statut « orange » a été supprimé.');
465 require_once 'validations.inc.php';
467 $myorange = new OrangeReq(S
::user(), $page->profile
, $gradYearNew);
469 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
473 public function value(ProfilePage
&$page, $field, $value, &$success)
475 $entryYear = $page->profile
->entry_year
;
476 $gradYear = $page->profile
->grad_year
;
477 $yearpromo = $page->profile
->grad_year
;
478 if ($page->profile
->mainEducation() == 'X') {
479 $yearpromo -= $page->profile
->mainEducationDuration();
482 if (is_null($value) ||
$value == $yearpromo) {
483 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
484 $promoChoice = array();
485 for ($i = $entryYear; $i <= $gradYear - $page->profile
->mainEducationDuration(); ++
$i) {
486 if ($page->profile
->mainEducation() == 'X') {
487 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
489 $promoChoice[] = $page->profile
->mainEducation() . strval($i +
$page->profile
->mainEducationDuration());
492 Platal
::page()->assign('promo_choice', $promoChoice);
497 // If this profile belongs to an X, $promoNew needs to be changed to
498 // the graduation year.
499 $gradYearNew = $value;
500 if ($page->profile
->mainEducation() == 'X') {
501 $gradYearNew +
= $page->profile
->mainEducationDuration();
504 if ($value < 1000 ||
$value > 9999) {
505 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
507 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
508 Platal
::page()->trigError('Trop tôt !');
511 return intval($value);
514 public function getText($value) {
520 class ProfileSettingGeneral
extends ProfilePage
522 protected $pg_template = 'profile/general.tpl';
524 public function __construct(PlWizard
&$wiz)
526 parent
::__construct($wiz);
527 $this->settings
['search_names']
528 = new ProfileSettingSearchNames();
529 $this->settings
['birthdate'] = new ProfileSettingDate();
530 $this->settings
['freetext_pub']
531 = $this->settings
['photo_pub']
532 = new ProfileSettingPub();
533 $this->settings
['freetext']
534 = $this->settings
['nationality1']
535 = $this->settings
['nationality2']
536 = $this->settings
['nationality3']
537 = $this->settings
['yourself']
538 = $this->settings
['promo_display']
540 $this->settings
['email_directory']
541 = new ProfileSettingEmail();
542 $this->settings
['email_directory_new']
543 = new ProfileSettingEmailDirectory();
544 $this->settings
['networking'] = new ProfileSettingNetworking();
545 $this->settings
['tels'] = new ProfileSettingPhones();
546 $this->settings
['edus'] = new ProfileSettingEdu();
547 $this->settings
['promo'] = new ProfileSettingPromo();
548 $this->watched
= array('freetext' => true
, 'tels' => true
,
549 'networking' => true
, 'edus' => true
,
550 'nationality1' => true
, 'nationality2' => true
,
551 'nationality3' => true
, 'search_names' => true
);
554 protected function _fetchData()
556 // Checkout all data...
557 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
558 p.email_directory as email_directory, pd.promo AS promo_display,
559 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
561 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
562 WHERE p.pid = {?}", $this->pid());
563 $this->values
= $res->fetchOneAssoc();
565 // Retreive photo informations
566 $res = XDB
::query("SELECT pub
568 WHERE pid = {?}", $this->pid());
569 $this->values
['photo_pub'] = $res->fetchOneCell();
572 $res = XDB
::query("SELECT COUNT(*)
574 WHERE type = 'photo' AND pid = {?}",
576 $this->values
['nouvellephoto'] = $res->fetchOneCell();
578 $this->values
['nouvellephoto'] = 0;
582 protected function _saveData()
584 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
585 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
586 ||
$this->changed
['email_directory']) {
587 if ($this->values
['nationality3'] == "") {
588 $this->values
['nationality3'] = NULL
;
590 if ($this->values
['nationality2'] == "") {
591 $this->values
['nationality2'] = $this->values
['nationality3'];
592 $this->values
['nationality3'] = NULL
;
594 if ($this->values
['nationality1'] == "") {
595 $this->values
['nationality1'] = $this->values
['nationality2'];
596 $this->values
['nationality2'] = $this->values
['nationality3'];
597 $this->values
['nationality3'] = NULL
;
599 if ($this->values
['nationality1'] == $this->values
['nationality2']
600 && $this->values
['nationality2'] == $this->values
['nationality3']) {
601 $this->values
['nationality2'] = NULL
;
602 $this->values
['nationality3'] = NULL
;
603 } else if ($this->values
['nationality1'] == $this->values
['nationality2']) {
604 $this->values
['nationality2'] = $this->values
['nationality3'];
605 $this->values
['nationality3'] = NULL
;
606 } else if ($this->values
['nationality2'] == $this->values
['nationality3']
607 ||
$this->values
['nationality1'] == $this->values
['nationality3']) {
608 $this->values
['nationality3'] = NULL
;
611 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
612 $this->values
['email_directory_new'] : $this->values
['email_directory'];
613 if ($new_email == "") {
617 XDB
::execute("UPDATE profiles
618 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
619 freetext = {?}, freetext_pub = {?}, email_directory = {?}
621 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
622 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
623 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
625 if ($this->changed
['photo_pub']) {
626 XDB
::execute("UPDATE profile_photos
629 $this->values
['photo_pub'], $this->pid());
631 if ($this->changed
['yourself']) {
633 XDB
::execute('UPDATE accounts
634 SET display_name = {?}
636 $this->values
['yourself'], $this->owner
->id());
638 XDB
::execute('UPDATE profile_display
640 WHERE pid = {?}', $this->values
['yourself'],
643 if ($this->changed
['promo_display']) {
644 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()) {
645 if (($this->profile
->mainEducation() == 'X'
646 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year
)
647 ||
($this->profile
->mainEducation() != 'X'
648 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration())) {
649 XDB
::execute('UPDATE profile_display
652 $this->values
['promo_display'], $this->pid());
658 public function _prepare(PlPage
&$page, $id)
660 require_once "education.func.inc.php";
662 $res = XDB
::query("SELECT id, field
663 FROM profile_education_field_enum
665 $page->assign('edu_fields', $res->fetchAllAssoc());
667 require_once "emails.combobox.inc.php";
668 fill_email_combobox($page, $this->owner
);
670 $res = XDB
::query("SELECT nw.nwid AS type, nw.name
671 FROM profile_networking_enum AS nw
673 $page->assign('network_list', $res->fetchAllAssoc());
675 $res = XDB
::query("SELECT public_name, private_name
679 $res = $res->fetchOneRow();
680 $page->assign('public_name', $res[0]);
681 $page->assign('private_name', $res[1]);
682 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
686 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: