Passed
Push — main ( b2d4dd...f05cfd )
by Stefan
01:14
created

FormInput::hasTab()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Base-class for all elements intended to get user input.
8
 *
9
 * #### History
10
 * - *2020-05-12*   initial version
11
 * - *2021-01-07*   PHP 7.4
12
 *
13
 * @package Formgenerator
14
 * @version 1.1.0
15
 * @author Stefanius <[email protected]>
16
 * @copyright MIT License - see the LICENSE file for details
17
 */
18
class FormInput extends FormElement
19
{
20
    /** @var string value input type    */
21
    protected string $strType;
22
    /** @var int|string size as number or as string including dimension ('%', 'px', 'em') */
23
    protected $size;
24
    /** @var string image displayed, if selectbutton is enabled     */
25
    protected string $strSelectImg;
26
    /** @var string tooltip for selectbutton     */
27
    protected string $strSelectImgTitle;
28
    /** @var string additional info for BrowseServer - Button     */
29
    protected string $strBrowseServer;
30
    /** @var string suffix directly after the element     */
31
    protected string $strSuffix;
32
    
33
    /**
34
     * @param string $strName Name (also used as ID, if not set separate)
35
     * @param int|string $size number set the size-attribute, a string is used for the width attribute
36
     * @param int $wFlags       
37
     */
38
    public function __construct(string $strName, $size, int $wFlags = 0) 
39
    {
40
        $this->oFlags = new FormFlags($wFlags);
41
        $this->strName = $strName;
42
        $this->size = $size;
43
        $this->iTab = -1;
44
        $this->strValidate = 'aEdit';
45
        $this->strType = 'text';
46
        $this->strSelectImg = '';
47
        $this->strSelectImgTitle = '';
48
        $this->strBrowseServer = '';
49
        $this->strSuffix = '';
50
        
51
        $this->addFlags($wFlags);
52
    }
53
    
54
    /**
55
     * Add flags to existing element. 
56
     * Maybe overloaded in derived class(es) 
57
     * @param int $wFlags
58
     * 
59
     * {@inheritDoc}
60
     * @see \SKien\Formgenerator\FormElement::addFlags()
61
     */
62
    public function addFlags($wFlags) : void
63
    {
64
        $this->oFlags->add($wFlags);
65
    }
66
    
67
    /**
68
     * set image and title for select-button (leave strImg blank for default)
69
     * @param string $strImg
70
     * @param string $strTitle (default = '')
71
     */
72
    public function setSelectImg(string $strImg, string $strTitle = '') : void
73
    {
74
        $this->strSelectImg = $strImg;
75
        $this->strSelectImgTitle = $strTitle;
76
    }
77
    
78
    /**
79
     * @param string $strBrowseServer
80
     */
81
    public function setBrowseServer(string $strBrowseServer) : void
82
    {
83
        $this->strBrowseServer = $strBrowseServer;
84
    }
85
    
86
    /**
87
     * @param string $strSuffix
88
     */
89
    public function setSuffix(string $strSuffix) : void
90
    {
91
        $this->strSuffix = $strSuffix;
92
    }
93
    
94
    /**
95
     * Build the HTML-notation for the input element.
96
     * {@inheritDoc}
97
     * @see \SKien\Formgenerator\FormElement::getHTML()
98
     */
99
    public function getHTML() : string
100
    {
101
        $this->processFlags();
102
        $this->setSize();
103
        $strHTML = $this->buildContainerDiv();
104
        
105
        $this->strID = $this->strID ?: $this->strName;
106
        
107
        $strHTML .= '<input';
108
        $strHTML .= ' type="' . $this->strType . '"';
109
        $strHTML .= ' name="' . $this->strName . '"';
110
        $strHTML .= $this->buildClass();
111
        $strHTML .= $this->buildID();
112
        $strHTML .= $this->buildStyle();
113
        $strHTML .= $this->buildAttributes();
114
        $strHTML .= $this->buildTab();
115
        $strHTML .= $this->buildValue();
116
        $strHTML .= '>';
117
        
118
        // some additional elements
119
        if ($this->oFlags->isSet(FormFlags::ADD_DTU)) {
120
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DTU) . '"  alt="[X]"';
121
            $strHTML .= ' id="' . $this->strName . 'DTU"';
122
            $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"';
123
            $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">';
124
        } else if ($this->oFlags->isSet(FormFlags::ADD_DATE_PICKER)) {
125
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DATE_PICKER) . '" alt="[X]"';
126
            $strHTML .= ' id="' . $this->strName . 'DP"';
127
            $strHTML .= ' title="Datum auswählen"';
128
            $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">';
129
        } else if ($this->oFlags->isSet(FormFlags::ADD_TIME_PICKER)) {
130
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_TIME_PICKER) . '"  alt="[X]"';
131
            $strHTML .= ' id="' . $this->strName . 'TP"';
132
            $strHTML .= ' title="Uhrzeit auswählen"';
133
            $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">';
134
        }
135
        $strHTML .= $this->buildSelectImage();
136
        $strHTML .= $this->buildSuffix();
137
        
138
        $strHTML .= '</div>' . PHP_EOL;
139
        
140
        return $strHTML;
141
    }
142
    
143
    /**
144
     * Input elements don't need tab index if hidden, read-only or disabled
145
     * {@inheritDoc}
146
     * @see \SKien\Formgenerator\FormElement::hasTab()
147
     */
148
    public function hasTab() : bool
149
    {
150
        return (!$this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED));
151
    }
152
    
153
    /**
154
     * Process the current flags before the HTML is generated.
155
     */
156
    protected function processFlags() : void
157
    {
158
        $this->setTypeFromFlags();
159
        
160
        if ($this->oFlags->isSet(FormFlags::MANDATORY)) {
161
            $this->addAttribute('required');
162
        }
163
        if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
164
            $this->addAttribute('readonly');
165
        } else if ($this->oFlags->isSet(FormFlags::DISABLED)) {
166
            $this->addAttribute('disabled');
167
        }
168
        if ($this->oFlags->isSet(FormFlags::ADD_EUR)) {
169
            $this->strSuffix = 'EUR';
170
        }
171
    }
172
173
    /**
174
     * Set the type depending on some flags
175
     */
176
    protected function setTypeFromFlags() : void
177
    {
178
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
179
            $this->strType = 'hidden';
180
        }
181
        if ($this->oFlags->isSet(FormFlags::PASSWORD)) {
182
            $this->strType = 'password';
183
        }
184
        if ($this->oFlags->isSet(FormFlags::FILE)) {
185
            $this->strType = 'file';
186
            if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
187
                $this->addStyle('visibility', 'hidden');
188
            }
189
        }
190
    }
191
    
192
    /**
193
     * Set the size of the element.
194
     * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a
195
     * string a width information including dimension (px, em, ...) is assumed.
196
     * 
197
     */
198
    protected function setSize() : void
199
    {
200
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
201
            $this->size = '';
202
        }
203
        if ((is_numeric($this->size)) && ($this->size > 0)) {
204
            $this->addAttribute('size', (string)$this->size);
205
        } else if (!empty($this->size)) {
206
            // size given as string including dimension
207
            $this->addStyle('width', $this->size);
208
        }
209
    }
210
211
    /**
212
     * {@inheritDoc}
213
     * @see \SKien\Formgenerator\FormElement::buildClass()
214
     */
215
    protected function buildClass() : string
216
    {
217
        if (!empty($this->strClass)) {
218
            $this->strClass .= ' ';
219
        }
220
        $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK';
221
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
222
            $this->strClass .= '_R';
223
        }
224
        if ($this->oFlags->isSet(FormFlags::ADD_COLOR_PICKER)) {
225
            $this->strClass .= ' jscolor {hash:true}';
226
        }
227
        return parent::buildClass();
228
    }
229
    
230
    /**
231
     * Build the markup for a suffix succeeding the input element.
232
     * @return string
233
     */
234
    protected function buildSuffix() : string
235
    {
236
        $strHTML = '';
237
        if (!empty($this->strSuffix)) {
238
            if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
239
                $strHTML .= '&nbsp;<span class="readonly">' . $this->strSuffix . '</span>';
240
            } else {
241
                $strHTML .= '&nbsp;' . $this->strSuffix;
242
            }
243
        }
244
        return $strHTML;
245
    }
246
    
247
    /**
248
     * Build the markup for a select button.
249
     * @param string $strClass
250
     * @return string
251
     */
252
    protected function buildSelectImage(string $strClass = 'picker') : string
253
    {
254
        $strHTML = '';
255
        if ($this->oFlags->isSet(FormFlags::ADD_SELBTN)) {
256
            $strImg = $this->strSelectImg;
257
            $strTitle = $this->strSelectImgTitle;
258
            if (empty($strImg)) {
259
                $strImg = $this->getStdImage(self::IMG_SEARCH);
260
            }
261
            $strHTML .= '<img class="' . $strClass . '" src="' . $strImg . '" alt="Auswahl"';
262
            if (!empty($strTitle)) {
263
                $strHTML .= ' title="' . $strTitle . '"';
264
            }
265
            if (!empty($this->strBrowseServer)) {
266
                $strHTML .= " onclick=\"BrowseServer('" . $this->strName . "','','" . $this->strBrowseServer . "');\">";
267
                if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
268
                    $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DELETE) . '" alt="L&ouml;schen" title="L&ouml;schen" ';
269
                    $strHTML .= " onclick=\"ResetInput('" . $this->strName . "');\">";
270
                }
271
            } else {
272
                $strHTML .= ' onclick="OnSelect(' . "'" . $this->strName . "'" . ');">';
273
            }
274
        }
275
        return $strHTML;
276
    }
277
}
278