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