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