2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ProfileSearchName
implements ProfileSetting
70 public function __construct()
74 public function value(ProfilePage
&$page, $field, $value, &$success)
78 public function save(ProfilePage
&$page, $field, $new_value)
83 class ProfileEdu
implements ProfileSetting
85 public function __construct()
89 static function sortByGradYear($line1, $line2) {
90 $a = (int) $line1['grad_year'];
91 $b = (int) $line2['grad_year'];
95 return ($a < $b) ?
-1 : 1;
98 public function value(ProfilePage
&$page, $field, $value, &$success)
101 if (is_null($value) ||
!is_array($value)) {
103 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
104 FROM profile_education
108 while($edu = $res->next()) {
113 foreach ($value as $key=>&$edu) {
114 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
115 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
116 $edu['error'] = true
;
125 usort($value, array("ProfileEdu", "sortByGradYear"));
130 public function save(ProfilePage
&$page, $field, $value)
132 XDB
::execute("DELETE FROM profile_education
135 foreach ($value as $eduid=>&$edu) {
136 if ($edu['eduid'] != '') {
137 XDB
::execute("INSERT INTO profile_education
138 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
139 fieldid = {?}, grad_year = {?}, program = {?}",
140 $eduid, S
::i('uid'), $edu['eduid'], $edu['degreeid'],
141 $edu['fieldid'], $edu['grad_year'], $edu['program']);
148 class ProfileEmailDirectory
implements ProfileSetting
150 public function __construct()
154 public function value(ProfilePage
&$page, $field, $value, &$success)
159 if (!is_null($value)) {
160 $email_stripped = strtolower(trim($value));
161 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
162 $p->assign('email_error', '1');
163 $p->assign('email_directory_error', $email_stripped);
164 $p->trigError('Adresse Email invalide');
167 $p->assign('email_error', '0');
173 public function save(ProfilePage
&$page, $field, $value)
178 class ProfileNetworking
implements ProfileSetting
185 public function __construct()
187 $this->email
= new ProfileEmail();
188 $this->pub
= new ProfilePub();
189 $this->web
= new ProfileWeb();
190 $this->number
= new ProfileNumber();
193 public function value(ProfilePage
&$page, $field, $value, &$success)
195 if (is_null($value)) {
197 $res = XDB
::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
198 FROM profile_networking AS n
199 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
202 while($network = $res->next()) {
206 if (!is_array($value)) {
209 $res = XDB
::iterator("SELECT filter, network_type AS type
210 FROM profile_networking_enum;");
212 while($filter = $res->next()) {
213 $filters[$filter['type']] = $filter['filter'];
216 foreach($value as $i=>&$network) {
217 if (!trim($network['address'])) {
220 if (!isset($network['pub'])) {
221 $network['pub'] = 'private';
223 $network['error'] = false
;
224 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
226 if ($filters[$network['type']] == 'web') {
227 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
228 } elseif ($filters[$network['type']] == 'email') {
229 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
230 } elseif ($filters[$network['type']] == 'number') {
231 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
235 $network['error'] = true
;
242 public function save(ProfilePage
&$page, $field, $value)
244 XDB
::execute("DELETE FROM profile_networking
247 if (!count($value)) {
251 foreach ($value as $id=>$network) {
252 XDB
::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
253 VALUES ({?}, {?}, {?}, {?}, {?})",
254 S
::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
259 class ProfileGeneral
extends ProfilePage
261 protected $pg_template = 'profile/general.tpl';
263 public function __construct(PlWizard
&$wiz)
265 parent
::__construct($wiz);
266 $this->settings
['nom'] = $this->settings
['prenom']
268 $this->settings
['naissance'] = new ProfileDate();
269 $this->settings
['freetext_pub']
270 = $this->settings
['photo_pub']
272 $this->settings
['freetext']
273 = $this->settings
['nationalite']
274 = $this->settings
['nationalite2']
275 = $this->settings
['nationalite3']
276 = $this->settings
['nick']
277 = $this->settings
['yourself']
278 = $this->settings
['display_name']
279 = $this->settings
['sort_name']
280 = $this->settings
['tooltip_name']
282 $this->settings
['synchro_ax']
284 $this->settings
['email_directory']
285 = new ProfileEmail();
286 $this->settings
['email_directory_new']
287 = new ProfileEmailDirectory();
288 $this->settings
['networking'] = new ProfileNetworking();
289 $this->settings
['tels'] = new ProfilePhones('user', 0);
290 $this->settings
['edus'] = new ProfileEdu();
291 $this->watched
= array('nom' => true
, 'freetext' => true
, 'tels' => true
,
292 'networking' => true
, 'edus' => true
,
293 'nationalite' => true
, 'nationalite2' => true
,
294 'nationalite3' => true
, 'nick' => true
);
297 protected function _fetchData()
299 // Checkout all data...
300 $res = XDB
::query("SELECT u.promo, u.promo_sortie, u.nom_usage, u.nationalite,
301 u.nationalite2, u.nationalite3, u.naissance,
302 t.display_tel as mobile, t.pub as mobile_pub,
303 d.email_directory as email_directory,
304 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
305 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
306 n.yourself, n.display AS display_name, n.sort AS sort_name,
307 n.tooltip AS tooltip_name
308 FROM auth_user_md5 AS u
309 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
310 INNER JOIN profile_names_display AS n ON(n.user_id = u.user_id)
311 LEFT JOIN profile_phones AS t ON(u.user_id = t.uid AND link_type = 'user')
312 LEFT JOIN profile_directory AS d ON(d.uid = u.user_id)
313 WHERE u.user_id = {?}", S
::v('uid', -1));
314 $this->values
= $res->fetchOneAssoc();
316 // Retreive photo informations
317 $res = XDB
::query("SELECT pub
319 WHERE uid = {?}", S
::v('uid'));
320 $this->values
['photo_pub'] = $res->fetchOneCell();
322 $res = XDB
::query("SELECT COUNT(*)
324 WHERE type='photo' AND user_id = {?}",
326 $this->values
['nouvellephoto'] = $res->fetchOneCell();
328 // Retreive search names info
329 $this->values
['search_names'] = XDB
::iterator("
330 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
331 FROM profile_names_search AS sn
332 WHERE sn.user_id = {?}
333 ORDER BY sn.name_type, search_score, search_name",
337 $res = XDB
::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
338 FROM profile_phones AS t
339 WHERE t.uid = {?} AND t.link_type = 'user'
342 $this->values
['tels'] = $res->fetchAllAssoc();
345 protected function _saveData()
347 if ($this->changed
['nationalite'] ||
$this->changed
['nationalite2'] ||
$this->changed
['nationalite3']
348 ||
$this->changed
['nom'] ||
$this->changed
['prenom'] ||
$this->changed
['naissance']) {
349 if ($this->values
['nationalite3'] == "") {
350 $this->values
['nationalite3'] = NULL
;
352 if ($this->values
['nationalite2'] == "") {
353 $this->values
['nationalite2'] = $this->values
['nationalite3'];
354 $this->values
['nationalite3'] = NULL
;
356 if ($this->values
['nationalite'] == "") {
357 $this->values
['nationalite'] = $this->values
['nationalite2'];
358 $this->values
['nationalite2'] = $this->values
['nationalite3'];
359 $this->values
['nationalite3'] = NULL
;
362 XDB
::execute("UPDATE auth_user_md5
363 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, nom={?}, prenom={?}, naissance={?}
364 WHERE user_id = {?}",
365 $this->values
['nationalite'], $this->values
['nationalite2'], $this->values
['nationalite3'],
366 $this->values
['nom'], $this->values
['prenom'],
367 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['naissance']),
370 if ($this->changed
['nick'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub'] ||
$this->changed
['synchro_ax']) {
371 XDB
::execute("UPDATE auth_user_quick
372 SET profile_nick= {?},
373 profile_freetext={?},
374 profile_freetext_pub={?}, profile_from_ax = {?}
375 WHERE user_id = {?}",
376 $this->values
['nick'],
377 $this->values
['freetext'], $this->values
['freetext_pub'],
378 $this->values
['synchro_ax'], S
::v('uid'));
380 if ($this->changed
['email_directory']) {
381 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
382 $this->values
['email_directory_new'] : $this->values
['email_directory'];
383 if ($new_email == "") {
386 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
388 S
::v('uid'), $new_email);
390 if ($this->changed
['nick']) {
391 require_once('user.func.inc.php');
392 user_reindex(S
::v('uid'));
394 if ($this->changed
['photo_pub']) {
395 XDB
::execute("UPDATE photo
398 $this->values
['photo_pub'], S
::v('uid'));
400 if ($this->changed
['yourself'] ||
$this->changed
['sort_name'] ||
401 $this-> changed
['display_name'] ||
$this->changed
['tooltip_name']) {
402 XDB
::execute("UPDATE profile_names_display AS n
403 SET n.yourself = {?},
404 n.sort = {?}, ". // SET
405 "n.display = {?}, ". // SET
406 "n.tooltip = {?} ". // SET
407 "WHERE n.user_id = {?}",
408 $this->values
['yourself'],
409 $this->values
['sort_name'],
410 $this->values
['display_name'],
411 $this->values
['tooltip_name'],
416 public function _prepare(PlPage
&$page, $id)
418 require_once "education.func.inc.php";
420 $res = XDB
::iterator("SELECT id, field
421 FROM profile_education_field_enum
423 $page->assign('edu_fields', $res->fetchAllAssoc());
425 require_once "emails.combobox.inc.php";
426 fill_email_combobox($page);
428 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
429 FROM profile_networking_enum AS nw
431 $page->assign('network_list', $res->fetchAllAssoc());
435 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: