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 $value = $this->clean($value);
131 require_once 'name.func.inc.php';
133 $value = $this->clean($value);
134 $res = XDB
::query("SELECT s.particle, s.name
135 FROM profile_name AS s
136 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
137 WHERE s.pid = {?} AND (e.type = 'lastname' OR e.type = 'firstname')
138 ORDER BY e.type = 'firstname'",
140 $res = $res->fetchAllAssoc();
142 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
143 $initial['firstname'] = $res[1]['name'];
144 $sn_types = build_types();
145 $this->search_names
= array();
146 foreach ($value as &$sn) {
147 $sn['name'] = trim($sn['name']);
148 if ($sn['type'] == 'firstname' ||
$sn['type'] == 'lastname') {
149 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
150 $initial[$sn['type']], $success_tmp);
151 $success = $success && $success_tmp;
154 if (isset($sn['particle']) && ($sn['particle'] != '')) {
155 // particle is before first blank
156 list($particle, $name) = explode(' ', $sn['name'], 2);
157 $particle = trim($particle) . ' ';
159 // particle is before first quote
160 list($particle, $name) = explode('\'', $sn['name'], 2);
161 $particle = trim($particle);
163 // actually there is no particle
173 if ($sn['name'] != '') {
175 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
177 'particle' => $particle,
178 'pub' => $sn['pub']);
180 if (isset($this->search_names
[$sn['typeid']])) {
181 $this->search_names
[$sn['typeid']][] = $sn['name'];
183 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
185 $sn['type_name'] = $sn_types[$sn['typeid']];
189 $res = XDB
::query("SELECT public_name, private_name
193 list($public_name, $private_name) = $res->fetchOneRow();
195 $sn_types_private = build_types('private');
196 $this->private_name_end
= build_private_name($this->search_names
, $sn_types_private);
197 $private_name = $public_name . $this->private_name_end
;
199 Platal
::page()->assign('public_name', $public_name);
200 Platal
::page()->assign('private_name', $private_name);
206 public function save(ProfilePage
&$page, $field, $value)
208 require_once 'name.func.inc.php';
209 require_once 'validations.inc.php';
211 $sn_old = build_sn_pub($page->pid());
212 XDB
::execute("DELETE FROM s
213 USING profile_name AS s
214 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
215 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
217 $has_new = set_alias_names($this->search_names
, $sn_old, $page->pid(), $page->owner
->id());
219 // Only requires validation if modification in public names
221 $new_names = new NamesReq(S
::user(), $page->profile
, $this->search_names
, $this->private_name_end
);
222 $new_names->submit();
223 Platal
::page()->trigWarning('La demande de modification de tes noms a bien été prise en compte.' .
224 ' Tu recevras un email dès que ces changements auront été effectués.');
226 $display_names = array();
227 build_display_names($display_names, $this->search_names
,
228 $page->profile
->isFemale(), $this->private_name_end
);
229 set_profile_display($display_names, $page->pid());
234 class ProfileSettingEdu
implements ProfileSetting
236 public function __construct() {
239 static function sortByGradYear($line1, $line2) {
240 $a = (isset($line1['grad_year'])) ?
(int) $line1['grad_year'] : 0;
241 $b = (isset($line2['grad_year'])) ?
(int) $line2['grad_year'] : 0;
245 return ($a < $b) ?
-1 : 1;
248 public function value(ProfilePage
&$page, $field, $value, &$success)
251 if (is_null($value)) {
253 $value = XDB
::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
254 FROM profile_education
255 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
258 } else if (!is_array($value)) {
262 foreach ($value as $key=>&$edu) {
263 if ($edu['eduid'] < 1 ||
!isset($edu['degreeid']) ||
$edu['degreeid'] < 1) {
264 Platal
::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
267 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
268 Platal
::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
269 $edu['grad_year'] = null
;
270 $edu['warning'] = true
;
278 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
283 public function save(ProfilePage
&$page, $field, $value)
285 XDB
::execute("DELETE FROM profile_education
286 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
288 foreach ($value as $eduid=>&$edu) {
289 if ($edu['eduid'] != '') {
290 XDB
::execute("INSERT INTO profile_education
291 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
292 fieldid = {?}, grad_year = {?}, program = {?}",
293 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
294 $edu['fieldid'], $edu['grad_year'], $edu['program']);
300 class ProfileSettingEmailDirectory
implements ProfileSetting
302 public function __construct(){}
303 public function save(ProfilePage
&$page, $field, $value){}
305 public function value(ProfilePage
&$page, $field, $value, &$success)
310 if (!is_null($value)) {
311 $email_stripped = strtolower(trim($value));
312 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
313 $p->assign('email_error', '1');
314 $p->assign('email_directory_error', $email_stripped);
315 $p->trigError('Adresse Email invalide');
318 $p->assign('email_error', '0');
325 class ProfileSettingNetworking
implements ProfileSetting
332 public function __construct()
334 $this->email
= new ProfileSettingEmail();
335 $this->pub
= new ProfileSettingPub();
336 $this->web
= new ProfileSettingWeb();
337 $this->number
= new ProfileSettingNumber();
340 public function value(ProfilePage
&$page, $field, $value, &$success)
342 if (is_null($value)) {
343 $value = XDB
::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
344 FROM profile_networking AS n
348 if (!is_array($value)) {
351 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
352 FROM profile_networking_enum;');
354 foreach($value as $i=>&$network) {
355 if (!trim($network['address'])) {
358 if (!isset($network['pub'])) {
359 $network['pub'] = 'private';
361 $network['error'] = false
;
362 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
364 $network['name'] = $filters[$network['type']]['name'];
365 if ($filters[$network['type']]['filter'] == 'web') {
366 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
367 } elseif ($filters[$network['type']]['filter'] == 'email') {
368 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
369 } elseif ($filters[$network['type']]['filter'] == 'number') {
370 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
374 $network['error'] = true
;
381 public function save(ProfilePage
&$page, $field, $value)
383 XDB
::execute("DELETE FROM profile_networking
386 if (!count($value)) {
390 foreach ($value as $id=>$network) {
391 XDB
::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
392 VALUES ({?}, {?}, {?}, {?}, {?})",
393 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
398 class ProfileSettingPromo
implements ProfileSetting
400 public function __construct(){}
402 public function save(ProfilePage
&$page, $field, $value)
404 $gradYearNew = $value;
405 if ($page->profile
->mainEducation() == 'X') {
406 $gradYearNew +
= $page->profile
->mainEducationDuration();
408 if (($page->profile
->mainEducation() != 'X'
409 && $value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration())
410 ||
($page->profile
->mainEducation() == 'X' && $value == $page->profile
->entry_year
)) {
411 XDB
::execute('UPDATE profile_display
414 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
415 XDB
::execute('UPDATE profile_education
417 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
418 $gradYearNew, $page->profile
->id());
419 Platal
::page()->trigSuccess('Ton statut « orange » a été supprimé.');
421 require_once 'validations.inc.php';
423 $myorange = new OrangeReq(S
::user(), $page->profile
, $gradYearNew);
425 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
429 public function value(ProfilePage
&$page, $field, $value, &$success)
431 $entryYear = $page->profile
->entry_year
;
432 $gradYear = $page->profile
->grad_year
;
433 $yearpromo = $page->profile
->grad_year
;
434 if ($page->profile
->mainEducation() == 'X') {
435 $yearpromo -= $page->profile
->mainEducationDuration();
438 if (is_null($value) ||
$value == $yearpromo) {
439 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
440 $promoChoice = array();
441 for ($i = $entryYear; $i <= $gradYear - $page->profile
->mainEducationDuration(); ++
$i) {
442 if ($page->profile
->mainEducation() == 'X') {
443 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
445 $promoChoice[] = $page->profile
->mainEducation() . strval($i +
$page->profile
->mainEducationDuration());
448 Platal
::page()->assign('promo_choice', $promoChoice);
453 // If this profile belongs to an X, $promoNew needs to be changed to
454 // the graduation year.
455 $gradYearNew = $value;
456 if ($page->profile
->mainEducation() == 'X') {
457 $gradYearNew +
= $page->profile
->mainEducationDuration();
460 if ($value < 1000 ||
$value > 9999) {
461 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
463 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
464 Platal
::page()->trigError('Trop tôt !');
467 return intval($value);
472 class ProfileSettingGeneral
extends ProfilePage
474 protected $pg_template = 'profile/general.tpl';
476 public function __construct(PlWizard
&$wiz)
478 parent
::__construct($wiz);
479 $this->settings
['search_names']
480 = new ProfileSettingSearchNames();
481 $this->settings
['birthdate'] = new ProfileSettingDate();
482 $this->settings
['freetext_pub']
483 = $this->settings
['photo_pub']
484 = new ProfileSettingPub();
485 $this->settings
['freetext']
486 = $this->settings
['nationality1']
487 = $this->settings
['nationality2']
488 = $this->settings
['nationality3']
489 = $this->settings
['yourself']
490 = $this->settings
['promo_display']
492 $this->settings
['email_directory']
493 = new ProfileSettingEmail();
494 $this->settings
['email_directory_new']
495 = new ProfileSettingEmailDirectory();
496 $this->settings
['networking'] = new ProfileSettingNetworking();
497 $this->settings
['tels'] = new ProfileSettingPhones('user', 0);
498 $this->settings
['edus'] = new ProfileSettingEdu();
499 $this->settings
['promo'] = new ProfileSettingPromo();
500 $this->watched
= array('freetext' => true
, 'tels' => true
,
501 'networking' => true
, 'edus' => true
,
502 'nationality1' => true
, 'nationality2' => true
,
503 'nationality3' => true
, 'search_names' => true
);
506 protected function _fetchData()
508 // Checkout all data...
509 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
510 pp.display_tel as mobile, pp.pub as mobile_pub,
511 p.email_directory as email_directory, pd.promo AS promo_display,
512 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
514 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
515 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
516 WHERE p.pid = {?}", $this->pid());
517 $this->values
= $res->fetchOneAssoc();
519 $this->values
['yourself'] = $this->owner
->displayName();
522 // Retreive photo informations
523 $res = XDB
::query("SELECT pub
525 WHERE pid = {?}", $this->pid());
526 $this->values
['photo_pub'] = $res->fetchOneCell();
529 $res = XDB
::query("SELECT COUNT(*)
531 WHERE type = 'photo' AND pid = {?}",
533 $this->values
['nouvellephoto'] = $res->fetchOneCell();
535 $this->values
['nouvellephoto'] = 0;
539 protected function _saveData()
541 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
542 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
543 ||
$this->changed
['email_directory']) {
544 if ($this->values
['nationality3'] == "") {
545 $this->values
['nationality3'] = NULL
;
547 if ($this->values
['nationality2'] == "") {
548 $this->values
['nationality2'] = $this->values
['nationality3'];
549 $this->values
['nationality3'] = NULL
;
551 if ($this->values
['nationality1'] == "") {
552 $this->values
['nationality1'] = $this->values
['nationality2'];
553 $this->values
['nationality2'] = $this->values
['nationality3'];
554 $this->values
['nationality3'] = NULL
;
556 if ($this->values
['nationality1'] == $this->values
['nationality2']
557 && $this->values
['nationality2'] == $this->values
['nationality3']) {
558 $this->values
['nationality2'] = NULL
;
559 $this->values
['nationality3'] = NULL
;
560 } else if ($this->values
['nationality1'] == $this->values
['nationality2']) {
561 $this->values
['nationality2'] = $this->values
['nationality3'];
562 $this->values
['nationality3'] = NULL
;
563 } else if ($this->values
['nationality2'] == $this->values
['nationality3']
564 ||
$this->values
['nationality1'] == $this->values
['nationality3']) {
565 $this->values
['nationality3'] = NULL
;
568 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
569 $this->values
['email_directory_new'] : $this->values
['email_directory'];
570 if ($new_email == "") {
574 XDB
::execute("UPDATE profiles
575 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
576 freetext = {?}, freetext_pub = {?}, email_directory = {?}
578 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
579 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
580 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
582 if ($this->changed
['photo_pub']) {
583 XDB
::execute("UPDATE profile_photos
586 $this->values
['photo_pub'], $this->pid());
588 if ($this->changed
['yourself']) {
590 XDB
::execute('UPDATE accounts
591 SET display_name = {?}
593 $this->values
['yourself'], $this->owner
->id());
595 XDB
::execute('UPDATE profile_display
597 WHERE pid = {?}', $this->values
['yourself'],
600 if ($this->changed
['promo_display']) {
601 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()) {
602 if (($this->profile
->mainEducation() == 'X'
603 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year
)
604 ||
($this->profile
->mainEducation() != 'X'
605 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration())) {
606 XDB
::execute('UPDATE profile_display
609 $this->values
['promo_display'], $this->pid());
615 public function _prepare(PlPage
&$page, $id)
617 require_once "education.func.inc.php";
619 $res = XDB
::query("SELECT id, field
620 FROM profile_education_field_enum
622 $page->assign('edu_fields', $res->fetchAllAssoc());
624 require_once "emails.combobox.inc.php";
625 fill_email_combobox($page, $this->owner
, $this->profile
);
627 $res = XDB
::query("SELECT nw.nwid AS type, nw.name
628 FROM profile_networking_enum AS nw
630 $page->assign('network_list', $res->fetchAllAssoc());
632 $res = XDB
::query("SELECT public_name, private_name
636 $res = $res->fetchOneRow();
637 $page->assign('public_name', $res[0]);
638 $page->assign('private_name', $res[1]);
639 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
643 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: