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(){}
87 static function sortByGradYear($line1, $line2) {
88 $a = (int) $line1['grad_year'];
89 $b = (int) $line2['grad_year'];
93 return ($a < $b) ?
-1 : 1;
96 public function value(ProfilePage
&$page, $field, $value, &$success)
99 if (is_null($value) ||
!is_array($value)) {
101 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
102 FROM profile_education
103 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
106 while($edu = $res->next()) {
111 foreach ($value as $key=>&$edu) {
112 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
113 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
114 $edu['error'] = true
;
123 usort($value, array("ProfileEdu", "sortByGradYear"));
128 public function save(ProfilePage
&$page, $field, $value)
130 XDB
::execute("DELETE FROM profile_education
131 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
133 foreach ($value as $eduid=>&$edu) {
134 if ($edu['eduid'] != '') {
135 XDB
::execute("INSERT INTO profile_education
136 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
137 fieldid = {?}, grad_year = {?}, program = {?}",
138 $eduid, S
::i('uid'), $edu['eduid'], $edu['degreeid'],
139 $edu['fieldid'], $edu['grad_year'], $edu['program']);
145 class ProfileEmailDirectory
implements ProfileSetting
147 public function __construct(){}
148 public function save(ProfilePage
&$page, $field, $value){}
150 public function value(ProfilePage
&$page, $field, $value, &$success)
155 if (!is_null($value)) {
156 $email_stripped = strtolower(trim($value));
157 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
158 $p->assign('email_error', '1');
159 $p->assign('email_directory_error', $email_stripped);
160 $p->trigError('Adresse Email invalide');
163 $p->assign('email_error', '0');
170 class ProfileNetworking
implements ProfileSetting
177 public function __construct()
179 $this->email
= new ProfileEmail();
180 $this->pub
= new ProfilePub();
181 $this->web
= new ProfileWeb();
182 $this->number
= new ProfileNumber();
185 public function value(ProfilePage
&$page, $field, $value, &$success)
187 if (is_null($value)) {
189 $res = XDB
::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
190 FROM profile_networking AS n
191 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
194 while($network = $res->next()) {
198 if (!is_array($value)) {
201 $res = XDB
::iterator("SELECT filter, network_type AS type
202 FROM profile_networking_enum;");
204 while($filter = $res->next()) {
205 $filters[$filter['type']] = $filter['filter'];
208 foreach($value as $i=>&$network) {
209 if (!trim($network['address'])) {
212 if (!isset($network['pub'])) {
213 $network['pub'] = 'private';
215 $network['error'] = false
;
216 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
218 if ($filters[$network['type']] == 'web') {
219 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
220 } elseif ($filters[$network['type']] == 'email') {
221 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
222 } elseif ($filters[$network['type']] == 'number') {
223 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
227 $network['error'] = true
;
234 public function save(ProfilePage
&$page, $field, $value)
236 XDB
::execute("DELETE FROM profile_networking
239 if (!count($value)) {
243 foreach ($value as $id=>$network) {
244 XDB
::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
245 VALUES ({?}, {?}, {?}, {?}, {?})",
246 S
::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
251 class ProfileGeneral
extends ProfilePage
253 protected $pg_template = 'profile/general.tpl';
255 public function __construct(PlWizard
&$wiz)
257 parent
::__construct($wiz);
258 $this->settings
['nom'] = $this->settings
['prenom']
260 $this->settings
['naissance'] = new ProfileDate();
261 $this->settings
['freetext_pub']
262 = $this->settings
['photo_pub']
264 $this->settings
['freetext']
265 = $this->settings
['nationalite']
266 = $this->settings
['nationalite2']
267 = $this->settings
['nationalite3']
268 = $this->settings
['nick']
269 = $this->settings
['yourself']
270 = $this->settings
['display_name']
271 = $this->settings
['sort_name']
272 = $this->settings
['tooltip_name']
273 = $this->settings
['promo_display']
275 $this->settings
['synchro_ax']
277 $this->settings
['email_directory']
278 = new ProfileEmail();
279 $this->settings
['email_directory_new']
280 = new ProfileEmailDirectory();
281 $this->settings
['networking'] = new ProfileNetworking();
282 $this->settings
['tels'] = new ProfilePhones('user', 0);
283 $this->settings
['edus'] = new ProfileEdu();
284 $this->watched
= array('nom' => true
, 'freetext' => true
, 'tels' => true
,
285 'networking' => true
, 'edus' => true
,
286 'nationalite' => true
, 'nationalite2' => true
,
287 'nationalite3' => true
, 'nick' => true
);
290 protected function _fetchData()
292 // Checkout all data...
293 $res = XDB
::query("SELECT p.promo AS promo_display, e.entry_year AS entry_year, e.grad_year AS grad_year,
294 u.nom_usage, u.nationalite, u.nationalite2, u.nationalite3, u.naissance,
295 t.display_tel as mobile, t.pub as mobile_pub,
296 d.email_directory as email_directory,
297 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
298 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
299 n.yourself, n.display AS display_name, n.sort AS sort_name,
300 n.tooltip AS tooltip_name
301 FROM auth_user_md5 AS u
302 INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id)
303 INNER JOIN profile_names_display AS n ON (n.user_id = u.user_id)
304 INNER JOIN profile_display AS p ON (p.pid = u.user_id)
305 INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags))
306 LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user')
307 LEFT JOIN profile_directory AS d ON (d.uid = u.user_id)
308 WHERE u.user_id = {?}", S
::v('uid', -1));
309 $this->values
= $res->fetchOneAssoc();
311 // Retreive photo informations
312 $res = XDB
::query("SELECT pub
314 WHERE uid = {?}", S
::v('uid'));
315 $this->values
['photo_pub'] = $res->fetchOneCell();
317 $res = XDB
::query("SELECT COUNT(*)
319 WHERE type='photo' AND user_id = {?}",
321 $this->values
['nouvellephoto'] = $res->fetchOneCell();
323 // Retreive search names info
324 $this->values
['search_names'] = XDB
::iterator("
325 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
326 FROM profile_names_search AS sn
327 WHERE sn.user_id = {?}
328 ORDER BY sn.name_type, search_score, search_name",
331 // Proposes choice for promo_display
332 if ($this->values
['entry_year'] != $this->values
['grad_year'] - 3) {
333 for ($i = $this->values
['entry_year']; $i < $this->values
['grad_year'] - 2; $i++
) {
334 $this->values
['promo_choice'][] = "X" . $i;
339 protected function _saveData()
341 if ($this->changed
['nationalite'] ||
$this->changed
['nationalite2'] ||
$this->changed
['nationalite3']
342 ||
$this->changed
['nom'] ||
$this->changed
['prenom'] ||
$this->changed
['naissance']) {
343 if ($this->values
['nationalite3'] == "") {
344 $this->values
['nationalite3'] = NULL
;
346 if ($this->values
['nationalite2'] == "") {
347 $this->values
['nationalite2'] = $this->values
['nationalite3'];
348 $this->values
['nationalite3'] = NULL
;
350 if ($this->values
['nationalite'] == "") {
351 $this->values
['nationalite'] = $this->values
['nationalite2'];
352 $this->values
['nationalite2'] = $this->values
['nationalite3'];
353 $this->values
['nationalite3'] = NULL
;
356 XDB
::execute("UPDATE auth_user_md5
357 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, nom={?}, prenom={?}, naissance={?}
358 WHERE user_id = {?}",
359 $this->values
['nationalite'], $this->values
['nationalite2'], $this->values
['nationalite3'],
360 $this->values
['nom'], $this->values
['prenom'],
361 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['naissance']),
364 if ($this->changed
['nick'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub'] ||
$this->changed
['synchro_ax']) {
365 XDB
::execute("UPDATE auth_user_quick
366 SET profile_nick= {?},
367 profile_freetext={?},
368 profile_freetext_pub={?}, profile_from_ax = {?}
369 WHERE user_id = {?}",
370 $this->values
['nick'],
371 $this->values
['freetext'], $this->values
['freetext_pub'],
372 $this->values
['synchro_ax'], S
::v('uid'));
374 if ($this->changed
['email_directory']) {
375 $new_email = ($this->values
['email_directory'] == "new@example.org") ?
376 $this->values
['email_directory_new'] : $this->values
['email_directory'];
377 if ($new_email == "") {
380 XDB
::execute("REPLACE INTO profile_directory (uid, email_directory)
382 S
::v('uid'), $new_email);
384 if ($this->changed
['nick']) {
385 require_once('user.func.inc.php');
386 user_reindex(S
::v('uid'));
388 if ($this->changed
['photo_pub']) {
389 XDB
::execute("UPDATE photo
392 $this->values
['photo_pub'], S
::v('uid'));
394 if ($this->changed
['yourself'] ||
$this->changed
['sort_name'] ||
395 $this-> changed
['display_name'] ||
$this->changed
['tooltip_name']) {
396 XDB
::execute("UPDATE profile_names_display AS n
397 SET n.yourself = {?},
398 n.sort = {?}, ". // SET
399 "n.display = {?}, ". // SET
400 "n.tooltip = {?} ". // SET
401 "WHERE n.user_id = {?}",
402 $this->values
['yourself'],
403 $this->values
['sort_name'],
404 $this->values
['display_name'],
405 $this->values
['tooltip_name'],
408 if ($this->changed
['promo_display']) {
409 XDB
::execute("UPDATE profile_display
412 $this->values
['promo_display'], S
::v('uid'));
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: