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'] . '