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['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
264 Platal
::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
265 $edu['grad_year'] = null
;
266 $edu['warning'] = true
;
274 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
279 public function save(ProfilePage
&$page, $field, $value)
281 XDB
::execute("DELETE FROM profile_education
282 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
284 foreach ($value as $eduid=>&$edu) {
285 if ($edu['eduid'] != '') {
286 XDB
::execute("INSERT INTO profile_education
287 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
288 fieldid = {?}, grad_year = {?}, program = {?}",
289 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
290 $edu['fieldid'], $edu['grad_year'], $edu['program']);
296 class ProfileSettingEmailDirectory
implements ProfileSetting
298 public function __construct(){}
299 public function save(ProfilePage
&$page, $field, $value){}
301 public function value(ProfilePage
&$page, $field, $value, &$success)
306 if (!is_null($value)) {
307 $email_stripped = strtolower(trim($value));
308 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
309 $p->assign('email_error', '1');
310 $p->assign('email_directory_error', $email_stripped);
311 $p->trigError('Adresse Email invalide');
314 $p->assign('email_error', '0');
321 class ProfileSettingNetworking
implements ProfileSetting
328 public function __construct()
330 $this->email
= new ProfileSettingEmail();
331 $this->pub
= new ProfileSettingPub();
332 $this->web
= new ProfileSettingWeb();
333 $this->number
= new ProfileSettingNumber();
336 public function value(ProfilePage
&$page, $field, $value, &$success)
338 if (is_null($value)) {
339 $value = XDB
::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
340 FROM profile_networking AS n
344 if (!is_array($value)) {
347 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
348 FROM profile_networking_enum;');
350 foreach($value as $i=>&$network) {
351 if (!trim($network['address'])) {
354 if (!isset($network['pub'])) {
355 $network['pub'] = 'private';
357 $network['error'] = false
;
358 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
360 $network['name'] = $filters[$network['type']]['name'];
361 if ($filters[$network['type']]['filter'] == 'web') {
362 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
363 } elseif ($filters[$network['type']]['filter'] == 'email') {
364 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
365 } elseif ($filters[$network['type']]['filter'] == 'number') {
366 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
370 $network['error'] = true
;
377 public function save(ProfilePage
&$page, $field, $value)
379 XDB
::execute("DELETE FROM profile_networking
382 if (!count($value)) {
386 foreach ($value as $id=>$network) {
387 XDB
::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
388 VALUES ({?}, {?}, {?}, {?}, {?})",
389 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
394 class ProfileSettingPromo
implements ProfileSetting
396 public function __construct(){}
398 public function save(ProfilePage
&$page, $field, $value)
400 $gradYearNew = $value;
401 if ($page->profile
->mainEducation() == 'X') {
402 $gradYearNew +
= $page->profile
->mainEducationDuration();
404 if (($page->profile
->mainEducation() != 'X'
405 && $value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration())
406 ||
($page->profile
->mainEducation() == 'X' && $value == $page->profile
->entry_year
)) {
407 XDB
::execute('UPDATE profile_display
410 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
411 XDB
::execute('UPDATE profile_education
413 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
414 $gradYearNew, $page->profile
->id());
415 Platal
::page()->trigSuccess('Ton statut « orange » a été supprimé.');
417 require_once 'validations.inc.php';
419 $myorange = new OrangeReq(S
::user(), $page->profile
, $gradYearNew);
421 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
425 public function value(ProfilePage
&$page, $field, $value, &$success)
427 $entryYear = $page->profile
->entry_year
;
428 $gradYear = $page->profile
->grad_year
;
429 $yearpromo = $page->profile
->grad_year
;
430 if ($page->profile
->mainEducation() == 'X') {
431 $yearpromo -= $page->profile
->mainEducationDuration();
434 if (is_null($value) ||
$value == $yearpromo) {
435 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
436 $promoChoice = array();
437 for ($i = $entryYear; $i <= $gradYear - $page->profile
->mainEducationDuration(); ++
$i) {
438 if ($page->profile
->mainEducation() == 'X') {
439 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
441 $promoChoice[] = $page->profile
->mainEducation() . strval($i +
$page->profile
->mainEducationDuration());
444 Platal
::page()->assign('promo_choice', $promoChoice);
449 // If this profile belongs to an X, $promoNew needs to be changed to
450 // the graduation year.
451 $gradYearNew = $value;
452 if ($page->profile
->mainEducation() == 'X') {
453 $gradYearNew +
= $page->profile
->mainEducationDuration();
456 if ($value < 1000 ||
$value > 9999) {
457 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
459 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
460 Platal
::page()->trigError('Trop tôt !');
463 return intval($value);
468 class ProfileSettingGeneral
extends ProfilePage
470 protected $pg_template = 'profile/general.tpl';
472 public function __construct(PlWizard
&$wiz)
474 parent
::__construct($wiz);
475 $this->settings
['search_names']
476 = new ProfileSettingSearchNames();
477 $this->settings
['birthdate'] = new ProfileSettingDate();
478 $this->settings
['freetext_pub']
479 = $this->settings
['photo_pub']
480 = new ProfileSettingPub();
481 $this->settings
['freetext']
482 = $this->settings
['nationality1']
483 = $this->settings
['nationality2']
484 = $this->settings
['nationality3']
485 = $this->settings
['yourself']
486 = $this->settings
['promo_display']
488 $this->settings
['email_directory']
489 = new ProfileSettingEmail();
490 $this->settings
['email_directory_new']
491 = new ProfileSettingEmailDirectory();
492 $this->settings
['networking'] = new ProfileSettingNetworking();
493 $this->settings
['tels'] = new ProfileSettingPhones('user', 0);
494 $this->settings
['edus'] = new ProfileSettingEdu();
495 $this->settings
['promo'] = new ProfileSettingPromo();
496 $this->watched
= array('freetext' => true
, 'tels' => true
,
497 'networking' => true
, 'edus' => true
,
498 'nationality1' => true
, 'nationality2' => true
,
499 'nationality3' => true
, 'search_names' => true
);
502 protected function _fetchData()
504 // Checkout all data...
505 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
506 pp.display_tel as mobile, pp.pub as mobile_pub,
507 p.email_directory as email_directory, pd.promo AS promo_display,
508 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
510 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
511 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
512 WHERE p.pid = {?}", $this->pid());
513 $this->values
= $res->fetchOneAssoc();
515 $this->values
['yourself'] = $this->owner
->displayName();
518 // Retreive photo informations
519 $res = XDB
::query("SELECT pub
521 WHERE pid = {?}", $this->pid());
522 $this->values
['photo_pub'] = $res->fetchOneCell();
525 $res = XDB
::query("SELECT COUNT(*)
527 WHERE type = 'photo' AND pid = {?}",
529 $this->values
['nouvellephoto'] = $res->fetchOneCell();
531 $this->values
['nouvellephoto'] = 0;
535 protected function _saveData()
537 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
538 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
539 ||
$this->changed
['email_directory']) {
540 if ($this->values
['nationality3'] == "") {
541 $this->values
['nationality3'] = NULL
;
543 if ($this->values
['nationality2'] == "") {
544 $this->values
['nationality2'] = $this->values
['nationality3'];
545 $this->values
['nationality3'] = NULL
;
547 if ($this->values
['nationality1'] == "") {
548 $this->values
['nationality1'] = $this->values
['nationality2'];
549 $this->values
['nationality2'] = $this->values
['nationality3'];
550 $this->values
['nationality3'] = NULL
;
552 if ($this->values
['nationality1'] == $this->values
['nationality2']
553 && $this->values
['nationality2'] == $this->values
['nationality3']) {
554 $this->values
['nationality2'] = NULL
;
555 $this->values
['nationality3'] = NULL
;
556 } else if ($this->values
['nationality1'] == $this->values
['nationality2']) {
557 $this->values
['nationality2'] = $this->values
['nationality3'];
558 $this->values
['nationality3'] = NULL
;
559 } else if ($this->values
['nationality2'] == $this->values
['nationality3']
560 ||
$this->values
['nationality1'] == $this->values
['nationality3']) {
561 $this->values
['nationality3'] = NULL
;
564 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
565 $this->values
['email_directory_new'] : $this->values
['email_directory'];
566 if ($new_email == "") {
570 XDB
::execute("UPDATE profiles
571 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
572 freetext = {?}, freetext_pub = {?}, email_directory = {?}
574 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
575 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
576 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
578 if ($this->changed
['photo_pub']) {
579 XDB
::execute("UPDATE profile_photos
582 $this->values
['photo_pub'], $this->pid());
584 if ($this->changed
['yourself']) {
586 XDB
::execute('UPDATE accounts
587 SET display_name = {?}
589 $this->values
['yourself'], $this->owner
->id());
591 XDB
::execute('UPDATE profile_display
593 WHERE pid = {?}', $this->values
['yourself'],
596 if ($this->changed
['promo_display']) {
597 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()) {
598 if (($this->profile
->mainEducation() == 'X'
599 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year
)
600 ||
($this->profile
->mainEducation() != 'X'
601 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration())) {
602 XDB
::execute('UPDATE profile_display
605 $this->values
['promo_display'], $this->pid());
611 public function _prepare(PlPage
&$page, $id)
613 require_once "education.func.inc.php";
615 $res = XDB
::query("SELECT id, field
616 FROM profile_education_field_enum
618 $page->assign('edu_fields', $res->fetchAllAssoc());
620 require_once "emails.combobox.inc.php";
621 fill_email_combobox($page, $this->owner
, $this->profile
);
623 $res = XDB
::query("SELECT nw.nwid AS type, nw.name
624 FROM profile_networking_enum AS nw
626 $page->assign('network_list', $res->fetchAllAssoc());
628 $res = XDB
::query("SELECT public_name, private_name
632 $res = $res->fetchOneRow();
633 $page->assign('public_name', $res[0]);
634 $page->assign('private_name', $res[1]);
635 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
639 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: