Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.32% covered (danger)
17.32%
22 / 127
10.53% covered (danger)
10.53%
2 / 19
CRAP
0.00% covered (danger)
0.00%
0 / 1
type_bool
17.32% covered (danger)
17.32%
22 / 127
10.53% covered (danger)
10.53%
2 / 19
2839.19
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 / 17
0.00% covered (danger)
0.00%
0 / 1
12
 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 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 validate_profile_field
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 get_profile_value
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
8.05
 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 / 21
0.00% covered (danger)
0.00%
0 / 1
272
 get_field_ident
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 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
 get_language_options_input
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 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
12
 get_excluded_options
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
132
 prepare_hidden_fields
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 display_options
0.00% covered (danger)
0.00%
0 / 9
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_bool 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 'bool';
64    }
65
66    /**
67    * {@inheritDoc}
68    */
69    public function get_options($default_lang_id, $field_data)
70    {
71        $profile_row = array(
72            'var_name'                => 'field_default_value',
73            'field_id'                => 1,
74            'lang_name'                => $field_data['lang_name'],
75            'lang_explain'            => $field_data['lang_explain'],
76            'lang_id'                => $default_lang_id,
77            'field_default_value'    => $field_data['field_default_value'],
78            'field_ident'            => 'field_default_value',
79            'field_type'            => $this->get_service_name(),
80            'field_length'            => $field_data['field_length'],
81            'lang_options'            => $field_data['lang_options'],
82        );
83
84        $options = array(
85            0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<label><input type="radio" class="radio" name="field_length" value="1"' . (($field_data['field_length'] == 1) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['RADIO_BUTTONS'] . '</label><label><input type="radio" class="radio" name="field_length" value="2"' . (($field_data['field_length'] == 2) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['CHECKBOX'] . '</label>'),
86            1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
87        );
88
89        return $options;
90    }
91
92    /**
93    * {@inheritDoc}
94    */
95    public function get_default_option_values()
96    {
97        return array(
98            'field_length'        => 1,
99            'field_minlen'        => 0,
100            'field_maxlen'        => 0,
101            'field_validation'    => '',
102            'field_novalue'        => 0,
103            'field_default_value'    => 0,
104        );
105    }
106
107    /**
108    * {@inheritDoc}
109    */
110    public function get_default_field_value($field_data)
111    {
112        return $field_data['field_default_value'];
113    }
114
115    /**
116    * {@inheritDoc}
117    */
118    public function get_profile_field($profile_row)
119    {
120        $var_name = 'pf_' . $profile_row['field_ident'];
121
122        // Checkbox
123        if ($profile_row['field_length'] == 2)
124        {
125            return ($this->request->is_set($var_name)) ? 1 : 0;
126        }
127        else
128        {
129            return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
130        }
131    }
132
133    /**
134    * {@inheritDoc}
135    */
136    public function validate_profile_field(&$field_value, $field_data)
137    {
138        $field_value = (bool) $field_value;
139
140        if (!$field_value && $field_data['field_required'])
141        {
142            return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name']));
143        }
144
145        return false;
146    }
147
148    /**
149    * {@inheritDoc}
150    */
151    public function get_profile_value($field_value, $field_data)
152    {
153        $field_id = $field_data['field_id'];
154        $lang_id = $field_data['lang_id'];
155
156        if (!$this->lang_helper->is_set($field_id, $lang_id))
157        {
158            $this->lang_helper->load_option_lang($lang_id);
159        }
160
161        if (!$field_value && $field_data['field_show_novalue'])
162        {
163            $field_value = $field_data['field_default_value'];
164        }
165
166        if ($field_data['field_length'] == 1)
167        {
168            return ($this->lang_helper->is_set($field_id, $lang_id, (int) $field_value)) ? $this->lang_helper->get($field_id, $lang_id, (int) $field_value) : null;
169        }
170        else if (!$field_value)
171        {
172            return null;
173        }
174        else
175        {
176            return $this->lang_helper->is_set($field_id, $lang_id, $field_value + 1) ? $this->lang_helper->get($field_id, $lang_id, $field_value + 1) : null;
177        }
178    }
179
180    /**
181    * {@inheritDoc}
182    */
183    public function get_profile_value_raw($field_value, $field_data)
184    {
185        if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue'])
186        {
187            return null;
188        }
189
190        if (!$field_value && $field_data['field_show_novalue'])
191        {
192            $field_value = $field_data['field_novalue'];
193        }
194
195        return $field_value;
196    }
197
198    /**
199    * {@inheritDoc}
200    */
201    public function generate_field($profile_row, $preview_options = false)
202    {
203        $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
204        $field_ident = $profile_row['field_ident'];
205        $default_value = $profile_row['field_default_value'];
206
207        // checkbox - set the value to "true" if it has been set to 1
208        if ($profile_row['field_length'] == 2)
209        {
210            $value = ($this->request->is_set($field_ident) && $this->request->variable($field_ident, $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]);
211        }
212        else
213        {
214            $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]);
215        }
216
217        $profile_row['field_value'] = (int) $value;
218        $this->template->assign_block_vars('bool', array_change_key_case($profile_row, CASE_UPPER));
219
220        if ($profile_row['field_length'] == 1)
221        {
222            if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1))
223            {
224                if ($preview_options)
225                {
226                    $this->lang_helper->load_preview_options($profile_row['field_id'], $profile_row['lang_id'], $preview_options);
227                }
228                else
229                {
230                    $this->lang_helper->load_option_lang($profile_row['lang_id']);
231                }
232            }
233
234            $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']);
235            if (is_array($options))
236            {
237                foreach ($options as $option_id => $option_value)
238                {
239                    $this->template->assign_block_vars('bool.options', array(
240                        'OPTION_ID'    => $option_id,
241                        'CHECKED'    => ($value == $option_id) ? ' checked="checked"' : '',
242                        'VALUE'        => $option_value,
243                    ));
244                }
245            }
246        }
247    }
248
249    /**
250    * {@inheritDoc}
251    */
252    public function get_field_ident($field_data)
253    {
254        return ($field_data['field_length'] == '1') ? '' : 'pf_' . $field_data['field_ident'];
255    }
256
257    /**
258    * {@inheritDoc}
259    */
260    public function get_database_column_type()
261    {
262        return 'TINT:2';
263    }
264
265    /**
266    * {@inheritDoc}
267    */
268    public function get_language_options($field_data)
269    {
270        $options = array(
271            'lang_name'        => 'string',
272            'lang_options'    => 'two_options',
273        );
274
275        if ($field_data['lang_explain'])
276        {
277            $options['lang_explain'] = 'text';
278        }
279
280        return $options;
281    }
282
283    /**
284    * {@inheritDoc}
285    */
286    public function get_language_options_input($field_data)
287    {
288        $field_data['l_lang_name']            = $this->request->variable('l_lang_name', array(0 => ''), true);
289        $field_data['l_lang_explain']        = $this->request->variable('l_lang_explain', array(0 => ''), true);
290        $field_data['l_lang_default_value']    = $this->request->variable('l_lang_default_value', array(0 => ''), true);
291
292        /**
293        * @todo check if this line is correct...
294        $field_data['l_lang_default_value']    = $this->request->variable('l_lang_default_value', array(0 => array('')), true);
295        */
296        $field_data['l_lang_options']    = $this->request->variable('l_lang_options', array(0 => array('')), true);
297
298        return $field_data;
299    }
300
301    /**
302    * {@inheritDoc}
303    */
304    public function prepare_options_form(&$exclude_options, &$visibility_options)
305    {
306        $exclude_options[1][] = 'lang_options';
307
308        return $this->request->variable('lang_options', array(''), true);
309    }
310
311    /**
312    * {@inheritDoc}
313    */
314    public function validate_options_on_submit($error, $field_data)
315    {
316        if (empty($field_data['lang_options'][0]) || empty($field_data['lang_options'][1]))
317        {
318            $error[] = $this->user->lang['NO_FIELD_ENTRIES'];
319        }
320
321        return $error;
322    }
323
324    /**
325    * {@inheritDoc}
326    */
327    public function get_excluded_options($key, $action, $current_value, &$field_data, $step)
328    {
329        if ($step == 2 && $key == 'field_default_value')
330        {
331            // 'field_length' == 1 defines radio buttons. Possible values are 1 or 2 only.
332            // 'field_length' == 2 defines checkbox. Possible values are 0 or 1 only.
333            // If we switch the type on step 2, we have to adjust field value.
334            // 1 is a common value for the checkbox and radio buttons.
335
336            // Adjust unchecked checkbox value.
337            // If we return or save settings from 2nd/3rd page
338            // and the checkbox is unchecked, set the value to 0.
339            if ($this->request->is_set('step') && !$this->request->is_set($key))
340            {
341                return 0;
342            }
343
344            // If we switch to the checkbox type but former radio buttons value was 2,
345            // which is not the case for the checkbox, set it to 0 (unchecked).
346            if ($field_data['field_length'] == 2 && $current_value == 2)
347            {
348                return 0;
349            }
350            // If we switch to the radio buttons but the former checkbox value was 0,
351            // which is not the case for the radio buttons, set it to 0.
352            else if ($field_data['field_length'] == 1 && $current_value == 0)
353            {
354                return 2;
355            }
356        }
357
358        if ($key == 'l_lang_options' && $this->request->is_set($key))
359        {
360            $field_data[$key] = $this->request->variable($key, array(0 => array('')), true);
361
362            return $current_value;
363        }
364
365        return parent::get_excluded_options($key, $action, $current_value, $field_data, $step);
366    }
367
368    /**
369    * {@inheritDoc}
370    */
371    public function prepare_hidden_fields($step, $key, $action, &$field_data)
372    {
373        if ($key == 'field_default_value')
374        {
375            $field_length = $this->request->variable('field_length', 0);
376
377            // Do a simple is set check if using checkbox.
378            if ($field_length == 2)
379            {
380                return $this->request->is_set($key);
381            }
382            return $this->request->variable($key, $field_data[$key], true);
383        }
384
385        $default_lang_options = array(
386            'l_lang_options'    => array(0 => array('')),
387            'lang_options'        => array(0 => ''),
388        );
389
390        if (isset($default_lang_options[$key]) && $this->request->is_set($key))
391        {
392            return $this->request->variable($key, $default_lang_options[$key], true);
393        }
394
395        return parent::prepare_hidden_fields($step, $key, $action, $field_data);
396    }
397
398    /**
399    * {@inheritDoc}
400    */
401    public function display_options(&$template_vars, &$field_data)
402    {
403        // Initialize these array elements if we are creating a new field
404        if (!count($field_data['lang_options']))
405        {
406            // No options have been defined for a boolean field.
407            $field_data['lang_options'][0] = '';
408            $field_data['lang_options'][1] = '';
409        }
410
411        $template_vars = array_merge($template_vars, array(
412            'S_BOOL'                    => true,
413            'L_LANG_OPTIONS_EXPLAIN'    => $this->user->lang['BOOL_ENTRIES_EXPLAIN'],
414            'FIRST_LANG_OPTION'            => $field_data['lang_options'][0],
415            'SECOND_LANG_OPTION'        => $field_data['lang_options'][1],
416        ));
417    }
418}