Merge commit 'origin/fusionax' into account
[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 {
26 return ($i = strpos($old, $new)) !== false
27 && ($i == 0 || $old{$i-1} == ' ')
28 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
29 }
30
31 private function prepareField($value)
32 {
33 $value = strtoupper(replace_accent($value));
34 return preg_replace('/[^A-Z]/', ' ', $value);
35 }
36
37 public function value(ProfilePage &$page, $field, $value, &$success)
38 {
39 $success = true;
40 $current = S::v($field);
41 $init = S::v($field . '_ini');
42 if (is_null($value)) {
43 return $current;
44 }
45 if ($value == $current || $value == $init) {
46 return $value;
47 }
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);
55 if (!$success) {
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)"));
58 }
59 return $success ? $value : $current;
60 }
61
62 public function save(ProfilePage &$page, $field, $new_value)
63 {
64 $_SESSION[$field] = $new_value;
65 }
66 }
67
68 class ProfileEdu implements ProfileSetting
69 {
70 public function __construct() {
71 }
72
73 static function sortByGradYear($line1, $line2) {
74 $a = (int) $line1['grad_year'];
75 $b = (int) $line2['grad_year'];
76 if ($a == $b) {
77 return 0;
78 }
79 return ($a < $b) ? -1 : 1;
80 }
81
82 public function value(ProfilePage &$page, $field, $value, &$success)
83 {
84 $success = true;
85 if (is_null($value) || !is_array($value)) {
86 $value = array();
87 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
88 FROM profile_education
89 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
90 ORDER BY id",
91 $page->pid());
92 while($edu = $res->next()) {
93 $value[] = $edu;
94 }
95 } else {
96 $i = 0;
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;
101 $success = false;
102 }
103 if ($key != $i) {
104 $value[$i] = $edu;
105 unset($value[$key]);
106 }
107 $i++;
108 }
109 usort($value, array("ProfileEdu", "sortByGradYear"));
110 }
111 return $value;
112 }
113
114 public function save(ProfilePage &$page, $field, $value)
115 {
116 XDB::execute("DELETE FROM profile_education
117 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
118 $page->pid());
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']);
126 }
127 }
128 }
129 }
130
131 class ProfileEmailDirectory implements ProfileSetting
132 {
133 public function __construct(){}
134 public function save(ProfilePage &$page, $field, $value){}
135
136 public function value(ProfilePage &$page, $field, $value, &$success)
137 {
138 $p = Platal::page();
139
140 $success = true;
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');
147 $success = false;
148 } else {
149 $p->assign('email_error', '0');
150 }
151 }
152 return $value;
153 }
154 }
155
156 class ProfileNetworking implements ProfileSetting
157 {
158 private $email;
159 private $pub;
160 private $web;
161 private $number;
162
163 public function __construct()
164 {
165 $this->email = new ProfileEmail();
166 $this->pub = new ProfilePub();
167 $this->web = new ProfileWeb();
168 $this->number = new ProfileNumber();
169 }
170
171 public function value(ProfilePage &$page, $field, $value, &$success)
172 {
173 if (is_null($value)) {
174 $value = array();
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)
178 WHERE n.uid = {?}",
179 $page->pid());
180 while($network = $res->next()) {
181 $value[] = $network;
182 }
183 }
184 if (!is_array($value)) {
185 $value = array();
186 }
187 $res = XDB::iterator("SELECT filter, network_type AS type
188 FROM profile_networking_enum;");
189 $filters = array();
190 while($filter = $res->next()) {
191 $filters[$filter['type']] = $filter['filter'];
192 }
193 $success = true;
194 foreach($value as $i=>&$network) {
195 if (!trim($network['address'])) {
196 unset($value[$i]);
197 } else {
198 if (!isset($network['pub'])) {
199 $network['pub'] = 'private';
200 }
201 $network['error'] = false;
202 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
203 $s = true;
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);
210 }
211 if (!$s) {
212 $success = false;
213 $network['error'] = true;
214 }
215 }
216 }
217 return $value;
218 }
219
220 public function save(ProfilePage &$page, $field, $value)
221 {
222 XDB::execute("DELETE FROM profile_networking
223 WHERE uid = {?}",
224 $page->pid());
225 if (!count($value)) {
226 return;
227 }
228 $insert = array();
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']);
233 }
234 }
235 }
236
237 class ProfileGeneral extends ProfilePage
238 {
239 protected $pg_template = 'profile/general.tpl';
240
241 public function __construct(PlWizard &$wiz)
242 {
243 parent::__construct($wiz);
244 $this->settings['birthdate'] = new ProfileDate();
245 $this->settings['freetext_pub']
246 = $this->settings['photo_pub']
247 = new ProfilePub();
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']
259 = null;
260 $this->settings['synchro_ax']
261 = new ProfileBool();
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);
273 }
274
275 protected function _fetchData()
276 {
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
285 FROM profiles AS pr
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();
293
294 // Retreive photo informations
295 $res = XDB::query("SELECT pub
296 FROM photo
297 WHERE uid = {?}", $this->pid());
298 $this->values['photo_pub'] = $res->fetchOneCell();
299
300 if ($this->owner) {
301 $res = XDB::query("SELECT COUNT(*)
302 FROM requests
303 WHERE type='photo' AND user_id = {?}",
304 $this->owner->id());
305 $this->values['nouvellephoto'] = $res->fetchOneCell();
306 } else {
307 $this->values['nouvellephoto'] = 0;
308 }
309
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",
320 $this->pid());
321
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)
326 ORDER BY id");
327
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();
334 } else {
335 $this->values['search_names'][] = array('typeid' => $sn_type['id'],
336 'type' => $sn_type['name'],
337 'pub' => 1,
338 'has_particle' => $sn_type['has_particle'],
339 'always_displayed' => 1);
340 }
341 }
342 do {
343 $this->values['search_names'][] = $sn;
344 } while ($sn = $sn_all->next());
345
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;
350 }
351 }
352 }
353
354 protected function _saveData()
355 {
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;
360 }
361 if ($this->values['nationality2'] == "") {
362 $this->values['nationality2'] = $this->values['nationality3'];
363 $this->values['nationality3'] = NULL;
364 }
365 if ($this->values['nationality1'] == "") {
366 $this->values['nationality1'] = $this->values['nationality2'];
367 $this->values['nationality2'] = $this->values['nationality3'];
368 $this->values['nationality3'] = NULL;
369 }
370
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'],
378 $this->pid());
379 }
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 == "") {
384 $new_email = NULL;
385 }
386 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
387 VALUES ({?}, {?})",
388 $this->pid(), $new_email);
389 }
390 if ($this->changed['photo_pub']) {
391 XDB::execute("UPDATE photo
392 SET pub = {?}
393 WHERE uid = {?}",
394 $this->values['photo_pub'], $this->pid());
395 }
396
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)",
403 $this->pid());
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);
409 if (!$name) {
410 list($particle, $name) = explode('\'', $sn['name'], 2);
411 }
412 } else {
413 $particle = '';
414 $name = $sn['name'];
415 }
416 $particle = trim($particle);
417 $name = trim($name);
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);
424 } else {
425 $search_names[$sn['typeid']] = array_merge($search_names[$sn['typeid']], array($name));
426 }
427 }
428 }
429
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 = {?}
442 WHERE pid = {?}",
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'));
449 }*/
450 } else {
451 XDB::execute("UPDATE profile_display
452 SET yourself = {?}
453 WHERE pid = {?}",
454 $this->values['yourself'], $this->pid());
455 }
456 }
457 if ($this->changed['promo_display']) {
458 XDB::execute("UPDATE profile_display
459 SET promo = {?}
460 WHERE pid = {?}",
461 $this->values['promo_display'], $this->pid());
462 }
463 }
464
465 public function _prepare(PlPage &$page, $id)
466 {
467 require_once "education.func.inc.php";
468
469 $res = XDB::iterator("SELECT id, field
470 FROM profile_education_field_enum
471 ORDER BY field");
472 $page->assign('edu_fields', $res->fetchAllAssoc());
473
474 require_once "emails.combobox.inc.php";
475 fill_email_combobox($page, $this->owner, $this->profile);
476
477 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
478 FROM profile_networking_enum AS nw
479 ORDER BY name");
480 $page->assign('network_list', $res->fetchAllAssoc());
481 }
482 }
483
484 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
485 ?>