Fixes jobs edition, in particular forbids jobs without an entreprise name.
[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($page->pid());
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, $page->pid(), $page->owner->id());
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,
220 $page->profile->isFemale(), $this->private_name_end);
221 set_profile_display($display_names, $page->pid());
222 }
223 }
224 }
225
226 class ProfileSettingEdu implements ProfileSetting
227 {
228 public function __construct() {
229 }
230
231 static function sortByGradYear($line1, $line2) {
232 $a = (int) $line1['grad_year'];
233 $b = (int) $line2['grad_year'];
234 if ($a == $b) {
235 return 0;
236 }
237 return ($a < $b) ? -1 : 1;
238 }
239
240 public function value(ProfilePage &$page, $field, $value, &$success)
241 {
242 $success = true;
243 if (is_null($value) || !is_array($value)) {
244 $value = array();
245 $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program
246 FROM profile_education
247 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)
248 ORDER BY id",
249 $page->pid());
250 } else {
251 $i = 0;
252 foreach ($value as $key=>&$edu) {
253 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
254 Platal::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
255 $edu['error'] = true;
256 $success = false;
257 }
258 if ($key != $i) {
259 $value[$i] = $edu;
260 unset($value[$key]);
261 }
262 $i++;
263 }
264 usort($value, array("ProfileSettingEdu", "sortByGradYear"));
265 }
266 return $value;
267 }
268
269 public function save(ProfilePage &$page, $field, $value)
270 {
271 XDB::execute("DELETE FROM profile_education
272 WHERE pid = {?} AND !FIND_IN_SET('primary', flags)",
273 $page->pid());
274 foreach ($value as $eduid=>&$edu) {
275 if ($edu['eduid'] != '') {
276 XDB::execute("INSERT INTO profile_education
277 SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?},
278 fieldid = {?}, grad_year = {?}, program = {?}",
279 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
280 $edu['fieldid'], $edu['grad_year'], $edu['program']);
281 }
282 }
283 }
284 }
285
286 class ProfileSettingEmailDirectory implements ProfileSetting
287 {
288 public function __construct(){}
289 public function save(ProfilePage &$page, $field, $value){}
290
291 public function value(ProfilePage &$page, $field, $value, &$success)
292 {
293 $p = Platal::page();
294
295 $success = true;
296 if (!is_null($value)) {
297 $email_stripped = strtolower(trim($value));
298 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
299 $p->assign('email_error', '1');
300 $p->assign('email_directory_error', $email_stripped);
301 $p->trigError('Adresse Email invalide');
302 $success = false;
303 } else {
304 $p->assign('email_error', '0');
305 }
306 }
307 return $value;
308 }
309 }
310
311 class ProfileSettingNetworking implements ProfileSetting
312 {
313 private $email;
314 private $pub;
315 private $web;
316 private $number;
317
318 public function __construct()
319 {
320 $this->email = new ProfileSettingEmail();
321 $this->pub = new ProfileSettingPub();
322 $this->web = new ProfileSettingWeb();
323 $this->number = new ProfileSettingNumber();
324 }
325
326 public function value(ProfilePage &$page, $field, $value, &$success)
327 {
328 if (is_null($value)) {
329 $value = XDB::fetchAllAssoc("SELECT n.address, n.network_type AS type, n.pub, m.name
330 FROM profile_networking AS n
331 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
332 WHERE n.pid = {?}",
333 $page->pid());
334 }
335 if (!is_array($value)) {
336 $value = array();
337 }
338 $filters = XDB::fetchAllAssoc('type', 'SELECT filter, network_type AS type
339 FROM profile_networking_enum;');
340 $success = true;
341 foreach($value as $i=>&$network) {
342 if (!trim($network['address'])) {
343 unset($value[$i]);
344 } else {
345 if (!isset($network['pub'])) {
346 $network['pub'] = 'private';
347 }
348 $network['error'] = false;
349 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
350 $s = true;
351 if ($filters[$network['type']] == 'web') {
352 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
353 } elseif ($filters[$network['type']] == 'email') {
354 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
355 } elseif ($filters[$network['type']] == 'number') {
356 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
357 }
358 if (!$s) {
359 $success = false;
360 $network['error'] = true;
361 }
362 }
363 }
364 return $value;
365 }
366
367 public function save(ProfilePage &$page, $field, $value)
368 {
369 XDB::execute("DELETE FROM profile_networking
370 WHERE pid = {?}",
371 $page->pid());
372 if (!count($value)) {
373 return;
374 }
375 $insert = array();
376 foreach ($value as $id=>$network) {
377 XDB::execute("INSERT INTO profile_networking (pid, nwid, network_type, address, pub)
378 VALUES ({?}, {?}, {?}, {?}, {?})",
379 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
380 }
381 }
382 }
383
384 class ProfileSettingPromo implements ProfileSetting
385 {
386 public function __construct(){}
387
388 public function save(ProfilePage &$page, $field, $value)
389 {
390 $gradYearNew = $value;
391 if ($page->profile->mainEducation() == 'X') {
392 $gradYearNew += $page->profile->mainEducationDuration();
393 }
394 if ($value == $page->profile->entry_year + $page->profile->mainEducationDuration()) {
395 XDB::execute('UPDATE profile_display
396 SET promo = {?}
397 WHERE pid = {?}',
398 $page->profile->mainEducation() . strval($value), $page->profile->id());
399 XDB::execute('UPDATE profile_education
400 SET grad_year = {?}
401 WHERE pid = {?} AND FIND_IN_SET(\'primary\', flags)',
402 $gradYearNew, $page->profile->id());
403 $page->trigSuccess('Ton statut "orange" a été supprimé.');
404 } else {
405 require_once 'validations.inc.php';
406
407 $myorange = new OrangeReq(S::user(), $gradYearNew);
408 $myorange->submit();
409 Platal::page()->trigSuccess('Tu pourras changer l\'affichage de ta promotion dès que ta nouvelle promotion aura été validée.');
410 }
411 }
412
413 public function value(ProfilePage &$page, $field, $value, &$success)
414 {
415 $entryYear = $page->profile->entry_year;
416 $gradYear = $page->profile->grad_year;
417 $success = true;
418 if (is_null($value) || $value == $page->profile->yearpromo()) {
419 if ($gradYear != $entryYear + $page->profile->mainEducationDuration()) {
420 $promoChoice = array();
421 for ($i = $entryYear + $page->profile->mainEducationDuration(); $i <= $gradYear; ++$i) {
422 $promoChoice[] = $page->profile->mainEducation() . strval($i);
423 }
424 Platal::page()->assign('promo_choice', $promoChoice);
425 }
426 if ($page->profile->mainEducation() == 'X') {
427 return $page->profile->grad_year - $page->profile->mainEducationDuration();
428 }
429 return $page->profile->yearpromo();
430 }
431
432 // If this profile belongs to an X, $promoNew needs to be changed to
433 // the graduation year.
434 $gradYearNew = $value;
435 if ($page->profile->mainEducation() == 'X') {
436 $gradYearNew += $page->profile->mainEducationDuration();
437 }
438
439 if ($value < 1000 || $value > 9999) {
440 Platal::page()->trigError('L\'année de sortie doit être un nombre de quatre chiffres.');
441 $success = false;
442 } elseif ($gradYearNew < $entryYear + $page->profile->mainEducationDuration()) {
443 Platal::page()->trigError('Trop tôt&nbsp;!');
444 $success = false;
445 }
446 return intval($value);
447 }
448 }
449
450
451 class ProfileSettingGeneral extends ProfilePage
452 {
453 protected $pg_template = 'profile/general.tpl';
454
455 public function __construct(PlWizard &$wiz)
456 {
457 parent::__construct($wiz);
458 $this->settings['search_names']
459 = new ProfileSettingSearchNames();
460 $this->settings['birthdate'] = new ProfileSettingDate();
461 $this->settings['freetext_pub']
462 = $this->settings['photo_pub']
463 = new ProfileSettingPub();
464 $this->settings['freetext']
465 = $this->settings['nationality1']
466 = $this->settings['nationality2']
467 = $this->settings['nationality3']
468 = $this->settings['yourself']
469 = $this->settings['promo_display']
470 = null;
471 $this->settings['email_directory']
472 = new ProfileSettingEmail();
473 $this->settings['email_directory_new']
474 = new ProfileSettingEmailDirectory();
475 $this->settings['networking'] = new ProfileSettingNetworking();
476 $this->settings['tels'] = new ProfileSettingPhones('user', 0);
477 $this->settings['edus'] = new ProfileSettingEdu();
478 $this->settings['promo'] = new ProfileSettingPromo();
479 $this->watched= array('freetext' => true, 'tels' => true,
480 'networking' => true, 'edus' => true,
481 'nationality1' => true, 'nationality2' => true,
482 'nationality3' => true, 'search_names' => true);
483 }
484
485 protected function _fetchData()
486 {
487 // Checkout all data...
488 $res = XDB::query("SELECT p.nationality1, p.nationality2, p.nationality3, p.birthdate,
489 pp.display_tel as mobile, pp.pub as mobile_pub,
490 p.email_directory as email_directory, pd.promo AS promo_display,
491 p.freetext, p.freetext_pub, p.ax_id AS matricule_ax, pd.yourself
492 FROM profiles AS p
493 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
494 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND link_type = 'user')
495 WHERE p.pid = {?}", $this->pid());
496 $this->values = $res->fetchOneAssoc();
497 if ($this->owner) {
498 $this->values['yourself'] = $this->owner->displayName();
499 }
500
501 // Retreive photo informations
502 $res = XDB::query("SELECT pub
503 FROM profile_photos
504 WHERE pid = {?}", $this->pid());
505 $this->values['photo_pub'] = $res->fetchOneCell();
506
507 if ($this->owner) {
508 $res = XDB::query("SELECT COUNT(*)
509 FROM requests
510 WHERE type = 'photo' AND uid = {?}",
511 $this->owner->id());
512 $this->values['nouvellephoto'] = $res->fetchOneCell();
513 } else {
514 $this->values['nouvellephoto'] = 0;
515 }
516 }
517
518 protected function _saveData()
519 {
520 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
521 || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub']
522 || $this->changed['email_directory']) {
523 if ($this->values['nationality3'] == "") {
524 $this->values['nationality3'] = NULL;
525 }
526 if ($this->values['nationality2'] == "") {
527 $this->values['nationality2'] = $this->values['nationality3'];
528 $this->values['nationality3'] = NULL;
529 }
530 if ($this->values['nationality1'] == "") {
531 $this->values['nationality1'] = $this->values['nationality2'];
532 $this->values['nationality2'] = $this->values['nationality3'];
533 $this->values['nationality3'] = NULL;
534 }
535 $new_email = ($this->values['email_directory'] == "new@example.org") ?
536 $this->values['email_directory_new'] : $this->values['email_directory'];
537 if ($new_email == "") {
538 $new_email = NULL;
539 }
540
541 XDB::execute("UPDATE profiles
542 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
543 freetext = {?}, freetext_pub = {?}, email_directory = {?}
544 WHERE pid = {?}",
545 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
546 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['birthdate']),
547 $this->values['freetext'], $this->values['freetext_pub'], $new_email, $this->pid());
548 }
549 if ($this->changed['photo_pub']) {
550 XDB::execute("UPDATE profile_photos
551 SET pub = {?}
552 WHERE pid = {?}",
553 $this->values['photo_pub'], $this->pid());
554 }
555 if ($this->changed['yourself']) {
556 XDB::execute('UPDATE accounts
557 SET display_name = {?}
558 WHERE pid = {?}', $this->pid());
559 }
560 if ($this->changed['promo_display']) {
561 if ($this->values['promo_display']{0} == $this->profile->mainEducation()
562 && intval(substr($this->values['promo_display'], 1, 4)) >= $this->profile->entry_year + $this->profile->mainEducationDuration()) {
563 XDB::execute('UPDATE profile_display
564 SET promo = {?}
565 WHERE pid = {?}',
566 $this->values['promo_display'], $this->pid());
567 }
568 }
569 }
570
571 public function _prepare(PlPage &$page, $id)
572 {
573 require_once "education.func.inc.php";
574
575 $res = XDB::query("SELECT id, field
576 FROM profile_education_field_enum
577 ORDER BY field");
578 $page->assign('edu_fields', $res->fetchAllAssoc());
579
580 require_once "emails.combobox.inc.php";
581 fill_email_combobox($page, $this->owner, $this->profile);
582
583 $res = XDB::query("SELECT nw.network_type AS type, nw.name
584 FROM profile_networking_enum AS nw
585 ORDER BY name");
586 $page->assign('network_list', $res->fetchAllAssoc());
587
588 $res = XDB::query("SELECT public_name, private_name
589 FROM profile_display
590 WHERE pid = {?}",
591 $this->pid());
592 $res = $res->fetchOneRow();
593 $page->assign('public_name', $res[0]);
594 $page->assign('private_name', $res[1]);
595 $page->assign('isFemale', $this->profile->isFemale() ? 1 : 0);
596 }
597 }
598
599 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
600 ?>