Amount column in payment table is now a number (was a text)
[platal.git] / modules / profile / general.inc.php
CommitLineData
fd38b30e
FB
1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
fd38b30e
FB
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
12bcf04b 22class ProfileSettingSearchNames implements ProfileSetting
fd38b30e 23{
dced83b4
SJ
24 private $private_name_end;
25 private $search_names;
b270577e
SJ
26 private $name_types;
27
28 public function __construct() {
29 $this->name_types = DirEnum::getOptions(DirEnum::NAMES);
30 }
c4b45374 31
e5bcd851
FB
32 private function matchWord($old, $new, $newLen)
33 {
93553cea 34 return ($i = strpos($old, $new)) !== false
fd38b30e
FB
35 && ($i == 0 || $old{$i-1} == ' ')
36 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
37 }
38
39 private function prepareField($value)
40 {
ac40839f 41 return name_to_basename($value);
fd38b30e
FB
42 }
43
26ba053e 44 private function prepare(ProfilePage $page, $field, $value, $init, &$success)
fd38b30e
FB
45 {
46 $success = true;
dced83b4
SJ
47 $ini = $this->prepareField($init);
48 $new = $this->prepareField($value);
c4b45374
SJ
49 $newLen = strlen($new);
50 $success = $this->matchWord($ini, $new, $newLen)
6cb58d39 51 || ($field == 'lastname' && $new == 'DE ' . $ini);
fd38b30e 52 if (!$success) {
c4b45374 53 $field = strtolower($field);
b270577e
SJ
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 . ").");
fd38b30e 56 }
c4b45374 57 return $success ? $value : $init;
fd38b30e
FB
58 }
59
b270577e
SJ
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
26ba053e 81 public function value(ProfilePage $page, $field, $value, &$success)
fd38b30e 82 {
dced83b4 83 $success = true;
c4b45374 84 $success_tmp = true;
09f9ebea 85
c4b45374
SJ
86 if (is_null($value)) {
87 $sn_all = XDB::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
6cb58d39 88 sn.particle, sn.typeid, e.type, e.name AS type_name,
c4b45374
SJ
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
97a98687
SJ
92 FROM profile_name AS sn
93 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
c4b45374
SJ
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",
111b2736 96 $page->pid());
c4b45374 97
6cb58d39
SJ
98 $sn_types = XDB::iterator("SELECT id, type, name,
99 FIND_IN_SET('has_particle', flags) AS has_particle
97a98687 100 FROM profile_name_enum
c4b45374
SJ
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;
111b2736
FB
110 if ($sn) {
111 $sn = $sn_all->next();
112 }
c4b45374 113 } else {
09f9ebea
SJ
114 $value[] = array('name' => '',
115 'particle' => '',
116 'typeid' => $sn_type['id'],
117 'type' => $sn_type['type'],
118 'type_name' => $sn_type['name'],
c4b45374 119 'has_particle' => $sn_type['has_particle'],
09f9ebea
SJ
120 'always_displayed' => 1,
121 'pub' => 1);
c4b45374
SJ
122 }
123 }
6cb58d39
SJ
124 if ($sn) {
125 do {
126 $value[] = $sn;
127 } while ($sn = $sn_all->next());
111b2736 128 }
bd3e755e
SJ
129 $namesRequest = ProfileValidate::get_typed_requests($page->pid(), 'usage');
130 if (count($namesRequest) > 0) {
131 Platal::page()->assign('validation', true);
132 }
b270577e 133 $value = $this->clean($value);
c4b45374 134 } else {
6cb58d39
SJ
135 require_once 'name.func.inc.php';
136
b270577e 137 $value = $this->clean($value);
c4b45374 138 $res = XDB::query("SELECT s.particle, s.name
97a98687
SJ
139 FROM profile_name AS s
140 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
2fa91df0
SJ
141 WHERE s.pid = {?} AND (e.type = 'lastname' OR e.type = 'firstname')
142 ORDER BY e.type = 'firstname'",
111b2736 143 $page->pid());
c4b45374
SJ
144 $res = $res->fetchAllAssoc();
145 $initial = array();
6cb58d39
SJ
146 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
147 $initial['firstname'] = $res[1]['name'];
148 $sn_types = build_types();
dced83b4
SJ
149 $this->search_names = array();
150 foreach ($value as &$sn) {
c4b45374 151 $sn['name'] = trim($sn['name']);
43b60c21 152 if (S::user()->isMe($page->owner) && ($sn['type'] == 'firstname' || $sn['type'] == 'lastname')) {
c4b45374
SJ
153 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
154 $initial[$sn['type']], $success_tmp);
155 $success = $success && $success_tmp;
156 }
dced83b4
SJ
157 if ($sn['pub']) {
158 if (isset($sn['particle']) && ($sn['particle'] != '')) {
faf5f834 159 // particle is before first blank
dced83b4
SJ
160 list($particle, $name) = explode(' ', $sn['name'], 2);
161 $particle = trim($particle) . ' ';
162 if (!$name) {
faf5f834 163 // particle is before first quote
dced83b4
SJ
164 list($particle, $name) = explode('\'', $sn['name'], 2);
165 $particle = trim($particle);
faf5f834
PC
166 if (!$name) {
167 // actually there is no particle
168 $particle = '';
169 $name = $sn['name'];
170 }
dced83b4
SJ
171 }
172 } else {
173 $particle = '';
174 $name = $sn['name'];
175 }
176 }
c4b45374 177 if ($sn['name'] != '') {
dced83b4
SJ
178 if ($sn['pub']) {
179 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
180 'name' => $name,
181 'particle' => $particle,
182 'pub' => $sn['pub']);
c4b45374 183 } else {
dced83b4
SJ
184 if (isset($this->search_names[$sn['typeid']])) {
185 $this->search_names[$sn['typeid']][] = $sn['name'];
186 } else {
187 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']);
188 }
6cb58d39 189 $sn['type_name'] = $sn_types[$sn['typeid']];
c4b45374
SJ
190 }
191 }
192 }
dced83b4
SJ
193 $res = XDB::query("SELECT public_name, private_name
194 FROM profile_display
195 WHERE pid = {?}",
f41cf525 196 $page->pid());
dced83b4
SJ
197 list($public_name, $private_name) = $res->fetchOneRow();
198 if ($success) {
dced83b4
SJ
199 $sn_types_private = build_types('private');
200 $this->private_name_end = build_private_name($this->search_names, $sn_types_private);
201 $private_name = $public_name . $this->private_name_end;
202 }
203 Platal::page()->assign('public_name', $public_name);
204 Platal::page()->assign('private_name', $private_name);
fd38b30e 205 }
09f9ebea 206
c4b45374 207 return $value;
fd38b30e
FB
208 }
209
26ba053e 210 public function save(ProfilePage $page, $field, $value)
fd38b30e 211 {
dced83b4 212 require_once 'name.func.inc.php';
024ec1e5 213
e8a7cf31 214 $sn_old = build_sn_pub($page->pid());
c4b45374 215 XDB::execute("DELETE FROM s
97a98687
SJ
216 USING profile_name AS s
217 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
c4b45374 218 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
111b2736 219 $page->pid());
f036c896 220 $has_new = set_alias_names($this->search_names, $sn_old, $page->pid(), $page->owner);
dced83b4
SJ
221
222 // Only requires validation if modification in public names
223 if ($has_new) {
0b981fbe 224 $new_names = new NamesReq(S::user(), $page->profile, $this->search_names, $this->private_name_end);
dced83b4 225 $new_names->submit();
b270577e
SJ
226 Platal::page()->trigWarning('La demande de modification de tes noms a bien été prise en compte.' .
227 ' Tu recevras un email dès que ces changements auront été effectués.');
dced83b4
SJ
228 } else {
229 $display_names = array();
e8a7cf31
SJ
230 build_display_names($display_names, $this->search_names,
231 $page->profile->isFemale(), $this->private_name_end);
f4b04704 232 set_profile_display($display_names, $page->profile);
c4b45374 233 }
fd38b30e 234 }
a0fce0c6
SJ
235
236 public function getText($value) {
237 $names = array();
238 foreach ($value as $name) {
239 if ($name['name'] != '') {
14aba233 240 $names[] = mb_strtolower($name['type_name']) . ' : ' . $name['name'];
a0fce0c6
SJ
241 }
242 }
243 return implode(', ' , $names);
244 }
fd38b30e
FB
245}
246
12bcf04b 247class ProfileSettingEdu implements ProfileSetting
576777d7 248{
e5bcd851
FB
249 public function __construct() {
250 }
043bbacf 251
5ba8842e 252 static function sortByGradYear($line1, $line2) {
be6806fa
SJ
253 $a = (isset($line1['grad_year'])) ? (int) $line1['grad_year'] : 0;
254 $b = (isset($line2['grad_year'])) ? (int) $line2['grad_year'] : 0;
22f0a0a8 255 if ($a == $b) {
5ba8842e
SJ
256 return 0;
257 }
22f0a0a8 258 return ($a < $b) ? -1 : 1;
5ba8842e
SJ
259 }
260
26ba053e 261 public function value(ProfilePage $page, $field, $value, &$success)
576777d7
FB
262 {
263 $success = true;
885fa398 264 if (is_null($value)) {
043bbacf 265 $value = array();
111b2736
FB
266 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
267 FROM profile_education
ce0b2c6f 268 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
111b2736
FB
269 ORDER BY id",
270 $page->pid());
885fa398
SJ
271 } else if (!is_array($value)) {
272 $value = null;
043bbacf
SJ
273 } else {
274 $i = 0;
275 foreach ($value as $key=>&$edu) {
3ede4850
SJ
276 if ($edu['eduid'] < 1 || !isset($edu['degreeid']) || $edu['degreeid'] < 1) {
277 Platal::page()->trigError('L\'université ou le diplôme d\'une formation manque.');
278 $success = false;
279 }
043bbacf 280 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
be6806fa 281 Platal::page()->trigWarning('L\'année d\'obtention du diplôme est mal ou non renseignée, elle doit être du type : 2004.');
83065276 282 $edu['grad_year'] = null;
be6806fa 283 $edu['warning'] = true;
043bbacf
SJ
284 }
285 if ($key != $i) {
286 $value[$i] = $edu;
287 unset($value[$key]);
288 }
289 $i++;
290 }
12bcf04b 291 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
576777d7
FB
292 }
293 return $value;
294 }
295
26ba053e 296 public function save(ProfilePage $page, $field, $value)
576777d7 297 {
043bbacf 298 XDB::execute("DELETE FROM profile_education
ce0b2c6f 299 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
e5bcd851 300 $page->pid());
043bbacf
SJ
301 foreach ($value as $eduid=>&$edu) {
302 if ($edu['eduid'] != '') {
8768e5af 303 $fieldId = ($edu['fieldid'] == 0) ? null : $edu['fieldid'];
043bbacf 304 XDB::execute("INSERT INTO profile_education
ce0b2c6f 305 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
1504ac45 306 fieldid = {?}, grad_year = {?}, program = {?}",
e5bcd851 307 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
8768e5af 308 $fieldId, $edu['grad_year'], $edu['program']);
043bbacf 309 }
576777d7
FB
310 }
311 }
a0fce0c6
SJ
312
313 public function getText($value) {
314 $schoolsList = DirEnum::getOptions(DirEnum::EDUSCHOOLS);
315 $degreesList = DirEnum::getOptions(DirEnum::EDUDEGREES);
316 $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS);
317 $educations = array();
14aba233
SJ
318 foreach ($value as $id => $education) {
319 // XXX: the following condition should be removed once there are no more incomplete educations.
320 if (is_null($education['eduid']) || is_null($education['degreeid'])) {
321 if (is_null($education['eduid']) && is_null($education['degreeid'])) {
322 $educations[$id] = 'formation manquante';
323 } else {
324 $educations[$id] = (is_null($education['eduid']) ? 'université manquante' : $schoolsList[$education['eduid']]) . ', '
325 . (is_null($education['degreeid']) ? 'diplôme manquant' : $degreesList[$education['degreeid']]);
326 }
327 } else {
328 $educations[$id] = $schoolsList[$education['eduid']] . ', ' . $degreesList[$education['degreeid']];
329 }
330
331 $details = array();
332 if ($education['grad_year']) {
333 $details[] = $education['grad_year'];
334 }
335 if ($education['program']) {
336 $details[] = '« ' . $education['program'] . ' »';
337 }
338 if ($education['fieldid']) {
339 $details[] = $fieldsList[$education['fieldid']];
340 }
341 if (count($details)) {
342 $educations[$id] .= ' (' . implode(', ', $details) . ')';
343 }
a0fce0c6
SJ
344 }
345 return implode(', ', $educations);
346 }
576777d7
FB
347}
348
d7de04af
SJ
349class ProfileSettingMainEdu implements ProfileSetting
350{
351 private $cycles;
352
353 public function __construct()
354 {
355 $eduDegrees = DirEnum::getOptions(DirEnum::EDUDEGREES);
356 $eduDegrees = array_flip($eduDegrees);
357 $this->cycles = array(
358 $eduDegrees[Profile::DEGREE_X] => 'Cycle polytechnicien',
359 $eduDegrees[Profile::DEGREE_M] => 'Cycle master',
360 $eduDegrees[Profile::DEGREE_D] => 'Cycle doctoral'
361 );
362 }
363
364 public function value(ProfilePage $page, $field, $value, &$success)
365 {
366 $success = true;
367 if (is_null($value)) {
368 $value = array();
369 $value = XDB::fetchAllAssoc("SELECT degreeid, fieldid, promo_year, program
370 FROM profile_education
371 WHERE pid = {?} AND FIND_IN_SET('primary', flags)
372 ORDER BY degreeid",
373 $page->pid());
374
375 foreach ($value as &$item) {
376 $item['cycle'] = $this->cycles[$item['degreeid']];
377 }
378 } elseif (!is_array($value)) {
379 $value = array();
380 } else {
381 foreach ($value as $key => $item) {
382 if (!isset($item['degreeid'])) {
383 unset($value[$key]);
384 }
385 }
386 }
387
388 return $value;
389 }
390
391 public function save(ProfilePage $page, $field, $value)
392 {
393 foreach ($value as $item) {
394 $fieldId = ($item['fieldid'] == 0) ? null : $item['fieldid'];
395 XDB::execute("UPDATE profile_education
396 SET fieldid = {?}, program = {?}
397 WHERE pid = {?} AND FIND_IN_SET('primary', flags) AND degreeid = {?}",
398 $fieldId, $item['program'], $page->pid(), $item['degreeid']);
399 }
400 }
401
402 public function getText($value) {
403 $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS);
404 $educations = array();
405 foreach ($value as $item) {
406 $details = array($this->cycles[$item['degreeid']]);
407 if ($education['program']) {
408 $details[] = '« ' . $education['program'] . ' »';
409 }
410 if ($education['fieldid']) {
411 $details[] = $fieldsList[$education['fieldid']];
412 }
413 }
414 return implode(', ', $educations);
415 }
416}
417
12bcf04b 418class ProfileSettingEmailDirectory implements ProfileSetting
b715c1e1 419{
b814a8b8 420 public function __construct(){}
26ba053e 421 public function save(ProfilePage $page, $field, $value){}
b715c1e1 422
26ba053e 423 public function value(ProfilePage $page, $field, $value, &$success)
b715c1e1 424 {
ad3fee9d 425 $p = Platal::page();
b715c1e1
SJ
426
427 $success = true;
428 if (!is_null($value)) {
429 $email_stripped = strtolower(trim($value));
ad3fee9d
SJ
430 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
431 $p->assign('email_error', '1');
432 $p->assign('email_directory_error', $email_stripped);
433 $p->trigError('Adresse Email invalide');
b715c1e1
SJ
434 $success = false;
435 } else {
ad3fee9d 436 $p->assign('email_error', '0');
b715c1e1
SJ
437 }
438 }
439 return $value;
440 }
a0fce0c6
SJ
441
442 public function getText($value) {
443 return $value;
444 }
b715c1e1
SJ
445}
446
12bcf04b 447class ProfileSettingNetworking implements ProfileSetting
d1a2252a
GB
448{
449 private $email;
450 private $pub;
451 private $web;
92446a53 452 private $number;
d1a2252a
GB
453
454 public function __construct()
455 {
12bcf04b
RB
456 $this->email = new ProfileSettingEmail();
457 $this->pub = new ProfileSettingPub();
458 $this->web = new ProfileSettingWeb();
459 $this->number = new ProfileSettingNumber();
d1a2252a
GB
460 }
461
26ba053e 462 public function value(ProfilePage $page, $field, $value, &$success)
d1a2252a
GB
463 {
464 if (is_null($value)) {
1f5cd004 465 $value = XDB::fetchAllAssoc("SELECT n.address, n.pub, n.nwid AS type
111b2736 466 FROM profile_networking AS n
ce0b2c6f 467 WHERE n.pid = {?}",
111b2736 468 $page->pid());
d1a2252a
GB
469 }
470 if (!is_array($value)) {
471 $value = array();
472 }
1f5cd004 473 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, nwid AS type, name
111b2736 474 FROM profile_networking_enum;');
d1a2252a
GB
475 $success = true;
476 foreach($value as $i=>&$network) {
92c3f9e5
GB
477 if (!trim($network['address'])) {
478 unset($value[$i]);
479 } else {
480 if (!isset($network['pub'])) {
481 $network['pub'] = 'private';
482 }
483 $network['error'] = false;
484 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
485 $s = true;
1f5cd004
PC
486 $network['name'] = $filters[$network['type']]['name'];
487 if ($filters[$network['type']]['filter'] == 'web') {
92c3f9e5 488 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
1f5cd004 489 } elseif ($filters[$network['type']]['filter'] == 'email') {
92c3f9e5 490 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
1f5cd004 491 } elseif ($filters[$network['type']]['filter'] == 'number') {
92c3f9e5
GB
492 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
493 }
494 if (!$s) {
495 $success = false;
496 $network['error'] = true;
497 }
d1a2252a
GB
498 }
499 }
500 return $value;
501 }
502
26ba053e 503 public function save(ProfilePage $page, $field, $value)
d1a2252a
GB
504 {
505 XDB::execute("DELETE FROM profile_networking
ce0b2c6f 506 WHERE pid = {?}",
e5bcd851 507 $page->pid());
d1a2252a
GB
508 if (!count($value)) {
509 return;
510 }
511 $insert = array();
512 foreach ($value as $id=>$network) {
1f5cd004 513 XDB::execute("INSERT INTO profile_networking (pid, id, nwid, address, pub)
d1a2252a 514 VALUES ({?}, {?}, {?}, {?}, {?})",
e5bcd851 515 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
d1a2252a
GB
516 }
517 }
a0fce0c6
SJ
518
519 public function getText($value) {
14aba233 520 static $pubs = array('public' => 'publique', 'ax' => 'annuaire AX', 'private' => 'privé');
a0fce0c6
SJ
521 $networkings = array();
522 foreach ($value as $network) {
14aba233 523 $networkings[] = $network['name'] . ' : ' . $network['address'] . ' (affichage ' . $pubs[$network['pub']] . ')';
a0fce0c6 524 }
14aba233 525 return implode(', ' , $networkings);
a0fce0c6 526 }
d1a2252a
GB
527}
528
7e233317
SJ
529class ProfileSettingPromo implements ProfileSetting
530{
531 public function __construct(){}
532
26ba053e 533 public function save(ProfilePage $page, $field, $value)
7e233317
SJ
534 {
535 $gradYearNew = $value;
536 if ($page->profile->mainEducation() == 'X') {
537 $gradYearNew += $page->profile->mainEducationDuration();
538 }
86ced4d4
SJ
539 if (($page->profile->mainEducation() != 'X'
540 && $value == $page->profile->entry_year + $page->profile->mainEducationDuration())
541 || ($page->profile->mainEducation() == 'X' && $value == $page->profile->entry_year)) {
7e233317
SJ
542 XDB::execute('UPDATE profile_display
543 SET promo = {?}
544 WHERE pid = {?}',
545 $page->profile->mainEducation() . strval($value), $page->profile->id());
546 XDB::execute('UPDATE profile_education
547 SET grad_year = {?}
548 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
549 $gradYearNew, $page->profile->id());
86ced4d4 550 Platal::page()->trigSuccess('Ton statut « orange » a été supprimé.');
7e233317 551 } else {
0b981fbe 552 $myorange = new OrangeReq(S::user(), $page->profile, $gradYearNew);
7e233317
SJ
553 $myorange->submit();
554 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
555 }
556 }
557
26ba053e 558 public function value(ProfilePage $page, $field, $value, &$success)
7e233317
SJ
559 {
560 $entryYear = $page->profile->entry_year;
561 $gradYear = $page->profile->grad_year;
7733ade1 562 $yearpromo = $page->profile->yearpromo();
7e233317 563 $success = true;
86ced4d4 564 if (is_null($value) || $value == $yearpromo) {
7e233317
SJ
565 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
566 $promoChoice = array();
86ced4d4
SJ
567 for ($i = $entryYear; $i <= $gradYear - $page->profile->mainEducationDuration(); ++$i) {
568 if ($page->profile->mainEducation() == 'X') {
569 $promoChoice[] = $page->profile->mainEducation() . strval($i);
570 } else {
571 $promoChoice[] = $page->profile->mainEducation() . strval($i + $page->profile->mainEducationDuration());
572 }
7e233317
SJ
573 }
574 Platal::page()->assign('promo_choice', $promoChoice);
575 }
86ced4d4 576 return $yearpromo;
7e233317
SJ
577 }
578
579 // If this profile belongs to an X, $promoNew needs to be changed to
580 // the graduation year.
581 $gradYearNew = $value;
582 if ($page->profile->mainEducation() == 'X') {
583 $gradYearNew += $page->profile->mainEducationDuration();
584 }
585
586 if ($value < 1000 || $value > 9999) {
587 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
588 $success = false;
589 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
590 Platal::page()->trigError('Trop tôt&nbsp;!');
591 $success = false;
592 }
593 return intval($value);
594 }
a0fce0c6
SJ
595
596 public function getText($value) {
597 return $value;
598 }
7e233317
SJ
599}
600
601
66c4bdaf 602class ProfilePageGeneral extends ProfilePage
fd38b30e
FB
603{
604 protected $pg_template = 'profile/general.tpl';
605
26ba053e 606 public function __construct(PlWizard $wiz)
fd38b30e
FB
607 {
608 parent::__construct($wiz);
c4b45374 609 $this->settings['search_names']
12bcf04b 610 = new ProfileSettingSearchNames();
42ec0fe2 611 $this->settings['nationality1']
e5bcd851
FB
612 = $this->settings['nationality2']
613 = $this->settings['nationality3']
7e233317 614 = $this->settings['promo_display']
93553cea 615 = null;
b715c1e1 616 $this->settings['email_directory']
12bcf04b 617 = new ProfileSettingEmail();
b715c1e1 618 $this->settings['email_directory_new']
12bcf04b
RB
619 = new ProfileSettingEmailDirectory();
620 $this->settings['networking'] = new ProfileSettingNetworking();
0b6c8b36 621 $this->settings['tels'] = new ProfileSettingPhones();
12bcf04b 622 $this->settings['edus'] = new ProfileSettingEdu();
d7de04af 623 $this->settings['main_edus'] = new ProfileSettingMainEdu();
7e233317 624 $this->settings['promo'] = new ProfileSettingPromo();
42ec0fe2 625 $this->watched= array('tels' => true,
043bbacf 626 'networking' => true, 'edus' => true,
e5bcd851 627 'nationality1' => true, 'nationality2' => true,
4ca15c31 628 'nationality3' => true, 'search_names' => true);
42ec0fe2
FB
629
630 /* Some fields editable under condition */
631 if (!S::user()->isMe($this->owner)) {
632 $this->settings['deathdate'] = new ProfileSettingDate(true);
c56a253e
SJ
633 $this->settings['birthdate'] = new ProfileSettingDate(true);
634 } else {
1447ed1a 635 $this->settings['yourself'] = null;
c56a253e 636 $this->settings['birthdate'] = new ProfileSettingDate();
1447ed1a 637 }
42ec0fe2
FB
638 if (S::user()->checkPerms('directory_private')
639 || S::user()->isMyProfile($this->owner)) {
1447ed1a 640 $this->settings['freetext'] = null;
42ec0fe2
FB
641 $this->settings['freetext_pub']
642 = $this->settings['photo_pub']
643 = new ProfileSettingPub();
644 $this->watched['freetext'] = true;
645 }
646
93553cea
FB
647 }
648
7c2e0f0d 649 protected function _fetchData()
93553cea 650 {
576777d7 651 // Checkout all data...
c56a253e 652 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, IF(p.birthdate = 0, '', p.birthdate) AS birthdate,
7e233317 653 p.email_directory as email_directory, pd.promo AS promo_display,
87db81e7
FB
654 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself,
655 p.deathdate
7e233317
SJ
656 FROM profiles AS p
657 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
7e233317 658 WHERE p.pid = {?}", $this->pid());
93553cea 659 $this->values = $res->fetchOneAssoc();
576777d7 660
576777d7
FB
661 // Retreive photo informations
662 $res = XDB::query("SELECT pub
5c4ea53f
FB
663 FROM profile_photos
664 WHERE pid = {?}", $this->pid());
bf9202ed
SJ
665 if ($res->numRows() == 0) {
666 $this->values['photo_pub'] = 'private';
667 } else {
668 $this->values['photo_pub'] = $res->fetchOneCell();
669 }
576777d7 670
e5bcd851
FB
671 if ($this->owner) {
672 $res = XDB::query("SELECT COUNT(*)
673 FROM requests
58bb2d9c 674 WHERE type = 'photo' AND pid = {?}",
e5bcd851
FB
675 $this->owner->id());
676 $this->values['nouvellephoto'] = $res->fetchOneCell();
677 } else {
678 $this->values['nouvellephoto'] = 0;
679 }
93553cea
FB
680 }
681
7c2e0f0d 682 protected function _saveData()
93553cea 683 {
e5bcd851 684 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
ce0b2c6f
FB
685 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
686 || $this->changed['email_directory']) {
e5bcd851
FB
687 if ($this->values['nationality3'] == "") {
688 $this->values['nationality3'] = NULL;
8450c2aa 689 }
e5bcd851
FB
690 if ($this->values['nationality2'] == "") {
691 $this->values['nationality2'] = $this->values['nationality3'];
692 $this->values['nationality3'] = NULL;
8450c2aa 693 }
e5bcd851 694 if ($this->values['nationality1'] == "") {
48a4525c 695 $this->values['nationality1'] = $this->values['nationality2'];
e5bcd851
FB
696 $this->values['nationality2'] = $this->values['nationality3'];
697 $this->values['nationality3'] = NULL;
8450c2aa 698 }
48a4525c
SJ
699 if ($this->values['nationality1'] == $this->values['nationality2']
700 && $this->values['nationality2'] == $this->values['nationality3']) {
701 $this->values['nationality2'] = NULL;
702 $this->values['nationality3'] = NULL;
703 } else if ($this->values['nationality1'] == $this->values['nationality2']) {
704 $this->values['nationality2'] = $this->values['nationality3'];
705 $this->values['nationality3'] = NULL;
706 } else if ($this->values['nationality2'] == $this->values['nationality3']
707 || $this->values['nationality1'] == $this->values['nationality3']) {
708 $this->values['nationality3'] = NULL;
709 }
710
ce0b2c6f
FB
711 $new_email = ($this->values['email_directory'] == "new@example.org") ?
712 $this->values['email_directory_new'] : $this->values['email_directory'];
713 if ($new_email == "") {
714 $new_email = NULL;
715 }
8450c2aa 716
e5bcd851
FB
717 XDB::execute("UPDATE profiles
718 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
ce0b2c6f 719 freetext = {?}, freetext_pub = {?}, email_directory = {?}
d1e61677 720 WHERE pid = {?}",
e5bcd851 721 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
87db81e7 722 ProfileSettingDate::toSQLDate($this->values['birthdate']),
ce0b2c6f 723 $this->values['freetext'], $this->values['freetext_pub'], $new_email, $this->pid());
b715c1e1 724 }
a7c28fff 725 if ($this->changed['photo_pub']) {
5c4ea53f 726 XDB::execute("UPDATE profile_photos
a7c28fff 727 SET pub = {?}
5c4ea53f 728 WHERE pid = {?}",
e5bcd851 729 $this->values['photo_pub'], $this->pid());
a7c28fff 730 }
9241c897 731 if (S::user()->isMe($this->owner) && $this->changed['yourself']) {
cb0af6e5
FB
732 if ($this->owner) {
733 XDB::execute('UPDATE accounts
734 SET display_name = {?}
735 WHERE uid = {?}',
736 $this->values['yourself'], $this->owner->id());
737 }
738 XDB::execute('UPDATE profile_display
739 SET yourself = {?}
740 WHERE pid = {?}', $this->values['yourself'],
741 $this->pid());
fb2c09c9 742 }
7e233317 743 if ($this->changed['promo_display']) {
86ced4d4 744 if ($this->values['promo_display']{0} == $this->profile->mainEducation()) {
7733ade1
SJ
745 $yearpromo = intval(substr($this->values['promo_display'], 1, 4));
746 if (($this->profile->mainEducation() == 'X' && $yearpromo >= $this->profile->entry_year)
86ced4d4 747 || ($this->profile->mainEducation() != 'X'
7733ade1 748 && $yearpromo >= $this->profile->entry_year + $this->profile->mainEducationDuration())) {
86ced4d4
SJ
749 XDB::execute('UPDATE profile_display
750 SET promo = {?}
751 WHERE pid = {?}',
752 $this->values['promo_display'], $this->pid());
7733ade1
SJ
753 XDB::execute('UPDATE profile_education
754 SET promo_year = {?}
755 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
756 $yearpromo, $this->pid());
86ced4d4 757 }
7e233317 758 }
b04882ff 759 }
f7190088 760 if (!S::user()->isMe($this->owner) && $this->changed['deathdate']) {
87db81e7
FB
761 XDB::execute('UPDATE profiles
762 SET deathdate = {?}, deathdate_rec = NOW()
763 WHERE pid = {?} AND deathdate_rec IS NULL',
764 ProfileSettingDate::toSQLDate($this->values['deathdate']), $this->pid());
765 if (XDB::affectedRows() > 0) {
766 $this->profile->clear();
767 if ($this->owner) {
768 $this->owner->clear(true);
769 }
770 } else {
771 /* deathdate_rec was not NULL, this is just an update of the death date
772 */
773 XDB::execute('UPDATE profiles
774 SET deathdate = {?}
775 WHERE pid = {?}',
776 ProfileSettingDate::toSQLDate($this->values['deathdate']), $this->pid());
777 }
778 }
fd38b30e
FB
779 }
780
26ba053e 781 public function _prepare(PlPage $page, $id)
fd38b30e 782 {
f711b03f 783 require_once "education.func.inc.php";
d1a2252a 784
111b2736
FB
785 $res = XDB::query("SELECT id, field
786 FROM profile_education_field_enum
787 ORDER BY field");
043bbacf
SJ
788 $page->assign('edu_fields', $res->fetchAllAssoc());
789
b715c1e1 790 require_once "emails.combobox.inc.php";
17c6e7bb 791 fill_email_combobox($page, array('source', 'redirect', 'job', 'directory'), $this->owner);
b715c1e1 792
1f5cd004 793 $res = XDB::query("SELECT nw.nwid AS type, nw.name
111b2736
FB
794 FROM profile_networking_enum AS nw
795 ORDER BY name");
d1a2252a 796 $page->assign('network_list', $res->fetchAllAssoc());
c4b45374
SJ
797
798 $res = XDB::query("SELECT public_name, private_name
799 FROM profile_display
800 WHERE pid = {?}",
111b2736 801 $this->pid());
c4b45374
SJ
802 $res = $res->fetchOneRow();
803 $page->assign('public_name', $res[0]);
804 $page->assign('private_name', $res[1]);
e8a7cf31 805 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
fd38b30e
FB
806 }
807}
808
809// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
810?>