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 $value = mb_strtoupper($value);
42 return preg_replace('/[^A-Z]/', ' ', $value);
45 private function prepare(ProfilePage
&$page, $field, $value, $init, &$success)
48 $ini = $this->prepareField($init);
49 $new = $this->prepareField($value);
50 $newLen = strlen($new);
51 $success = $this->matchWord($ini, $new, $newLen)
52 ||
($field == 'lastname' && $new == 'DE ' . $ini);
54 $field = strtolower($field);
55 Platal
::page()->trigError("Le " . $this->name_types
[$field] . " que tu as choisi (" . $value .
56 ") est trop loin de ton " . $this->name_types
[$field] . " initial (" . $init . ").");
58 return $success ?
$value : $init;
61 /* Removes duplicated entries for the fields that do not allow them. */
62 private function clean($value)
64 $single_types = XDB
::fetchAllAssoc('id',
66 FROM profile_name_enum
67 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
69 foreach ($value as $key => $item) {
70 if (isset($single_types[$item['typeid']])) {
71 if ($single_types[$item['typeid']] === true
) {
74 $single_types[$item['typeid']] = true
;
82 public function value(ProfilePage
&$page, $field, $value, &$success)
87 if (is_null($value)) {
88 $sn_all = XDB
::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
89 sn.particle, sn.typeid, e.type, e.name AS type_name,
90 FIND_IN_SET('has_particle', e.flags) AS has_particle,
91 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
92 FIND_IN_SET('public', e.flags) AS pub
93 FROM profile_name AS sn
94 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
95 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
96 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
99 $sn_types = XDB
::iterator("SELECT id, type, name,
100 FIND_IN_SET('has_particle', flags) AS has_particle
101 FROM profile_name_enum
102 WHERE NOT FIND_IN_SET('not_displayed', flags)
103 AND FIND_IN_SET('always_displayed', flags)
107 $sn = $sn_all->next();
108 while ($sn_type = $sn_types->next()) {
109 if ($sn_type['id'] == $sn['typeid']) {
112 $sn = $sn_all->next();
115 $value[] = array('name' => '',
117 'typeid' => $sn_type['id'],
118 'type' => $sn_type['type'],
119 'type_name' => $sn_type['name'],
120 'has_particle' => $sn_type['has_particle'],
121 'always_displayed' => 1,
128 } while ($sn = $sn_all->next());
130 $value = $this->clean($value);
132 require_once 'name.func.inc.php';
134 $value = $this->clean($value);
135 $res = XDB
::query("SELECT s.particle, s.name
136 FROM profile_name AS s
137 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
138 WHERE s.pid = {?} AND e.type LIKE '%ini'
139 ORDER BY e.type = 'firstname_ini'",
141 $res = $res->fetchAllAssoc();
143 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
144 $initial['firstname'] = $res[1]['name'];
145 $sn_types = build_types();
146 $this->search_names
= array();
147 foreach ($value as &$sn) {
148 $sn['name'] = trim($sn['name']);
149 if ($sn['type'] == 'firstname' ||
$sn['type'] == 'lastname') {
150 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
151 $initial[$sn['type']], $success_tmp);
152 $success = $success && $success_tmp;
155 if (isset($sn['particle']) && ($sn['particle'] != '')) {
156 list($particle, $name) = explode(' ', $sn['name'], 2);
157 $particle = trim($particle) . ' ';
159 list($particle, $name) = explode('\'', $sn['name'], 2);
160 $particle = trim($particle);
167 if ($sn['name'] != '') {
169 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
171 'particle' => $particle,
172 'pub' => $sn['pub']);
174 if (isset($this->search_names
[$sn['typeid']])) {
175 $this->search_names
[$sn['typeid']][] = $sn['name'];
177 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
179 $sn['type_name'] = $sn_types[$sn['typeid']];
183 $res = XDB
::query("SELECT public_name, private_name
187 list($public_name, $private_name) = $res->fetchOneRow();
189 $sn_types_private = build_types('private');
190 $this->private_name_end
= build_private_name($this->search_names
, $sn_types_private);
191 $private_name = $public_name . $this->private_name_end
;
193 Platal
::page()->assign('public_name', $public_name);
194 Platal
::page()->assign('private_name', $private_name);
200 public function save(ProfilePage
&$page, $field, $value)
202 require_once 'name.func.inc.php';
203 $sn_old = build_sn_pub();
204 XDB
::execute("DELETE FROM s
205 USING profile_name AS s
206 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
207 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
209 $has_new = set_alias_names($this->search_names
, $sn_old);
211 // Only requires validation if modification in public names
213 $new_names = new NamesReq(S
::user(), $this->search_names
, $this->private_name_end
);
214 $new_names->submit();
215 Platal
::page()->trigWarning('La demande de modification de tes noms a bien été prise en compte.' .
216 ' Tu recevras un email dès que ces changements auront été effectués.');
218 $display_names = array();
219 build_display_names($display_names, $this->search_names
, $this->private_name_end
);
220 set_profile_display($display_names);
225 class ProfileSettingEdu
implements ProfileSetting
227 public function __construct() {
230 static function sortByGradYear($line1, $line2) {
231 $a = (int) $line1['grad_year'];
232 $b = (int) $line2['grad_year'];
236 return ($a < $b) ?
-1 : 1;
239 public function value(ProfilePage
&$page, $field, $value, &$success)
242 if (is_null($value) ||
!is_array($value)) {
244 $value = XDB
::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
245 FROM profile_education
246 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
251 foreach ($value as $key=>&$edu) {
252 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
253 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
254 $edu['error'] = true
;
263 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
268 public function save(ProfilePage
&$page, $field, $value)
270 XDB
::execute("DELETE FROM profile_education
271 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
273 foreach ($value as $eduid=>&$edu) {
274 if ($edu['eduid'] != '') {
275 XDB
::execute("INSERT INTO profile_education
276 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
277 fieldid = {?}, grad_year = {?}, program = {?}",
278 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
279 $edu['fieldid'], $edu['grad_year'], $edu['program']);
285 class ProfileSettingEmailDirectory
implements ProfileSetting
287 public function __construct(){}
288 public function save(ProfilePage
&$page, $field, $value){}
290 public function value(ProfilePage
&$page, $field, $value, &$success)
295 if (!is_null($value)) {
296 $email_stripped = strtolower(trim($value));
297 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
298 $p->assign('email_error', '1');
299 $p->assign('email_directory_error', $email_stripped);
300 $p->trigError('Adresse Email invalide');
303 $p->assign('email_error', '0');
310 class ProfileSettingNetworking
implements ProfileSetting
317 public function __construct()
319 $this->email
= new ProfileSettingEmail();
320 $this->pub
= new ProfileSettingPub();
321 $this->web
= new ProfileSettingWeb();
322 $this->number
= new ProfileSettingNumber();
325 public function value(ProfilePage
&$page, $field, $value, &$success)
327 if (is_null($value)) {
328 $value = XDB
::fetchAllAssoc("SELECT n.address, n.network_type AS type, n.pub, m.name
329 FROM profile_networking AS n
330 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
334 if (!is_array($value)) {
337 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, network_type AS type
338 FROM profile_networking_enum;');
340 foreach($value as $i=>&$network) {
341 if (!trim($network['address'])) {
344 if (!isset($network['pub'])) {
345 $network['pub'] = 'private';
347 $network['error'] = false
;
348 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
350 if ($filters[$network['type']] == 'web') {
351 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
352 } elseif ($filters[$network['type']] == 'email') {
353 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
354 } elseif ($filters[$network['type']] == 'number') {
355 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
359 $network['error'] = true
;
366 public function save(ProfilePage
&$page, $field, $value)
368 XDB
::execute("DELETE FROM profile_networking
371 if (!count($value)) {
375 foreach ($value as $id=>$network) {
376 XDB
::execute("INSERT INTO profile_networking (pid, nwid, network_type, address, pub)
377 VALUES ({?}, {?}, {?}, {?}, {?})",
378 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
383 class ProfileSettingPromo
implements ProfileSetting
385 public function __construct(){}
387 public function save(ProfilePage
&$page, $field, $value)
389 $gradYearNew = $value;
390 if ($page->profile
->mainEducation() == 'X') {
391 $gradYearNew +
= $page->profile
->mainEducationDuration();
393 if ($value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration()) {
394 XDB
::execute('UPDATE profile_display
397 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
398 XDB
::execute('UPDATE profile_education
400 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
401 $gradYearNew, $page->profile
->id());
402 $page->trigSuccess('Ton statut "orange" a été supprimé.');
404 require_once 'validations.inc.php';
406 $myorange = new OrangeReq(S
::user(), $gradYearNew);
408 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
412 public function value(ProfilePage
&$page, $field, $value, &$success)
414 $entryYear = $page->profile
->entry_year
;
415 $gradYear = $page->profile
->grad_year
;
417 if (is_null($value) ||
$value == $page->profile
->yearpromo()) {
418 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
419 $promoChoice = array();
420 for ($i = $entryYear +
$page->profile
->mainEducationDuration(); $i <= $gradYear; ++
$i) {
421 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
423 Platal
::page()->assign('promo_choice', $promoChoice);
425 if ($page->profile
->mainEducation() == 'X') {
426 return $page->profile
->grad_year
- $page->profile
->mainEducationDuration();
428 return $page->profile
->yearpromo();
431 // If this profile belongs to an X, $promoNew needs to be changed to
432 // the graduation year.
433 $gradYearNew = $value;
434 if ($page->profile
->mainEducation() == 'X') {
435 $gradYearNew +
= $page->profile
->mainEducationDuration();
438 if ($value < 1000 ||
$value > 9999) {
439 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
441 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
442 Platal
::page()->trigError('Trop tôt !');
445 return intval($value);
450 class ProfileSettingGeneral
extends ProfilePage
452 protected $pg_template = 'profile/general.tpl';
454 public function __construct(PlWizard
&$wiz)
456 parent
::__construct($wiz);
457 $this->settings
['search_names']
458 = new ProfileSettingSearchNames();
459 $this->settings
['birthdate'] = new ProfileSettingDate();
460 $this->settings
['freetext_pub']
461 = $this->settings
['photo_pub']
462 = new ProfileSettingPub();
463 $this->settings
['freetext']
464 = $this->settings
['nationality1']
465 = $this->settings
['nationality2']
466 = $this->settings
['nationality3']
467 = $this->settings
['yourself']
468 = $this->settings
['promo_display']
470 $this->settings
['email_directory']
471 = new ProfileSettingEmail();
472 $this->settings
['email_directory_new']
473 = new ProfileSettingEmailDirectory();
474 $this->settings
['networking'] = new ProfileSettingNetworking();
475 $this->settings
['tels'] = new ProfileSettingPhones('user', 0);
476 $this->settings
['edus'] = new ProfileSettingEdu();
477 $this->settings
['promo'] = new ProfileSettingPromo();
478 $this->watched
= array('freetext' => true
, 'tels' => true
,
479 'networking' => true
, 'edus' => true
,
480 'nationality1' => true
, 'nationality2' => true
,
481 'nationality3' => true
, 'search_names' => true
);
484 protected function _fetchData()
486 // Checkout all data...
487 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
488 pp.display_tel as mobile, pp.pub as mobile_pub,
489 p.email_directory as email_directory, pd.promo AS promo_display,
490 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
492 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
493 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
494 WHERE p.pid = {?}", $this->pid());
495 $this->values
= $res->fetchOneAssoc();
497 $this->values
['yourself'] = $this->owner
->displayName();
500 // Retreive photo informations
501 $res = XDB
::query("SELECT pub
503 WHERE pid = {?}", $this->pid());
504 $this->values
['photo_pub'] = $res->fetchOneCell();
507 $res = XDB
::query("SELECT COUNT(*)
509 WHERE type = 'photo' AND uid = {?}",
511 $this->values
['nouvellephoto'] = $res->fetchOneCell();
513 $this->values
['nouvellephoto'] = 0;
517 protected function _saveData()
519 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
520 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
521 ||
$this->changed
['email_directory']) {
522 if ($this->values
['nationality3'] == "") {
523 $this->values
['nationality3'] = NULL
;
525 if ($this->values
['nationality2'] == "") {
526 $this->values
['nationality2'] = $this->values
['nationality3'];
527 $this->values
['nationality3'] = NULL
;
529 if ($this->values
['nationality1'] == "") {
530 $this->values
['nationality1'] = $this->values
['nationality2'];
531 $this->values
['nationality2'] = $this->values
['nationality3'];
532 $this->values
['nationality3'] = NULL
;
534 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
535 $this->values
['email_directory_new'] : $this->values
['email_directory'];
536 if ($new_email == "") {
540 XDB
::execute("UPDATE profiles
541 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
542 freetext = {?}, freetext_pub = {?}, email_directory = {?}
544 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
545 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
546 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
548 if ($this->changed
['photo_pub']) {
549 XDB
::execute("UPDATE profile_photos
552 $this->values
['photo_pub'], $this->pid());
554 if ($this->changed
['yourself']) {
555 XDB
::execute('UPDATE accounts
556 SET display_name = {?}
557 WHERE pid = {?}', $this->pid());
559 if ($this->changed
['promo_display']) {
560 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()
561 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration()) {
562 XDB
::execute('UPDATE profile_display
565 $this->values
['promo_display'], $this->pid());
570 public function _prepare(PlPage
&$page, $id)
572 require_once "education.func.inc.php";
574 $res = XDB
::query("SELECT id, field
575 FROM profile_education_field_enum
577 $page->assign('edu_fields', $res->fetchAllAssoc());
579 require_once "emails.combobox.inc.php";
580 fill_email_combobox($page, $this->owner
, $this->profile
);
582 $res = XDB
::query("SELECT nw.network_type AS type, nw.name
583 FROM profile_networking_enum AS nw
585 $page->assign('network_list', $res->fetchAllAssoc());
587 $res = XDB
::query("SELECT public_name, private_name
591 $res = $res->fetchOneRow();
592 $page->assign('public_name', $res[0]);
593 $page->assign('private_name', $res[1]);
597 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: