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