3b26dddee408e10b20f8990f51f64e052c1155a6
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
25 private $private_name;
26 private $directory_name;
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} == ' ');
36 private function prepareField($value)
38 $value = strtoupper(replace_accent($value));
39 return preg_replace('/[^A-Z]/', ' ', $value);
42 private function prepare(ProfilePage
&$page, $field, $value, $init, &$success)
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);
51 $field = strtolower($field);
52 Platal
::page()->trigError("Le " . $field . " que tu as choisi (" . $value .
53 ") est trop loin de ton " . $field . " initial (" . $init . ").");
55 return $success ?
$value : $init;
58 public function value(ProfilePage
&$page, $field, $value, &$success)
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",
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)
81 $sn = $sn_all->next();
82 while ($sn_type = $sn_types->next()) {
83 if ($sn_type['id'] == $sn['typeid']) {
85 $sn = $sn_all->next();
87 $value[] = array('typeid' => $sn_type['id'],
88 'type' => $sn_type['name'],
90 'has_particle' => $sn_type['has_particle'],
91 'always_displayed' => 1);
96 } while ($sn = $sn_all->next());
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'",
104 $res = $res->fetchAllAssoc();
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;
116 if ($sn['name'] != '') {
117 if (!isset($search_names[$sn['typeid']])) {
118 $search_names[$sn['typeid']] = array($sn['name'], $name);
120 $search_names[$sn['typeid']] = array_merge($search_names[$sn['typeid']], array($name));
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
);
139 public function save(ProfilePage
&$page, $field, $value)
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)",
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) . ' ';
152 list($particle, $name) = explode('\'', $sn['name'], 2);
153 $particle = trim($particle);
160 XDB
::execute("INSERT INTO profile_name_search (particle, name, typeid, pid)
161 VALUES ({?}, {?}, {?}, {?})",
162 $particle, $name, $sn['typeid'], S
::i('uid'));
165 XDB
::execute("UPDATE profile_display
166 SET public_name = {?}, private_name = {?},
167 directory_name = {?}, short_name = {?}, sort_name = {?}
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'));*/
176 class ProfileEdu
implements ProfileSetting
178 public function __construct(){}
180 static function sortByGradYear($line1, $line2) {
181 $a = (int) $line1['grad_year'];
182 $b = (int) $line2['grad_year'];
186 return ($a < $b) ?
-1 : 1;
189 public function value(ProfilePage
&$page, $field, $value, &$success)
192 if (is_null($value) ||
!is_array($value)) {
194 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
195 FROM profile_education
196 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
199 while($edu = $res->next()) {
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
;
216 usort($value, array("ProfileEdu", "sortByGradYear"));
221 public function save(ProfilePage
&$page, $field, $value)
223 XDB
::execute("DELETE FROM profile_education
224 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
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']);
238 class ProfileEmailDirectory
implements ProfileSetting
240 public function __construct(){}
241 public function save(ProfilePage
&$page, $field, $value){}
243 public function value(ProfilePage
&$page, $field, $value, &$success)
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');
256 $p->assign('email_error', '0');
263 class ProfileNetworking
implements ProfileSetting
270 public function __construct()
272 $this->email
= new ProfileEmail();
273 $this->pub
= new ProfilePub();
274 $this->web
= new ProfileWeb();
275 $this->number
= new ProfileNumber();
278 public function value(ProfilePage
&$page, $field, $value, &$success)
280 if (is_null($value)) {
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)
287 while($network = $res->next()) {
291 if (!is_array($value)) {
294 $res = XDB
::iterator("SELECT filter, network_type AS type
295 FROM profile_networking_enum;");
297 while($filter = $res->next()) {
298 $filters[$filter['type']] = $filter['filter'];
301 foreach($value as $i=>&$network) {
302 if (!trim($network['address'])) {
305 if (!isset($network['pub'])) {
306 $network['pub'] = 'private';
308 $network['error'] = false
;
309 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
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);
320 $network['error'] = true
;
327 public function save(ProfilePage
&$page, $field, $value)
329 XDB
::execute("DELETE FROM profile_networking
332 if (!count($value)) {
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']);
344 class ProfileGeneral
extends ProfilePage
346 protected $pg_template = 'profile/general.tpl';
348 public function __construct(PlWizard
&$wiz)
350 parent
::__construct($wiz);
351 $this->settings
['search_names']
352 = new ProfileSearchNames();
353 $this->settings
['naissance']
355 $this->settings
['freetext_pub']
356 = $this->settings
['photo_pub']
358 $this->settings
['freetext']
359 = $this->settings
['nationalite']
360 = $this->settings
['nationalite2']
361 = $this->settings
['nationalite3']
362 = $this->settings
['yourself']
363 = $this->settings
['promo']
365 $this->settings
['synchro_ax']
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
);
380 protected function _fetchData()
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();
398 // Retreive photo informations
399 $res = XDB
::query("SELECT pub
401 WHERE uid = {?}", S
::v('uid'));
402 $this->values
['photo_pub'] = $res->fetchOneCell();
404 $res = XDB
::query("SELECT COUNT(*)
406 WHERE type='photo' AND user_id = {?}",
408 $this->values
['nouvellephoto'] = $res->fetchOneCell();
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;
418 protected function _saveData()
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
;
425 if ($this->values
['nationalite2'] == "") {
426 $this->values
['nationalite2'] = $this->values
['nationalite3'];
427 $this->values
['nationalite3'] = NULL
;
429 if ($this->values
['nationalite'] == "") {
430 $this->values
['nationalite'] = $this->values
['nationalite2'];
431 $this->values
['nationalite2'] = $this->values
['nationalite3'];
432 $this->values
['nationalite3'] = NULL
;
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']),
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'));
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 == "") {
455 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
457 S
::v('uid'), $new_email);
459 if ($this->changed
['photo_pub']) {
460 XDB
::execute("UPDATE photo
463 $this->values
['photo_pub'], S
::v('uid'));
465 if ($this->changed
['yourself']) {
466 XDB
::execute("UPDATE profile_display
469 $this->values
['yourself'], S
::v('uid'));
471 if ($this->changed
['promo']) {
472 XDB
::execute("UPDATE profile_display
475 $this->values
['promo'], S
::v('uid'));
479 public function _prepare(PlPage
&$page, $id)
481 require_once "education.func.inc.php";
483 $res = XDB
::iterator("SELECT id, field
484 FROM profile_education_field_enum
486 $page->assign('edu_fields', $res->fetchAllAssoc());
488 require_once "emails.combobox.inc.php";
489 fill_email_combobox($page);
491 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
492 FROM profile_networking_enum AS nw
494 $page->assign('network_list', $res->fetchAllAssoc());
496 $res = XDB
::query("SELECT public_name, private_name
500 $res = $res->fetchOneRow();
501 $page->assign('public_name', $res[0]);
502 $page->assign('private_name', $res[1]);
506 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: