(a little test for later merges)
[phpbb.git] / phpBB / includes / functions_profile_fields.php
blob9e356414a92c699d03d88943baca47ea6a95ac1f
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Custom Profile Fields
21 * @package phpBB3
23 class custom_profile
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();
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
34 function generate_profile_fields($mode, $lang_id)
36 global $db, $template, $auth;
38 $sql_where = '';
39 switch ($mode)
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;
46 case 'profile':
47 // Show hidden fields to moderators/admins
48 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
50 $sql_where .= ' AND f.field_show_profile = 1';
52 break;
54 default:
55 trigger_error('Wrong profile mode specified', E_USER_ERROR);
56 break;
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);
68 while ($row = $db->sql_fetchrow($result))
70 // Return templated field
71 $tpl_snippet = $this->process_field_row('change', $row);
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'];
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)
84 $db->sql_freeresult($result);
87 /**
88 * Validate entered profile field data
89 * @access public
91 function validate_profile_field($field_type, &$field_value, $field_data)
93 switch ($field_type)
95 case FIELD_DATE:
96 $field_validate = explode('-', $field_value);
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;
102 if ((!$day || !$month || !$year) && !$field_data['field_required'])
104 return false;
107 if ((!$day || !$month || !$year) && $field_data['field_required'])
109 return 'FIELD_REQUIRED';
112 if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
114 return 'FIELD_INVALID_DATE';
117 if (checkdate($month, $day, $year) === false)
119 return 'FIELD_INVALID_DATE';
121 break;
123 case FIELD_BOOL:
124 $field_value = (bool) $field_value;
126 if (!$field_value && $field_data['field_required'])
128 return 'FIELD_REQUIRED';
130 break;
132 case FIELD_INT:
133 if (trim($field_value) === '' && !$field_data['field_required'])
135 return false;
138 $field_value = (int) $field_value;
140 if ($field_value < $field_data['field_minlen'])
142 return 'FIELD_TOO_SMALL';
144 else if ($field_value > $field_data['field_maxlen'])
146 return 'FIELD_TOO_LARGE';
148 break;
150 case FIELD_DROPDOWN:
151 $field_value = (int) $field_value;
153 if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
155 return 'FIELD_REQUIRED';
157 break;
159 case FIELD_STRING:
160 case FIELD_TEXT:
161 if (empty($field_value) && !$field_data['field_required'])
163 return false;
165 else if (empty($field_value) && $field_data['field_required'])
167 return 'FIELD_REQUIRED';
170 if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
172 return 'FIELD_TOO_SHORT';
174 else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
176 return 'FIELD_TOO_LONG';
179 if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
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))
184 return 'FIELD_INVALID_CHARS';
187 break;
190 return false;
194 * Build profile cache, used for display
195 * @access private
197 function build_cache()
199 global $db, $user, $auth;
201 $this->profile_cache = array();
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);
214 while ($row = $db->sql_fetchrow($result))
216 $this->profile_cache[$row['field_ident']] = $row;
218 $db->sql_freeresult($result);
222 * Get language entries for options and store them here for later use
224 function get_option_lang($field_id, $lang_id, $field_type, $preview)
226 global $db;
228 if ($preview)
230 $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
232 foreach ($lang_options as $num => $var)
234 $this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
237 else
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);
247 while ($row = $db->sql_fetchrow($result))
249 $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
251 $db->sql_freeresult($result);
256 * Submit profile field for validation
257 * @access public
259 function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
261 global $auth, $db, $user;
263 $sql_where = '';
264 switch ($mode)
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;
271 case 'profile':
272 // Show hidden fields to moderators/admins
273 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
275 $sql_where .= ' AND f.field_show_profile = 1';
277 break;
279 default:
280 trigger_error('Wrong profile mode specified', E_USER_ERROR);
281 break;
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);
293 while ($row = $db->sql_fetchrow($result))
295 $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
296 $check_value = $cp_data['pf_' . $row['field_ident']];
298 if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
300 // If not and only showing common error messages, use this one
301 $error = '';
302 switch ($cp_result)
304 case 'FIELD_INVALID_DATE':
305 case 'FIELD_REQUIRED':
306 $error = sprintf($user->lang[$cp_result], $row['lang_name']);
307 break;
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;
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;
319 case 'FIELD_INVALID_CHARS':
320 switch ($row['field_validation'])
322 case '[0-9]+':
323 $error = sprintf($user->lang[$cp_result . '_NUMBERS_ONLY'], $row['lang_name']);
324 break;
326 case '[\w]+':
327 $error = sprintf($user->lang[$cp_result . '_ALPHA_ONLY'], $row['lang_name']);
328 break;
330 case '[\w_\+\. \-\[\]]+':
331 $error = sprintf($user->lang[$cp_result . '_SPACERS_ONLY'], $row['lang_name']);
332 break;
334 break;
337 if ($error != '')
339 $cp_error[] = $error;
343 $db->sql_freeresult($result);
347 * Update profile field data directly
349 function update_profile_field_data($user_id, &$cp_data)
351 global $db;
353 if (!sizeof($cp_data))
355 return;
358 switch ($db->sql_layer)
360 case 'oracle':
361 case 'firebird':
362 case 'postgres':
363 $right_delim = $left_delim = '"';
364 break;
366 case 'sqlite':
367 case 'mssql':
368 case 'mssql_odbc':
369 $right_delim = ']';
370 $left_delim = '[';
371 break;
373 case 'mysql':
374 case 'mysql4':
375 case 'mysqli':
376 $right_delim = $left_delim = '`';
377 break;
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)
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;
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);
393 if (!$db->sql_affectedrows())
395 $cp_data_sql['user_id'] = (int) $user_id;
397 $db->sql_return_on_error(true);
399 $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp_data_sql);
400 $db->sql_query($sql);
402 $db->sql_return_on_error(false);
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
411 function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
413 global $db;
415 if ($mode == 'grab')
417 if (!is_array($user_id))
419 $user_id = array($user_id);
422 if (!sizeof($this->profile_cache))
424 $this->build_cache();
427 if (!sizeof($user_id))
429 return array();
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);
437 $field_data = array();
438 while ($row = $db->sql_fetchrow($result))
440 $field_data[$row['user_id']] = $row;
442 $db->sql_freeresult($result);
444 $user_fields = array();
446 // Go through the fields in correct order
447 foreach (array_keys($this->profile_cache) as $used_ident)
449 foreach ($field_data as $user_id => $row)
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];
456 return $user_fields;
458 else if ($mode == 'show')
460 // $profile_row == $user_fields[$row['user_id']];
461 $tpl_fields = array();
462 $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
464 foreach ($profile_row as $ident => $ident_ary)
466 $value = $this->get_profile_value($ident_ary);
468 if ($value === NULL)
470 continue;
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'],
479 'S_PROFILE_' . strtoupper($ident) => true
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'],
488 'S_PROFILE_' . strtoupper($ident) => true
492 return $tpl_fields;
494 else
496 trigger_error('Wrong mode for custom profile', E_USER_ERROR);
501 * Get Profile Value for display
503 function get_profile_value($ident_ary)
505 $value = $ident_ary['value'];
506 $field_type = $ident_ary['data']['field_type'];
508 switch ($this->profile_types[$field_type])
510 case 'int':
511 if ($value === '')
513 return NULL;
515 return (int) $value;
516 break;
518 case 'string':
519 case 'text':
520 if (!$value)
522 return NULL;
525 $value = make_clickable($value);
526 $value = censor_text($value);
527 $value = bbcode_nl2br($value);
528 return $value;
529 break;
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;
538 if (!$day && !$month && !$year)
540 return NULL;
542 else if ($day && $month && $year)
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);
549 return $value;
550 break;
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]))
557 $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
560 if ($value == $ident_ary['data']['field_novalue'])
562 return NULL;
565 $value = (int) $value;
567 // User not having a value assigned
568 if (!isset($this->options_lang[$field_id][$lang_id][$value]))
570 return NULL;
573 return $this->options_lang[$field_id][$lang_id][$value];
574 break;
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]))
581 $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
584 if ($ident_ary['data']['field_length'] == 1)
586 return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
588 else if (!$value)
590 return NULL;
592 else
594 return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1];
596 break;
598 default:
599 trigger_error('Unknown profile type', E_USER_ERROR);
600 break;
605 * Get field value for registration/profile
606 * @access private
608 function get_var($field_validation, &$profile_row, $default_value, $preview)
610 global $user;
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)
617 $value = (isset($_REQUEST[$profile_row['field_ident']])) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
619 else if ($profile_row['field_type'] == FIELD_INT)
621 if (isset($_REQUEST[$profile_row['field_ident']]))
623 $value = ($_REQUEST[$profile_row['field_ident']] === '') ? NULL : request_var($profile_row['field_ident'], $default_value);
625 else
627 if (!$preview && isset($user->profile_fields[$user_ident]) && is_null($user->profile_fields[$user_ident]))
629 $value = NULL;
631 else if (!isset($user->profile_fields[$user_ident]) || $preview)
633 $value = $default_value;
635 else
637 $value = $user->profile_fields[$user_ident];
641 return (is_null($value) || $value === '') ? '' : (int) $value;
643 else
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]);
647 if (gettype($value) == 'string')
649 $value = utf8_normalize_nfc($value);
653 switch ($field_validation)
655 case 'int':
656 return (int) $value;
657 break;
660 return $value;
664 * Process int-type
665 * @access private
667 function generate_int($profile_row, $preview = false)
669 global $template;
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));
676 * Process date-type
677 * @access private
679 function generate_date($profile_row, $preview = false)
681 global $user, $template;
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'];
686 $now = getdate();
688 if (!isset($_REQUEST[$profile_row['field_ident'] . '_day']))
690 if ($profile_row['field_default_value'] == 'now')
692 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
694 list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
696 else
698 if ($preview && $profile_row['field_default_value'] == 'now')
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]));
703 else
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);
711 $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
712 for ($i = 1; $i < 32; $i++)
714 $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
717 $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
718 for ($i = 1; $i < 13; $i++)
720 $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
723 $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
724 for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++)
726 $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
728 unset($now);
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));
735 * Process bool-type
736 * @access private
738 function generate_bool($profile_row, $preview = false)
740 global $template;
742 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
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));
747 if ($profile_row['field_length'] == 1)
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']]))
751 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview);
754 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
756 $template->assign_block_vars('bool.options', array(
757 'OPTION_ID' => $option_id,
758 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '',
759 'VALUE' => $option_value)
766 * Process string-type
767 * @access private
769 function generate_string($profile_row, $preview = false)
771 global $template;
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));
778 * Process text-type
779 * @access private
781 function generate_text($profile_row, $preview = false)
783 global $template;
784 global $user, $phpEx, $phpbb_root_path;
786 $field_length = explode('|', $profile_row['field_length']);
787 $profile_row['field_rows'] = $field_length[0];
788 $profile_row['field_cols'] = $field_length[1];
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));
795 * Process dropdown-type
796 * @access private
798 function generate_dropdown($profile_row, $preview = false)
800 global $user, $template;
802 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
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']]))
806 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview);
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));
812 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
814 $template->assign_block_vars('dropdown.options', array(
815 'OPTION_ID' => $option_id,
816 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '',
817 'VALUE' => $option_value)
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
827 function process_field_row($mode, $profile_row)
829 global $template;
831 $preview = ($mode == 'preview') ? true : false;
833 // set template filename
834 $template->set_filenames(array(
835 'cp_body' => 'custom_profile_fields.html')
838 // empty previously filled blockvars
839 foreach ($this->profile_types as $field_case => $field_type)
841 $template->destroy_block_vars($field_type);
844 // Assign template variables
845 $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']];
846 $this->$type_func($profile_row, $preview);
848 // Return templated data
849 return $template->assign_display('cp_body');
853 * Build Array for user insertion into custom profile fields table
855 function build_insert_sql_array($cp_data)
857 global $db, $user, $auth;
859 $sql_not_in = array();
860 foreach ($cp_data as $key => $null)
862 $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
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);
872 while ($row = $db->sql_fetchrow($result))
874 if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
876 $now = getdate();
877 $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
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'];
882 $db->sql_freeresult($result);
884 return $cp_data;
888 * Get profile field value on submit
889 * @access private
891 function get_profile_field($profile_row)
893 global $phpbb_root_path, $phpEx;
894 global $config;
896 $var_name = 'pf_' . $profile_row['field_ident'];
898 switch ($profile_row['field_type'])
900 case FIELD_DATE:
902 if (!isset($_REQUEST[$var_name . '_day']))
904 if ($profile_row['field_default_value'] == 'now')
906 $now = getdate();
907 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
909 list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
911 else
913 $day = request_var($var_name . '_day', 0);
914 $month = request_var($var_name . '_month', 0);
915 $year = request_var($var_name . '_year', 0);
918 $var = sprintf('%2d-%2d-%4d', $day, $month, $year);
919 break;
921 case FIELD_BOOL:
922 // Checkbox
923 if ($profile_row['field_length'] == 2)
925 $var = (isset($_REQUEST[$var_name])) ? 1 : 0;
927 else
929 $var = request_var($var_name, (int) $profile_row['field_default_value']);
931 break;
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;
938 case FIELD_INT:
939 if (isset($_REQUEST[$var_name]) && $_REQUEST[$var_name] === '')
941 $var = NULL;
943 else
945 $var = request_var($var_name, (int) $profile_row['field_default_value']);
947 break;
949 case FIELD_DROPDOWN:
950 $var = request_var($var_name, (int) $profile_row['field_default_value']);
951 break;
953 default:
954 $var = request_var($var_name, $profile_row['field_default_value']);
955 break;
958 return $var;
963 * Custom Profile Fields ACP
964 * @package phpBB3
966 class custom_profile_admin extends custom_profile
968 var $vars = array();
971 * Return possible validation options
973 function validate_options()
975 global $user;
977 $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
979 $validate_options = '';
980 foreach ($validate_ary as $lang => $value)
982 $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
983 $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
986 return $validate_options;
990 * Get string options for second step in ACP
992 function get_string_options()
994 global $user;
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>')
1003 return $options;
1007 * Get text options for second step in ACP
1009 function get_text_options()
1011 global $user;
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>')
1020 return $options;
1024 * Get int options for second step in ACP
1026 function get_int_options()
1028 global $user;
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'] . '" />')
1037 return $options;
1041 * Get bool options for second step in ACP
1043 function get_bool_options()
1045 global $user, $config, $lang_defs;
1047 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
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']
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))
1067 return $options;
1071 * Get dropdown options for second step in ACP
1073 function get_dropdown_options()
1075 global $user, $config, $lang_defs;
1077 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
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']
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'];
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]))
1101 return $options;
1105 * Get date options for second step in ACP
1107 function get_date_options()
1109 global $user, $config, $lang_defs;
1111 $default_lang_id = $lang_defs['iso'][$config['default_lang']];
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']
1124 $always_now = request_var('always_now', -1);
1125 if ($always_now == -1)
1127 $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false;
1129 else
1131 $s_checked = ($always_now) ? true : false;
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>'),
1139 return $options;