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