From 3c640e57c5815c970e706f724fb29a3114826352 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 15:41:49 +0100 Subject: [ticket/12115] Convert user occupation to a profile field PHPBB3-12115 --- .../data/v310/profilefield_occupation.php | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php new file mode 100644 index 0000000000..09c71127f9 --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php @@ -0,0 +1,146 @@ +db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_occupation'); + } + + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v310\profilefield_types', + ); + } + + public function update_schema() + { + return array( + 'change_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_phpbb_occupation' => array('MTEXT', ''), + ), + ), + ); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'create_occupation_custom_field'))), + array('custom', array(array($this, 'convert_occupation_to_custom_field'))), + ); + } + + public function create_occupation_custom_field() + { + $sql = 'SELECT MAX(field_order) as max_field_order + FROM ' . PROFILE_FIELDS_TABLE; + $result = $this->db->sql_query($sql); + $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); + $this->db->sql_freeresult($result); + + $sql_ary = array( + 'field_name' => 'phpbb_occupation', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_occupation', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + 'field_order' => $max_field_order + 1, + ); + + $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + $field_id = (int) $this->db->sql_nextid(); + + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); + + $sql = 'SELECT lang_id + FROM ' . LANG_TABLE; + $result = $this->db->sql_query($sql); + while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) + { + $insert_buffer->add(array( + 'field_id' => $field_id, + 'lang_id' => $lang_id, + 'lang_name' => 'OCCUPATION', + 'lang_explain' => '', + 'lang_default_value' => '', + )); + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + } + + /** + * @param int $start Start of staggering step + * @return mixed int start of the next step, null if the end was reached + */ + public function convert_occupation_to_custom_field($start) + { + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); + $limit = 250; + $converted_users = 0; + + $sql = 'SELECT user_id, user_occ + FROM ' . $this->table_prefix . "users + WHERE user_occ <> '' + ORDER BY user_id"; + $result = $this->db->sql_query_limit($sql, $limit, $start); + + while ($row = $this->db->sql_fetchrow($result)) + { + $converted_users++; + + $cp_data = array( + 'pf_phpbb_occupation' => $row['user_occ'], + ); + + $sql = 'UPDATE ' . $this->fields_data_table . ' + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' + WHERE user_id = ' . (int) $row['user_id']; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data['user_id'] = (int) $row['user_id']; + $insert_buffer->insert($cp_data); + } + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + + if ($converted_users < $limit) + { + // No more users left, we are done... + return; + } + + return $start + $limit; + } +} -- cgit v1.2.1 From f97d268a79502243c6bf259cde854519eea42391 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 18:00:17 +0100 Subject: [ticket/12115] Translate profile field name before displaying it in errors PHPBB3-12115 --- phpBB/phpbb/profilefields/type/type_base.php | 8 ++++++++ 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 | 9 +++++++++ phpBB/phpbb/profilefields/type/type_string_common.php | 12 ++++++------ 7 files changed, 31 insertions(+), 14 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/profilefields/type/type_base.php b/phpBB/phpbb/profilefields/type/type_base.php index 95e9b8768b..9c363a7b4e 100644 --- a/phpBB/phpbb/profilefields/type/type_base.php +++ b/phpBB/phpbb/profilefields/type/type_base.php @@ -76,6 +76,14 @@ abstract class type_base implements type_interface return 'pf_' . $field_data['field_ident']; } + /** + * {@inheritDoc} + */ + public function get_field_name($field_name) + { + return isset($this->user->lang[$field_name]) ? $this->user->lang[$field_name] : $field_name; + } + /** * {@inheritDoc} */ diff --git a/phpBB/phpbb/profilefields/type/type_bool.php b/phpBB/phpbb/profilefields/type/type_bool.php index 47a5ed4992..fa9c0a8714 100644 --- a/phpBB/phpbb/profilefields/type/type_bool.php +++ b/phpBB/phpbb/profilefields/type/type_bool.php @@ -136,7 +136,7 @@ class type_bool extends type_base if (!$field_value && $field_data['field_required']) { - return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); + return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($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 0de80f2baf..fc012dd97a 100644 --- a/phpBB/phpbb/profilefields/type/type_date.php +++ b/phpBB/phpbb/profilefields/type/type_date.php @@ -159,17 +159,17 @@ class type_date extends type_base if ((!$day || !$month || !$year) && $field_data['field_required']) { - return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); + return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])); } if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) { - return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']); + return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($field_data['lang_name'])); } if (checkdate($month, $day, $year) === false) { - return $this->user->lang('FIELD_INVALID_DATE', $field_data['lang_name']); + return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($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 b5b393d91b..bcf0ba05f9 100644 --- a/phpBB/phpbb/profilefields/type/type_dropdown.php +++ b/phpBB/phpbb/profilefields/type/type_dropdown.php @@ -137,12 +137,12 @@ class type_dropdown extends type_base 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']); + return $this->user->lang('FIELD_INVALID_VALUE', $this->get_field_name($field_data['lang_name'])); } if ($field_value == $field_data['field_novalue'] && $field_data['field_required']) { - return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); + return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($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 77a4f813da..b89faca018 100644 --- a/phpBB/phpbb/profilefields/type/type_int.php +++ b/phpBB/phpbb/profilefields/type/type_int.php @@ -126,11 +126,11 @@ class type_int extends type_base if ($field_value < $field_data['field_minlen']) { - return $this->user->lang('FIELD_TOO_SMALL', (int) $row['field_minlen'], $row['lang_name']); + return $this->user->lang('FIELD_TOO_SMALL', (int) $field_data['field_minlen'], $this->get_field_name($field_data['lang_name'])); } else if ($field_value > $field_data['field_maxlen']) { - return $this->user->lang('FIELD_TOO_LARGE', (int) $row['field_maxlen'], $row['lang_name']); + return $this->user->lang('FIELD_TOO_LARGE', (int) $field_data['field_maxlen'], $this->get_field_name($field_data['lang_name'])); } return false; diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 7d0cf7662f..93020935e9 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -174,4 +174,13 @@ interface type_interface * @return null */ public function display_options(&$template_vars, &$field_data); + + /** + * Return templated value/field. Possible values for $mode are: + * change == user is able to set/enter profile values; preview == just show the value + * @param string $mode + * @param array $profile_row + * @return null + */ + public function process_field_row($mode, $profile_row); } diff --git a/phpBB/phpbb/profilefields/type/type_string_common.php b/phpBB/phpbb/profilefields/type/type_string_common.php index d322099c34..f00a7e6a08 100644 --- a/phpBB/phpbb/profilefields/type/type_string_common.php +++ b/phpBB/phpbb/profilefields/type/type_string_common.php @@ -52,16 +52,16 @@ abstract class type_string_common extends type_base } else if (trim($field_value) === '' && $field_data['field_required']) { - return $this->user->lang('FIELD_REQUIRED', $field_data['lang_name']); + return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])); } if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen']) { - return $this->user->lang('FIELD_TOO_SHORT', (int) $row['field_minlen'], $row['lang_name']); + return $this->user->lang('FIELD_TOO_SHORT', (int) $field_data['field_minlen'], $this->get_field_name($field_data['lang_name'])); } else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen']) { - return $this->user->lang('FIELD_TOO_LONG', (int) $row['field_maxlen'], $row['lang_name']); + return $this->user->lang('FIELD_TOO_LONG', (int) $field_data['field_maxlen'], $this->get_field_name($field_data['lang_name'])); } if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*') @@ -72,13 +72,13 @@ abstract class type_string_common extends type_base switch ($row['field_validation']) { case '[0-9]+': - return $this->user->lang('FIELD_INVALID_CHARS_NUMBERS_ONLY', $row['lang_name']); + return $this->user->lang('FIELD_INVALID_CHARS_NUMBERS_ONLY', $this->get_field_name($field_data['lang_name'])); case '[\w]+': - return $this->user->lang('FIELD_INVALID_CHARS_ALPHA_ONLY', $row['lang_name']); + return $this->user->lang('FIELD_INVALID_CHARS_ALPHA_ONLY', $this->get_field_name($field_data['lang_name'])); case '[\w_\+\. \-\[\]]+': - return $this->user->lang('FIELD_INVALID_CHARS_SPACERS_ONLY', $row['lang_name']); + return $this->user->lang('FIELD_INVALID_CHARS_SPACERS_ONLY', $this->get_field_name($field_data['lang_name'])); } } } -- cgit v1.2.1 From 8dab016a7b652e2a0aafe03c085d9c51ab6f3ff2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 18:04:38 +0100 Subject: [ticket/12115] Add methods to interface PHPBB3-12115 --- phpBB/phpbb/profilefields/type/type_interface.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/profilefields/type/type_interface.php b/phpBB/phpbb/profilefields/type/type_interface.php index 93020935e9..94f6882524 100644 --- a/phpBB/phpbb/profilefields/type/type_interface.php +++ b/phpBB/phpbb/profilefields/type/type_interface.php @@ -32,6 +32,13 @@ interface type_interface */ public function get_service_name(); + /** + * Get the name of template file for this type + * + * @return string Returns the name of the template file + */ + public function get_template_filename(); + /** * Get dropdown options for second step in ACP * @@ -178,8 +185,9 @@ interface type_interface /** * Return templated value/field. Possible values for $mode are: * change == user is able to set/enter profile values; preview == just show the value - * @param string $mode - * @param array $profile_row + * + * @param string $mode Mode for displaying the field (preview|change) + * @param array $profile_row Array with data for this field * @return null */ public function process_field_row($mode, $profile_row); -- cgit v1.2.1 From bbada27ee9e797c7f6ce997152bc1efb8c8f125d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 21:45:59 +0100 Subject: [ticket/12115] Also port user interests to profile fields PHPBB3-12115 --- phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php index 09c71127f9..ee276fa0f9 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php @@ -19,7 +19,7 @@ class profilefield_occupation extends \phpbb\db\migration\migration static public function depends_on() { return array( - '\phpbb\db\migration\data\v310\profilefield_types', + '\phpbb\db\migration\data\v310\profilefield_interests', ); } @@ -83,7 +83,7 @@ class profilefield_occupation extends \phpbb\db\migration\migration $result = $this->db->sql_query($sql); while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) { - $insert_buffer->add(array( + $insert_buffer->insert(array( 'field_id' => $field_id, 'lang_id' => $lang_id, 'lang_name' => 'OCCUPATION', @@ -120,7 +120,7 @@ class profilefield_occupation extends \phpbb\db\migration\migration 'pf_phpbb_occupation' => $row['user_occ'], ); - $sql = 'UPDATE ' . $this->fields_data_table . ' + $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' WHERE user_id = ' . (int) $row['user_id']; $this->db->sql_query($sql); -- cgit v1.2.1 From c2cf294be539442d331ccd771a665e9113081261 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Sun, 2 Feb 2014 21:50:32 +0100 Subject: [ticket/12115] Add a new migration for profilefield_interests With this we avoid missing to copy one, when the admin already created a phpbb_* profile field. PHPBB3-12115 --- .../migration/data/v310/profilefield_interests.php | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 phpBB/phpbb/db/migration/data/v310/profilefield_interests.php (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php new file mode 100644 index 0000000000..77e23fcd17 --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php @@ -0,0 +1,146 @@ +db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_interests'); + } + + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v310\profilefield_types', + ); + } + + public function update_schema() + { + return array( + 'change_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_phpbb_interests' => array('MTEXT', ''), + ), + ), + ); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'create_interests_custom_field'))), + array('custom', array(array($this, 'convert_interests_to_custom_field'))), + ); + } + + public function create_interests_custom_field() + { + $sql = 'SELECT MAX(field_order) as max_field_order + FROM ' . PROFILE_FIELDS_TABLE; + $result = $this->db->sql_query($sql); + $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); + $this->db->sql_freeresult($result); + + $sql_ary = array( + 'field_name' => 'phpbb_interests', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_interests', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + 'field_order' => $max_field_order + 1, + ); + + $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + $field_id = (int) $this->db->sql_nextid(); + + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); + + $sql = 'SELECT lang_id + FROM ' . LANG_TABLE; + $result = $this->db->sql_query($sql); + while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) + { + $insert_buffer->insert(array( + 'field_id' => $field_id, + 'lang_id' => $lang_id, + 'lang_name' => 'INTERESTS', + 'lang_explain' => '', + 'lang_default_value' => '', + )); + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + } + + /** + * @param int $start Start of staggering step + * @return mixed int start of the next step, null if the end was reached + */ + public function convert_interests_to_custom_field($start) + { + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); + $limit = 250; + $converted_users = 0; + + $sql = 'SELECT user_id, user_interests + FROM ' . $this->table_prefix . "users + WHERE user_interests <> '' + ORDER BY user_id"; + $result = $this->db->sql_query_limit($sql, $limit, $start); + + while ($row = $this->db->sql_fetchrow($result)) + { + $converted_users++; + + $cp_data = array( + 'pf_phpbb_interests' => $row['user_interests'], + ); + + $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' + WHERE user_id = ' . (int) $row['user_id']; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data['user_id'] = (int) $row['user_id']; + $insert_buffer->insert($cp_data); + } + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + + if ($converted_users < $limit) + { + // No more users left, we are done... + return; + } + + return $start + $limit; + } +} -- cgit v1.2.1 From 190301021bc85ea4226c1347e1a1209c95997e6f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Feb 2014 11:18:59 +0100 Subject: [ticket/12115] Use base class to reduce duplicate code PHPBB3-12115 --- .../migration/data/v310/profilefield_interests.php | 153 ++++---------------- .../data/v310/profilefield_occupation.php | 153 ++++---------------- .../db/migration/profilefield_base_migration.php | 155 +++++++++++++++++++++ 3 files changed, 209 insertions(+), 252 deletions(-) create mode 100644 phpBB/phpbb/db/migration/profilefield_base_migration.php (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php index 77e23fcd17..d73bc78edb 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_interests.php @@ -9,13 +9,8 @@ namespace phpbb\db\migration\data\v310; -class profilefield_interests extends \phpbb\db\migration\migration +class profilefield_interests extends \phpbb\db\migration\profilefield_base_migration { - public function effectively_installed() - { - return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_interests'); - } - static public function depends_on() { return array( @@ -23,124 +18,30 @@ class profilefield_interests extends \phpbb\db\migration\migration ); } - public function update_schema() - { - return array( - 'change_columns' => array( - $this->table_prefix . 'profile_fields_data' => array( - 'pf_phpbb_interests' => array('MTEXT', ''), - ), - ), - ); - } - - public function update_data() - { - return array( - array('custom', array(array($this, 'create_interests_custom_field'))), - array('custom', array(array($this, 'convert_interests_to_custom_field'))), - ); - } - - public function create_interests_custom_field() - { - $sql = 'SELECT MAX(field_order) as max_field_order - FROM ' . PROFILE_FIELDS_TABLE; - $result = $this->db->sql_query($sql); - $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); - $this->db->sql_freeresult($result); - - $sql_ary = array( - 'field_name' => 'phpbb_interests', - 'field_type' => 'profilefields.type.text', - 'field_ident' => 'phpbb_interests', - 'field_length' => '3|30', - 'field_minlen' => '2', - 'field_maxlen' => '500', - 'field_novalue' => '', - 'field_default_value' => '', - 'field_validation' => '.*', - 'field_required' => 0, - 'field_show_novalue' => 0, - 'field_show_on_reg' => 0, - 'field_show_on_pm' => 0, - 'field_show_on_vt' => 0, - 'field_show_profile' => 1, - 'field_hide' => 0, - 'field_no_view' => 0, - 'field_active' => 1, - 'field_order' => $max_field_order + 1, - ); - - $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); - $field_id = (int) $this->db->sql_nextid(); - - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); - - $sql = 'SELECT lang_id - FROM ' . LANG_TABLE; - $result = $this->db->sql_query($sql); - while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) - { - $insert_buffer->insert(array( - 'field_id' => $field_id, - 'lang_id' => $lang_id, - 'lang_name' => 'INTERESTS', - 'lang_explain' => '', - 'lang_default_value' => '', - )); - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - } - - /** - * @param int $start Start of staggering step - * @return mixed int start of the next step, null if the end was reached - */ - public function convert_interests_to_custom_field($start) - { - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); - $limit = 250; - $converted_users = 0; - - $sql = 'SELECT user_id, user_interests - FROM ' . $this->table_prefix . "users - WHERE user_interests <> '' - ORDER BY user_id"; - $result = $this->db->sql_query_limit($sql, $limit, $start); - - while ($row = $this->db->sql_fetchrow($result)) - { - $converted_users++; - - $cp_data = array( - 'pf_phpbb_interests' => $row['user_interests'], - ); - - $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data - SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' - WHERE user_id = ' . (int) $row['user_id']; - $this->db->sql_query($sql); - - if (!$this->db->sql_affectedrows()) - { - $cp_data['user_id'] = (int) $row['user_id']; - $insert_buffer->insert($cp_data); - } - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - - if ($converted_users < $limit) - { - // No more users left, we are done... - return; - } - - return $start + $limit; - } + protected $profilefield_name = 'phpbb_interests'; + + protected $profilefield_database_type = array('MTEXT', ''); + + protected $profilefield_data = array( + 'field_name' => 'phpbb_interests', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_interests', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + ); + + protected $user_column_name = 'user_interests'; } diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php index ee276fa0f9..b0ea961c08 100644 --- a/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_occupation.php @@ -9,13 +9,8 @@ namespace phpbb\db\migration\data\v310; -class profilefield_occupation extends \phpbb\db\migration\migration +class profilefield_occupation extends \phpbb\db\migration\profilefield_base_migration { - public function effectively_installed() - { - return $this->db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_phpbb_occupation'); - } - static public function depends_on() { return array( @@ -23,124 +18,30 @@ class profilefield_occupation extends \phpbb\db\migration\migration ); } - public function update_schema() - { - return array( - 'change_columns' => array( - $this->table_prefix . 'profile_fields_data' => array( - 'pf_phpbb_occupation' => array('MTEXT', ''), - ), - ), - ); - } - - public function update_data() - { - return array( - array('custom', array(array($this, 'create_occupation_custom_field'))), - array('custom', array(array($this, 'convert_occupation_to_custom_field'))), - ); - } - - public function create_occupation_custom_field() - { - $sql = 'SELECT MAX(field_order) as max_field_order - FROM ' . PROFILE_FIELDS_TABLE; - $result = $this->db->sql_query($sql); - $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); - $this->db->sql_freeresult($result); - - $sql_ary = array( - 'field_name' => 'phpbb_occupation', - 'field_type' => 'profilefields.type.text', - 'field_ident' => 'phpbb_occupation', - 'field_length' => '3|30', - 'field_minlen' => '2', - 'field_maxlen' => '500', - 'field_novalue' => '', - 'field_default_value' => '', - 'field_validation' => '.*', - 'field_required' => 0, - 'field_show_novalue' => 0, - 'field_show_on_reg' => 0, - 'field_show_on_pm' => 0, - 'field_show_on_vt' => 0, - 'field_show_profile' => 1, - 'field_hide' => 0, - 'field_no_view' => 0, - 'field_active' => 1, - 'field_order' => $max_field_order + 1, - ); - - $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); - $this->db->sql_query($sql); - $field_id = (int) $this->db->sql_nextid(); - - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); - - $sql = 'SELECT lang_id - FROM ' . LANG_TABLE; - $result = $this->db->sql_query($sql); - while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) - { - $insert_buffer->insert(array( - 'field_id' => $field_id, - 'lang_id' => $lang_id, - 'lang_name' => 'OCCUPATION', - 'lang_explain' => '', - 'lang_default_value' => '', - )); - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - } - - /** - * @param int $start Start of staggering step - * @return mixed int start of the next step, null if the end was reached - */ - public function convert_occupation_to_custom_field($start) - { - $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); - $limit = 250; - $converted_users = 0; - - $sql = 'SELECT user_id, user_occ - FROM ' . $this->table_prefix . "users - WHERE user_occ <> '' - ORDER BY user_id"; - $result = $this->db->sql_query_limit($sql, $limit, $start); - - while ($row = $this->db->sql_fetchrow($result)) - { - $converted_users++; - - $cp_data = array( - 'pf_phpbb_occupation' => $row['user_occ'], - ); - - $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data - SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' - WHERE user_id = ' . (int) $row['user_id']; - $this->db->sql_query($sql); - - if (!$this->db->sql_affectedrows()) - { - $cp_data['user_id'] = (int) $row['user_id']; - $insert_buffer->insert($cp_data); - } - } - $this->db->sql_freeresult($result); - - $insert_buffer->flush(); - - if ($converted_users < $limit) - { - // No more users left, we are done... - return; - } - - return $start + $limit; - } + protected $profilefield_name = 'phpbb_occupation'; + + protected $profilefield_database_type = array('MTEXT', ''); + + protected $profilefield_data = array( + 'field_name' => 'phpbb_occupation', + 'field_type' => 'profilefields.type.text', + 'field_ident' => 'phpbb_occupation', + 'field_length' => '3|30', + 'field_minlen' => '2', + 'field_maxlen' => '500', + 'field_novalue' => '', + 'field_default_value' => '', + 'field_validation' => '.*', + 'field_required' => 0, + 'field_show_novalue' => 0, + 'field_show_on_reg' => 0, + 'field_show_on_pm' => 0, + 'field_show_on_vt' => 0, + 'field_show_profile' => 1, + 'field_hide' => 0, + 'field_no_view' => 0, + 'field_active' => 1, + ); + + protected $user_column_name = 'user_occ'; } diff --git a/phpBB/phpbb/db/migration/profilefield_base_migration.php b/phpBB/phpbb/db/migration/profilefield_base_migration.php new file mode 100644 index 0000000000..e637aec9f3 --- /dev/null +++ b/phpBB/phpbb/db/migration/profilefield_base_migration.php @@ -0,0 +1,155 @@ +db_tools->sql_column_exists($this->table_prefix . 'profile_fields_data', 'pf_' . $this->profilefield_name); + } + + public function update_schema() + { + return array( + 'add_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_' . $this->profilefield_name => $this->profilefield_database_type, + ), + ), + ); + } + + public function revert_schema() + { + return array( + 'drop_columns' => array( + $this->table_prefix . 'profile_fields_data' => array( + 'pf_' . $this->profilefield_name, + ), + ), + ); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'create_custom_field'))), + array('custom', array(array($this, 'convert_user_field_to_custom_field'))), + ); + } + + public function create_custom_field() + { + $sql = 'SELECT MAX(field_order) as max_field_order + FROM ' . PROFILE_FIELDS_TABLE; + $result = $this->db->sql_query($sql); + $max_field_order = (int) $this->db->sql_fetchfield('max_field_order'); + $this->db->sql_freeresult($result); + + $sql_ary = array_merge($this->profilefield_data, array( + 'field_order' => $max_field_order + 1, + )); + + $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); + $this->db->sql_query($sql); + $field_id = (int) $this->db->sql_nextid(); + + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); + + $sql = 'SELECT lang_id + FROM ' . LANG_TABLE; + $result = $this->db->sql_query($sql); + while ($lang_id = (int) $this->db->sql_fetchfield('lang_id')) + { + $insert_buffer->insert(array( + 'field_id' => $field_id, + 'lang_id' => $lang_id, + 'lang_name' => strtoupper(substr($this->profilefield_name, 6)),// Remove phpbb_ from field name + 'lang_explain' => '', + 'lang_default_value' => '', + )); + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + } + + /** + * @param int $start Start of staggering step + * @return mixed int start of the next step, null if the end was reached + */ + public function convert_user_field_to_custom_field($start) + { + $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . 'profile_fields_data'); + $limit = 250; + $converted_users = 0; + + $sql = 'SELECT user_id, ' . $this->user_column_name . ' + FROM ' . $this->table_prefix . 'users + WHERE ' . $this->user_column_name . " <> '' + ORDER BY user_id"; + $result = $this->db->sql_query_limit($sql, $limit, $start); + + while ($row = $this->db->sql_fetchrow($result)) + { + $converted_users++; + + $cp_data = array( + 'pf_' . $this->profilefield_name => $row[$this->user_column_name], + ); + + $sql = 'UPDATE ' . $this->table_prefix . 'profile_fields_data + SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . ' + WHERE user_id = ' . (int) $row['user_id']; + $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows()) + { + $cp_data['user_id'] = (int) $row['user_id']; + $cp_data = array_merge($this->get_insert_sql_array(), $cp_data); + $insert_buffer->insert($cp_data); + } + } + $this->db->sql_freeresult($result); + + $insert_buffer->flush(); + + if ($converted_users < $limit) + { + // No more users left, we are done... + return; + } + + return $start + $limit; + } + + protected function get_insert_sql_array() + { + static $profile_row; + + if ($profile_row === null) + { + global $phpbb_container; + $manager = $phpbb_container->get('profilefields.manager'); + $profile_row = $manager->build_insert_sql_array(array()); + } + + return $profile_row; + } +} -- cgit v1.2.1 From 26640bad3d959fd763feeae7647423b4495cf969 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Feb 2014 11:19:31 +0100 Subject: [ticket/12115] Add migration file to remove old user columns PHPBB3-12115 --- .../migration/data/v310/profilefield_cleanup.php | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 phpBB/phpbb/db/migration/data/v310/profilefield_cleanup.php (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/data/v310/profilefield_cleanup.php b/phpBB/phpbb/db/migration/data/v310/profilefield_cleanup.php new file mode 100644 index 0000000000..9e7ba68327 --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v310/profilefield_cleanup.php @@ -0,0 +1,51 @@ +db_tools->sql_column_exists($this->table_prefix . 'users', 'user_occ') && + !$this->db_tools->sql_column_exists($this->table_prefix . 'users', 'user_interests'); + } + + static public function depends_on() + { + return array( + '\phpbb\db\migration\data\v310\profilefield_interests', + '\phpbb\db\migration\data\v310\profilefield_occupation', + ); + } + + public function update_schema() + { + return array( + 'drop_columns' => array( + $this->table_prefix . 'users' => array( + 'user_occ', + 'user_interests', + ), + ), + ); + } + + public function revert_schema() + { + return array( + 'add_columns' => array( + $this->table_prefix . 'users' => array( + 'user_occ' => array('MTEXT', ''), + 'user_interests' => array('MTEXT', ''), + ), + ), + ); + } +} -- cgit v1.2.1 From 4663ea7c8288ae56e00e2270aee49cb14a7c0276 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 6 Feb 2014 14:18:30 +0100 Subject: [ticket/12115] Remove space from line ending PHPBB3-12115 --- phpBB/phpbb/db/migration/profilefield_base_migration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'phpBB/phpbb') diff --git a/phpBB/phpbb/db/migration/profilefield_base_migration.php b/phpBB/phpbb/db/migration/profilefield_base_migration.php index e637aec9f3..dec7a4c2bb 100644 --- a/phpBB/phpbb/db/migration/profilefield_base_migration.php +++ b/phpBB/phpbb/db/migration/profilefield_base_migration.php @@ -68,7 +68,7 @@ abstract class profilefield_base_migration extends \phpbb\db\migration\migration $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); $this->db->sql_query($sql); - $field_id = (int) $this->db->sql_nextid(); + $field_id = (int) $this->db->sql_nextid(); $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE); -- cgit v1.2.1