Passed
Push — main ( f05cfd...29a9c1 )
by Stefan
02:38
created

FormInput::buildClass()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 10
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
        parent::__construct($wFlags);
41
        $this->strName = $strName;
42
        $this->size = $size;
43
        $this->strValidate = 'aEdit';
44
        $this->strType = 'text';
45
        $this->strSelectImg = '';
46
        $this->strSelectImgTitle = '';
47
        $this->strBrowseServer = '';
48
        $this->strSuffix = '';
49
        
50
        $this->addFlags($wFlags);
51
    }
52
    
53
    /**
54
     * Add flags to existing element. 
55
     * Maybe overloaded in derived class(es) 
56
     * @param int $wFlags
57
     * 
58
     * {@inheritDoc}
59
     * @see \SKien\Formgenerator\FormElement::addFlags()
60
     */
61
    public function addFlags($wFlags) : void
62
    {
63
        $this->oFlags->add($wFlags);
64
    }
65
    
66
    /**
67
     * set image and title for select-button (leave strImg blank for default)
68
     * @param string $strImg
69
     * @param string $strTitle (default = '')
70
     */
71
    public function setSelectImg(string $strImg, string $strTitle = '') : void
72
    {
73
        $this->strSelectImg = $strImg;
74
        $this->strSelectImgTitle = $strTitle;
75
    }
76
    
77
    /**
78
     * @param string $strBrowseServer
79
     */
80
    public function setBrowseServer(string $strBrowseServer) : void
81
    {
82
        $this->strBrowseServer = $strBrowseServer;
83
    }
84
    
85
    /**
86
     * @param string $strSuffix
87
     */
88
    public function setSuffix(string $strSuffix) : void
89
    {
90
        $this->strSuffix = $strSuffix;
91
    }
92
    
93
    /**
94
     * Build the HTML-notation for the input element.
95
     * {@inheritDoc}
96
     * @see \SKien\Formgenerator\FormElement::getHTML()
97
     */
98
    public function getHTML() : string
99
    {
100
        $this->processFlags();
101
        $this->setSize();
102
        $strHTML = $this->buildContainerDiv();
103
        
104
        $this->strID = $this->strID ?: $this->strName;
105
        
106
        $strHTML .= '<input';
107
        $strHTML .= ' type="' . $this->strType . '"';
108
        $strHTML .= ' name="' . $this->strName . '"';
109
        $strHTML .= $this->buildClass();
110
        $strHTML .= $this->buildID();
111
        $strHTML .= $this->buildStyle();
112
        $strHTML .= $this->buildAttributes();
113
        $strHTML .= $this->buildTabindex();
114
        $strHTML .= $this->buildValue();
115
        $strHTML .= '>';
116
        
117
        // some additional elements
118
        if ($this->oFlags->isSet(FormFlags::ADD_DTU)) {
119
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DTU) . '"  alt="[X]"';
120
            $strHTML .= ' id="' . $this->strName . 'DTU"';
121
            $strHTML .= ' title="aktuelle Datum Uhrzeit / Benutzername eintragen"';
122
            $strHTML .= ' onclick="OnInsertDateTimeUser(' . "'" . $this->strName . "'" . ');">';
123
        } else if ($this->oFlags->isSet(FormFlags::ADD_DATE_PICKER)) {
124
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DATE_PICKER) . '" alt="[X]"';
125
            $strHTML .= ' id="' . $this->strName . 'DP"';
126
            $strHTML .= ' title="Datum auswählen"';
127
            $strHTML .= ' onclick="OnDatePicker(' . "'" . $this->strName . "'" . ');">';
128
        } else if ($this->oFlags->isSet(FormFlags::ADD_TIME_PICKER)) {
129
            $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_TIME_PICKER) . '"  alt="[X]"';
130
            $strHTML .= ' id="' . $this->strName . 'TP"';
131
            $strHTML .= ' title="Uhrzeit auswählen"';
132
            $strHTML .= ' onclick="OnTimePicker(' . "'" . $this->strName . "'" . ');">';
133
        }
134
        $strHTML .= $this->buildSelectImage();
135
        $strHTML .= $this->buildSuffix();
136
        
137
        $strHTML .= '</div>' . PHP_EOL;
138
        
139
        return $strHTML;
140
    }
141
    
142
    /**
143
     * Set the tab index of the element.
144
     * Method is called from the PageGenerator after an element is added to the form.
145
     * @param int $iTabindex
146
     * @return int the number of indexes, the element needs
147
     */
148
    public function setTabindex(int $iTabindex) : int
149
    {
150
        if ($this->oFlags->isSet(FormFlags::HIDDEN | FormFlags::READ_ONLY | FormFlags::DISABLED)) {
151
            return 0;
152
        }
153
        $this->iTabindex = $iTabindex;
154
        return 1;
155
    }
156
    
157
    /**
158
     * Process the current flags before the HTML is generated.
159
     */
160
    protected function processFlags() : void
161
    {
162
        $this->setTypeFromFlags();
163
        
164
        if ($this->oFlags->isSet(FormFlags::MANDATORY)) {
165
            $this->addAttribute('required');
166
        }
167
        if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
168
            $this->addAttribute('readonly');
169
        } else if ($this->oFlags->isSet(FormFlags::DISABLED)) {
170
            $this->addAttribute('disabled');
171
        }
172
    }
173
174
    /**
175
     * Set the type depending on some flags
176
     */
177
    protected function setTypeFromFlags() : void
178
    {
179
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
180
            $this->strType = 'hidden';
181
        }
182
        if ($this->oFlags->isSet(FormFlags::PASSWORD)) {
183
            $this->strType = 'password';
184
        }
185
        if ($this->oFlags->isSet(FormFlags::FILE)) {
186
            $this->strType = 'file';
187
            if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
188
                $this->addStyle('visibility', 'hidden');
189
            }
190
        }
191
    }
192
    
193
    /**
194
     * Set the size of the element.
195
     * If property $size contains numeric value, the HTML attrib 'size' is set, in case of a
196
     * string a width information including dimension (px, em, ...) is assumed.
197
     * 
198
     */
199
    protected function setSize() : void
200
    {
201
        if ($this->oFlags->isSet(FormFlags::HIDDEN)) {
202
            $this->size = '';
203
        }
204
        if ((is_numeric($this->size)) && ($this->size > 0)) {
205
            $this->addAttribute('size', (string)$this->size);
206
        } else if (!empty($this->size)) {
207
            // size given as string including dimension
208
            $this->addStyle('width', $this->size);
209
        }
210
    }
211
212
    /**
213
     * {@inheritDoc}
214
     * @see \SKien\Formgenerator\FormElement::buildClass()
215
     */
216
    protected function buildClass() : string
217
    {
218
        if (!empty($this->strClass)) {
219
            $this->strClass .= ' ';
220
        }
221
        $this->strClass .= ($this->oFlags->isSet(FormFlags::MANDATORY)) ? ' inputMand' : ' inputOK';
222
        if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) {
223
            $this->strClass .= '_R';
224
        }
225
        return parent::buildClass();
226
    }
227
    
228
    /**
229
     * Build the markup for a suffix succeeding the input element.
230
     * @return string
231
     */
232
    protected function buildSuffix() : string
233
    {
234
        $strHTML = '';
235
        if (!empty($this->strSuffix)) {
236
            if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
237
                $strHTML .= '&nbsp;<span class="readonly">' . $this->strSuffix . '</span>';
238
            } else {
239
                $strHTML .= '&nbsp;' . $this->strSuffix;
240
            }
241
        }
242
        return $strHTML;
243
    }
244
    
245
    /**
246
     * Build the markup for a select button.
247
     * @param string $strClass
248
     * @return string
249
     */
250
    protected function buildSelectImage(string $strClass = 'picker') : string
251
    {
252
        $strHTML = '';
253
        if ($this->oFlags->isSet(FormFlags::ADD_SELBTN)) {
254
            $strImg = $this->strSelectImg;
255
            $strTitle = $this->strSelectImgTitle;
256
            if (empty($strImg)) {
257
                $strImg = $this->getStdImage(self::IMG_SEARCH);
258
            }
259
            $strHTML .= '<img class="' . $strClass . '" src="' . $strImg . '" alt="Auswahl"';
260
            if (!empty($strTitle)) {
261
                $strHTML .= ' title="' . $strTitle . '"';
262
            }
263
            if (!empty($this->strBrowseServer)) {
264
                $strHTML .= " onclick=\"BrowseServer('" . $this->strName . "','','" . $this->strBrowseServer . "');\">";
265
                if ($this->oFlags->isSet(FormFlags::READ_ONLY)) {
266
                    $strHTML .= '<img class="picker" src="' . $this->getStdImage(self::IMG_DELETE) . '" alt="L&ouml;schen" title="L&ouml;schen" ';
267
                    $strHTML .= " onclick=\"ResetInput('" . $this->strName . "');\">";
268
                }
269
            } else {
270
                $strHTML .= ' onclick="OnSelect(' . "'" . $this->strName . "'" . ');">';
271
            }
272
        }
273
        return $strHTML;
274
    }
275
}
276