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