Passed
Push — main ( e2b5f1...353913 )
by Stefan
02:09
created

FormCheck   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 48
dl 0
loc 136
rs 10
c 2
b 0
f 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A buildReadonlySurrogate() 0 15 3
A buildUncheckedSurrogate() 0 15 1
A getBoolValue() 0 15 3
A buildCheckBox() 0 13 2
A getHTML() 0 21 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Checkbox input field.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormCheck extends FormInput
14
{
15
    /**
16
     * Create a checkbox.
17
     * As default, the value 'on' is submitted for a checked element. This value can be 
18
     * changed by setting a btnValue for the elementname in the FormData. <br/>
19
     * <b>Note: HTML does not submit any value for unchecked checkboxes !!!.</b>
20
     * @param string $strName   name AND id of the element
21
     * @param int $wFlags    (default: 0)
22
     * @param string $strSuffix Text after the checkbox (default: '')
23
     */
24
    public function __construct(string $strName, int $wFlags = 0, string $strSuffix = '') 
25
    {
26
        $this->oFlags = new FormFlags($wFlags);
27
        $this->strName = $strName;
28
        $this->strSuffix = $strSuffix;
29
        if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) {
30
            $this->addAttribute('disabled');
31
        }
32
    }
33
    
34
    /**
35
     * Build the HTML-markup for the checkbox.
36
     * Checkbox is a special case, as it behaves particularly with regard to the transferred 
37
     * value and does not support read-only mode.
38
     * There is special treatment for both cases, which is explained in more detail in the 
39
     * respective code areas bolow.  
40
     * @return string
41
     */
42
    public function getHTML() : string 
43
    {
44
        $this->processFlags();
45
        $bChecked = $this->getBoolValue();
46
        if ($bChecked) {
47
            $this->addAttribute('checked');
48
        }
49
        
50
        $strHTML = $this->buildContainerDiv();
51
        
52
        $strHTML .= $this->buildUncheckedSurrogate();
53
        $strHTML .= $this->buildCheckBox();
54
        $strHTML .= $this->buildReadonlySurrogate($bChecked);
55
        
56
        if (!empty($this->strSuffix)) {
57
            $strHTML .= '&nbsp;' . $this->strSuffix;
58
        }
59
        
60
        $strHTML .= '</div>' . PHP_EOL;
61
62
        return $strHTML;
63
    }
64
    
65
    /**
66
     * Get the value as bool. <ul>
67
     * <li> 'real' bool values as is </li>
68
     * <li> numeric: 0 -> false, all other -> true </li>
69
     * <li> string: '1', 'on', 'true', 'yes' (case insensitive) -> true, all other -> false </li></ul>
70
     * @return bool
71
     */
72
    protected function getBoolValue() : bool
73
    {
74
        $bChecked = false;
75
        $value = $this->oFG->getData()->getValue($this->strName);
76
        if (is_bool($value)) {
77
            $bChecked = $value;
78
        } else {
79
            if (is_numeric($value)) {
80
                $bChecked = ($value !== 0);
81
            } else {
82
                $value = strtolower($value);
83
                $bChecked = in_array($value, ['1', 'on', 'true', 'yes']);
84
            }
85
        }
86
        return $bChecked;
87
    }
88
    
89
    /**
90
     * Build the checkbox.
91
     * @return string
92
     */
93
    protected function buildCheckBox() : string
94
    {
95
        $strHTML = '<input type="checkbox"';
96
        $strHTML .= $this->buildStyle();
97
        $strHTML .= $this->buildAttributes();
98
        $strHTML .= $this->buildTabindex();
99
        $strHTML .= ' id="' . $this->strName . '"';
100
        if (!$this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) {
101
            $strHTML .= ' name="' . $this->strName . '"';
102
        }
103
        $strHTML .= '>';
104
        
105
        return $strHTML;
106
    }
107
    
108
    /**
109
     * Build the surrogate for unchecked box.
110
     * @return string
111
     */
112
    protected function buildUncheckedSurrogate() : string
113
    {
114
        // We insert a hidden edit field with same name/id BEFORE we create the checkbox
115
        // and  alwas set its value to 'off'.
116
        // If the checkbox is activated, 'on' (or the value set with BtnValue) is posted as normal. 
117
        // However, since the value of a non-activated checkbox is not transferred at all, in this 
118
        // case the hidden field defined in the previous order comes into play and the fixed value 
119
        // 'off' is transferred.
120
        // This is particularly helpful/important if a database operation is to be carried out 
121
        // dynamically on the basis of the transferred fields: If the 'off' were not transferred, 
122
        // the field would not be taken into account either and would therefore never be set from 
123
        // 'on' to 'off'!! 
124
        $strHTML = '<input type="hidden" value="off" name="' . $this->strName . '">';
125
        
126
        return $strHTML;
127
    }
128
    
129
    /**
130
     * Build the surrogate in case of readonly checkbox.
131
     * @param bool $bChecked
132
     * @return string
133
     */
134
    protected function buildReadonlySurrogate(bool $bChecked) : string
135
    {
136
        // NOTE: because checkboxes don't support readonly-attribute and disabled checkboxes
137
        // are not posted to reciever, we insert an hidden field with name and id to keep
138
        // value 'alive'
139
        // -> so we dont set name and id for readonly or disabled checkbox!
140
        $strHTML = '';
141
        if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) {
142
            $strHTML .= '<input';
143
            $strHTML .= ' type="hidden"';
144
            $strHTML .= ' name="' . $this->strName . '"';
145
            $strValue = ($bChecked) ? 'on' :'off';
146
            $strHTML .= ' value="' . $strValue . '">';
147
        }
148
        return $strHTML;        
149
    }
150
}
151