Adds verification for 'Prénom' and 'Nom patronymique' so that they remain close enoug...
[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 $public_name;
25 private $private_name;
26 private $directory_name;
27 private $short_name;
28 private $sort_name;
29
30 private function matchWord($old, $new, $newLen) {
31 return ($i = strpos($old, $new)) !== false
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
42 private function prepare(ProfilePage &$page, $field, $value, $init, &$success)
43 {
44 $success = true;
45 $ini = $this->prepareField($init);
46 $new = $this->prepareField($value);
47 $newLen = strlen($new);
48 $success = $this->matchWord($ini, $new, $newLen)
49 || ($field == 'Nom patronymique' && $new == 'DE ' . $ini);
50 if (!$success) {
51 $field = strtolower($field);
52 Platal::page()->trigError("Le " . $field . " que tu as choisi (" . $value .
53 ") est trop loin de ton " . $field . " initial (" . $init . ").");
54 }
55 return $success ? $value : $init;
56 }
57
58 public function value(ProfilePage &$page, $field, $value, &$success)
59 {
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'));*/
173 }
174 }
175
176 class ProfileEdu implements ProfileSetting
177 {
178 public function __construct(){}
179
180 static function sortByGradYear($line1, $line2) {
181 $a = (int) $line1['grad_year'];
182 $b = (int) $line2['grad_year'];
183 if ($a == $b) {
184 return 0;
185 }
186 return ($a < $b) ? -1 : 1;
187 }
188
189 public function value(ProfilePage &$page, $field, $value, &$success)
190 {
191 $success = true;
192 if (is_null($value) || !is_array($value)) {
193 $value = array();
194 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
195 FROM profile_education
196 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
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 }
216 usort($value, array("ProfileEdu", "sortByGradYear"));
217 }
218 return $value;
219 }
220
221 public function save(ProfilePage &$page, $field, $value)
222 {
223 XDB::execute("DELETE FROM profile_education
224 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
225 S::i('uid'));
226 foreach ($value as $eduid=>&$edu) {
227 if ($edu['eduid'] != '') {
228 XDB::execute("INSERT INTO profile_education
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']);
233 }
234 }
235 }
236 }
237
238 class ProfileEmailDirectory implements ProfileSetting
239 {
240 public function __construct(){}
241 public function save(ProfilePage &$page, $field, $value){}
242
243 public function value(ProfilePage &$page, $field, $value, &$success)
244 {
245 $p = Platal::page();
246
247 $success = true;
248 if (!is_null($value)) {
249 $email_stripped = strtolower(trim($value));
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');
254 $success = false;
255 } else {
256 $p->assign('email_error', '0');
257 }
258 }
259 return $value;
260 }
261 }
262
263 class ProfileNetworking implements ProfileSetting
264 {
265 private $email;
266 private $pub;
267 private $web;
268 private $number;
269
270 public function __construct()
271 {
272 $this->email = new ProfileEmail();
273 $this->pub = new ProfilePub();
274 $this->web = new ProfileWeb();
275 $this->number = new ProfileNumber();
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 }
294 $res = XDB::iterator("SELECT filter, network_type AS type
295 FROM profile_networking_enum;");
296 $filters = array();
297 while($filter = $res->next()) {
298 $filters[$filter['type']] = $filter['filter'];
299 }
300 $success = true;
301 foreach($value as $i=>&$network) {
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 }
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
344 class ProfileGeneral extends ProfilePage
345 {
346 protected $pg_template = 'profile/general.tpl';
347
348 public function __construct(PlWizard &$wiz)
349 {
350 parent::__construct($wiz);
351 $this->settings['search_names']
352 = new ProfileSearchNames();
353 $this->settings['naissance']
354 = new ProfileDate();
355 $this->settings['freetext_pub']
356 = $this->settings['photo_pub']
357 = new ProfilePub();
358 $this->settings['freetext']
359 = $this->settings['nationalite']
360 = $this->settings['nationalite2']
361 = $this->settings['nationalite3']
362 = $this->settings['yourself']
363 = $this->settings['promo']
364 = null;
365 $this->settings['synchro_ax']
366 = new ProfileBool();
367 $this->settings['email_directory']
368 = new ProfileEmail();
369 $this->settings['email_directory_new']
370 = new ProfileEmailDirectory();
371 $this->settings['networking'] = new ProfileNetworking();
372 $this->settings['tels'] = new ProfilePhones('user', 0);
373 $this->settings['edus'] = new ProfileEdu();
374 $this->watched= array('freetext' => true, 'tels' => true,
375 'networking' => true, 'edus' => true,
376 'nationalite' => true, 'nationalite2' => true,
377 'nationalite3' => true);
378 }
379
380 protected function _fetchData()
381 {
382 // Checkout all data...
383 $res = XDB::query("SELECT p.promo, e.entry_year AS entry_year, e.grad_year AS grad_year,
384 u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
385 t.display_tel as mobile, t.pub as mobile_pub,
386 d.email_directory as email_directory,
387 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
388 q.profile_from_ax as synchro_ax, u.matricule_ax, p.yourself
389 FROM auth_user_md5 AS u
390 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
391 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
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)
395 WHERE u.user_id = {?}", S::v('uid', -1));
396 $this->values = $res->fetchOneAssoc();
397
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();
409
410 // Proposes choice for promotion
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 }
416 }
417
418 protected function _saveData()
419 {
420 if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3']
421 || $this->changed['naissance']) {
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
435 XDB::execute("UPDATE auth_user_md5
436 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, naissance={?}
437 WHERE user_id = {?}",
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'));
441 }
442 if ($this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
443 XDB::execute("UPDATE auth_user_quick
444 SET profile_freetext={?}, profile_freetext_pub={?}, profile_from_ax = {?}
445 WHERE user_id = {?}",
446 $this->values['freetext'], $this->values['freetext_pub'],
447 $this->values['synchro_ax'], S::v('uid'));
448 }
449 if ($this->changed['email_directory']) {
450 $new_email = ($this->values['email_directory'] == "new@example.org") ?
451 $this->values['email_directory_new'] : $this->values['email_directory'];
452 if ($new_email == "") {
453 $new_email = NULL;
454 }
455 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
456 VALUES ({?}, {?})",
457 S::v('uid'), $new_email);
458 }
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 }
465 if ($this->changed['yourself']) {
466 XDB::execute("UPDATE profile_display
467 SET yourself = {?}
468 WHERE pid = {?}",
469 $this->values['yourself'], S::v('uid'));
470 }
471 if ($this->changed['promo']) {
472 XDB::execute("UPDATE profile_display
473 SET promo = {?}
474 WHERE pid = {?}",
475 $this->values['promo'], S::v('uid'));
476 }
477 }
478
479 public function _prepare(PlPage &$page, $id)
480 {
481 require_once "education.func.inc.php";
482
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
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");
494 $page->assign('network_list', $res->fetchAllAssoc());
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]);
503 }
504 }
505
506 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
507 ?>