1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-09 09:05:23 +02:00

[ticket/11201] Allow manipulating the intended variables with the type class

PHPBB3-11201
This commit is contained in:
Joas Schilling 2014-01-17 13:35:38 +01:00
parent ae38cfaa70
commit 67f477fc8f
8 changed files with 180 additions and 99 deletions

View File

@ -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)

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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);
}
}