4fd43dcd033277386c2a4cdd18e499241a8c1592
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 == 'lastname' && $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.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",
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)
79 $sn = $sn_all->next();
80 while ($sn_type = $sn_types->next()) {
81 if ($sn_type['id'] == $sn['typeid']) {
83 $sn = $sn_all->next();
85 $value[] = array('typeid' => $sn_type['id'],
86 'type' => $sn_type['type'],
87 'type_name' => $sn_type['name'],
89 'has_particle' => $sn_type['has_particle'],
90 'always_displayed' => 1);
96 } while ($sn = $sn_all->next());
99 require_once 'name.func.inc.php';
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'",
107 $res = $res->fetchAllAssoc();
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;
121 if (isset($sn['particle']) && ($sn['particle'] != '')) {
122 list($particle, $name) = explode(' ', $sn['name'], 2);
123 $particle = trim($particle) . ' ';
125 list($particle, $name) = explode('\'', $sn['name'], 2);
126 $particle = trim($particle);
133 if ($sn['name'] != '') {
135 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name'],
137 'particle' => $particle,
138 'pub' => $sn['pub']);
140 if (isset($this->search_names
[$sn['typeid']])) {
141 $this->search_names
[$sn['typeid']][] = $sn['name'];
143 $this->search_names
[$sn['typeid']] = array('fullname' => $sn['name']);
145 $sn['type_name'] = $sn_types[$sn['typeid']];
149 $res = XDB
::query("SELECT public_name, private_name
153 list($public_name, $private_name) = $res->fetchOneRow();
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
;
159 Platal
::page()->assign('public_name', $public_name);
160 Platal
::page()->assign('private_name', $private_name);
165 public function save(ProfilePage
&$page, $field, $value)
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)",
174 $has_new = set_alias_names($this->search_names
, $sn_old);
176 // Only requires validation if modification in public names
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.");
183 $display_names = array();
184 build_display_names($display_names, $this->search_names
, $this->private_name_end
);
185 set_profile_display($display_names);
190 class ProfileEdu
implements ProfileSetting
192 public function __construct(){}
194 static function sortByGradYear($line1, $line2) {
195 $a = (int) $line1['grad_year'];
196 $b = (int) $line2['grad_year'];
200 return ($a < $b) ?
-1 : 1;
203 public function value(ProfilePage
&$page, $field, $value, &$success)
206 if (is_null($value) ||
!is_array($value)) {
208 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
209 FROM profile_education
210 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
213 while($edu = $res->next()) {
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
;
230 usort($value, array("ProfileEdu", "sortByGradYear"));
235 public function save(ProfilePage
&$page, $field, $value)
237 XDB
::execute("DELETE FROM profile_education
238 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
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']);
252 class ProfileEmailDirectory
implements ProfileSetting
254 public function __construct(){}
255 public function save(ProfilePage
&$page, $field, $value){}
257 public function value(ProfilePage
&$page, $field, $value, &$success)
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');
270 $p->assign('email_error', '0');
277 class ProfileNetworking
implements ProfileSetting
284 public function __construct()
286 $this->email
= new ProfileEmail();
287 $this->pub
= new ProfilePub();
288 $this->web
= new ProfileWeb();
289 $this->number
= new ProfileNumber();
292 public function value(ProfilePage
&$page, $field, $value, &$success)
294 if (is_null($value)) {
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)
301 while($network = $res->next()) {
305 if (!is_array($value)) {
308 $res = XDB
::iterator("SELECT filter, network_type AS type
309 FROM profile_networking_enum;");
311 while($filter = $res->next()) {
312 $filters[$filter['type']] = $filter['filter'];
315 foreach($value as $i=>&$network) {
316 if (!trim($network['address'])) {
319 if (!isset($network['pub'])) {
320 $network['pub'] = 'private';
322 $network['error'] = false
;
323 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
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);
334 $network['error'] = true
;
341 public function save(ProfilePage
&$page, $field, $value)
343 XDB
::execute("DELETE FROM profile_networking
346 if (!count($value)) {
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']);
358 class ProfileGeneral
extends ProfilePage
360 protected $pg_template = 'profile/general.tpl';
362 public function __construct(PlWizard
&$wiz)
364 parent
::__construct($wiz);
365 $this->settings
['search_names']
366 = new ProfileSearchNames();
367 $this->settings
['naissance']
369 $this->settings
['freetext_pub']
370 = $this->settings
['photo_pub']
372 $this->settings
['freetext']
373 = $this->settings
['nationalite']
374 = $this->settings
['nationalite2']
375 = $this->settings
['nationalite3']
376 = $this->settings
['yourself']
377 = $this->settings
['promo']
379 $this->settings
['email_directory']
380 = new ProfileEmail();
381 $this->settings
['email_directory_new']
382 = new ProfileEmailDirectory();
383 $this->settings
['networking'] = new ProfileNetworking();
384 $this->settings
['tels'] = new ProfilePhones('user', 0);
385 $this->settings
['edus'] = new ProfileEdu();
386 $this->watched
= array('freetext' => true
, 'tels' => true
,
387 'networking' => true
, 'edus' => true
,
388 'nationalite' => true
, 'nationalite2' => true
,
389 'nationalite3' => true
, 'search_names' => true
);
392 protected function _fetchData()
394 // Checkout all data...
395 $res = XDB
::query("SELECT p.promo, e.entry_year AS entry_year, e.grad_year AS grad_year,
396 u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
397 t.display_tel as mobile, t.pub as mobile_pub,
398 d.email_directory as email_directory,
399 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
400 u.matricule_ax, p.yourself
401 FROM auth_user_md5 AS u
402 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
403 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
404 INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags))
405 LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user')
406 LEFT JOIN profile_directory AS d ON (d.uid = u.user_id)
407 WHERE u.user_id = {?}", S
::v('uid', -1));
408 $this->values
= $res->fetchOneAssoc();
410 // Retreive photo informations
411 $res = XDB
::query("SELECT pub
413 WHERE uid = {?}", S
::v('uid'));
414 $this->values
['photo_pub'] = $res->fetchOneCell();
416 $res = XDB
::query("SELECT COUNT(*)
418 WHERE type='photo' AND user_id = {?}",
420 $this->values
['nouvellephoto'] = $res->fetchOneCell();
422 // Proposes choice for promotion
423 if ($this->values
['entry_year'] != $this->values
['grad_year'] - 3) {
424 for ($i = $this->values
['entry_year']; $i < $this->values
['grad_year'] - 2; $i++
) {
425 $this->values
['promo_choice'][] = "X" . $i;
430 protected function _saveData()
432 if ($this->changed
['nationalite'] ||
$this->changed
['nationalite2'] ||
$this->changed
['nationalite3']
433 ||
$this->changed
['naissance']) {
434 if ($this->values
['nationalite3'] == "") {
435 $this->values
['nationalite3'] = NULL
;
437 if ($this->values
['nationalite2'] == "") {
438 $this->values
['nationalite2'] = $this->values
['nationalite3'];
439 $this->values
['nationalite3'] = NULL
;
441 if ($this->values
['nationalite'] == "") {
442 $this->values
['nationalite'] = $this->values
['nationalite2'];
443 $this->values
['nationalite2'] = $this->values
['nationalite3'];
444 $this->values
['nationalite3'] = NULL
;
447 XDB
::execute("UPDATE auth_user_md5
448 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, naissance={?}
449 WHERE user_id = {?}",
450 $this->values
['nationalite'], $this->values
['nationalite2'], $this->values
['nationalite3'],
451 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['naissance']),
454 if ($this->changed
['freetext'] ||
$this->changed
['freetext_pub']) {
455 XDB
::execute("UPDATE auth_user_quick
456 SET profile_freetext={?}, profile_freetext_pub={?}
457 WHERE user_id = {?}",
458 $this->values
['freetext'], $this->values
['freetext_pub'], S
::v('uid'));
460 if ($this->changed
['email_directory']) {
461 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
462 $this->values
['email_directory_new'] : $this->values
['email_directory'];
463 if ($new_email == "") {
466 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
468 S
::v('uid'), $new_email);
470 if ($this->changed
['photo_pub']) {
471 XDB
::execute("UPDATE photo
474 $this->values
['photo_pub'], S
::v('uid'));
476 if ($this->changed
['yourself']) {
477 XDB
::execute("UPDATE profile_display
480 $this->values
['yourself'], S
::v('uid'));
482 if ($this->changed
['promo']) {
483 XDB
::execute("UPDATE profile_display
486 $this->values
['promo'], S
::v('uid'));
490 public function _prepare(PlPage
&$page, $id)
492 require_once "education.func.inc.php";
494 $res = XDB
::iterator("SELECT id, field
495 FROM profile_education_field_enum
497 $page->assign('edu_fields', $res->fetchAllAssoc());
499 require_once "emails.combobox.inc.php";
500 fill_email_combobox($page);
502 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
503 FROM profile_networking_enum AS nw
505 $page->assign('network_list', $res->fetchAllAssoc());
507 $res = XDB
::query("SELECT public_name, private_name
511 $res = $res->fetchOneRow();
512 $page->assign('public_name', $res[0]);
513 $page->assign('private_name', $res[1]);
517 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: