Merge commit 'origin/master' into fusionax
[platal.git] / modules / profile / general.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
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
22 class ProfileSearchNames implements ProfileSetting
23 {
24 private $private_name_end;
25 private $search_names;
26
27 private function matchWord($old, $new, $newLen) {
28 return ($i = strpos($old, $new)) !== false
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
39 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
40 {
41 $success = true;
42 $ini = $this->prepareField($init);
43 $new = $this->prepareField($value);
44 $newLen = strlen($new);
45 $success = $this->matchWord($ini, $new, $newLen)
46 || ($field == 'lastname' && $new == 'DE ' . $ini);
47 if (!$success) {
48 $field = strtolower($field);
49 Platal::page()->trigError("Le " . $field . " que tu as choisi (" . $value .
50 ") est trop loin de ton " . $field . " initial (" . $init . ").");
51 }
52 return $success ? $value : $init;
53 }
54
55 public function value(ProfilePage &$page, $field, $value, &$success)
56 {
57 $success = true;
58 $success_tmp = true;
59 if (is_null($value)) {
60 $sn_all = XDB::iterator("SELECT CONCAT(sn.particle, sn.name) AS name,
61 sn.particle, sn.typeid, e.type, e.name AS type_name,
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
65 FROM profile_name AS sn
66 INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid)
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
71 $sn_types = XDB::iterator("SELECT id, type, name,
72 FIND_IN_SET('has_particle', flags) AS has_particle
73 FROM profile_name_enum
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 {
85 $value[] = array('typeid' => $sn_type['id'],
86 'type' => $sn_type['type'],
87 'type_name' => $sn_type['name'],
88 'pub' => 1,
89 'has_particle' => $sn_type['has_particle'],
90 'always_displayed' => 1);
91 }
92 }
93 if ($sn) {
94 do {
95 $value[] = $sn;
96 } while ($sn = $sn_all->next());
97 }
98 } else {
99 require_once 'name.func.inc.php';
100
101 $res = XDB::query("SELECT s.particle, s.name
102 FROM profile_name AS s
103 INNER JOIN profile_name_enum AS e ON (e.id = s.typeid)
104 WHERE s.pid = {?} AND e.type LIKE '%ini'
105 ORDER BY e.type = 'firstname_ini'",
106 S::i('uid'));
107 $res = $res->fetchAllAssoc();
108 $initial = array();
109 $initial['lastname'] = $res[0]['particle'] . $res[0]['name'];
110 $initial['firstname'] = $res[1]['name'];
111 $sn_types = build_types();
112 $this->search_names = array();
113 foreach ($value as &$sn) {
114 $sn['name'] = trim($sn['name']);
115 if ($sn['type'] == 'firstname' || $sn['type'] == 'lastname') {
116 $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'],
117 $initial[$sn['type']], $success_tmp);
118 $success = $success && $success_tmp;
119 }
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 }
133 if ($sn['name'] != '') {
134 if ($sn['pub']) {
135 $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'],
136 'name' => $name,
137 'particle' => $particle,
138 'pub' => $sn['pub']);
139 } else {
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 }
145 $sn['type_name'] = $sn_types[$sn['typeid']];
146 }
147 }
148 }
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) {
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);
161 }
162 return $value;
163 }
164
165 public function save(ProfilePage &$page, $field, $value)
166 {
167 require_once 'name.func.inc.php';
168 $sn_old = build_sn_pub();
169 XDB::execute("DELETE FROM s
170 USING profile_name AS s
171 INNER JOIN profile_name_enum AS e ON (s.typeid = e.id)
172 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
173 S::i('uid'));
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();
180 Platal::page()->trigWarning("La demande de modification de tes noms a bien été prises en compte." .
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);
186 }
187 }
188 }
189
190 class ProfileEdu implements ProfileSetting
191 {
192 public function __construct(){}
193
194 static function sortByGradYear($line1, $line2) {
195 $a = (int) $line1['grad_year'];
196 $b = (int) $line2['grad_year'];
197 if ($a == $b) {
198 return 0;
199 }
200 return ($a < $b) ? -1 : 1;
201 }
202
203 public function value(ProfilePage &$page, $field, $value, &$success)
204 {
205 $success = true;
206 if (is_null($value) || !is_array($value)) {
207 $value = array();
208 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
209 FROM profile_education
210 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
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 }
230 usort($value, array("ProfileEdu", "sortByGradYear"));
231 }
232 return $value;
233 }
234
235 public function save(ProfilePage &$page, $field, $value)
236 {
237 XDB::execute("DELETE FROM profile_education
238 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
239 S::i('uid'));
240 foreach ($value as $eduid=>&$edu) {
241 if ($edu['eduid'] != '') {
242 XDB::execute("INSERT INTO profile_education
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']);
247 }
248 }
249 }
250 }
251
252 class ProfileEmailDirectory implements ProfileSetting
253 {
254 public function __construct(){}
255 public function save(ProfilePage &$page, $field, $value){}
256
257 public function value(ProfilePage &$page, $field, $value, &$success)
258 {
259 $p = Platal::page();
260
261 $success = true;
262 if (!is_null($value)) {
263 $email_stripped = strtolower(trim($value));
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');
268 $success = false;
269 } else {
270 $p->assign('email_error', '0');
271 }
272 }
273 return $value;
274 }
275 }
276
277 class ProfileNetworking implements ProfileSetting
278 {
279 private $email;
280 private $pub;
281 private $web;
282 private $number;
283
284 public function __construct()
285 {
286 $this->email = new ProfileEmail();
287 $this->pub = new ProfilePub();
288 $this->web = new ProfileWeb();
289 $this->number = new ProfileNumber();
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 }
308 $res = XDB::iterator("SELECT filter, network_type AS type
309 FROM profile_networking_enum;");
310 $filters = array();
311 while($filter = $res->next()) {
312 $filters[$filter['type']] = $filter['filter'];
313 }
314 $success = true;
315 foreach($value as $i=>&$network) {
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 }
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
358 class ProfileGeneral extends ProfilePage
359 {
360 protected $pg_template = 'profile/general.tpl';
361
362 public function __construct(PlWizard &$wiz)
363 {
364 parent::__construct($wiz);
365 $this->settings['search_names']
366 = new ProfileSearchNames();
367 $this->settings['naissance']
368 = new ProfileDate();
369 $this->settings['freetext_pub']
370 = $this->settings['photo_pub']
371 = new ProfilePub();
372 $this->settings['freetext']
373 = $this->settings['nationalite']
374 = $this->settings['nationalite2']
375 = $this->settings['nationalite3']
376 = $this->settings['yourself']
377 = $this->settings['promo']
378 = null;
379 $this->settings['synchro_ax']
380 = new ProfileBool();
381 $this->settings['email_directory']
382 = new ProfileEmail();
383 $this->settings['email_directory_new']
384 = new ProfileEmailDirectory();
385 $this->settings['networking'] = new ProfileNetworking();
386 $this->settings['tels'] = new ProfilePhones('user', 0);
387 $this->settings['edus'] = new ProfileEdu();
388 $this->watched= array('freetext' => true, 'tels' => true,
389 'networking' => true, 'edus' => true,
390 'nationalite' => true, 'nationalite2' => true,
391 'nationalite3' => true, 'search_names' => true);
392 }
393
394 protected function _fetchData()
395 {
396 // Checkout all data...
397 $res = XDB::query("SELECT p.promo, e.entry_year AS entry_year, e.grad_year AS grad_year,
398 u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
399 t.display_tel as mobile, t.pub as mobile_pub,
400 d.email_directory as email_directory,
401 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
402 q.profile_from_ax as synchro_ax, u.matricule_ax, p.yourself
403 FROM auth_user_md5 AS u
404 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
405 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
406 INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags))
407 LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user')
408 LEFT JOIN profile_directory AS d ON (d.uid = u.user_id)
409 WHERE u.user_id = {?}", S::v('uid', -1));
410 $this->values = $res->fetchOneAssoc();
411
412 // Retreive photo informations
413 $res = XDB::query("SELECT pub
414 FROM photo
415 WHERE uid = {?}", S::v('uid'));
416 $this->values['photo_pub'] = $res->fetchOneCell();
417
418 $res = XDB::query("SELECT COUNT(*)
419 FROM requests
420 WHERE type='photo' AND user_id = {?}",
421 S::v('uid'));
422 $this->values['nouvellephoto'] = $res->fetchOneCell();
423
424 // Proposes choice for promotion
425 if ($this->values['entry_year'] != $this->values['grad_year'] - 3) {
426 for ($i = $this->values['entry_year']; $i < $this->values['grad_year'] - 2; $i++) {
427 $this->values['promo_choice'][] = "X" . $i;
428 }
429 }
430 }
431
432 protected function _saveData()
433 {
434 if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3']
435 || $this->changed['naissance']) {
436 if ($this->values['nationalite3'] == "") {
437 $this->values['nationalite3'] = NULL;
438 }
439 if ($this->values['nationalite2'] == "") {
440 $this->values['nationalite2'] = $this->values['nationalite3'];
441 $this->values['nationalite3'] = NULL;
442 }
443 if ($this->values['nationalite'] == "") {
444 $this->values['nationalite'] = $this->values['nationalite2'];
445 $this->values['nationalite2'] = $this->values['nationalite3'];
446 $this->values['nationalite3'] = NULL;
447 }
448
449 XDB::execute("UPDATE auth_user_md5
450 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, naissance={?}
451 WHERE user_id = {?}",
452 $this->values['nationalite'], $this->values['nationalite2'], $this->values['nationalite3'],
453 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
454 S::v('uid'));
455 }
456 if ($this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
457 XDB::execute("UPDATE auth_user_quick
458 SET profile_freetext={?}, profile_freetext_pub={?}, profile_from_ax = {?}
459 WHERE user_id = {?}",
460 $this->values['freetext'], $this->values['freetext_pub'],
461 $this->values['synchro_ax'], S::v('uid'));
462 }
463 if ($this->changed['email_directory']) {
464 $new_email = ($this->values['email_directory'] == "new@example.org") ?
465 $this->values['email_directory_new'] : $this->values['email_directory'];
466 if ($new_email == "") {
467 $new_email = NULL;
468 }
469 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
470 VALUES ({?}, {?})",
471 S::v('uid'), $new_email);
472 }
473 if ($this->changed['photo_pub']) {
474 XDB::execute("UPDATE photo
475 SET pub = {?}
476 WHERE uid = {?}",
477 $this->values['photo_pub'], S::v('uid'));
478 }
479 if ($this->changed['yourself']) {
480 XDB::execute("UPDATE profile_display
481 SET yourself = {?}
482 WHERE pid = {?}",
483 $this->values['yourself'], S::v('uid'));
484 }
485 if ($this->changed['promo']) {
486 XDB::execute("UPDATE profile_display
487 SET promo = {?}
488 WHERE pid = {?}",
489 $this->values['promo'], S::v('uid'));
490 }
491 }
492
493 public function _prepare(PlPage &$page, $id)
494 {
495 require_once "education.func.inc.php";
496
497 $res = XDB::iterator("SELECT id, field
498 FROM profile_education_field_enum
499 ORDER BY field");
500 $page->assign('edu_fields', $res->fetchAllAssoc());
501
502 require_once "emails.combobox.inc.php";
503 fill_email_combobox($page);
504
505 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
506 FROM profile_networking_enum AS nw
507 ORDER BY name");
508 $page->assign('network_list', $res->fetchAllAssoc());
509
510 $res = XDB::query("SELECT public_name, private_name
511 FROM profile_display
512 WHERE pid = {?}",
513 S::v('uid'));
514 $res = $res->fetchOneRow();
515 $page->assign('public_name', $res[0]);
516 $page->assign('private_name', $res[1]);
517 }
518 }
519
520 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
521 ?>