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($page->pid());
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, $page->pid(), $page->owner
->id());
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
,
220 $page->profile
->isFemale(), $this->private_name_end
);
221 set_profile_display($display_names, $page->pid());
226 class ProfileSettingEdu
implements ProfileSetting
228 public function __construct() {
231 static function sortByGradYear($line1, $line2) {
232 $a = (int) $line1['grad_year'];
233 $b = (int) $line2['grad_year'];
237 return ($a < $b) ?
-1 : 1;
240 public function value(ProfilePage
&$page, $field, $value, &$success)
243 if (is_null($value) ||
!is_array($value)) {
245 $value = XDB
::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
246 FROM profile_education
247 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
252 foreach ($value as $key=>&$edu) {
253 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
254 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
255 $edu['error'] = true
;
264 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
269 public function save(ProfilePage
&$page, $field, $value)
271 XDB
::execute("DELETE FROM profile_education
272 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
274 foreach ($value as $eduid=>&$edu) {
275 if ($edu['eduid'] != '') {
276 XDB
::execute("INSERT INTO profile_education
277 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
278 fieldid = {?}, grad_year = {?}, program = {?}",
279 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
280 $edu['fieldid'], $edu['grad_year'], $edu['program']);
286 class ProfileSettingEmailDirectory
implements ProfileSetting
288 public function __construct(){}
289 public function save(ProfilePage
&$page, $field, $value){}
291 public function value(ProfilePage
&$page, $field, $value, &$success)
296 if (!is_null($value)) {
297 $email_stripped = strtolower(trim($value));
298 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
299 $p->assign('email_error', '1');
300 $p->assign('email_directory_error', $email_stripped);
301 $p->trigError('Adresse Email invalide');
304 $p->assign('email_error', '0');
311 class ProfileSettingNetworking
implements ProfileSetting
318 public function __construct()
320 $this->email
= new ProfileSettingEmail();
321 $this->pub
= new ProfileSettingPub();
322 $this->web
= new ProfileSettingWeb();
323 $this->number
= new ProfileSettingNumber();
326 public function value(ProfilePage
&$page, $field, $value, &$success)
328 if (is_null($value)) {
329 $value = XDB
::fetchAllAssoc("SELECT n.address, n.network_type AS type, n.pub, m.name
330 FROM profile_networking AS n
331 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
335 if (!is_array($value)) {
338 $filters = XDB
::fetchAllAssoc('type', 'SELECT filter, network_type AS type
339 FROM profile_networking_enum;');
341 foreach($value as $i=>&$network) {
342 if (!trim($network['address'])) {
345 if (!isset($network['pub'])) {
346 $network['pub'] = 'private';
348 $network['error'] = false
;
349 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
351 if ($filters[$network['type']] == 'web') {
352 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
353 } elseif ($filters[$network['type']] == 'email') {
354 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
355 } elseif ($filters[$network['type']] == 'number') {
356 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
360 $network['error'] = true
;
367 public function save(ProfilePage
&$page, $field, $value)
369 XDB
::execute("DELETE FROM profile_networking
372 if (!count($value)) {
376 foreach ($value as $id=>$network) {
377 XDB
::execute("INSERT INTO profile_networking (pid, nwid, network_type, address, pub)
378 VALUES ({?}, {?}, {?}, {?}, {?})",
379 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
384 class ProfileSettingPromo
implements ProfileSetting
386 public function __construct(){}
388 public function save(ProfilePage
&$page, $field, $value)
390 $gradYearNew = $value;
391 if ($page->profile
->mainEducation() == 'X') {
392 $gradYearNew +
= $page->profile
->mainEducationDuration();
394 if ($value == $page->profile
->entry_year +
$page->profile
->mainEducationDuration()) {
395 XDB
::execute('UPDATE profile_display
398 $page->profile
->mainEducation() . strval($value), $page->profile
->id());
399 XDB
::execute('UPDATE profile_education
401 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
402 $gradYearNew, $page->profile
->id());
403 $page->trigSuccess('Ton statut "orange" a été supprimé.');
405 require_once 'validations.inc.php';
407 $myorange = new OrangeReq(S
::user(), $gradYearNew);
409 Platal
::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
413 public function value(ProfilePage
&$page, $field, $value, &$success)
415 $entryYear = $page->profile
->entry_year
;
416 $gradYear = $page->profile
->grad_year
;
418 if (is_null($value) ||
$value == $page->profile
->yearpromo()) {
419 if ($gradYear != $entryYear +
$page->profile
->mainEducationDuration()) {
420 $promoChoice = array();
421 for ($i = $entryYear +
$page->profile
->mainEducationDuration(); $i <= $gradYear; ++
$i) {
422 $promoChoice[] = $page->profile
->mainEducation() . strval($i);
424 Platal
::page()->assign('promo_choice', $promoChoice);
426 if ($page->profile
->mainEducation() == 'X') {
427 return $page->profile
->grad_year
- $page->profile
->mainEducationDuration();
429 return $page->profile
->yearpromo();
432 // If this profile belongs to an X, $promoNew needs to be changed to
433 // the graduation year.
434 $gradYearNew = $value;
435 if ($page->profile
->mainEducation() == 'X') {
436 $gradYearNew +
= $page->profile
->mainEducationDuration();
439 if ($value < 1000 ||
$value > 9999) {
440 Platal
::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
442 } elseif ($gradYearNew < $entryYear +
$page->profile
->mainEducationDuration()) {
443 Platal
::page()->trigError('Trop tôt !');
446 return intval($value);
451 class ProfileSettingGeneral
extends ProfilePage
453 protected $pg_template = 'profile/general.tpl';
455 public function __construct(PlWizard
&$wiz)
457 parent
::__construct($wiz);
458 $this->settings
['search_names']
459 = new ProfileSettingSearchNames();
460 $this->settings
['birthdate'] = new ProfileSettingDate();
461 $this->settings
['freetext_pub']
462 = $this->settings
['photo_pub']
463 = new ProfileSettingPub();
464 $this->settings
['freetext']
465 = $this->settings
['nationality1']
466 = $this->settings
['nationality2']
467 = $this->settings
['nationality3']
468 = $this->settings
['yourself']
469 = $this->settings
['promo_display']
471 $this->settings
['email_directory']
472 = new ProfileSettingEmail();
473 $this->settings
['email_directory_new']
474 = new ProfileSettingEmailDirectory();
475 $this->settings
['networking'] = new ProfileSettingNetworking();
476 $this->settings
['tels'] = new ProfileSettingPhones('user', 0);
477 $this->settings
['edus'] = new ProfileSettingEdu();
478 $this->settings
['promo'] = new ProfileSettingPromo();
479 $this->watched
= array('freetext' => true
, 'tels' => true
,
480 'networking' => true
, 'edus' => true
,
481 'nationality1' => true
, 'nationality2' => true
,
482 'nationality3' => true
, 'search_names' => true
);
485 protected function _fetchData()
487 // Checkout all data...
488 $res = XDB
::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
489 pp.display_tel as mobile, pp.pub as mobile_pub,
490 p.email_directory as email_directory, pd.promo AS promo_display,
491 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
493 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
494 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
495 WHERE p.pid = {?}", $this->pid());
496 $this->values
= $res->fetchOneAssoc();
498 $this->values
['yourself'] = $this->owner
->displayName();
501 // Retreive photo informations
502 $res = XDB
::query("SELECT pub
504 WHERE pid = {?}", $this->pid());
505 $this->values
['photo_pub'] = $res->fetchOneCell();
508 $res = XDB
::query("SELECT COUNT(*)
510 WHERE type = 'photo' AND uid = {?}",
512 $this->values
['nouvellephoto'] = $res->fetchOneCell();
514 $this->values
['nouvellephoto'] = 0;
518 protected function _saveData()
520 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
521 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']
522 ||
$this->changed
['email_directory']) {
523 if ($this->values
['nationality3'] == "") {
524 $this->values
['nationality3'] = NULL
;
526 if ($this->values
['nationality2'] == "") {
527 $this->values
['nationality2'] = $this->values
['nationality3'];
528 $this->values
['nationality3'] = NULL
;
530 if ($this->values
['nationality1'] == "") {
531 $this->values
['nationality1'] = $this->values
['nationality2'];
532 $this->values
['nationality2'] = $this->values
['nationality3'];
533 $this->values
['nationality3'] = NULL
;
535 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
536 $this->values
['email_directory_new'] : $this->values
['email_directory'];
537 if ($new_email == "") {
541 XDB
::execute("UPDATE profiles
542 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
543 freetext = {?}, freetext_pub = {?}, email_directory = {?}
545 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
546 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
547 $this->values
['freetext'], $this->values
['freetext_pub'], $new_email, $this->pid());
549 if ($this->changed
['photo_pub']) {
550 XDB
::execute("UPDATE profile_photos
553 $this->values
['photo_pub'], $this->pid());
555 if ($this->changed
['yourself']) {
556 XDB
::execute('UPDATE accounts
557 SET display_name = {?}
558 WHERE pid = {?}', $this->pid());
560 if ($this->changed
['promo_display']) {
561 if ($this->values
['promo_display']{0} == $this->profile
->mainEducation()
562 && intval(substr($this->values
['promo_display'], 1, 4)) >= $this->profile
->entry_year +
$this->profile
->mainEducationDuration()) {
563 XDB
::execute('UPDATE profile_display
566 $this->values
['promo_display'], $this->pid());
571 public function _prepare(PlPage
&$page, $id)
573 require_once "education.func.inc.php";
575 $res = XDB
::query("SELECT id, field
576 FROM profile_education_field_enum
578 $page->assign('edu_fields', $res->fetchAllAssoc());
580 require_once "emails.combobox.inc.php";
581 fill_email_combobox($page, $this->owner
, $this->profile
);
583 $res = XDB
::query("SELECT nw.network_type AS type, nw.name
584 FROM profile_networking_enum AS nw
586 $page->assign('network_list', $res->fetchAllAssoc());
588 $res = XDB
::query("SELECT public_name, private_name
592 $res = $res->fetchOneRow();
593 $page->assign('public_name', $res[0]);
594 $page->assign('private_name', $res[1]);
595 $page->assign('isFemale', $this->profile
->isFemale() ?
1 : 0);
599 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: