Merge commit 'origin/master' into fusionax
[platal.git] / modules / profile / general.inc.php
CommitLineData
fd38b30e
FB
1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 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
c4b45374 22class ProfileSearchNames implements ProfileSetting
fd38b30e 23{
dced83b4
SJ
24 private $private_name_end;
25 private $search_names;
c4b45374 26
fd38b30e 27 private function matchWord($old, $new, $newLen) {
93553cea 28 return ($i = strpos($old, $new)) !== false
fd38b30e
FB
29 && ($i == 0 || $old{$i-1} == ' ')
30 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
31 }
32
33 private function prepareField($value)
34 {
a953f7e7 35 $value = mb_strtoupper($value);
fd38b30e
FB
36 return preg_replace('/[^A-Z]/', ' ', $value);
37 }
38
c4b45374 39 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
fd38b30e
FB
40 {
41 $success = true;
dced83b4
SJ
42 $ini = $this->prepareField($init);
43 $new = $this->prepareField($value);
c4b45374
SJ
44 $newLen = strlen($new);
45 $success = $this->matchWord($ini, $new, $newLen)
6cb58d39 46 || ($field == 'lastname' && $new == 'DE ' . $ini);
fd38b30e 47 if (!$success) {
c4b45374
SJ
48 $field = strtolower($field);
49 Platal::page()->trigError("Le " . $field . " que tu as choisi (" . $value .
50 ") est trop loin de ton " . $field . " initial (" . $init . ").");
fd38b30e 51 }
c4b45374 52 return $success ? $value : $init;
fd38b30e
FB
53 }
54
c4b45374 55 public function value(ProfilePage &$page, $field, $value, &$success)
fd38b30e 56 {
dced83b4 57 $success = true;
c4b45374 58 $success_tmp = true;
09f9ebea 59
c4b45374
SJ
60 if (is_null($value)) {
61 $sn_all = XDB::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
6cb58d39 62 sn.particle, sn.typeid, e.type, e.name AS type_name,
c4b45374
SJ
63 FIND_IN_SET('has_particle', e.flags) AS has_particle,
64 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
65 FIND_IN_SET('public', e.flags) AS pub
97a98687
SJ
66 FROM profile_name AS sn
67 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
c4b45374
SJ
68 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
69 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
70 S::v('uid'));
71
6cb58d39
SJ
72 $sn_types = XDB::iterator("SELECT id, type, name,
73 FIND_IN_SET('has_particle', flags) AS has_particle
97a98687 74 FROM profile_name_enum
c4b45374
SJ
75 WHERE NOT FIND_IN_SET('not_displayed', flags)
76 AND FIND_IN_SET('always_displayed', flags)
77 ORDER BY id");
78
79 $value = array();
80 $sn = $sn_all->next();
81 while ($sn_type = $sn_types->next()) {
82 if ($sn_type['id'] == $sn['typeid']) {
83 $value[] = $sn;
84 $sn = $sn_all->next();
85 } else {
09f9ebea
SJ
86 $value[] = array('name' => '',
87 'particle' => '',
88 'typeid' => $sn_type['id'],
89 'type' => $sn_type['type'],
90 'type_name' => $sn_type['name'],
c4b45374 91 'has_particle' => $sn_type['has_particle'],
09f9ebea
SJ
92 'always_displayed' => 1,
93 'pub' => 1);
c4b45374
SJ
94 }
95 }
6cb58d39
SJ
96 if ($sn) {
97 do {
98 $value[] = $sn;
99 } while ($sn = $sn_all->next());
100 }
c4b45374 101 } else {
6cb58d39
SJ
102 require_once 'name.func.inc.php';
103
c4b45374 104 $res = XDB::query("SELECT s.particle, s.name
97a98687
SJ
105 FROM profile_name AS s
106 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
6cb58d39
SJ
107 WHERE s.pid = {?} AND e.type LIKE '%ini'
108 ORDER BY e.type = 'firstname_ini'",
c4b45374
SJ
109 S::i('uid'));
110 $res = $res->fetchAllAssoc();
111 $initial = array();
6cb58d39
SJ
112 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
113 $initial['firstname'] = $res[1]['name'];
114 $sn_types = build_types();
dced83b4
SJ
115 $this->search_names = array();
116 foreach ($value as &$sn) {
c4b45374 117 $sn['name'] = trim($sn['name']);
6cb58d39 118 if ($sn['type'] == 'firstname' || $sn['type'] == 'lastname') {
c4b45374
SJ
119 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
120 $initial[$sn['type']], $success_tmp);
121 $success = $success && $success_tmp;
122 }
dced83b4
SJ
123 if ($sn['pub']) {
124 if (isset($sn['particle']) && ($sn['particle'] != '')) {
125 list($particle, $name) = explode(' ', $sn['name'], 2);
126 $particle = trim($particle) . ' ';
127 if (!$name) {
128 list($particle, $name) = explode('\'', $sn['name'], 2);
129 $particle = trim($particle);
130 }
131 } else {
132 $particle = '';
133 $name = $sn['name'];
134 }
135 }
c4b45374 136 if ($sn['name'] != '') {
dced83b4
SJ
137 if ($sn['pub']) {
138 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
139 'name' => $name,
140 'particle' => $particle,
141 'pub' => $sn['pub']);
c4b45374 142 } else {
dced83b4
SJ
143 if (isset($this->search_names[$sn['typeid']])) {
144 $this->search_names[$sn['typeid']][] = $sn['name'];
145 } else {
146 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']);
147 }
6cb58d39 148 $sn['type_name'] = $sn_types[$sn['typeid']];
c4b45374
SJ
149 }
150 }
151 }
dced83b4
SJ
152 $res = XDB::query("SELECT public_name, private_name
153 FROM profile_display
154 WHERE pid = {?}",
155 S::v('uid'));
156 list($public_name, $private_name) = $res->fetchOneRow();
157 if ($success) {
dced83b4
SJ
158 $sn_types_private = build_types('private');
159 $this->private_name_end = build_private_name($this->search_names, $sn_types_private);
160 $private_name = $public_name . $this->private_name_end;
161 }
162 Platal::page()->assign('public_name', $public_name);
163 Platal::page()->assign('private_name', $private_name);
c4b45374 164 }
09f9ebea 165
c4b45374
SJ
166 return $value;
167 }
168
169 public function save(ProfilePage &$page, $field, $value)
170 {
dced83b4
SJ
171 require_once 'name.func.inc.php';
172 $sn_old = build_sn_pub();
c4b45374 173 XDB::execute("DELETE FROM s
97a98687
SJ
174 USING profile_name AS s
175 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
c4b45374
SJ
176 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
177 S::i('uid'));
dced83b4
SJ
178 $has_new = set_alias_names($this->search_names, $sn_old);
179
180 // Only requires validation if modification in public names
181 if ($has_new) {
182 $new_names = new NamesReq(S::user(), $this->search_names, $this->private_name_end);
183 $new_names->submit();
9f3c170a 184 Platal::page()->trigWarning("La demande de modification de tes noms a bien été prise en compte." .
dced83b4
SJ
185 " Tu recevras un email dès que ces changements auront été effectués.");
186 } else {
187 $display_names = array();
188 build_display_names($display_names, $this->search_names, $this->private_name_end);
189 set_profile_display($display_names);
c4b45374 190 }
fd38b30e
FB
191 }
192}
193
043bbacf 194class ProfileEdu implements ProfileSetting
576777d7 195{
b814a8b8 196 public function __construct(){}
043bbacf 197
5ba8842e 198 static function sortByGradYear($line1, $line2) {
22f0a0a8
SJ
199 $a = (int) $line1['grad_year'];
200 $b = (int) $line2['grad_year'];
201 if ($a == $b) {
5ba8842e
SJ
202 return 0;
203 }
22f0a0a8 204 return ($a < $b) ? -1 : 1;
5ba8842e
SJ
205 }
206
576777d7
FB
207 public function value(ProfilePage &$page, $field, $value, &$success)
208 {
209 $success = true;
043bbacf
SJ
210 if (is_null($value) || !is_array($value)) {
211 $value = array();
1504ac45 212 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
043bbacf 213 FROM profile_education
fb2c09c9 214 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
043bbacf
SJ
215 ORDER BY id",
216 S::v('uid'));
217 while($edu = $res->next()) {
218 $value[] = $edu;
219 }
220 } else {
221 $i = 0;
222 foreach ($value as $key=>&$edu) {
223 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
224 Platal::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
225 $edu['error'] = true;
226 $success = false;
227 }
228 if ($key != $i) {
229 $value[$i] = $edu;
230 unset($value[$key]);
231 }
232 $i++;
233 }
5ba8842e 234 usort($value, array("ProfileEdu", "sortByGradYear"));
576777d7
FB
235 }
236 return $value;
237 }
238
043bbacf 239 public function save(ProfilePage &$page, $field, $value)
576777d7 240 {
043bbacf 241 XDB::execute("DELETE FROM profile_education
fb2c09c9 242 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
043bbacf
SJ
243 S::i('uid'));
244 foreach ($value as $eduid=>&$edu) {
245 if ($edu['eduid'] != '') {
246 XDB::execute("INSERT INTO profile_education
1504ac45
SJ
247 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
248 fieldid = {?}, grad_year = {?}, program = {?}",
249 $eduid, S::i('uid'), $edu['eduid'], $edu['degreeid'],
250 $edu['fieldid'], $edu['grad_year'], $edu['program']);
043bbacf 251 }
576777d7
FB
252 }
253 }
254}
255
b715c1e1
SJ
256class ProfileEmailDirectory implements ProfileSetting
257{
b814a8b8
SJ
258 public function __construct(){}
259 public function save(ProfilePage &$page, $field, $value){}
b715c1e1
SJ
260
261 public function value(ProfilePage &$page, $field, $value, &$success)
262 {
ad3fee9d 263 $p = Platal::page();
b715c1e1
SJ
264
265 $success = true;
266 if (!is_null($value)) {
267 $email_stripped = strtolower(trim($value));
ad3fee9d
SJ
268 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
269 $p->assign('email_error', '1');
270 $p->assign('email_directory_error', $email_stripped);
271 $p->trigError('Adresse Email invalide');
b715c1e1
SJ
272 $success = false;
273 } else {
ad3fee9d 274 $p->assign('email_error', '0');
b715c1e1
SJ
275 }
276 }
277 return $value;
278 }
b715c1e1
SJ
279}
280
d1a2252a
GB
281class ProfileNetworking implements ProfileSetting
282{
283 private $email;
284 private $pub;
285 private $web;
92446a53 286 private $number;
d1a2252a
GB
287
288 public function __construct()
289 {
290 $this->email = new ProfileEmail();
291 $this->pub = new ProfilePub();
292 $this->web = new ProfileWeb();
92446a53 293 $this->number = new ProfileNumber();
d1a2252a
GB
294 }
295
296 public function value(ProfilePage &$page, $field, $value, &$success)
297 {
298 if (is_null($value)) {
299 $value = array();
300 $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
301 FROM profile_networking AS n
302 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
303 WHERE n.uid = {?}",
304 S::i('uid'));
305 while($network = $res->next()) {
306 $value[] = $network;
307 }
308 }
309 if (!is_array($value)) {
310 $value = array();
311 }
b715c1e1
SJ
312 $res = XDB::iterator("SELECT filter, network_type AS type
313 FROM profile_networking_enum;");
d1a2252a
GB
314 $filters = array();
315 while($filter = $res->next()) {
316 $filters[$filter['type']] = $filter['filter'];
317 }
318 $success = true;
319 foreach($value as $i=>&$network) {
92c3f9e5
GB
320 if (!trim($network['address'])) {
321 unset($value[$i]);
322 } else {
323 if (!isset($network['pub'])) {
324 $network['pub'] = 'private';
325 }
326 $network['error'] = false;
327 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
328 $s = true;
329 if ($filters[$network['type']] == 'web') {
330 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
331 } elseif ($filters[$network['type']] == 'email') {
332 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
333 } elseif ($filters[$network['type']] == 'number') {
334 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
335 }
336 if (!$s) {
337 $success = false;
338 $network['error'] = true;
339 }
d1a2252a
GB
340 }
341 }
342 return $value;
343 }
344
345 public function save(ProfilePage &$page, $field, $value)
346 {
347 XDB::execute("DELETE FROM profile_networking
348 WHERE uid = {?}",
349 S::i('uid'));
350 if (!count($value)) {
351 return;
352 }
353 $insert = array();
354 foreach ($value as $id=>$network) {
355 XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
356 VALUES ({?}, {?}, {?}, {?}, {?})",
357 S::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
358 }
359 }
360}
361
fd38b30e
FB
362class ProfileGeneral extends ProfilePage
363{
364 protected $pg_template = 'profile/general.tpl';
365
366 public function __construct(PlWizard &$wiz)
367 {
368 parent::__construct($wiz);
c4b45374
SJ
369 $this->settings['search_names']
370 = new ProfileSearchNames();
62835757
SJ
371 $this->settings['naissance']
372 = new ProfileDate();
bde2be3b 373 $this->settings['freetext_pub']
576777d7 374 = $this->settings['photo_pub']
93553cea
FB
375 = new ProfilePub();
376 $this->settings['freetext']
576777d7 377 = $this->settings['nationalite']
8450c2aa
SJ
378 = $this->settings['nationalite2']
379 = $this->settings['nationalite3']
b04882ff 380 = $this->settings['yourself']
62835757 381 = $this->settings['promo']
93553cea 382 = null;
b715c1e1
SJ
383 $this->settings['email_directory']
384 = new ProfileEmail();
385 $this->settings['email_directory_new']
386 = new ProfileEmailDirectory();
d1a2252a 387 $this->settings['networking'] = new ProfileNetworking();
043bbacf
SJ
388 $this->settings['tels'] = new ProfilePhones('user', 0);
389 $this->settings['edus'] = new ProfileEdu();
6e32823c 390 $this->watched= array('freetext' => true, 'tels' => true,
043bbacf 391 'networking' => true, 'edus' => true,
8450c2aa 392 'nationalite' => true, 'nationalite2' => true,
981d6bb2 393 'nationalite3' => true, 'search_names' => true);
93553cea
FB
394 }
395
7c2e0f0d 396 protected function _fetchData()
93553cea 397 {
576777d7 398 // Checkout all data...
62835757 399 $res = XDB::query("SELECT p.promo, e.entry_year AS entry_year, e.grad_year AS grad_year,
6e32823c 400 u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
5e4417a9 401 t.display_tel as mobile, t.pub as mobile_pub,
b715c1e1 402 d.email_directory as email_directory,
93553cea 403 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
fb5a54cb 404 u.matricule_ax, p.yourself
b715c1e1 405 FROM auth_user_md5 AS u
fb2c09c9 406 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
d1319488 407 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
fb2c09c9
SJ
408 INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags))
409 LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user')
410 LEFT JOIN profile_directory AS d ON (d.uid = u.user_id)
93553cea
FB
411 WHERE u.user_id = {?}", S::v('uid', -1));
412 $this->values = $res->fetchOneAssoc();
576777d7 413
576777d7
FB
414 // Retreive photo informations
415 $res = XDB::query("SELECT pub
416 FROM photo
417 WHERE uid = {?}", S::v('uid'));
418 $this->values['photo_pub'] = $res->fetchOneCell();
419
420 $res = XDB::query("SELECT COUNT(*)
421 FROM requests
422 WHERE type='photo' AND user_id = {?}",
423 S::v('uid'));
424 $this->values['nouvellephoto'] = $res->fetchOneCell();
b715c1e1 425
62835757 426 // Proposes choice for promotion
fb2c09c9
SJ
427 if ($this->values['entry_year'] != $this->values['grad_year'] - 3) {
428 for ($i = $this->values['entry_year']; $i < $this->values['grad_year'] - 2; $i++) {
429 $this->values['promo_choice'][] = "X" . $i;
430 }
431 }
93553cea
FB
432 }
433
7c2e0f0d 434 protected function _saveData()
93553cea 435 {
8450c2aa 436 if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3']
6e32823c 437 || $this->changed['naissance']) {
8450c2aa
SJ
438 if ($this->values['nationalite3'] == "") {
439 $this->values['nationalite3'] = NULL;
440 }
441 if ($this->values['nationalite2'] == "") {
442 $this->values['nationalite2'] = $this->values['nationalite3'];
443 $this->values['nationalite3'] = NULL;
444 }
445 if ($this->values['nationalite'] == "") {
446 $this->values['nationalite'] = $this->values['nationalite2'];
447 $this->values['nationalite2'] = $this->values['nationalite3'];
448 $this->values['nationalite3'] = NULL;
449 }
450
fb2c09c9 451 XDB::execute("UPDATE auth_user_md5
6e32823c 452 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, naissance={?}
fb2c09c9 453 WHERE user_id = {?}",
6e32823c
SJ
454 $this->values['nationalite'], $this->values['nationalite2'], $this->values['nationalite3'],
455 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
456 S::v('uid'));
576777d7 457 }
fb5a54cb 458 if ($this->changed['freetext'] || $this->changed['freetext_pub']) {
576777d7 459 XDB::execute("UPDATE auth_user_quick
fb5a54cb 460 SET profile_freetext={?}, profile_freetext_pub={?}
6e32823c 461 WHERE user_id = {?}",
fb5a54cb 462 $this->values['freetext'], $this->values['freetext_pub'], S::v('uid'));
576777d7 463 }
b715c1e1 464 if ($this->changed['email_directory']) {
6ea6d9c1 465 $new_email = ($this->values['email_directory'] == "new@example.org") ?
b715c1e1 466 $this->values['email_directory_new'] : $this->values['email_directory'];
039ed1a1
SJ
467 if ($new_email == "") {
468 $new_email = NULL;
469 }
b715c1e1
SJ
470 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
471 VALUES ({?}, {?})",
472 S::v('uid'), $new_email);
473 }
a7c28fff
FB
474 if ($this->changed['photo_pub']) {
475 XDB::execute("UPDATE photo
476 SET pub = {?}
477 WHERE uid = {?}",
478 $this->values['photo_pub'], S::v('uid'));
479 }
c4b45374
SJ
480 if ($this->changed['yourself']) {
481 XDB::execute("UPDATE profile_display
482 SET yourself = {?}
483 WHERE pid = {?}",
484 $this->values['yourself'], S::v('uid'));
fb2c09c9 485 }
62835757 486 if ($this->changed['promo']) {
fb2c09c9 487 XDB::execute("UPDATE profile_display
d1319488
FB
488 SET promo = {?}
489 WHERE pid = {?}",
62835757 490 $this->values['promo'], S::v('uid'));
b04882ff 491 }
fd38b30e
FB
492 }
493
04334c61 494 public function _prepare(PlPage &$page, $id)
fd38b30e 495 {
f711b03f 496 require_once "education.func.inc.php";
d1a2252a 497
043bbacf
SJ
498 $res = XDB::iterator("SELECT id, field
499 FROM profile_education_field_enum
500 ORDER BY field");
501 $page->assign('edu_fields', $res->fetchAllAssoc());
502
b715c1e1
SJ
503 require_once "emails.combobox.inc.php";
504 fill_email_combobox($page);
505
506 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
507 FROM profile_networking_enum AS nw
508 ORDER BY name");
d1a2252a 509 $page->assign('network_list', $res->fetchAllAssoc());
c4b45374
SJ
510
511 $res = XDB::query("SELECT public_name, private_name
512 FROM profile_display
513 WHERE pid = {?}",
514 S::v('uid'));
515 $res = $res->fetchOneRow();
516 $page->assign('public_name', $res[0]);
517 $page->assign('private_name', $res[1]);
fd38b30e
FB
518 }
519}
520
521// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
522?>