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 ProfileNom
implements ProfileSetting
24 private function matchWord($old, $new, $newLen) {
25 return ($i = strpos($old, $new)) !== false
26 && ($i == 0 ||
$old{$i-1} == ' ')
27 && ($i +
$newLen == strlen($old) ||
$old{$i +
$newLen} == ' ');
30 private function prepareField($value)
32 $value = strtoupper(replace_accent($value));
33 return preg_replace('/[^A-Z]/', ' ', $value);
36 public function value(ProfilePage
&$page, $field, $value, &$success)
39 $current = S
::v($field);
40 $init = S
::v($field . '_ini');
41 if (is_null($value)) {
44 if ($value == $current ||
$value == $init) {
47 $ini = $this->prepareField($init);
48 $old = $this->prepareField($current);
49 $new = $this->prepareField($value);
50 $newLen = strlen($new);
51 $success = $this->matchWord($old, $new, $newLen)
52 ||
$this->matchWord($ini, $new, $newLen)
53 ||
($field == 'nom' && $new == 'DE ' . $old);
55 Platal
::page()->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
56 . (($init == $current)?
"" : " et de ton prénom précédent ($current)"));
58 return $success ?
$value : $current;
61 public function save(ProfilePage
&$page, $field, $new_value)
63 $_SESSION[$field] = $new_value;
67 class ProfileEdu
implements ProfileSetting
69 public function __construct(){}
71 static function sortByGradYear($line1, $line2) {
72 $a = (int) $line1['grad_year'];
73 $b = (int) $line2['grad_year'];
77 return ($a < $b) ?
-1 : 1;
80 public function value(ProfilePage
&$page, $field, $value, &$success)
83 if (is_null($value) ||
!is_array($value)) {
85 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
86 FROM profile_education
87 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
90 while($edu = $res->next()) {
95 foreach ($value as $key=>&$edu) {
96 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
97 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
107 usort($value, array("ProfileEdu", "sortByGradYear"));
112 public function save(ProfilePage
&$page, $field, $value)
114 XDB
::execute("DELETE FROM profile_education
115 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
117 foreach ($value as $eduid=>&$edu) {
118 if ($edu['eduid'] != '') {
119 XDB
::execute("INSERT INTO profile_education
120 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
121 fieldid = {?}, grad_year = {?}, program = {?}",
122 $eduid, S
::i('uid'), $edu['eduid'], $edu['degreeid'],
123 $edu['fieldid'], $edu['grad_year'], $edu['program']);
129 class ProfileEmailDirectory
implements ProfileSetting
131 public function __construct(){}
132 public function save(ProfilePage
&$page, $field, $value){}
134 public function value(ProfilePage
&$page, $field, $value, &$success)
139 if (!is_null($value)) {
140 $email_stripped = strtolower(trim($value));
141 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
142 $p->assign('email_error', '1');
143 $p->assign('email_directory_error', $email_stripped);
144 $p->trigError('Adresse Email invalide');
147 $p->assign('email_error', '0');
154 class ProfileNetworking
implements ProfileSetting
161 public function __construct()
163 $this->email
= new ProfileEmail();
164 $this->pub
= new ProfilePub();
165 $this->web
= new ProfileWeb();
166 $this->number
= new ProfileNumber();
169 public function value(ProfilePage
&$page, $field, $value, &$success)
171 if (is_null($value)) {
173 $res = XDB
::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
174 FROM profile_networking AS n
175 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
178 while($network = $res->next()) {
182 if (!is_array($value)) {
185 $res = XDB
::iterator("SELECT filter, network_type AS type
186 FROM profile_networking_enum;");
188 while($filter = $res->next()) {
189 $filters[$filter['type']] = $filter['filter'];
192 foreach($value as $i=>&$network) {
193 if (!trim($network['address'])) {
196 if (!isset($network['pub'])) {
197 $network['pub'] = 'private';
199 $network['error'] = false
;
200 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
202 if ($filters[$network['type']] == 'web') {
203 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
204 } elseif ($filters[$network['type']] == 'email') {
205 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
206 } elseif ($filters[$network['type']] == 'number') {
207 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
211 $network['error'] = true
;
218 public function save(ProfilePage
&$page, $field, $value)
220 XDB
::execute("DELETE FROM profile_networking
223 if (!count($value)) {
227 foreach ($value as $id=>$network) {
228 XDB
::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
229 VALUES ({?}, {?}, {?}, {?}, {?})",
230 S
::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
235 class ProfileGeneral
extends ProfilePage
237 protected $pg_template = 'profile/general.tpl';
239 public function __construct(PlWizard
&$wiz)
241 parent
::__construct($wiz);
242 $this->settings
['naissance'] = new ProfileDate();
243 $this->settings
['freetext_pub']
244 = $this->settings
['photo_pub']
246 $this->settings
['freetext']
247 = $this->settings
['nationalite']
248 = $this->settings
['nationalite2']
249 = $this->settings
['nationalite3']
250 = $this->settings
['nick']
251 = $this->settings
['yourself']
252 = $this->settings
['display_name']
253 = $this->settings
['sort_name']
254 = $this->settings
['tooltip_name']
255 = $this->settings
['promo_display']
256 = $this->settings
['search_names']
258 $this->settings
['synchro_ax']
260 $this->settings
['email_directory']
261 = new ProfileEmail();
262 $this->settings
['email_directory_new']
263 = new ProfileEmailDirectory();
264 $this->settings
['networking'] = new ProfileNetworking();
265 $this->settings
['tels'] = new ProfilePhones('user', 0);
266 $this->settings
['edus'] = new ProfileEdu();
267 $this->watched
= array('freetext' => true
, 'tels' => true
,
268 'networking' => true
, 'edus' => true
,
269 'nationalite' => true
, 'nationalite2' => true
,
270 'nationalite3' => true
);
273 protected function _fetchData()
275 // Checkout all data...
276 $res = XDB
::query("SELECT p.promo AS promo_display, e.entry_year AS entry_year, e.grad_year AS grad_year,
277 u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
278 t.display_tel as mobile, t.pub as mobile_pub,
279 d.email_directory as email_directory,
280 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
281 q.profile_from_ax as synchro_ax, u.matricule_ax,
282 p.public_name, p.private_name, p.yourself
283 FROM auth_user_md5 AS u
284 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
285 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
286 INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags))
287 LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user')
288 LEFT JOIN profile_directory AS d ON (d.uid = u.user_id)
289 WHERE u.user_id = {?}", S
::v('uid', -1));
290 $this->values
= $res->fetchOneAssoc();
292 // Retreive photo informations
293 $res = XDB
::query("SELECT pub
295 WHERE uid = {?}", S
::v('uid'));
296 $this->values
['photo_pub'] = $res->fetchOneCell();
298 $res = XDB
::query("SELECT COUNT(*)
300 WHERE type='photo' AND user_id = {?}",
302 $this->values
['nouvellephoto'] = $res->fetchOneCell();
304 // Retreive search names info
305 $sn_all = XDB
::iterator("SELECT IF(sn.particle = '', sn.name, CONCAT(sn.particle, ' ', sn.name)) AS name,
306 sn.particle, sn.typeid, e.name AS type,
307 FIND_IN_SET('has_particle', e.flags) AS has_particle,
308 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
309 FIND_IN_SET('public', e.flags) AS pub
310 FROM profile_name_search AS sn
311 INNER JOIN profile_name_search_enum AS e ON (e.id = sn.typeid)
312 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
313 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
316 $sn_types = XDB
::iterator("SELECT id, name, FIND_IN_SET('has_particle', flags) AS has_particle
317 FROM profile_name_search_enum
318 WHERE NOT FIND_IN_SET('not_displayed', flags)
319 AND FIND_IN_SET('always_displayed', flags)
322 $this->values
['search_names'] = array();
323 $sn = $sn_all->next();
324 while ($sn_type = $sn_types->next()) {
325 if ($sn_type['id'] == $sn['typeid']) {
326 $this->values
['search_names'][] = $sn;
327 $sn = $sn_all->next();
329 $this->values
['search_names'][] = array('typeid' => $sn_type['id'],
330 'type' => $sn_type['name'],
332 'has_particle' => $sn_type['has_particle'],
333 'always_displayed' => 1);
337 $this->values
['search_names'][] = $sn;
338 } while ($sn = $sn_all->next());
340 // Proposes choice for promo_display
341 if ($this->values
['entry_year'] != $this->values
['grad_year'] - 3) {
342 for ($i = $this->values
['entry_year']; $i < $this->values
['grad_year'] - 2; $i++
) {
343 $this->values
['promo_choice'][] = "X" . $i;
348 protected function _saveData()
350 if ($this->changed
['nationalite'] ||
$this->changed
['nationalite2'] ||
$this->changed
['nationalite3']
351 ||
$this->changed
['naissance']) {
352 if ($this->values
['nationalite3'] == "") {
353 $this->values
['nationalite3'] = NULL
;
355 if ($this->values
['nationalite2'] == "") {
356 $this->values
['nationalite2'] = $this->values
['nationalite3'];
357 $this->values
['nationalite3'] = NULL
;
359 if ($this->values
['nationalite'] == "") {
360 $this->values
['nationalite'] = $this->values
['nationalite2'];
361 $this->values
['nationalite2'] = $this->values
['nationalite3'];
362 $this->values
['nationalite3'] = NULL
;
365 XDB
::execute("UPDATE auth_user_md5
366 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, naissance={?}
367 WHERE user_id = {?}",
368 $this->values
['nationalite'], $this->values
['nationalite2'], $this->values
['nationalite3'],
369 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['naissance']),
372 if ($this->changed
['freetext'] ||
$this->changed
['freetext_pub'] ||
$this->changed
['synchro_ax']) {
373 XDB
::execute("UPDATE auth_user_quick
374 SET profile_freetext={?}, profile_freetext_pub={?}, profile_from_ax = {?}
375 WHERE user_id = {?}",
376 $this->values
['freetext'], $this->values
['freetext_pub'],
377 $this->values
['synchro_ax'], S
::v('uid'));
379 if ($this->changed
['email_directory']) {
380 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
381 $this->values
['email_directory_new'] : $this->values
['email_directory'];
382 if ($new_email == "") {
385 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
387 S
::v('uid'), $new_email);
389 if ($this->changed
['photo_pub']) {
390 XDB
::execute("UPDATE photo
393 $this->values
['photo_pub'], S
::v('uid'));
396 if ($this->changed
['yourself'] ||
$this->changed
['search_names']) {
397 if ($this->changed
['search_names']) {
398 XDB
::execute("DELETE FROM s
399 USING profile_name_search AS s
400 INNER JOIN profile_name_search_enum AS e ON (s.typeid = e.id)
401 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
403 $search_names = array();
404 foreach ($this->values
['search_names'] as $sn) {
405 if ($sn['name'] != '') {
406 if ($sn['particle']) {
407 list($particle, $name) = explode(' ', $sn['name'], 2);
409 list($particle, $name) = explode('\'', $sn['name'], 2);
415 $particle = trim($particle);
417 $sn['name'] = trim($sn['name']);
418 XDB
::execute("INSERT INTO profile_name_search (particle, name, typeid, pid)
419 VALUES ({?}, {?}, {?}, {?})",
420 $particle, $name, $sn['typeid'], S
::i('uid'));
421 if (!isset($search_names[$sn['typeid']])) {
422 $search_names[$sn['typeid']] = array($sn['name'], $name);
424 $search_names[$sn['typeid']] = array_merge($search_names[$sn['typeid']], array($name));
429 require_once 'name.func.inc.php';
430 $sn_types_public = build_types('public');
431 $sn_types_private = build_types('private');
432 $full_name = build_full_name($search_names, $sn_types_public);
433 $directory_name = build_directory_name($search_names, $sn_types_public, $full_name);
434 $short_name = short_name($search_names, $sn_types_public);
435 $sort_name = short_name($search_names, $sn_types_public);
436 $this->values
['public_name'] = build_public_name($search_names, $sn_types_public, $full_name);
437 $this->values
['private_name'] = $public_name . build_private_name($search_names, $sn_types_private);
438 XDB
::execute("UPDATE profile_display
439 SET yourself = {?}, public_name = {?}, private_name = {?},
440 directory_name = {?}, short_name = {?}, sort_name = {?}
442 $this->values
['yourself'], $this->values
['public_name'],
443 $this->values
['private_name'], $directory_name, $short_name,
444 $sort_name, S
::v('uid'));
445 /*if ($this->changed['search_names']) {
446 require_once('user.func.inc.php');
447 user_reindex(S::v('uid'));
450 XDB
::execute("UPDATE profile_display
453 $this->values
['yourself'], S
::v('uid'));
456 if ($this->changed
['promo_display']) {
457 XDB
::execute("UPDATE profile_display
460 $this->values
['promo_display'], S
::v('uid'));
464 public function _prepare(PlPage
&$page, $id)
466 require_once "education.func.inc.php";
468 $res = XDB
::iterator("SELECT id, field
469 FROM profile_education_field_enum
471 $page->assign('edu_fields', $res->fetchAllAssoc());
473 require_once "emails.combobox.inc.php";
474 fill_email_combobox($page);
476 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
477 FROM profile_networking_enum AS nw
479 $page->assign('network_list', $res->fetchAllAssoc());
483 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: