Allows empty grad years (Closes #1155).
[platal.git] / modules / profile / general.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once 'name.func.inc.php';
23
24 class ProfileSettingSearchNames implements ProfileSetting
25 {
26 private $private_name_end;
27 private $search_names;
28 private $name_types;
29
30 public function __construct() {
31 $this->name_types = DirEnum::getOptions(DirEnum::NAMES);
32 }
33
34 private function matchWord($old, $new, $newLen)
35 {
36 return ($i = strpos($old, $new)) !== false
37 && ($i == 0 || $old{$i-1} == ' ')
38 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
39 }
40
41 private function prepareField($value)
42 {
43 return name_to_basename($value);
44 }
45
46 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
47 {
48 $success = true;
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);
54 if (!$success) {
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 . ").");
58 }
59 return $success ? $value : $init;
60 }
61
62 /* Removes duplicated entries for the fields that do not allow them. */
63 private function clean($value)
64 {
65 $single_types = XDB::fetchAllAssoc('id',
66 'SELECT id, 0
67 FROM profile_name_enum
68 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
69
70 foreach ($value as $key => $item) {
71 if (isset($single_types[$item['typeid']])) {
72 if ($single_types[$item['typeid']] === true) {
73 unset($value[$key]);
74 } else {
75 $single_types[$item['typeid']] = true;
76 }
77 }
78 }
79
80 return $value;
81 }
82
83 public function value(ProfilePage &$page, $field, $value, &$success)
84 {
85 $success = true;
86 $success_tmp = true;
87
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",
98 $page->pid());
99
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)
105 ORDER BY id");
106
107 $value = array();
108 $sn = $sn_all->next();
109 while ($sn_type = $sn_types->next()) {
110 if ($sn_type['id'] == $sn['typeid']) {
111 $value[] = $sn;
112 if ($sn) {
113 $sn = $sn_all->next();
114 }
115 } else {
116 $value[] = array('name' => '',
117 'particle' => '',
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,
123 'pub' => 1);
124 }
125 }
126 if ($sn) {
127 do {
128 $value[] = $sn;
129 } while ($sn = $sn_all->next());
130 }
131 $value = $this->clean($value);
132 } else {
133 require_once 'name.func.inc.php';
134
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 = 'lastname' OR e.type = 'firstname')
140 ORDER BY e.type = 'firstname'",
141 $page->pid());
142 $res = $res->fetchAllAssoc();
143 $initial = array();
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;
154 }
155 if ($sn['pub']) {
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) . ' ';
160 if (!$name) {
161 // particle is before first quote
162 list($particle, $name) = explode('\'', $sn['name'], 2);
163 $particle = trim($particle);
164 if (!$name) {
165 // actually there is no particle
166 $particle = '';
167 $name = $sn['name'];
168 }
169 }
170 } else {
171 $particle = '';
172 $name = $sn['name'];
173 }
174 }
175 if ($sn['name'] != '') {
176 if ($sn['pub']) {
177 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
178 'name' => $name,
179 'particle' => $particle,
180 'pub' => $sn['pub']);
181 } else {
182 if (isset($this->search_names[$sn['typeid']])) {
183 $this->search_names[$sn['typeid']][] = $sn['name'];
184 } else {
185 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']);
186 }
187 $sn['type_name'] = $sn_types[$sn['typeid']];
188 }
189 }
190 }
191 $res = XDB::query("SELECT public_name, private_name
192 FROM profile_display
193 WHERE pid = {?}",
194 S::v('uid'));
195 list($public_name, $private_name) = $res->fetchOneRow();
196 if ($success) {
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;
200 }
201 Platal::page()->assign('public_name', $public_name);
202 Platal::page()->assign('private_name', $private_name);
203 }
204
205 return $value;
206 }
207
208 public function save(ProfilePage &$page, $field, $value)
209 {
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)",
216 $page->pid());
217 $has_new = set_alias_names($this->search_names, $sn_old, $page->pid(), $page->owner->id());
218
219 // Only requires validation if modification in public names
220 if ($has_new) {
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.');
225 } else {
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());
230 }
231 }
232 }
233
234 class ProfileSettingEdu implements ProfileSetting
235 {
236 public function __construct() {
237 }
238
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;
242 if ($a == $b) {
243 return 0;
244 }
245 return ($a < $b) ? -1 : 1;
246 }
247
248 public function value(ProfilePage &$page, $field, $value, &$success)
249 {
250 $success = true;
251 if (is_null($value) || !is_array($value)) {
252 $value = array();
253 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
254 FROM profile_education
255 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
256 ORDER BY id",
257 $page->pid());
258 } else {
259 $i = 0;
260 foreach ($value as $key=>&$edu) {
261 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
262 Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
263 $edu['warning'] = true;
264 }
265 if ($key != $i) {
266 $value[$i] = $edu;
267 unset($value[$key]);
268 }
269 $i++;
270 }
271 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
272 }
273 return $value;
274 }
275
276 public function save(ProfilePage &$page, $field, $value)
277 {
278 XDB::execute("DELETE FROM profile_education
279 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
280 $page->pid());
281 foreach ($value as $eduid=>&$edu) {
282 if ($edu['eduid'] != '') {
283 XDB::execute("INSERT INTO profile_education
284 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
285 fieldid = {?}, grad_year = {?}, program = {?}",
286 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
287 $edu['fieldid'], $edu['grad_year'], $edu['program']);
288 }
289 }
290 }
291 }
292
293 class ProfileSettingEmailDirectory implements ProfileSetting
294 {
295 public function __construct(){}
296 public function save(ProfilePage &$page, $field, $value){}
297
298 public function value(ProfilePage &$page, $field, $value, &$success)
299 {
300 $p = Platal::page();
301
302 $success = true;
303 if (!is_null($value)) {
304 $email_stripped = strtolower(trim($value));
305 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
306 $p->assign('email_error', '1');
307 $p->assign('email_directory_error', $email_stripped);
308 $p->trigError('Adresse Email invalide');
309 $success = false;
310 } else {
311 $p->assign('email_error', '0');
312 }
313 }
314 return $value;
315 }
316 }
317
318 class ProfileSettingNetworking implements ProfileSetting
319 {
320 private $email;
321 private $pub;
322 private $web;
323 private $number;
324
325 public function __construct()
326 {
327 $this->email = new ProfileSettingEmail();
328 $this->pub = new ProfileSettingPub();
329 $this->web = new ProfileSettingWeb();
330 $this->number = new ProfileSettingNumber();
331 }
332
333 public function value(ProfilePage &$page, $field, $value, &$success)
334 {
335 if (is_null($value)) {
336 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
337 FROM profile_networking AS n
338 WHERE n.pid = {?}",
339 $page->pid());
340 }
341 if (!is_array($value)) {
342 $value = array();
343 }
344 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
345 FROM profile_networking_enum;');
346 $success = true;
347 foreach($value as $i=>&$network) {
348 if (!trim($network['address'])) {
349 unset($value[$i]);
350 } else {
351 if (!isset($network['pub'])) {
352 $network['pub'] = 'private';
353 }
354 $network['error'] = false;
355 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
356 $s = true;
357 $network['name'] = $filters[$network['type']]['name'];
358 if ($filters[$network['type']]['filter'] == 'web') {
359 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
360 } elseif ($filters[$network['type']]['filter'] == 'email') {
361 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
362 } elseif ($filters[$network['type']]['filter'] == 'number') {
363 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
364 }
365 if (!$s) {
366 $success = false;
367 $network['error'] = true;
368 }
369 }
370 }
371 return $value;
372 }
373
374 public function save(ProfilePage &$page, $field, $value)
375 {
376 XDB::execute("DELETE FROM profile_networking
377 WHERE pid = {?}",
378 $page->pid());
379 if (!count($value)) {
380 return;
381 }
382 $insert = array();
383 foreach ($value as $id=>$network) {
384 XDB::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
385 VALUES ({?}, {?}, {?}, {?}, {?})",
386 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
387 }
388 }
389 }
390
391 class ProfileSettingPromo implements ProfileSetting
392 {
393 public function __construct(){}
394
395 public function save(ProfilePage &$page, $field, $value)
396 {
397 $gradYearNew = $value;
398 if ($page->profile->mainEducation() == 'X') {
399 $gradYearNew += $page->profile->mainEducationDuration();
400 }
401 if (($page->profile->mainEducation() != 'X'
402 && $value == $page->profile->entry_year + $page->profile->mainEducationDuration())
403 || ($page->profile->mainEducation() == 'X' && $value == $page->profile->entry_year)) {
404 XDB::execute('UPDATE profile_display
405 SET promo = {?}
406 WHERE pid = {?}',
407 $page->profile->mainEducation() . strval($value), $page->profile->id());
408 XDB::execute('UPDATE profile_education
409 SET grad_year = {?}
410 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
411 $gradYearNew, $page->profile->id());
412 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
413 } else {
414 require_once 'validations.inc.php';
415
416 $myorange = new OrangeReq(S::user(), $gradYearNew);
417 $myorange->submit();
418 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
419 }
420 }
421
422 public function value(ProfilePage &$page, $field, $value, &$success)
423 {
424 $entryYear = $page->profile->entry_year;
425 $gradYear = $page->profile->grad_year;
426 $yearpromo = $page->profile->grad_year;
427 if ($page->profile->mainEducation() == 'X') {
428 $yearpromo -= $page->profile->mainEducationDuration();
429 }
430 $success = true;
431 if (is_null($value) || $value == $yearpromo) {
432 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
433 $promoChoice = array();
434 for ($i = $entryYear; $i <= $gradYear - $page->profile->mainEducationDuration(); ++$i) {
435 if ($page->profile->mainEducation() == 'X') {
436 $promoChoice[] = $page->profile->mainEducation() . strval($i);
437 } else {
438 $promoChoice[] = $page->profile->mainEducation() . strval($i + $page->profile->mainEducationDuration());
439 }
440 }
441 Platal::page()->assign('promo_choice', $promoChoice);
442 }
443 return $yearpromo;
444 }
445
446 // If this profile belongs to an X, $promoNew needs to be changed to
447 // the graduation year.
448 $gradYearNew = $value;
449 if ($page->profile->mainEducation() == 'X') {
450 $gradYearNew += $page->profile->mainEducationDuration();
451 }
452
453 if ($value < 1000 || $value > 9999) {
454 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
455 $success = false;
456 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
457 Platal::page()->trigError('Trop tôt&nbsp;!');
458 $success = false;
459 }
460 return intval($value);
461 }
462 }
463
464
465 class ProfileSettingGeneral extends ProfilePage
466 {
467 protected $pg_template = 'profile/general.tpl';
468
469 public function __construct(PlWizard &$wiz)
470 {
471 parent::__construct($wiz);
472 $this->settings['search_names']
473 = new ProfileSettingSearchNames();
474 $this->settings['birthdate'] = new ProfileSettingDate();
475 $this->settings['freetext_pub']
476 = $this->settings['photo_pub']
477 = new ProfileSettingPub();
478 $this->settings['freetext']
479 = $this->settings['nationality1']
480 = $this->settings['nationality2']
481 = $this->settings['nationality3']
482 = $this->settings['yourself']
483 = $this->settings['promo_display']
484 = null;
485 $this->settings['email_directory']
486 = new ProfileSettingEmail();
487 $this->settings['email_directory_new']
488 = new ProfileSettingEmailDirectory();
489 $this->settings['networking'] = new ProfileSettingNetworking();
490 $this->settings['tels'] = new ProfileSettingPhones('user', 0);
491 $this->settings['edus'] = new ProfileSettingEdu();
492 $this->settings['promo'] = new ProfileSettingPromo();
493 $this->watched= array('freetext' => true, 'tels' => true,
494 'networking' => true, 'edus' => true,
495 'nationality1' => true, 'nationality2' => true,
496 'nationality3' => true, 'search_names' => true);
497 }
498
499 protected function _fetchData()
500 {
501 // Checkout all data...
502 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
503 pp.display_tel as mobile, pp.pub as mobile_pub,
504 p.email_directory as email_directory, pd.promo AS promo_display,
505 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
506 FROM profiles AS p
507 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
508 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
509 WHERE p.pid = {?}", $this->pid());
510 $this->values = $res->fetchOneAssoc();
511 if ($this->owner) {
512 $this->values['yourself'] = $this->owner->displayName();
513 }
514
515 // Retreive photo informations
516 $res = XDB::query("SELECT pub
517 FROM profile_photos
518 WHERE pid = {?}", $this->pid());
519 $this->values['photo_pub'] = $res->fetchOneCell();
520
521 if ($this->owner) {
522 $res = XDB::query("SELECT COUNT(*)
523 FROM requests
524 WHERE type = 'photo' AND uid = {?}",
525 $this->owner->id());
526 $this->values['nouvellephoto'] = $res->fetchOneCell();
527 } else {
528 $this->values['nouvellephoto'] = 0;
529 }
530 }
531
532 protected function _saveData()
533 {
534 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
535 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
536 || $this->changed['email_directory']) {
537 if ($this->values['nationality3'] == "") {
538 $this->values['nationality3'] = NULL;
539 }
540 if ($this->values['nationality2'] == "") {
541 $this->values['nationality2'] = $this->values['nationality3'];
542 $this->values['nationality3'] = NULL;
543 }
544 if ($this->values['nationality1'] == "") {
545 $this->values['nationality1'] = $this->values['nationality2'];
546 $this->values['nationality2'] = $this->values['nationality3'];
547 $this->values['nationality3'] = NULL;
548 }
549 $new_email = ($this->values['email_directory'] == "new@example.org") ?
550 $this->values['email_directory_new'] : $this->values['email_directory'];
551 if ($new_email == "") {
552 $new_email = NULL;
553 }
554
555 XDB::execute("UPDATE profiles
556 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
557 freetext = {?}, freetext_pub = {?}, email_directory = {?}
558 WHERE pid = {?}",
559 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
560 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['birthdate']),
561 $this->values['freetext'], $this->values['freetext_pub'], $new_email, $this->pid());
562 }
563 if ($this->changed['photo_pub']) {
564 XDB::execute("UPDATE profile_photos
565 SET pub = {?}
566 WHERE pid = {?}",
567 $this->values['photo_pub'], $this->pid());
568 }
569 if ($this->changed['yourself']) {
570 if ($this->owner) {
571 XDB::execute('UPDATE accounts
572 SET display_name = {?}
573 WHERE uid = {?}',
574 $this->values['yourself'], $this->owner->id());
575 }
576 XDB::execute('UPDATE profile_display
577 SET yourself = {?}
578 WHERE pid = {?}', $this->values['yourself'],
579 $this->pid());
580 }
581 if ($this->changed['promo_display']) {
582 if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
583 if (($this->profile->mainEducation() == 'X'
584 && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year)
585 || ($this->profile->mainEducation() != 'X'
586 && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
587 XDB::execute('UPDATE profile_display
588 SET promo = {?}
589 WHERE pid = {?}',
590 $this->values['promo_display'], $this->pid());
591 }
592 }
593 }
594 }
595
596 public function _prepare(PlPage &$page, $id)
597 {
598 require_once "education.func.inc.php";
599
600 $res = XDB::query("SELECT id, field
601 FROM profile_education_field_enum
602 ORDER BY field");
603 $page->assign('edu_fields', $res->fetchAllAssoc());
604
605 require_once "emails.combobox.inc.php";
606 fill_email_combobox($page, $this->owner, $this->profile);
607
608 $res = XDB::query("SELECT nw.nwid AS type, nw.name
609 FROM profile_networking_enum AS nw
610 ORDER BY name");
611 $page->assign('network_list', $res->fetchAllAssoc());
612
613 $res = XDB::query("SELECT public_name, private_name
614 FROM profile_display
615 WHERE pid = {?}",
616 $this->pid());
617 $res = $res->fetchOneRow();
618 $page->assign('public_name', $res[0]);
619 $page->assign('private_name', $res[1]);
620 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
621 }
622 }
623
624 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
625 ?>