Skip to content

Commit aa2f0a6

Browse files
committed
[ticket/11201] Change type from integer to service name
PHPBB3-11201
1 parent 0ec6af3 commit aa2f0a6

12 files changed

Lines changed: 210 additions & 34 deletions

File tree

phpBB/includes/acp/acp_profile.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class acp_profile
2424

2525
var $edit_lang_id;
2626
var $lang_defs;
27+
protected $type_collection;
2728

2829
function main($id, $mode)
2930
{
@@ -50,6 +51,7 @@ function main($id, $mode)
5051
}
5152

5253
$cp = $phpbb_container->get('profilefields');
54+
$this->type_collection = $phpbb_container->get('profilefields.type_collection');
5355

5456
// Build Language array
5557
// Based on this, we decide which elements need to be edited later and which language items are missing
@@ -341,7 +343,7 @@ function main($id, $mode)
341343
$this->edit_lang_id = $field_row['lang_id'];
342344
}
343345
$field_type = $field_row['field_type'];
344-
$profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]);
346+
$profile_field = $this->type_collection[$field_type];
345347

346348
// Get language entries
347349
$sql = 'SELECT *
@@ -365,14 +367,14 @@ function main($id, $mode)
365367
// We are adding a new field, define basic params
366368
$lang_options = $field_row = array();
367369

368-
$field_type = request_var('field_type', 0);
370+
$field_type = request_var('field_type', '');
369371

370-
if (!$field_type)
372+
if (!isset($this->type_collection[$field_type]))
371373
{
372374
trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
373375
}
374376

375-
$profile_field = $phpbb_container->get('profilefields.type.' . $cp->profile_types[$field_type]);
377+
$profile_field = $this->type_collection[$field_type];
376378
$field_row = array_merge($profile_field->get_default_option_values(), array(
377379
'field_ident' => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))),
378380
'field_required' => 0,
@@ -623,7 +625,7 @@ function main($id, $mode)
623625
'S_FIELD_NO_VIEW' => ($cp->vars['field_no_view']) ? true : false,
624626

625627
'L_LANG_SPECIFIC' => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
626-
'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])],
628+
'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())],
627629
'FIELD_IDENT' => $cp->vars['field_ident'],
628630
'LANG_NAME' => $cp->vars['lang_name'],
629631
'LANG_EXPLAIN' => $cp->vars['lang_explain'],
@@ -707,9 +709,10 @@ function main($id, $mode)
707709
$s_one_need_edit = true;
708710
}
709711

712+
$profile_field = $this->type_collection[$row['field_type']];
710713
$template->assign_block_vars('fields', array(
711714
'FIELD_IDENT' => $row['field_ident'],
712-
'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($cp->profile_types[$row['field_type']])],
715+
'FIELD_TYPE' => $user->lang['FIELD_' . strtoupper($profile_field->get_name())],
713716

714717
'L_ACTIVATE_DEACTIVATE' => $user->lang[$active_lang],
715718
'U_ACTIVATE_DEACTIVATE' => $this->u_action . "&action=$active_value&field_id=$id",
@@ -731,15 +734,15 @@ function main($id, $mode)
731734
}
732735

733736
$s_select_type = '';
734-
foreach ($cp->profile_types as $key => $value)
737+
foreach ($this->type_collection as $key => $profile_field)
735738
{
736-
$s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($value)] . '</option>';
739+
$s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($profile_field->get_name())] . '</option>';
737740
}
738741

739742
$template->assign_vars(array(
740743
'U_ACTION' => $this->u_action,
741-
'S_TYPE_OPTIONS' => $s_select_type)
742-
);
744+
'S_TYPE_OPTIONS' => $s_select_type,
745+
));
743746
}
744747

745748
/**
@@ -764,9 +767,8 @@ function build_language_options(&$cp, $field_type, $action = 'create')
764767
}
765768
$db->sql_freeresult($result);
766769

767-
$type_collection = $phpbb_container->get('profilefields.type_collection');
768-
$profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]];
769-
$options = $profile_type->get_language_options($cp->vars);
770+
$profile_field = $this->type_collection[$field_type];
771+
$options = $profile_field->get_language_options($cp->vars);
770772

771773
$lang_options = array();
772774

@@ -906,16 +908,15 @@ function save_profile_field(&$cp, $field_type, $action = 'create')
906908
$db->sql_query($sql);
907909
}
908910

909-
$type_collection = $phpbb_container->get('profilefields.type_collection');
910-
$profile_type = $type_collection['profilefields.type.' . $cp->profile_types[$field_type]];
911+
$profile_field = $this->type_collection[$field_type];
911912

912913
if ($action == 'create')
913914
{
914915
$field_ident = 'pf_' . $field_ident;
915916

916917
$db_tools = $phpbb_container->get('dbal.tools');
917918

918-
list($sql_type, $null) = $db_tools->get_column_type($profile_type->get_database_column_type());
919+
list($sql_type, $null) = $db_tools->get_column_type($profile_field->get_database_column_type());
919920
$profile_sql[] = $this->add_field_ident($field_ident, $sql_type);
920921
}
921922

@@ -990,7 +991,7 @@ function save_profile_field(&$cp, $field_type, $action = 'create')
990991
foreach ($cp->vars['lang_options'] as $option_id => $value)
991992
{
992993
$sql_ary = array(
993-
'field_type' => (int) $field_type,
994+
'field_type' => $field_type,
994995
'lang_value' => $value
995996
);
996997

@@ -1045,7 +1046,7 @@ function save_profile_field(&$cp, $field_type, $action = 'create')
10451046
'field_id' => (int) $field_id,
10461047
'lang_id' => (int) $lang_id,
10471048
'option_id' => (int) $option_id,
1048-
'field_type' => (int) $field_type,
1049+
'field_type' => $field_type,
10491050
'lang_value' => $value
10501051
);
10511052
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
*
4+
* @package migration
5+
* @copyright (c) 2014 phpBB Group
6+
* @license http://opensource.org/licenses/gpl-license.php GNU Public License v2
7+
*
8+
*/
9+
10+
namespace phpbb\db\migration\data\v310;
11+
12+
class profilefield_types extends \phpbb\db\migration\migration
13+
{
14+
15+
static public function depends_on()
16+
{
17+
return array(
18+
'\phpbb\db\migration\data\v310\alpha2',
19+
);
20+
}
21+
22+
public function update_schema()
23+
{
24+
return array(
25+
'change_columns' => array(
26+
$this->table_prefix . 'profile_fields' => array(
27+
'field_type' => array('VCHAR:100', ''),
28+
),
29+
$this->table_prefix . 'profile_fields_lang' => array(
30+
'field_type' => array('VCHAR:100', ''),
31+
),
32+
),
33+
);
34+
}
35+
36+
public function update_data()
37+
{
38+
return array(
39+
array('custom', array(array($this, 'update_profile_fields_type'))),
40+
array('custom', array(array($this, 'update_profile_fields_lang_type'))),
41+
);
42+
}
43+
44+
public function update_profile_fields_type()
45+
{
46+
// Update profile field types
47+
$sql = 'SELECT field_type
48+
FROM ' . $this->table_prefix . 'profile_fields
49+
GROUP BY field_type';
50+
$result = $this->db->sql_query($sql);
51+
52+
while ($row = $this->db->sql_fetchrow($result))
53+
{
54+
$sql = 'UPDATE ' . $this->table_prefix . "profile_fields
55+
SET field_type = '" . $this->db->sql_escape($this->convert_phpbb30_field_type($row['field_type'])) . "'
56+
WHERE field_type = '" . $this->db->sql_escape($row['field_type']) . "'";
57+
$this->sql_query($sql);
58+
}
59+
$this->db->sql_freeresult($result);
60+
}
61+
62+
public function update_profile_fields_lang_type()
63+
{
64+
// Update profile field language types
65+
$sql = 'SELECT field_type
66+
FROM ' . $this->table_prefix . 'profile_fields_lang
67+
GROUP BY field_type';
68+
$result = $this->db->sql_query($sql);
69+
70+
while ($row = $this->db->sql_fetchrow($result))
71+
{
72+
$sql = 'UPDATE ' . $this->table_prefix . "profile_fields_lang
73+
SET field_type = '" . $this->db->sql_escape($this->convert_phpbb30_field_type($row['field_type'])) . "'
74+
WHERE field_type = '" . $this->db->sql_escape($row['field_type']) . "'";
75+
$this->sql_query($sql);
76+
}
77+
$this->db->sql_freeresult($result);
78+
}
79+
80+
/**
81+
* Determine the new field type for a given phpBB 3.0 field type
82+
*
83+
* @param $field_type int Field type in 3.0
84+
* @return string Field new type which is used since 3.1
85+
*/
86+
public function convert_phpbb30_field_type($field_type)
87+
{
88+
switch ($field_type)
89+
{
90+
case FIELD_INT:
91+
return 'profilefields.type.int';
92+
case FIELD_STRING:
93+
return 'profilefields.type.string';
94+
case FIELD_TEXT:
95+
return 'profilefields.type.text';
96+
case FIELD_BOOL:
97+
return 'profilefields.type.bool';
98+
case FIELD_DROPDOWN:
99+
return 'profilefields.type.dropdown';
100+
case FIELD_DATE:
101+
return 'profilefields.type.date';
102+
default:
103+
return $field_type;
104+
}
105+
}
106+
}

phpBB/phpbb/profilefields/lang_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function get_option_lang($field_id, $lang_id, $field_type, $preview_optio
6565
FROM ' . PROFILE_FIELDS_LANG_TABLE . "
6666
WHERE field_id = $field_id
6767
AND lang_id = $lang_id
68-
AND field_type = $field_type
68+
AND field_type = '" . $this->db->sql_escape($field_type) . "'
6969
ORDER BY option_id";
7070
$result = $this->db->sql_query($sql);
7171

phpBB/phpbb/profilefields/profilefields.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ public function generate_profile_fields($mode, $lang_id)
8282

8383
while ($row = $this->db->sql_fetchrow($result))
8484
{
85-
8685
// Return templated field
8786
$tpl_snippet = $this->process_field_row('change', $row);
88-
$profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]];
87+
$profile_field = $this->type_collection[$row['field_type']];
8988

9089
$this->template->assign_block_vars('profile_fields', array(
9190
'LANG_NAME' => $row['lang_name'],
@@ -160,7 +159,7 @@ public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
160159

161160
while ($row = $this->db->sql_fetchrow($result))
162161
{
163-
$profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]];
162+
$profile_field = $this->type_collection[$row['field_type']];
164163
$cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row);
165164
$check_value = $cp_data['pf_' . $row['field_ident']];
166165

@@ -300,7 +299,7 @@ public function generate_profile_fields_template($mode, $user_id = 0, $profile_r
300299

301300
foreach ($profile_row as $ident => $ident_ary)
302301
{
303-
$profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]];
302+
$profile_field = $this->type_collection[$ident_ary['data']['field_type']];
304303
$value = $profile_field->get_profile_value($ident_ary['value'], $ident_ary['data']);
305304

306305
if ($value === NULL)
@@ -349,13 +348,13 @@ public function process_field_row($mode, $profile_row)
349348
));
350349

351350
// empty previously filled blockvars
352-
foreach ($this->profile_types as $field_case => $field_type)
351+
foreach ($this->type_collection as $field_key => $field_type)
353352
{
354-
$this->template->destroy_block_vars($field_type);
353+
$this->template->destroy_block_vars($field_type->get_name());
355354
}
356355

357356
// Assign template variables
358-
$profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$profile_row['field_type']]];
357+
$profile_field = $this->type_collection[$profile_row['field_type']];
359358
$profile_field->generate_field($profile_row, $preview_options);
360359

361360
// Return templated data
@@ -382,7 +381,7 @@ public function build_insert_sql_array($cp_data)
382381

383382
while ($row = $this->db->sql_fetchrow($result))
384383
{
385-
$profile_field = $this->type_collection['profilefields.type.' . $this->profile_types[$row['field_type']]];
384+
$profile_field = $this->type_collection[$row['field_type']];
386385
$cp_data['pf_' . $row['field_ident']] = $profile_field->get_default_field_value($row);
387386
}
388387
$this->db->sql_freeresult($result);

phpBB/phpbb/profilefields/type/type_base.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public function __construct(\phpbb\request\request $request, \phpbb\template\tem
2121
$this->user = $user;
2222
}
2323

24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function get_service_name()
28+
{
29+
return 'profilefields.type.' . $this->get_name();
30+
}
31+
2432
/**
2533
* {@inheritDoc}
2634
*/

phpBB/phpbb/profilefields/type/type_bool.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpb
2323
$this->user = $user;
2424
}
2525

26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function get_name()
30+
{
31+
return 'bool';
32+
}
33+
2634
/**
2735
* {@inheritDoc}
2836
*/
@@ -36,7 +44,7 @@ public function get_options($default_lang_id, $field_data)
3644
'lang_id' => $default_lang_id,
3745
'field_default_value' => $field_data['field_default_value'],
3846
'field_ident' => 'field_default_value',
39-
'field_type' => FIELD_BOOL,
47+
'field_type' => $this->get_service_name(),
4048
'field_length' => $field_data['field_length'],
4149
'lang_options' => $field_data['lang_options']
4250
);
@@ -163,7 +171,7 @@ public function generate_field($profile_row, $preview_options = false)
163171
{
164172
if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1))
165173
{
166-
$this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview_options);
174+
$this->lang_helper->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], $this->get_service_name(), $preview_options);
167175
}
168176

169177
$options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']);

phpBB/phpbb/profilefields/type/type_date.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public function __construct(\phpbb\profilefields\profilefields $profilefields, \
2222
$this->user = $user;
2323
}
2424

25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function get_name()
29+
{
30+
return 'date';
31+
}
32+
2533
/**
2634
* {@inheritDoc}
2735
*/
@@ -34,7 +42,7 @@ public function get_options($default_lang_id, $field_data)
3442
'lang_id' => $default_lang_id,
3543
'field_default_value' => $field_data['field_default_value'],
3644
'field_ident' => 'field_default_value',
37-
'field_type' => FIELD_DATE,
45+
'field_type' => $this->get_service_name(),
3846
'field_length' => $field_data['field_length'],
3947
);
4048

0 commit comments

Comments
 (0)