baa845de94cd1a74db8c20a87ca6314ab072d8c0
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)
26 return ($i = strpos($old, $new)) !== false
27 && ($i == 0 ||
$old{$i-1} == ' ')
28 && ($i +
$newLen == strlen($old) ||
$old{$i +
$newLen} == ' ');
31 private function prepareField($value)
33 $value = strtoupper(replace_accent($value));
34 return preg_replace('/[^A-Z]/', ' ', $value);
37 public function value(ProfilePage
&$page, $field, $value, &$success)
40 $current = S
::v($field);
41 $init = S
::v($field . '_ini');
42 if (is_null($value)) {
45 if ($value == $current ||
$value == $init) {
48 $ini = $this->prepareField($init);
49 $old = $this->prepareField($current);
50 $new = $this->prepareField($value);
51 $newLen = strlen($new);
52 $success = $this->matchWord($old, $new, $newLen)
53 ||
$this->matchWord($ini, $new, $newLen)
54 ||
($field == 'nom' && $new == 'DE ' . $old);
56 Platal
::page()->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
57 . (($init == $current)?
"" : " et de ton prénom précédent ($current)"));
59 return $success ?
$value : $current;
62 public function save(ProfilePage
&$page, $field, $new_value)
64 $_SESSION[$field] = $new_value;
68 class ProfileEdu
implements ProfileSetting
70 public function __construct() {
73 static function sortByGradYear($line1, $line2) {
74 $a = (int) $line1['grad_year'];
75 $b = (int) $line2['grad_year'];
79 return ($a < $b) ?
-1 : 1;
82 public function value(ProfilePage
&$page, $field, $value, &$success)
85 if (is_null($value) ||
!is_array($value)) {
87 $res = XDB
::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
88 FROM profile_education
89 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
92 while($edu = $res->next()) {
97 foreach ($value as $key=>&$edu) {
98 if (($edu['grad_year'] < 1921) ||
($edu['grad_year'] > (date('Y') +
4))) {
99 Platal
::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
100 $edu['error'] = true
;
109 usort($value, array("ProfileEdu", "sortByGradYear"));
114 public function save(ProfilePage
&$page, $field, $value)
116 XDB
::execute("DELETE FROM profile_education
117 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
119 foreach ($value as $eduid=>&$edu) {
120 if ($edu['eduid'] != '') {
121 XDB
::execute("INSERT INTO profile_education
122 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
123 fieldid = {?}, grad_year = {?}, program = {?}",
124 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
125 $edu['fieldid'], $edu['grad_year'], $edu['program']);
131 class ProfileEmailDirectory
implements ProfileSetting
133 public function __construct(){}
134 public function save(ProfilePage
&$page, $field, $value){}
136 public function value(ProfilePage
&$page, $field, $value, &$success)
141 if (!is_null($value)) {
142 $email_stripped = strtolower(trim($value));
143 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values
['email_directory'] == "new@example.org")) {
144 $p->assign('email_error', '1');
145 $p->assign('email_directory_error', $email_stripped);
146 $p->trigError('Adresse Email invalide');
149 $p->assign('email_error', '0');
156 class ProfileNetworking
implements ProfileSetting
163 public function __construct()
165 $this->email
= new ProfileEmail();
166 $this->pub
= new ProfilePub();
167 $this->web
= new ProfileWeb();
168 $this->number
= new ProfileNumber();
171 public function value(ProfilePage
&$page, $field, $value, &$success)
173 if (is_null($value)) {
175 $res = XDB
::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
176 FROM profile_networking AS n
177 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
180 while($network = $res->next()) {
184 if (!is_array($value)) {
187 $res = XDB
::iterator("SELECT filter, network_type AS type
188 FROM profile_networking_enum;");
190 while($filter = $res->next()) {
191 $filters[$filter['type']] = $filter['filter'];
194 foreach($value as $i=>&$network) {
195 if (!trim($network['address'])) {
198 if (!isset($network['pub'])) {
199 $network['pub'] = 'private';
201 $network['error'] = false
;
202 $network['pub'] = $this->pub
->value($page, 'pub', $network['pub'], $s);
204 if ($filters[$network['type']] == 'web') {
205 $network['address'] = $this->web
->value($page, 'address', $network['address'], $s);
206 } elseif ($filters[$network['type']] == 'email') {
207 $network['address'] = $this->email
->value($page, 'address', $network['address'], $s);
208 } elseif ($filters[$network['type']] == 'number') {
209 $network['address'] = $this->number
->value($page, 'address', $network['address'], $s);
213 $network['error'] = true
;
220 public function save(ProfilePage
&$page, $field, $value)
222 XDB
::execute("DELETE FROM profile_networking
225 if (!count($value)) {
229 foreach ($value as $id=>$network) {
230 XDB
::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
231 VALUES ({?}, {?}, {?}, {?}, {?})",
232 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
237 class ProfileGeneral
extends ProfilePage
239 protected $pg_template = 'profile/general.tpl';
241 public function __construct(PlWizard
&$wiz)
243 parent
::__construct($wiz);
244 $this->settings
['birthdate'] = new ProfileDate();
245 $this->settings
['freetext_pub']
246 = $this->settings
['photo_pub']
248 $this->settings
['freetext']
249 = $this->settings
['nationality1']
250 = $this->settings
['nationality2']
251 = $this->settings
['nationality3']
252 = $this->settings
['nick']
253 = $this->settings
['yourself']
254 = $this->settings
['display_name']
255 = $this->settings
['sort_name']
256 = $this->settings
['tooltip_name']
257 = $this->settings
['promo_display']
258 = $this->settings
['search_names']
260 $this->settings
['synchro_ax']
262 $this->settings
['email_directory']
263 = new ProfileEmail();
264 $this->settings
['email_directory_new']
265 = new ProfileEmailDirectory();
266 $this->settings
['networking'] = new ProfileNetworking();
267 $this->settings
['tels'] = new ProfilePhones('user', 0);
268 $this->settings
['edus'] = new ProfileEdu();
269 $this->watched
= array('freetext' => true
, 'tels' => true
,
270 'networking' => true
, 'edus' => true
,
271 'nationality1' => true
, 'nationality2' => true
,
272 'nationality3' => true
, 'nick' => true
);
275 protected function _fetchData()
277 // Checkout all data...
278 $res = XDB
::query("SELECT p.promo AS promo_display, e.entry_year AS entry_year, e.grad_year AS grad_year,
279 pr.nationality1, pr.nationality2, pr.nationality3, pr.birthdate,
280 t.display_tel as mobile, t.pub as mobile_pub,
281 d.email_directory as email_directory,
282 pr.freetext, pr.freetext_pub as freetext_pub,
283 n.yourself, n.display AS display_name, n.sort AS sort_name,
284 n.tooltip AS tooltip_name
286 INNER JOIN profile_names_display AS n ON (n.user_id = pr.pid)
287 INNER JOIN profile_display AS p ON (p.pid = pr.pid)
288 INNER JOIN profile_education AS e ON (e.uid = pr.pid AND FIND_IN_SET('primary', e.flags))
289 LEFT JOIN profile_phones AS t ON (t.uid = pr.pid AND link_type = 'user')
290 LEFT JOIN profile_directory AS d ON (d.uid = pr.pid)
291 WHERE pr.pid = {?}", $this->pid());
292 $this->values
= $res->fetchOneAssoc();
294 // Retreive photo informations
295 $res = XDB
::query("SELECT pub
297 WHERE uid = {?}", $this->pid());
298 $this->values
['photo_pub'] = $res->fetchOneCell();
301 $res = XDB
::query("SELECT COUNT(*)
303 WHERE type='photo' AND user_id = {?}",
305 $this->values
['nouvellephoto'] = $res->fetchOneCell();
307 $this->values
['nouvellephoto'] = 0;
310 // Retreive search names info
311 $sn_all = XDB
::iterator("SELECT IF(sn.particle = '', sn.name, CONCAT(sn.particle, ' ', sn.name)) AS name,
312 sn.particle, sn.typeid, e.name AS type,
313 FIND_IN_SET('has_particle', e.flags) AS has_particle,
314 FIND_IN_SET('always_displayed', e.flags) AS always_displayed,
315 FIND_IN_SET('public', e.flags) AS pub
316 FROM profile_name_search AS sn
317 INNER JOIN profile_name_search_enum AS e ON (e.id = sn.typeid)
318 WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)
319 ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name",
322 $sn_types = XDB
::iterator("SELECT id, name, FIND_IN_SET('has_particle', flags) AS has_particle
323 FROM profile_name_search_enum
324 WHERE NOT FIND_IN_SET('not_displayed', flags)
325 AND FIND_IN_SET('always_displayed', flags)
328 $this->values
['search_names'] = array();
329 $sn = $sn_all->next();
330 while ($sn_type = $sn_types->next()) {
331 if ($sn_type['id'] == $sn['typeid']) {
332 $this->values
['search_names'][] = $sn;
333 $sn = $sn_all->next();
335 $this->values
['search_names'][] = array('typeid' => $sn_type['id'],
336 'type' => $sn_type['name'],
338 'has_particle' => $sn_type['has_particle'],
339 'always_displayed' => 1);
343 $this->values
['search_names'][] = $sn;
344 } while ($sn = $sn_all->next());
346 // Proposes choice for promo_display
347 if ($this->values
['entry_year'] != $this->values
['grad_year'] - 3) {
348 for ($i = $this->values
['entry_year']; $i < $this->values
['grad_year'] - 2; $i++
) {
349 $this->values
['promo_choice'][] = "X" . $i;
354 protected function _saveData()
356 if ($this->changed
['nationality1'] ||
$this->changed
['nationality2'] ||
$this->changed
['nationality3']
357 ||
$this->changed
['birthdate'] ||
$this->changed
['freetext'] ||
$this->changed
['freetext_pub']) {
358 if ($this->values
['nationality3'] == "") {
359 $this->values
['nationality3'] = NULL
;
361 if ($this->values
['nationality2'] == "") {
362 $this->values
['nationality2'] = $this->values
['nationality3'];
363 $this->values
['nationality3'] = NULL
;
365 if ($this->values
['nationality1'] == "") {
366 $this->values
['nationality1'] = $this->values
['nationality2'];
367 $this->values
['nationality2'] = $this->values
['nationality3'];
368 $this->values
['nationality3'] = NULL
;
371 XDB
::execute("UPDATE profiles
372 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
373 freetext = {?}, freetext_pub = {?}
374 WHERE user_id = {?}",
375 $this->values
['nationality1'], $this->values
['nationality2'], $this->values
['nationality3'],
376 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values
['birthdate']),
377 $this->values
['freetext'], $this->values
['freetext_pub'],
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 $this->pid(), $new_email);
390 if ($this->changed
['photo_pub']) {
391 XDB
::execute("UPDATE photo
394 $this->values
['photo_pub'], $this->pid());
397 if ($this->changed
['yourself'] ||
$this->changed
['search_names']) {
398 if ($this->changed
['search_names']) {
399 XDB
::execute("DELETE FROM s
400 USING profile_name_search AS s
401 INNER JOIN profile_name_search_enum AS e ON (s.typeid = e.id)
402 WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)",
404 $search_names = array();
405 foreach ($this->values
['search_names'] as $sn) {
406 if ($sn['name'] != '') {
407 if ($sn['particle']) {
408 list($particle, $name) = explode(' ', $sn['name'], 2);
410 list($particle, $name) = explode('\'', $sn['name'], 2);
416 $particle = trim($particle);
418 $sn['name'] = trim($sn['name']);
419 XDB
::execute("INSERT INTO profile_name_search (particle, name, typeid, pid)
420 VALUES ({?}, {?}, {?}, {?})",
421 $particle, $name, $sn['typeid'], $this->pid());
422 if (!isset($search_names[$sn['typeid']])) {
423 $search_names[$sn['typeid']] = array($sn['name'], $name);
425 $search_names[$sn['typeid']] = array_merge($search_names[$sn['typeid']], array($name));
430 require_once 'name.func.inc.php';
431 $sn_types_public = build_types('public');
432 $sn_types_private = build_types('private');
433 $full_name = build_full_name($search_names, $sn_types_public);
434 $directory_name = build_directory_name($search_names, $sn_types_public, $full_name);
435 $short_name = short_name($search_names, $sn_types_public);
436 $sort_name = short_name($search_names, $sn_types_public);
437 $this->values
['public_name'] = build_public_name($search_names, $sn_types_public, $full_name);
438 $this->values
['private_name'] = $public_name . build_private_name($search_names, $sn_types_private);
439 XDB
::execute("UPDATE profile_display
440 SET yourself = {?}, public_name = {?}, private_name = {?},
441 directory_name = {?}, short_name = {?}, sort_name = {?}
443 $this->values
['yourself'], $this->values
['public_name'],
444 $this->values
['private_name'], $directory_name, $short_name,
445 $sort_name, $this->pid());
446 /*if ($this->changed['search_names']) {
447 require_once('user.func.inc.php');
448 user_reindex(S::v('uid'));
451 XDB
::execute("UPDATE profile_display
454 $this->values
['yourself'], $this->pid());
457 if ($this->changed
['promo_display']) {
458 XDB
::execute("UPDATE profile_display
461 $this->values
['promo_display'], $this->pid());
465 public function _prepare(PlPage
&$page, $id)
467 require_once "education.func.inc.php";
469 $res = XDB
::iterator("SELECT id, field
470 FROM profile_education_field_enum
472 $page->assign('edu_fields', $res->fetchAllAssoc());
474 require_once "emails.combobox.inc.php";
475 fill_email_combobox($page, $this->owner
, $this->profile
);
477 $res = XDB
::iterator("SELECT nw.network_type AS type, nw.name
478 FROM profile_networking_enum AS nw
480 $page->assign('network_list', $res->fetchAllAssoc());
484 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: