From 2471b19d73c38359ed70a7c9e3a82a297946c5b8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 17 Sep 2013 15:17:54 +0200 Subject: [ticket/11201] Make profile field classes autoloadable PHPBB3-11201 --- phpBB/config/profilefields.yml | 23 + phpBB/config/services.yml | 1 + phpBB/includes/acp/acp_profile.php | 5 +- phpBB/includes/acp/acp_users.php | 3 +- phpBB/includes/functions_profile_fields.php | 1187 --------------------------- phpBB/includes/functions_user.php | 9 +- phpBB/includes/ucp/ucp_pm_viewmessage.php | 8 +- phpBB/includes/ucp/ucp_profile.php | 7 +- phpBB/includes/ucp/ucp_register.php | 4 +- phpBB/memberlist.php | 6 +- phpBB/phpbb/profilefields/admin.php | 191 +++++ phpBB/phpbb/profilefields/profilefields.php | 974 ++++++++++++++++++++++ phpBB/viewtopic.php | 6 +- 13 files changed, 1202 insertions(+), 1222 deletions(-) create mode 100644 phpBB/config/profilefields.yml delete mode 100644 phpBB/includes/functions_profile_fields.php create mode 100644 phpBB/phpbb/profilefields/admin.php create mode 100644 phpBB/phpbb/profilefields/profilefields.php diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml new file mode 100644 index 0000000000..3b9b1a60a0 --- /dev/null +++ b/phpBB/config/profilefields.yml @@ -0,0 +1,23 @@ +services: + profilefields: + class: \phpbb\profilefields\profilefields + arguments: + - @auth + - @dbal.conn + - @request + - @template + - @user + + profilefields.admin: + class: \phpbb\profilefields\admin + arguments: + - @auth + - @dbal.conn + - @request + - @template + - @user + + #migrator.tool.permission1: + # class: \phpbb\profilefields\profilefields + # tags: + # - { name: ffoobar.tool } diff --git a/phpBB/config/services.yml b/phpBB/config/services.yml index c11184d6b6..9c9a0f625c 100644 --- a/phpBB/config/services.yml +++ b/phpBB/config/services.yml @@ -8,6 +8,7 @@ imports: - { resource: auth_providers.yml } - { resource: console.yml } - { resource: mimetype_guessers.yml } + - { resource: profilefields.yml } services: acl.permissions: diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 6efd778b12..510239a837 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -29,11 +29,10 @@ class acp_profile { global $config, $db, $user, $auth, $template, $cache; global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix; - global $request; + global $request, $phpbb_container; include($phpbb_root_path . 'includes/functions_posting.' . $phpEx); include($phpbb_root_path . 'includes/functions_user.' . $phpEx); - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); $user->add_lang(array('ucp', 'acp/profile')); $this->tpl_name = 'acp_profile'; @@ -60,7 +59,7 @@ class acp_profile FIELD_DROPDOWN => array('field_length' => 0, 'field_minlen' => 0, 'field_maxlen' => 5, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0), ); - $cp = new custom_profile_admin(); + $cp = $phpbb_container->get('profilefields.admin'); // Build Language array // Based on this, we decide which elements need to be edited later and which language items are missing diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index b24adfc586..beb6fa1960 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1343,9 +1343,8 @@ class acp_users case 'profile': include($phpbb_root_path . 'includes/functions_user.' . $phpEx); - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $cp_data = $cp_error = array(); diff --git a/phpBB/includes/functions_profile_fields.php b/phpBB/includes/functions_profile_fields.php deleted file mode 100644 index 7dd0b0e87d..0000000000 --- a/phpBB/includes/functions_profile_fields.php +++ /dev/null @@ -1,1187 +0,0 @@ - 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date'); - var $profile_cache = array(); - var $options_lang = array(); - - /** - * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) - * Called by ucp_profile and ucp_register - * @access public - */ - function generate_profile_fields($mode, $lang_id) - { - global $db, $template, $auth; - - $sql_where = ''; - switch ($mode) - { - case 'register': - // If the field is required we show it on the registration page - $sql_where .= ' AND f.field_show_on_reg = 1'; - break; - - case 'profile': - // Show hidden fields to moderators/admins - if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) - { - $sql_where .= ' AND f.field_show_profile = 1'; - } - break; - - default: - trigger_error('Wrong profile mode specified', E_USER_ERROR); - break; - } - - $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f - WHERE f.field_active = 1 - $sql_where - AND l.lang_id = $lang_id - AND l.field_id = f.field_id - ORDER BY f.field_order"; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - // Return templated field - $tpl_snippet = $this->process_field_row('change', $row); - - // Some types are multivalue, we can't give them a field_id as we would not know which to pick - $type = (int) $row['field_type']; - - $template->assign_block_vars('profile_fields', array( - 'LANG_NAME' => $row['lang_name'], - 'LANG_EXPLAIN' => $row['lang_explain'], - 'FIELD' => $tpl_snippet, - 'FIELD_ID' => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'], - 'S_REQUIRED' => ($row['field_required']) ? true : false) - ); - } - $db->sql_freeresult($result); - } - - /** - * Validate entered profile field data - * @access public - */ - function validate_profile_field($field_type, &$field_value, $field_data) - { - switch ($field_type) - { - case FIELD_DATE: - $field_validate = explode('-', $field_value); - - $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; - $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; - $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0; - - if ((!$day || !$month || !$year) && !$field_data['field_required']) - { - return false; - } - - if ((!$day || !$month || !$year) && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - - if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) - { - return 'FIELD_INVALID_DATE'; - } - - if (checkdate($month, $day, $year) === false) - { - return 'FIELD_INVALID_DATE'; - } - break; - - case FIELD_BOOL: - $field_value = (bool) $field_value; - - if (!$field_value && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - break; - - case FIELD_INT: - if (trim($field_value) === '' && !$field_data['field_required']) - { - return false; - } - - $field_value = (int) $field_value; - - if ($field_value < $field_data['field_minlen']) - { - return 'FIELD_TOO_SMALL'; - } - else if ($field_value > $field_data['field_maxlen']) - { - return 'FIELD_TOO_LARGE'; - } - break; - - case FIELD_DROPDOWN: - $field_value = (int) $field_value; - - // retrieve option lang data if necessary - if (!isset($this->options_lang[$field_data['field_id']]) || !isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->options_lang[$file_data['field_id']][$field_data['lang_id']])) - { - $this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); - } - - if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) - { - return 'FIELD_INVALID_VALUE'; - } - - if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - break; - - case FIELD_STRING: - case FIELD_TEXT: - if (trim($field_value) === '' && !$field_data['field_required']) - { - return false; - } - else if (trim($field_value) === '' && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - - if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) - { - return 'FIELD_TOO_SHORT'; - } - else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) - { - return 'FIELD_TOO_LONG'; - } - - if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') - { - $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value); - if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) - { - return 'FIELD_INVALID_CHARS'; - } - } - break; - } - - return false; - } - - /** - * Build profile cache, used for display - * @access private - */ - function build_cache() - { - global $db, $user, $auth; - - $this->profile_cache = array(); - - // Display hidden/no_view fields for admin/moderator - $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f - WHERE l.lang_id = ' . $user->get_iso_lang_id() . ' - AND f.field_active = 1 ' . - ((!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . ' - AND f.field_no_view = 0 - AND l.field_id = f.field_id - ORDER BY f.field_order'; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $this->profile_cache[$row['field_ident']] = $row; - } - $db->sql_freeresult($result); - } - - /** - * Get language entries for options and store them here for later use - */ - function get_option_lang($field_id, $lang_id, $field_type, $preview) - { - global $db; - - if ($preview) - { - $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options']; - - foreach ($lang_options as $num => $var) - { - $this->options_lang[$field_id][$lang_id][($num + 1)] = $var; - } - } - else - { - $sql = 'SELECT option_id, lang_value - FROM ' . PROFILE_FIELDS_LANG_TABLE . " - WHERE field_id = $field_id - AND lang_id = $lang_id - AND field_type = $field_type - ORDER BY option_id"; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value']; - } - $db->sql_freeresult($result); - } - } - - /** - * Submit profile field for validation - * @access public - */ - function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) - { - global $auth, $db, $user; - - $sql_where = ''; - switch ($mode) - { - case 'register': - // If the field is required we show it on the registration page - $sql_where .= ' AND f.field_show_on_reg = 1'; - break; - - case 'profile': - // Show hidden fields to moderators/admins - if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) - { - $sql_where .= ' AND f.field_show_profile = 1'; - } - break; - - default: - trigger_error('Wrong profile mode specified', E_USER_ERROR); - break; - } - - $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f - WHERE l.lang_id = $lang_id - AND f.field_active = 1 - $sql_where - AND l.field_id = f.field_id - ORDER BY f.field_order"; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row); - $check_value = $cp_data['pf_' . $row['field_ident']]; - - if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false) - { - // If not and only showing common error messages, use this one - $error = ''; - switch ($cp_result) - { - case 'FIELD_INVALID_DATE': - case 'FIELD_INVALID_VALUE': - case 'FIELD_REQUIRED': - $error = $user->lang($cp_result, $row['lang_name']); - break; - - case 'FIELD_TOO_SHORT': - case 'FIELD_TOO_SMALL': - $error = $user->lang($cp_result, (int) $row['field_minlen'], $row['lang_name']); - break; - - case 'FIELD_TOO_LONG': - case 'FIELD_TOO_LARGE': - $error = $user->lang($cp_result, (int) $row['field_maxlen'], $row['lang_name']); - break; - - case 'FIELD_INVALID_CHARS': - switch ($row['field_validation']) - { - case '[0-9]+': - $error = $user->lang($cp_result . '_NUMBERS_ONLY', $row['lang_name']); - break; - - case '[\w]+': - $error = $user->lang($cp_result . '_ALPHA_ONLY', $row['lang_name']); - break; - - case '[\w_\+\. \-\[\]]+': - $error = $user->lang($cp_result . '_SPACERS_ONLY', $row['lang_name']); - break; - } - break; - } - - if ($error != '') - { - $cp_error[] = $error; - } - } - } - $db->sql_freeresult($result); - } - - /** - * Update profile field data directly - */ - function update_profile_field_data($user_id, &$cp_data) - { - global $db; - - if (!sizeof($cp_data)) - { - return; - } - - switch ($db->sql_layer) - { - case 'oracle': - case 'firebird': - case 'postgres': - $right_delim = $left_delim = '"'; - break; - - case 'sqlite': - case 'mssql': - case 'mssql_odbc': - case 'mssqlnative': - $right_delim = ']'; - $left_delim = '['; - break; - - case 'mysql': - case 'mysql4': - case 'mysqli': - $right_delim = $left_delim = '`'; - break; - } - - // use new array for the UPDATE; changes in the key do not affect the original array - $cp_data_sql = array(); - foreach ($cp_data as $key => $value) - { - // Firebird is case sensitive with delimiter - $cp_data_sql[$left_delim . (($db->sql_layer == 'firebird' || $db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value; - } - - $sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . ' - SET ' . $db->sql_build_array('UPDATE', $cp_data_sql) . " - WHERE user_id = $user_id"; - $db->sql_query($sql); - - if (!$db->sql_affectedrows()) - { - $cp_data_sql['user_id'] = (int) $user_id; - - $db->sql_return_on_error(true); - - $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp_data_sql); - $db->sql_query($sql); - - $db->sql_return_on_error(false); - } - } - - /** - * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled) - * 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 - * @access public - */ - function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) - { - global $db; - - if ($mode == 'grab') - { - if (!is_array($user_id)) - { - $user_id = array($user_id); - } - - if (!sizeof($this->profile_cache)) - { - $this->build_cache(); - } - - if (!sizeof($user_id)) - { - return array(); - } - - $sql = 'SELECT * - FROM ' . PROFILE_FIELDS_DATA_TABLE . ' - WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id)); - $result = $db->sql_query($sql); - - $field_data = array(); - while ($row = $db->sql_fetchrow($result)) - { - $field_data[$row['user_id']] = $row; - } - $db->sql_freeresult($result); - - $user_fields = array(); - - $user_ids = $user_id; - - // Go through the fields in correct order - foreach (array_keys($this->profile_cache) as $used_ident) - { - foreach ($field_data as $user_id => $row) - { - $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident]; - $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; - } - - foreach ($user_ids as $user_id) - { - if (!isset($user_fields[$user_id][$used_ident]) && $this->profile_cache[$used_ident]['field_show_novalue']) - { - $user_fields[$user_id][$used_ident]['value'] = ''; - $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; - } - } - } - - return $user_fields; - } - else if ($mode == 'show') - { - // $profile_row == $user_fields[$row['user_id']]; - $tpl_fields = array(); - $tpl_fields['row'] = $tpl_fields['blockrow'] = array(); - - foreach ($profile_row as $ident => $ident_ary) - { - $value = $this->get_profile_value($ident_ary); - - if ($value === NULL) - { - continue; - } - - $tpl_fields['row'] += array( - 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value, - 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_' . strtoupper($ident) . '_NAME' => $ident_ary['data']['lang_name'], - 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'], - - 'S_PROFILE_' . strtoupper($ident) => true - ); - - $tpl_fields['blockrow'][] = array( - 'PROFILE_FIELD_VALUE' => $value, - 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_FIELD_NAME' => $ident_ary['data']['lang_name'], - 'PROFILE_FIELD_EXPLAIN' => $ident_ary['data']['lang_explain'], - - 'S_PROFILE_' . strtoupper($ident) => true - ); - } - - return $tpl_fields; - } - else - { - trigger_error('Wrong mode for custom profile', E_USER_ERROR); - } - } - - /** - * Get Profile Value for display - */ - function get_profile_value($ident_ary) - { - $value = $ident_ary['value']; - $field_type = $ident_ary['data']['field_type']; - - switch ($this->profile_types[$field_type]) - { - case 'int': - if ($value === '' && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - return (int) $value; - break; - - case 'string': - case 'text': - if (!$value && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - - $value = make_clickable($value); - $value = censor_text($value); - $value = bbcode_nl2br($value); - return $value; - break; - - // case 'datetime': - case 'date': - $date = explode('-', $value); - $day = (isset($date[0])) ? (int) $date[0] : 0; - $month = (isset($date[1])) ? (int) $date[1] : 0; - $year = (isset($date[2])) ? (int) $date[2] : 0; - - if (!$day && !$month && !$year && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - else if ($day && $month && $year) - { - global $user; - // Date should display as the same date for every user regardless of timezone - - return $user->create_datetime() - ->setDate($year, $month, $day) - ->setTime(0, 0, 0) - ->format($user->lang['DATE_FORMAT'], true); - } - - return $value; - break; - - case 'dropdown': - $field_id = $ident_ary['data']['field_id']; - $lang_id = $ident_ary['data']['lang_id']; - if (!isset($this->options_lang[$field_id][$lang_id])) - { - $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); - } - - if ($value == $ident_ary['data']['field_novalue'] && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - - $value = (int) $value; - - // User not having a value assigned - if (!isset($this->options_lang[$field_id][$lang_id][$value])) - { - if ($ident_ary['data']['field_show_novalue']) - { - $value = $ident_ary['data']['field_novalue']; - } - else - { - return NULL; - } - } - - return $this->options_lang[$field_id][$lang_id][$value]; - break; - - case 'bool': - $field_id = $ident_ary['data']['field_id']; - $lang_id = $ident_ary['data']['lang_id']; - if (!isset($this->options_lang[$field_id][$lang_id])) - { - $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); - } - - if (!$value && $ident_ary['data']['field_show_novalue']) - { - $value = $ident_ary['data']['field_default_value']; - } - - if ($ident_ary['data']['field_length'] == 1) - { - return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL; - } - else if (!$value) - { - return NULL; - } - else - { - return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1]; - } - break; - - default: - trigger_error('Unknown profile type', E_USER_ERROR); - break; - } - } - - /** - * Get field value for registration/profile - * @access private - */ - function get_var($field_validation, &$profile_row, $default_value, $preview) - { - global $user; - global $request; - - $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; - $user_ident = $profile_row['field_ident']; - // checkbox - set the value to "true" if it has been set to 1 - if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2) - { - $value = (isset($_REQUEST[$profile_row['field_ident']]) && request_var($profile_row['field_ident'], $default_value) == 1) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]); - } - else if ($profile_row['field_type'] == FIELD_INT) - { - if (isset($_REQUEST[$profile_row['field_ident']])) - { - $value = ($request->variable($profile_row['field_ident'], '') === '') ? NULL : $request->variable($profile_row['field_ident'], $default_value); - } - else - { - if (!$preview && array_key_exists($user_ident, $user->profile_fields) && is_null($user->profile_fields[$user_ident])) - { - $value = NULL; - } - else if (!isset($user->profile_fields[$user_ident]) || $preview) - { - $value = $default_value; - } - else - { - $value = $user->profile_fields[$user_ident]; - } - } - - return (is_null($value) || $value === '') ? '' : (int) $value; - } - else - { - $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]); - - if (gettype($value) == 'string') - { - $value = utf8_normalize_nfc($value); - } - } - - switch ($field_validation) - { - case 'int': - return (int) $value; - break; - } - - return $value; - } - - /** - * Process int-type - * @access private - */ - function generate_int($profile_row, $preview = false) - { - global $template; - - $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process date-type - * @access private - */ - function generate_date($profile_row, $preview = false) - { - global $user, $template; - - $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; - $user_ident = $profile_row['field_ident']; - - $now = getdate(); - - if (!isset($_REQUEST[$profile_row['field_ident'] . '_day'])) - { - if ($profile_row['field_default_value'] == 'now') - { - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident])); - } - else - { - if ($preview && $profile_row['field_default_value'] == 'now') - { - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident])); - } - else - { - $day = request_var($profile_row['field_ident'] . '_day', 0); - $month = request_var($profile_row['field_ident'] . '_month', 0); - $year = request_var($profile_row['field_ident'] . '_year', 0); - } - } - - $profile_row['s_day_options'] = ''; - for ($i = 1; $i < 32; $i++) - { - $profile_row['s_day_options'] .= '"; - } - - $profile_row['s_month_options'] = ''; - for ($i = 1; $i < 13; $i++) - { - $profile_row['s_month_options'] .= '"; - } - - $profile_row['s_year_options'] = ''; - for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++) - { - $profile_row['s_year_options'] .= '"; - } - unset($now); - - $profile_row['field_value'] = 0; - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process bool-type - * @access private - */ - function generate_bool($profile_row, $preview = false) - { - global $template; - - $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - - $profile_row['field_value'] = $value; - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - - if ($profile_row['field_length'] == 1) - { - 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']])) - { - $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview); - } - - foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) - { - $template->assign_block_vars('bool.options', array( - 'OPTION_ID' => $option_id, - 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '', - 'VALUE' => $option_value) - ); - } - } - } - - /** - * Process string-type - * @access private - */ - function generate_string($profile_row, $preview = false) - { - global $template; - - $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process text-type - * @access private - */ - function generate_text($profile_row, $preview = false) - { - global $template; - global $user, $phpEx, $phpbb_root_path; - - $field_length = explode('|', $profile_row['field_length']); - $profile_row['field_rows'] = $field_length[0]; - $profile_row['field_cols'] = $field_length[1]; - - $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process dropdown-type - * @access private - */ - function generate_dropdown($profile_row, $preview = false) - { - global $user, $template; - - $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - - 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']])) - { - $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview); - } - - $profile_row['field_value'] = $value; - $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - - foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) - { - $template->assign_block_vars('dropdown.options', array( - 'OPTION_ID' => $option_id, - 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '', - 'VALUE' => $option_value) - ); - } - } - - /** - * Return Templated value/field. Possible values for $mode are: - * change == user is able to set/enter profile values; preview == just show the value - * @access private - */ - function process_field_row($mode, $profile_row) - { - global $template; - - $preview = ($mode == 'preview') ? true : false; - - // set template filename - $template->set_filenames(array( - 'cp_body' => 'custom_profile_fields.html') - ); - - // empty previously filled blockvars - foreach ($this->profile_types as $field_case => $field_type) - { - $template->destroy_block_vars($field_type); - } - - // Assign template variables - $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']]; - $this->$type_func($profile_row, $preview); - - // Return templated data - return $template->assign_display('cp_body'); - } - - /** - * Build Array for user insertion into custom profile fields table - */ - function build_insert_sql_array($cp_data) - { - global $db, $user, $auth; - - $sql_not_in = array(); - foreach ($cp_data as $key => $null) - { - $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key; - } - - $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f - WHERE l.lang_id = ' . $user->get_iso_lang_id() . ' - ' . ((sizeof($sql_not_in)) ? ' AND ' . $db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . ' - AND l.field_id = f.field_id'; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE) - { - $now = getdate(); - $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - else if ($row['field_default_value'] === '' && $row['field_type'] == FIELD_INT) - { - // We cannot insert an empty string into an integer column. - $row['field_default_value'] = NULL; - } - - $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value']; - } - $db->sql_freeresult($result); - - return $cp_data; - } - - /** - * Get profile field value on submit - * @access private - */ - function get_profile_field($profile_row) - { - global $phpbb_root_path, $phpEx; - global $config; - global $request; - - $var_name = 'pf_' . $profile_row['field_ident']; - - switch ($profile_row['field_type']) - { - case FIELD_DATE: - - if (!isset($_REQUEST[$var_name . '_day'])) - { - if ($profile_row['field_default_value'] == 'now') - { - $now = getdate(); - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - list($day, $month, $year) = explode('-', $profile_row['field_default_value']); - } - else - { - $day = request_var($var_name . '_day', 0); - $month = request_var($var_name . '_month', 0); - $year = request_var($var_name . '_year', 0); - } - - $var = sprintf('%2d-%2d-%4d', $day, $month, $year); - break; - - case FIELD_BOOL: - // Checkbox - if ($profile_row['field_length'] == 2) - { - $var = (isset($_REQUEST[$var_name])) ? 1 : 0; - } - else - { - $var = request_var($var_name, (int) $profile_row['field_default_value']); - } - break; - - case FIELD_STRING: - case FIELD_TEXT: - $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true)); - break; - - case FIELD_INT: - if (isset($_REQUEST[$var_name]) && $request->variable($var_name, '') === '') - { - $var = NULL; - } - else - { - $var = request_var($var_name, (int) $profile_row['field_default_value']); - } - break; - - case FIELD_DROPDOWN: - $var = request_var($var_name, (int) $profile_row['field_default_value']); - break; - - default: - $var = request_var($var_name, $profile_row['field_default_value']); - break; - } - - return $var; - } -} - -/** -* Custom Profile Fields ACP -* @package phpBB3 -*/ -class custom_profile_admin extends custom_profile -{ - var $vars = array(); - - /** - * Return possible validation options - */ - function validate_options() - { - global $user; - - $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+'); - - $validate_options = ''; - foreach ($validate_ary as $lang => $value) - { - $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : ''; - $validate_options .= ''; - } - - return $validate_options; - } - - /** - * Get string options for second step in ACP - */ - function get_string_options() - { - global $user; - - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get text options for second step in ACP - */ - function get_text_options() - { - global $user; - - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $user->lang['ROWS'] . '
' . $user->lang['COLUMNS'] . ' '), - 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get int options for second step in ACP - */ - function get_int_options() - { - global $user; - - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get bool options for second step in ACP - */ - function get_bool_options() - { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; - - $profile_row = array( - 'var_name' => 'field_default_value', - 'field_id' => 1, - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_BOOL, - 'field_length' => $this->vars['field_length'], - 'lang_options' => $this->vars['lang_options'] - ); - - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)) - ); - - return $options; - } - - /** - * Get dropdown options for second step in ACP - */ - function get_dropdown_options() - { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; - - $profile_row[0] = array( - 'var_name' => 'field_default_value', - 'field_id' => 1, - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DROPDOWN, - 'lang_options' => $this->vars['lang_options'] - ); - - $profile_row[1] = $profile_row[0]; - $profile_row[1]['var_name'] = 'field_novalue'; - $profile_row[1]['field_ident'] = 'field_novalue'; - $profile_row[1]['field_default_value'] = $this->vars['field_novalue']; - - $options = array( - 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])) - ); - - return $options; - } - - /** - * Get date options for second step in ACP - */ - function get_date_options() - { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; - - $profile_row = array( - 'var_name' => 'field_default_value', - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DATE, - 'field_length' => $this->vars['field_length'] - ); - - $always_now = request_var('always_now', -1); - if ($always_now == -1) - { - $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false; - } - else - { - $s_checked = ($always_now) ? true : false; - } - - $options = array( - 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), - 1 => array('TITLE' => $user->lang['ALWAYS_TODAY'], 'FIELD' => ''), - ); - - return $options; - } -} diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index dba6d3d6c2..b2cd911a0d 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -162,7 +162,7 @@ function user_update_name($old_name, $new_name) function user_add($user_row, $cp_data = false) { global $db, $user, $auth, $config, $phpbb_root_path, $phpEx; - global $phpbb_dispatcher; + global $phpbb_dispatcher, $phpbb_container; if (empty($user_row['username']) || !isset($user_row['group_id']) || !isset($user_row['user_email']) || !isset($user_row['user_type'])) { @@ -276,12 +276,7 @@ function user_add($user_row, $cp_data = false) { $cp_data['user_id'] = (int) $user_id; - if (!class_exists('custom_profile')) - { - include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - } - - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp->build_insert_sql_array($cp_data)); $db->sql_query($sql); diff --git a/phpBB/includes/ucp/ucp_pm_viewmessage.php b/phpBB/includes/ucp/ucp_pm_viewmessage.php index cebbadc7c7..78a0e010e6 100644 --- a/phpBB/includes/ucp/ucp_pm_viewmessage.php +++ b/phpBB/includes/ucp/ucp_pm_viewmessage.php @@ -20,7 +20,7 @@ if (!defined('IN_PHPBB')) */ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) { - global $user, $template, $auth, $db, $cache; + global $user, $template, $auth, $db, $cache, $phpbb_container; global $phpbb_root_path, $request, $phpEx, $config, $phpbb_dispatcher; $user->add_lang(array('viewtopic', 'memberlist')); @@ -61,11 +61,7 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) // Load the custom profile fields if ($config['load_cpf_pm']) { - if (!class_exists('custom_profile')) - { - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - } - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $profile_fields = $cp->generate_profile_fields_template('grab', $author_id); } diff --git a/phpBB/includes/ucp/ucp_profile.php b/phpBB/includes/ucp/ucp_profile.php index 2252b2ea17..672588c51f 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -29,8 +29,7 @@ class ucp_profile function main($id, $mode) { global $cache, $config, $db, $user, $auth, $template, $phpbb_root_path, $phpEx; - global $request; - global $phpbb_container; + global $request, $phpbb_container; $user->add_lang('posting'); @@ -257,9 +256,7 @@ class ucp_profile trigger_error('NO_AUTH_PROFILEINFO'); } - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $cp_data = $cp_error = array(); diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 1641c6eef1..333aa2816f 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -35,8 +35,6 @@ class ucp_register trigger_error('UCP_REGISTER_DISABLE'); } - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - $coppa = $request->is_set('coppa') ? (int) $request->variable('coppa', false) : false; $agreed = $request->variable('agreed', false); $submit = $request->is_set_post('submit'); @@ -78,7 +76,7 @@ class ucp_register } } - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $error = $cp_data = $cp_error = array(); $s_hidden_fields = array(); diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index 14425ca6e4..84fbb8218a 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -617,8 +617,7 @@ switch ($mode) $profile_fields = array(); if ($config['load_cpf_viewprofile']) { - include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); $profile_fields = $cp->generate_profile_fields_template('grab', $user_id); $profile_fields = (isset($profile_fields[$user_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields[$user_id]) : array(); } @@ -1555,8 +1554,7 @@ switch ($mode) // Load custom profile fields if ($config['load_cpf_memberlist']) { - include_once($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); // Grab all profile fields from users in id cache for later use - similar to the poster cache $profile_fields_cache = $cp->generate_profile_fields_template('grab', $user_list); diff --git a/phpBB/phpbb/profilefields/admin.php b/phpBB/phpbb/profilefields/admin.php new file mode 100644 index 0000000000..b784b91c55 --- /dev/null +++ b/phpBB/phpbb/profilefields/admin.php @@ -0,0 +1,191 @@ + '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+'); + + $validate_options = ''; + foreach ($validate_ary as $lang => $value) + { + $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : ''; + $validate_options .= ''; + } + + return $validate_options; + } + + /** + * Get string options for second step in ACP + */ + function get_string_options() + { + global $user; + + $options = array( + 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') + ); + + return $options; + } + + /** + * Get text options for second step in ACP + */ + function get_text_options() + { + global $user; + + $options = array( + 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $user->lang['ROWS'] . '
' . $user->lang['COLUMNS'] . ' '), + 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') + ); + + return $options; + } + + /** + * Get int options for second step in ACP + */ + function get_int_options() + { + global $user; + + $options = array( + 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), + 2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), + 3 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => '') + ); + + return $options; + } + + /** + * Get bool options for second step in ACP + */ + function get_bool_options() + { + global $user, $config, $lang_defs; + + $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + + $profile_row = array( + 'var_name' => 'field_default_value', + 'field_id' => 1, + 'lang_name' => $this->vars['lang_name'], + 'lang_explain' => $this->vars['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $this->vars['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_BOOL, + 'field_length' => $this->vars['field_length'], + 'lang_options' => $this->vars['lang_options'] + ); + + $options = array( + 0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), + 1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)) + ); + + return $options; + } + + /** + * Get dropdown options for second step in ACP + */ + function get_dropdown_options() + { + global $user, $config, $lang_defs; + + $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + + $profile_row[0] = array( + 'var_name' => 'field_default_value', + 'field_id' => 1, + 'lang_name' => $this->vars['lang_name'], + 'lang_explain' => $this->vars['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $this->vars['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_DROPDOWN, + 'lang_options' => $this->vars['lang_options'] + ); + + $profile_row[1] = $profile_row[0]; + $profile_row[1]['var_name'] = 'field_novalue'; + $profile_row[1]['field_ident'] = 'field_novalue'; + $profile_row[1]['field_default_value'] = $this->vars['field_novalue']; + + $options = array( + 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), + 1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])) + ); + + return $options; + } + + /** + * Get date options for second step in ACP + */ + function get_date_options() + { + global $user, $config, $lang_defs; + + $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + + $profile_row = array( + 'var_name' => 'field_default_value', + 'lang_name' => $this->vars['lang_name'], + 'lang_explain' => $this->vars['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $this->vars['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_DATE, + 'field_length' => $this->vars['field_length'] + ); + + $always_now = request_var('always_now', -1); + if ($always_now == -1) + { + $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false; + } + else + { + $s_checked = ($always_now) ? true : false; + } + + $options = array( + 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), + 1 => array('TITLE' => $user->lang['ALWAYS_TODAY'], 'FIELD' => ''), + ); + + return $options; + } +} diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php new file mode 100644 index 0000000000..3d9339d1d7 --- /dev/null +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -0,0 +1,974 @@ + 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date'); + var $profile_cache = array(); + var $options_lang = array(); + + /** + * + */ + public function __construct($auth, $db, $request, $template, $user) + { + $this->auth = $auth; + $this->db = $db; + $this->request = $request; + $this->template = $template; + $this->user = $user; + } + + /** + * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) + * Called by ucp_profile and ucp_register + * @access public + */ + function generate_profile_fields($mode, $lang_id) + { + $sql_where = ''; + switch ($mode) + { + case 'register': + // If the field is required we show it on the registration page + $sql_where .= ' AND f.field_show_on_reg = 1'; + break; + + case 'profile': + // Show hidden fields to moderators/admins + if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) + { + $sql_where .= ' AND f.field_show_profile = 1'; + } + break; + + default: + trigger_error('Wrong profile mode specified', E_USER_ERROR); + break; + } + + $sql = 'SELECT l.*, f.* + FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f + WHERE f.field_active = 1 + $sql_where + AND l.lang_id = $lang_id + AND l.field_id = f.field_id + ORDER BY f.field_order"; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + // Return templated field + $tpl_snippet = $this->process_field_row('change', $row); + + // Some types are multivalue, we can't give them a field_id as we would not know which to pick + $type = (int) $row['field_type']; + + $this->template->assign_block_vars('profile_fields', array( + 'LANG_NAME' => $row['lang_name'], + 'LANG_EXPLAIN' => $row['lang_explain'], + 'FIELD' => $tpl_snippet, + 'FIELD_ID' => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'], + 'S_REQUIRED' => ($row['field_required']) ? true : false) + ); + } + $this->db->sql_freeresult($result); + } + + /** + * Validate entered profile field data + * @access public + */ + function validate_profile_field($field_type, &$field_value, $field_data) + { + switch ($field_type) + { + case FIELD_DATE: + $field_validate = explode('-', $field_value); + + $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; + $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; + $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0; + + if ((!$day || !$month || !$year) && !$field_data['field_required']) + { + return false; + } + + if ((!$day || !$month || !$year) && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) + { + return 'FIELD_INVALID_DATE'; + } + + if (checkdate($month, $day, $year) === false) + { + return 'FIELD_INVALID_DATE'; + } + break; + + case FIELD_BOOL: + $field_value = (bool) $field_value; + + if (!$field_value && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + break; + + case FIELD_INT: + if (trim($field_value) === '' && !$field_data['field_required']) + { + return false; + } + + $field_value = (int) $field_value; + + if ($field_value < $field_data['field_minlen']) + { + return 'FIELD_TOO_SMALL'; + } + else if ($field_value > $field_data['field_maxlen']) + { + return 'FIELD_TOO_LARGE'; + } + break; + + case FIELD_DROPDOWN: + $field_value = (int) $field_value; + + // retrieve option lang data if necessary + if (!isset($this->options_lang[$field_data['field_id']]) || !isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->options_lang[$file_data['field_id']][$field_data['lang_id']])) + { + $this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); + } + + if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) + { + return 'FIELD_INVALID_VALUE'; + } + + if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + break; + + case FIELD_STRING: + case FIELD_TEXT: + if (trim($field_value) === '' && !$field_data['field_required']) + { + return false; + } + else if (trim($field_value) === '' && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) + { + return 'FIELD_TOO_SHORT'; + } + else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) + { + return 'FIELD_TOO_LONG'; + } + + if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') + { + $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value); + if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) + { + return 'FIELD_INVALID_CHARS'; + } + } + break; + } + + return false; + } + + /** + * Build profile cache, used for display + * @access private + */ + function build_cache() + { + $this->profile_cache = array(); + + // Display hidden/no_view fields for admin/moderator + $sql = 'SELECT l.*, f.* + FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f + WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' + AND f.field_active = 1 ' . + ((!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . ' + AND f.field_no_view = 0 + AND l.field_id = f.field_id + ORDER BY f.field_order'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $this->profile_cache[$row['field_ident']] = $row; + } + $this->db->sql_freeresult($result); + } + + /** + * Get language entries for options and store them here for later use + */ + function get_option_lang($field_id, $lang_id, $field_type, $preview) + { + if ($preview) + { + $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options']; + + foreach ($lang_options as $num => $var) + { + $this->options_lang[$field_id][$lang_id][($num + 1)] = $var; + } + } + else + { + $sql = 'SELECT option_id, lang_value + FROM ' . PROFILE_FIELDS_LANG_TABLE . " + WHERE field_id = $field_id + AND lang_id = $lang_id + AND field_type = $field_type + ORDER BY option_id"; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value']; + } + $this->db->sql_freeresult($result); + } + } + + /** + * Submit profile field for validation + * @access public + */ + function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) + { + $sql_where = ''; + switch ($mode) + { + case 'register': + // If the field is required we show it on the registration page + $sql_where .= ' AND f.field_show_on_reg = 1'; + break; + + case 'profile': + // Show hidden fields to moderators/admins + if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) + { + $sql_where .= ' AND f.field_show_profile = 1'; + } + break; + + default: + trigger_error('Wrong profile mode specified', E_USER_ERROR); + break; + } + + $sql = 'SELECT l.*, f.* + FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f + WHERE l.lang_id = $lang_id + AND f.field_active = 1 + $sql_where + AND l.field_id = f.field_id + ORDER BY f.field_order"; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row); + $check_value = $cp_data['pf_' . $row['field_ident']]; + + if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false) + { + // If not and only showing common error messages, use this one + $error = ''; + switch ($cp_result) + { + case 'FIELD_INVALID_DATE': + case 'FIELD_INVALID_VALUE': + case 'FIELD_REQUIRED': + $error = $this->user->lang($cp_result, $row['lang_name']); + break; + + case 'FIELD_TOO_SHORT': + case 'FIELD_TOO_SMALL': + $error = $this->user->lang($cp_result, (int) $row['field_minlen'], $row['lang_name']); + break; + + case 'FIELD_TOO_LONG': + case 'FIELD_TOO_LARGE': + $error = $this->user->lang($cp_result, (int) $row['field_maxlen'], $row['lang_name']); + break; + + case 'FIELD_INVALID_CHARS': + switch ($row['field_validation']) + { + case '[0-9]+': + $error = $this->user->lang($cp_result . '_NUMBERS_ONLY', $row['lang_name']); + break; + + case '[\w]+': + $error = $this->user->lang($cp_result . '_ALPHA_ONLY', $row['lang_name']); + break; + + case '[\w_\+\. \-\[\]]+': + $error = $this->user->lang($cp_result . '_SPACERS_ONLY', $row['lang_name']); + break; + } + break; + } + + if ($error != '') + { + $cp_error[] = $error; + } + } + } + $this->db->sql_freeresult($result); + } + + /** + * Update profile field data directly + */ + function update_profile_field_data($user_id, &$cp_data) + { + if (!sizeof($cp_data)) + { + return; + } + + switch ($db->sql_layer) + { + case 'oracle': + case 'firebird': + case 'postgres': + $right_delim = $left_delim = '"'; + break; + + case 'sqlite': + case 'mssql': + case 'mssql_odbc': + case 'mssqlnative': + $right_delim = ']'; + $left_delim = '['; + break; + + case 'mysql': + case 'mysql4': + case 'mysqli': + $right_delim = $left_delim = '`'; + break; + } + + // use new array for the UPDATE; changes in the key do not affect the original array + $cp_data_sql = array(); + foreach ($cp_data as $key => $value) + { + // Firebird is case sensitive with delimiter + $cp_data_sql[$left_delim . (($this->db->sql_layer == 'firebird' || $this->db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value; + } + + $sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . ' + SET ' . $this->db->sql_build_array('UPDATE', $cp_data_sql) . " + WHERE user_id = $user_id"; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data_sql['user_id'] = (int) $user_id; + + $this->db->sql_return_on_error(true); + + $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $this->db->sql_build_array('INSERT', $cp_data_sql); + $this->db->sql_query($sql); + + $this->db->sql_return_on_error(false); + } + } + + /** + * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled) + * 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 + * @access public + */ + function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) + { + if ($mode == 'grab') + { + if (!is_array($user_id)) + { + $user_id = array($user_id); + } + + if (!sizeof($this->profile_cache)) + { + $this->build_cache(); + } + + if (!sizeof($user_id)) + { + return array(); + } + + $sql = 'SELECT * + FROM ' . PROFILE_FIELDS_DATA_TABLE . ' + WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_id)); + $result = $this->db->sql_query($sql); + + $field_data = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $field_data[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + + $user_fields = array(); + + $user_ids = $user_id; + + // Go through the fields in correct order + foreach (array_keys($this->profile_cache) as $used_ident) + { + foreach ($field_data as $user_id => $row) + { + $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident]; + $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; + } + + foreach ($user_ids as $user_id) + { + if (!isset($user_fields[$user_id][$used_ident]) && $this->profile_cache[$used_ident]['field_show_novalue']) + { + $user_fields[$user_id][$used_ident]['value'] = ''; + $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; + } + } + } + + return $user_fields; + } + else if ($mode == 'show') + { + // $profile_row == $user_fields[$row['user_id']]; + $tpl_fields = array(); + $tpl_fields['row'] = $tpl_fields['blockrow'] = array(); + + foreach ($profile_row as $ident => $ident_ary) + { + $value = $this->get_profile_value($ident_ary); + + if ($value === NULL) + { + continue; + } + + $tpl_fields['row'] += array( + 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value, + 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'], + 'PROFILE_' . strtoupper($ident) . '_NAME' => $ident_ary['data']['lang_name'], + 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'], + + 'S_PROFILE_' . strtoupper($ident) => true + ); + + $tpl_fields['blockrow'][] = array( + 'PROFILE_FIELD_VALUE' => $value, + 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'], + 'PROFILE_FIELD_NAME' => $ident_ary['data']['lang_name'], + 'PROFILE_FIELD_EXPLAIN' => $ident_ary['data']['lang_explain'], + + 'S_PROFILE_' . strtoupper($ident) => true + ); + } + + return $tpl_fields; + } + else + { + trigger_error('Wrong mode for custom profile', E_USER_ERROR); + } + } + + /** + * Get Profile Value for display + */ + function get_profile_value($ident_ary) + { + $value = $ident_ary['value']; + $field_type = $ident_ary['data']['field_type']; + + switch ($this->profile_types[$field_type]) + { + case 'int': + if ($value === '' && !$ident_ary['data']['field_show_novalue']) + { + return NULL; + } + return (int) $value; + break; + + case 'string': + case 'text': + if (!$value && !$ident_ary['data']['field_show_novalue']) + { + return NULL; + } + + $value = make_clickable($value); + $value = censor_text($value); + $value = bbcode_nl2br($value); + return $value; + break; + + // case 'datetime': + case 'date': + $date = explode('-', $value); + $day = (isset($date[0])) ? (int) $date[0] : 0; + $month = (isset($date[1])) ? (int) $date[1] : 0; + $year = (isset($date[2])) ? (int) $date[2] : 0; + + if (!$day && !$month && !$year && !$ident_ary['data']['field_show_novalue']) + { + return NULL; + } + else if ($day && $month && $year) + { + // Date should display as the same date for every user regardless of timezone + return $this->user->create_datetime() + ->setDate($year, $month, $day) + ->setTime(0, 0, 0) + ->format($user->lang['DATE_FORMAT'], true); + } + + return $value; + break; + + case 'dropdown': + $field_id = $ident_ary['data']['field_id']; + $lang_id = $ident_ary['data']['lang_id']; + if (!isset($this->options_lang[$field_id][$lang_id])) + { + $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); + } + + if ($value == $ident_ary['data']['field_novalue'] && !$ident_ary['data']['field_show_novalue']) + { + return NULL; + } + + $value = (int) $value; + + // User not having a value assigned + if (!isset($this->options_lang[$field_id][$lang_id][$value])) + { + if ($ident_ary['data']['field_show_novalue']) + { + $value = $ident_ary['data']['field_novalue']; + } + else + { + return NULL; + } + } + + return $this->options_lang[$field_id][$lang_id][$value]; + break; + + case 'bool': + $field_id = $ident_ary['data']['field_id']; + $lang_id = $ident_ary['data']['lang_id']; + if (!isset($this->options_lang[$field_id][$lang_id])) + { + $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); + } + + if (!$value && $ident_ary['data']['field_show_novalue']) + { + $value = $ident_ary['data']['field_default_value']; + } + + if ($ident_ary['data']['field_length'] == 1) + { + return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL; + } + else if (!$value) + { + return NULL; + } + else + { + return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1]; + } + break; + + default: + trigger_error('Unknown profile type', E_USER_ERROR); + break; + } + } + + /** + * Get field value for registration/profile + * @access private + */ + function get_var($field_validation, &$profile_row, $default_value, $preview) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $user_ident = $profile_row['field_ident']; + // checkbox - set the value to "true" if it has been set to 1 + if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2) + { + $value = (isset($_REQUEST[$profile_row['field_ident']]) && request_var($profile_row['field_ident'], $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $default_value : $this->user->profile_fields[$user_ident]); + } + else if ($profile_row['field_type'] == FIELD_INT) + { + if (isset($_REQUEST[$profile_row['field_ident']])) + { + $value = ($this->request->variable($profile_row['field_ident'], '') === '') ? NULL : $this->request->variable($profile_row['field_ident'], $default_value); + } + else + { + if (!$preview && array_key_exists($user_ident, $this->user->profile_fields) && is_null($this->user->profile_fields[$user_ident])) + { + $value = NULL; + } + else if (!isset($this->user->profile_fields[$user_ident]) || $preview) + { + $value = $default_value; + } + else + { + $value = $this->user->profile_fields[$user_ident]; + } + } + + return (is_null($value) || $value === '') ? '' : (int) $value; + } + else + { + $value = (isset($_REQUEST[$profile_row['field_ident']])) ? request_var($profile_row['field_ident'], $default_value, true) : ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $default_value : $this->user->profile_fields[$user_ident]); + + if (gettype($value) == 'string') + { + $value = utf8_normalize_nfc($value); + } + } + + switch ($field_validation) + { + case 'int': + return (int) $value; + break; + } + + return $value; + } + + /** + * Process int-type + * @access private + */ + function generate_int($profile_row, $preview = false) + { + $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + } + + /** + * Process date-type + * @access private + */ + function generate_date($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $user_ident = $profile_row['field_ident']; + + $now = getdate(); + + if (!isset($_REQUEST[$profile_row['field_ident'] . '_day'])) + { + if ($profile_row['field_default_value'] == 'now') + { + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + } + else + { + if ($preview && $profile_row['field_default_value'] == 'now') + { + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + } + else + { + $day = request_var($profile_row['field_ident'] . '_day', 0); + $month = request_var($profile_row['field_ident'] . '_month', 0); + $year = request_var($profile_row['field_ident'] . '_year', 0); + } + } + + $profile_row['s_day_options'] = ''; + for ($i = 1; $i < 32; $i++) + { + $profile_row['s_day_options'] .= '"; + } + + $profile_row['s_month_options'] = ''; + for ($i = 1; $i < 13; $i++) + { + $profile_row['s_month_options'] .= '"; + } + + $profile_row['s_year_options'] = ''; + for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++) + { + $profile_row['s_year_options'] .= '"; + } + unset($now); + + $profile_row['field_value'] = 0; + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + } + + /** + * Process bool-type + * @access private + */ + function generate_bool($profile_row, $preview = false) + { + $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); + + $profile_row['field_value'] = $value; + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + + if ($profile_row['field_length'] == 1) + { + 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']])) + { + $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview); + } + + foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + { + $this->template->assign_block_vars('bool.options', array( + 'OPTION_ID' => $option_id, + 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '', + 'VALUE' => $option_value) + ); + } + } + } + + /** + * Process string-type + * @access private + */ + function generate_string($profile_row, $preview = false) + { + $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + } + + /** + * Process text-type + * @access private + */ + function generate_text($profile_row, $preview = false) + { + $field_length = explode('|', $profile_row['field_length']); + $profile_row['field_rows'] = $field_length[0]; + $profile_row['field_cols'] = $field_length[1]; + + $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + } + + /** + * Process dropdown-type + * @access private + */ + function generate_dropdown($profile_row, $preview = false) + { + $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); + + 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']])) + { + $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview); + } + + $profile_row['field_value'] = $value; + $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); + + foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + { + $this->template->assign_block_vars('dropdown.options', array( + 'OPTION_ID' => $option_id, + 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '', + 'VALUE' => $option_value) + ); + } + } + + /** + * Return Templated value/field. Possible values for $mode are: + * change == user is able to set/enter profile values; preview == just show the value + * @access private + */ + function process_field_row($mode, $profile_row) + { + $preview = ($mode == 'preview') ? true : false; + + // set template filename + $this->template->set_filenames(array( + 'cp_body' => 'custom_profile_fields.html', + )); + + // empty previously filled blockvars + foreach ($this->profile_types as $field_case => $field_type) + { + $this->template->destroy_block_vars($field_type); + } + + // Assign template variables + $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']]; + $this->$type_func($profile_row, $preview); + + // Return templated data + return $this->template->assign_display('cp_body'); + } + + /** + * Build Array for user insertion into custom profile fields table + */ + function build_insert_sql_array($cp_data) + { + $sql_not_in = array(); + foreach ($cp_data as $key => $null) + { + $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key; + } + + $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value + FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f + WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' + ' . ((sizeof($sql_not_in)) ? ' AND ' . $this->db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . ' + AND l.field_id = f.field_id'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE) + { + $now = getdate(); + $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + else if ($row['field_default_value'] === '' && $row['field_type'] == FIELD_INT) + { + // We cannot insert an empty string into an integer column. + $row['field_default_value'] = NULL; + } + + $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value']; + } + $this->db->sql_freeresult($result); + + return $cp_data; + } + + /** + * Get profile field value on submit + * @access private + */ + function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + + switch ($profile_row['field_type']) + { + case FIELD_DATE: + + if (!isset($_REQUEST[$var_name . '_day'])) + { + if ($profile_row['field_default_value'] == 'now') + { + $now = getdate(); + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + list($day, $month, $year) = explode('-', $profile_row['field_default_value']); + } + else + { + $day = request_var($var_name . '_day', 0); + $month = request_var($var_name . '_month', 0); + $year = request_var($var_name . '_year', 0); + } + + $var = sprintf('%2d-%2d-%4d', $day, $month, $year); + break; + + case FIELD_BOOL: + // Checkbox + if ($profile_row['field_length'] == 2) + { + $var = (isset($_REQUEST[$var_name])) ? 1 : 0; + } + else + { + $var = request_var($var_name, (int) $profile_row['field_default_value']); + } + break; + + case FIELD_STRING: + case FIELD_TEXT: + $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true)); + break; + + case FIELD_INT: + if (isset($_REQUEST[$var_name]) && $this->request->variable($var_name, '') === '') + { + $var = NULL; + } + else + { + $var = request_var($var_name, (int) $profile_row['field_default_value']); + } + break; + + case FIELD_DROPDOWN: + $var = request_var($var_name, (int) $profile_row['field_default_value']); + break; + + default: + $var = request_var($var_name, $profile_row['field_default_value']); + break; + } + + return $var; + } +} diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 14040176cb..396de132cb 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -1265,11 +1265,7 @@ $db->sql_freeresult($result); // Load custom profile fields if ($config['load_cpf_viewtopic']) { - if (!class_exists('custom_profile')) - { - include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx); - } - $cp = new custom_profile(); + $cp = $phpbb_container->get('profilefields'); // Grab all profile fields from users in id cache for later use - similar to the poster cache $profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache); -- cgit v1.2.1 From c6658396362f56e1f6605b032bbb7d12275e4749 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 10 Jan 2014 23:00:37 +0100 Subject: [ticket/11201] Remove global use from admin class PHPBB3-11201 --- phpBB/config/profilefields.yml | 1 + phpBB/includes/acp/acp_profile.php | 2 +- phpBB/phpbb/profilefields/admin.php | 83 ++++++++++++++++++------------------- 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 3b9b1a60a0..4fac8bed9b 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -12,6 +12,7 @@ services: class: \phpbb\profilefields\admin arguments: - @auth + - @config - @dbal.conn - @request - @template diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 510239a837..3c2e6448ce 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -849,7 +849,7 @@ class acp_profile // Build options based on profile type $function = 'get_' . $cp->profile_types[$field_type] . '_options'; - $options = $cp->$function(); + $options = $cp->$function($this->lang_defs); foreach ($options as $num => $option_ary) { diff --git a/phpBB/phpbb/profilefields/admin.php b/phpBB/phpbb/profilefields/admin.php index b784b91c55..1cca496265 100644 --- a/phpBB/phpbb/profilefields/admin.php +++ b/phpBB/phpbb/profilefields/admin.php @@ -17,20 +17,31 @@ class admin extends profilefields { var $vars = array(); + /** + * + */ + public function __construct($auth, $config, $db, $request, $template, $user) + { + $this->auth = $auth; + $this->config = $config; + $this->db = $db; + $this->request = $request; + $this->template = $template; + $this->user = $user; + } + /** * Return possible validation options */ function validate_options() { - global $user; - $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+'); $validate_options = ''; foreach ($validate_ary as $lang => $value) { $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : ''; - $validate_options .= ''; + $validate_options .= ''; } return $validate_options; @@ -39,15 +50,13 @@ class admin extends profilefields /** * Get string options for second step in ACP */ - function get_string_options() + function get_string_options($lang_defs) { - global $user; - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') ); return $options; @@ -56,15 +65,13 @@ class admin extends profilefields /** * Get text options for second step in ACP */ - function get_text_options() + function get_text_options($lang_defs) { - global $user; - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $user->lang['ROWS'] . '
' . $user->lang['COLUMNS'] . ' '), - 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '') + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $user->lang['ROWS'] . '
' . $user->lang['COLUMNS'] . ' '), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') ); return $options; @@ -73,15 +80,13 @@ class admin extends profilefields /** * Get int options for second step in ACP */ - function get_int_options() + function get_int_options($lang_defs) { - global $user; - $options = array( - 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), - 2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), - 3 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => '') + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => '') ); return $options; @@ -90,11 +95,9 @@ class admin extends profilefields /** * Get bool options for second step in ACP */ - function get_bool_options() + function get_bool_options($lang_defs) { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; $profile_row = array( 'var_name' => 'field_default_value', @@ -110,8 +113,8 @@ class admin extends profilefields ); $options = array( - 0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)) + 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)) ); return $options; @@ -120,11 +123,9 @@ class admin extends profilefields /** * Get dropdown options for second step in ACP */ - function get_dropdown_options() + function get_dropdown_options($lang_defs) { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; $profile_row[0] = array( 'var_name' => 'field_default_value', @@ -144,8 +145,8 @@ class admin extends profilefields $profile_row[1]['field_default_value'] = $this->vars['field_novalue']; $options = array( - 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])) + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), + 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])) ); return $options; @@ -154,11 +155,9 @@ class admin extends profilefields /** * Get date options for second step in ACP */ - function get_date_options() + function get_date_options($lang_defs) { - global $user, $config, $lang_defs; - - $default_lang_id = $lang_defs['iso'][$config['default_lang']]; + $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; $profile_row = array( 'var_name' => 'field_default_value', @@ -182,8 +181,8 @@ class admin extends profilefields } $options = array( - 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), - 1 => array('TITLE' => $user->lang['ALWAYS_TODAY'], 'FIELD' => ''), + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), + 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => ''), ); return $options; -- cgit v1.2.1 From a7e3538e5b0cb12610b5c051a5d30292970249d1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 11 Jan 2014 01:44:48 +0100 Subject: [ticket/11201] Move get_options to type classes PHPBB3-11201 --- phpBB/config/profilefields.yml | 52 ++++-- phpBB/includes/acp/acp_profile.php | 23 +-- phpBB/phpbb/profilefields/admin.php | 190 --------------------- phpBB/phpbb/profilefields/type/type_bool.php | 63 +++++++ phpBB/phpbb/profilefields/type/type_date.php | 71 ++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 67 ++++++++ phpBB/phpbb/profilefields/type/type_int.php | 51 ++++++ phpBB/phpbb/profilefields/type/type_interface.php | 39 +++++ phpBB/phpbb/profilefields/type/type_string.php | 51 ++++++ .../profilefields/type/type_string_common.php | 30 ++++ phpBB/phpbb/profilefields/type/type_text.php | 51 ++++++ 11 files changed, 471 insertions(+), 217 deletions(-) delete mode 100644 phpBB/phpbb/profilefields/admin.php create mode 100644 phpBB/phpbb/profilefields/type/type_bool.php create mode 100644 phpBB/phpbb/profilefields/type/type_date.php create mode 100644 phpBB/phpbb/profilefields/type/type_dropdown.php create mode 100644 phpBB/phpbb/profilefields/type/type_int.php create mode 100644 phpBB/phpbb/profilefields/type/type_interface.php create mode 100644 phpBB/phpbb/profilefields/type/type_string.php create mode 100644 phpBB/phpbb/profilefields/type/type_string_common.php create mode 100644 phpBB/phpbb/profilefields/type/type_text.php diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 4fac8bed9b..37df3d9019 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -8,17 +8,47 @@ services: - @template - @user - profilefields.admin: - class: \phpbb\profilefields\admin + profilefields.type.bool: + class: \phpbb\profilefields\type\type_bool arguments: - - @auth - - @config - - @dbal.conn - - @request - - @template + - @profilefields + - @user + tags: + - { name: profilefield.type } + + profilefields.type.date: + class: \phpbb\profilefields\type\type_date + arguments: + - @profilefields + - @user + tags: + - { name: profilefield.type } + + profilefields.type.dropdown: + class: \phpbb\profilefields\type\type_dropdown + arguments: + - @profilefields - @user + tags: + - { name: profilefield.type } - #migrator.tool.permission1: - # class: \phpbb\profilefields\profilefields - # tags: - # - { name: ffoobar.tool } + profilefields.type.int: + class: \phpbb\profilefields\type\type_int + arguments: + - @user + tags: + - { name: profilefield.type } + + profilefields.type.string: + class: \phpbb\profilefields\type\type_string + arguments: + - @user + tags: + - { name: profilefield.type } + + profilefields.type.text: + class: \phpbb\profilefields\type\type_text + arguments: + - @user + tags: + - { name: profilefield.type } diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 3c2e6448ce..44c2e0f11e 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -49,17 +49,7 @@ class acp_profile trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING); } - // Define some default values for each field type - $default_values = array( - FIELD_STRING => array('field_length' => 10, 'field_minlen' => 0, 'field_maxlen' => 20, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''), - FIELD_TEXT => array('field_length' => '5|80', 'field_minlen' => 0, 'field_maxlen' => 1000, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''), - FIELD_INT => array('field_length' => 5, 'field_minlen' => 0, 'field_maxlen' => 100, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0), - FIELD_DATE => array('field_length' => 10, 'field_minlen' => 10, 'field_maxlen' => 10, 'field_validation' => '', 'field_novalue' => ' 0- 0- 0', 'field_default_value' => ' 0- 0- 0'), - FIELD_BOOL => array('field_length' => 1, 'field_minlen' => 0, 'field_maxlen' => 0, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0), - FIELD_DROPDOWN => array('field_length' => 0, 'field_minlen' => 0, 'field_maxlen' => 5, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0), - ); - - $cp = $phpbb_container->get('profilefields.admin'); + $cp = $phpbb_container->get('profilefields'); // Build Language array // Based on this, we decide which elements need to be edited later and which language items are missing @@ -93,10 +83,10 @@ class acp_profile // Have some fields been defined? if (isset($this->lang_defs['entry'])) { - foreach ($this->lang_defs['entry'] as $field_id => $field_ary) + foreach ($this->lang_defs['entry'] as $field_ident => $field_ary) { // Fill an array with the languages that are missing for each field - $this->lang_defs['diff'][$field_id] = array_diff(array_values($this->lang_defs['iso']), $field_ary); + $this->lang_defs['diff'][$field_ident] = array_diff(array_values($this->lang_defs['iso']), $field_ary); } } @@ -381,7 +371,8 @@ class acp_profile trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $field_row = array_merge($default_values[$field_type], array( + $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); + $field_row = array_merge($profile_field->get_default_values(), array( 'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))), 'field_required' => 0, 'field_show_novalue'=> 0, @@ -848,8 +839,8 @@ class acp_profile ); // Build options based on profile type - $function = 'get_' . $cp->profile_types[$field_type] . '_options'; - $options = $cp->$function($this->lang_defs); + $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); + $options = $profile_field->get_options($this->lang_defs, $cp->vars); foreach ($options as $num => $option_ary) { diff --git a/phpBB/phpbb/profilefields/admin.php b/phpBB/phpbb/profilefields/admin.php deleted file mode 100644 index 1cca496265..0000000000 --- a/phpBB/phpbb/profilefields/admin.php +++ /dev/null @@ -1,190 +0,0 @@ -auth = $auth; - $this->config = $config; - $this->db = $db; - $this->request = $request; - $this->template = $template; - $this->user = $user; - } - - /** - * Return possible validation options - */ - function validate_options() - { - $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+'); - - $validate_options = ''; - foreach ($validate_ary as $lang => $value) - { - $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : ''; - $validate_options .= ''; - } - - return $validate_options; - } - - /** - * Get string options for second step in ACP - */ - function get_string_options($lang_defs) - { - $options = array( - 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get text options for second step in ACP - */ - function get_text_options($lang_defs) - { - $options = array( - 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $user->lang['ROWS'] . '
' . $user->lang['COLUMNS'] . ' '), - 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), - 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get int options for second step in ACP - */ - function get_int_options($lang_defs) - { - $options = array( - 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), - 2 => array('TITLE' => $this->user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => '') - ); - - return $options; - } - - /** - * Get bool options for second step in ACP - */ - function get_bool_options($lang_defs) - { - $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; - - $profile_row = array( - 'var_name' => 'field_default_value', - 'field_id' => 1, - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_BOOL, - 'field_length' => $this->vars['field_length'], - 'lang_options' => $this->vars['lang_options'] - ); - - $options = array( - 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)) - ); - - return $options; - } - - /** - * Get dropdown options for second step in ACP - */ - function get_dropdown_options($lang_defs) - { - $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; - - $profile_row[0] = array( - 'var_name' => 'field_default_value', - 'field_id' => 1, - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DROPDOWN, - 'lang_options' => $this->vars['lang_options'] - ); - - $profile_row[1] = $profile_row[0]; - $profile_row[1]['var_name'] = 'field_novalue'; - $profile_row[1]['field_ident'] = 'field_novalue'; - $profile_row[1]['field_default_value'] = $this->vars['field_novalue']; - - $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])) - ); - - return $options; - } - - /** - * Get date options for second step in ACP - */ - function get_date_options($lang_defs) - { - $default_lang_id = $lang_defs['iso'][$this->config['default_lang']]; - - $profile_row = array( - 'var_name' => 'field_default_value', - 'lang_name' => $this->vars['lang_name'], - 'lang_explain' => $this->vars['lang_explain'], - 'lang_id' => $default_lang_id, - 'field_default_value' => $this->vars['field_default_value'], - 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DATE, - 'field_length' => $this->vars['field_length'] - ); - - $always_now = request_var('always_now', -1); - if ($always_now == -1) - { - $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false; - } - else - { - $s_checked = ($always_now) ? true : false; - } - - $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), - 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => ''), - ); - - return $options; - } -} diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php new file mode 100644 index 0000000000..b3dafa30d4 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -0,0 +1,63 @@ +profilefields = $profilefields; + $this->user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $profile_row = array( + 'var_name' => 'field_default_value', + 'field_id' => 1, + 'lang_name' => $field_data['lang_name'], + 'lang_explain' => $field_data['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $field_data['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_BOOL, + 'field_length' => $field_data['field_length'], + 'lang_options' => $field_data['lang_options'] + ); + + $options = array( + 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)) + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => 1, + 'field_minlen' => 0, + 'field_maxlen' => 0, + 'field_validation' => '', + 'field_novalue' => 0, + 'field_default_value' => 0, + ); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php new file mode 100644 index 0000000000..9639b45770 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -0,0 +1,71 @@ +profilefields = $profilefields; + $this->user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $profile_row = array( + 'var_name' => 'field_default_value', + 'lang_name' => $field_data['lang_name'], + 'lang_explain' => $field_data['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $field_data['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_DATE, + 'field_length' => $field_data['field_length'] + ); + + $always_now = request_var('always_now', -1); + if ($always_now == -1) + { + $s_checked = ($field_data['field_default_value'] == 'now') ? true : false; + } + else + { + $s_checked = ($always_now) ? true : false; + } + + $options = array( + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)), + 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => ''), + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => 10, + 'field_minlen' => 10, + 'field_maxlen' => 10, + 'field_validation' => '', + 'field_novalue' => ' 0- 0- 0', + 'field_default_value' => ' 0- 0- 0', + ); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php new file mode 100644 index 0000000000..8a101f1fc1 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -0,0 +1,67 @@ +profilefields = $profilefields; + $this->user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $profile_row[0] = array( + 'var_name' => 'field_default_value', + 'field_id' => 1, + 'lang_name' => $field_data['lang_name'], + 'lang_explain' => $field_data['lang_explain'], + 'lang_id' => $default_lang_id, + 'field_default_value' => $field_data['field_default_value'], + 'field_ident' => 'field_default_value', + 'field_type' => FIELD_DROPDOWN, + 'lang_options' => $field_data['lang_options'] + ); + + $profile_row[1] = $profile_row[0]; + $profile_row[1]['var_name'] = 'field_novalue'; + $profile_row[1]['field_ident'] = 'field_novalue'; + $profile_row[1]['field_default_value'] = $field_data['field_novalue']; + + $options = array( + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[0])), + 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[1])) + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => 0, + 'field_minlen' => 0, + 'field_maxlen' => 5, + 'field_validation' => '', + 'field_novalue' => 0, + 'field_default_value' => 0, + ); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php new file mode 100644 index 0000000000..7cc74b6e44 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -0,0 +1,51 @@ +user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $options = array( + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => '') + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => 5, + 'field_minlen' => 0, + 'field_maxlen' => 100, + 'field_validation' => '', + 'field_novalue' => 0, + 'field_default_value' => 0, + ); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php new file mode 100644 index 0000000000..8b011aa48e --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -0,0 +1,39 @@ +user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $options = array( + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => 10, + 'field_minlen' => 0, + 'field_maxlen' => 20, + 'field_validation' => '.*', + 'field_novalue' => '', + 'field_default_value' => '', + ); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php new file mode 100644 index 0000000000..dc2cd2f157 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -0,0 +1,30 @@ + '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+'); + + $validate_options = ''; + foreach ($validate_ary as $lang => $value) + { + $selected = ($field_data['field_validation'] == $value) ? ' selected="selected"' : ''; + $validate_options .= ''; + } + + return $validate_options; + } +} diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php new file mode 100644 index 0000000000..4b58ef0486 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -0,0 +1,51 @@ +user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_options($default_lang_id, $field_data) + { + $options = array( + 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $this->user->lang['ROWS'] . '
' . $this->user->lang['COLUMNS'] . ' '), + 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), + 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') + ); + + return $options; + } + + /** + * {@inheritDoc} + */ + public function get_default_values() + { + return array( + 'field_length' => '5|80', + 'field_minlen' => 0, + 'field_maxlen' => 1000, + 'field_validation' => '.*', + 'field_novalue' => '', + 'field_default_value' => '', + ); + } +} -- cgit v1.2.1 From b3803d563a857e12f540a315c51cf97a6c0be438 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 13 Jan 2014 21:05:19 +0100 Subject: [ticket/11201] Move get_profile_field() to type classes PHPBB3-11201 --- phpBB/config/profilefields.yml | 7 ++ phpBB/phpbb/profilefields/profilefields.php | 79 ++--------------------- phpBB/phpbb/profilefields/type/type_bool.php | 21 +++++- phpBB/phpbb/profilefields/type/type_date.php | 29 ++++++++- phpBB/phpbb/profilefields/type/type_dropdown.php | 12 +++- phpBB/phpbb/profilefields/type/type_int.php | 19 +++++- phpBB/phpbb/profilefields/type/type_interface.php | 8 +++ phpBB/phpbb/profilefields/type/type_string.php | 12 +++- phpBB/phpbb/profilefields/type/type_text.php | 12 +++- 9 files changed, 119 insertions(+), 80 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 37df3d9019..486788df3b 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -4,6 +4,7 @@ services: arguments: - @auth - @dbal.conn + - @service_container - @request - @template - @user @@ -12,6 +13,7 @@ services: class: \phpbb\profilefields\type\type_bool arguments: - @profilefields + - @request - @user tags: - { name: profilefield.type } @@ -20,6 +22,7 @@ services: class: \phpbb\profilefields\type\type_date arguments: - @profilefields + - @request - @user tags: - { name: profilefield.type } @@ -28,6 +31,7 @@ services: class: \phpbb\profilefields\type\type_dropdown arguments: - @profilefields + - @request - @user tags: - { name: profilefield.type } @@ -35,6 +39,7 @@ services: profilefields.type.int: class: \phpbb\profilefields\type\type_int arguments: + - @request - @user tags: - { name: profilefield.type } @@ -42,6 +47,7 @@ services: profilefields.type.string: class: \phpbb\profilefields\type\type_string arguments: + - @request - @user tags: - { name: profilefield.type } @@ -49,6 +55,7 @@ services: profilefields.type.text: class: \phpbb\profilefields\type\type_text arguments: + - @request - @user tags: - { name: profilefield.type } diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 3d9339d1d7..b34a85f9fa 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -22,10 +22,11 @@ class profilefields /** * */ - public function __construct($auth, $db, $request, $template, $user) + public function __construct($auth, $db, /** @todo: */ $phpbb_container, $request, $template, $user) { $this->auth = $auth; $this->db = $db; + $this->container = $phpbb_container; $this->request = $request; $this->template = $template; $this->user = $user; @@ -300,7 +301,8 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row); + $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); $check_value = $cp_data['pf_' . $row['field_ident']]; if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false) @@ -362,7 +364,7 @@ class profilefields return; } - switch ($db->sql_layer) + switch ($this->db->sql_layer) { case 'oracle': case 'firebird': @@ -900,75 +902,4 @@ class profilefields return $cp_data; } - - /** - * Get profile field value on submit - * @access private - */ - function get_profile_field($profile_row) - { - $var_name = 'pf_' . $profile_row['field_ident']; - - switch ($profile_row['field_type']) - { - case FIELD_DATE: - - if (!isset($_REQUEST[$var_name . '_day'])) - { - if ($profile_row['field_default_value'] == 'now') - { - $now = getdate(); - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - list($day, $month, $year) = explode('-', $profile_row['field_default_value']); - } - else - { - $day = request_var($var_name . '_day', 0); - $month = request_var($var_name . '_month', 0); - $year = request_var($var_name . '_year', 0); - } - - $var = sprintf('%2d-%2d-%4d', $day, $month, $year); - break; - - case FIELD_BOOL: - // Checkbox - if ($profile_row['field_length'] == 2) - { - $var = (isset($_REQUEST[$var_name])) ? 1 : 0; - } - else - { - $var = request_var($var_name, (int) $profile_row['field_default_value']); - } - break; - - case FIELD_STRING: - case FIELD_TEXT: - $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true)); - break; - - case FIELD_INT: - if (isset($_REQUEST[$var_name]) && $this->request->variable($var_name, '') === '') - { - $var = NULL; - } - else - { - $var = request_var($var_name, (int) $profile_row['field_default_value']); - } - break; - - case FIELD_DROPDOWN: - $var = request_var($var_name, (int) $profile_row['field_default_value']); - break; - - default: - $var = request_var($var_name, $profile_row['field_default_value']); - break; - } - - return $var; - } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index b3dafa30d4..31d1c7d780 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -14,9 +14,10 @@ class type_bool implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) { $this->profilefields = $profilefields; + $this->request = $request; $this->user = $user; } @@ -60,4 +61,22 @@ class type_bool implements type_interface 'field_default_value' => 0, ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + + // Checkbox + if ($profile_row['field_length'] == 2) + { + return ($this->request->is_set($var_name)) ? 1 : 0; + } + else + { + return $this->request->variable($var_name, (int) $profile_row['field_default_value']); + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 9639b45770..f5b5182222 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -14,9 +14,10 @@ class type_date implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) { $this->profilefields = $profilefields; + $this->request = $request; $this->user = $user; } @@ -68,4 +69,30 @@ class type_date implements type_interface 'field_default_value' => ' 0- 0- 0', ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + + if (!$this->request->is_set($var_name . '_day')) + { + if ($profile_row['field_default_value'] == 'now') + { + $now = getdate(); + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + list($day, $month, $year) = explode('-', $profile_row['field_default_value']); + } + else + { + $day = $this->request->variable($var_name . '_day', 0); + $month = $this->request->variable($var_name . '_month', 0); + $year = $this->request->variable($var_name . '_year', 0); + } + + return sprintf('%2d-%2d-%4d', $day, $month, $year); + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 8a101f1fc1..7324ef3ee7 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -14,9 +14,10 @@ class type_dropdown implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) { $this->profilefields = $profilefields; + $this->request = $request; $this->user = $user; } @@ -64,4 +65,13 @@ class type_dropdown implements type_interface 'field_default_value' => 0, ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + return $this->request->variable($var_name, (int) $profile_row['field_default_value']); + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 7cc74b6e44..c40d137a2b 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -14,8 +14,9 @@ class type_int implements type_interface /** * */ - public function __construct($user) + public function __construct(\phpbb\request\request $request, \phpbb\user $user) { + $this->request = $request; $this->user = $user; } @@ -48,4 +49,20 @@ class type_int implements type_interface 'field_default_value' => 0, ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + if ($this->request->is_set($var_name) && $this->request->variable($var_name, '') === '') + { + return null; + } + else + { + return $this->request->variable($var_name, (int) $profile_row['field_default_value']); + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 8b011aa48e..f3ca903e30 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -36,4 +36,12 @@ interface type_interface * @return array with values like default field size and more */ public function get_default_values(); + + /** + * Get profile field value on submit + * + * @param array $profile_row Array with data for this field + * @return mixed Submitted value of the profile field + */ + public function get_profile_field($profile_row); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index c7f9d188ae..64b0660e6e 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -14,8 +14,9 @@ class type_string extends type_string_common implements type_interface /** * */ - public function __construct($user) + public function __construct(\phpbb\request\request $request, \phpbb\user $user) { + $this->request = $request; $this->user = $user; } @@ -48,4 +49,13 @@ class type_string extends type_string_common implements type_interface 'field_default_value' => '', ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true); + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 4b58ef0486..8fff2c917a 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -14,8 +14,9 @@ class type_text extends type_string_common implements type_interface /** * */ - public function __construct($user) + public function __construct(\phpbb\request\request $request, \phpbb\user $user) { + $this->request = $request; $this->user = $user; } @@ -48,4 +49,13 @@ class type_text extends type_string_common implements type_interface 'field_default_value' => '', ); } + + /** + * {@inheritDoc} + */ + public function get_profile_field($profile_row) + { + $var_name = 'pf_' . $profile_row['field_ident']; + return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true); + } } -- cgit v1.2.1 From ee78aed2f1c428b4b45474098f7279402e1f1270 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 10:13:47 +0100 Subject: [ticket/11201] Move validate_profile_field() to type class PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 119 +-------------------- phpBB/phpbb/profilefields/type/type_bool.php | 15 +++ phpBB/phpbb/profilefields/type/type_date.php | 34 ++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 26 +++++ phpBB/phpbb/profilefields/type/type_int.php | 24 +++++ phpBB/phpbb/profilefields/type/type_interface.php | 9 ++ phpBB/phpbb/profilefields/type/type_string.php | 8 ++ .../profilefields/type/type_string_common.php | 40 +++++++ phpBB/phpbb/profilefields/type/type_text.php | 8 ++ 9 files changed, 165 insertions(+), 118 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index b34a85f9fa..d8ba04a862 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -88,123 +88,6 @@ class profilefields $this->db->sql_freeresult($result); } - /** - * Validate entered profile field data - * @access public - */ - function validate_profile_field($field_type, &$field_value, $field_data) - { - switch ($field_type) - { - case FIELD_DATE: - $field_validate = explode('-', $field_value); - - $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; - $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; - $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0; - - if ((!$day || !$month || !$year) && !$field_data['field_required']) - { - return false; - } - - if ((!$day || !$month || !$year) && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - - if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) - { - return 'FIELD_INVALID_DATE'; - } - - if (checkdate($month, $day, $year) === false) - { - return 'FIELD_INVALID_DATE'; - } - break; - - case FIELD_BOOL: - $field_value = (bool) $field_value; - - if (!$field_value && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - break; - - case FIELD_INT: - if (trim($field_value) === '' && !$field_data['field_required']) - { - return false; - } - - $field_value = (int) $field_value; - - if ($field_value < $field_data['field_minlen']) - { - return 'FIELD_TOO_SMALL'; - } - else if ($field_value > $field_data['field_maxlen']) - { - return 'FIELD_TOO_LARGE'; - } - break; - - case FIELD_DROPDOWN: - $field_value = (int) $field_value; - - // retrieve option lang data if necessary - if (!isset($this->options_lang[$field_data['field_id']]) || !isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->options_lang[$file_data['field_id']][$field_data['lang_id']])) - { - $this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); - } - - if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) - { - return 'FIELD_INVALID_VALUE'; - } - - if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - break; - - case FIELD_STRING: - case FIELD_TEXT: - if (trim($field_value) === '' && !$field_data['field_required']) - { - return false; - } - else if (trim($field_value) === '' && $field_data['field_required']) - { - return 'FIELD_REQUIRED'; - } - - if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) - { - return 'FIELD_TOO_SHORT'; - } - else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) - { - return 'FIELD_TOO_LONG'; - } - - if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') - { - $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value); - if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) - { - return 'FIELD_INVALID_CHARS'; - } - } - break; - } - - return false; - } - /** * Build profile cache, used for display * @access private @@ -305,7 +188,7 @@ class profilefields $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); $check_value = $cp_data['pf_' . $row['field_ident']]; - if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false) + if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false) { // If not and only showing common error messages, use this one $error = ''; diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 31d1c7d780..c26e9aa160 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -79,4 +79,19 @@ class type_bool implements type_interface return $this->request->variable($var_name, (int) $profile_row['field_default_value']); } } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + $field_value = (bool) $field_value; + + if (!$field_value && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + return false; + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index f5b5182222..a9b4bc731b 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -95,4 +95,38 @@ class type_date implements type_interface return sprintf('%2d-%2d-%4d', $day, $month, $year); } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + $field_validate = explode('-', $field_value); + + $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; + $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; + $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0; + + if ((!$day || !$month || !$year) && !$field_data['field_required']) + { + return false; + } + + if ((!$day || !$month || !$year) && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) + { + return 'FIELD_INVALID_DATE'; + } + + if (checkdate($month, $day, $year) === false) + { + return 'FIELD_INVALID_DATE'; + } + + return false; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 7324ef3ee7..8f2fe01d75 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -74,4 +74,30 @@ class type_dropdown implements type_interface $var_name = 'pf_' . $profile_row['field_ident']; return $this->request->variable($var_name, (int) $profile_row['field_default_value']); } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + $field_value = (int) $field_value; + + // retrieve option lang data if necessary + if (!isset($this->profilefields->options_lang[$field_data['field_id']]) || !isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->profilefields->options_lang[$file_data['field_id']][$field_data['lang_id']])) + { + $this->profilefields->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); + } + + if (!isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) + { + return 'FIELD_INVALID_VALUE'; + } + + if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + return false; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index c40d137a2b..d27adf5696 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -65,4 +65,28 @@ class type_int implements type_interface return $this->request->variable($var_name, (int) $profile_row['field_default_value']); } } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + if (trim($field_value) === '' && !$field_data['field_required']) + { + return false; + } + + $field_value = (int) $field_value; + + if ($field_value < $field_data['field_minlen']) + { + return 'FIELD_TOO_SMALL'; + } + else if ($field_value > $field_data['field_maxlen']) + { + return 'FIELD_TOO_LARGE'; + } + + return false; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index f3ca903e30..3c0c479a09 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -44,4 +44,13 @@ interface type_interface * @return mixed Submitted value of the profile field */ public function get_profile_field($profile_row); + + /** + * Validate entered profile field data + * + * @param mixed $field_value Field value to validate + * @param array $field_data Array with requirements of the field + * @return mixed String with key of the error language string, false otherwise + */ + public function validate_profile_field(&$field_value, $field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 64b0660e6e..84469749f1 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -58,4 +58,12 @@ class type_string extends type_string_common implements type_interface $var_name = 'pf_' . $profile_row['field_ident']; return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true); } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + return $this->validate_string_profile_field('string', &$field_value, $field_data); + } } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index dc2cd2f157..afb75f3026 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -27,4 +27,44 @@ abstract class type_string_common return $validate_options; } + + /** + * Validate entered profile field data + * + * @param string $field_type Field type (string or text) + * @param mixed $field_value Field value to validate + * @param array $field_data Array with requirements of the field + * @return mixed String with key of the error language string, false otherwise + */ + public function validate_string_profile_field($field_type, &$field_value, $field_data) + { + if (trim($field_value) === '' && !$field_data['field_required']) + { + return false; + } + else if (trim($field_value) === '' && $field_data['field_required']) + { + return 'FIELD_REQUIRED'; + } + + if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) + { + return 'FIELD_TOO_SHORT'; + } + else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) + { + return 'FIELD_TOO_LONG'; + } + + if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') + { + $field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value); + if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) + { + return 'FIELD_INVALID_CHARS'; + } + } + + return false; + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 8fff2c917a..2f8fcb12f5 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -58,4 +58,12 @@ class type_text extends type_string_common implements type_interface $var_name = 'pf_' . $profile_row['field_ident']; return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true); } + + /** + * {@inheritDoc} + */ + public function validate_profile_field(&$field_value, $field_data) + { + return $this->validate_string_profile_field('text', &$field_value, $field_data); + } } -- cgit v1.2.1 From dda5992a7931b4620d742e7ce489a006f1a7b05a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 10:20:33 +0100 Subject: [ticket/11201] Rename test file for now, so the other tests run PHPBB3-11201 --- tests/profile/custom_test.php | 55 ------------------------------------------ tests/profile/custom_test~.php | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 55 deletions(-) delete mode 100644 tests/profile/custom_test.php create mode 100644 tests/profile/custom_test~.php diff --git a/tests/profile/custom_test.php b/tests/profile/custom_test.php deleted file mode 100644 index 1f33b45ba9..0000000000 --- a/tests/profile/custom_test.php +++ /dev/null @@ -1,55 +0,0 @@ -createXMLDataSet(dirname(__FILE__).'/fixtures/profile_fields.xml'); - } - - static public function dropdownFields() - { - return array( - // note, there is an offset of 1 between option_id (0-indexed) - // in the database and values (1-indexed) to avoid problems with - // transmitting 0 in an HTML form - // required, value, expected - array(1, '0', 'FIELD_INVALID_VALUE', 'Required field should throw error for out-of-range value'), - array(1, '1', 'FIELD_REQUIRED', 'Required field should throw error for default value'), - array(1, '2', false, 'Required field should accept non-default value'), - array(0, '0', 'FIELD_INVALID_VALUE', 'Optional field should throw error for out-of-range value'), - array(0, '1', false, 'Optional field should accept default value'), - array(0, '2', false, 'Optional field should accept non-default value'), - ); - } - - /** - * @dataProvider dropdownFields - */ - public function test_dropdown_validate($field_required, $field_value, $expected, $description) - { - global $db; - $db = $this->new_dbal(); - - $field_data = array( - 'field_id' => 1, - 'lang_id' => 1, - 'field_novalue' => 1, - 'field_required' => $field_required, - ); - - $cp = new custom_profile; - $result = $cp->validate_profile_field(FIELD_DROPDOWN, $field_value, $field_data); - - $this->assertEquals($expected, $result, $description); - } -} diff --git a/tests/profile/custom_test~.php b/tests/profile/custom_test~.php new file mode 100644 index 0000000000..1f33b45ba9 --- /dev/null +++ b/tests/profile/custom_test~.php @@ -0,0 +1,55 @@ +createXMLDataSet(dirname(__FILE__).'/fixtures/profile_fields.xml'); + } + + static public function dropdownFields() + { + return array( + // note, there is an offset of 1 between option_id (0-indexed) + // in the database and values (1-indexed) to avoid problems with + // transmitting 0 in an HTML form + // required, value, expected + array(1, '0', 'FIELD_INVALID_VALUE', 'Required field should throw error for out-of-range value'), + array(1, '1', 'FIELD_REQUIRED', 'Required field should throw error for default value'), + array(1, '2', false, 'Required field should accept non-default value'), + array(0, '0', 'FIELD_INVALID_VALUE', 'Optional field should throw error for out-of-range value'), + array(0, '1', false, 'Optional field should accept default value'), + array(0, '2', false, 'Optional field should accept non-default value'), + ); + } + + /** + * @dataProvider dropdownFields + */ + public function test_dropdown_validate($field_required, $field_value, $expected, $description) + { + global $db; + $db = $this->new_dbal(); + + $field_data = array( + 'field_id' => 1, + 'lang_id' => 1, + 'field_novalue' => 1, + 'field_required' => $field_required, + ); + + $cp = new custom_profile; + $result = $cp->validate_profile_field(FIELD_DROPDOWN, $field_value, $field_data); + + $this->assertEquals($expected, $result, $description); + } +} -- cgit v1.2.1 From 0d79ccaaddb8c34bdf34780de104beeaf559308f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 10:36:44 +0100 Subject: [ticket/11201] Move get_profile_value() to type class PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 121 +-------------------- phpBB/phpbb/profilefields/type/type_bool.php | 32 ++++++ phpBB/phpbb/profilefields/type/type_date.php | 26 +++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 35 ++++++ phpBB/phpbb/profilefields/type/type_int.php | 12 ++ phpBB/phpbb/profilefields/type/type_interface.php | 9 ++ .../profilefields/type/type_string_common.php | 16 +++ 7 files changed, 132 insertions(+), 119 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index d8ba04a862..362859d57a 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -365,7 +365,8 @@ class profilefields foreach ($profile_row as $ident => $ident_ary) { - $value = $this->get_profile_value($ident_ary); + $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']); if ($value === NULL) { @@ -399,124 +400,6 @@ class profilefields } } - /** - * Get Profile Value for display - */ - function get_profile_value($ident_ary) - { - $value = $ident_ary['value']; - $field_type = $ident_ary['data']['field_type']; - - switch ($this->profile_types[$field_type]) - { - case 'int': - if ($value === '' && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - return (int) $value; - break; - - case 'string': - case 'text': - if (!$value && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - - $value = make_clickable($value); - $value = censor_text($value); - $value = bbcode_nl2br($value); - return $value; - break; - - // case 'datetime': - case 'date': - $date = explode('-', $value); - $day = (isset($date[0])) ? (int) $date[0] : 0; - $month = (isset($date[1])) ? (int) $date[1] : 0; - $year = (isset($date[2])) ? (int) $date[2] : 0; - - if (!$day && !$month && !$year && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - else if ($day && $month && $year) - { - // Date should display as the same date for every user regardless of timezone - return $this->user->create_datetime() - ->setDate($year, $month, $day) - ->setTime(0, 0, 0) - ->format($user->lang['DATE_FORMAT'], true); - } - - return $value; - break; - - case 'dropdown': - $field_id = $ident_ary['data']['field_id']; - $lang_id = $ident_ary['data']['lang_id']; - if (!isset($this->options_lang[$field_id][$lang_id])) - { - $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); - } - - if ($value == $ident_ary['data']['field_novalue'] && !$ident_ary['data']['field_show_novalue']) - { - return NULL; - } - - $value = (int) $value; - - // User not having a value assigned - if (!isset($this->options_lang[$field_id][$lang_id][$value])) - { - if ($ident_ary['data']['field_show_novalue']) - { - $value = $ident_ary['data']['field_novalue']; - } - else - { - return NULL; - } - } - - return $this->options_lang[$field_id][$lang_id][$value]; - break; - - case 'bool': - $field_id = $ident_ary['data']['field_id']; - $lang_id = $ident_ary['data']['lang_id']; - if (!isset($this->options_lang[$field_id][$lang_id])) - { - $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); - } - - if (!$value && $ident_ary['data']['field_show_novalue']) - { - $value = $ident_ary['data']['field_default_value']; - } - - if ($ident_ary['data']['field_length'] == 1) - { - return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL; - } - else if (!$value) - { - return NULL; - } - else - { - return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1]; - } - break; - - default: - trigger_error('Unknown profile type', E_USER_ERROR); - break; - } - } - /** * Get field value for registration/profile * @access private diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index c26e9aa160..7edb2ff2c6 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -94,4 +94,36 @@ class type_bool implements type_interface return false; } + + /** + * {@inheritDoc} + */ + public function get_profile_value($field_value, $field_data) + { + $field_id = $field_data['field_id']; + $lang_id = $field_data['lang_id']; + + if (!isset($this->profilefields->options_lang[$field_id][$lang_id])) + { + $this->profilefields->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); + } + + if (!$field_value && $field_data['field_show_novalue']) + { + $field_value = $field_data['field_default_value']; + } + + if ($field_data['field_length'] == 1) + { + return (isset($this->profilefields->options_lang[$field_id][$lang_id][(int) $field_value])) ? $this->options_lang[$field_id][$lang_id][(int) $field_value] : null; + } + else if (!$field_value) + { + return null; + } + else + { + return $this->profilefields->options_lang[$field_id][$lang_id][(int) ($field_value) + 1]; + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index a9b4bc731b..e7f1e9543e 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -129,4 +129,30 @@ class type_date implements type_interface return false; } + + /** + * {@inheritDoc} + */ + public function get_profile_value($field_value, $field_data) + { + $date = explode('-', $field_value); + $day = (isset($date[0])) ? (int) $date[0] : 0; + $month = (isset($date[1])) ? (int) $date[1] : 0; + $year = (isset($date[2])) ? (int) $date[2] : 0; + + if (!$day && !$month && !$year && !$field_data['field_show_novalue']) + { + return null; + } + else if ($day && $month && $year) + { + // Date should display as the same date for every user regardless of timezone + return $this->user->create_datetime() + ->setDate($year, $month, $day) + ->setTime(0, 0, 0) + ->format($user->lang['DATE_FORMAT'], true); + } + + return $field_value; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 8f2fe01d75..cfc8b289da 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -100,4 +100,39 @@ class type_dropdown implements type_interface return false; } + + /** + * {@inheritDoc} + */ + public function get_profile_value($field_value, $field_data) + { + $field_id = $field_data['field_id']; + $lang_id = $field_data['lang_id']; + if (!isset($this->profilefields->options_lang[$field_id][$lang_id])) + { + $this->profilefields->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); + } + + if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue']) + { + return null; + } + + $field_value = (int) $field_value; + + // User not having a value assigned + if (!isset($this->profilefields->options_lang[$field_id][$lang_id][$field_value])) + { + if ($field_data['field_show_novalue']) + { + $field_value = $field_data['field_novalue']; + } + else + { + return null; + } + } + + return $this->profilefields->options_lang[$field_id][$lang_id][$field_value]; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index d27adf5696..5aaa0036f7 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -89,4 +89,16 @@ class type_int implements type_interface return false; } + + /** + * {@inheritDoc} + */ + public function get_profile_value($field_value, $field_data) + { + if ($field_value === '' && !$field_data['field_show_novalue']) + { + return null; + } + return (int) $field_value; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 3c0c479a09..57f495ec9a 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -53,4 +53,13 @@ interface type_interface * @return mixed String with key of the error language string, false otherwise */ public function validate_profile_field(&$field_value, $field_data); + + /** + * Get Profile Value for display + * + * @param mixed $field_value Field value as stored in the database + * @param array $field_data Array with requirements of the field + * @return mixed Field value to display + */ + public function get_profile_value($field_value, $field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index afb75f3026..c026d93948 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -67,4 +67,20 @@ abstract class type_string_common return false; } + + /** + * {@inheritDoc} + */ + public function get_profile_value($field_value, $field_data) + { + if (!$field_value && !$field_data['field_show_novalue']) + { + return null; + } + + $field_value = make_clickable($field_value); + $field_value = censor_text($field_value); + $field_value = bbcode_nl2br($field_value); + return $field_value; + } } -- cgit v1.2.1 From 190c2e989a27bcd1ec9592f0d22faf32b496ed52 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 10:50:12 +0100 Subject: [ticket/11201] Remove call by refernce PHPBB3-11201 --- phpBB/phpbb/profilefields/type/type_string.php | 2 +- phpBB/phpbb/profilefields/type/type_text.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 84469749f1..e477ff4ea7 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -64,6 +64,6 @@ class type_string extends type_string_common implements type_interface */ public function validate_profile_field(&$field_value, $field_data) { - return $this->validate_string_profile_field('string', &$field_value, $field_data); + return $this->validate_string_profile_field('string', $field_value, $field_data); } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 2f8fcb12f5..f8377015aa 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -64,6 +64,6 @@ class type_text extends type_string_common implements type_interface */ public function validate_profile_field(&$field_value, $field_data) { - return $this->validate_string_profile_field('text', &$field_value, $field_data); + return $this->validate_string_profile_field('text', $field_value, $field_data); } } -- cgit v1.2.1 From d57c43d39769c7f29df85684428f6e120d65c103 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 12:04:02 +0100 Subject: [ticket/11201] Move error message generation to type class PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 44 +--------------------- phpBB/phpbb/profilefields/type/type_bool.php | 2 +- phpBB/phpbb/profilefields/type/type_date.php | 6 +-- phpBB/phpbb/profilefields/type/type_dropdown.php | 4 +- phpBB/phpbb/profilefields/type/type_int.php | 4 +- phpBB/phpbb/profilefields/type/type_interface.php | 2 +- .../profilefields/type/type_string_common.php | 18 +++++++-- 7 files changed, 25 insertions(+), 55 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 362859d57a..9edefdcca5 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -190,48 +190,8 @@ class profilefields if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false) { - // If not and only showing common error messages, use this one - $error = ''; - switch ($cp_result) - { - case 'FIELD_INVALID_DATE': - case 'FIELD_INVALID_VALUE': - case 'FIELD_REQUIRED': - $error = $this->user->lang($cp_result, $row['lang_name']); - break; - - case 'FIELD_TOO_SHORT': - case 'FIELD_TOO_SMALL': - $error = $this->user->lang($cp_result, (int) $row['field_minlen'], $row['lang_name']); - break; - - case 'FIELD_TOO_LONG': - case 'FIELD_TOO_LARGE': - $error = $this->user->lang($cp_result, (int) $row['field_maxlen'], $row['lang_name']); - break; - - case 'FIELD_INVALID_CHARS': - switch ($row['field_validation']) - { - case '[0-9]+': - $error = $this->user->lang($cp_result . '_NUMBERS_ONLY', $row['lang_name']); - break; - - case '[\w]+': - $error = $this->user->lang($cp_result . '_ALPHA_ONLY', $row['lang_name']); - break; - - case '[\w_\+\. \-\[\]]+': - $error = $this->user->lang($cp_result . '_SPACERS_ONLY', $row['lang_name']); - break; - } - break; - } - - if ($error != '') - { - $cp_error[] = $error; - } + // If the result is not false, it's an error message + $cp_error[] = $cp_result; } } $this->db->sql_freeresult($result); diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 7edb2ff2c6..c492e2bb81 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -89,7 +89,7 @@ class type_bool implements type_interface if (!$field_value && $field_data['field_required']) { - return 'FIELD_REQUIRED'; + return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); } return false; diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index e7f1e9543e..48fe651d17 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -114,17 +114,17 @@ class type_date implements type_interface if ((!$day || !$month || !$year) && $field_data['field_required']) { - return 'FIELD_REQUIRED'; + return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); } if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) { - return 'FIELD_INVALID_DATE'; + return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']); } if (checkdate($month, $day, $year) === false) { - return 'FIELD_INVALID_DATE'; + return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']); } return false; diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index cfc8b289da..0b7ee88217 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -90,12 +90,12 @@ class type_dropdown implements type_interface if (!isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) { - return 'FIELD_INVALID_VALUE'; + return $this->user->lang('FIELD_INVALID_VALUE', $field_data['lang_name']); } if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) { - return 'FIELD_REQUIRED'; + return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); } return false; diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 5aaa0036f7..0bb9d2049b 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -80,11 +80,11 @@ class type_int implements type_interface if ($field_value < $field_data['field_minlen']) { - return 'FIELD_TOO_SMALL'; + return $this->user->lang('FIELD_TOO_SMALL', (int) $row['field_minlen'], $row['lang_name']); } else if ($field_value > $field_data['field_maxlen']) { - return 'FIELD_TOO_LARGE'; + return $this->user->lang('FIELD_TOO_LARGE', (int) $row['field_maxlen'], $row['lang_name']); } return false; diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 57f495ec9a..36dfb5686f 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -50,7 +50,7 @@ interface type_interface * * @param mixed $field_value Field value to validate * @param array $field_data Array with requirements of the field - * @return mixed String with key of the error language string, false otherwise + * @return mixed String with the error message */ public function validate_profile_field(&$field_value, $field_data); diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index c026d93948..46e44fa85a 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -44,16 +44,16 @@ abstract class type_string_common } else if (trim($field_value) === '' && $field_data['field_required']) { - return 'FIELD_REQUIRED'; + return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); } if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) { - return 'FIELD_TOO_SHORT'; + return $this->user->lang('FIELD_TOO_SHORT', (int) $row['field_minlen'], $row['lang_name']); } else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) { - return 'FIELD_TOO_LONG'; + return $this->user->lang('FIELD_TOO_LONG', (int) $row['field_maxlen'], $row['lang_name']); } if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') @@ -61,7 +61,17 @@ abstract class type_string_common $field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value); if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) { - return 'FIELD_INVALID_CHARS'; + switch ($row['field_validation']) + { + case '[0-9]+': + return $this->user->lang('FIELD_INVALID_CHARS_NUMBERS_ONLY', $row['lang_name']); + + case '[\w]+': + return $this->user->lang('FIELD_INVALID_CHARS_ALPHA_ONLY', $row['lang_name']); + + case '[\w_\+\. \-\[\]]+': + return $this->user->lang('FIELD_INVALID_CHARS_SPACERS_ONLY', $row['lang_name']); + } } } -- cgit v1.2.1 From daf21fcb30c5c76c1d3f0b2a2bce8d8c1d8aed14 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 12:51:32 +0100 Subject: [ticket/11201] Move generate_field() method to type class PHPBB3-11201 --- phpBB/config/profilefields.yml | 6 + phpBB/phpbb/profilefields/profilefields.php | 207 +--------------------- phpBB/phpbb/profilefields/type/type_bool.php | 44 ++++- phpBB/phpbb/profilefields/type/type_date.php | 57 +++++- phpBB/phpbb/profilefields/type/type_dropdown.php | 32 +++- phpBB/phpbb/profilefields/type/type_int.php | 37 +++- phpBB/phpbb/profilefields/type/type_interface.php | 9 + phpBB/phpbb/profilefields/type/type_string.php | 17 +- phpBB/phpbb/profilefields/type/type_text.php | 20 ++- 9 files changed, 218 insertions(+), 211 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 486788df3b..2cecb80534 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -14,6 +14,7 @@ services: arguments: - @profilefields - @request + - @template - @user tags: - { name: profilefield.type } @@ -23,6 +24,7 @@ services: arguments: - @profilefields - @request + - @template - @user tags: - { name: profilefield.type } @@ -32,6 +34,7 @@ services: arguments: - @profilefields - @request + - @template - @user tags: - { name: profilefield.type } @@ -40,6 +43,7 @@ services: class: \phpbb\profilefields\type\type_int arguments: - @request + - @template - @user tags: - { name: profilefield.type } @@ -48,6 +52,7 @@ services: class: \phpbb\profilefields\type\type_string arguments: - @request + - @template - @user tags: - { name: profilefield.type } @@ -56,6 +61,7 @@ services: class: \phpbb\profilefields\type\type_text arguments: - @request + - @template - @user tags: - { name: profilefield.type } diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 9edefdcca5..ba0baa53f0 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -360,209 +360,6 @@ class profilefields } } - /** - * Get field value for registration/profile - * @access private - */ - function get_var($field_validation, &$profile_row, $default_value, $preview) - { - $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; - $user_ident = $profile_row['field_ident']; - // checkbox - set the value to "true" if it has been set to 1 - if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2) - { - $value = (isset($_REQUEST[$profile_row['field_ident']]) && request_var($profile_row['field_ident'], $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $default_value : $this->user->profile_fields[$user_ident]); - } - else if ($profile_row['field_type'] == FIELD_INT) - { - if (isset($_REQUEST[$profile_row['field_ident']])) - { - $value = ($this->request->variable($profile_row['field_ident'], '') === '') ? NULL : $this->request->variable($profile_row['field_ident'], $default_value); - } - else - { - if (!$preview && array_key_exists($user_ident, $this->user->profile_fields) && is_null($this->user->profile_fields[$user_ident])) - { - $value = NULL; - } - else if (!isset($this->user->profile_fields[$user_ident]) || $preview) - { - $value = $default_value; - } - else - { - $value = $this->user->profile_fields[$user_ident]; - } - } - - return (is_null($value) || $value === '') ? '' : (int) $value; - } - else - { - $value = (isset($_REQUEST[$profile_row['field_ident']])) ? request_var($profile_row['field_ident'], $default_value, true) : ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $default_value : $this->user->profile_fields[$user_ident]); - - if (gettype($value) == 'string') - { - $value = utf8_normalize_nfc($value); - } - } - - switch ($field_validation) - { - case 'int': - return (int) $value; - break; - } - - return $value; - } - - /** - * Process int-type - * @access private - */ - function generate_int($profile_row, $preview = false) - { - $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process date-type - * @access private - */ - function generate_date($profile_row, $preview = false) - { - $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; - $user_ident = $profile_row['field_ident']; - - $now = getdate(); - - if (!isset($_REQUEST[$profile_row['field_ident'] . '_day'])) - { - if ($profile_row['field_default_value'] == 'now') - { - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); - } - else - { - if ($preview && $profile_row['field_default_value'] == 'now') - { - $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); - } - else - { - $day = request_var($profile_row['field_ident'] . '_day', 0); - $month = request_var($profile_row['field_ident'] . '_month', 0); - $year = request_var($profile_row['field_ident'] . '_year', 0); - } - } - - $profile_row['s_day_options'] = ''; - for ($i = 1; $i < 32; $i++) - { - $profile_row['s_day_options'] .= '"; - } - - $profile_row['s_month_options'] = ''; - for ($i = 1; $i < 13; $i++) - { - $profile_row['s_month_options'] .= '"; - } - - $profile_row['s_year_options'] = ''; - for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++) - { - $profile_row['s_year_options'] .= '"; - } - unset($now); - - $profile_row['field_value'] = 0; - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process bool-type - * @access private - */ - function generate_bool($profile_row, $preview = false) - { - $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - - $profile_row['field_value'] = $value; - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - - if ($profile_row['field_length'] == 1) - { - 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']])) - { - $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview); - } - - foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) - { - $this->template->assign_block_vars('bool.options', array( - 'OPTION_ID' => $option_id, - 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '', - 'VALUE' => $option_value) - ); - } - } - } - - /** - * Process string-type - * @access private - */ - function generate_string($profile_row, $preview = false) - { - $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process text-type - * @access private - */ - function generate_text($profile_row, $preview = false) - { - $field_length = explode('|', $profile_row['field_length']); - $profile_row['field_rows'] = $field_length[0]; - $profile_row['field_cols'] = $field_length[1]; - - $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview); - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - } - - /** - * Process dropdown-type - * @access private - */ - function generate_dropdown($profile_row, $preview = false) - { - $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); - - 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']])) - { - $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview); - } - - $profile_row['field_value'] = $value; - $this->template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER)); - - foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) - { - $this->template->assign_block_vars('dropdown.options', array( - 'OPTION_ID' => $option_id, - 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '', - 'VALUE' => $option_value) - ); - } - } - /** * Return Templated value/field. Possible values for $mode are: * change == user is able to set/enter profile values; preview == just show the value @@ -584,8 +381,8 @@ class profilefields } // Assign template variables - $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']]; - $this->$type_func($profile_row, $preview); + $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$profile_row['field_type']]); + $profile_field->generate_field($profile_row, $preview); // Return templated data return $this->template->assign_display('cp_body'); diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index c492e2bb81..ee75578a16 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -14,10 +14,11 @@ class type_bool implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->profilefields = $profilefields; $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -126,4 +127,45 @@ class type_bool implements type_interface return $this->profilefields->options_lang[$field_id][$lang_id][(int) ($field_value) + 1]; } } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; + $default_value = $profile_row['lang_default_value']; + + $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); + // checkbox - set the value to "true" if it has been set to 1 + if ($profile_row['field_length'] == 2) + { + $value = ($this->request->is_set($field_ident) && $this->request->variable($field_ident, $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + } + else + { + $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + } + + $profile_row['field_value'] = (int) $value; + $this->template->assign_block_vars('bool', array_change_key_case($profile_row, CASE_UPPER)); + + if ($profile_row['field_length'] == 1) + { + if (!isset($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']])) + { + $this->profilefields->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview); + } + + foreach ($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + { + $this->template->assign_block_vars('bool.options', array( + 'OPTION_ID' => $option_id, + 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '', + 'VALUE' => $option_value) + ); + } + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 48fe651d17..36e82dabce 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -14,10 +14,11 @@ class type_date implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->profilefields = $profilefields; $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -155,4 +156,58 @@ class type_date implements type_interface return $field_value; } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + + $now = getdate(); + + if (!$this->request->is_set($profile_row['field_ident'] . '_day')) + { + if ($profile_row['field_default_value'] == 'now') + { + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + } + else + { + if ($preview && $profile_row['field_default_value'] == 'now') + { + $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + } + else + { + $day = $this->request->variable($profile_row['field_ident'] . '_day', 0); + $month = $this->request->variable($profile_row['field_ident'] . '_month', 0); + $year = $this->request->variable($profile_row['field_ident'] . '_year', 0); + } + } + + $profile_row['s_day_options'] = ''; + for ($i = 1; $i < 32; $i++) + { + $profile_row['s_day_options'] .= '"; + } + + $profile_row['s_month_options'] = ''; + for ($i = 1; $i < 13; $i++) + { + $profile_row['s_month_options'] .= '"; + } + + $profile_row['s_year_options'] = ''; + for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++) + { + $profile_row['s_year_options'] .= '"; + } + + $profile_row['field_value'] = 0; + $this->template->assign_block_vars('date', array_change_key_case($profile_row, CASE_UPPER)); + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 0b7ee88217..61fa8d7585 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -14,10 +14,11 @@ class type_dropdown implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->profilefields = $profilefields; $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -135,4 +136,33 @@ class type_dropdown implements type_interface return $this->profilefields->options_lang[$field_id][$lang_id][$field_value]; } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; + $default_value = $profile_row['lang_default_value']; + + $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + + if (!isset($this->profilefields->options_lang[$profile_row['field_id']]) || !isset($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']])) + { + $this->profilefields->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview); + } + + $profile_row['field_value'] = (int) $value; + $this->template->assign_block_vars('dropdown', array_change_key_case($profile_row, CASE_UPPER)); + + foreach ($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + { + $this->template->assign_block_vars('dropdown.options', array( + 'OPTION_ID' => $option_id, + 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '', + 'VALUE' => $option_value) + ); + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 0bb9d2049b..7285b0f4ba 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -14,9 +14,10 @@ class type_int implements type_interface /** * */ - public function __construct(\phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -101,4 +102,38 @@ class type_int implements type_interface } return (int) $field_value; } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; + $default_value = $profile_row['lang_default_value']; + + if ($this->request->is_set($field_ident)) + { + $value = ($this->request->variable($field_ident, '') === '') ? null : $this->request->variable($field_ident, $default_value); + } + else + { + if (!$preview && array_key_exists($field_ident, $this->user->profile_fields) && is_null($this->user->profile_fields[$field_ident])) + { + $value = null; + } + else if (!isset($this->user->profile_fields[$field_ident]) || $preview) + { + $value = $default_value; + } + else + { + $value = $this->user->profile_fields[$field_ident]; + } + } + + $profile_row['field_value'] = (is_null($value) || $value === '') ? '' : (int) $value; + + $this->template->assign_block_vars('int', array_change_key_case($profile_row, CASE_UPPER)); + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 36dfb5686f..92925c3023 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -62,4 +62,13 @@ interface type_interface * @return mixed Field value to display */ public function get_profile_value($field_value, $field_data); + + /** + * Generate the input field for display + * + * @param array $profile_row Array with data for this field + * @param bool $preview Do we preview the form + * @return null + */ + public function generate_field($profile_row, $preview = false); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index e477ff4ea7..c9929b1110 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -14,9 +14,10 @@ class type_string extends type_string_common implements type_interface /** * */ - public function __construct(\phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -66,4 +67,18 @@ class type_string extends type_string_common implements type_interface { return $this->validate_string_profile_field('string', $field_value, $field_data); } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; + $default_value = $profile_row['lang_default_value']; + $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + + + $this->template->assign_block_vars('string', array_change_key_case($profile_row, CASE_UPPER)); + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index f8377015aa..476e1d204f 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -14,9 +14,10 @@ class type_text extends type_string_common implements type_interface /** * */ - public function __construct(\phpbb\request\request $request, \phpbb\user $user) + public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->request = $request; + $this->template = $template; $this->user = $user; } @@ -66,4 +67,21 @@ class type_text extends type_string_common implements type_interface { return $this->validate_string_profile_field('text', $field_value, $field_data); } + + /** + * {@inheritDoc} + */ + public function generate_field($profile_row, $preview = false) + { + $field_length = explode('|', $profile_row['field_length']); + $profile_row['field_rows'] = $field_length[0]; + $profile_row['field_cols'] = $field_length[1]; + $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; + $default_value = $profile_row['lang_default_value']; + + $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + + $this->template->assign_block_vars('text', array_change_key_case($profile_row, CASE_UPPER)); + } } -- cgit v1.2.1 From d601aaad263e19b47865bca56da527f4fdf504e1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 13:38:24 +0100 Subject: [ticket/11201] Remove type related code from build_insert_sql_array() PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 2 +- phpBB/phpbb/profilefields/profilefields.php | 14 ++------------ phpBB/phpbb/profilefields/type/type_bool.php | 10 +++++++++- phpBB/phpbb/profilefields/type/type_date.php | 16 +++++++++++++++- phpBB/phpbb/profilefields/type/type_dropdown.php | 10 +++++++++- phpBB/phpbb/profilefields/type/type_int.php | 16 +++++++++++++++- phpBB/phpbb/profilefields/type/type_interface.php | 12 ++++++++++-- phpBB/phpbb/profilefields/type/type_string.php | 2 +- phpBB/phpbb/profilefields/type/type_string_common.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_text.php | 2 +- 10 files changed, 71 insertions(+), 21 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 44c2e0f11e..b3239af1ab 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -372,7 +372,7 @@ class acp_profile } $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); - $field_row = array_merge($profile_field->get_default_values(), array( + $field_row = array_merge($profile_field->get_default_option_values(), array( 'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))), 'field_required' => 0, 'field_show_novalue'=> 0, diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index ba0baa53f0..e3201712df 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -408,18 +408,8 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE) - { - $now = getdate(); - $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - } - else if ($row['field_default_value'] === '' && $row['field_type'] == FIELD_INT) - { - // We cannot insert an empty string into an integer column. - $row['field_default_value'] = NULL; - } - - $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value']; + $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row); } $this->db->sql_freeresult($result); diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index ee75578a16..e60806becd 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -51,7 +51,7 @@ class type_bool implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => 1, @@ -63,6 +63,14 @@ class type_bool implements type_interface ); } + /** + * {@inheritDoc} + */ + public function get_default_field_value($field_data) + { + return $field_data['field_default_value']; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 36e82dabce..7aabafcb14 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -59,7 +59,7 @@ class type_date implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => 10, @@ -71,6 +71,20 @@ class type_date implements type_interface ); } + /** + * {@inheritDoc} + */ + public function get_default_field_value($field_data) + { + if ($field_data['field_default_value'] == 'now') + { + $now = getdate(); + $field_data['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); + } + + return $field_data['field_default_value']; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 61fa8d7585..c461815426 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -55,7 +55,7 @@ class type_dropdown implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => 0, @@ -67,6 +67,14 @@ class type_dropdown implements type_interface ); } + /** + * {@inheritDoc} + */ + public function get_default_field_value($field_data) + { + return $field_data['field_default_value']; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 7285b0f4ba..f650436d21 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -39,7 +39,7 @@ class type_int implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => 5, @@ -51,6 +51,20 @@ class type_int implements type_interface ); } + /** + * {@inheritDoc} + */ + public function get_default_field_value($field_data) + { + if ($field_data['field_default_value'] === '') + { + // We cannot insert an empty string into an integer column. + return null; + } + + return $field_data['field_default_value']; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 92925c3023..d53c038d2d 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -31,11 +31,19 @@ interface type_interface public function get_options($default_lang_id, $field_data); /** - * Get default values for this type + * Get default values for the options of this type * * @return array with values like default field size and more */ - public function get_default_values(); + public function get_default_option_values(); + + /** + * Get default value for this type + * + * @param array $field_data Array with data for this field + * @return mixed default value for new users when no value is given + */ + public function get_default_field_value($field_data); /** * Get profile field value on submit diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index c9929b1110..090b5a2a54 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -39,7 +39,7 @@ class type_string extends type_string_common implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => 10, diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index 46e44fa85a..02b640bb44 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -28,6 +28,14 @@ abstract class type_string_common return $validate_options; } + /** + * {@inheritDoc} + */ + public function get_default_field_value($field_data) + { + return $field_data['lang_default_value']; + } + /** * Validate entered profile field data * diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 476e1d204f..e73ffc5375 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -39,7 +39,7 @@ class type_text extends type_string_common implements type_interface /** * {@inheritDoc} */ - public function get_default_values() + public function get_default_option_values() { return array( 'field_length' => '5|80', -- cgit v1.2.1 From 9dec023632ae09dd457982db4a5e68fa3bb49887 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 13:42:18 +0100 Subject: [ticket/11201] Add visibility to methods PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index e3201712df..78b408ba56 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -35,9 +35,8 @@ class profilefields /** * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) * Called by ucp_profile and ucp_register - * @access public */ - function generate_profile_fields($mode, $lang_id) + public function generate_profile_fields($mode, $lang_id) { $sql_where = ''; switch ($mode) @@ -90,9 +89,8 @@ class profilefields /** * Build profile cache, used for display - * @access private */ - function build_cache() + protected function build_cache() { $this->profile_cache = array(); @@ -117,7 +115,7 @@ class profilefields /** * Get language entries for options and store them here for later use */ - function get_option_lang($field_id, $lang_id, $field_type, $preview) + public function get_option_lang($field_id, $lang_id, $field_type, $preview) { if ($preview) { @@ -148,9 +146,8 @@ class profilefields /** * Submit profile field for validation - * @access public */ - function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) + public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) { $sql_where = ''; switch ($mode) @@ -200,7 +197,7 @@ class profilefields /** * Update profile field data directly */ - function update_profile_field_data($user_id, &$cp_data) + public function update_profile_field_data($user_id, &$cp_data) { if (!sizeof($cp_data)) { @@ -259,9 +256,8 @@ class profilefields /** * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled) * 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 - * @access public */ - function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) + public function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) { if ($mode == 'grab') { @@ -363,9 +359,8 @@ class profilefields /** * Return Templated value/field. Possible values for $mode are: * change == user is able to set/enter profile values; preview == just show the value - * @access private */ - function process_field_row($mode, $profile_row) + protected function process_field_row($mode, $profile_row) { $preview = ($mode == 'preview') ? true : false; @@ -391,7 +386,7 @@ class profilefields /** * Build Array for user insertion into custom profile fields table */ - function build_insert_sql_array($cp_data) + public function build_insert_sql_array($cp_data) { $sql_not_in = array(); foreach ($cp_data as $key => $null) -- cgit v1.2.1 From cd6bdc7b2719bea5c89e96efa5b175ed54a6b496 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 14 Jan 2014 15:08:33 +0100 Subject: [ticket/11201] Remove remaining type depending code to type class PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 10 ++++------ phpBB/phpbb/profilefields/type/type_bool.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_date.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_int.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 11 +++++++++++ phpBB/phpbb/profilefields/type/type_string_common.php | 8 ++++++++ 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 78b408ba56..e01611460b 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -72,17 +72,15 @@ class profilefields { // Return templated field $tpl_snippet = $this->process_field_row('change', $row); - - // Some types are multivalue, we can't give them a field_id as we would not know which to pick - $type = (int) $row['field_type']; + $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); $this->template->assign_block_vars('profile_fields', array( 'LANG_NAME' => $row['lang_name'], 'LANG_EXPLAIN' => $row['lang_explain'], 'FIELD' => $tpl_snippet, - 'FIELD_ID' => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'], - 'S_REQUIRED' => ($row['field_required']) ? true : false) - ); + 'FIELD_ID' => $profile_field->get_field_ident($row), + 'S_REQUIRED' => ($row['field_required']) ? true : false, + )); } $this->db->sql_freeresult($result); } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index e60806becd..f4b056d3fc 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -176,4 +176,12 @@ class type_bool implements type_interface } } } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return ($field_data['field_length'] == '1') ? '' : 'pf_' . $field_data['field_ident']; + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 7aabafcb14..21b55874e5 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -224,4 +224,12 @@ class type_date implements type_interface $profile_row['field_value'] = 0; $this->template->assign_block_vars('date', array_change_key_case($profile_row, CASE_UPPER)); } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return ''; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index c461815426..9b434fd085 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -173,4 +173,12 @@ class type_dropdown implements type_interface ); } } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return 'pf_' . $field_data['field_ident']; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index f650436d21..7ac99f5b80 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -150,4 +150,12 @@ class type_int implements type_interface $this->template->assign_block_vars('int', array_change_key_case($profile_row, CASE_UPPER)); } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return 'pf_' . $field_data['field_ident']; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index d53c038d2d..9c78e1956d 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -79,4 +79,15 @@ interface type_interface * @return null */ public function generate_field($profile_row, $preview = false); + + /** + * Get the ident of the field + * + * Some types are multivalue, we can't give them a field_id + * as we would not know which to pick. + * + * @param array $field_data Array with data for this field + * @return string ident of the field + */ + public function get_field_ident($field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index 02b640bb44..686c50d4db 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -101,4 +101,12 @@ abstract class type_string_common $field_value = bbcode_nl2br($field_value); return $field_value; } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return 'pf_' . $field_data['field_ident']; + } } -- cgit v1.2.1 From 5df7f76e6b810f08076b905f189bc7e4c3f8b8b1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 11:40:03 +0100 Subject: [ticket/11201] Split language options into a new class Otherwise we run into a circular dependency PHPBB3-11201 --- phpBB/config/profilefields.yml | 17 +++- phpBB/phpbb/profilefields/lang_helper.php | 114 ++++++++++++++++++++++ phpBB/phpbb/profilefields/profilefields.php | 37 +------ phpBB/phpbb/profilefields/type/type_bool.php | 28 +++--- phpBB/phpbb/profilefields/type/type_date.php | 10 +- phpBB/phpbb/profilefields/type/type_dropdown.php | 33 ++++--- phpBB/phpbb/profilefields/type/type_int.php | 8 +- phpBB/phpbb/profilefields/type/type_interface.php | 6 +- phpBB/phpbb/profilefields/type/type_string.php | 6 +- phpBB/phpbb/profilefields/type/type_text.php | 6 +- 10 files changed, 180 insertions(+), 85 deletions(-) create mode 100644 phpBB/phpbb/profilefields/lang_helper.php diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 2cecb80534..95242fe10c 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -5,14 +5,27 @@ services: - @auth - @dbal.conn - @service_container + #- @profilefields.type_collection - @request - @template - @user + profilefields.lang_helper: + class: \phpbb\profilefields\lang_helper + arguments: + - @dbal.conn + + profilefields.type_collection: + class: phpbb\di\service_collection + arguments: + - @service_container + tags: + - { name: service_collection, tag: profilefield.type } + profilefields.type.bool: class: \phpbb\profilefields\type\type_bool arguments: - - @profilefields + - @profilefields.lang_helper - @request - @template - @user @@ -32,7 +45,7 @@ services: profilefields.type.dropdown: class: \phpbb\profilefields\type\type_dropdown arguments: - - @profilefields + - @profilefields.lang_helper - @request - @template - @user diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php new file mode 100644 index 0000000000..ddf77f5e42 --- /dev/null +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -0,0 +1,114 @@ +db = $db; + } + + /** + * Get language entries for options and store them here for later use + */ + public function get_option_lang($field_id, $lang_id, $field_type, $preview_options) + { + if ($preview_options !== false) + { + $lang_options = (!is_array($preview_options)) ? explode("\n", $preview_options) : $preview_options; + + foreach ($lang_options as $num => $var) + { + $this->options_lang[$field_id][$lang_id][($num + 1)] = $var; + } + } + else + { + $sql = 'SELECT option_id, lang_value + FROM ' . PROFILE_FIELDS_LANG_TABLE . " + WHERE field_id = $field_id + AND lang_id = $lang_id + AND field_type = $field_type + ORDER BY option_id"; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value']; + } + $this->db->sql_freeresult($result); + } + } + + /** + * Are language options set for this field? + * + * @param int $field_id Database ID of the field + * @param int $lang_id ID of the language + * @param int $field_value Selected value of the field + * @return boolean + */ + public function is_set($field_id, $lang_id = null, $field_value = null) + { + $is_set = isset($this->lang_helper->options_lang[$field_id]); + + if ($is_set && (!is_null($lang_id) || !is_null($field_value))) + { + $is_set = isset($this->lang_helper->options_lang[$field_id][$lang_id]); + } + + if ($is_set && !is_null($field_value)) + { + $is_set = isset($this->lang_helper->options_lang[$field_id][$lang_id][$field_value]); + } + + return $is_set; + } + + /** + * Get the selected language string + * + * @param int $field_id Database ID of the field + * @param int $lang_id ID of the language + * @param int $field_value Selected value of the field + * @return string + */ + public function get($field_id, $lang_id, $field_value = null) + { + if (!is_null($field_value)) + { + return $this->lang_helper->options_lang[$field_id][$lang_id]; + } + + return $this->lang_helper->options_lang[$field_id][$lang_id][$field_value]; + } +} diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index e01611460b..0ed63223f5 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -17,7 +17,6 @@ class profilefields { var $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date'); var $profile_cache = array(); - var $options_lang = array(); /** * @@ -110,38 +109,6 @@ class profilefields $this->db->sql_freeresult($result); } - /** - * Get language entries for options and store them here for later use - */ - public function get_option_lang($field_id, $lang_id, $field_type, $preview) - { - if ($preview) - { - $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options']; - - foreach ($lang_options as $num => $var) - { - $this->options_lang[$field_id][$lang_id][($num + 1)] = $var; - } - } - else - { - $sql = 'SELECT option_id, lang_value - FROM ' . PROFILE_FIELDS_LANG_TABLE . " - WHERE field_id = $field_id - AND lang_id = $lang_id - AND field_type = $field_type - ORDER BY option_id"; - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value']; - } - $this->db->sql_freeresult($result); - } - } - /** * Submit profile field for validation */ @@ -360,7 +327,7 @@ class profilefields */ protected function process_field_row($mode, $profile_row) { - $preview = ($mode == 'preview') ? true : false; + $preview_options = ($mode == 'preview') ? $this->vars['lang_options'] : false; // set template filename $this->template->set_filenames(array( @@ -375,7 +342,7 @@ class profilefields // Assign template variables $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$profile_row['field_type']]); - $profile_field->generate_field($profile_row, $preview); + $profile_field->generate_field($profile_row, $preview_options); // Return templated data return $this->template->assign_display('cp_body'); diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f4b056d3fc..f32f4a7513 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -14,9 +14,9 @@ class type_bool implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { - $this->profilefields = $profilefields; + $this->lang_helper = $lang_helper; $this->request = $request; $this->template = $template; $this->user = $user; @@ -42,7 +42,7 @@ class type_bool implements type_interface $options = array( 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)) + 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)), ); return $options; @@ -112,9 +112,9 @@ class type_bool implements type_interface $field_id = $field_data['field_id']; $lang_id = $field_data['lang_id']; - if (!isset($this->profilefields->options_lang[$field_id][$lang_id])) + if (!$this->lang_helper->is_set($field_id, $lang_id)) { - $this->profilefields->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); + $this->lang_helper->get_option_lang($field_id, $lang_id, FIELD_BOOL, false); } if (!$field_value && $field_data['field_show_novalue']) @@ -124,7 +124,7 @@ class type_bool implements type_interface if ($field_data['field_length'] == 1) { - return (isset($this->profilefields->options_lang[$field_id][$lang_id][(int) $field_value])) ? $this->options_lang[$field_id][$lang_id][(int) $field_value] : null; + return ($this->lang_helper->is_set($field_id, $lang_id, (int) $field_value)) ? $this->lang_helper->get($field_id, $lang_id, (int) $field_value) : null; } else if (!$field_value) { @@ -132,28 +132,27 @@ class type_bool implements type_interface } else { - return $this->profilefields->options_lang[$field_id][$lang_id][(int) ($field_value) + 1]; + return $this->lang_helper->is_set($field_id, $lang_id, $field_value + 1); } } /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; $default_value = $profile_row['lang_default_value']; - $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview); // checkbox - set the value to "true" if it has been set to 1 if ($profile_row['field_length'] == 2) { - $value = ($this->request->is_set($field_ident) && $this->request->variable($field_ident, $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + $value = ($this->request->is_set($field_ident) && $this->request->variable($field_ident, $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); } else { - $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); } $profile_row['field_value'] = (int) $value; @@ -161,12 +160,13 @@ class type_bool implements type_interface if ($profile_row['field_length'] == 1) { - if (!isset($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']])) + if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1)) { - $this->profilefields->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview); + $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview_options); } - foreach ($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']); + foreach ($options as $option_id => $option_value) { $this->template->assign_block_vars('bool.options', array( 'OPTION_ID' => $option_id, diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 21b55874e5..fda09c05e2 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -35,7 +35,7 @@ class type_date implements type_interface 'field_default_value' => $field_data['field_default_value'], 'field_ident' => 'field_default_value', 'field_type' => FIELD_DATE, - 'field_length' => $field_data['field_length'] + 'field_length' => $field_data['field_length'], ); $always_now = request_var('always_now', -1); @@ -174,7 +174,7 @@ class type_date implements type_interface /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; @@ -186,14 +186,14 @@ class type_date implements type_interface { $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); } - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); } else { - if ($preview && $profile_row['field_default_value'] == 'now') + if ($preview_options !== false && $profile_row['field_default_value'] == 'now') { $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); } else { diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 9b434fd085..a3217f004c 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -14,9 +14,9 @@ class type_dropdown implements type_interface /** * */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { - $this->profilefields = $profilefields; + $this->lang_helper = $lang_helper; $this->request = $request; $this->template = $template; $this->user = $user; @@ -36,7 +36,7 @@ class type_dropdown implements type_interface 'field_default_value' => $field_data['field_default_value'], 'field_ident' => 'field_default_value', 'field_type' => FIELD_DROPDOWN, - 'lang_options' => $field_data['lang_options'] + 'lang_options' => $field_data['lang_options'], ); $profile_row[1] = $profile_row[0]; @@ -46,7 +46,7 @@ class type_dropdown implements type_interface $options = array( 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[1])) + 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[1])), ); return $options; @@ -92,12 +92,12 @@ class type_dropdown implements type_interface $field_value = (int) $field_value; // retrieve option lang data if necessary - if (!isset($this->profilefields->options_lang[$field_data['field_id']]) || !isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->profilefields->options_lang[$file_data['field_id']][$field_data['lang_id']])) + if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], 1)) { - $this->profilefields->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); + $this->lang_helper->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); } - if (!isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value])) + if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], $field_value)) { return $this->user->lang('FIELD_INVALID_VALUE', $field_data['lang_name']); } @@ -117,9 +117,9 @@ class type_dropdown implements type_interface { $field_id = $field_data['field_id']; $lang_id = $field_data['lang_id']; - if (!isset($this->profilefields->options_lang[$field_id][$lang_id])) + if (!$this->lang_helper->is_set($field_id, $lang_id)) { - $this->profilefields->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); + $this->lang_helper->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); } if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue']) @@ -130,7 +130,7 @@ class type_dropdown implements type_interface $field_value = (int) $field_value; // User not having a value assigned - if (!isset($this->profilefields->options_lang[$field_id][$lang_id][$field_value])) + if (!$this->lang_helper->is_set($field_id, $lang_id, $field_value)) { if ($field_data['field_show_novalue']) { @@ -142,29 +142,30 @@ class type_dropdown implements type_interface } } - return $this->profilefields->options_lang[$field_id][$lang_id][$field_value]; + return $this->lang_helper->get($field_id, $lang_id, $field_value); } /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; $default_value = $profile_row['lang_default_value']; - $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); - if (!isset($this->profilefields->options_lang[$profile_row['field_id']]) || !isset($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']])) + if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1)) { - $this->profilefields->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview); + $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview_options); } $profile_row['field_value'] = (int) $value; $this->template->assign_block_vars('dropdown', array_change_key_case($profile_row, CASE_UPPER)); - foreach ($this->profilefields->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value) + $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']); + foreach ($options as $option_id => $option_value) { $this->template->assign_block_vars('dropdown.options', array( 'OPTION_ID' => $option_id, diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 7ac99f5b80..13e39039c3 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -30,7 +30,7 @@ class type_int implements type_interface 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), 1 => array('TITLE' => $this->user->lang['MIN_FIELD_NUMBER'], 'FIELD' => ''), 2 => array('TITLE' => $this->user->lang['MAX_FIELD_NUMBER'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => '') + 3 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => ''), ); return $options; @@ -120,7 +120,7 @@ class type_int implements type_interface /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; @@ -132,11 +132,11 @@ class type_int implements type_interface } else { - if (!$preview && array_key_exists($field_ident, $this->user->profile_fields) && is_null($this->user->profile_fields[$field_ident])) + if ($preview_options === false && array_key_exists($field_ident, $this->user->profile_fields) && is_null($this->user->profile_fields[$field_ident])) { $value = null; } - else if (!isset($this->user->profile_fields[$field_ident]) || $preview) + else if (!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) { $value = $default_value; } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 9c78e1956d..5b662205b3 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -74,11 +74,11 @@ interface type_interface /** * Generate the input field for display * - * @param array $profile_row Array with data for this field - * @param bool $preview Do we preview the form + * @param array $profile_row Array with data for this field + * @param mixed $preview_options When previewing we use different data * @return null */ - public function generate_field($profile_row, $preview = false); + public function generate_field($profile_row, $preview_options = false); /** * Get the ident of the field diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 090b5a2a54..57edd14d62 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -30,7 +30,7 @@ class type_string extends type_string_common implements type_interface 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ''), 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => ''), ); return $options; @@ -71,12 +71,12 @@ class type_string extends type_string_common implements type_interface /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; $default_value = $profile_row['lang_default_value']; - $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); $this->template->assign_block_vars('string', array_change_key_case($profile_row, CASE_UPPER)); diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index e73ffc5375..da1bb32859 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -30,7 +30,7 @@ class type_text extends type_string_common implements type_interface 0 => array('TITLE' => $this->user->lang['FIELD_LENGTH'], 'FIELD' => ' ' . $this->user->lang['ROWS'] . '
' . $this->user->lang['COLUMNS'] . ' '), 1 => array('TITLE' => $this->user->lang['MIN_FIELD_CHARS'], 'FIELD' => ''), 2 => array('TITLE' => $this->user->lang['MAX_FIELD_CHARS'], 'FIELD' => ''), - 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => '') + 3 => array('TITLE' => $this->user->lang['FIELD_VALIDATION'], 'FIELD' => ''), ); return $options; @@ -71,7 +71,7 @@ class type_text extends type_string_common implements type_interface /** * {@inheritDoc} */ - public function generate_field($profile_row, $preview = false) + public function generate_field($profile_row, $preview_options = false) { $field_length = explode('|', $profile_row['field_length']); $profile_row['field_rows'] = $field_length[0]; @@ -80,7 +80,7 @@ class type_text extends type_string_common implements type_interface $field_ident = $profile_row['field_ident']; $default_value = $profile_row['lang_default_value']; - $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview) ? $default_value : $this->user->profile_fields[$field_ident]); + $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); $this->template->assign_block_vars('text', array_change_key_case($profile_row, CASE_UPPER)); } -- cgit v1.2.1 From 7fd5f16fa2aeeaddfe21062ffe3c4052ec5ec6c1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 12:16:04 +0100 Subject: [ticket/11201] Use a service collection for the types instead of the container PHPBB3-11201 --- phpBB/config/profilefields.yml | 4 ++-- phpBB/phpbb/profilefields/profilefields.php | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 95242fe10c..efc41707df 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -4,11 +4,11 @@ services: arguments: - @auth - @dbal.conn - - @service_container - #- @profilefields.type_collection - @request - @template - @user + calls: + - [set_type_collection, [@profilefields.type_collection]] profilefields.lang_helper: class: \phpbb\profilefields\lang_helper diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 0ed63223f5..38b8600b7b 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -21,16 +21,29 @@ class profilefields /** * */ - public function __construct($auth, $db, /** @todo: */ $phpbb_container, $request, $template, $user) + public function __construct($auth, $db, $request, $template, $user) { $this->auth = $auth; $this->db = $db; - $this->container = $phpbb_container; $this->request = $request; $this->template = $template; $this->user = $user; } + /** + * Setter for the type collection + * + * We need to set the type collection later, + * in order to avoid a circular dependency + * + * @param \phpbb\di\service_collection $type_collection + * @return null + */ + public function set_type_collection(\phpbb\di\service_collection $type_collection) + { + $this->type_collection = $type_collection; + } + /** * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) * Called by ucp_profile and ucp_register @@ -69,9 +82,10 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { + // Return templated field $tpl_snippet = $this->process_field_row('change', $row); - $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; $this->template->assign_block_vars('profile_fields', array( 'LANG_NAME' => $row['lang_name'], @@ -146,7 +160,7 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); $check_value = $cp_data['pf_' . $row['field_ident']]; @@ -286,7 +300,7 @@ class profilefields foreach ($profile_row as $ident => $ident_ary) { - $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']); if ($value === NULL) @@ -341,7 +355,7 @@ class profilefields } // Assign template variables - $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$profile_row['field_type']]); + $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$profile_row['field_type']]]; $profile_field->generate_field($profile_row, $preview_options); // Return templated data @@ -368,7 +382,7 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - $profile_field = $this->container->get('profilefields.type.' . $this->profile_types[$row['field_type']]); + $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row); } $this->db->sql_freeresult($result); -- cgit v1.2.1 From 1dbc2d621861249a40633012fe33af02c097e69b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 12:36:54 +0100 Subject: [ticket/11201] Fix dropdown tests The error message is now already the language string including the fieldname and not the language key anymore. PHPBB3-11201 --- phpBB/phpbb/profilefields/lang_helper.php | 10 ++--- tests/profile/custom_test.php | 68 +++++++++++++++++++++++++++++++ tests/profile/custom_test~.php | 55 ------------------------- 3 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 tests/profile/custom_test.php delete mode 100644 tests/profile/custom_test~.php diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php index ddf77f5e42..627c76e76e 100644 --- a/phpBB/phpbb/profilefields/lang_helper.php +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -79,16 +79,16 @@ class lang_helper */ public function is_set($field_id, $lang_id = null, $field_value = null) { - $is_set = isset($this->lang_helper->options_lang[$field_id]); + $is_set = isset($this->options_lang[$field_id]); if ($is_set && (!is_null($lang_id) || !is_null($field_value))) { - $is_set = isset($this->lang_helper->options_lang[$field_id][$lang_id]); + $is_set = isset($this->options_lang[$field_id][$lang_id]); } if ($is_set && !is_null($field_value)) { - $is_set = isset($this->lang_helper->options_lang[$field_id][$lang_id][$field_value]); + $is_set = isset($this->options_lang[$field_id][$lang_id][$field_value]); } return $is_set; @@ -106,9 +106,9 @@ class lang_helper { if (!is_null($field_value)) { - return $this->lang_helper->options_lang[$field_id][$lang_id]; + return $this->options_lang[$field_id][$lang_id]; } - return $this->lang_helper->options_lang[$field_id][$lang_id][$field_value]; + return $this->options_lang[$field_id][$lang_id][$field_value]; } } diff --git a/tests/profile/custom_test.php b/tests/profile/custom_test.php new file mode 100644 index 0000000000..cfc350c162 --- /dev/null +++ b/tests/profile/custom_test.php @@ -0,0 +1,68 @@ +createXMLDataSet(dirname(__FILE__) . '/fixtures/profile_fields.xml'); + } + + static public function dropdown_fields() + { + return array( + // note, there is an offset of 1 between option_id (0-indexed) + // in the database and values (1-indexed) to avoid problems with + // transmitting 0 in an HTML form + // required, value, expected + array(1, '0', 'FIELD_INVALID_VALUE-field', 'Required field should throw error for out-of-range value'), + array(1, '1', 'FIELD_REQUIRED-field', 'Required field should throw error for default value'), + array(1, '2', false, 'Required field should accept non-default value'), + array(0, '0', 'FIELD_INVALID_VALUE-field', 'Optional field should throw error for out-of-range value'), + array(0, '1', false, 'Optional field should accept default value'), + array(0, '2', false, 'Optional field should accept non-default value'), + ); + } + + /** + * @dataProvider dropdown_fields + */ + public function test_dropdown_validate($field_required, $field_value, $expected, $description) + { + global $db; + $db = $this->new_dbal(); + + $field_data = array( + 'field_id' => 1, + 'lang_id' => 1, + 'lang_name' => 'field', + 'field_novalue' => 1, + 'field_required' => $field_required, + ); + $user = $this->getMock('\phpbb\user'); + $user->expects($this->any()) + ->method('lang') + ->will($this->returnCallback(array($this, 'return_callback_implode'))); + + $cp = new \phpbb\profilefields\type\type_dropdown( + new \phpbb\profilefields\lang_helper($db), + $this->getMock('\phpbb\request\request'), + $this->getMock('\phpbb\template\template'), + $user + ); + $result = $cp->validate_profile_field($field_value, $field_data); + + $this->assertEquals($expected, $result, $description); + } + + public function return_callback_implode() + { + return implode('-', func_get_args()); + } +} diff --git a/tests/profile/custom_test~.php b/tests/profile/custom_test~.php deleted file mode 100644 index 1f33b45ba9..0000000000 --- a/tests/profile/custom_test~.php +++ /dev/null @@ -1,55 +0,0 @@ -createXMLDataSet(dirname(__FILE__).'/fixtures/profile_fields.xml'); - } - - static public function dropdownFields() - { - return array( - // note, there is an offset of 1 between option_id (0-indexed) - // in the database and values (1-indexed) to avoid problems with - // transmitting 0 in an HTML form - // required, value, expected - array(1, '0', 'FIELD_INVALID_VALUE', 'Required field should throw error for out-of-range value'), - array(1, '1', 'FIELD_REQUIRED', 'Required field should throw error for default value'), - array(1, '2', false, 'Required field should accept non-default value'), - array(0, '0', 'FIELD_INVALID_VALUE', 'Optional field should throw error for out-of-range value'), - array(0, '1', false, 'Optional field should accept default value'), - array(0, '2', false, 'Optional field should accept non-default value'), - ); - } - - /** - * @dataProvider dropdownFields - */ - public function test_dropdown_validate($field_required, $field_value, $expected, $description) - { - global $db; - $db = $this->new_dbal(); - - $field_data = array( - 'field_id' => 1, - 'lang_id' => 1, - 'field_novalue' => 1, - 'field_required' => $field_required, - ); - - $cp = new custom_profile; - $result = $cp->validate_profile_field(FIELD_DROPDOWN, $field_value, $field_data); - - $this->assertEquals($expected, $result, $description); - } -} -- cgit v1.2.1 From 8e86e56120279a0522b2b05e627edde1b8a11d80 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 15:34:17 +0100 Subject: [ticket/11201] Add database column type to type class PHPBB3-11201 --- phpBB/config/profilefields.yml | 2 ++ phpBB/phpbb/profilefields/profilefields.php | 2 +- phpBB/phpbb/profilefields/type/type_bool.php | 11 ++++++++++- phpBB/phpbb/profilefields/type/type_date.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 11 ++++++++++- phpBB/phpbb/profilefields/type/type_int.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 7 +++++++ phpBB/phpbb/profilefields/type/type_string.php | 9 ++++++++- phpBB/phpbb/profilefields/type/type_text.php | 8 ++++++++ 9 files changed, 62 insertions(+), 4 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index efc41707df..5aa30ac87a 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -26,6 +26,7 @@ services: class: \phpbb\profilefields\type\type_bool arguments: - @profilefields.lang_helper + - @profilefields - @request - @template - @user @@ -46,6 +47,7 @@ services: class: \phpbb\profilefields\type\type_dropdown arguments: - @profilefields.lang_helper + - @profilefields - @request - @template - @user diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 38b8600b7b..a580ca2937 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -339,7 +339,7 @@ class profilefields * Return Templated value/field. Possible values for $mode are: * change == user is able to set/enter profile values; preview == just show the value */ - protected function process_field_row($mode, $profile_row) + public function process_field_row($mode, $profile_row) { $preview_options = ($mode == 'preview') ? $this->vars['lang_options'] : false; diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f32f4a7513..6abac8c7bf 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -14,9 +14,10 @@ class type_bool implements type_interface /** * */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; + $this->profilefields = $profilefields; $this->request = $request; $this->template = $template; $this->user = $user; @@ -184,4 +185,12 @@ class type_bool implements type_interface { return ($field_data['field_length'] == '1') ? '' : 'pf_' . $field_data['field_ident']; } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'TINT:2'; + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index fda09c05e2..1fcbf2ea3e 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -232,4 +232,12 @@ class type_date implements type_interface { return ''; } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'VCHAR:10'; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index a3217f004c..e2b0dc646c 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -14,9 +14,10 @@ class type_dropdown implements type_interface /** * */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; + $this->profilefields = $profilefields; $this->request = $request; $this->template = $template; $this->user = $user; @@ -182,4 +183,12 @@ class type_dropdown implements type_interface { return 'pf_' . $field_data['field_ident']; } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'UINT'; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 13e39039c3..202005008f 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -158,4 +158,12 @@ class type_int implements type_interface { return 'pf_' . $field_data['field_ident']; } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'BINT'; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 5b662205b3..917b95f653 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -90,4 +90,11 @@ interface type_interface * @return string ident of the field */ public function get_field_ident($field_data); + + /** + * Get the column type for the database + * + * @return string Returns the database column type + */ + public function get_database_column_type(); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 57edd14d62..a9d9fddfb0 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -78,7 +78,14 @@ class type_string extends type_string_common implements type_interface $default_value = $profile_row['lang_default_value']; $profile_row['field_value'] = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value, true) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); - $this->template->assign_block_vars('string', array_change_key_case($profile_row, CASE_UPPER)); } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'VCHAR'; + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index da1bb32859..58cc2d2741 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -84,4 +84,12 @@ class type_text extends type_string_common implements type_interface $this->template->assign_block_vars('text', array_change_key_case($profile_row, CASE_UPPER)); } + + /** + * {@inheritDoc} + */ + public function get_database_column_type() + { + return 'MTEXT'; + } } -- cgit v1.2.1 From 7d2d18fb50c5c071098de594ec85897437e57007 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 16:37:21 +0100 Subject: [ticket/11201] Move "get type" code into a new function PHPBB3-11201 --- phpBB/phpbb/db/tools.php | 106 +++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 46 deletions(-) diff --git a/phpBB/phpbb/db/tools.php b/phpBB/phpbb/db/tools.php index 4360c89ac3..65098b643b 100644 --- a/phpBB/phpbb/db/tools.php +++ b/phpBB/phpbb/db/tools.php @@ -1474,52 +1474,7 @@ class tools } // Get type - if (strpos($column_data[0], ':') !== false) - { - list($orig_column_type, $column_length) = explode(':', $column_data[0]); - if (!is_array($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'])) - { - $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'], $column_length); - } - else - { - if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'])) - { - switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][0]) - { - case 'div': - $column_length /= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][1]; - $column_length = ceil($column_length); - $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length); - break; - } - } - - if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'])) - { - switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][0]) - { - case 'mult': - $column_length *= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][1]; - if ($column_length > $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][2]) - { - $column_type = $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][3]; - } - else - { - $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length); - } - break; - } - } - } - $orig_column_type .= ':'; - } - else - { - $orig_column_type = $column_data[0]; - $column_type = $this->dbms_type_map[$this->sql_layer][$column_data[0]]; - } + list($column_type, $orig_column_type) = $this->get_column_type($column_data[0]); // Adjust default value if db-dependent specified if (is_array($column_data[1])) @@ -1694,6 +1649,65 @@ class tools return $return_array; } + /** + * Get the column's database type from the type map + * + * @param string $column_map_type + * @return array column type for this database + * and map type without length + */ + function get_column_type($column_map_type) + { + if (strpos($column_map_type, ':') !== false) + { + list($orig_column_type, $column_length) = explode(':', $column_map_type); + if (!is_array($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'])) + { + $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'], $column_length); + } + else + { + if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'])) + { + switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][0]) + { + case 'div': + $column_length /= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['rule'][1]; + $column_length = ceil($column_length); + $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length); + break; + } + } + + if (isset($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'])) + { + switch ($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][0]) + { + case 'mult': + $column_length *= $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][1]; + if ($column_length > $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][2]) + { + $column_type = $this->dbms_type_map[$this->sql_layer][$orig_column_type . ':']['limit'][3]; + } + else + { + $column_type = sprintf($this->dbms_type_map[$this->sql_layer][$orig_column_type . ':'][0], $column_length); + } + break; + } + } + } + $orig_column_type .= ':'; + } + else + { + $orig_column_type = $column_map_type; + $column_type = $this->dbms_type_map[$this->sql_layer][$column_map_type]; + } + + return array($column_type, $orig_column_type); + } + /** * Add new column */ -- cgit v1.2.1 From bd1425d07561345b7f92977f81bb1fb9e9cda927 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 16:39:06 +0100 Subject: [ticket/11201] Use database type from the type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 206 +++---------------------------------- 1 file changed, 16 insertions(+), 190 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index b3239af1ab..2d4097d526 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -1062,7 +1062,7 @@ class acp_profile */ function save_profile_field(&$cp, $field_type, $action = 'create') { - global $db, $config, $user; + global $db, $config, $user, $phpbb_container; $field_id = request_var('field_id', 0); @@ -1126,7 +1126,13 @@ class acp_profile if ($action == 'create') { $field_ident = 'pf_' . $field_ident; - $profile_sql[] = $this->add_field_ident($field_ident, $field_type); + + $db_tools = $phpbb_container->get('dbal.tools'); + $type_collection = $phpbb_container->get('profilefields.type_collection'); + $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; + + list($sql_type, $null) = $db_tools->get_column_type($profile_type->get_database_column_type()); + $profile_sql[] = $this->add_field_ident($field_ident, $sql_type); } $sql_ary = array( @@ -1401,7 +1407,7 @@ class acp_profile /** * Return sql statement for adding a new field ident (profile field) to the profile fields data table */ - function add_field_ident($field_ident, $field_type) + function add_field_ident($field_ident, $sql_type) { global $db; @@ -1410,73 +1416,11 @@ class acp_profile case 'mysql': case 'mysql4': case 'mysqli': - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. - $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD `$field_ident` "; - - switch ($field_type) - { - case FIELD_STRING: - $sql .= ' VARCHAR(255) '; - break; - - case FIELD_DATE: - $sql .= 'VARCHAR(10) '; - break; - - case FIELD_TEXT: - $sql .= "TEXT"; - // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED"; - break; - - case FIELD_BOOL: - $sql .= 'TINYINT(2) '; - break; - - case FIELD_DROPDOWN: - $sql .= 'MEDIUMINT(8) '; - break; - - case FIELD_INT: - $sql .= 'BIGINT(20) '; - break; - } + $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD `$field_ident` " . $sql_type; break; case 'sqlite': - - switch ($field_type) - { - case FIELD_STRING: - $type = ' VARCHAR(255) '; - break; - - case FIELD_DATE: - $type = 'VARCHAR(10) '; - break; - - case FIELD_TEXT: - $type = "TEXT(65535)"; - // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED"; - break; - - case FIELD_BOOL: - $type = 'TINYINT(2) '; - break; - - case FIELD_DROPDOWN: - $type = 'MEDIUMINT(8) '; - break; - - case FIELD_INT: - $type = 'BIGINT(20) '; - break; - } - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. if (version_compare(sqlite_libversion(), '3.0') == -1) { $sql = "SELECT sql @@ -1511,7 +1455,7 @@ class acp_profile $columns = implode(',', $column_list); - $new_table_cols = $field_ident . ' ' . $type . ',' . $new_table_cols; + $new_table_cols = $field_ident . ' ' . $sql_type . ',' . $new_table_cols; // create a new table and fill it up. destroy the temp one $db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');'); @@ -1520,7 +1464,7 @@ class acp_profile } else { - $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident [$type]"; + $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident [$sql_type]"; } break; @@ -1528,140 +1472,22 @@ class acp_profile case 'mssql': case 'mssql_odbc': case 'mssqlnative': - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. - $sql = 'ALTER TABLE [' . PROFILE_FIELDS_DATA_TABLE . "] ADD [$field_ident] "; - - switch ($field_type) - { - case FIELD_STRING: - $sql .= ' [VARCHAR] (255) '; - break; - - case FIELD_DATE: - $sql .= '[VARCHAR] (10) '; - break; - - case FIELD_TEXT: - $sql .= "[TEXT]"; - // ADD {$field_ident}_bbcode_uid [VARCHAR] (5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield [INT] UNSIGNED"; - break; - - case FIELD_BOOL: - case FIELD_DROPDOWN: - $sql .= '[INT] '; - break; - - case FIELD_INT: - $sql .= '[FLOAT] '; - break; - } + $sql = 'ALTER TABLE [' . PROFILE_FIELDS_DATA_TABLE . "] ADD [$field_ident] " . $sql_type; break; case 'postgres': - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. - $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD COLUMN \"$field_ident\" "; - - switch ($field_type) - { - case FIELD_STRING: - $sql .= ' VARCHAR(255) '; - break; - - case FIELD_DATE: - $sql .= 'VARCHAR(10) '; - break; - - case FIELD_TEXT: - $sql .= "TEXT"; - // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield INT4 UNSIGNED"; - break; - - case FIELD_BOOL: - $sql .= 'INT2 '; - break; - - case FIELD_DROPDOWN: - $sql .= 'INT4 '; - break; - - case FIELD_INT: - $sql .= 'INT8 '; - break; - } + $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD COLUMN \"$field_ident\" " . $sql_type; break; case 'firebird': - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. - $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' ADD "' . strtoupper($field_ident) . '" '; - - switch ($field_type) - { - case FIELD_STRING: - $sql .= ' VARCHAR(255) '; - break; - - case FIELD_DATE: - $sql .= 'VARCHAR(10) '; - break; - - case FIELD_TEXT: - $sql .= "BLOB SUB_TYPE TEXT"; - // ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield INTEGER UNSIGNED"; - break; - - case FIELD_BOOL: - case FIELD_DROPDOWN: - $sql .= 'INTEGER '; - break; - - case FIELD_INT: - $sql .= 'DOUBLE PRECISION '; - break; - } + $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' ADD "' . strtoupper($field_ident) . '" ' . $sql_type; break; case 'oracle': - - // We are defining the biggest common value, because of the possibility to edit the min/max values of each field. - $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident "; - - switch ($field_type) - { - case FIELD_STRING: - $sql .= ' VARCHAR2(255) '; - break; - - case FIELD_DATE: - $sql .= 'VARCHAR2(10) '; - break; - - case FIELD_TEXT: - $sql .= "CLOB"; - // ADD {$field_ident}_bbcode_uid VARCHAR2(5) NOT NULL, - // ADD {$field_ident}_bbcode_bitfield NUMBER(11) UNSIGNED"; - break; - - case FIELD_BOOL: - $sql .= 'NUMBER(2) '; - break; - - case FIELD_DROPDOWN: - $sql .= 'NUMBER(8) '; - break; - - case FIELD_INT: - $sql .= 'NUMBER(20) '; - break; - } + $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident " . $sql_type; break; } -- cgit v1.2.1 From 78603bb96d0afa0695b296013dec17485d74ea45 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Jan 2014 18:25:28 +0100 Subject: [ticket/11201] Move language option determination into type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 30 +++-------------------- phpBB/phpbb/profilefields/type/type_bool.php | 18 ++++++++++++++ phpBB/phpbb/profilefields/type/type_date.php | 17 +++++++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 18 ++++++++++++++ phpBB/phpbb/profilefields/type/type_int.php | 17 +++++++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 8 ++++++ phpBB/phpbb/profilefields/type/type_string.php | 22 +++++++++++++++++ phpBB/phpbb/profilefields/type/type_text.php | 22 +++++++++++++++++ 8 files changed, 126 insertions(+), 26 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 2d4097d526..6ee051e37b 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -942,7 +942,7 @@ class acp_profile */ function build_language_options(&$cp, $field_type, $action = 'create') { - global $user, $config, $db; + global $user, $config, $db, $phpbb_container; $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']]; @@ -959,31 +959,9 @@ class acp_profile } $db->sql_freeresult($result); - $options = array(); - $options['lang_name'] = 'string'; - if ($cp->vars['lang_explain']) - { - $options['lang_explain'] = 'text'; - } - - switch ($field_type) - { - case FIELD_BOOL: - $options['lang_options'] = 'two_options'; - break; - - case FIELD_DROPDOWN: - $options['lang_options'] = 'optionfield'; - break; - - case FIELD_TEXT: - case FIELD_STRING: - if (strlen($cp->vars['lang_default_value'])) - { - $options['lang_default_value'] = ($field_type == FIELD_STRING) ? 'string' : 'text'; - } - break; - } + $type_collection = $phpbb_container->get('profilefields.type_collection'); + $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; + $options = $profile_type->get_language_options($cp->vars); $lang_options = array(); diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 6abac8c7bf..f4e0ac0f86 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -193,4 +193,22 @@ class type_bool implements type_interface { return 'TINT:2'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + 'lang_options' => 'two_options', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + return $options; + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 1fcbf2ea3e..d2a10cdd15 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -240,4 +240,21 @@ class type_date implements type_interface { return 'VCHAR:10'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + return $options; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index e2b0dc646c..82c128861a 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -191,4 +191,22 @@ class type_dropdown implements type_interface { return 'UINT'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + 'lang_options' => 'optionfield', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + return $options; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 202005008f..a2e6f8e663 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -166,4 +166,21 @@ class type_int implements type_interface { return 'BINT'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + return $options; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 917b95f653..f6f6355698 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -97,4 +97,12 @@ interface type_interface * @return string Returns the database column type */ public function get_database_column_type(); + + /** + * Get the options we need to display for the language input fields in the ACP + * + * @param array $field_data Array with data for this field + * @return array Returns the language options we need to generate + */ + public function get_language_options($field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index a9d9fddfb0..ee2eb8b760 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -88,4 +88,26 @@ class type_string extends type_string_common implements type_interface { return 'VCHAR'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + if (strlen($field_data['lang_default_value'])) + { + $options['lang_default_value'] = 'string'; + } + + return $options; + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 58cc2d2741..7aea982f6f 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -92,4 +92,26 @@ class type_text extends type_string_common implements type_interface { return 'MTEXT'; } + + /** + * {@inheritDoc} + */ + public function get_language_options($field_data) + { + $options = array( + 'lang_name' => 'string', + ); + + if ($field_data['lang_explain']) + { + $options['lang_explain'] = 'text'; + } + + if (strlen($field_data['lang_default_value'])) + { + $options['lang_default_value'] = 'text'; + } + + return $options; + } } -- cgit v1.2.1 From 0bdec35cd4127cd4f1e96b9a77340580c6045423 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 16 Jan 2014 16:00:52 +0100 Subject: [ticket/11201] Move grabbing the input of the language options to type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 25 +++++----------------- phpBB/phpbb/profilefields/lang_helper.php | 10 ++++++++- phpBB/phpbb/profilefields/type/type_bool.php | 20 ++++++++++++++++- phpBB/phpbb/profilefields/type/type_date.php | 18 ++++++++++++++-- phpBB/phpbb/profilefields/type/type_dropdown.php | 15 ++++++++++++- phpBB/phpbb/profilefields/type/type_int.php | 15 ++++++++++++- phpBB/phpbb/profilefields/type/type_interface.php | 10 ++++++++- .../profilefields/type/type_string_common.php | 13 +++++++++++ 8 files changed, 99 insertions(+), 27 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 6ee051e37b..63f5a0e1f5 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -840,7 +840,7 @@ class acp_profile // Build options based on profile type $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); - $options = $profile_field->get_options($this->lang_defs, $cp->vars); + $options = $profile_field->get_options($this->lang_defs['iso'][$config['default_lang']], $cp->vars); foreach ($options as $num => $option_ary) { @@ -1101,13 +1101,14 @@ class acp_profile $db->sql_query($sql); } + $type_collection = $phpbb_container->get('profilefields.type_collection'); + $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; + if ($action == 'create') { $field_ident = 'pf_' . $field_ident; $db_tools = $phpbb_container->get('dbal.tools'); - $type_collection = $phpbb_container->get('profilefields.type_collection'); - $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; list($sql_type, $null) = $db_tools->get_column_type($profile_type->get_database_column_type()); $profile_sql[] = $this->add_field_ident($field_ident, $sql_type); @@ -1164,23 +1165,7 @@ class acp_profile } } - // These are always arrays because the key is the language id... - $cp->vars['l_lang_name'] = utf8_normalize_nfc(request_var('l_lang_name', array(0 => ''), true)); - $cp->vars['l_lang_explain'] = utf8_normalize_nfc(request_var('l_lang_explain', array(0 => ''), true)); - $cp->vars['l_lang_default_value'] = utf8_normalize_nfc(request_var('l_lang_default_value', array(0 => ''), true)); - - if ($field_type != FIELD_BOOL) - { - $cp->vars['l_lang_options'] = utf8_normalize_nfc(request_var('l_lang_options', array(0 => ''), true)); - } - else - { - /** - * @todo check if this line is correct... - $cp->vars['l_lang_default_value'] = request_var('l_lang_default_value', array(0 => array('')), true); - */ - $cp->vars['l_lang_options'] = utf8_normalize_nfc(request_var('l_lang_options', array(0 => array('')), true)); - } + $cp->vars = $profile_type->get_language_options_input($cp->vars); if ($cp->vars['lang_options']) { diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php index 627c76e76e..569eaaca40 100644 --- a/phpBB/phpbb/profilefields/lang_helper.php +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -48,6 +48,14 @@ class lang_helper foreach ($lang_options as $num => $var) { + if (!isset($this->options_lang[$field_id])) + { + $this->options_lang[$field_id] = array(); + } + if (!isset($this->options_lang[$field_id][$lang_id])) + { + $this->options_lang[$field_id][$lang_id] = array(); + } $this->options_lang[$field_id][$lang_id][($num + 1)] = $var; } } @@ -104,7 +112,7 @@ class lang_helper */ public function get($field_id, $lang_id, $field_value = null) { - if (!is_null($field_value)) + if (is_null($field_value)) { return $this->options_lang[$field_id][$lang_id]; } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f4e0ac0f86..a3291d5ce1 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -144,7 +144,7 @@ class type_bool implements type_interface { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; - $default_value = $profile_row['lang_default_value']; + $default_value = $profile_row['field_default_value']; // checkbox - set the value to "true" if it has been set to 1 if ($profile_row['field_length'] == 2) @@ -211,4 +211,22 @@ class type_bool implements type_interface return $options; } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + + /** + * @todo check if this line is correct... + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => array('')), true); + */ + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => array('')), true); + + return $field_data; + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index d2a10cdd15..c76c7e1d49 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -177,6 +177,7 @@ class type_date implements type_interface public function generate_field($profile_row, $preview_options = false) { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; + $field_ident = $profile_row['field_ident']; $now = getdate(); @@ -186,14 +187,14 @@ class type_date implements type_interface { $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); } - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$field_ident])); } else { if ($preview_options !== false && $profile_row['field_default_value'] == 'now') { $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); - list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$user_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$user_ident])); + list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$field_ident])); } else { @@ -257,4 +258,17 @@ class type_date implements type_interface return $options; } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); + + return $field_data; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 82c128861a..4313f0e64c 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -153,7 +153,7 @@ class type_dropdown implements type_interface { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; - $default_value = $profile_row['lang_default_value']; + $default_value = $profile_row['field_default_value']; $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]); @@ -209,4 +209,17 @@ class type_dropdown implements type_interface return $options; } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); + + return $field_data; + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index a2e6f8e663..9901f61795 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -124,7 +124,7 @@ class type_int implements type_interface { $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; $field_ident = $profile_row['field_ident']; - $default_value = $profile_row['lang_default_value']; + $default_value = $profile_row['field_default_value']; if ($this->request->is_set($field_ident)) { @@ -183,4 +183,17 @@ class type_int implements type_interface return $options; } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); + + return $field_data; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index f6f6355698..9390c4171b 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -102,7 +102,15 @@ interface type_interface * Get the options we need to display for the language input fields in the ACP * * @param array $field_data Array with data for this field - * @return array Returns the language options we need to generate + * @return array Returns the language options we need to generate */ public function get_language_options($field_data); + + /** + * Get the input for the supplied language options + * + * @param array $field_data Array with data for this field + * @return array Returns the language options we need to generate + */ + public function get_language_options_input($field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index 686c50d4db..88ca905aee 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -109,4 +109,17 @@ abstract class type_string_common { return 'pf_' . $field_data['field_ident']; } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); + + return $field_data; + } } -- cgit v1.2.1 From 9653764fb15de3e3126466c98525c121a8a3ac0b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 09:55:52 +0100 Subject: [ticket/11201] Move field type depending preparation of the options to class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 35 +++++----------------- phpBB/phpbb/profilefields/type/type_bool.php | 10 +++++++ phpBB/phpbb/profilefields/type/type_date.php | 8 +++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 10 +++++++ phpBB/phpbb/profilefields/type/type_int.php | 8 +++++ phpBB/phpbb/profilefields/type/type_interface.php | 9 ++++++ .../profilefields/type/type_string_common.php | 10 +++++++ 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 63f5a0e1f5..e682996ba8 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -341,6 +341,7 @@ class acp_profile $this->edit_lang_id = $field_row['lang_id']; } $field_type = $field_row['field_type']; + $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); // Get language entries $sql = 'SELECT * @@ -397,23 +398,6 @@ class acp_profile 3 => array('l_lang_name', 'l_lang_explain', 'l_lang_default_value', 'l_lang_options') ); - // Text-based fields require the lang_default_value to be excluded - if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT) - { - $exclude[1][] = 'lang_default_value'; - } - - // option-specific fields require lang_options to be excluded - if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN) - { - $exclude[1][] = 'lang_options'; - } - - $cp->vars['field_ident'] = ($action == 'create' && $step == 1) ? utf8_clean_string(request_var('field_ident', $field_row['field_ident'], true)) : request_var('field_ident', $field_row['field_ident']); - $cp->vars['lang_name'] = utf8_normalize_nfc(request_var('lang_name', $field_row['lang_name'], true)); - $cp->vars['lang_explain'] = utf8_normalize_nfc(request_var('lang_explain', $field_row['lang_explain'], true)); - $cp->vars['lang_default_value'] = utf8_normalize_nfc(request_var('lang_default_value', $field_row['lang_default_value'], true)); - // Visibility Options... $visibility_ary = array( 'field_required', @@ -425,6 +409,13 @@ class acp_profile 'field_hide', ); + $options = $profile_field->prepare_options_form($exclude, $visibility_ary); + + $cp->vars['field_ident'] = ($action == 'create' && $step == 1) ? utf8_clean_string(request_var('field_ident', $field_row['field_ident'], true)) : request_var('field_ident', $field_row['field_ident']); + $cp->vars['lang_name'] = utf8_normalize_nfc(request_var('lang_name', $field_row['lang_name'], true)); + $cp->vars['lang_explain'] = utf8_normalize_nfc(request_var('lang_explain', $field_row['lang_explain'], true)); + $cp->vars['lang_default_value'] = utf8_normalize_nfc(request_var('lang_default_value', $field_row['lang_default_value'], true)); + foreach ($visibility_ary as $val) { $cp->vars[$val] = ($submit || $save) ? request_var($val, 0) : $field_row[$val]; @@ -432,16 +423,6 @@ class acp_profile $cp->vars['field_no_view'] = request_var('field_no_view', (int) $field_row['field_no_view']); - // A boolean field expects an array as the lang options - if ($field_type == FIELD_BOOL) - { - $options = utf8_normalize_nfc(request_var('lang_options', array(''), true)); - } - else - { - $options = utf8_normalize_nfc(request_var('lang_options', '', true)); - } - // If the user has submitted a form with options (i.e. dropdown field) if ($options) { diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index a3291d5ce1..f27c5af1ee 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -229,4 +229,14 @@ class type_bool implements type_interface return $field_data; } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + $exclude_options[1][] = 'lang_options'; + + return $this->request->variable('lang_options', array(''), true); + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index c76c7e1d49..37b8db2779 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -271,4 +271,12 @@ class type_date implements type_interface return $field_data; } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + return $this->request->variable('lang_options', '', true); + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 4313f0e64c..25671c88fa 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -222,4 +222,14 @@ class type_dropdown implements type_interface return $field_data; } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + $exclude_options[1][] = 'lang_options'; + + return $this->request->variable('lang_options', '', true); + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 9901f61795..35f021f0fa 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -196,4 +196,12 @@ class type_int implements type_interface return $field_data; } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + return $this->request->variable('lang_options', '', true); + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 9390c4171b..f8d5c46239 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -113,4 +113,13 @@ interface type_interface * @return array Returns the language options we need to generate */ public function get_language_options_input($field_data); + + /** + * Allows exclusion of options in single steps of the creation process + * + * @param array $exclude_options Array with options that should be excluded in the steps + * @param array $visibility_options Array with options responsible for the fields visibility + * @return mixed Returns the provided language options + */ + public function prepare_options_form(&$exclude_options, &$visibility_options); } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index 88ca905aee..a6145e910b 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -122,4 +122,14 @@ abstract class type_string_common return $field_data; } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + $exclude_options[1][] = 'lang_default_value'; + + return $this->request->variable('lang_options', '', true); + } } -- cgit v1.2.1 From 5e2ffe0d5cc4b7280b0bf51202cdbde4fca1e03b Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 10:12:30 +0100 Subject: [ticket/11201] Add abstract base class with shared methods PHPBB3-11201 --- phpBB/phpbb/profilefields/type/type_base.php | 52 ++++++++++++++++++++++ phpBB/phpbb/profilefields/type/type_bool.php | 2 +- phpBB/phpbb/profilefields/type/type_date.php | 23 +--------- phpBB/phpbb/profilefields/type/type_dropdown.php | 23 +--------- phpBB/phpbb/profilefields/type/type_int.php | 23 +--------- phpBB/phpbb/profilefields/type/type_string.php | 2 +- .../profilefields/type/type_string_common.php | 23 +--------- phpBB/phpbb/profilefields/type/type_text.php | 2 +- 8 files changed, 59 insertions(+), 91 deletions(-) create mode 100644 phpBB/phpbb/profilefields/type/type_base.php diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php new file mode 100644 index 0000000000..00e8d74327 --- /dev/null +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -0,0 +1,52 @@ +request = $request; + $this->template = $template; + $this->user = $user; + } + + /** + * {@inheritDoc} + */ + public function get_field_ident($field_data) + { + return 'pf_' . $field_data['field_ident']; + } + + /** + * {@inheritDoc} + */ + public function get_language_options_input($field_data) + { + $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); + $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); + + return $field_data; + } + + /** + * {@inheritDoc} + */ + public function prepare_options_form(&$exclude_options, &$visibility_options) + { + return $this->request->variable('lang_options', '', true); + } +} diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f27c5af1ee..4cfd147ea2 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_bool implements type_interface +class type_bool extends type_base { /** * diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 37b8db2779..a2c9111351 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_date implements type_interface +class type_date extends type_base { /** * @@ -258,25 +258,4 @@ class type_date implements type_interface return $options; } - - /** - * {@inheritDoc} - */ - public function get_language_options_input($field_data) - { - $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); - $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); - $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); - $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); - - return $field_data; - } - - /** - * {@inheritDoc} - */ - public function prepare_options_form(&$exclude_options, &$visibility_options) - { - return $this->request->variable('lang_options', '', true); - } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 25671c88fa..2119443a7a 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_dropdown implements type_interface +class type_dropdown extends type_base { /** * @@ -176,14 +176,6 @@ class type_dropdown implements type_interface } } - /** - * {@inheritDoc} - */ - public function get_field_ident($field_data) - { - return 'pf_' . $field_data['field_ident']; - } - /** * {@inheritDoc} */ @@ -210,19 +202,6 @@ class type_dropdown implements type_interface return $options; } - /** - * {@inheritDoc} - */ - public function get_language_options_input($field_data) - { - $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); - $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); - $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); - $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); - - return $field_data; - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 35f021f0fa..2cd9d3b2c4 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_int implements type_interface +class type_int extends type_base { /** * @@ -183,25 +183,4 @@ class type_int implements type_interface return $options; } - - /** - * {@inheritDoc} - */ - public function get_language_options_input($field_data) - { - $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); - $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); - $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); - $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); - - return $field_data; - } - - /** - * {@inheritDoc} - */ - public function prepare_options_form(&$exclude_options, &$visibility_options) - { - return $this->request->variable('lang_options', '', true); - } } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index ee2eb8b760..66157ddc20 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_string extends type_string_common implements type_interface +class type_string extends type_string_common { /** * diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index a6145e910b..d322099c34 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -abstract class type_string_common +abstract class type_string_common extends type_base { /** * Return possible validation options @@ -102,27 +102,6 @@ abstract class type_string_common return $field_value; } - /** - * {@inheritDoc} - */ - public function get_field_ident($field_data) - { - return 'pf_' . $field_data['field_ident']; - } - - /** - * {@inheritDoc} - */ - public function get_language_options_input($field_data) - { - $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); - $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); - $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); - $field_data['l_lang_options'] = $this->request->variable('l_lang_options', array(0 => ''), true); - - return $field_data; - } - /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 7aea982f6f..309ed88818 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -9,7 +9,7 @@ namespace phpbb\profilefields\type; -class type_text extends type_string_common implements type_interface +class type_text extends type_string_common { /** * -- cgit v1.2.1 From ae38cfaa701458aaebbc0991512d8788db07f9cb Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 10:20:00 +0100 Subject: [ticket/11201] Move type specific error messages to type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 10 +--------- phpBB/phpbb/profilefields/type/type_base.php | 8 ++++++++ phpBB/phpbb/profilefields/type/type_bool.php | 13 +++++++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 13 +++++++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 9 +++++++++ 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index e682996ba8..39d1c70990 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -623,15 +623,7 @@ class acp_profile $error[] = $user->lang['EMPTY_USER_FIELD_NAME']; } - if ($field_type == FIELD_DROPDOWN && !sizeof($cp->vars['lang_options'])) - { - $error[] = $user->lang['NO_FIELD_ENTRIES']; - } - - if ($field_type == FIELD_BOOL && (empty($cp->vars['lang_options'][0]) || empty($cp->vars['lang_options'][1]))) - { - $error[] = $user->lang['NO_FIELD_ENTRIES']; - } + $error = $profile_field->validate_options_on_submit($error, $cp->vars); // Check for already existing field ident if ($action != 'edit') diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 00e8d74327..976411baee 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -49,4 +49,12 @@ abstract class type_base implements type_interface { return $this->request->variable('lang_options', '', true); } + + /** + * {@inheritDoc} + */ + public function validate_options_on_submit($error, $field_data) + { + return $error; + } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 4cfd147ea2..24319c2bfe 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -239,4 +239,17 @@ class type_bool extends type_base return $this->request->variable('lang_options', array(''), true); } + + /** + * {@inheritDoc} + */ + public function validate_options_on_submit($error, $field_data) + { + if (empty($field_data['lang_options'][0]) || empty($field_data['lang_options'][1])) + { + $error[] = $this->user->lang['NO_FIELD_ENTRIES']; + } + + return $error; + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 2119443a7a..a113b1d5ef 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -211,4 +211,17 @@ class type_dropdown extends type_base return $this->request->variable('lang_options', '', true); } + + /** + * {@inheritDoc} + */ + public function validate_options_on_submit($error, $field_data) + { + if (!sizeof($field_data['lang_options'])) + { + $error[] = $this->user->lang['NO_FIELD_ENTRIES']; + } + + return $error; + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index f8d5c46239..ad67536543 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -122,4 +122,13 @@ interface type_interface * @return mixed Returns the provided language options */ public function prepare_options_form(&$exclude_options, &$visibility_options); + + /** + * Allows exclusion of options in single steps of the creation process + * + * @param array $error Array with error messages + * @param array $field_data Array with data for this field + * @return array Array with error messages + */ + public function validate_options_on_submit($error, $field_data); } -- cgit v1.2.1 From 67f477fc8f184e5b2585bda914e2fbbabf4afcd8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 13:35:38 +0100 Subject: [ticket/11201] Allow manipulating the intended variables with the type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 105 ++-------------------- phpBB/phpbb/profilefields/type/type_base.php | 18 ++++ phpBB/phpbb/profilefields/type/type_bool.php | 44 +++++++++ phpBB/phpbb/profilefields/type/type_date.php | 42 +++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 15 ++++ phpBB/phpbb/profilefields/type/type_int.php | 17 ++++ phpBB/phpbb/profilefields/type/type_interface.php | 12 +++ phpBB/phpbb/profilefields/type/type_text.php | 26 ++++++ 8 files changed, 180 insertions(+), 99 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 39d1c70990..a1b3c46146 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -450,93 +450,9 @@ class acp_profile { $var = utf8_normalize_nfc(request_var($key, $field_row[$key], true)); - // Manipulate the intended variables a little bit if needed - if ($field_type == FIELD_DROPDOWN && $key == 'field_maxlen') - { - // Get the number of options if this key is 'field_maxlen' - $var = sizeof(explode("\n", utf8_normalize_nfc(request_var('lang_options', '', true)))); - } - else if ($field_type == FIELD_TEXT && $key == 'field_length') - { - if (isset($_REQUEST['rows'])) - { - $cp->vars['rows'] = request_var('rows', 0); - $cp->vars['columns'] = request_var('columns', 0); - $var = $cp->vars['rows'] . '|' . $cp->vars['columns']; - } - else - { - $row_col = explode('|', $var); - $cp->vars['rows'] = $row_col[0]; - $cp->vars['columns'] = $row_col[1]; - } - } - else if ($field_type == FIELD_DATE && $key == 'field_default_value') - { - $always_now = request_var('always_now', -1); - - if ($always_now == 1 || ($always_now === -1 && $var == 'now')) - { - $now = getdate(); - - $cp->vars['field_default_value_day'] = $now['mday']; - $cp->vars['field_default_value_month'] = $now['mon']; - $cp->vars['field_default_value_year'] = $now['year']; - $var = 'now'; - $request->overwrite('field_default_value', $var, \phpbb\request\request_interface::POST); - } - else - { - if (isset($_REQUEST['field_default_value_day'])) - { - $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0); - $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0); - $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0); - $var = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']); - $request->overwrite('field_default_value', $var, \phpbb\request\request_interface::POST); - } - else - { - list($cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']) = explode('-', $var); - } - } - } - else if ($field_type == FIELD_BOOL && $key == 'field_default_value') - { - // 'field_length' == 1 defines radio buttons. Possible values are 1 or 2 only. - // 'field_length' == 2 defines checkbox. Possible values are 0 or 1 only. - // If we switch the type on step 2, we have to adjust field value. - // 1 is a common value for the checkbox and radio buttons. - - // Adjust unchecked checkbox value. - // If we return or save settings from 2nd/3rd page - // and the checkbox is unchecked, set the value to 0. - if (isset($_REQUEST['step']) && !isset($_REQUEST[$key])) - { - $var = 0; - } - - // If we switch to the checkbox type but former radio buttons value was 2, - // which is not the case for the checkbox, set it to 0 (unchecked). - if ($cp->vars['field_length'] == 2 && $var == 2) - { - $var = 0; - } - // If we switch to the radio buttons but the former checkbox value was 0, - // which is not the case for the radio buttons, set it to 0. - else if ($cp->vars['field_length'] == 1 && $var == 0) - { - $var = 2; - } - } - else if ($field_type == FIELD_INT && $key == 'field_default_value') - { - // Permit an empty string - if ($action == 'create' && request_var('field_default_value', '') === '') - { - $var = ''; - } - } + $field_data = $cp->vars; + $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 2); + $cp->vars = $field_data; $cp->vars[$key] = $var; } @@ -585,18 +501,10 @@ class acp_profile { $cp->vars[$key] = $$key; } - else if ($key == 'l_lang_options' && $field_type == FIELD_BOOL) - { - $cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => array('')), true)); - } - else if ($key == 'l_lang_options' && is_array($cp->vars[$key])) - { - foreach ($cp->vars[$key] as $lang_id => $options) - { - $cp->vars[$key][$lang_id] = explode("\n", $options); - } - } + $field_data = $cp->vars; + $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 3); + $cp->vars = $field_data; } // Check for general issues in every step @@ -812,7 +720,6 @@ class acp_profile ); // Build options based on profile type - $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); $options = $profile_field->get_options($this->lang_defs['iso'][$config['default_lang']], $cp->vars); foreach ($options as $num => $option_ary) diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 976411baee..cd03af57f7 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -57,4 +57,22 @@ abstract class type_base implements type_interface { return $error; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 3 && ($field_data[$key] || $action != 'edit') && $key == 'l_lang_options' && is_array($field_data[$key])) + { + foreach ($field_data[$key] as $lang_id => $options) + { + $field_data[$key][$lang_id] = explode("\n", $options); + } + + return $current_value; + } + + return $current_value; + } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 24319c2bfe..acf20e93e0 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -252,4 +252,48 @@ class type_bool extends type_base return $error; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 2 && $key == 'field_default_value') + { + // 'field_length' == 1 defines radio buttons. Possible values are 1 or 2 only. + // 'field_length' == 2 defines checkbox. Possible values are 0 or 1 only. + // If we switch the type on step 2, we have to adjust field value. + // 1 is a common value for the checkbox and radio buttons. + + // Adjust unchecked checkbox value. + // If we return or save settings from 2nd/3rd page + // and the checkbox is unchecked, set the value to 0. + if ($this->request->is_set('step') && !$this->request->is_set($key)) + { + return 0; + } + + // If we switch to the checkbox type but former radio buttons value was 2, + // which is not the case for the checkbox, set it to 0 (unchecked). + if ($field_data['field_length'] == 2 && $current_value == 2) + { + return 0; + } + // If we switch to the radio buttons but the former checkbox value was 0, + // which is not the case for the radio buttons, set it to 0. + else if ($field_data['field_length'] == 1 && $current_value == 0) + { + return 2; + } + } + + if ($step == 3 && ($field_data[$key] || $action != 'edit') && $key == 'l_lang_options') + { + $field_data[$key] = $this->request->variable($key, array(0 => array('')), true); + + return $current_value; + } + + return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index a2c9111351..099d1dbb50 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -258,4 +258,46 @@ class type_date extends type_base return $options; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 2 && $key == 'field_default_value') + { + $always_now = $this->request->variable('always_now', -1); + + if ($always_now == 1 || ($always_now === -1 && $current_value == 'now')) + { + $now = getdate(); + + $field_data['field_default_value_day'] = $now['mday']; + $field_data['field_default_value_month'] = $now['mon']; + $field_data['field_default_value_year'] = $now['year']; + $current_value = 'now'; + $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST); + } + else + { + if ($this->request->is_set('field_default_value_day')) + { + $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0); + $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0); + $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0); + $current_value = sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']); + $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST); + } + else + { + list($field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']) = explode('-', $current_value); + } + } + + return $current_value; + } + + + return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); + } } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index a113b1d5ef..0bf36fcbfd 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -224,4 +224,19 @@ class type_dropdown extends type_base return $error; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 2 && $key == 'field_maxlen') + { + // Get the number of options if this key is 'field_maxlen' + return sizeof(explode("\n", $this->request->variable('lang_options', '', true))); + } + + + return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); + } } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 2cd9d3b2c4..e22b45e7a4 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -183,4 +183,21 @@ class type_int extends type_base return $options; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 2 && $key == 'field_default_value') + { + // Permit an empty string + if ($action == 'create' && $this->request->variable('field_default_value', '') === '') + { + return ''; + } + } + + return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index ad67536543..7cbdd30ddf 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -131,4 +131,16 @@ interface type_interface * @return array Array with error messages */ public function validate_options_on_submit($error, $field_data); + + /** + * Allows manipulating the intended variables if needed + * + * @param string $key Name of the option + * @param string $action Currently performed action (create|edit) + * @param mixed $current_value Currently value of the option + * @param array $field_data Array with data for this field + * @param int $step Step on which the option is excluded + * @return mixed Final value of the option + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step); } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 309ed88818..4437334d2b 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -114,4 +114,30 @@ class type_text extends type_string_common return $options; } + + /** + * {@inheritDoc} + */ + public function get_excluded_options($key, $action, $current_value, &$field_data, $step) + { + if ($step == 2 && $key == 'field_length') + { + if ($this->request->is_set('rows')) + { + $field_data['rows'] = $this->request->variable('rows', 0); + $field_data['columns'] = $this->request->variable('columns', 0); + $current_value = $field_data['rows'] . '|' . $field_data['columns']; + } + else + { + $row_col = explode('|', $current_value); + $field_data['rows'] = $row_col[0]; + $field_data['columns'] = $row_col[1]; + } + + return $current_value; + } + + return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); + } } -- cgit v1.2.1 From af5c747f6f01e9e5c10f5b32e7336ba9fb85aeeb Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 14:41:01 +0100 Subject: [ticket/11201] Move preparation of hidden fields to type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 48 +++-------------------- phpBB/phpbb/profilefields/type/type_base.php | 20 ++++++++++ phpBB/phpbb/profilefields/type/type_bool.php | 30 ++++++++++++++ phpBB/phpbb/profilefields/type/type_date.php | 25 ++++++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 11 ++++++ phpBB/phpbb/profilefields/type/type_text.php | 15 +++++++ 6 files changed, 106 insertions(+), 43 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index a1b3c46146..e846b5798e 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -568,54 +568,16 @@ class acp_profile $_new_key_ary = array(); + $field_data = $cp->vars; foreach ($key_ary as $key) { - if ($field_type == FIELD_TEXT && $key == 'field_length' && isset($_REQUEST['rows'])) + $var = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data); + if (!is_null($var)) { - $cp->vars['rows'] = request_var('rows', 0); - $cp->vars['columns'] = request_var('columns', 0); - $_new_key_ary[$key] = $cp->vars['rows'] . '|' . $cp->vars['columns']; - } - else if ($field_type == FIELD_DATE && $key == 'field_default_value') - { - $always_now = request_var('always_now', 0); - - if ($always_now) - { - $_new_key_ary[$key] = 'now'; - } - else if (isset($_REQUEST['field_default_value_day'])) - { - $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0); - $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0); - $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0); - $_new_key_ary[$key] = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']); - } - } - else if ($field_type == FIELD_BOOL && $key == 'l_lang_options' && isset($_REQUEST['l_lang_options'])) - { - $_new_key_ary[$key] = utf8_normalize_nfc(request_var($key, array(array('')), true)); - } - else if ($field_type == FIELD_BOOL && $key == 'field_default_value') - { - $_new_key_ary[$key] = request_var($key, $cp->vars[$key]); - } - else - { - if (!isset($_REQUEST[$key])) - { - $var = false; - } - else if ($key == 'field_ident' && isset($cp->vars[$key])) - { - $_new_key_ary[$key]= $cp->vars[$key]; - } - else - { - $_new_key_ary[$key] = ($field_type == FIELD_BOOL && $key == 'lang_options') ? utf8_normalize_nfc(request_var($key, array(''), true)) : utf8_normalize_nfc(request_var($key, '', true)); - } + $_new_key_ary[$key] = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data); } } + $cp->vars = $field_data; $s_hidden_fields .= build_hidden_fields($_new_key_ary); } diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index cd03af57f7..cb262d54a8 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -75,4 +75,24 @@ abstract class type_base implements type_interface return $current_value; } + + /** + * {@inheritDoc} + */ + public function prepare_hidden_fields($step, $key, $action, &$field_data) + { + if (!$this->request->is_set($key)) + { + // Do not set this variable, we will use the default value + return null; + } + else if ($key == 'field_ident' && isset($field_data[$key])) + { + return $field_data[$key]; + } + else + { + return $this->request->variable($key, '', true); + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index acf20e93e0..f303e40213 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -296,4 +296,34 @@ class type_bool extends type_base return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); } + + /** + * {@inheritDoc} + */ + public function prepare_hidden_fields($step, $key, $action, &$field_data) + { + if ($key == 'l_lang_options' && $this->request->is_set('l_lang_options')) + { + return $this->request->variable($key, array(array('')), true); + } + else if ($key == 'field_default_value') + { + return $this->request->variable($key, $field_data[$key]); + } + else + { + if (!$this->request->is_set($key)) + { + return false; + } + else if ($key == 'field_ident' && isset($field_data[$key])) + { + return $field_data[$key]; + } + else + { + return ($key == 'lang_options') ? $this->request->variable($key, array(''), true) : $this->request->variable($key, '', true); + } + } + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 099d1dbb50..9c706af7a3 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -300,4 +300,29 @@ class type_date extends type_base return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); } + + /** + * {@inheritDoc} + */ + public function prepare_hidden_fields($step, $key, $action, &$field_data) + { + if ($key == 'field_default_value') + { + $always_now = $this->request->variable('always_now', 0); + + if ($always_now) + { + return 'now'; + } + else if ($this->request->is_set'field_default_value_day')) + { + $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0); + $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0); + $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0); + return sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']); + } + } + + return parent::prepare_hidden_fields($step, $key, $action, $field_data); + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 7cbdd30ddf..9ce975f676 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -143,4 +143,15 @@ interface type_interface * @return mixed Final value of the option */ public function get_excluded_options($key, $action, $current_value, &$field_data, $step); + + /** + * Allows manipulating the intended variables if needed + * + * @param string $key Name of the option + * @param int $step Step on which the option is hidden + * @param string $action Currently performed action (create|edit) + * @param array $field_data Array with data for this field + * @return mixed Final value of the option + */ + public function prepare_hidden_fields($step, $key, $action, &$field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 4437334d2b..60abb2e421 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -140,4 +140,19 @@ class type_text extends type_string_common return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); } + + /** + * {@inheritDoc} + */ + public function prepare_hidden_fields($step, $key, $action, &$field_data) + { + if ($key == 'field_length' && $this->request->is_set('rows')) + { + $field_data['rows'] = $this->request->variable('rows', 0); + $field_data['columns'] = $this->request->variable('columns', 0); + return $field_data['rows'] . '|' . $field_data['columns']; + } + + return parent::prepare_hidden_fields($step, $key, $action, $field_data); + } } -- cgit v1.2.1 From 74d36591e1664702b6aae58802c8d89d7bc450b4 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 14:56:13 +0100 Subject: [ticket/11201] Move custom template variable assignment to type class PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 51 ++++------------------- phpBB/phpbb/profilefields/type/type_base.php | 8 ++++ phpBB/phpbb/profilefields/type/type_bool.php | 21 ++++++++++ phpBB/phpbb/profilefields/type/type_date.php | 2 +- phpBB/phpbb/profilefields/type/type_dropdown.php | 19 +++++++++ phpBB/phpbb/profilefields/type/type_interface.php | 19 ++++----- phpBB/phpbb/profilefields/type/type_string.php | 12 ++++++ phpBB/phpbb/profilefields/type/type_text.php | 12 ++++++ 8 files changed, 89 insertions(+), 55 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index e846b5798e..5387f23049 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -611,9 +611,7 @@ class acp_profile { // Create basic options - only small differences between field types case 1: - - // Build common create options - $template->assign_vars(array( + $template_vars = array( 'S_STEP_ONE' => true, 'S_FIELD_REQUIRED' => ($cp->vars['field_required']) ? true : false, 'S_FIELD_SHOW_NOVALUE'=> ($cp->vars['field_show_novalue']) ? true : false, @@ -628,50 +626,15 @@ class acp_profile 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])], 'FIELD_IDENT' => $cp->vars['field_ident'], 'LANG_NAME' => $cp->vars['lang_name'], - 'LANG_EXPLAIN' => $cp->vars['lang_explain']) + 'LANG_EXPLAIN' => $cp->vars['lang_explain'], ); - // String and Text needs to set default values here... - if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT) - { - $template->assign_vars(array( - 'S_TEXT' => ($field_type == FIELD_TEXT) ? true : false, - 'S_STRING' => ($field_type == FIELD_STRING) ? true : false, - - 'L_DEFAULT_VALUE_EXPLAIN' => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_DEFAULT_VALUE_EXPLAIN'], - 'LANG_DEFAULT_VALUE' => $cp->vars['lang_default_value']) - ); - } - - if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN) - { - // Initialize these array elements if we are creating a new field - if (!sizeof($cp->vars['lang_options'])) - { - if ($field_type == FIELD_BOOL) - { - // No options have been defined for a boolean field. - $cp->vars['lang_options'][0] = ''; - $cp->vars['lang_options'][1] = ''; - } - else - { - // No options have been defined for the dropdown menu - $cp->vars['lang_options'] = array(); - } - } - - $template->assign_vars(array( - 'S_BOOL' => ($field_type == FIELD_BOOL) ? true : false, - 'S_DROPDOWN' => ($field_type == FIELD_DROPDOWN) ? true : false, - - 'L_LANG_OPTIONS_EXPLAIN' => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_ENTRIES_EXPLAIN'], - 'LANG_OPTIONS' => ($field_type == FIELD_DROPDOWN) ? implode("\n", $cp->vars['lang_options']) : '', - 'FIRST_LANG_OPTION' => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][0] : '', - 'SECOND_LANG_OPTION' => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][1] : '') - ); - } + $field_data = $cp->vars; + $profile_field->display_options($template_vars, $field_data); + $cp->vars = $field_data; + // Build common create options + $template->assign_vars($template_vars); break; case 2: diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index cb262d54a8..2fd4c50f6b 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -95,4 +95,12 @@ abstract class type_base implements type_interface return $this->request->variable($key, '', true); } } + + /** + * {@inheritDoc} + */ + public function display_options(&$template_vars, &$field_data) + { + return; + } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f303e40213..016ceb6000 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -326,4 +326,25 @@ class type_bool extends type_base } } } + + /** + * {@inheritDoc} + */ + public function display_options(&$template_vars, &$field_data) + { + // Initialize these array elements if we are creating a new field + if (!sizeof($field_data['lang_options'])) + { + // No options have been defined for a boolean field. + $field_data['lang_options'][0] = ''; + $field_data['lang_options'][1] = ''; + } + + $template_vars = array_merge($template_vars, array( + 'S_BOOL' => true, + 'L_LANG_OPTIONS_EXPLAIN' => $this->user->lang['BOOL_ENTRIES_EXPLAIN'], + 'FIRST_LANG_OPTION' => $field_data['lang_options'][0], + 'SECOND_LANG_OPTION' => $field_data['lang_options'][1], + )); + } } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 9c706af7a3..301e05c9ae 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -314,7 +314,7 @@ class type_date extends type_base { return 'now'; } - else if ($this->request->is_set'field_default_value_day')) + else if ($this->request->is_set('field_default_value_day')) { $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0); $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0); diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 0bf36fcbfd..95f250f444 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -239,4 +239,23 @@ class type_dropdown extends type_base return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); } + + /** + * {@inheritDoc} + */ + public function display_options(&$template_vars, &$field_data) + { + // Initialize these array elements if we are creating a new field + if (!sizeof($field_data['lang_options'])) + { + // No options have been defined for the dropdown menu + $field_data['lang_options'] = array(); + } + + $template_vars = array_merge($template_vars, array( + 'S_DROPDOWN' => true, + 'L_LANG_OPTIONS_EXPLAIN' => $this->user->lang['DROPDOWN_ENTRIES_EXPLAIN'], + 'LANG_OPTIONS' => implode("\n", $field_data['lang_options']), + )); + } } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 9ce975f676..e0c2ba824d 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -11,16 +11,6 @@ namespace phpbb\profilefields\type; interface type_interface { - /* - public function validate(&$value, $validation); - - public function prepare_for_storage($value); - - public function prepare_for_display($value); - - public function prepare_for_edit($value); - */ - /** * Get dropdown options for second step in ACP * @@ -154,4 +144,13 @@ interface type_interface * @return mixed Final value of the option */ public function prepare_hidden_fields($step, $key, $action, &$field_data); + + /** + * Allows assigning of additional template variables + * + * @param array $template_vars Template variables we are going to assign + * @param array $field_data Array with data for this field + * @return null + */ + public function display_options(&$template_vars, &$field_data); } diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 66157ddc20..9542a6256a 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -110,4 +110,16 @@ class type_string extends type_string_common return $options; } + + /** + * {@inheritDoc} + */ + public function display_options(&$template_vars, &$field_data) + { + $template_vars = array_merge($template_vars, array( + 'S_STRING' => true, + 'L_DEFAULT_VALUE_EXPLAIN' => $this->user->lang['STRING_DEFAULT_VALUE_EXPLAIN'], + 'LANG_DEFAULT_VALUE' => $field_data['lang_default_value'], + )); + } } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 60abb2e421..0ead505262 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -155,4 +155,16 @@ class type_text extends type_string_common return parent::prepare_hidden_fields($step, $key, $action, $field_data); } + + /** + * {@inheritDoc} + */ + public function display_options(&$template_vars, &$field_data) + { + $template_vars = array_merge($template_vars, array( + 'S_TEXT' => true, + 'L_DEFAULT_VALUE_EXPLAIN' => $this->user->lang['TEXT_DEFAULT_VALUE_EXPLAIN'], + 'LANG_DEFAULT_VALUE' => $field_data['lang_default_value'], + )); + } } -- cgit v1.2.1 From 0ec6af38a97239003be76a85eb86e2f0aef5e99c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 15:03:26 +0100 Subject: [ticket/11201] Fix construction of dropdown type once again PHPBB3-11201 --- tests/profile/custom_test.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/profile/custom_test.php b/tests/profile/custom_test.php index cfc350c162..9a59225d70 100644 --- a/tests/profile/custom_test.php +++ b/tests/profile/custom_test.php @@ -50,10 +50,20 @@ class phpbb_profile_custom_test extends phpbb_database_test_case ->method('lang') ->will($this->returnCallback(array($this, 'return_callback_implode'))); + $request = $this->getMock('\phpbb\request\request'); + $template = $this->getMock('\phpbb\template\template'); + $cp = new \phpbb\profilefields\type\type_dropdown( new \phpbb\profilefields\lang_helper($db), - $this->getMock('\phpbb\request\request'), - $this->getMock('\phpbb\template\template'), + new \phpbb\profilefields\profilefields( + $this->getMock('\phpbb\auth\auth'), + $db, + $request, + $template, + $user + ), + $request, + $template, $user ); $result = $cp->validate_profile_field($field_value, $field_data); -- cgit v1.2.1 From aa2f0a652ff19490def8137bc73255dff282f305 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 19:33:29 +0100 Subject: [ticket/11201] Change type from integer to service name PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 37 +++---- .../db/migration/data/v310/profilefield_types.php | 106 +++++++++++++++++++++ phpBB/phpbb/profilefields/lang_helper.php | 2 +- phpBB/phpbb/profilefields/profilefields.php | 15 ++- phpBB/phpbb/profilefields/type/type_base.php | 8 ++ phpBB/phpbb/profilefields/type/type_bool.php | 12 ++- phpBB/phpbb/profilefields/type/type_date.php | 10 +- phpBB/phpbb/profilefields/type/type_dropdown.php | 16 +++- phpBB/phpbb/profilefields/type/type_int.php | 8 ++ phpBB/phpbb/profilefields/type/type_interface.php | 14 +++ phpBB/phpbb/profilefields/type/type_string.php | 8 ++ phpBB/phpbb/profilefields/type/type_text.php | 8 ++ 12 files changed, 210 insertions(+), 34 deletions(-) create mode 100644 phpBB/phpbb/db/migration/data/v310/profilefield_types.php diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index 5387f23049..feb8265cf5 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -24,6 +24,7 @@ class acp_profile var $edit_lang_id; var $lang_defs; + protected $type_collection; function main($id, $mode) { @@ -50,6 +51,7 @@ class acp_profile } $cp = $phpbb_container->get('profilefields'); + $this->type_collection = $phpbb_container->get('profilefields.type_collection'); // Build Language array // Based on this, we decide which elements need to be edited later and which language items are missing @@ -341,7 +343,7 @@ class acp_profile $this->edit_lang_id = $field_row['lang_id']; } $field_type = $field_row['field_type']; - $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); + $profile_field = $this->type_collection[$field_type]; // Get language entries $sql = 'SELECT * @@ -365,14 +367,14 @@ class acp_profile // We are adding a new field, define basic params $lang_options = $field_row = array(); - $field_type = request_var('field_type', 0); + $field_type = request_var('field_type', ''); - if (!$field_type) + if (!isset($this->type_collection[$field_type])) { trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING); } - $profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]); + $profile_field = $this->type_collection[$field_type]; $field_row = array_merge($profile_field->get_default_option_values(), array( 'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))), 'field_required' => 0, @@ -623,7 +625,7 @@ class acp_profile 'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false, 'L_LANG_SPECIFIC' => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']), - 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])], + 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())], 'FIELD_IDENT' => $cp->vars['field_ident'], 'LANG_NAME' => $cp->vars['lang_name'], 'LANG_EXPLAIN' => $cp->vars['lang_explain'], @@ -707,9 +709,10 @@ class acp_profile $s_one_need_edit = true; } + $profile_field = $this->type_collection[$row['field_type']]; $template->assign_block_vars('fields', array( 'FIELD_IDENT' => $row['field_ident'], - 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$row['field_type']])], + 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())], 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang], 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&action=$active_value&field_id=$id", @@ -731,15 +734,15 @@ class acp_profile } $s_select_type = ''; - foreach ($cp->profile_types as $key => $value) + foreach ($this->type_collection as $key => $profile_field) { - $s_select_type .= ''; + $s_select_type .= ''; } $template->assign_vars(array( 'U_ACTION' => $this->u_action, - 'S_TYPE_OPTIONS' => $s_select_type) - ); + 'S_TYPE_OPTIONS' => $s_select_type, + )); } /** @@ -764,9 +767,8 @@ class acp_profile } $db->sql_freeresult($result); - $type_collection = $phpbb_container->get('profilefields.type_collection'); - $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; - $options = $profile_type->get_language_options($cp->vars); + $profile_field = $this->type_collection[$field_type]; + $options = $profile_field->get_language_options($cp->vars); $lang_options = array(); @@ -906,8 +908,7 @@ class acp_profile $db->sql_query($sql); } - $type_collection = $phpbb_container->get('profilefields.type_collection'); - $profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; + $profile_field = $this->type_collection[$field_type]; if ($action == 'create') { @@ -915,7 +916,7 @@ class acp_profile $db_tools = $phpbb_container->get('dbal.tools'); - list($sql_type, $null) = $db_tools->get_column_type($profile_type->get_database_column_type()); + list($sql_type, $null) = $db_tools->get_column_type($profile_field->get_database_column_type()); $profile_sql[] = $this->add_field_ident($field_ident, $sql_type); } @@ -990,7 +991,7 @@ class acp_profile foreach ($cp->vars['lang_options'] as $option_id => $value) { $sql_ary = array( - 'field_type' => (int) $field_type, + 'field_type' => $field_type, 'lang_value' => $value ); @@ -1045,7 +1046,7 @@ class acp_profile 'field_id' => (int) $field_id, 'lang_id' => (int) $lang_id, 'option_id' => (int) $option_id, - 'field_type' => (int) $field_type, + 'field_type' => $field_type, 'lang_value' => $value ); } diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_types.php b/phpBB/phpbb/db/migration/data/v310/profilefield_types.php new file mode 100644 index 0000000000..553ff592a4 --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_types.php @@ -0,0 +1,106 @@ + array( + $this->table_prefix . 'profile_fields' => array( + 'field_type' => array('VCHAR:100', ''), + ), + $this->table_prefix . 'profile_fields_lang' => array( + 'field_type' => array('VCHAR:100', ''), + ), + ), + ); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'update_profile_fields_type'))), + array('custom', array(array($this, 'update_profile_fields_lang_type'))), + ); + } + + public function update_profile_fields_type() + { + // Update profile field types + $sql = 'SELECT field_type + FROM ' . $this->table_prefix . 'profile_fields + GROUP BY field_type'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $sql = 'UPDATE ' . $this->table_prefix . "profile_fields + SET field_type = '" . $this->db->sql_escape($this->convert_phpbb30_field_type($row['field_type'])) . "' + WHERE field_type = '" . $this->db->sql_escape($row['field_type']) . "'"; + $this->sql_query($sql); + } + $this->db->sql_freeresult($result); + } + + public function update_profile_fields_lang_type() + { + // Update profile field language types + $sql = 'SELECT field_type + FROM ' . $this->table_prefix . 'profile_fields_lang + GROUP BY field_type'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $sql = 'UPDATE ' . $this->table_prefix . "profile_fields_lang + SET field_type = '" . $this->db->sql_escape($this->convert_phpbb30_field_type($row['field_type'])) . "' + WHERE field_type = '" . $this->db->sql_escape($row['field_type']) . "'"; + $this->sql_query($sql); + } + $this->db->sql_freeresult($result); + } + + /** + * Determine the new field type for a given phpBB 3.0 field type + * + * @param $field_type int Field type in 3.0 + * @return string Field new type which is used since 3.1 + */ + public function convert_phpbb30_field_type($field_type) + { + switch ($field_type) + { + case FIELD_INT: + return 'profilefields.type.int'; + case FIELD_STRING: + return 'profilefields.type.string'; + case FIELD_TEXT: + return 'profilefields.type.text'; + case FIELD_BOOL: + return 'profilefields.type.bool'; + case FIELD_DROPDOWN: + return 'profilefields.type.dropdown'; + case FIELD_DATE: + return 'profilefields.type.date'; + default: + return $field_type; + } + } +} diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php index 569eaaca40..432c0aa40b 100644 --- a/phpBB/phpbb/profilefields/lang_helper.php +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -65,7 +65,7 @@ class lang_helper FROM ' . PROFILE_FIELDS_LANG_TABLE . " WHERE field_id = $field_id AND lang_id = $lang_id - AND field_type = $field_type + AND field_type = '" . $this->db->sql_escape($field_type) . "' ORDER BY option_id"; $result = $this->db->sql_query($sql); diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index a580ca2937..bd9765d04c 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -82,10 +82,9 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - // Return templated field $tpl_snippet = $this->process_field_row('change', $row); - $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; + $profile_field = $this->type_collection[$row['field_type']]; $this->template->assign_block_vars('profile_fields', array( 'LANG_NAME' => $row['lang_name'], @@ -160,7 +159,7 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; + $profile_field = $this->type_collection[$row['field_type']]; $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); $check_value = $cp_data['pf_' . $row['field_ident']]; @@ -300,7 +299,7 @@ class profilefields foreach ($profile_row as $ident => $ident_ary) { - $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; + $profile_field = $this->type_collection[$ident_ary['data']['field_type']]; $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']); if ($value === NULL) @@ -349,13 +348,13 @@ class profilefields )); // empty previously filled blockvars - foreach ($this->profile_types as $field_case => $field_type) + foreach ($this->type_collection as $field_key => $field_type) { - $this->template->destroy_block_vars($field_type); + $this->template->destroy_block_vars($field_type->get_name()); } // Assign template variables - $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$profile_row['field_type']]]; + $profile_field = $this->type_collection[$profile_row['field_type']]; $profile_field->generate_field($profile_row, $preview_options); // Return templated data @@ -382,7 +381,7 @@ class profilefields while ($row = $this->db->sql_fetchrow($result)) { - $profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]]; + $profile_field = $this->type_collection[$row['field_type']]; $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row); } $this->db->sql_freeresult($result); diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 2fd4c50f6b..7aee74d9ff 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -21,6 +21,14 @@ abstract class type_base implements type_interface $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_service_name() + { + return 'profilefields.type.' . $this->get_name(); + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 016ceb6000..f466b60099 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -23,6 +23,14 @@ class type_bool extends type_base $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'bool'; + } + /** * {@inheritDoc} */ @@ -36,7 +44,7 @@ class type_bool extends type_base 'lang_id' => $default_lang_id, 'field_default_value' => $field_data['field_default_value'], 'field_ident' => 'field_default_value', - 'field_type' => FIELD_BOOL, + 'field_type' => $this->get_service_name(), 'field_length' => $field_data['field_length'], 'lang_options' => $field_data['lang_options'] ); @@ -163,7 +171,7 @@ class type_bool extends type_base { if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1)) { - $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview_options); + $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], $this->get_service_name(), $preview_options); } $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']); diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 301e05c9ae..e59c959f5d 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -22,6 +22,14 @@ class type_date extends type_base $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'date'; + } + /** * {@inheritDoc} */ @@ -34,7 +42,7 @@ class type_date extends type_base 'lang_id' => $default_lang_id, 'field_default_value' => $field_data['field_default_value'], 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DATE, + 'field_type' => $this->get_service_name(), 'field_length' => $field_data['field_length'], ); diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 95f250f444..c9d0936816 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -23,6 +23,14 @@ class type_dropdown extends type_base $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'dropdown'; + } + /** * {@inheritDoc} */ @@ -36,7 +44,7 @@ class type_dropdown extends type_base 'lang_id' => $default_lang_id, 'field_default_value' => $field_data['field_default_value'], 'field_ident' => 'field_default_value', - 'field_type' => FIELD_DROPDOWN, + 'field_type' => $this->get_service_name(), 'lang_options' => $field_data['lang_options'], ); @@ -95,7 +103,7 @@ class type_dropdown extends type_base // retrieve option lang data if necessary if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], 1)) { - $this->lang_helper->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false); + $this->lang_helper->get_option_lang($field_data['field_id'], $field_data['lang_id'], $this->get_service_name(), false); } if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], $field_value)) @@ -120,7 +128,7 @@ class type_dropdown extends type_base $lang_id = $field_data['lang_id']; if (!$this->lang_helper->is_set($field_id, $lang_id)) { - $this->lang_helper->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false); + $this->lang_helper->get_option_lang($field_id, $lang_id, $this->get_service_name(), false); } if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue']) @@ -159,7 +167,7 @@ class type_dropdown extends type_base if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1)) { - $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview_options); + $this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], $this->get_service_name(), $preview_options); } $profile_row['field_value'] = (int) $value; diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index e22b45e7a4..5b5f84d41b 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -21,6 +21,14 @@ class type_int extends type_base $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'int'; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index e0c2ba824d..5a88200bad 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -11,6 +11,20 @@ namespace phpbb\profilefields\type; interface type_interface { + /** + * Get the name of the type, used for error messages and template loops + * + * @return string lowercase version of the fields type + */ + public function get_name(); + + /** + * Get the name of service representing the type + * + * @return string lowercase version of the fields type + */ + public function get_service_name(); + /** * Get dropdown options for second step in ACP * diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 9542a6256a..73563bc579 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -21,6 +21,14 @@ class type_string extends type_string_common $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'string'; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 0ead505262..1638bffcff 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -21,6 +21,14 @@ class type_text extends type_string_common $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return 'text'; + } + /** * {@inheritDoc} */ -- cgit v1.2.1 From 39ff387d36001c14dc2130feaf811d51e946c886 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 17 Jan 2014 21:53:08 +0100 Subject: [ticket/11201] Update schema files and unit test PHPBB3-11201 --- phpBB/includes/db/schema_data.php | 4 ++-- phpBB/install/schemas/firebird_schema.sql | 4 ++-- phpBB/install/schemas/mssql_schema.sql | 4 ++-- phpBB/install/schemas/mysql_40_schema.sql | 4 ++-- phpBB/install/schemas/mysql_41_schema.sql | 4 ++-- phpBB/install/schemas/oracle_schema.sql | 4 ++-- phpBB/install/schemas/postgres_schema.sql | 4 ++-- phpBB/install/schemas/sqlite_schema.sql | 4 ++-- tests/profile/fixtures/profile_fields.xml | 6 +++--- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/phpBB/includes/db/schema_data.php b/phpBB/includes/db/schema_data.php index 69d39e0f8c..039cb18ec2 100644 --- a/phpBB/includes/db/schema_data.php +++ b/phpBB/includes/db/schema_data.php @@ -758,7 +758,7 @@ $schema_data['phpbb_profile_fields'] = array( 'COLUMNS' => array( 'field_id' => array('UINT', NULL, 'auto_increment'), 'field_name' => array('VCHAR_UNI', ''), - 'field_type' => array('TINT:4', 0), + 'field_type' => array('VCHAR:100', ''), 'field_ident' => array('VCHAR:20', ''), 'field_length' => array('VCHAR:20', ''), 'field_minlen' => array('VCHAR', ''), @@ -796,7 +796,7 @@ $schema_data['phpbb_profile_fields_lang'] = array( 'field_id' => array('UINT', 0), 'lang_id' => array('UINT', 0), 'option_id' => array('UINT', 0), - 'field_type' => array('TINT:4', 0), + 'field_type' => array('VCHAR:100', ''), 'lang_value' => array('VCHAR_UNI', ''), ), 'PRIMARY_KEY' => array('field_id', 'lang_id', 'option_id'), diff --git a/phpBB/install/schemas/firebird_schema.sql b/phpBB/install/schemas/firebird_schema.sql index 6c6aca447d..a5e49fcdad 100644 --- a/phpBB/install/schemas/firebird_schema.sql +++ b/phpBB/install/schemas/firebird_schema.sql @@ -908,7 +908,7 @@ CREATE INDEX phpbb_privmsgs_to_usr_flder_id ON phpbb_privmsgs_to(user_id, folder CREATE TABLE phpbb_profile_fields ( field_id INTEGER NOT NULL, field_name VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE, - field_type INTEGER DEFAULT 0 NOT NULL, + field_type VARCHAR(100) CHARACTER SET NONE DEFAULT '' NOT NULL, field_ident VARCHAR(20) CHARACTER SET NONE DEFAULT '' NOT NULL, field_length VARCHAR(20) CHARACTER SET NONE DEFAULT '' NOT NULL, field_minlen VARCHAR(255) CHARACTER SET NONE DEFAULT '' NOT NULL, @@ -957,7 +957,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id INTEGER DEFAULT 0 NOT NULL, lang_id INTEGER DEFAULT 0 NOT NULL, option_id INTEGER DEFAULT 0 NOT NULL, - field_type INTEGER DEFAULT 0 NOT NULL, + field_type VARCHAR(100) CHARACTER SET NONE DEFAULT '' NOT NULL, lang_value VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE );; diff --git a/phpBB/install/schemas/mssql_schema.sql b/phpBB/install/schemas/mssql_schema.sql index 49804bdcc0..58a5749428 100644 --- a/phpBB/install/schemas/mssql_schema.sql +++ b/phpBB/install/schemas/mssql_schema.sql @@ -1114,7 +1114,7 @@ GO CREATE TABLE [phpbb_profile_fields] ( [field_id] [int] IDENTITY (1, 1) NOT NULL , [field_name] [varchar] (255) DEFAULT ('') NOT NULL , - [field_type] [int] DEFAULT (0) NOT NULL , + [field_type] [varchar] (100) DEFAULT ('') NOT NULL , [field_ident] [varchar] (20) DEFAULT ('') NOT NULL , [field_length] [varchar] (20) DEFAULT ('') NOT NULL , [field_minlen] [varchar] (255) DEFAULT ('') NOT NULL , @@ -1172,7 +1172,7 @@ CREATE TABLE [phpbb_profile_fields_lang] ( [field_id] [int] DEFAULT (0) NOT NULL , [lang_id] [int] DEFAULT (0) NOT NULL , [option_id] [int] DEFAULT (0) NOT NULL , - [field_type] [int] DEFAULT (0) NOT NULL , + [field_type] [varchar] (100) DEFAULT ('') NOT NULL , [lang_value] [varchar] (255) DEFAULT ('') NOT NULL ) ON [PRIMARY] GO diff --git a/phpBB/install/schemas/mysql_40_schema.sql b/phpBB/install/schemas/mysql_40_schema.sql index e287e7dde1..dfdf312588 100644 --- a/phpBB/install/schemas/mysql_40_schema.sql +++ b/phpBB/install/schemas/mysql_40_schema.sql @@ -645,7 +645,7 @@ CREATE TABLE phpbb_privmsgs_to ( CREATE TABLE phpbb_profile_fields ( field_id mediumint(8) UNSIGNED NOT NULL auto_increment, field_name blob NOT NULL, - field_type tinyint(4) DEFAULT '0' NOT NULL, + field_type varbinary(100) DEFAULT '' NOT NULL, field_ident varbinary(20) DEFAULT '' NOT NULL, field_length varbinary(20) DEFAULT '' NOT NULL, field_minlen varbinary(255) DEFAULT '' NOT NULL, @@ -681,7 +681,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, lang_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, option_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, - field_type tinyint(4) DEFAULT '0' NOT NULL, + field_type varbinary(100) DEFAULT '' NOT NULL, lang_value blob NOT NULL, PRIMARY KEY (field_id, lang_id, option_id) ); diff --git a/phpBB/install/schemas/mysql_41_schema.sql b/phpBB/install/schemas/mysql_41_schema.sql index 870fbd05e4..e352a894f6 100644 --- a/phpBB/install/schemas/mysql_41_schema.sql +++ b/phpBB/install/schemas/mysql_41_schema.sql @@ -645,7 +645,7 @@ CREATE TABLE phpbb_privmsgs_to ( CREATE TABLE phpbb_profile_fields ( field_id mediumint(8) UNSIGNED NOT NULL auto_increment, field_name varchar(255) DEFAULT '' NOT NULL, - field_type tinyint(4) DEFAULT '0' NOT NULL, + field_type varchar(100) DEFAULT '' NOT NULL, field_ident varchar(20) DEFAULT '' NOT NULL, field_length varchar(20) DEFAULT '' NOT NULL, field_minlen varchar(255) DEFAULT '' NOT NULL, @@ -681,7 +681,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, lang_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, option_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, - field_type tinyint(4) DEFAULT '0' NOT NULL, + field_type varchar(100) DEFAULT '' NOT NULL, lang_value varchar(255) DEFAULT '' NOT NULL, PRIMARY KEY (field_id, lang_id, option_id) ) CHARACTER SET `utf8` COLLATE `utf8_bin`; diff --git a/phpBB/install/schemas/oracle_schema.sql b/phpBB/install/schemas/oracle_schema.sql index 5354e5b1bb..bdc66880ca 100644 --- a/phpBB/install/schemas/oracle_schema.sql +++ b/phpBB/install/schemas/oracle_schema.sql @@ -1217,7 +1217,7 @@ CREATE INDEX phpbb_privmsgs_to_usr_flder_id ON phpbb_privmsgs_to (user_id, folde CREATE TABLE phpbb_profile_fields ( field_id number(8) NOT NULL, field_name varchar2(765) DEFAULT '' , - field_type number(4) DEFAULT '0' NOT NULL, + field_type varchar2(100) DEFAULT '' , field_ident varchar2(20) DEFAULT '' , field_length varchar2(20) DEFAULT '' , field_minlen varchar2(255) DEFAULT '' , @@ -1277,7 +1277,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id number(8) DEFAULT '0' NOT NULL, lang_id number(8) DEFAULT '0' NOT NULL, option_id number(8) DEFAULT '0' NOT NULL, - field_type number(4) DEFAULT '0' NOT NULL, + field_type varchar2(100) DEFAULT '' , lang_value varchar2(765) DEFAULT '' , CONSTRAINT pk_phpbb_profile_fields_lang PRIMARY KEY (field_id, lang_id, option_id) ) diff --git a/phpBB/install/schemas/postgres_schema.sql b/phpBB/install/schemas/postgres_schema.sql index 7773602c16..0b34b8b22c 100644 --- a/phpBB/install/schemas/postgres_schema.sql +++ b/phpBB/install/schemas/postgres_schema.sql @@ -853,7 +853,7 @@ CREATE SEQUENCE phpbb_profile_fields_seq; CREATE TABLE phpbb_profile_fields ( field_id INT4 DEFAULT nextval('phpbb_profile_fields_seq'), field_name varchar(255) DEFAULT '' NOT NULL, - field_type INT2 DEFAULT '0' NOT NULL, + field_type varchar(100) DEFAULT '' NOT NULL, field_ident varchar(20) DEFAULT '' NOT NULL, field_length varchar(20) DEFAULT '' NOT NULL, field_minlen varchar(255) DEFAULT '' NOT NULL, @@ -893,7 +893,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id INT4 DEFAULT '0' NOT NULL CHECK (field_id >= 0), lang_id INT4 DEFAULT '0' NOT NULL CHECK (lang_id >= 0), option_id INT4 DEFAULT '0' NOT NULL CHECK (option_id >= 0), - field_type INT2 DEFAULT '0' NOT NULL, + field_type varchar(100) DEFAULT '' NOT NULL, lang_value varchar(255) DEFAULT '' NOT NULL, PRIMARY KEY (field_id, lang_id, option_id) ); diff --git a/phpBB/install/schemas/sqlite_schema.sql b/phpBB/install/schemas/sqlite_schema.sql index 079d48bc65..683fcaeddf 100644 --- a/phpBB/install/schemas/sqlite_schema.sql +++ b/phpBB/install/schemas/sqlite_schema.sql @@ -626,7 +626,7 @@ CREATE INDEX phpbb_privmsgs_to_usr_flder_id ON phpbb_privmsgs_to (user_id, folde CREATE TABLE phpbb_profile_fields ( field_id INTEGER PRIMARY KEY NOT NULL , field_name varchar(255) NOT NULL DEFAULT '', - field_type tinyint(4) NOT NULL DEFAULT '0', + field_type varchar(100) NOT NULL DEFAULT '', field_ident varchar(20) NOT NULL DEFAULT '', field_length varchar(20) NOT NULL DEFAULT '', field_minlen varchar(255) NOT NULL DEFAULT '', @@ -661,7 +661,7 @@ CREATE TABLE phpbb_profile_fields_lang ( field_id INTEGER UNSIGNED NOT NULL DEFAULT '0', lang_id INTEGER UNSIGNED NOT NULL DEFAULT '0', option_id INTEGER UNSIGNED NOT NULL DEFAULT '0', - field_type tinyint(4) NOT NULL DEFAULT '0', + field_type varchar(100) NOT NULL DEFAULT '', lang_value varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (field_id, lang_id, option_id) ); diff --git a/tests/profile/fixtures/profile_fields.xml b/tests/profile/fixtures/profile_fields.xml index 0b2929f625..e0c260bbf5 100644 --- a/tests/profile/fixtures/profile_fields.xml +++ b/tests/profile/fixtures/profile_fields.xml @@ -10,21 +10,21 @@ 1 1 0 - 5 + profilefields.type.dropdown Default Option 1 1 1 - 5 + profilefields.type.dropdown First Alternative 1 1 2 - 5 + profilefields.type.dropdown Third Alternative -- cgit v1.2.1 From 431fa7b59321376fa7ceb44ac62de30c6edb29a8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:36:18 +0100 Subject: [ticket/11201] Inject table names rather then using constants PHPBB3-11201 --- phpBB/config/profilefields.yml | 4 ++++ phpBB/config/tables.yml | 4 ++++ phpBB/phpbb/profilefields/lang_helper.php | 12 ++++++++++-- phpBB/phpbb/profilefields/profilefields.php | 20 ++++++++++++-------- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 5aa30ac87a..6c6ccedcea 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -7,6 +7,9 @@ services: - @request - @template - @user + - %tables.profile_fields% + - %tables.profile_fields_language% + - %tables.profile_fields_data% calls: - [set_type_collection, [@profilefields.type_collection]] @@ -14,6 +17,7 @@ services: class: \phpbb\profilefields\lang_helper arguments: - @dbal.conn + - %tables.profile_fields_options_language% profilefields.type_collection: class: phpbb\di\service_collection diff --git a/phpBB/config/tables.yml b/phpBB/config/tables.yml index 0d364eb6b0..e4f7bda89b 100644 --- a/phpBB/config/tables.yml +++ b/phpBB/config/tables.yml @@ -10,6 +10,10 @@ parameters: tables.modules: %core.table_prefix%modules tables.notification_types: %core.table_prefix%notification_types tables.notifications: %core.table_prefix%notifications + tables.profile_fields: %core.table_prefix%profile_fields + tables.profile_fields_data: %core.table_prefix%profile_fields_data + tables.profile_fields_options_language: %core.table_prefix%profile_fields_lang + tables.profile_fields_language: %core.table_prefix%profile_lang tables.posts: %core.table_prefix%posts tables.topics: %core.table_prefix%topics tables.user_notifications: %core.table_prefix%user_notifications diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php index 432c0aa40b..cf4a248d1b 100644 --- a/phpBB/phpbb/profilefields/lang_helper.php +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -27,14 +27,22 @@ class lang_helper */ protected $db; + /** + * Table where the language strings are stored + * @var string + */ + protected $language_table; + /** * Construct * * @param \phpbb\db\driver\driver $db Database object + * @param string $language_table Table where the language strings are stored */ - public function __construct($db) + public function __construct($db, $language_table) { $this->db = $db; + $this->language_table = $language_table; } /** @@ -62,7 +70,7 @@ class lang_helper else { $sql = 'SELECT option_id, lang_value - FROM ' . PROFILE_FIELDS_LANG_TABLE . " + FROM ' . $this->language_table . " WHERE field_id = $field_id AND lang_id = $lang_id AND field_type = '" . $this->db->sql_escape($field_type) . "' diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index bd9765d04c..af0b733f32 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -21,13 +21,17 @@ class profilefields /** * */ - public function __construct($auth, $db, $request, $template, $user) + public function __construct($auth, $db, $request, $template, $user, $fields_table, $fields_language_table, $fields_data_table) { $this->auth = $auth; $this->db = $db; $this->request = $request; $this->template = $template; $this->user = $user; + + $this->fields_table = $fields_table; + $this->fields_language_table = $fields_language_table; + $this->fields_data_table = $fields_data_table; } /** @@ -72,7 +76,7 @@ class profilefields } $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f WHERE f.field_active = 1 $sql_where AND l.lang_id = $lang_id @@ -106,7 +110,7 @@ class profilefields // Display hidden/no_view fields for admin/moderator $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' AND f.field_active = 1 ' . ((!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . ' @@ -149,7 +153,7 @@ class profilefields } $sql = 'SELECT l.*, f.* - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f WHERE l.lang_id = $lang_id AND f.field_active = 1 $sql_where @@ -213,7 +217,7 @@ class profilefields $cp_data_sql[$left_delim . (($this->db->sql_layer == 'firebird' || $this->db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value; } - $sql = 'UPDATE ' . PROFILE_FIELDS_DATA_TABLE . ' + $sql = 'UPDATE ' . $this->fields_data_table . ' SET ' . $this->db->sql_build_array('UPDATE', $cp_data_sql) . " WHERE user_id = $user_id"; $this->db->sql_query($sql); @@ -224,7 +228,7 @@ class profilefields $this->db->sql_return_on_error(true); - $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $this->db->sql_build_array('INSERT', $cp_data_sql); + $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data_sql); $this->db->sql_query($sql); $this->db->sql_return_on_error(false); @@ -255,7 +259,7 @@ class profilefields } $sql = 'SELECT * - FROM ' . PROFILE_FIELDS_DATA_TABLE . ' + FROM ' . $this->fields_data_table . ' WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_id)); $result = $this->db->sql_query($sql); @@ -373,7 +377,7 @@ class profilefields } $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value - FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' ' . ((sizeof($sql_not_in)) ? ' AND ' . $this->db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . ' AND l.field_id = f.field_id'; -- cgit v1.2.1 From 876e5e5fbbf3049da2b96eacc7b13b29dd484642 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:40:12 +0100 Subject: [ticket/11201] Cast some variables to integer PHPBB3-11201 --- phpBB/phpbb/profilefields/lang_helper.php | 6 +++--- phpBB/phpbb/profilefields/profilefields.php | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/phpBB/phpbb/profilefields/lang_helper.php b/phpBB/phpbb/profilefields/lang_helper.php index cf4a248d1b..7bae1bdc18 100644 --- a/phpBB/phpbb/profilefields/lang_helper.php +++ b/phpBB/phpbb/profilefields/lang_helper.php @@ -70,9 +70,9 @@ class lang_helper else { $sql = 'SELECT option_id, lang_value - FROM ' . $this->language_table . " - WHERE field_id = $field_id - AND lang_id = $lang_id + FROM ' . $this->language_table . ' + WHERE field_id = ' . (int) $field_id . ' + AND lang_id = ' . (int) $lang_id . " AND field_type = '" . $this->db->sql_escape($field_type) . "' ORDER BY option_id"; $result = $this->db->sql_query($sql); diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index af0b733f32..acb1e6afcb 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -79,9 +79,9 @@ class profilefields FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f WHERE f.field_active = 1 $sql_where - AND l.lang_id = $lang_id + AND l.lang_id = " . (int) $lang_id . ' AND l.field_id = f.field_id - ORDER BY f.field_order"; + ORDER BY f.field_order'; $result = $this->db->sql_query($sql); while ($row = $this->db->sql_fetchrow($result)) @@ -153,8 +153,8 @@ class profilefields } $sql = 'SELECT l.*, f.* - FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f - WHERE l.lang_id = $lang_id + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f + WHERE l.lang_id = ' . (int) $lang_id . " AND f.field_active = 1 $sql_where AND l.field_id = f.field_id @@ -218,8 +218,8 @@ class profilefields } $sql = 'UPDATE ' . $this->fields_data_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $cp_data_sql) . " - WHERE user_id = $user_id"; + SET ' . $this->db->sql_build_array('UPDATE', $cp_data_sql) . ' + WHERE user_id = ' . (int) $user_id; $this->db->sql_query($sql); if (!$this->db->sql_affectedrows()) -- cgit v1.2.1 From 1c884d07ebd4629611a66e606e369217ffddd7d8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:44:24 +0100 Subject: [ticket/11201] Add some commas at the last array entry PHPBB3-11201 --- phpBB/phpbb/profilefields/type/type_bool.php | 8 ++++---- phpBB/phpbb/profilefields/type/type_dropdown.php | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f466b60099..a44e3ee770 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -46,7 +46,7 @@ class type_bool extends type_base 'field_ident' => 'field_default_value', 'field_type' => $this->get_service_name(), 'field_length' => $field_data['field_length'], - 'lang_options' => $field_data['lang_options'] + 'lang_options' => $field_data['lang_options'], ); $options = array( @@ -180,8 +180,8 @@ class type_bool extends type_base $this->template->assign_block_vars('bool.options', array( 'OPTION_ID' => $option_id, 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '', - 'VALUE' => $option_value) - ); + 'VALUE' => $option_value, + )); } } } @@ -226,7 +226,7 @@ class type_bool extends type_base public function get_language_options_input($field_data) { $field_data['l_lang_name'] = $this->request->variable('l_lang_name', array(0 => ''), true); - $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); + $field_data['l_lang_explain'] = $this->request->variable('l_lang_explain', array(0 => ''), true); $field_data['l_lang_default_value'] = $this->request->variable('l_lang_default_value', array(0 => ''), true); /** diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index c9d0936816..3645ae773c 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -179,8 +179,8 @@ class type_dropdown extends type_base $this->template->assign_block_vars('dropdown.options', array( 'OPTION_ID' => $option_id, 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '', - 'VALUE' => $option_value) - ); + 'VALUE' => $option_value, + )); } } -- cgit v1.2.1 From 7a8818fbc6989015c0abc86c66975fafa91fa3c8 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:46:48 +0100 Subject: [ticket/11201] Add visibility and remove unused variable PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index acb1e6afcb..b95940aefb 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -15,8 +15,7 @@ namespace phpbb\profilefields; */ class profilefields { - var $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date'); - var $profile_cache = array(); + protected $profile_cache = array(); /** * -- cgit v1.2.1 From 7fac23839893d0a9bd278ed8bc557cbd8ce90dd9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:47:19 +0100 Subject: [ticket/11201] Update copyright in class file PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index b95940aefb..7206ffcf0b 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -2,7 +2,7 @@ /** * * @package phpBB3 -* @copyright (c) 2005 phpBB Group +* @copyright (c) 2014 phpBB Group * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 * */ -- cgit v1.2.1 From b1bed49eae48692a5d621b9fe29187a533840c81 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 12:53:41 +0100 Subject: [ticket/11201] Add variables to classes and add constructor doc blocks PHPBB3-11201 --- phpBB/phpbb/profilefields/type/type_base.php | 23 +++++++++++++++ phpBB/phpbb/profilefields/type/type_bool.php | 37 ++++++++++++++++++++++++ phpBB/phpbb/profilefields/type/type_date.php | 30 +++++++++++++++++++ phpBB/phpbb/profilefields/type/type_dropdown.php | 37 ++++++++++++++++++++++++ phpBB/phpbb/profilefields/type/type_int.php | 23 +++++++++++++++ phpBB/phpbb/profilefields/type/type_string.php | 23 +++++++++++++++ phpBB/phpbb/profilefields/type/type_text.php | 23 +++++++++++++++ 7 files changed, 196 insertions(+) diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 7aee74d9ff..195186ec86 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -12,7 +12,30 @@ namespace phpbb\profilefields\type; abstract class type_base implements type_interface { /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index a44e3ee770..f5f9003e6f 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -12,7 +12,44 @@ namespace phpbb\profilefields\type; class type_bool extends type_base { /** + * Profile fields language helper + * @var \phpbb\profilefields\lang_helper + */ + protected $lang_helper; + + /** + * Profile fields object + * @var \phpbb\profilefields\profilefields + */ + protected $profilefields; + + /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper + * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index e59c959f5d..9b13cbac2e 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -12,7 +12,37 @@ namespace phpbb\profilefields\type; class type_date extends type_base { /** + * Profile fields object + * @var \phpbb\profilefields\profilefields + */ + protected $profilefields; + + /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 3645ae773c..6bbd3f0842 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -12,7 +12,44 @@ namespace phpbb\profilefields\type; class type_dropdown extends type_base { /** + * Profile fields language helper + * @var \phpbb\profilefields\lang_helper + */ + protected $lang_helper; + + /** + * Profile fields object + * @var \phpbb\profilefields\profilefields + */ + protected $profilefields; + + /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper + * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 5b5f84d41b..8e43a6e312 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -12,7 +12,30 @@ namespace phpbb\profilefields\type; class type_int extends type_base { /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 73563bc579..8b3dd1f73d 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -12,7 +12,30 @@ namespace phpbb\profilefields\type; class type_string extends type_string_common { /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 1638bffcff..26671d2129 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -12,7 +12,30 @@ namespace phpbb\profilefields\type; class type_text extends type_string_common { /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + /** + * Construct * + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $language_table Table where the language strings are stored */ public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { -- cgit v1.2.1 From df85364baa440e8c244ae90235ad74400981eef5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 13:01:29 +0100 Subject: [ticket/11201] Remove db depending code from field class sql_build_array() should already take care of that PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 39 +++-------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 7206ffcf0b..8b1e10e78d 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -178,56 +178,25 @@ class profilefields /** * Update profile field data directly */ - public function update_profile_field_data($user_id, &$cp_data) + public function update_profile_field_data($user_id, $cp_data) { if (!sizeof($cp_data)) { return; } - switch ($this->db->sql_layer) - { - case 'oracle': - case 'firebird': - case 'postgres': - $right_delim = $left_delim = '"'; - break; - - case 'sqlite': - case 'mssql': - case 'mssql_odbc': - case 'mssqlnative': - $right_delim = ']'; - $left_delim = '['; - break; - - case 'mysql': - case 'mysql4': - case 'mysqli': - $right_delim = $left_delim = '`'; - break; - } - - // use new array for the UPDATE; changes in the key do not affect the original array - $cp_data_sql = array(); - foreach ($cp_data as $key => $value) - { - // Firebird is case sensitive with delimiter - $cp_data_sql[$left_delim . (($this->db->sql_layer == 'firebird' || $this->db->sql_layer == 'oracle') ? strtoupper($key) : $key) . $right_delim] = $value; - } - $sql = 'UPDATE ' . $this->fields_data_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $cp_data_sql) . ' + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' WHERE user_id = ' . (int) $user_id; $this->db->sql_query($sql); if (!$this->db->sql_affectedrows()) { - $cp_data_sql['user_id'] = (int) $user_id; + $cp_data['user_id'] = (int) $user_id; $this->db->sql_return_on_error(true); - $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data_sql); + $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data); $this->db->sql_query($sql); $this->db->sql_return_on_error(false); -- cgit v1.2.1 From 197e026699e838c087b6d11a16ed6c2d9f15e0b0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 13:08:15 +0100 Subject: [ticket/11201] Add a method to return the translated full name of the type PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 6 +++--- phpBB/phpbb/profilefields/profilefields.php | 2 +- phpBB/phpbb/profilefields/type/type_base.php | 10 +++++++++- phpBB/phpbb/profilefields/type/type_bool.php | 2 +- phpBB/phpbb/profilefields/type/type_date.php | 2 +- phpBB/phpbb/profilefields/type/type_dropdown.php | 2 +- phpBB/phpbb/profilefields/type/type_int.php | 2 +- phpBB/phpbb/profilefields/type/type_interface.php | 11 +++++++++-- phpBB/phpbb/profilefields/type/type_string.php | 2 +- phpBB/phpbb/profilefields/type/type_text.php | 2 +- 10 files changed, 28 insertions(+), 13 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index feb8265cf5..d7a733eded 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -625,7 +625,7 @@ class acp_profile 'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false, 'L_LANG_SPECIFIC' => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']), - 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())], + 'FIELD_TYPE' => $profile_field->get_name(), 'FIELD_IDENT' => $cp->vars['field_ident'], 'LANG_NAME' => $cp->vars['lang_name'], 'LANG_EXPLAIN' => $cp->vars['lang_explain'], @@ -712,7 +712,7 @@ class acp_profile $profile_field = $this->type_collection[$row['field_type']]; $template->assign_block_vars('fields', array( 'FIELD_IDENT' => $row['field_ident'], - 'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())], + 'FIELD_TYPE' => $profile_field->get_name(), 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang], 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&action=$active_value&field_id=$id", @@ -736,7 +736,7 @@ class acp_profile $s_select_type = ''; foreach ($this->type_collection as $key => $profile_field) { - $s_select_type .= ''; + $s_select_type .= ''; } $template->assign_vars(array( diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 8b1e10e78d..f63064bd56 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -322,7 +322,7 @@ class profilefields // empty previously filled blockvars foreach ($this->type_collection as $field_key => $field_type) { - $this->template->destroy_block_vars($field_type->get_name()); + $this->template->destroy_block_vars($field_type->get_name_short()); } // Assign template variables diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 195186ec86..6961d208e9 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -44,12 +44,20 @@ abstract class type_base implements type_interface $this->user = $user; } + /** + * {@inheritDoc} + */ + public function get_name() + { + return $this->user->lang('FIELD_' . strtoupper($this->get_name_short())); + } + /** * {@inheritDoc} */ public function get_service_name() { - return 'profilefields.type.' . $this->get_name(); + return 'profilefields.type.' . $this->get_name_short(); } /** diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index f5f9003e6f..68d17fb4cf 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -63,7 +63,7 @@ class type_bool extends type_base /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'bool'; } diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 9b13cbac2e..f874ff33c3 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -55,7 +55,7 @@ class type_date extends type_base /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'date'; } diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 6bbd3f0842..5596e8f172 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -63,7 +63,7 @@ class type_dropdown extends type_base /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'dropdown'; } diff --git a/phpBB/phpbb/profilefields/type/type_int.php b/phpBB/phpbb/profilefields/type/type_int.php index 8e43a6e312..6ffc8fbea0 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -47,7 +47,7 @@ class type_int extends type_base /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'int'; } diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 5a88200bad..7d0cf7662f 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -12,12 +12,19 @@ namespace phpbb\profilefields\type; interface type_interface { /** - * Get the name of the type, used for error messages and template loops + * Get the translated name of the type * - * @return string lowercase version of the fields type + * @return string Translated name of the field type */ public function get_name(); + /** + * Get the short name of the type, used for error messages and template loops + * + * @return string lowercase version of the fields type + */ + public function get_name_short(); + /** * Get the name of service representing the type * diff --git a/phpBB/phpbb/profilefields/type/type_string.php b/phpBB/phpbb/profilefields/type/type_string.php index 8b3dd1f73d..12a14d9b83 100644 --- a/phpBB/phpbb/profilefields/type/type_string.php +++ b/phpBB/phpbb/profilefields/type/type_string.php @@ -47,7 +47,7 @@ class type_string extends type_string_common /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'string'; } diff --git a/phpBB/phpbb/profilefields/type/type_text.php b/phpBB/phpbb/profilefields/type/type_text.php index 26671d2129..660bb20ef8 100644 --- a/phpBB/phpbb/profilefields/type/type_text.php +++ b/phpBB/phpbb/profilefields/type/type_text.php @@ -47,7 +47,7 @@ class type_text extends type_string_common /** * {@inheritDoc} */ - public function get_name() + public function get_name_short() { return 'text'; } -- cgit v1.2.1 From ad8d4492a703f48997bfba51e962bd2fc91a3acc Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 13:12:50 +0100 Subject: [ticket/11201] Add tables to constructor in tests PHPBB3-11201 --- tests/profile/custom_test.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/profile/custom_test.php b/tests/profile/custom_test.php index 9a59225d70..654c9f6e5a 100644 --- a/tests/profile/custom_test.php +++ b/tests/profile/custom_test.php @@ -35,7 +35,7 @@ class phpbb_profile_custom_test extends phpbb_database_test_case */ public function test_dropdown_validate($field_required, $field_value, $expected, $description) { - global $db; + global $db, $table_prefix; $db = $this->new_dbal(); $field_data = array( @@ -54,13 +54,16 @@ class phpbb_profile_custom_test extends phpbb_database_test_case $template = $this->getMock('\phpbb\template\template'); $cp = new \phpbb\profilefields\type\type_dropdown( - new \phpbb\profilefields\lang_helper($db), + new \phpbb\profilefields\lang_helper($db, $table_prefix . 'profile_fields_lang'), new \phpbb\profilefields\profilefields( $this->getMock('\phpbb\auth\auth'), $db, $request, $template, - $user + $user, + $table_prefix . 'profile_fields', + $table_prefix . 'profile_lang', + $table_prefix . 'profile_fields_data' ), $request, $template, -- cgit v1.2.1 From f40f774b2e9b862e9dafa110db07ffe3ffaaccdf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sat, 18 Jan 2014 13:28:17 +0100 Subject: [ticket/11201] Fix some variable names PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 2 +- phpBB/phpbb/profilefields/type/type_date.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index d7a733eded..d588a34cec 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -971,7 +971,7 @@ class acp_profile } } - $cp->vars = $profile_type->get_language_options_input($cp->vars); + $cp->vars = $profile_field->get_language_options_input($cp->vars); if ($cp->vars['lang_options']) { diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index f874ff33c3..335a63b3f4 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -203,7 +203,7 @@ class type_date extends type_base return $this->user->create_datetime() ->setDate($year, $month, $day) ->setTime(0, 0, 0) - ->format($user->lang['DATE_FORMAT'], true); + ->format($this->user->lang['DATE_FORMAT'], true); } return $field_value; -- cgit v1.2.1 From 0ce98d7eac4724154650e2be357fceec02c44174 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 19 Jan 2014 14:52:51 +0100 Subject: [ticket/11201] Allow translation of profile field name and explanation PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 8 ++++---- phpBB/styles/prosilver/template/custom_profile_fields.html | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index f63064bd56..772c178c89 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -282,8 +282,8 @@ class profilefields $tpl_fields['row'] += array( 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value, 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_' . strtoupper($ident) . '_NAME' => $ident_ary['data']['lang_name'], - 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'], + 'PROFILE_' . strtoupper($ident) . '_NAME' => $this->user->lang($ident_ary['data']['lang_name']), + 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $this->user->lang($ident_ary['data']['lang_explain']), 'S_PROFILE_' . strtoupper($ident) => true ); @@ -291,8 +291,8 @@ class profilefields $tpl_fields['blockrow'][] = array( 'PROFILE_FIELD_VALUE' => $value, 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_FIELD_NAME' => $ident_ary['data']['lang_name'], - 'PROFILE_FIELD_EXPLAIN' => $ident_ary['data']['lang_explain'], + 'PROFILE_FIELD_NAME' => $this->user->lang($ident_ary['data']['lang_name']), + 'PROFILE_FIELD_EXPLAIN' => $this->user->lang($ident_ary['data']['lang_explain']), 'S_PROFILE_' . strtoupper($ident) => true ); diff --git a/phpBB/styles/prosilver/template/custom_profile_fields.html b/phpBB/styles/prosilver/template/custom_profile_fields.html index 7de97f64cb..764cac7b3a 100644 --- a/phpBB/styles/prosilver/template/custom_profile_fields.html +++ b/phpBB/styles/prosilver/template/custom_profile_fields.html @@ -5,7 +5,7 @@ - + -- cgit v1.2.1 From ec8fd57f284cb902be4ce8bd9c6a56c9a18007ae Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 19 Jan 2014 14:55:47 +0100 Subject: [ticket/11201] Add commas on last array entry PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index 772c178c89..deb2eeb036 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -285,7 +285,7 @@ class profilefields 'PROFILE_' . strtoupper($ident) . '_NAME' => $this->user->lang($ident_ary['data']['lang_name']), 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $this->user->lang($ident_ary['data']['lang_explain']), - 'S_PROFILE_' . strtoupper($ident) => true + 'S_PROFILE_' . strtoupper($ident) => true, ); $tpl_fields['blockrow'][] = array( @@ -294,7 +294,7 @@ class profilefields 'PROFILE_FIELD_NAME' => $this->user->lang($ident_ary['data']['lang_name']), 'PROFILE_FIELD_EXPLAIN' => $this->user->lang($ident_ary['data']['lang_explain']), - 'S_PROFILE_' . strtoupper($ident) => true + 'S_PROFILE_' . strtoupper($ident) => true, ); } -- cgit v1.2.1 From 4213b93cde5bb19b8d2fec43bb4043cd057beec9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 19 Jan 2014 15:00:26 +0100 Subject: [ticket/11201] Add parameters and variables to profile field class PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 47 ++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index deb2eeb036..ee5c987d36 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -15,12 +15,57 @@ namespace phpbb\profilefields; */ class profilefields { + /** + * Auth object + * @var \phpbb\auth\auth + */ + protected $auth; + + /** + * Database object + * @var \phpbb\db\driver\driver + */ + protected $db; + + /** + * Request object + * @var \phpbb\request\request + */ + protected $request; + + /** + * Template object + * @var \phpbb\template\template + */ + protected $template; + + /** + * User object + * @var \phpbb\user + */ + protected $user; + + protected $fields_table; + + protected $fields_language_table; + + protected $fields_data_table; + protected $profile_cache = array(); /** + * Construct * + * @param \phpbb\auth\auth $auth Auth object + * @param \phpbb\db\driver\driver $db Database object + * @param \phpbb\request\request $request Request object + * @param \phpbb\template\template $template Template object + * @param \phpbb\user $user User object + * @param string $fields_table + * @param string $fields_language_table + * @param string $fields_data_table */ - public function __construct($auth, $db, $request, $template, $user, $fields_table, $fields_language_table, $fields_data_table) + public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $fields_table, $fields_language_table, $fields_data_table) { $this->auth = $auth; $this->db = $db; -- cgit v1.2.1 From 430a0c1c212015f142777a6a31ce056c11e98ed2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 19 Jan 2014 17:58:12 +0100 Subject: [ticket/11201] Also translate profile fields in UCP and ACP PHPBB3-11201 --- phpBB/phpbb/profilefields/profilefields.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php index ee5c987d36..aec2756211 100644 --- a/phpBB/phpbb/profilefields/profilefields.php +++ b/phpBB/phpbb/profilefields/profilefields.php @@ -135,8 +135,8 @@ class profilefields $profile_field = $this->type_collection[$row['field_type']]; $this->template->assign_block_vars('profile_fields', array( - 'LANG_NAME' => $row['lang_name'], - 'LANG_EXPLAIN' => $row['lang_explain'], + 'LANG_NAME' => $this->user->lang($row['lang_name']), + 'LANG_EXPLAIN' => $this->user->lang($row['lang_explain']), 'FIELD' => $tpl_snippet, 'FIELD_ID' => $profile_field->get_field_ident($row), 'S_REQUIRED' => ($row['field_required']) ? true : false, -- cgit v1.2.1 From 12932e8113f32be76734703681d41afb36fd7309 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 20 Jan 2014 00:15:06 +0100 Subject: [ticket/11201] Use !== null, its faster PHPBB3-11201 --- phpBB/includes/acp/acp_profile.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index d588a34cec..c37d501aac 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -574,7 +574,7 @@ class acp_profile foreach ($key_ary as $key) { $var = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data); - if (!is_null($var)) + if ($var !== null) { $_new_key_ary[$key] = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data); } -- cgit v1.2.1 From 514a59ffa7b55b30d9af7aadf8735d6c2dfd48e1 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 12:39:21 +0100 Subject: [ticket/11201] Fix parameter description PHPBB3-11201 --- phpBB/phpbb/db/migration/data/v310/profilefield_types.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_types.php b/phpBB/phpbb/db/migration/data/v310/profilefield_types.php index 553ff592a4..2152aaee20 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_types.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_types.php @@ -80,7 +80,7 @@ class profilefield_types extends \phpbb\db\migration\migration /** * Determine the new field type for a given phpBB 3.0 field type * - * @param $field_type int Field type in 3.0 + * @param $field_type string Field type in 3.0 * @return string Field new type which is used since 3.1 */ public function convert_phpbb30_field_type($field_type) -- cgit v1.2.1 From 7bcbdfc1b15e0d014a22c76bb1eab6ccbc3d6dc7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 12:53:29 +0100 Subject: [ticket/11201] Rename profilefields class to manager PHPBB3-11201 --- phpBB/config/profilefields.yml | 10 +- phpBB/includes/acp/acp_profile.php | 2 +- phpBB/includes/acp/acp_users.php | 2 +- phpBB/includes/functions_user.php | 2 +- phpBB/includes/ucp/ucp_pm_viewmessage.php | 2 +- phpBB/includes/ucp/ucp_profile.php | 2 +- phpBB/includes/ucp/ucp_register.php | 2 +- phpBB/memberlist.php | 4 +- phpBB/phpbb/profilefields/manager.php | 408 +++++++++++++++++++++++ phpBB/phpbb/profilefields/profilefields.php | 408 ----------------------- phpBB/phpbb/profilefields/type/type_bool.php | 10 +- phpBB/phpbb/profilefields/type/type_date.php | 12 +- phpBB/phpbb/profilefields/type/type_dropdown.php | 14 +- phpBB/viewtopic.php | 2 +- 14 files changed, 440 insertions(+), 440 deletions(-) create mode 100644 phpBB/phpbb/profilefields/manager.php delete mode 100644 phpBB/phpbb/profilefields/profilefields.php diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 6c6ccedcea..8044bc8bc5 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -1,6 +1,6 @@ services: - profilefields: - class: \phpbb\profilefields\profilefields + profilefields.manager: + class: \phpbb\profilefields\manager arguments: - @auth - @dbal.conn @@ -30,7 +30,7 @@ services: class: \phpbb\profilefields\type\type_bool arguments: - @profilefields.lang_helper - - @profilefields + - @profilefields.manager - @request - @template - @user @@ -40,7 +40,7 @@ services: profilefields.type.date: class: \phpbb\profilefields\type\type_date arguments: - - @profilefields + - @profilefields.manager - @request - @template - @user @@ -51,7 +51,7 @@ services: class: \phpbb\profilefields\type\type_dropdown arguments: - @profilefields.lang_helper - - @profilefields + - @profilefields.manager - @request - @template - @user diff --git a/phpBB/includes/acp/acp_profile.php b/phpBB/includes/acp/acp_profile.php index c37d501aac..2fa1f38bd3 100644 --- a/phpBB/includes/acp/acp_profile.php +++ b/phpBB/includes/acp/acp_profile.php @@ -50,7 +50,7 @@ class acp_profile trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING); } - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $this->type_collection = $phpbb_container->get('profilefields.type_collection'); // Build Language array diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index beb6fa1960..4153d78ed4 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1344,7 +1344,7 @@ class acp_users include($phpbb_root_path . 'includes/functions_user.' . $phpEx); - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $cp_data = $cp_error = array(); diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index b2cd911a0d..17cdd0ce39 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -276,7 +276,7 @@ function user_add($user_row, $cp_data = false) { $cp_data['user_id'] = (int) $user_id; - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $sql = 'INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $cp->build_insert_sql_array($cp_data)); $db->sql_query($sql); diff --git a/phpBB/includes/ucp/ucp_pm_viewmessage.php b/phpBB/includes/ucp/ucp_pm_viewmessage.php index 78a0e010e6..119b84564a 100644 --- a/phpBB/includes/ucp/ucp_pm_viewmessage.php +++ b/phpBB/includes/ucp/ucp_pm_viewmessage.php @@ -61,7 +61,7 @@ function view_message($id, $mode, $folder_id, $msg_id, $folder, $message_row) // Load the custom profile fields if ($config['load_cpf_pm']) { - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $profile_fields = $cp->generate_profile_fields_template('grab', $author_id); } diff --git a/phpBB/includes/ucp/ucp_profile.php b/phpBB/includes/ucp/ucp_profile.php index 672588c51f..2fdd4bc905 100644 --- a/phpBB/includes/ucp/ucp_profile.php +++ b/phpBB/includes/ucp/ucp_profile.php @@ -256,7 +256,7 @@ class ucp_profile trigger_error('NO_AUTH_PROFILEINFO'); } - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $cp_data = $cp_error = array(); diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 333aa2816f..cb5f96133a 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -76,7 +76,7 @@ class ucp_register } } - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $error = $cp_data = $cp_error = array(); $s_hidden_fields = array(); diff --git a/phpBB/memberlist.php b/phpBB/memberlist.php index 84fbb8218a..019b1873a0 100644 --- a/phpBB/memberlist.php +++ b/phpBB/memberlist.php @@ -617,7 +617,7 @@ switch ($mode) $profile_fields = array(); if ($config['load_cpf_viewprofile']) { - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); $profile_fields = $cp->generate_profile_fields_template('grab', $user_id); $profile_fields = (isset($profile_fields[$user_id])) ? $cp->generate_profile_fields_template('show', false, $profile_fields[$user_id]) : array(); } @@ -1554,7 +1554,7 @@ switch ($mode) // Load custom profile fields if ($config['load_cpf_memberlist']) { - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); // Grab all profile fields from users in id cache for later use - similar to the poster cache $profile_fields_cache = $cp->generate_profile_fields_template('grab', $user_list); diff --git a/phpBB/phpbb/profilefields/manager.php b/phpBB/phpbb/profilefields/manager.php new file mode 100644 index 0000000000..9f64e1dcf7 --- /dev/null +++ b/phpBB/phpbb/profilefields/manager.php @@ -0,0 +1,408 @@ +auth = $auth; + $this->db = $db; + $this->request = $request; + $this->template = $template; + $this->user = $user; + + $this->fields_table = $fields_table; + $this->fields_language_table = $fields_language_table; + $this->fields_data_table = $fields_data_table; + } + + /** + * Setter for the type collection + * + * We need to set the type collection later, + * in order to avoid a circular dependency + * + * @param \phpbb\di\service_collection $type_collection + * @return null + */ + public function set_type_collection(\phpbb\di\service_collection $type_collection) + { + $this->type_collection = $type_collection; + } + + /** + * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) + * Called by ucp_profile and ucp_register + */ + public function generate_profile_fields($mode, $lang_id) + { + $sql_where = ''; + switch ($mode) + { + case 'register': + // If the field is required we show it on the registration page + $sql_where .= ' AND f.field_show_on_reg = 1'; + break; + + case 'profile': + // Show hidden fields to moderators/admins + if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) + { + $sql_where .= ' AND f.field_show_profile = 1'; + } + break; + + default: + trigger_error('Wrong profile mode specified', E_USER_ERROR); + break; + } + + $sql = 'SELECT l.*, f.* + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f + WHERE f.field_active = 1 + $sql_where + AND l.lang_id = " . (int) $lang_id . ' + AND l.field_id = f.field_id + ORDER BY f.field_order'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + // Return templated field + $tpl_snippet = $this->process_field_row('change', $row); + $profile_field = $this->type_collection[$row['field_type']]; + + $this->template->assign_block_vars('profile_fields', array( + 'LANG_NAME' => $this->user->lang($row['lang_name']), + 'LANG_EXPLAIN' => $this->user->lang($row['lang_explain']), + 'FIELD' => $tpl_snippet, + 'FIELD_ID' => $profile_field->get_field_ident($row), + 'S_REQUIRED' => ($row['field_required']) ? true : false, + )); + } + $this->db->sql_freeresult($result); + } + + /** + * Build profile cache, used for display + */ + protected function build_cache() + { + $this->profile_cache = array(); + + // Display hidden/no_view fields for admin/moderator + $sql = 'SELECT l.*, f.* + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f + WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' + AND f.field_active = 1 ' . + ((!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . ' + AND f.field_no_view = 0 + AND l.field_id = f.field_id + ORDER BY f.field_order'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $this->profile_cache[$row['field_ident']] = $row; + } + $this->db->sql_freeresult($result); + } + + /** + * Submit profile field for validation + */ + public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) + { + $sql_where = ''; + switch ($mode) + { + case 'register': + // If the field is required we show it on the registration page + $sql_where .= ' AND f.field_show_on_reg = 1'; + break; + + case 'profile': + // Show hidden fields to moderators/admins + if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) + { + $sql_where .= ' AND f.field_show_profile = 1'; + } + break; + + default: + trigger_error('Wrong profile mode specified', E_USER_ERROR); + break; + } + + $sql = 'SELECT l.*, f.* + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f + WHERE l.lang_id = ' . (int) $lang_id . " + AND f.field_active = 1 + $sql_where + AND l.field_id = f.field_id + ORDER BY f.field_order"; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $profile_field = $this->type_collection[$row['field_type']]; + $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); + $check_value = $cp_data['pf_' . $row['field_ident']]; + + if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false) + { + // If the result is not false, it's an error message + $cp_error[] = $cp_result; + } + } + $this->db->sql_freeresult($result); + } + + /** + * Update profile field data directly + */ + public function update_profile_field_data($user_id, $cp_data) + { + if (!sizeof($cp_data)) + { + return; + } + + $sql = 'UPDATE ' . $this->fields_data_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' + WHERE user_id = ' . (int) $user_id; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data['user_id'] = (int) $user_id; + + $this->db->sql_return_on_error(true); + + $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data); + $this->db->sql_query($sql); + + $this->db->sql_return_on_error(false); + } + } + + /** + * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled) + * 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 + */ + public function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) + { + if ($mode == 'grab') + { + if (!is_array($user_id)) + { + $user_id = array($user_id); + } + + if (!sizeof($this->profile_cache)) + { + $this->build_cache(); + } + + if (!sizeof($user_id)) + { + return array(); + } + + $sql = 'SELECT * + FROM ' . $this->fields_data_table . ' + WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_id)); + $result = $this->db->sql_query($sql); + + $field_data = array(); + while ($row = $this->db->sql_fetchrow($result)) + { + $field_data[$row['user_id']] = $row; + } + $this->db->sql_freeresult($result); + + $user_fields = array(); + + $user_ids = $user_id; + + // Go through the fields in correct order + foreach (array_keys($this->profile_cache) as $used_ident) + { + foreach ($field_data as $user_id => $row) + { + $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident]; + $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; + } + + foreach ($user_ids as $user_id) + { + if (!isset($user_fields[$user_id][$used_ident]) && $this->profile_cache[$used_ident]['field_show_novalue']) + { + $user_fields[$user_id][$used_ident]['value'] = ''; + $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; + } + } + } + + return $user_fields; + } + else if ($mode == 'show') + { + // $profile_row == $user_fields[$row['user_id']]; + $tpl_fields = array(); + $tpl_fields['row'] = $tpl_fields['blockrow'] = array(); + + foreach ($profile_row as $ident => $ident_ary) + { + $profile_field = $this->type_collection[$ident_ary['data']['field_type']]; + $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']); + + if ($value === NULL) + { + continue; + } + + $tpl_fields['row'] += array( + 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value, + 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'], + 'PROFILE_' . strtoupper($ident) . '_NAME' => $this->user->lang($ident_ary['data']['lang_name']), + 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $this->user->lang($ident_ary['data']['lang_explain']), + + 'S_PROFILE_' . strtoupper($ident) => true, + ); + + $tpl_fields['blockrow'][] = array( + 'PROFILE_FIELD_VALUE' => $value, + 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'], + 'PROFILE_FIELD_NAME' => $this->user->lang($ident_ary['data']['lang_name']), + 'PROFILE_FIELD_EXPLAIN' => $this->user->lang($ident_ary['data']['lang_explain']), + + 'S_PROFILE_' . strtoupper($ident) => true, + ); + } + + return $tpl_fields; + } + else + { + trigger_error('Wrong mode for custom profile', E_USER_ERROR); + } + } + + /** + * Return Templated value/field. Possible values for $mode are: + * change == user is able to set/enter profile values; preview == just show the value + */ + public function process_field_row($mode, $profile_row) + { + $preview_options = ($mode == 'preview') ? $this->vars['lang_options'] : false; + + // set template filename + $this->template->set_filenames(array( + 'cp_body' => 'custom_profile_fields.html', + )); + + // empty previously filled blockvars + foreach ($this->type_collection as $field_key => $field_type) + { + $this->template->destroy_block_vars($field_type->get_name_short()); + } + + // Assign template variables + $profile_field = $this->type_collection[$profile_row['field_type']]; + $profile_field->generate_field($profile_row, $preview_options); + + // Return templated data + return $this->template->assign_display('cp_body'); + } + + /** + * Build Array for user insertion into custom profile fields table + */ + public function build_insert_sql_array($cp_data) + { + $sql_not_in = array(); + foreach ($cp_data as $key => $null) + { + $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key; + } + + $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value + FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f + WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' + ' . ((sizeof($sql_not_in)) ? ' AND ' . $this->db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . ' + AND l.field_id = f.field_id'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $profile_field = $this->type_collection[$row['field_type']]; + $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row); + } + $this->db->sql_freeresult($result); + + return $cp_data; + } +} diff --git a/phpBB/phpbb/profilefields/profilefields.php b/phpBB/phpbb/profilefields/profilefields.php deleted file mode 100644 index aec2756211..0000000000 --- a/phpBB/phpbb/profilefields/profilefields.php +++ /dev/null @@ -1,408 +0,0 @@ -auth = $auth; - $this->db = $db; - $this->request = $request; - $this->template = $template; - $this->user = $user; - - $this->fields_table = $fields_table; - $this->fields_language_table = $fields_language_table; - $this->fields_data_table = $fields_data_table; - } - - /** - * Setter for the type collection - * - * We need to set the type collection later, - * in order to avoid a circular dependency - * - * @param \phpbb\di\service_collection $type_collection - * @return null - */ - public function set_type_collection(\phpbb\di\service_collection $type_collection) - { - $this->type_collection = $type_collection; - } - - /** - * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) - * Called by ucp_profile and ucp_register - */ - public function generate_profile_fields($mode, $lang_id) - { - $sql_where = ''; - switch ($mode) - { - case 'register': - // If the field is required we show it on the registration page - $sql_where .= ' AND f.field_show_on_reg = 1'; - break; - - case 'profile': - // Show hidden fields to moderators/admins - if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) - { - $sql_where .= ' AND f.field_show_profile = 1'; - } - break; - - default: - trigger_error('Wrong profile mode specified', E_USER_ERROR); - break; - } - - $sql = 'SELECT l.*, f.* - FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . " f - WHERE f.field_active = 1 - $sql_where - AND l.lang_id = " . (int) $lang_id . ' - AND l.field_id = f.field_id - ORDER BY f.field_order'; - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - // Return templated field - $tpl_snippet = $this->process_field_row('change', $row); - $profile_field = $this->type_collection[$row['field_type']]; - - $this->template->assign_block_vars('profile_fields', array( - 'LANG_NAME' => $this->user->lang($row['lang_name']), - 'LANG_EXPLAIN' => $this->user->lang($row['lang_explain']), - 'FIELD' => $tpl_snippet, - 'FIELD_ID' => $profile_field->get_field_ident($row), - 'S_REQUIRED' => ($row['field_required']) ? true : false, - )); - } - $this->db->sql_freeresult($result); - } - - /** - * Build profile cache, used for display - */ - protected function build_cache() - { - $this->profile_cache = array(); - - // Display hidden/no_view fields for admin/moderator - $sql = 'SELECT l.*, f.* - FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f - WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' - AND f.field_active = 1 ' . - ((!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . ' - AND f.field_no_view = 0 - AND l.field_id = f.field_id - ORDER BY f.field_order'; - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - $this->profile_cache[$row['field_ident']] = $row; - } - $this->db->sql_freeresult($result); - } - - /** - * Submit profile field for validation - */ - public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error) - { - $sql_where = ''; - switch ($mode) - { - case 'register': - // If the field is required we show it on the registration page - $sql_where .= ' AND f.field_show_on_reg = 1'; - break; - - case 'profile': - // Show hidden fields to moderators/admins - if (!$this->auth->acl_gets('a_', 'm_') && !$this->auth->acl_getf_global('m_')) - { - $sql_where .= ' AND f.field_show_profile = 1'; - } - break; - - default: - trigger_error('Wrong profile mode specified', E_USER_ERROR); - break; - } - - $sql = 'SELECT l.*, f.* - FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f - WHERE l.lang_id = ' . (int) $lang_id . " - AND f.field_active = 1 - $sql_where - AND l.field_id = f.field_id - ORDER BY f.field_order"; - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - $profile_field = $this->type_collection[$row['field_type']]; - $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row); - $check_value = $cp_data['pf_' . $row['field_ident']]; - - if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false) - { - // If the result is not false, it's an error message - $cp_error[] = $cp_result; - } - } - $this->db->sql_freeresult($result); - } - - /** - * Update profile field data directly - */ - public function update_profile_field_data($user_id, $cp_data) - { - if (!sizeof($cp_data)) - { - return; - } - - $sql = 'UPDATE ' . $this->fields_data_table . ' - SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' - WHERE user_id = ' . (int) $user_id; - $this->db->sql_query($sql); - - if (!$this->db->sql_affectedrows()) - { - $cp_data['user_id'] = (int) $user_id; - - $this->db->sql_return_on_error(true); - - $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data); - $this->db->sql_query($sql); - - $this->db->sql_return_on_error(false); - } - } - - /** - * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled) - * 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 - */ - public function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false) - { - if ($mode == 'grab') - { - if (!is_array($user_id)) - { - $user_id = array($user_id); - } - - if (!sizeof($this->profile_cache)) - { - $this->build_cache(); - } - - if (!sizeof($user_id)) - { - return array(); - } - - $sql = 'SELECT * - FROM ' . $this->fields_data_table . ' - WHERE ' . $this->db->sql_in_set('user_id', array_map('intval', $user_id)); - $result = $this->db->sql_query($sql); - - $field_data = array(); - while ($row = $this->db->sql_fetchrow($result)) - { - $field_data[$row['user_id']] = $row; - } - $this->db->sql_freeresult($result); - - $user_fields = array(); - - $user_ids = $user_id; - - // Go through the fields in correct order - foreach (array_keys($this->profile_cache) as $used_ident) - { - foreach ($field_data as $user_id => $row) - { - $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident]; - $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; - } - - foreach ($user_ids as $user_id) - { - if (!isset($user_fields[$user_id][$used_ident]) && $this->profile_cache[$used_ident]['field_show_novalue']) - { - $user_fields[$user_id][$used_ident]['value'] = ''; - $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident]; - } - } - } - - return $user_fields; - } - else if ($mode == 'show') - { - // $profile_row == $user_fields[$row['user_id']]; - $tpl_fields = array(); - $tpl_fields['row'] = $tpl_fields['blockrow'] = array(); - - foreach ($profile_row as $ident => $ident_ary) - { - $profile_field = $this->type_collection[$ident_ary['data']['field_type']]; - $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']); - - if ($value === NULL) - { - continue; - } - - $tpl_fields['row'] += array( - 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value, - 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_' . strtoupper($ident) . '_NAME' => $this->user->lang($ident_ary['data']['lang_name']), - 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $this->user->lang($ident_ary['data']['lang_explain']), - - 'S_PROFILE_' . strtoupper($ident) => true, - ); - - $tpl_fields['blockrow'][] = array( - 'PROFILE_FIELD_VALUE' => $value, - 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'], - 'PROFILE_FIELD_NAME' => $this->user->lang($ident_ary['data']['lang_name']), - 'PROFILE_FIELD_EXPLAIN' => $this->user->lang($ident_ary['data']['lang_explain']), - - 'S_PROFILE_' . strtoupper($ident) => true, - ); - } - - return $tpl_fields; - } - else - { - trigger_error('Wrong mode for custom profile', E_USER_ERROR); - } - } - - /** - * Return Templated value/field. Possible values for $mode are: - * change == user is able to set/enter profile values; preview == just show the value - */ - public function process_field_row($mode, $profile_row) - { - $preview_options = ($mode == 'preview') ? $this->vars['lang_options'] : false; - - // set template filename - $this->template->set_filenames(array( - 'cp_body' => 'custom_profile_fields.html', - )); - - // empty previously filled blockvars - foreach ($this->type_collection as $field_key => $field_type) - { - $this->template->destroy_block_vars($field_type->get_name_short()); - } - - // Assign template variables - $profile_field = $this->type_collection[$profile_row['field_type']]; - $profile_field->generate_field($profile_row, $preview_options); - - // Return templated data - return $this->template->assign_display('cp_body'); - } - - /** - * Build Array for user insertion into custom profile fields table - */ - public function build_insert_sql_array($cp_data) - { - $sql_not_in = array(); - foreach ($cp_data as $key => $null) - { - $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key; - } - - $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value - FROM ' . $this->fields_language_table . ' l, ' . $this->fields_table . ' f - WHERE l.lang_id = ' . $this->user->get_iso_lang_id() . ' - ' . ((sizeof($sql_not_in)) ? ' AND ' . $this->db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . ' - AND l.field_id = f.field_id'; - $result = $this->db->sql_query($sql); - - while ($row = $this->db->sql_fetchrow($result)) - { - $profile_field = $this->type_collection[$row['field_type']]; - $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row); - } - $this->db->sql_freeresult($result); - - return $cp_data; - } -} diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 68d17fb4cf..6b34d6923f 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -19,7 +19,7 @@ class type_bool extends type_base /** * Profile fields object - * @var \phpbb\profilefields\profilefields + * @var \phpbb\profilefields\manager */ protected $profilefields; @@ -45,16 +45,16 @@ class type_bool extends type_base * Construct * * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper - * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; - $this->profilefields = $profilefields; + $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -88,7 +88,7 @@ class type_bool extends type_base $options = array( 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)), + 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row)), ); return $options; diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index 335a63b3f4..a71e6fde68 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -13,9 +13,9 @@ class type_date extends type_base { /** * Profile fields object - * @var \phpbb\profilefields\profilefields + * @var \phpbb\profilefields\manager */ - protected $profilefields; + protected $manager; /** * Request object @@ -38,15 +38,15 @@ class type_date extends type_base /** * Construct * - * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { - $this->profilefields = $profilefields; + $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -87,7 +87,7 @@ class type_date extends type_base } $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row)), + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row)), 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => ''), ); diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index 5596e8f172..aee19fda30 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -19,9 +19,9 @@ class type_dropdown extends type_base /** * Profile fields object - * @var \phpbb\profilefields\profilefields + * @var \phpbb\profilefields\manager */ - protected $profilefields; + protected $manager; /** * Request object @@ -45,16 +45,16 @@ class type_dropdown extends type_base * Construct * * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper - * @param \phpbb\profilefields\profilefields $profilefields Profile fields object + * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\profilefields $profilefields, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; - $this->profilefields = $profilefields; + $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -91,8 +91,8 @@ class type_dropdown extends type_base $profile_row[1]['field_default_value'] = $field_data['field_novalue']; $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->profilefields->process_field_row('preview', $profile_row[1])), + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row[0])), + 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row[1])), ); return $options; diff --git a/phpBB/viewtopic.php b/phpBB/viewtopic.php index 396de132cb..227cfdd029 100644 --- a/phpBB/viewtopic.php +++ b/phpBB/viewtopic.php @@ -1265,7 +1265,7 @@ $db->sql_freeresult($result); // Load custom profile fields if ($config['load_cpf_viewtopic']) { - $cp = $phpbb_container->get('profilefields'); + $cp = $phpbb_container->get('profilefields.manager'); // Grab all profile fields from users in id cache for later use - similar to the poster cache $profile_fields_tmp = $cp->generate_profile_fields_template('grab', $id_cache); -- cgit v1.2.1 From cacd43bfbd547d711bfdd4c424adfef9d20528e7 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 16:05:01 +0100 Subject: [ticket/11201] Remove dependency from types on the manager PHPBB3-11201 --- phpBB/config/profilefields.yml | 21 ++++------ phpBB/phpbb/profilefields/manager.php | 53 +++++------------------- phpBB/phpbb/profilefields/type/type_base.php | 22 ++++++++++ phpBB/phpbb/profilefields/type/type_bool.php | 12 +----- phpBB/phpbb/profilefields/type/type_date.php | 12 +----- phpBB/phpbb/profilefields/type/type_dropdown.php | 14 ++----- 6 files changed, 48 insertions(+), 86 deletions(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index 8044bc8bc5..b73d4b64c0 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -1,20 +1,20 @@ services: profilefields.manager: - class: \phpbb\profilefields\manager + class: phpbb\profilefields\manager arguments: - @auth - @dbal.conn - @request - @template + - @profilefields.type_collection - @user - %tables.profile_fields% - %tables.profile_fields_language% - %tables.profile_fields_data% calls: - - [set_type_collection, [@profilefields.type_collection]] profilefields.lang_helper: - class: \phpbb\profilefields\lang_helper + class: phpbb\profilefields\lang_helper arguments: - @dbal.conn - %tables.profile_fields_options_language% @@ -27,10 +27,9 @@ services: - { name: service_collection, tag: profilefield.type } profilefields.type.bool: - class: \phpbb\profilefields\type\type_bool + class: phpbb\profilefields\type\type_bool arguments: - @profilefields.lang_helper - - @profilefields.manager - @request - @template - @user @@ -38,9 +37,8 @@ services: - { name: profilefield.type } profilefields.type.date: - class: \phpbb\profilefields\type\type_date + class: phpbb\profilefields\type\type_date arguments: - - @profilefields.manager - @request - @template - @user @@ -48,10 +46,9 @@ services: - { name: profilefield.type } profilefields.type.dropdown: - class: \phpbb\profilefields\type\type_dropdown + class: phpbb\profilefields\type\type_dropdown arguments: - @profilefields.lang_helper - - @profilefields.manager - @request - @template - @user @@ -59,7 +56,7 @@ services: - { name: profilefield.type } profilefields.type.int: - class: \phpbb\profilefields\type\type_int + class: phpbb\profilefields\type\type_int arguments: - @request - @template @@ -68,7 +65,7 @@ services: - { name: profilefield.type } profilefields.type.string: - class: \phpbb\profilefields\type\type_string + class: phpbb\profilefields\type\type_string arguments: - @request - @template @@ -77,7 +74,7 @@ services: - { name: profilefield.type } profilefields.type.text: - class: \phpbb\profilefields\type\type_text + class: phpbb\profilefields\type\type_text arguments: - @request - @template diff --git a/phpBB/phpbb/profilefields/manager.php b/phpBB/phpbb/profilefields/manager.php index 9f64e1dcf7..7564c920c9 100644 --- a/phpBB/phpbb/profilefields/manager.php +++ b/phpBB/phpbb/profilefields/manager.php @@ -39,6 +39,12 @@ class manager */ protected $template; + /** + * Service Collection object + * @var \phpbb\di\service_collection + */ + protected $type_collection; + /** * User object * @var \phpbb\user @@ -60,17 +66,19 @@ class manager * @param \phpbb\db\driver\driver $db Database object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object + * @param \phpbb\di\service_collection $type_collection * @param \phpbb\user $user User object * @param string $fields_table * @param string $fields_language_table * @param string $fields_data_table */ - public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $fields_table, $fields_language_table, $fields_data_table) + public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver $db, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\di\service_collection $type_collection, \phpbb\user $user, $fields_table, $fields_language_table, $fields_data_table) { $this->auth = $auth; $this->db = $db; $this->request = $request; $this->template = $template; + $this->type_collection = $type_collection; $this->user = $user; $this->fields_table = $fields_table; @@ -78,20 +86,6 @@ class manager $this->fields_data_table = $fields_data_table; } - /** - * Setter for the type collection - * - * We need to set the type collection later, - * in order to avoid a circular dependency - * - * @param \phpbb\di\service_collection $type_collection - * @return null - */ - public function set_type_collection(\phpbb\di\service_collection $type_collection) - { - $this->type_collection = $type_collection; - } - /** * Assign editable fields to template, mode can be profile (for profile change) or register (for registration) * Called by ucp_profile and ucp_register @@ -131,8 +125,8 @@ class manager while ($row = $this->db->sql_fetchrow($result)) { // Return templated field - $tpl_snippet = $this->process_field_row('change', $row); $profile_field = $this->type_collection[$row['field_type']]; + $tpl_snippet = $profile_field->process_field_row('change', $row); $this->template->assign_block_vars('profile_fields', array( 'LANG_NAME' => $this->user->lang($row['lang_name']), @@ -351,33 +345,6 @@ class manager } } - /** - * Return Templated value/field. Possible values for $mode are: - * change == user is able to set/enter profile values; preview == just show the value - */ - public function process_field_row($mode, $profile_row) - { - $preview_options = ($mode == 'preview') ? $this->vars['lang_options'] : false; - - // set template filename - $this->template->set_filenames(array( - 'cp_body' => 'custom_profile_fields.html', - )); - - // empty previously filled blockvars - foreach ($this->type_collection as $field_key => $field_type) - { - $this->template->destroy_block_vars($field_type->get_name_short()); - } - - // Assign template variables - $profile_field = $this->type_collection[$profile_row['field_type']]; - $profile_field->generate_field($profile_row, $preview_options); - - // Return templated data - return $this->template->assign_display('cp_body'); - } - /** * Build Array for user insertion into custom profile fields table */ diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 6961d208e9..248a89e5ac 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -142,4 +142,26 @@ abstract class type_base implements type_interface { return; } + + /** + * Return templated value/field. Possible values for $mode are: + * change == user is able to set/enter profile values; preview == just show the value + */ + public function process_field_row($mode, $profile_row) + { + $preview_options = ($mode == 'preview') ? $profile_row['lang_options'] : false; + + // set template filename + $this->template->set_filenames(array( + 'cp_' . $this->get_name_short() . '_body' => 'custom_profile_fields.html', + )); + + // empty previously filled blockvars + $this->template->destroy_block_vars($this->get_name_short()); + + // Assign template variables + $this->generate_field($profile_row, $preview_options); + + return $this->template->assign_display('cp_' . $this->get_name_short() . '_body'); + } } diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 6b34d6923f..47a5ed4992 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -17,12 +17,6 @@ class type_bool extends type_base */ protected $lang_helper; - /** - * Profile fields object - * @var \phpbb\profilefields\manager - */ - protected $profilefields; - /** * Request object * @var \phpbb\request\request @@ -45,16 +39,14 @@ class type_bool extends type_base * Construct * * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper - * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; - $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -88,7 +80,7 @@ class type_bool extends type_base $options = array( 0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => ''), - 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row)), + 1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), ); return $options; diff --git a/phpBB/phpbb/profilefields/type/type_date.php b/phpBB/phpbb/profilefields/type/type_date.php index a71e6fde68..409c2e7be0 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -11,12 +11,6 @@ namespace phpbb\profilefields\type; class type_date extends type_base { - /** - * Profile fields object - * @var \phpbb\profilefields\manager - */ - protected $manager; - /** * Request object * @var \phpbb\request\request @@ -38,15 +32,13 @@ class type_date extends type_base /** * Construct * - * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { - $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -87,7 +79,7 @@ class type_date extends type_base } $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row)), + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => ''), ); diff --git a/phpBB/phpbb/profilefields/type/type_dropdown.php b/phpBB/phpbb/profilefields/type/type_dropdown.php index aee19fda30..9a6545249d 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -17,12 +17,6 @@ class type_dropdown extends type_base */ protected $lang_helper; - /** - * Profile fields object - * @var \phpbb\profilefields\manager - */ - protected $manager; - /** * Request object * @var \phpbb\request\request @@ -45,16 +39,14 @@ class type_dropdown extends type_base * Construct * * @param \phpbb\profilefields\lang_helper $lang_helper Profile fields language helper - * @param \phpbb\profilefields\manager $manager Profile fields object * @param \phpbb\request\request $request Request object * @param \phpbb\template\template $template Template object * @param \phpbb\user $user User object * @param string $language_table Table where the language strings are stored */ - public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\profilefields\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) + public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) { $this->lang_helper = $lang_helper; - $this->manager = $manager; $this->request = $request; $this->template = $template; $this->user = $user; @@ -91,8 +83,8 @@ class type_dropdown extends type_base $profile_row[1]['field_default_value'] = $field_data['field_novalue']; $options = array( - 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row[0])), - 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->manager->process_field_row('preview', $profile_row[1])), + 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])), + 1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])), ); return $options; -- cgit v1.2.1 From cbad102f88d285ae64e65bf10cddfdf2c615f138 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 16:20:02 +0100 Subject: [ticket/11201] Split template file into multiple files PHPBB3-11201 --- phpBB/phpbb/profilefields/type/type_base.php | 12 +++++++-- .../prosilver/template/custom_profile_fields.html | 31 ---------------------- .../prosilver/template/profilefields/bool.html | 7 +++++ .../prosilver/template/profilefields/date.html | 5 ++++ .../prosilver/template/profilefields/dropdown.html | 5 ++++ .../prosilver/template/profilefields/int.html | 3 +++ .../prosilver/template/profilefields/string.html | 3 +++ .../prosilver/template/profilefields/text.html | 3 +++ 8 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 phpBB/styles/prosilver/template/custom_profile_fields.html create mode 100644 phpBB/styles/prosilver/template/profilefields/bool.html create mode 100644 phpBB/styles/prosilver/template/profilefields/date.html create mode 100644 phpBB/styles/prosilver/template/profilefields/dropdown.html create mode 100644 phpBB/styles/prosilver/template/profilefields/int.html create mode 100644 phpBB/styles/prosilver/template/profilefields/string.html create mode 100644 phpBB/styles/prosilver/template/profilefields/text.html diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 248a89e5ac..95e9b8768b 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -60,6 +60,14 @@ abstract class type_base implements type_interface return 'profilefields.type.' . $this->get_name_short(); } + /** + * {@inheritDoc} + */ + public function get_template_filename() + { + return 'profilefields/' . $this->get_name_short() . '.html'; + } + /** * {@inheritDoc} */ @@ -153,7 +161,7 @@ abstract class type_base implements type_interface // set template filename $this->template->set_filenames(array( - 'cp_' . $this->get_name_short() . '_body' => 'custom_profile_fields.html', + 'cp_body' => $this->get_template_filename(), )); // empty previously filled blockvars @@ -162,6 +170,6 @@ abstract class type_base implements type_interface // Assign template variables $this->generate_field($profile_row, $preview_options); - return $this->template->assign_display('cp_' . $this->get_name_short() . '_body'); + return $this->template->assign_display('cp_body'); } } diff --git a/phpBB/styles/prosilver/template/custom_profile_fields.html b/phpBB/styles/prosilver/template/custom_profile_fields.html deleted file mode 100644 index 764cac7b3a..0000000000 --- a/phpBB/styles/prosilver/template/custom_profile_fields.html +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - checked="checked" /> - - - - - - - - - - - - diff --git a/phpBB/styles/prosilver/template/profilefields/bool.html b/phpBB/styles/prosilver/template/profilefields/bool.html new file mode 100644 index 0000000000..f1d7ba75f4 --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/bool.html @@ -0,0 +1,7 @@ + + + + + checked="checked" /> + + diff --git a/phpBB/styles/prosilver/template/profilefields/date.html b/phpBB/styles/prosilver/template/profilefields/date.html new file mode 100644 index 0000000000..5d5bc04ed6 --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/date.html @@ -0,0 +1,5 @@ + + + + + diff --git a/phpBB/styles/prosilver/template/profilefields/dropdown.html b/phpBB/styles/prosilver/template/profilefields/dropdown.html new file mode 100644 index 0000000000..243b7039da --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/dropdown.html @@ -0,0 +1,5 @@ + + + diff --git a/phpBB/styles/prosilver/template/profilefields/int.html b/phpBB/styles/prosilver/template/profilefields/int.html new file mode 100644 index 0000000000..a6f9a0a49e --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/int.html @@ -0,0 +1,3 @@ + + + diff --git a/phpBB/styles/prosilver/template/profilefields/string.html b/phpBB/styles/prosilver/template/profilefields/string.html new file mode 100644 index 0000000000..cf457d39ad --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/string.html @@ -0,0 +1,3 @@ + + + diff --git a/phpBB/styles/prosilver/template/profilefields/text.html b/phpBB/styles/prosilver/template/profilefields/text.html new file mode 100644 index 0000000000..f54c63925c --- /dev/null +++ b/phpBB/styles/prosilver/template/profilefields/text.html @@ -0,0 +1,3 @@ + + + -- cgit v1.2.1 From a59d6e94a5bc30e99be5535632ccf2046817dd30 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 16:32:20 +0100 Subject: [ticket/11201] Remove empty calls section from .yml PHPBB3-11201 --- phpBB/config/profilefields.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/phpBB/config/profilefields.yml b/phpBB/config/profilefields.yml index b73d4b64c0..5a861a4b56 100644 --- a/phpBB/config/profilefields.yml +++ b/phpBB/config/profilefields.yml @@ -11,7 +11,6 @@ services: - %tables.profile_fields% - %tables.profile_fields_language% - %tables.profile_fields_data% - calls: profilefields.lang_helper: class: phpbb\profilefields\lang_helper -- cgit v1.2.1