Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
remove_profilefield_wlm
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 8
90
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 update_schema
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 revert_schema
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 revert_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 delete_custom_profile_field_data
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 get_custom_profile_field_id
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 create_custom_field
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration\data\v320;
15
16class remove_profilefield_wlm extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return array(
21            '\phpbb\db\migration\data\v320\dev',
22        );
23    }
24
25    public function update_schema()
26    {
27        return array(
28            'drop_columns'    => array(
29                $this->table_prefix . 'profile_fields_data'            => array(
30                    'pf_phpbb_wlm',
31                ),
32            ),
33        );
34    }
35
36    public function revert_schema()
37    {
38        return array(
39            'add_columns'    => array(
40                $this->table_prefix . 'profile_fields_data'            => array(
41                    'pf_phpbb_wlm'        => array('VCHAR', ''),
42                ),
43            ),
44        );
45    }
46
47    public function update_data()
48    {
49        return array(
50            array('custom', array(array($this, 'delete_custom_profile_field_data'))),
51        );
52    }
53
54    public function revert_data()
55    {
56        return array(
57            array('custom', array(array($this, 'create_custom_field'))),
58        );
59    }
60
61    public function delete_custom_profile_field_data()
62    {
63        $field_id = $this->get_custom_profile_field_id();
64
65        $sql = 'DELETE FROM ' . PROFILE_FIELDS_TABLE . '
66            WHERE field_id = ' . (int) $field_id;
67        $this->db->sql_query($sql);
68
69        $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . '
70            WHERE field_id = ' . (int) $field_id;
71        $this->db->sql_query($sql);
72
73        $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . '
74            WHERE field_id = ' . (int) $field_id;
75        $this->db->sql_query($sql);
76    }
77
78    /**
79     * Get custom profile field id
80     * @return    int    custom profile filed id
81     */
82    public function get_custom_profile_field_id()
83    {
84        $sql = 'SELECT field_id
85            FROM ' . PROFILE_FIELDS_TABLE . "
86            WHERE field_name = 'phpbb_wlm'";
87        $result = $this->db->sql_query($sql);
88        $field_id = (int) $this->db->sql_fetchfield('field_id');
89        $this->db->sql_freeresult($result);
90
91        return $field_id;
92    }
93
94    public function create_custom_field()
95    {
96        $sql = 'SELECT MAX(field_order) as max_field_order
97            FROM ' . PROFILE_FIELDS_TABLE;
98        $result = $this->db->sql_query($sql);
99        $max_field_order = (int) $this->db->sql_fetchfield('max_field_order');
100        $this->db->sql_freeresult($result);
101
102        $sql_ary = array(
103            'field_name'            => 'phpbb_wlm',
104            'field_type'            => 'profilefields.type.string',
105            'field_ident'            => 'phpbb_wlm',
106            'field_length'            => '40',
107            'field_minlen'            => '5',
108            'field_maxlen'            => '255',
109            'field_novalue'            => '',
110            'field_default_value'    => '',
111            'field_validation'        => '.*',
112            'field_required'        => 0,
113            'field_show_novalue'    => 0,
114            'field_show_on_reg'        => 0,
115            'field_show_on_pm'        => 1,
116            'field_show_on_vt'        => 1,
117            'field_show_on_ml'        => 0,
118            'field_show_profile'    => 1,
119            'field_hide'            => 0,
120            'field_no_view'            => 0,
121            'field_active'            => 1,
122            'field_is_contact'        => 1,
123            'field_contact_desc'    => '',
124            'field_contact_url'        => '',
125            'field_order'            => $max_field_order + 1,
126        );
127
128        $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
129        $this->db->sql_query($sql);
130        $field_id = (int) $this->db->sql_nextid();
131
132        $insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, PROFILE_LANG_TABLE);
133
134        $sql = 'SELECT lang_id
135            FROM ' . LANG_TABLE;
136        $result = $this->db->sql_query($sql);
137        $lang_name = 'WLM';
138        while ($lang_id = (int) $this->db->sql_fetchfield('lang_id'))
139        {
140            $insert_buffer->insert(array(
141                'field_id'                => (int) $field_id,
142                'lang_id'                => (int) $lang_id,
143                'lang_name'                => $lang_name,
144                'lang_explain'            => '',
145                'lang_default_value'    => '',
146            ));
147        }
148        $this->db->sql_freeresult($result);
149
150        $insert_buffer->flush();
151    }
152}