Fix name change / orange request
[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 class ProfileSettingSearchNames implements ProfileSetting
23 {
24 private $private_name_end;
25 private $search_names;
26 private $name_types;
27
28 public function __construct() {
29 $this->name_types = DirEnum::getOptions(DirEnum::NAMES);
30 }
31
32 private function matchWord($old, $new, $newLen)
33 {
34 return ($i = strpos($old, $new)) !== false
35 && ($i == 0 || $old{$i-1} == ' ')
36 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
37 }
38
39 private function prepareField($value)
40 {
41 return name_to_basename($value);
42 }
43
44 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
45 {
46 $success = true;
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);
52 if (!$success) {
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 . ").");
56 }
57 return $success ? $value : $init;
58 }
59
60 /* Removes duplicated entries for the fields that do not allow them. */
61 private function clean($value)
62 {
63 $single_types = XDB::fetchAllAssoc('id',
64 'SELECT id, 0
65 FROM profile_name_enum
66 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
67
68 foreach ($value as $key => $item) {
69 if (isset($single_types[$item['typeid']])) {
70 if ($single_types[$item['typeid']] === true) {
71 unset($value[$key]);
72 } else {
73 $single_types[$item['typeid']] = true;
74 }
75 }
76 }
77
78 return $value;
79 }
80
81 public function value(ProfilePage &$page, $field, $value, &$success)
82 {
83 $success = true;
84 $success_tmp = true;
85
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",
96 $page->pid());
97
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)
103 ORDER BY id");
104
105 $value = array();
106 $sn = $sn_all->next();
107 while ($sn_type = $sn_types->next()) {
108 if ($sn_type['id'] == $sn['typeid']) {
109 $value[] = $sn;
110 if ($sn) {
111 $sn = $sn_all->next();
112 }
113 } else {
114 $value[] = array('name' => '',
115 'particle' => '',
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,
121 'pub' => 1);
122 }
123 }
124 if ($sn) {
125 do {
126 $value[] = $sn;
127 } while ($sn = $sn_all->next());
128 }
129 $value = $this->clean($value);
130 } else {
131 require_once 'name.func.inc.php';
132
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'",
139 $page->pid());
140 $res = $res->fetchAllAssoc();
141 $initial = array();
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;
152 }
153 if ($sn['pub']) {
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) . ' ';
158 if (!$name) {
159 // particle is before first quote
160 list($particle, $name) = explode('\'', $sn['name'], 2);
161 $particle = trim($particle);
162 if (!$name) {
163 // actually there is no particle
164 $particle = '';
165 $name = $sn['name'];
166 }
167 }
168 } else {
169 $particle = '';
170 $name = $sn['name'];
171 }
172 }
173 if ($sn['name'] != '') {
174 if ($sn['pub']) {
175 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
176 'name' => $name,
177 'particle' => $particle,
178 'pub' => $sn['pub']);
179 } else {
180 if (isset($this->search_names[$sn['typeid']])) {
181 $this->search_names[$sn['typeid']][] = $sn['name'];
182 } else {
183 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']);
184 }
185 $sn['type_name'] = $sn_types[$sn['typeid']];
186 }
187 }
188 }
189 $res = XDB::query("SELECT public_name, private_name
190 FROM profile_display
191 WHERE pid = {?}",
192 $page->pid());
193 list($public_name, $private_name) = $res->fetchOneRow();
194 if ($success) {
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;
198 }
199 Platal::page()->assign('public_name', $public_name);
200 Platal::page()->assign('private_name', $private_name);
201 }
202
203 return $value;
204 }
205
206 public function save(ProfilePage &$page, $field, $value)
207 {
208 require_once 'name.func.inc.php';
209 require_once 'validations.inc.php';
210
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(), $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.');
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)) {
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 if (!is_array($value)) {
259 $value = null;
260 } else {
261 $i = 0;
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;
267 }
268 if ($key != $i) {
269 $value[$i] = $edu;
270 unset($value[$key]);
271 }
272 $i++;
273 }
274 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
275 }
276 return $value;
277 }
278
279 public function save(ProfilePage &$page, $field, $value)
280 {
281 XDB::execute("DELETE FROM profile_education
282 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
283 $page->pid());
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']);
291 }
292 }
293 }
294 }
295
296 class ProfileSettingEmailDirectory implements ProfileSetting
297 {
298 public function __construct(){}
299 public function save(ProfilePage &$page, $field, $value){}
300
301 public function value(ProfilePage &$page, $field, $value, &$success)
302 {
303 $p = Platal::page();
304
305 $success = true;
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');
312 $success = false;
313 } else {
314 $p->assign('email_error', '0');
315 }
316 }
317 return $value;
318 }
319 }
320
321 class ProfileSettingNetworking implements ProfileSetting
322 {
323 private $email;
324 private $pub;
325 private $web;
326 private $number;
327
328 public function __construct()
329 {
330 $this->email = new ProfileSettingEmail();
331 $this->pub = new ProfileSettingPub();
332 $this->web = new ProfileSettingWeb();
333 $this->number = new ProfileSettingNumber();
334 }
335
336 public function value(ProfilePage &$page, $field, $value, &$success)
337 {
338 if (is_null($value)) {
339 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
340 FROM profile_networking AS n
341 WHERE n.pid = {?}",
342 $page->pid());
343 }
344 if (!is_array($value)) {
345 $value = array();
346 }
347 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
348 FROM profile_networking_enum;');
349 $success = true;
350 foreach($value as $i=>&$network) {
351 if (!trim($network['address'])) {
352 unset($value[$i]);
353 } else {
354 if (!isset($network['pub'])) {
355 $network['pub'] = 'private';
356 }
357 $network['error'] = false;
358 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
359 $s = true;
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);
367 }
368 if (!$s) {
369 $success = false;
370 $network['error'] = true;
371 }
372 }
373 }
374 return $value;
375 }
376
377 public function save(ProfilePage &$page, $field, $value)
378 {
379 XDB::execute("DELETE FROM profile_networking
380 WHERE pid = {?}",
381 $page->pid());
382 if (!count($value)) {
383 return;
384 }
385 $insert = array();
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']);
390 }
391 }
392 }
393
394 class ProfileSettingPromo implements ProfileSetting
395 {
396 public function __construct(){}
397
398 public function save(ProfilePage &$page, $field, $value)
399 {
400 $gradYearNew = $value;
401 if ($page->profile->mainEducation() == 'X') {
402 $gradYearNew += $page->profile->mainEducationDuration();
403 }
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
408 SET promo = {?}
409 WHERE pid = {?}',
410 $page->profile->mainEducation() . strval($value), $page->profile->id());
411 XDB::execute('UPDATE profile_education
412 SET grad_year = {?}
413 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
414 $gradYearNew, $page->profile->id());
415 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
416 } else {
417 require_once 'validations.inc.php';
418
419 $myorange = new OrangeReq(S::user(), $page->profile, $gradYearNew);
420 $myorange->submit();
421 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
422 }
423 }
424
425 public function value(ProfilePage &$page, $field, $value, &$success)
426 {
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();
432 }
433 $success = true;
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);
440 } else {
441 $promoChoice[] = $page->profile->mainEducation() . strval($i + $page->profile->mainEducationDuration());
442 }
443 }
444 Platal::page()->assign('promo_choice', $promoChoice);
445 }
446 return $yearpromo;
447 }
448
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();
454 }
455
456 if ($value < 1000 || $value > 9999) {
457 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
458 $success = false;
459 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
460 Platal::page()->trigError('Trop tôt&nbsp;!');
461 $success = false;
462 }
463 return intval($value);
464 }
465 }
466
467
468 class ProfileSettingGeneral extends ProfilePage
469 {
470 protected $pg_template = 'profile/general.tpl';
471
472 public function __construct(PlWizard &$wiz)
473 {
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']
487 = null;
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);
500 }
501
502 protected function _fetchData()
503 {
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
509 FROM profiles AS p
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();
514 if ($this->owner) {
515 $this->values['yourself'] = $this->owner->displayName();
516 }
517
518 // Retreive photo informations
519 $res = XDB::query("SELECT pub
520 FROM profile_photos
521 WHERE pid = {?}", $this->pid());
522 $this->values['photo_pub'] = $res->fetchOneCell();
523
524 if ($this->owner) {
525 $res = XDB::query("SELECT COUNT(*)
526 FROM requests
527 WHERE type = 'photo' AND pid = {?}",
528 $this->owner->id());
529 $this->values['nouvellephoto'] = $res->fetchOneCell();
530 } else {
531 $this->values['nouvellephoto'] = 0;
532 }
533 }
534
535 protected function _saveData()
536 {
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;
542 }
543 if ($this->values['nationality2'] == "") {
544 $this->values['nationality2'] = $this->values['nationality3'];
545 $this->values['nationality3'] = NULL;
546 }
547 if ($this->values['nationality1'] == "") {
548 $this->values['nationality1'] = $this->values['nationality2'];
549 $this->values['nationality2'] = $this->values['nationality3'];
550 $this->values['nationality3'] = NULL;
551 }
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;
562 }
563
564 $new_email = ($this->values['email_directory'] == "new@example.org") ?
565 $this->values['email_directory_new'] : $this->values['email_directory'];
566 if ($new_email == "") {
567 $new_email = NULL;
568 }
569
570 XDB::execute("UPDATE profiles
571 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
572 freetext = {?}, freetext_pub = {?}, email_directory = {?}
573 WHERE pid = {?}",
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());
577 }
578 if ($this->changed['photo_pub']) {
579 XDB::execute("UPDATE profile_photos
580 SET pub = {?}
581 WHERE pid = {?}",
582 $this->values['photo_pub'], $this->pid());
583 }
584 if ($this->changed['yourself']) {
585 if ($this->owner) {
586 XDB::execute('UPDATE accounts
587 SET display_name = {?}
588 WHERE uid = {?}',
589 $this->values['yourself'], $this->owner->id());
590 }
591 XDB::execute('UPDATE profile_display
592 SET yourself = {?}
593 WHERE pid = {?}', $this->values['yourself'],
594 $this->pid());
595 }
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
603 SET promo = {?}
604 WHERE pid = {?}',
605 $this->values['promo_display'], $this->pid());
606 }
607 }
608 }
609 }
610
611 public function _prepare(PlPage &$page, $id)
612 {
613 require_once "education.func.inc.php";
614
615 $res = XDB::query("SELECT id, field
616 FROM profile_education_field_enum
617 ORDER BY field");
618 $page->assign('edu_fields', $res->fetchAllAssoc());
619
620 require_once "emails.combobox.inc.php";
621 fill_email_combobox($page, $this->owner, $this->profile);
622
623 $res = XDB::query("SELECT nw.nwid AS type, nw.name
624 FROM profile_networking_enum AS nw
625 ORDER BY name");
626 $page->assign('network_list', $res->fetchAllAssoc());
627
628 $res = XDB::query("SELECT public_name, private_name
629 FROM profile_display
630 WHERE pid = {?}",
631 $this->pid());
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);
636 }
637 }
638
639 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
640 ?>