source: branches/posledni/forum/includes/functions_profile_fields.php

Last change on this file was 702, checked in by george, 15 years ago
  • Upraveno: Aktualizace fóra.
File size: 33.3 KB
Line 
1<?php
2/**
3*
4* @package phpBB3
5* @version $Id$
6* @copyright (c) 2005 phpBB Group
7* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8*
9*/
10
11/**
12* @ignore
13*/
14if (!defined('IN_PHPBB'))
15{
16 exit;
17}
18
19/**
20* Custom Profile Fields
21* @package phpBB3
22*/
23class custom_profile
24{
25 var $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date');
26 var $profile_cache = array();
27 var $options_lang = array();
28
29 /**
30 * Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
31 * Called by ucp_profile and ucp_register
32 * @access public
33 */
34 function generate_profile_fields($mode, $lang_id)
35 {
36 global $db, $template, $auth;
37
38 $sql_where = '';
39 switch ($mode)
40 {
41 case 'register':
42 // If the field is required we show it on the registration page
43 $sql_where .= ' AND f.field_show_on_reg = 1';
44 break;
45
46 case 'profile':
47 // Show hidden fields to moderators/admins
48 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
49 {
50 $sql_where .= ' AND f.field_show_profile = 1';
51 }
52 break;
53
54 default:
55 trigger_error('Wrong profile mode specified', E_USER_ERROR);
56 break;
57 }
58
59 $sql = 'SELECT l.*, f.*
60 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
61 WHERE f.field_active = 1
62 $sql_where
63 AND l.lang_id = $lang_id
64 AND l.field_id = f.field_id
65 ORDER BY f.field_order";
66 $result = $db->sql_query($sql);
67
68 while ($row = $db->sql_fetchrow($result))
69 {
70 // Return templated field
71 $tpl_snippet = $this->process_field_row('change', $row);
72
73 // Some types are multivalue, we can't give them a field_id as we would not know which to pick
74 $type = (int) $row['field_type'];
75
76 $template->assign_block_vars('profile_fields', array(
77 'LANG_NAME' => $row['lang_name'],
78 'LANG_EXPLAIN' => $row['lang_explain'],
79 'FIELD' => $tpl_snippet,
80 'FIELD_ID' => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'],
81 'S_REQUIRED' => ($row['field_required']) ? true : false)
82 );
83 }
84 $db->sql_freeresult($result);
85 }
86
87 /**
88 * Validate entered profile field data
89 * @access public
90 */
91 function validate_profile_field($field_type, &$field_value, $field_data)
92 {
93 switch ($field_type)
94 {
95 case FIELD_DATE:
96 $field_validate = explode('-', $field_value);
97
98 $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
99 $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
100 $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
101
102 if ((!$day || !$month || !$year) && !$field_data['field_required'])
103 {
104 return false;
105 }
106
107 if ((!$day || !$month || !$year) && $field_data['field_required'])
108 {
109 return 'FIELD_REQUIRED';
110 }
111
112 if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
113 {
114 return 'FIELD_INVALID_DATE';
115 }
116
117 if (checkdate($month, $day, $year) === false)
118 {
119 return 'FIELD_INVALID_DATE';
120 }
121 break;
122
123 case FIELD_BOOL:
124 $field_value = (bool) $field_value;
125
126 if (!$field_value && $field_data['field_required'])
127 {
128 return 'FIELD_REQUIRED';
129 }
130 break;
131
132 case FIELD_INT:
133 if (trim($field_value) === '' && !$field_data['field_required'])
134 {
135 return false;
136 }
137
138 $field_value = (int) $field_value;
139
140 if ($field_value < $field_data['field_minlen'])
141 {
142 return 'FIELD_TOO_SMALL';
143 }
144 else if ($field_value > $field_data['field_maxlen'])
145 {
146 return 'FIELD_TOO_LARGE';
147 }
148 break;
149
150 case FIELD_DROPDOWN:
151 $field_value = (int) $field_value;
152
153 if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
154 {
155 return 'FIELD_REQUIRED';
156 }
157 break;
158
159 case FIELD_STRING:
160 case FIELD_TEXT:
161 if (trim($field_value) === '' && !$field_data['field_required'])
162 {
163 return false;
164 }
165 else if (trim($field_value) === '' && $field_data['field_required'])
166 {
167 return 'FIELD_REQUIRED';
168 }
169
170 if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
171 {
172 return 'FIELD_TOO_SHORT';
173 }
174 else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
175 {
176 return 'FIELD_TOO_LONG';
177 }
178
179 if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
180 {
181 $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value);
182 if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
183 {
184 return 'FIELD_INVALID_CHARS';
185 }
186 }
187 break;
188 }
189
190 return false;
191 }
192
193 /**
194 * Build profile cache, used for display
195 * @access private
196 */
197 function build_cache()
198 {
199 global $db, $user, $auth;
200
201 $this->profile_cache = array();
202
203 // Display hidden/no_view fields for admin/moderator
204 $sql = 'SELECT l.*, f.*
205 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
206 WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
207 AND f.field_active = 1 ' .
208 ((!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . '
209 AND f.field_no_view = 0
210 AND l.field_id = f.field_id
211 ORDER BY f.field_order';
212 $result = $db->sql_query($sql);
213
214 while ($row = $db->sql_fetchrow($result))
215 {
216 $this->profile_cache[$row['field_ident']] = $row;
217 }
218 $db->sql_freeresult($result);
219 }
220
221 /**
222 * Get language entries for options and store them here for later use
223 */
224 function get_option_lang($field_id, $lang_id, $field_type, $preview)
225 {
226 global $db;
227
228 if ($preview)
229 {
230 $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
231
232 foreach ($lang_options as $num => $var)
233 {
234 $this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
235 }
236 }
237 else
238 {
239 $sql = 'SELECT option_id, lang_value
240 FROM ' . PROFILE_FIELDS_LANG_TABLE . "
241 WHERE field_id = $field_id
242 AND lang_id = $lang_id
243 AND field_type = $field_type
244 ORDER BY option_id";
245 $result = $db->sql_query($sql);
246
247 while ($row = $db->sql_fetchrow($result))
248 {
249 $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
250 }
251 $db->sql_freeresult($result);
252 }
253 }
254
255 /**
256 * Submit profile field for validation
257 * @access public
258 */
259 function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
260 {
261 global $auth, $db, $user;
262
263 $sql_where = '';
264 switch ($mode)
265 {
266 case 'register':
267 // If the field is required we show it on the registration page
268 $sql_where .= ' AND f.field_show_on_reg = 1';
269 break;
270
271 case 'profile':
272 // Show hidden fields to moderators/admins
273 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
274 {
275 $sql_where .= ' AND f.field_show_profile = 1';
276 }
277 break;
278
279 default:
280 trigger_error('Wrong profile mode specified', E_USER_ERROR);
281 break;
282 }
283
284 $sql = 'SELECT l.*, f.*
285 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
286 WHERE l.lang_id = $lang_id
287 AND f.field_active = 1
288 $sql_where
289 AND l.field_id = f.field_id
290 ORDER BY f.field_order";
291 $result = $db->sql_query($sql);
292
293 while ($row = $db->sql_fetchrow($result))
294 {
295 $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
296 $check_value = $cp_data['pf_' . $row['field_ident']];
297
298 if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
299 {
300 // If not and only showing common error messages, use this one
301 $error = '';
302 switch ($cp_result)
303 {
304 case 'FIELD_INVALID_DATE':
305 case 'FIELD_REQUIRED':
306 $error = sprintf($user->lang[$cp_result], $row['lang_name']);
307 break;
308
309 case 'FIELD_TOO_SHORT':
310 case 'FIELD_TOO_SMALL':
311 $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_minlen']);
312 break;
313
314 case 'FIELD_TOO_LONG':
315 case 'FIELD_TOO_LARGE':
316 $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_maxlen']);
317 break;
318
319 case 'FIELD_INVALID_CHARS':
320 switch ($row['field_validation'])
321 {
322 case '[0-9]+':
323 $error = sprintf($user->lang[$cp_result . '_NUMBERS_ONLY'], $row['lang_name']);
324 break;
325
326 case '[\w]+':
327 $error = sprintf($user->lang[$cp_result . '_ALPHA_ONLY'], $row['lang_name']);
328 break;
329
330 case '[\w_\+\. \-\[\]]+':
331 $error = sprintf($user->lang[$cp_result . '_SPACERS_ONLY'], $row['lang_name']);
332 break;
333 }
334 break;
335 }
336
337 if ($error != '')
338 {
339 $cp_error[] = $error;
340 }
341 }
342 }
343 $db->sql_freeresult($result);
344 }
345
346 /**
347 * Update profile field data directly
348 */
349 function update_profile_field_data($user_id, &$cp_data)
350 {
351 global $db;
352
353 if (!sizeof($cp_data))
354 {
355 return;
356 }
357
358 switch ($db->sql_layer)
359 {
360 case 'oracle':
361 case 'firebird':
362 case 'postgres':
363 $right_delim = $left_delim = '"';
364 break;
365
366 case 'sqlite':
367 case 'mssql':
368 case 'mssql_odbc':
369 $right_delim = ']';
370 $left_delim = '[';
371 break;
372
373 case 'mysql':
374 case 'mysql4':
375 case 'mysqli':
376 $right_delim = $left_delim = '`';
377 break;
378 }
379
380 // use new array for the UPDATE; changes in the key do not affect the original array
381 $cp_data_sql = array();
382 foreach ($cp_data as $key => $value)
383 {
384 // Firebird is case sensitive with delimiter
385 $cp_data_sql[$left_delim . (($db->sql_layer == 'firebird' || $db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value;
386 }
387
388 $sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . '
389 SET ' . $db->sql_build_array('UPDATE', $cp_data_sql) . "
390 WHERE user_id = $user_id";
391 $db->sql_query($sql);
392
393 if (!$db->sql_affectedrows())
394 {
395 $cp_data_sql['user_id'] = (int) $user_id;
396
397 $db->sql_return_on_error(true);
398
399 $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp_data_sql);
400 $db->sql_query($sql);
401
402 $db->sql_return_on_error(false);
403 }
404 }
405
406 /**
407 * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
408 * This is directly connected to the user -> mode == grab is to grab the user specific fields, mode == show is for assigning the row to the template
409 * @access public
410 */
411 function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
412 {
413 global $db;
414
415 if ($mode == 'grab')
416 {
417 if (!is_array($user_id))
418 {
419 $user_id = array($user_id);
420 }
421
422 if (!sizeof($this->profile_cache))
423 {
424 $this->build_cache();
425 }
426
427 if (!sizeof($user_id))
428 {
429 return array();
430 }
431
432 $sql = 'SELECT *
433 FROM ' . PROFILE_FIELDS_DATA_TABLE . '
434 WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
435 $result = $db->sql_query($sql);
436
437 $field_data = array();
438 while ($row = $db->sql_fetchrow($result))
439 {
440 $field_data[$row['user_id']] = $row;
441 }
442 $db->sql_freeresult($result);
443
444 $user_fields = array();
445
446 // Go through the fields in correct order
447 foreach (array_keys($this->profile_cache) as $used_ident)
448 {
449 foreach ($field_data as $user_id => $row)
450 {
451 $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
452 $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
453 }
454 }
455
456 return $user_fields;
457 }
458 else if ($mode == 'show')
459 {
460 // $profile_row == $user_fields[$row['user_id']];
461 $tpl_fields = array();
462 $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
463
464 foreach ($profile_row as $ident => $ident_ary)
465 {
466 $value = $this->get_profile_value($ident_ary);
467
468 if ($value === NULL)
469 {
470 continue;
471 }
472
473 $tpl_fields['row'] += array(
474 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value,
475 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'],
476 'PROFILE_' . strtoupper($ident) . '_NAME' => $ident_ary['data']['lang_name'],
477 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
478
479 'S_PROFILE_' . strtoupper($ident) => true
480 );
481
482 $tpl_fields['blockrow'][] = array(
483 'PROFILE_FIELD_VALUE' => $value,
484 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'],
485 'PROFILE_FIELD_NAME' => $ident_ary['data']['lang_name'],
486 'PROFILE_FIELD_EXPLAIN' => $ident_ary['data']['lang_explain'],
487
488 'S_PROFILE_' . strtoupper($ident) => true
489 );
490 }
491
492 return $tpl_fields;
493 }
494 else
495 {
496 trigger_error('Wrong mode for custom profile', E_USER_ERROR);
497 }
498 }
499
500 /**
501 * Get Profile Value for display
502 */
503 function get_profile_value($ident_ary)
504 {
505 $value = $ident_ary['value'];
506 $field_type = $ident_ary['data']['field_type'];
507
508 switch ($this->profile_types[$field_type])
509 {
510 case 'int':
511 if ($value === '')
512 {
513 return NULL;
514 }
515 return (int) $value;
516 break;
517
518 case 'string':
519 case 'text':
520 if (!$value)
521 {
522 return NULL;
523 }
524
525 $value = make_clickable($value);
526 $value = censor_text($value);
527 $value = bbcode_nl2br($value);
528 return $value;
529 break;
530
531 // case 'datetime':
532 case 'date':
533 $date = explode('-', $value);
534 $day = (isset($date[0])) ? (int) $date[0] : 0;
535 $month = (isset($date[1])) ? (int) $date[1] : 0;
536 $year = (isset($date[2])) ? (int) $date[2] : 0;
537
538 if (!$day && !$month && !$year)
539 {
540 return NULL;
541 }
542 else if ($day && $month && $year)
543 {
544 global $user;
545 // d/m/y 00:00 GMT isn't necessarily on the same d/m/y in the user's timezone, so add the timezone seconds
546 return $user->format_date(gmmktime(0, 0, 0, $month, $day, $year) + $user->timezone + $user->dst, $user->lang['DATE_FORMAT'], true);
547 }
548
549 return $value;
550 break;
551
552 case 'dropdown':
553 $field_id = $ident_ary['data']['field_id'];
554 $lang_id = $ident_ary['data']['lang_id'];
555 if (!isset($this->options_lang[$field_id][$lang_id]))
556 {
557 $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
558 }
559
560 if ($value == $ident_ary['data']['field_novalue'])
561 {
562 return NULL;
563 }
564
565 $value = (int) $value;
566
567 // User not having a value assigned
568 if (!isset($this->options_lang[$field_id][$lang_id][$value]))
569 {
570 return NULL;
571 }
572
573 return $this->options_lang[$field_id][$lang_id][$value];
574 break;
575
576 case 'bool':
577 $field_id = $ident_ary['data']['field_id'];
578 $lang_id = $ident_ary['data']['lang_id'];
579 if (!isset($this->options_lang[$field_id][$lang_id]))
580 {
581 $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
582 }
583
584 if ($ident_ary['data']['field_length'] == 1)
585 {
586 return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
587 }
588 else if (!$value)
589 {
590 return NULL;
591 }
592 else
593 {
594 return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1];
595 }
596 break;
597
598 default:
599 trigger_error('Unknown profile type', E_USER_ERROR);
600 break;
601 }
602 }
603
604 /**
605 * Get field value for registration/profile
606 * @access private
607 */
608 function get_var($field_validation, &$profile_row, $default_value, $preview)
609 {
610 global $user;
611
612 $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
613 $user_ident = $profile_row['field_ident'];
614 // checkbox - only testing for isset
615 if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2)
616 {
617 $value = (isset($_REQUEST[$profile_row['field_ident']])) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
618 }
619 else if ($profile_row['field_type'] == FIELD_INT)
620 {
621 if (isset($_REQUEST[$profile_row['field_ident']]))
622 {
623 $value = ($_REQUEST[$profile_row['field_ident']] === '') ? NULL : request_var($profile_row['field_ident'], $default_value);
624 }
625 else
626 {
627 if (!$preview && array_key_exists($user_ident, $user->profile_fields) && is_null($user->profile_fields[$user_ident]))
628 {
629 $value = NULL;
630 }
631 else if (!isset($user->profile_fields[$user_ident]) || $preview)
632 {
633 $value = $default_value;
634 }
635 else
636 {
637 $value = $user->profile_fields[$user_ident];
638 }
639 }
640
641 return (is_null($value) || $value === '') ? '' : (int) $value;
642 }
643 else
644 {
645 $value = (isset($_REQUEST[$profile_row['field_ident']])) ? request_var($profile_row['field_ident'], $default_value, true) : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
646
647 if (gettype($value) == 'string')
648 {
649 $value = utf8_normalize_nfc($value);
650 }
651 }
652
653 switch ($field_validation)
654 {
655 case 'int':
656 return (int) $value;
657 break;
658 }
659
660 return $value;
661 }
662
663 /**
664 * Process int-type
665 * @access private
666 */
667 function generate_int($profile_row, $preview = false)
668 {
669 global $template;
670
671 $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
672 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
673 }
674
675 /**
676 * Process date-type
677 * @access private
678 */
679 function generate_date($profile_row, $preview = false)
680 {
681 global $user, $template;
682
683 $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
684 $user_ident = $profile_row['field_ident'];
685
686 $now = getdate();
687
688 if (!isset($_REQUEST[$profile_row['field_ident'] . '_day']))
689 {
690 if ($profile_row['field_default_value'] == 'now')
691 {
692 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
693 }
694 list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
695 }
696 else
697 {
698 if ($preview && $profile_row['field_default_value'] == 'now')
699 {
700 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
701 list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
702 }
703 else
704 {
705 $day = request_var($profile_row['field_ident'] . '_day', 0);
706 $month = request_var($profile_row['field_ident'] . '_month', 0);
707 $year = request_var($profile_row['field_ident'] . '_year', 0);
708 }
709 }
710
711 $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
712 for ($i = 1; $i < 32; $i++)
713 {
714 $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
715 }
716
717 $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
718 for ($i = 1; $i < 13; $i++)
719 {
720 $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
721 }
722
723 $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
724 for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++)
725 {
726 $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
727 }
728 unset($now);
729
730 $profile_row['field_value'] = 0;
731 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
732 }
733
734 /**
735 * Process bool-type
736 * @access private
737 */
738 function generate_bool($profile_row, $preview = false)
739 {
740 global $template;
741
742 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
743
744 $profile_row['field_value'] = $value;
745 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
746
747 if ($profile_row['field_length'] == 1)
748 {
749 if (!isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
750 {
751 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview);
752 }
753
754 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
755 {
756 $template->assign_block_vars('bool.options', array(
757 'OPTION_ID' => $option_id,
758 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '',
759 'VALUE' => $option_value)
760 );
761 }
762 }
763 }
764
765 /**
766 * Process string-type
767 * @access private
768 */
769 function generate_string($profile_row, $preview = false)
770 {
771 global $template;
772
773 $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
774 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
775 }
776
777 /**
778 * Process text-type
779 * @access private
780 */
781 function generate_text($profile_row, $preview = false)
782 {
783 global $template;
784 global $user, $phpEx, $phpbb_root_path;
785
786 $field_length = explode('|', $profile_row['field_length']);
787 $profile_row['field_rows'] = $field_length[0];
788 $profile_row['field_cols'] = $field_length[1];
789
790 $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
791 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
792 }
793
794 /**
795 * Process dropdown-type
796 * @access private
797 */
798 function generate_dropdown($profile_row, $preview = false)
799 {
800 global $user, $template;
801
802 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
803
804 if (!isset($this->options_lang[$profile_row['field_id']]) || !isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
805 {
806 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview);
807 }
808
809 $profile_row['field_value'] = $value;
810 $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
811
812 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
813 {
814 $template->assign_block_vars('dropdown.options', array(
815 'OPTION_ID' => $option_id,
816 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '',
817 'VALUE' => $option_value)
818 );
819 }
820 }
821
822 /**
823 * Return Templated value/field. Possible values for $mode are:
824 * change == user is able to set/enter profile values; preview == just show the value
825 * @access private
826 */
827 function process_field_row($mode, $profile_row)
828 {
829 global $template;
830
831 $preview = ($mode == 'preview') ? true : false;
832
833 // set template filename
834 $template->set_filenames(array(
835 'cp_body' => 'custom_profile_fields.html')
836 );
837
838 // empty previously filled blockvars
839 foreach ($this->profile_types as $field_case => $field_type)
840 {
841 $template->destroy_block_vars($field_type);
842 }
843
844 // Assign template variables
845 $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']];
846 $this->$type_func($profile_row, $preview);
847
848 // Return templated data
849 return $template->assign_display('cp_body');
850 }
851
852 /**
853 * Build Array for user insertion into custom profile fields table
854 */
855 function build_insert_sql_array($cp_data)
856 {
857 global $db, $user, $auth;
858
859 $sql_not_in = array();
860 foreach ($cp_data as $key => $null)
861 {
862 $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
863 }
864
865 $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value
866 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
867 WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
868 ' . ((sizeof($sql_not_in)) ? ' AND ' . $db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . '
869 AND l.field_id = f.field_id';
870 $result = $db->sql_query($sql);
871
872 while ($row = $db->sql_fetchrow($result))
873 {
874 if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
875 {
876 $now = getdate();
877 $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
878 }
879
880 $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value'];
881 }
882 $db->sql_freeresult($result);
883
884 return $cp_data;
885 }
886
887 /**
888 * Get profile field value on submit
889 * @access private
890 */
891 function get_profile_field($profile_row)
892 {
893 global $phpbb_root_path, $phpEx;
894 global $config;
895
896 $var_name = 'pf_' . $profile_row['field_ident'];
897
898 switch ($profile_row['field_type'])
899 {
900 case FIELD_DATE:
901
902 if (!isset($_REQUEST[$var_name . '_day']))
903 {
904 if ($profile_row['field_default_value'] == 'now')
905 {
906 $now = getdate();
907 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
908 }
909 list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
910 }
911 else
912 {
913 $day = request_var($var_name . '_day', 0);
914 $month = request_var($var_name . '_month', 0);
915 $year = request_var($var_name . '_year', 0);
916 }
917
918 $var = sprintf('%2d-%2d-%4d', $day, $month, $year);
919 break;
920
921 case FIELD_BOOL:
922 // Checkbox
923 if ($profile_row['field_length'] == 2)
924 {
925 $var = (isset($_REQUEST[$var_name])) ? 1 : 0;
926 }
927 else
928 {
929 $var = request_var($var_name, (int) $profile_row['field_default_value']);
930 }
931 break;
932
933 case FIELD_STRING:
934 case FIELD_TEXT:
935 $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true));
936 break;
937
938 case FIELD_INT:
939 if (isset($_REQUEST[$var_name]) && $_REQUEST[$var_name] === '')
940 {
941 $var = NULL;
942 }
943 else
944 {
945 $var = request_var($var_name, (int) $profile_row['field_default_value']);
946 }
947 break;
948
949 case FIELD_DROPDOWN:
950 $var = request_var($var_name, (int) $profile_row['field_default_value']);
951 break;
952
953 default:
954 $var = request_var($var_name, $profile_row['field_default_value']);
955 break;
956 }
957
958 return $var;
959 }
960}
961
962/**
963* Custom Profile Fields ACP
964* @package phpBB3
965*/
966class custom_profile_admin extends custom_profile
967{
968 var $vars = array();
969
970 /**
971 * Return possible validation options
972 */
973 function validate_options()
974 {
975 global $user;
976
977 $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
978
979 $validate_options = '';
980 foreach ($validate_ary as $lang => $value)
981 {
982 $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
983 $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
984 }
985
986 return $validate_options;
987 }
988
989 /**
990 * Get string options for second step in ACP
991 */
992 function get_string_options()
993 {
994 global $user;
995
996 $options = array(
997 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
998 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
999 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
1000 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
1001 );
1002
1003 return $options;
1004 }
1005
1006 /**
1007 * Get text options for second step in ACP
1008 */
1009 function get_text_options()
1010 {
1011 global $user;
1012
1013 $options = array(
1014 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input name="rows" size="5" value="' . $this->vars['rows'] . '" /> ' . $user->lang['ROWS'] . '</dd><dd><input name="columns" size="5" value="' . $this->vars['columns'] . '" /> ' . $user->lang['COLUMNS'] . ' <input type="hidden" name="field_length" value="' . $this->vars['field_length'] . '" />'),
1015 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_minlen" size="10" value="' . $this->vars['field_minlen'] . '" />'),
1016 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_maxlen" size="10" value="' . $this->vars['field_maxlen'] . '" />'),
1017 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
1018 );
1019
1020 return $options;
1021 }
1022
1023 /**
1024 * Get int options for second step in ACP
1025 */
1026 function get_int_options()
1027 {
1028 global $user;
1029
1030 $options = array(
1031 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
1032 1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'], 'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
1033 2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'], 'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
1034 3 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => '<input type="post" name="field_default_value" value="' . $this->vars['field_default_value'] . '" />')
1035 );
1036
1037 return $options;
1038 }
1039
1040 /**
1041 * Get bool options for second step in ACP
1042 */
1043 function get_bool_options()
1044 {
1045 global $user, $config, $lang_defs;
1046
1047 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1048
1049 $profile_row = array(
1050 'var_name' => 'field_default_value',
1051 'field_id' => 1,
1052 'lang_name' => $this->vars['lang_name'],
1053 'lang_explain' => $this->vars['lang_explain'],
1054 'lang_id' => $default_lang_id,
1055 'field_default_value' => $this->vars['field_default_value'],
1056 'field_ident' => 'field_default_value',
1057 'field_type' => FIELD_BOOL,
1058 'field_length' => $this->vars['field_length'],
1059 'lang_options' => $this->vars['lang_options']
1060 );
1061
1062 $options = array(
1063 0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<label><input type="radio" class="radio" name="field_length" value="1"' . (($this->vars['field_length'] == 1) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['RADIO_BUTTONS'] . '</label><label><input type="radio" class="radio" name="field_length" value="2"' . (($this->vars['field_length'] == 2) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['CHECKBOX'] . '</label>'),
1064 1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row))
1065 );
1066
1067 return $options;
1068 }
1069
1070 /**
1071 * Get dropdown options for second step in ACP
1072 */
1073 function get_dropdown_options()
1074 {
1075 global $user, $config, $lang_defs;
1076
1077 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1078
1079 $profile_row[0] = array(
1080 'var_name' => 'field_default_value',
1081 'field_id' => 1,
1082 'lang_name' => $this->vars['lang_name'],
1083 'lang_explain' => $this->vars['lang_explain'],
1084 'lang_id' => $default_lang_id,
1085 'field_default_value' => $this->vars['field_default_value'],
1086 'field_ident' => 'field_default_value',
1087 'field_type' => FIELD_DROPDOWN,
1088 'lang_options' => $this->vars['lang_options']
1089 );
1090
1091 $profile_row[1] = $profile_row[0];
1092 $profile_row[1]['var_name'] = 'field_novalue';
1093 $profile_row[1]['field_ident'] = 'field_novalue';
1094 $profile_row[1]['field_default_value'] = $this->vars['field_novalue'];
1095
1096 $options = array(
1097 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
1098 1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1]))
1099 );
1100
1101 return $options;
1102 }
1103
1104 /**
1105 * Get date options for second step in ACP
1106 */
1107 function get_date_options()
1108 {
1109 global $user, $config, $lang_defs;
1110
1111 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1112
1113 $profile_row = array(
1114 'var_name' => 'field_default_value',
1115 'lang_name' => $this->vars['lang_name'],
1116 'lang_explain' => $this->vars['lang_explain'],
1117 'lang_id' => $default_lang_id,
1118 'field_default_value' => $this->vars['field_default_value'],
1119 'field_ident' => 'field_default_value',
1120 'field_type' => FIELD_DATE,
1121 'field_length' => $this->vars['field_length']
1122 );
1123
1124 $always_now = request_var('always_now', -1);
1125 if ($always_now == -1)
1126 {
1127 $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false;
1128 }
1129 else
1130 {
1131 $s_checked = ($always_now) ? true : false;
1132 }
1133
1134 $options = array(
1135 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
1136 1 => array('TITLE' => $user->lang['ALWAYS_TODAY'], 'FIELD' => '<label><input type="radio" class="radio" name="always_now" value="1"' . (($s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['YES'] . '</label><label><input type="radio" class="radio" name="always_now" value="0"' . ((!$s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['NO'] . '</label>'),
1137 );
1138
1139 return $options;
1140 }
1141}
1142
1143?>
Note: See TracBrowser for help on using the repository browser.