Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
24.27% covered (danger)
24.27%
25 / 103
12.50% covered (danger)
12.50%
2 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
type_dropdown
24.27% covered (danger)
24.27%
25 / 103
12.50% covered (danger)
12.50%
2 / 16
845.99
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_name_short
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_options
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 get_default_option_values
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 get_default_field_value
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_profile_field
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 validate_profile_field
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 get_profile_value
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
6.56
 get_profile_value_raw
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
5
 generate_field
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
110
 get_database_column_type
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_language_options
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 prepare_options_form
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 validate_options_on_submit
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 get_excluded_options
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 display_options
0.00% covered (danger)
0.00%
0 / 7
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\profilefields\type;
15
16class type_dropdown extends type_base
17{
18    /**
19    * Profile fields language helper
20    * @var \phpbb\profilefields\lang_helper
21    */
22    protected $lang_helper;
23
24    /**
25    * Request object
26    * @var \phpbb\request\request
27    */
28    protected $request;
29
30    /**
31    * Template object
32    * @var \phpbb\template\template
33    */
34    protected $template;
35
36    /**
37    * User object
38    * @var \phpbb\user
39    */
40    protected $user;
41
42    /**
43    * Construct
44    *
45    * @param    \phpbb\profilefields\lang_helper        $lang_helper    Profile fields language helper
46    * @param    \phpbb\request\request        $request    Request object
47    * @param    \phpbb\template\template    $template    Template object
48    * @param    \phpbb\user                    $user        User object
49    */
50    public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
51    {
52        $this->lang_helper = $lang_helper;
53        $this->request = $request;
54        $this->template = $template;
55        $this->user = $user;
56    }
57
58    /**
59    * {@inheritDoc}
60    */
61    public function get_name_short()
62    {
63        return 'dropdown';
64    }
65
66    /**
67    * {@inheritDoc}
68    */
69    public function get_options($default_lang_id, $field_data)
70    {
71        $profile_row = [];
72
73        $profile_row[0] = array(
74            'var_name'                => 'field_default_value',
75            'field_id'                => 1,
76            'lang_name'                => $field_data['lang_name'],
77            'lang_explain'            => $field_data['lang_explain'],
78            'lang_id'                => $default_lang_id,
79            'field_default_value'    => $field_data['field_default_value'],
80            'field_ident'            => 'field_default_value',
81            'field_type'            => $this->get_service_name(),
82            'lang_options'            => $field_data['lang_options'],
83        );
84
85        $profile_row[1] = $profile_row[0];
86        $profile_row[1]['var_name'] = 'field_novalue';
87        $profile_row[1]['field_ident'] = 'field_novalue';
88        $profile_row[1]['field_default_value']    = $field_data['field_novalue'];
89
90        $options = array(
91            0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
92            1 => array('TITLE' => $this->user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $this->user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1])),
93        );
94
95        return $options;
96    }
97
98    /**
99    * {@inheritDoc}
100    */
101    public function get_default_option_values()
102    {
103        return array(
104            'field_length'        => 0,
105            'field_minlen'        => 0,
106            'field_maxlen'        => 5,
107            'field_validation'    => '',
108            'field_novalue'        => 0,
109            'field_default_value'    => 0,
110        );
111    }
112
113    /**
114    * {@inheritDoc}
115    */
116    public function get_default_field_value($field_data)
117    {
118        return $field_data['field_default_value'];
119    }
120
121    /**
122    * {@inheritDoc}
123    */
124    public function get_profile_field($profile_row)
125    {
126        $var_name = 'pf_' . $profile_row['field_ident'];
127        return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
128    }
129
130    /**
131    * {@inheritDoc}
132    */
133    public function validate_profile_field(&$field_value, $field_data)
134    {
135        $field_value = (int) $field_value;
136
137        // retrieve option lang data if necessary
138        if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], 1))
139        {
140            $this->lang_helper->load_option_lang($field_data['lang_id']);
141        }
142
143        if (!$this->lang_helper->is_set($field_data['field_id'], $field_data['lang_id'], $field_value))
144        {
145            return $this->user->lang('FIELD_INVALID_VALUE', $this->get_field_name($field_data['lang_name']));
146        }
147
148        if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
149        {
150            return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name']));
151        }
152
153        return false;
154    }
155
156    /**
157    * {@inheritDoc}
158    */
159    public function get_profile_value($field_value, $field_data)
160    {
161        $field_id = $field_data['field_id'];
162        $lang_id = $field_data['lang_id'];
163        if (!$this->lang_helper->is_set($field_id, $lang_id))
164        {
165            $this->lang_helper->load_option_lang($lang_id);
166        }
167
168        if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue'])
169        {
170            return null;
171        }
172
173        $field_value = (int) $field_value;
174
175        // User not having a value assigned
176        if (!$this->lang_helper->is_set($field_id, $lang_id, $field_value))
177        {
178            if ($field_data['field_show_novalue'])
179            {
180                $field_value = $field_data['field_novalue'];
181            }
182            else
183            {
184                return null;
185            }
186        }
187
188        return $this->lang_helper->get($field_id, $lang_id, $field_value);
189    }
190
191    /**
192    * {@inheritDoc}
193    */
194    public function get_profile_value_raw($field_value, $field_data)
195    {
196        if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue'])
197        {
198            return null;
199        }
200
201        if (!$field_value && $field_data['field_show_novalue'])
202        {
203            $field_value = $field_data['field_novalue'];
204        }
205
206        return $field_value;
207    }
208
209    /**
210    * {@inheritDoc}
211    */
212    public function generate_field($profile_row, $preview_options = false)
213    {
214        $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
215        $field_ident = $profile_row['field_ident'];
216        $default_value = $profile_row['field_default_value'];
217
218        $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]);
219
220        if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1))
221        {
222            if ($preview_options)
223            {
224                $this->lang_helper->load_preview_options($profile_row['field_id'], $profile_row['lang_id'], $preview_options);
225            }
226            else
227            {
228                $this->lang_helper->load_option_lang($profile_row['lang_id']);
229            }
230        }
231
232        $profile_row['field_value'] = (int) $value;
233        $this->template->assign_block_vars('dropdown', array_change_key_case($profile_row, CASE_UPPER));
234
235        $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']);
236        if (is_array($options))
237        {
238            foreach ($options as $option_id => $option_value)
239            {
240                $this->template->assign_block_vars('dropdown.options', array(
241                    'OPTION_ID'    => $option_id,
242                    'SELECTED'    => ($value == $option_id) ? ' selected="selected"' : '',
243                    'VALUE'        => $option_value,
244                ));
245            }
246        }
247    }
248
249    /**
250    * {@inheritDoc}
251    */
252    public function get_database_column_type()
253    {
254        return 'UINT';
255    }
256
257    /**
258    * {@inheritDoc}
259    */
260    public function get_language_options($field_data)
261    {
262        $options = array(
263            'lang_name'        => 'string',
264            'lang_options'    => 'optionfield',
265        );
266
267        if ($field_data['lang_explain'])
268        {
269            $options['lang_explain'] = 'text';
270        }
271
272        return $options;
273    }
274
275    /**
276    * {@inheritDoc}
277    */
278    public function prepare_options_form(&$exclude_options, &$visibility_options)
279    {
280        $exclude_options[1][] = 'lang_options';
281
282        return $this->request->variable('lang_options', '', true);
283    }
284
285    /**
286    * {@inheritDoc}
287    */
288    public function validate_options_on_submit($error, $field_data)
289    {
290        if (!count($field_data['lang_options']))
291        {
292            $error[] = $this->user->lang['NO_FIELD_ENTRIES'];
293        }
294
295        return $error;
296    }
297
298    /**
299    * {@inheritDoc}
300    */
301    public function get_excluded_options($key, $action, $current_value, &$field_data, $step)
302    {
303        if ($step == 2 && $key == 'field_maxlen')
304        {
305            // Get the number of options if this key is 'field_maxlen'
306            return count(explode("\n", $this->request->variable('lang_options', '', true)));
307        }
308
309        return parent::get_excluded_options($key, $action, $current_value, $field_data, $step);
310    }
311
312    /**
313    * {@inheritDoc}
314    */
315    public function display_options(&$template_vars, &$field_data)
316    {
317        // Initialize these array elements if we are creating a new field
318        if (!count($field_data['lang_options']))
319        {
320            // No options have been defined for the dropdown menu
321            $field_data['lang_options'] = array();
322        }
323
324        $template_vars = array_merge($template_vars, array(
325            'S_DROPDOWN'                => true,
326            'L_LANG_OPTIONS_EXPLAIN'    => $this->user->lang['DROPDOWN_ENTRIES_EXPLAIN'],
327            'LANG_OPTIONS'                => implode("\n", $field_data['lang_options']),
328        ));
329    }
330}