b5888366d84345d7bbeb1009557be02ada7d8ec8
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileSearchNames
implements ProfileSetting
24 private $private_name_end;
25 private $search_names;
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} == ' ');
33 private function prepareField($value)
35 $value = strtoupper(replace_accent($value));
36 return preg_replace('/[^A-Z]/', ' ', $value);
39 private function prepare(ProfilePage
&$page, $field, $value, $init, &$success)
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);
48 $field = strtolower($field);
49 Platal
::page()->trigError("Le " . $field . " que tu as choisi (" . $value .
50 ") est trop loin de ton " . $field . " initial (" . $init . ").");
52 return $success ?
$value : $init;
55 public function value(ProfilePage
&$page, $field, $value, &$success)
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",
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)
78 $sn = $sn_all->next();
79 while ($sn_type = $sn_types->next()) {
80 if ($sn_type['id'] == $sn['typeid']) {
82 $sn = $sn_all->next();
84 $value[] = array('typeid' => $sn_type['id'],
85 'type' => $sn_type['name'],
87 'has_particle' => $sn_type['has_particle'],
88 'always_displayed' => 1);
93 } while ($sn = $sn_all->next());
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'",
101 $res = $res->fetchAllAssoc();
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;
114 if (isset($sn['particle']) && ($sn['particle'] != '')) {
115 list($particle, $name) = explode(' ', $sn['name'], 2);
116 $particle = trim($particle) . ' ';
118 list($particle, $name) = explode('\'', $sn['name'], 2);
119 $particle = trim($particle);
126 if ($sn['name'] != '') {
128 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
130 'particle' => $particle,
131 'pub' => $sn['pub']);
133 if (isset($this->search_names
[$sn['typeid']])) {
134 $this->search_names
[$sn['typeid']][] = $sn['name'];
136 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
141 $res = XDB
::query("SELECT public_name, private_name
145 list($public_name, $private_name) = $res->fetchOneRow();
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
;
152 Platal
::page()->assign('public_name', $public_name);
153 Platal
::page()->assign('private_name', $private_name);
158 public function save(ProfilePage
&$page, $field, $value)
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)",
167 $has_new = set_alias_names($this->search_names
, $sn_old);
169 // Only requires validation if modification in public names
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.");
176 $display_names = array();
177 build_display_names($display_names, $this->search_names
, $this->private_name_end
);
178 set_profile_display($display_names);
183 class ProfileEdu
implements ProfileSetting
185 public function __construct(){}
187 static function sortByGradYear($line1, $line2) {
188 $a = (int) $line1['grad_year'];
189 $b = (int) $line2['grad_year'];
193 return ($a < $b) ?
-1 : 1;
196 public function value(ProfilePage
&$page, $field, $value, &$success)
199 if (is_null($value) ||
!is_array($value)) {
201 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
202 FROM profile_education
203 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
206 while($edu = $res->next()) {
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
;
223 usort($value, array("ProfileEdu", "sortByGradYear"));
228 public function save(ProfilePage
&$page, $field, $value)
230 XDB
::execute("DELETE FROM profile_education
231 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
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']);
245 class ProfileEmailDirectory
implements ProfileSetting
247 public function __construct(){}
248 public function save(ProfilePage
&$page, $field, $value){}
250 public function value(ProfilePage
&$page, $field, $value, &$success)
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');
263 $p->assign('email_error', '0');
270 class ProfileNetworking
implements ProfileSetting
277 public function __construct()
279 $this->email
= new ProfileEmail();
280 $this->pub
= new ProfilePub();
281 $this->web
= new ProfileWeb();
282 $this->number
= new ProfileNumber();
285 public function value(ProfilePage
&$page, $field, $value, &$success)
287 if (is_null($value)) {
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)
294 while($network = $res->next()) {
298 if (!is_array($value)) {
301 $res = XDB
::iterator("SELECT filter, network_type AS type
302 FROM profile_networking_enum;");
304 while($filter = $res->next()) {
305 $filters[$filter['type']] = $filter['filter'];
308 foreach($value as $i=>&$network) {
309 if (!trim($network['address'])) {
312 if (!isset($network['pub'])) {
313 $network['pub'] = 'private';
315 $network['error'] = false
;
316 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
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);
327 $network['error'] = true
;
334 public function save(ProfilePage
&$page, $field, $value)
336 XDB
::execute("DELETE FROM profile_networking
339 if (!count($value)) {
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']);
351 class ProfileGeneral
extends ProfilePage
353 protected $pg_template = 'profile/general.tpl';
355 public function __construct(PlWizard
&$wiz)
357 parent
::__construct($wiz);
358 $this->settings
['search_names']
359 = new ProfileSearchNames();
360 $this->settings
['naissance']
362 $this->settings
['freetext_pub']
363 = $this->settings
['photo_pub']
365 $this->settings
['freetext']
366 = $this->settings
['nationalite']
367 = $this->settings
['nationalite2']
368 = $this->settings
['nationalite3']
369 = $this->settings
['yourself']
370 = $this->settings
['promo']
372 $this->settings
['synchro_ax']
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
);
387 protected function _fetchData()
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();
405 // Retreive photo informations
406 $res = XDB
::query("SELECT pub
408 WHERE uid = {?}", S
::v('uid'));
409 $this->values
['photo_pub'] = $res->fetchOneCell();
411 $res = XDB
::query("SELECT COUNT(*)
413 WHERE type='photo' AND user_id = {?}",
415 $this->values
['nouvellephoto'] = $res->fetchOneCell();
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;
425 protected function _saveData()
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
;
432 if ($this->values
['nationalite2'] == "") {
433 $this->values
['nationalite2'] = $this->values
['nationalite3'];
434 $this->values
['nationalite3'] = NULL
;
436 if ($this->values
['nationalite'] == "") {
437 $this->values
['nationalite'] = $this->values
['nationalite2'];
438 $this->values
['nationalite2'] = $this->values
['nationalite3'];
439 $this->values
['nationalite3'] = NULL
;
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']),
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'));
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 == "") {
462 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
464 S
::v('uid'), $new_email);
466 if ($this->changed
['photo_pub']) {
467 XDB
::execute("UPDATE photo
470 $this->values
['photo_pub'], S
::v('uid'));
472 if ($this->changed
['yourself']) {
473 XDB
::execute("UPDATE profile_display
476 $this->values
['yourself'], S
::v('uid'));
478 if ($this->changed
['promo']) {
479 XDB
::execute("UPDATE profile_display
482 $this->values
['promo'], S
::v('uid'));
486 public function _prepare(PlPage
&$page, $id)
488 require_once "education.func.inc.php";
490 $res = XDB
::iterator("SELECT id, field
491 FROM profile_education_field_enum
493 $page->assign('edu_fields', $res->fetchAllAssoc());
495 require_once "emails.combobox.inc.php";
496 fill_email_combobox($page);
498 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
499 FROM profile_networking_enum AS nw
501 $page->assign('network_list', $res->fetchAllAssoc());
503 $res = XDB
::query("SELECT public_name, private_name
507 $res = $res->fetchOneRow();
508 $page->assign('public_name', $res[0]);
509 $page->assign('private_name', $res[1]);
513 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: