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 require_once 'name.func.inc.php';
24 class ProfileSettingSearchNames
implements ProfileSetting
26 private $private_name_end;
27 private $search_names;
30 public function __construct() {
31 $this->name_types
= DirEnum
::getOptions(DirEnum
::NAMES
);
34 private function matchWord($old, $new, $newLen)
36 return ($i = strpos($old, $new)) !== false
37 && ($i == 0 ||
$old{$i-1} == ' ')
38 && ($i +
$newLen == strlen($old) ||
$old{$i +
$newLen} == ' ');
41 private function prepareField($value)
43 return name_to_basename($value);
46 private function prepare(ProfilePage
&$page, $field, $value, $init, &$success)
49 $ini = $this->prepareField($init);
50 $new = $this->prepareField($value);
51 $newLen = strlen($new);
52 $success = $this->matchWord($ini, $new, $newLen)
53 ||
($field == 'lastname' && $new == 'DE ' . $ini);
55 $field = strtolower($field);
56 Platal
::page()->trigError("Le " . $this->name_types
[$field] . " que tu as choisi (" . $value .
57 ") est trop loin de ton " . $this->name_types
[$field] . " initial (" . $init . ").");
59 return $success ?
$value : $init;
62 /* Removes duplicated entries for the fields that do not allow them. */
63 private function clean($value)
65 $single_types = XDB
::fetchAllAssoc('id',
67 FROM profile_name_enum
68 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
70 foreach ($value as $key => $item) {
71 if (isset($single_types[$item['typeid']])) {
72 if ($single_types[$item['typeid']] === true
) {
75 $single_types[$item['typeid']] = true
;
83 public function value(ProfilePage
&$page, $field, $value, &$success)
88 if (is_null($value)) {
89 $sn_all = XDB
::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
90 sn.particle, sn.typeid, e.type, e.name AS type_name,
91 FIND_IN_SET('has_particle', e.flags) AS has_particle,
92 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
93 FIND_IN_SET('public', e.flags) AS pub
94 FROM profile_name AS sn
95 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
96 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
97 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
100 $sn_types = XDB
::iterator("SELECT id, type, name,
101 FIND_IN_SET('has_particle', flags) AS has_particle
102 FROM profile_name_enum
103 WHERE NOT FIND_IN_SET('not_displayed', flags)
104 AND FIND_IN_SET('always_displayed', flags)
108 $sn = $sn_all->next();
109 while ($sn_type = $sn_types->next()) {
110 if ($sn_type['id'] == $sn['typeid']) {
113 $sn = $sn_all->next();
116 $value[] = array('name' => '',
118 'typeid' => $sn_type['id'],
119 'type' => $sn_type['type'],
120 'type_name' => $sn_type['name'],
121 'has_particle' => $sn_type['has_particle'],
122 'always_displayed' => 1,
129 } while ($sn = $sn_all->next());
131 $value = $this->clean($value);
133 require_once 'name.func.inc.php';
135 $value = $this->clean($value);
136 $res = XDB
::query("SELECT s.particle, s.name
137 FROM profile_name AS s
138 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
139 WHERE s.pid = {?} AND e.type LIKE '%ini'
140 ORDER BY e.type = 'firstname_ini'",
142 $res = $res->fetchAllAssoc();
144 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
145 $initial['firstname'] = $res[1]['name'];
146 $sn_types = build_types();
147 $this->search_names
= array();
148 foreach ($value as &$sn) {
149 $sn['name'] = trim($sn['name']);
150 if ($sn['type'] == 'firstname' ||
$sn['type'] == 'lastname') {
151 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
152 $initial[$sn['type']], $success_tmp);
153 $success = $success && $success_tmp;
156 if (isset($sn['particle']) && ($sn['particle'] != '')) {
157 // particle is before first blank
158 list($particle, $name) = explode(' ', $sn['name'], 2);
159 $particle = trim($particle) . ' ';
161 // particle is before first quote
162 list($particle, $name) = explode('\'', $sn['name'], 2);
163 $particle = trim($particle);
165 // actually there is no particle
175 if ($sn['name'] != '') {
177 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
179 'particle' => $particle,
180 'pub' => $sn['pub']);
182 if (isset($this->search_names
[$sn['typeid']])) {
183 $this->search_names
[$sn['typeid']][] = $sn['name'];
185 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
187 $sn['type_name'] = $sn_types[$sn['typeid']];
191 $res = XDB
::query("SELECT public_name, private_name
195 list($public_name, $private_name) = $res->fetchOneRow();
197 $sn_types_private = build_types('private');
198 $this->private_name_end
= build_private_name($this->search_names
, $sn_types_private);
199 $private_name = $public_name . $this->private_name_end
;
201 Platal
::page()->assign('public_name', $public_name);
202 Platal
::page()->assign('private_name', $private_name);
208 public function save(ProfilePage
&$page, $field, $value)
210 require_once 'name.func.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(), $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 = (int) $line1['grad_year'];
241 $b = (int) $line2['grad_year'];
245 return ($a < $b) ?
-1 : 1;
248 public function value(ProfilePage
&$page, $field, $value, &$success)
251 if (is_null($value) ||
!is_array($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)
260 foreach ($value as $key=>&$edu) {
261 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
262 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
263 $edu['error'] = true
;
272 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
277 public function save(ProfilePage
&$page, $field, $value)
279 XDB
::execute("DELETE FROM profile_education
280 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
282 foreach ($value as $eduid=>&$edu) {
283 if ($edu['eduid'] != '') {
284 XDB
::execute("INSERT INTO profile_education
285 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
286 fieldid = {?}, grad_year = {?}, program = {?}",
287 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
288 $edu['fieldid'], $edu['grad_year'], $edu['program']);
294 class ProfileSettingEmailDirectory
implements ProfileSetting
296 public function __construct(){}
297 public function save(ProfilePage
&$page, $field, $value){}
299 public function value(ProfilePage
&$page, $field, $value, &$success)
304 if (!is_null($value)) {
305 $email_stripped = strtolower(trim($value));
306 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
307 $p->assign('email_error', '1');
308 $p->assign('email_directory_error', $email_stripped);
309 $p->trigError('Adresse Email invalide');
312 $p->assign('email_error', '0');
319 class ProfileSettingNetworking
implements ProfileSetting
326 public function __construct()
328 $this->email
= new ProfileSettingEmail();
329 $this->pub
= new ProfileSettingPub();
330 $this->web
= new ProfileSettingWeb();
331 $this->number
= new ProfileSettingNumber();
334 public function value(ProfilePage
&$page, $field, $value, &$success)
336 if (is_null($value)) {
337 $value = XDB
::fetchAllAssoc("SELECT n.address, n.network_type AS type, n.pub, m.name
338 FROM profile_networking AS n
339 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
343 if (!is_array($value)) {
346 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, network_type AS type
347 FROM profile_networking_enum;');
349 foreach($value as $i=>&$network) {
350 if (!trim($network['address'])) {
353 if (!isset($network['pub'])) {
354 $network['pub'] = 'private';
356 $network['error'] = false
;
357 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
359 if ($filters[$network['type']] == 'web') {
360 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
361 } elseif ($filters[$network['type']] == 'email') {
362 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
363 } elseif ($filters[$network['type']] == 'number') {
364 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
368 $network['error'] = true
;
375 public function save(ProfilePage
&$page, $field, $value)
377 XDB
::execute("DELETE FROM profile_networking
380 if (!count($value)) {
384 foreach ($value as $id=>$network) {
385 XDB
::execute("INSERT INTO profile_networking (pid, nwid, network_type, address, pub)
386 VALUES ({?}, {?}, {?}, {?}, {?})",
387 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
392 class ProfileSettingPromo
implements ProfileSetting
394 public function __construct(){}
396 public function save(ProfilePage
&$page, $field, $value)
398 $gradYearNew = $value;
399 if ($page->profile
->mainEducation() == 'X') {
400 $gradYearNew +
= $page->profile
->mainEducationDuration();
402 if ($value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration()) {
403 XDB
::execute('UPDATE profile_display
406 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
407 XDB
::execute('UPDATE profile_education
409 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
410 $gradYearNew, $page->profile
->id());
411 $page->trigSuccess('Ton statut "orange" a été supprimé.');
413 require_once 'validations.inc.php';
415 $myorange = new OrangeReq(S
::user(), $gradYearNew);
417 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
421 public function value(ProfilePage
&$page, $field, $value, &$success)
423 $entryYear = $page->profile
->entry_year
;
424 $gradYear = $page->profile
->grad_year
;
426 if (is_null($value) ||
$value == $page->profile
->yearpromo()) {
427 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
428 $promoChoice = array();
429 for ($i = $entryYear +
$page->profile
->mainEducationDuration(); $i <= $gradYear; ++
$i) {
430 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
432 Platal
::page()->assign('promo_choice', $promoChoice);
434 if ($page->profile
->mainEducation() == 'X') {
435 return $page->profile
->grad_year
- $page->profile
->mainEducationDuration();
437 return $page->profile
->yearpromo();
440 // If this profile belongs to an X, $promoNew needs to be changed to
441 // the graduation year.
442 $gradYearNew = $value;
443 if ($page->profile
->mainEducation() == 'X') {
444 $gradYearNew +
= $page->profile
->mainEducationDuration();
447 if ($value < 1000 ||
$value > 9999) {
448 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
450 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
451 Platal
::page()->trigError('Trop tôt !');
454 return intval($value);
459 class ProfileSettingGeneral
extends ProfilePage
461 protected $pg_template = 'profile/general.tpl';
463 public function __construct(PlWizard
&$wiz)
465 parent
::__construct($wiz);
466 $this->settings
['search_names']
467 = new ProfileSettingSearchNames();
468 $this->settings
['birthdate'] = new ProfileSettingDate();
469 $this->settings
['freetext_pub']
470 = $this->settings
['photo_pub']
471 = new ProfileSettingPub();
472 $this->settings
['freetext']
473 = $this->settings
['nationality1']
474 = $this->settings
['nationality2']
475 = $this->settings
['nationality3']
476 = $this->settings
['yourself']
477 = $this->settings
['promo_display']
479 $this->settings
['email_directory']
480 = new ProfileSettingEmail();
481 $this->settings
['email_directory_new']
482 = new ProfileSettingEmailDirectory();
483 $this->settings
['networking'] = new ProfileSettingNetworking();
484 $this->settings
['tels'] = new ProfileSettingPhones('user', 0);
485 $this->settings
['edus'] = new ProfileSettingEdu();
486 $this->settings
['promo'] = new ProfileSettingPromo();
487 $this->watched
= array('freetext' => true
, 'tels' => true
,
488 'networking' => true
, 'edus' => true
,
489 'nationality1' => true
, 'nationality2' => true
,
490 'nationality3' => true
, 'search_names' => true
);
493 protected function _fetchData()
495 // Checkout all data...
496 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
497 pp.display_tel as mobile, pp.pub as mobile_pub,
498 p.email_directory as email_directory, pd.promo AS promo_display,
499 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
501 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
502 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
503 WHERE p.pid = {?}", $this->pid());
504 $this->values
= $res->fetchOneAssoc();
506 $this->values
['yourself'] = $this->owner
->displayName();
509 // Retreive photo informations
510 $res = XDB
::query("SELECT pub
512 WHERE pid = {?}", $this->pid());
513 $this->values
['photo_pub'] = $res->fetchOneCell();
516 $res = XDB
::query("SELECT COUNT(*)
518 WHERE type = 'photo' AND uid = {?}",
520 $this->values
['nouvellephoto'] = $res->fetchOneCell();
522 $this->values
['nouvellephoto'] = 0;
526 protected function _saveData()
528 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
529 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
530 ||
$this->changed
['email_directory']) {
531 if ($this->values
['nationality3'] == "") {
532 $this->values
['nationality3'] = NULL
;
534 if ($this->values
['nationality2'] == "") {
535 $this->values
['nationality2'] = $this->values
['nationality3'];
536 $this->values
['nationality3'] = NULL
;
538 if ($this->values
['nationality1'] == "") {
539 $this->values
['nationality1'] = $this->values
['nationality2'];
540 $this->values
['nationality2'] = $this->values
['nationality3'];
541 $this->values
['nationality3'] = NULL
;
543 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
544 $this->values
['email_directory_new'] : $this->values
['email_directory'];
545 if ($new_email == "") {
549 XDB
::execute("UPDATE profiles
550 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
551 freetext = {?}, freetext_pub = {?}, email_directory = {?}
553 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
554 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
555 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
557 if ($this->changed
['photo_pub']) {
558 XDB
::execute("UPDATE profile_photos
561 $this->values
['photo_pub'], $this->pid());
563 if ($this->changed
['yourself']) {
564 XDB
::execute('UPDATE accounts
565 SET display_name = {?}
567 $this->values
['yourself'], $this->owner
->id());
569 if ($this->changed
['promo_display']) {
570 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()
571 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration()) {
572 XDB
::execute('UPDATE profile_display
575 $this->values
['promo_display'], $this->pid());
580 public function _prepare(PlPage
&$page, $id)
582 require_once "education.func.inc.php";
584 $res = XDB
::query("SELECT id, field
585 FROM profile_education_field_enum
587 $page->assign('edu_fields', $res->fetchAllAssoc());
589 require_once "emails.combobox.inc.php";
590 fill_email_combobox($page, $this->owner
, $this->profile
);
592 $res = XDB
::query("SELECT nw.network_type AS type, nw.name
593 FROM profile_networking_enum AS nw
595 $page->assign('network_list', $res->fetchAllAssoc());
597 $res = XDB
::query("SELECT public_name, private_name
601 $res = $res->fetchOneRow();
602 $page->assign('public_name', $res[0]);
603 $page->assign('private_name', $res[1]);
604 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
608 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: