Merge commit 'origin/master' into fusionax
[platal.git] / modules / profile / general.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class ProfileNom implements ProfileSetting
23 {
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} == ' ');
28 }
29
30 private function prepareField($value)
31 {
32 $value = strtoupper(replace_accent($value));
33 return preg_replace('/[^A-Z]/', ' ', $value);
34 }
35
36 public function value(ProfilePage &$page, $field, $value, &$success)
37 {
38 $success = true;
39 $current = S::v($field);
40 $init = S::v($field . '_ini');
41 if (is_null($value)) {
42 return $current;
43 }
44 if ($value == $current || $value == $init) {
45 return $value;
46 }
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);
54 if (!$success) {
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)"));
57 }
58 return $success ? $value : $current;
59 }
60
61 public function save(ProfilePage &$page, $field, $new_value)
62 {
63 $_SESSION[$field] = $new_value;
64 }
65 }
66
67 class ProfileEdu implements ProfileSetting
68 {
69 public function __construct(){}
70
71 static function sortByGradYear($line1, $line2) {
72 $a = (int) $line1['grad_year'];
73 $b = (int) $line2['grad_year'];
74 if ($a == $b) {
75 return 0;
76 }
77 return ($a < $b) ? -1 : 1;
78 }
79
80 public function value(ProfilePage &$page, $field, $value, &$success)
81 {
82 $success = true;
83 if (is_null($value) || !is_array($value)) {
84 $value = array();
85 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
86 FROM profile_education
87 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
88 ORDER BY id",
89 S::v('uid'));
90 while($edu = $res->next()) {
91 $value[] = $edu;
92 }
93 } else {
94 $i = 0;
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.');
98 $edu['error'] = true;
99 $success = false;
100 }
101 if ($key != $i) {
102 $value[$i] = $edu;
103 unset($value[$key]);
104 }
105 $i++;
106 }
107 usort($value, array("ProfileEdu", "sortByGradYear"));
108 }
109 return $value;
110 }
111
112 public function save(ProfilePage &$page, $field, $value)
113 {
114 XDB::execute("DELETE FROM profile_education
115 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
116 S::i('uid'));
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']);
124 }
125 }
126 }
127 }
128
129 class ProfileEmailDirectory implements ProfileSetting
130 {
131 public function __construct(){}
132 public function save(ProfilePage &$page, $field, $value){}
133
134 public function value(ProfilePage &$page, $field, $value, &$success)
135 {
136 $p = Platal::page();
137
138 $success = true;
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');
145 $success = false;
146 } else {
147 $p->assign('email_error', '0');
148 }
149 }
150 return $value;
151 }
152 }
153
154 class ProfileNetworking implements ProfileSetting
155 {
156 private $email;
157 private $pub;
158 private $web;
159 private $number;
160
161 public function __construct()
162 {
163 $this->email = new ProfileEmail();
164 $this->pub = new ProfilePub();
165 $this->web = new ProfileWeb();
166 $this->number = new ProfileNumber();
167 }
168
169 public function value(ProfilePage &$page, $field, $value, &$success)
170 {
171 if (is_null($value)) {
172 $value = array();
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)
176 WHERE n.uid = {?}",
177 S::i('uid'));
178 while($network = $res->next()) {
179 $value[] = $network;
180 }
181 }
182 if (!is_array($value)) {
183 $value = array();
184 }
185 $res = XDB::iterator("SELECT filter, network_type AS type
186 FROM profile_networking_enum;");
187 $filters = array();
188 while($filter = $res->next()) {
189 $filters[$filter['type']] = $filter['filter'];
190 }
191 $success = true;
192 foreach($value as $i=>&$network) {
193 if (!trim($network['address'])) {
194 unset($value[$i]);
195 } else {
196 if (!isset($network['pub'])) {
197 $network['pub'] = 'private';
198 }
199 $network['error'] = false;
200 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
201 $s = true;
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);
208 }
209 if (!$s) {
210 $success = false;
211 $network['error'] = true;
212 }
213 }
214 }
215 return $value;
216 }
217
218 public function save(ProfilePage &$page, $field, $value)
219 {
220 XDB::execute("DELETE FROM profile_networking
221 WHERE uid = {?}",
222 S::i('uid'));
223 if (!count($value)) {
224 return;
225 }
226 $insert = array();
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']);
231 }
232 }
233 }
234
235 class ProfileGeneral extends ProfilePage
236 {
237 protected $pg_template = 'profile/general.tpl';
238
239 public function __construct(PlWizard &$wiz)
240 {
241 parent::__construct($wiz);
242 $this->settings['naissance'] = new ProfileDate();
243 $this->settings['freetext_pub']
244 = $this->settings['photo_pub']
245 = new ProfilePub();
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']
257 = null;
258 $this->settings['synchro_ax']
259 = new ProfileBool();
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);
271 }
272
273 protected function _fetchData()
274 {
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();
291
292 // Retreive photo informations
293 $res = XDB::query("SELECT pub
294 FROM photo
295 WHERE uid = {?}", S::v('uid'));
296 $this->values['photo_pub'] = $res->fetchOneCell();
297
298 $res = XDB::query("SELECT COUNT(*)
299 FROM requests
300 WHERE type='photo' AND user_id = {?}",
301 S::v('uid'));
302 $this->values['nouvellephoto'] = $res->fetchOneCell();
303
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",
314 S::v('uid'));
315
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)
320 ORDER BY id");
321
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();
328 } else {
329 $this->values['search_names'][] = array('typeid' => $sn_type['id'],
330 'type' => $sn_type['name'],
331 'pub' => 1,
332 'has_particle' => $sn_type['has_particle'],
333 'always_displayed' => 1);
334 }
335 }
336 do {
337 $this->values['search_names'][] = $sn;
338 } while ($sn = $sn_all->next());
339
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;
344 }
345 }
346 }
347
348 protected function _saveData()
349 {
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;
354 }
355 if ($this->values['nationalite2'] == "") {
356 $this->values['nationalite2'] = $this->values['nationalite3'];
357 $this->values['nationalite3'] = NULL;
358 }
359 if ($this->values['nationalite'] == "") {
360 $this->values['nationalite'] = $this->values['nationalite2'];
361 $this->values['nationalite2'] = $this->values['nationalite3'];
362 $this->values['nationalite3'] = NULL;
363 }
364
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']),
370 S::v('uid'));
371 }
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'));
378 }
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 == "") {
383 $new_email = NULL;
384 }
385 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
386 VALUES ({?}, {?})",
387 S::v('uid'), $new_email);
388 }
389 if ($this->changed['photo_pub']) {
390 XDB::execute("UPDATE photo
391 SET pub = {?}
392 WHERE uid = {?}",
393 $this->values['photo_pub'], S::v('uid'));
394 }
395
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)",
402 S::i('uid'));
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);
408 if (!$name) {
409 list($particle, $name) = explode('\'', $sn['name'], 2);
410 }
411 } else {
412 $particle = '';
413 $name = $sn['name'];
414 }
415 $particle = trim($particle);
416 $name = trim($name);
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);
423 } else {
424 $search_names[$sn['typeid']] = array_merge($search_names[$sn['typeid']], array($name));
425 }
426 }
427 }
428
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 = {?}
441 WHERE pid = {?}",
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'));
448 }*/
449 } else {
450 XDB::execute("UPDATE profile_display
451 SET yourself = {?}
452 WHERE pid = {?}",
453 $this->values['yourself'], S::v('uid'));
454 }
455 }
456 if ($this->changed['promo_display']) {
457 XDB::execute("UPDATE profile_display
458 SET promo = {?}
459 WHERE pid = {?}",
460 $this->values['promo_display'], S::v('uid'));
461 }
462 }
463
464 public function _prepare(PlPage &$page, $id)
465 {
466 require_once "education.func.inc.php";
467
468 $res = XDB::iterator("SELECT id, field
469 FROM profile_education_field_enum
470 ORDER BY field");
471 $page->assign('edu_fields', $res->fetchAllAssoc());
472
473 require_once "emails.combobox.inc.php";
474 fill_email_combobox($page);
475
476 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
477 FROM profile_networking_enum AS nw
478 ORDER BY name");
479 $page->assign('network_list', $res->fetchAllAssoc());
480 }
481 }
482
483 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
484 ?>