[ticket/11201] Change type from integer to service name

PHPBB3-11201
This commit is contained in:
Joas Schilling 2014-01-17 19:33:29 +01:00
parent 0ec6af38a9
commit aa2f0a652f
12 changed files with 210 additions and 34 deletions

View file

@ -24,6 +24,7 @@ class acp_profile
var $edit_lang_id; var $edit_lang_id;
var $lang_defs; var $lang_defs;
protected $type_collection;
function main($id, $mode) function main($id, $mode)
{ {
@ -50,6 +51,7 @@ class acp_profile
} }
$cp = $phpbb_container->get('profilefields'); $cp = $phpbb_container->get('profilefields');
$this->type_collection = $phpbb_container->get('profilefields.type_collection');
// Build Language array // Build Language array
// Based on this, we decide which elements need to be edited later and which language items are missing // 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']; $this->edit_lang_id = $field_row['lang_id'];
} }
$field_type = $field_row['field_type']; $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 // Get language entries
$sql = 'SELECT * $sql = 'SELECT *
@ -365,14 +367,14 @@ class acp_profile
// We are adding a new field, define basic params // We are adding a new field, define basic params
$lang_options = $field_row = array(); $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); 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_row = array_merge($profile_field->get_default_option_values(), array(
'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))), 'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))),
'field_required' => 0, 'field_required' => 0,
@ -623,7 +625,7 @@ class acp_profile
'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false, 'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false,
'L_LANG_SPECIFIC' => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']), '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'], 'FIELD_IDENT' => $cp->vars['field_ident'],
'LANG_NAME' => $cp->vars['lang_name'], 'LANG_NAME' => $cp->vars['lang_name'],
'LANG_EXPLAIN' => $cp->vars['lang_explain'], 'LANG_EXPLAIN' => $cp->vars['lang_explain'],
@ -707,9 +709,10 @@ class acp_profile
$s_one_need_edit = true; $s_one_need_edit = true;
} }
$profile_field = $this->type_collection[$row['field_type']];
$template->assign_block_vars('fields', array( $template->assign_block_vars('fields', array(
'FIELD_IDENT' => $row['field_ident'], '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], 'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang],
'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&action=$active_value&field_id=$id", 'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&action=$active_value&field_id=$id",
@ -731,15 +734,15 @@ class acp_profile
} }
$s_select_type = ''; $s_select_type = '';
foreach ($cp->profile_types as $key => $value) foreach ($this->type_collection as $key => $profile_field)
{ {
$s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($value)] . '</option>'; $s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($profile_field->get_name())] . '</option>';
} }
$template->assign_vars(array( $template->assign_vars(array(
'U_ACTION' => $this->u_action, '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); $db->sql_freeresult($result);
$type_collection = $phpbb_container->get('profilefields.type_collection'); $profile_field = $this->type_collection[$field_type];
$profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]]; $options = $profile_field->get_language_options($cp->vars);
$options = $profile_type->get_language_options($cp->vars);
$lang_options = array(); $lang_options = array();
@ -906,8 +908,7 @@ class acp_profile
$db->sql_query($sql); $db->sql_query($sql);
} }
$type_collection = $phpbb_container->get('profilefields.type_collection'); $profile_field = $this->type_collection[$field_type];
$profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]];
if ($action == 'create') if ($action == 'create')
{ {
@ -915,7 +916,7 @@ class acp_profile
$db_tools = $phpbb_container->get('dbal.tools'); $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); $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) foreach ($cp->vars['lang_options'] as $option_id => $value)
{ {
$sql_ary = array( $sql_ary = array(
'field_type' => (int) $field_type, 'field_type' => $field_type,
'lang_value' => $value 'lang_value' => $value
); );
@ -1045,7 +1046,7 @@ class acp_profile
'field_id' => (int) $field_id, 'field_id' => (int) $field_id,
'lang_id' => (int) $lang_id, 'lang_id' => (int) $lang_id,
'option_id' => (int) $option_id, 'option_id' => (int) $option_id,
'field_type' => (int) $field_type, 'field_type' => $field_type,
'lang_value' => $value 'lang_value' => $value
); );
} }

View file

@ -0,0 +1,106 @@
<?php
/**
*
* @package migration
* @copyright (c) 2014 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
*
*/
namespace phpbb\db\migration\data\v310;
class profilefield_types extends \phpbb\db\migration\migration
{
static public function depends_on()
{
return array(
'\phpbb\db\migration\data\v310\alpha2',
);
}
public function update_schema()
{
return array(
'change_columns' => 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;
}
}
}

View file

@ -65,7 +65,7 @@ class lang_helper
FROM ' . PROFILE_FIELDS_LANG_TABLE . " FROM ' . PROFILE_FIELDS_LANG_TABLE . "
WHERE field_id = $field_id WHERE field_id = $field_id
AND lang_id = $lang_id AND lang_id = $lang_id
AND field_type = $field_type AND field_type = '" . $this->db->sql_escape($field_type) . "'
ORDER BY option_id"; ORDER BY option_id";
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);

View file

@ -82,10 +82,9 @@ class profilefields
while ($row = $this->db->sql_fetchrow($result)) while ($row = $this->db->sql_fetchrow($result))
{ {
// Return templated field // Return templated field
$tpl_snippet = $this->process_field_row('change', $row); $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( $this->template->assign_block_vars('profile_fields', array(
'LANG_NAME' => $row['lang_name'], 'LANG_NAME' => $row['lang_name'],
@ -160,7 +159,7 @@ class profilefields
while ($row = $this->db->sql_fetchrow($result)) 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); $cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row);
$check_value = $cp_data['pf_' . $row['field_ident']]; $check_value = $cp_data['pf_' . $row['field_ident']];
@ -300,7 +299,7 @@ class profilefields
foreach ($profile_row as $ident => $ident_ary) 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']); $value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']);
if ($value === NULL) if ($value === NULL)
@ -349,13 +348,13 @@ class profilefields
)); ));
// empty previously filled blockvars // 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 // 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); $profile_field->generate_field($profile_row, $preview_options);
// Return templated data // Return templated data
@ -382,7 +381,7 @@ class profilefields
while ($row = $this->db->sql_fetchrow($result)) 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); $cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row);
} }
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);

View file

@ -21,6 +21,14 @@ abstract class type_base implements type_interface
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_service_name()
{
return 'profilefields.type.' . $this->get_name();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -23,6 +23,14 @@ class type_bool extends type_base
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'bool';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -36,7 +44,7 @@ class type_bool extends type_base
'lang_id' => $default_lang_id, 'lang_id' => $default_lang_id,
'field_default_value' => $field_data['field_default_value'], 'field_default_value' => $field_data['field_default_value'],
'field_ident' => 'field_default_value', 'field_ident' => 'field_default_value',
'field_type' => FIELD_BOOL, 'field_type' => $this->get_service_name(),
'field_length' => $field_data['field_length'], 'field_length' => $field_data['field_length'],
'lang_options' => $field_data['lang_options'] '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)) 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']); $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']);

View file

@ -22,6 +22,14 @@ class type_date extends type_base
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'date';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -34,7 +42,7 @@ class type_date extends type_base
'lang_id' => $default_lang_id, 'lang_id' => $default_lang_id,
'field_default_value' => $field_data['field_default_value'], 'field_default_value' => $field_data['field_default_value'],
'field_ident' => 'field_default_value', 'field_ident' => 'field_default_value',
'field_type' => FIELD_DATE, 'field_type' => $this->get_service_name(),
'field_length' => $field_data['field_length'], 'field_length' => $field_data['field_length'],
); );

View file

@ -23,6 +23,14 @@ class type_dropdown extends type_base
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'dropdown';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -36,7 +44,7 @@ class type_dropdown extends type_base
'lang_id' => $default_lang_id, 'lang_id' => $default_lang_id,
'field_default_value' => $field_data['field_default_value'], 'field_default_value' => $field_data['field_default_value'],
'field_ident' => 'field_default_value', 'field_ident' => 'field_default_value',
'field_type' => FIELD_DROPDOWN, 'field_type' => $this->get_service_name(),
'lang_options' => $field_data['lang_options'], 'lang_options' => $field_data['lang_options'],
); );
@ -95,7 +103,7 @@ class type_dropdown extends type_base
// retrieve option lang data if necessary // retrieve option lang data if necessary
if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], 1)) 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)) 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']; $lang_id = $field_data['lang_id'];
if (!$this->lang_helper->is_set($field_id, $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']) 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)) 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; $profile_row['field_value'] = (int) $value;

View file

@ -21,6 +21,14 @@ class type_int extends type_base
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'int';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -11,6 +11,20 @@ namespace phpbb\profilefields\type;
interface type_interface 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 * Get dropdown options for second step in ACP
* *

View file

@ -21,6 +21,14 @@ class type_string extends type_string_common
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'string';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -21,6 +21,14 @@ class type_text extends type_string_common
$this->user = $user; $this->user = $user;
} }
/**
* {@inheritDoc}
*/
public function get_name()
{
return 'text';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */