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