d6a2b212bfcc56b8640389eb2f146738f2cfc1f7
[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 $value = mb_strtoupper($value);
42 return preg_replace('/[^A-Z]/', ' ', $value);
43 }
44
45 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
46 {
47 $success = true;
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);
53 if (!$success) {
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 . ").");
57 }
58 return $success ? $value : $init;
59 }
60
61 /* Removes duplicated entries for the fields that do not allow them. */
62 private function clean($value)
63 {
64 $single_types = XDB::fetchAllAssoc('id',
65 'SELECT id, 0
66 FROM profile_name_enum
67 WHERE NOT FIND_IN_SET(\'allow_duplicates\', flags)');
68
69 foreach ($value as $key => $item) {
70 if (isset($single_types[$item['typeid']])) {
71 if ($single_types[$item['typeid']] === true) {
72 unset($value[$key]);
73 } else {
74 $single_types[$item['typeid']] = true;
75 }
76 }
77 }
78
79 return $value;
80 }
81
82 public function value(ProfilePage &$page, $field, $value, &$success)
83 {
84 $success = true;
85 $success_tmp = true;
86
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",
97 $page->pid());
98
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)
104 ORDER BY id");
105
106 $value = array();
107 $sn = $sn_all->next();
108 while ($sn_type = $sn_types->next()) {
109 if ($sn_type['id'] == $sn['typeid']) {
110 $value[] = $sn;
111 if ($sn) {
112 $sn = $sn_all->next();
113 }
114 } else {
115 $value[] = array('name' => '',
116 'particle' => '',
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,
122 'pub' => 1);
123 }
124 }
125 if ($sn) {
126 do {
127 $value[] = $sn;
128 } while ($sn = $sn_all->next());
129 }
130 $value = $this->clean($value);
131 } else {
132 require_once 'name.func.inc.php';
133
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'",
140 $page->pid());
141 $res = $res->fetchAllAssoc();
142 $initial = array();
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;
153 }
154 if ($sn['pub']) {
155 if (isset($sn['particle']) && ($sn['particle'] != '')) {
156 list($particle, $name) = explode(' ', $sn['name'], 2);
157 $particle = trim($particle) . ' ';
158 if (!$name) {
159 list($particle, $name) = explode('\'', $sn['name'], 2);
160 $particle = trim($particle);
161 }
162 } else {
163 $particle = '';
164 $name = $sn['name'];
165 }
166 }
167 if ($sn['name'] != '') {
168 if ($sn['pub']) {
169 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
170 'name' => $name,
171 'particle' => $particle,
172 'pub' => $sn['pub']);
173 } else {
174 if (isset($this->search_names[$sn['typeid']])) {
175 $this->search_names[$sn['typeid']][] = $sn['name'];
176 } else {
177 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']);
178 }
179 $sn['type_name'] = $sn_types[$sn['typeid']];
180 }
181 }
182 }
183 $res = XDB::query("SELECT public_name, private_name
184 FROM profile_display
185 WHERE pid = {?}",
186 S::v('uid'));
187 list($public_name, $private_name) = $res->fetchOneRow();
188 if ($success) {
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;
192 }
193 Platal::page()->assign('public_name', $public_name);
194 Platal::page()->assign('private_name', $private_name);
195 }
196
197 return $value;
198 }
199
200 public function save(ProfilePage &$page, $field, $value)
201 {
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)",
208 $page->pid());
209 $has_new = set_alias_names($this->search_names, $sn_old);
210
211 // Only requires validation if modification in public names
212 if ($has_new) {
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.');
217 } else {
218 $display_names = array();
219 build_display_names($display_names, $this->search_names, $this->private_name_end);
220 set_profile_display($display_names);
221 }
222 }
223 }
224
225 class ProfileSettingEdu implements ProfileSetting
226 {
227 public function __construct() {
228 }
229
230 static function sortByGradYear($line1, $line2) {
231 $a = (int) $line1['grad_year'];
232 $b = (int) $line2['grad_year'];
233 if ($a == $b) {
234 return 0;
235 }
236 return ($a < $b) ? -1 : 1;
237 }
238
239 public function value(ProfilePage &$page, $field, $value, &$success)
240 {
241 $success = true;
242 if (is_null($value) || !is_array($value)) {
243 $value = array();
244 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
245 FROM profile_education
246 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
247 ORDER BY id",
248 $page->pid());
249 } else {
250 $i = 0;
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;
255 $success = false;
256 }
257 if ($key != $i) {
258 $value[$i] = $edu;
259 unset($value[$key]);
260 }
261 $i++;
262 }
263 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
264 }
265 return $value;
266 }
267
268 public function save(ProfilePage &$page, $field, $value)
269 {
270 XDB::execute("DELETE FROM profile_education
271 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
272 $page->pid());
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']);
280 }
281 }
282 }
283 }
284
285 class ProfileSettingEmailDirectory implements ProfileSetting
286 {
287 public function __construct(){}
288 public function save(ProfilePage &$page, $field, $value){}
289
290 public function value(ProfilePage &$page, $field, $value, &$success)
291 {
292 $p = Platal::page();
293
294 $success = true;
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');
301 $success = false;
302 } else {
303 $p->assign('email_error', '0');
304 }
305 }
306 return $value;
307 }
308 }
309
310 class ProfileSettingNetworking implements ProfileSetting
311 {
312 private $email;
313 private $pub;
314 private $web;
315 private $number;
316
317 public function __construct()
318 {
319 $this->email = new ProfileSettingEmail();
320 $this->pub = new ProfileSettingPub();
321 $this->web = new ProfileSettingWeb();
322 $this->number = new ProfileSettingNumber();
323 }
324
325 public function value(ProfilePage &$page, $field, $value, &$success)
326 {
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)
331 WHERE n.pid = {?}",
332 $page->pid());
333 }
334 if (!is_array($value)) {
335 $value = array();
336 }
337 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, network_type AS type
338 FROM profile_networking_enum;');
339 $success = true;
340 foreach($value as $i=>&$network) {
341 if (!trim($network['address'])) {
342 unset($value[$i]);
343 } else {
344 if (!isset($network['pub'])) {
345 $network['pub'] = 'private';
346 }
347 $network['error'] = false;
348 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
349 $s = true;
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);
356 }
357 if (!$s) {
358 $success = false;
359 $network['error'] = true;
360 }
361 }
362 }
363 return $value;
364 }
365
366 public function save(ProfilePage &$page, $field, $value)
367 {
368 XDB::execute("DELETE FROM profile_networking
369 WHERE pid = {?}",
370 $page->pid());
371 if (!count($value)) {
372 return;
373 }
374 $insert = array();
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']);
379 }
380 }
381 }
382
383 class ProfileSettingPromo implements ProfileSetting
384 {
385 public function __construct(){}
386
387 public function save(ProfilePage &$page, $field, $value)
388 {
389 $gradYearNew = $value;
390 if ($page->profile->mainEducation() == 'X') {
391 $gradYearNew += $page->profile->mainEducationDuration();
392 }
393 if ($value == $page->profile->entry_year + $page->profile->mainEducationDuration()) {
394 XDB::execute('UPDATE profile_display
395 SET promo = {?}
396 WHERE pid = {?}',
397 $page->profile->mainEducation() . strval($value), $page->profile->id());
398 XDB::execute('UPDATE profile_education
399 SET grad_year = {?}
400 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
401 $gradYearNew, $page->profile->id());
402 $page->trigSuccess('Ton statut "orange" a été supprimé.');
403 } else {
404 require_once 'validations.inc.php';
405
406 $myorange = new OrangeReq(S::user(), $gradYearNew);
407 $myorange->submit();
408 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
409 }
410 }
411
412 public function value(ProfilePage &$page, $field, $value, &$success)
413 {
414 $entryYear = $page->profile->entry_year;
415 $gradYear = $page->profile->grad_year;
416 $success = true;
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);
422 }
423 Platal::page()->assign('promo_choice', $promoChoice);
424 }
425 if ($page->profile->mainEducation() == 'X') {
426 return $page->profile->grad_year - $page->profile->mainEducationDuration();
427 }
428 return $page->profile->yearpromo();
429 }
430
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();
436 }
437
438 if ($value < 1000 || $value > 9999) {
439 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
440 $success = false;
441 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
442 Platal::page()->trigError('Trop tôt&nbsp;!');
443 $success = false;
444 }
445 return intval($value);
446 }
447 }
448
449
450 class ProfileSettingGeneral extends ProfilePage
451 {
452 protected $pg_template = 'profile/general.tpl';
453
454 public function __construct(PlWizard &$wiz)
455 {
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']
469 = null;
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);
482 }
483
484 protected function _fetchData()
485 {
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
491 FROM profiles AS p
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();
496 if ($this->owner) {
497 $this->values['yourself'] = $this->owner->displayName();
498 }
499
500 // Retreive photo informations
501 $res = XDB::query("SELECT pub
502 FROM profile_photos
503 WHERE pid = {?}", $this->pid());
504 $this->values['photo_pub'] = $res->fetchOneCell();
505
506 if ($this->owner) {
507 $res = XDB::query("SELECT COUNT(*)
508 FROM requests
509 WHERE type = 'photo' AND uid = {?}",
510 $this->owner->id());
511 $this->values['nouvellephoto'] = $res->fetchOneCell();
512 } else {
513 $this->values['nouvellephoto'] = 0;
514 }
515 }
516
517 protected function _saveData()
518 {
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;
524 }
525 if ($this->values['nationality2'] == "") {
526 $this->values['nationality2'] = $this->values['nationality3'];
527 $this->values['nationality3'] = NULL;
528 }
529 if ($this->values['nationality1'] == "") {
530 $this->values['nationality1'] = $this->values['nationality2'];
531 $this->values['nationality2'] = $this->values['nationality3'];
532 $this->values['nationality3'] = NULL;
533 }
534 $new_email = ($this->values['email_directory'] == "new@example.org") ?
535 $this->values['email_directory_new'] : $this->values['email_directory'];
536 if ($new_email == "") {
537 $new_email = NULL;
538 }
539
540 XDB::execute("UPDATE profiles
541 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
542 freetext = {?}, freetext_pub = {?}, email_directory = {?}
543 WHERE pid = {?}",
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());
547 }
548 if ($this->changed['photo_pub']) {
549 XDB::execute("UPDATE profile_photos
550 SET pub = {?}
551 WHERE pid = {?}",
552 $this->values['photo_pub'], $this->pid());
553 }
554 if ($this->changed['yourself']) {
555 XDB::execute('UPDATE accounts
556 SET display_name = {?}
557 WHERE pid = {?}', $this->pid());
558 }
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
563 SET promo = {?}
564 WHERE pid = {?}',
565 $this->values['promo_display'], $this->pid());
566 }
567 }
568 }
569
570 public function _prepare(PlPage &$page, $id)
571 {
572 require_once "education.func.inc.php";
573
574 $res = XDB::query("SELECT id, field
575 FROM profile_education_field_enum
576 ORDER BY field");
577 $page->assign('edu_fields', $res->fetchAllAssoc());
578
579 require_once "emails.combobox.inc.php";
580 fill_email_combobox($page, $this->owner, $this->profile);
581
582 $res = XDB::query("SELECT nw.network_type AS type, nw.name
583 FROM profile_networking_enum AS nw
584 ORDER BY name");
585 $page->assign('network_list', $res->fetchAllAssoc());
586
587 $res = XDB::query("SELECT public_name, private_name
588 FROM profile_display
589 WHERE pid = {?}",
590 $this->pid());
591 $res = $res->fetchOneRow();
592 $page->assign('public_name', $res[0]);
593 $page->assign('private_name', $res[1]);
594 }
595 }
596
597 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
598 ?>