-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-flexi-html_form.php
167 lines (153 loc) · 4.08 KB
/
class-flexi-html_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Create HTML form tags
* PHP Form Class from ODude.com
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
* @link https://odude.com/
* @since 1.0.0
* @author ODude <[email protected]>
* @package Flexi
* @subpackage Flexi/includes
*/
class flexi_HTML_Form
{
private $tag;
private $xhtml;
public function __construct($xhtml = true)
{
$this->xhtml = $xhtml;
}
public function startForm($action = '#', $method = 'post', $id = '', $attr_ar = array())
{
$str = "<form action=\"$action\" method=\"$method\"";
if (!empty($id)) {
$str .= " id=\"$id\"";
}
$str .= $attr_ar ? $this->addAttributes($attr_ar) . '>' : '>';
return $str;
}
private function addAttributes($attr_ar)
{
$str = '';
// check minimized (boolean) attributes
$min_atts = array(
'checked', 'disabled', 'readonly', 'multiple',
'required', 'autofocus', 'novalidate', 'formnovalidate',
); // html5
foreach ($attr_ar as $key => $val) {
if (in_array($key, $min_atts)) {
if (!empty($val)) {
$str .= $this->xhtml ? " $key=\"$key\"" : " $key";
}
} else {
$str .= " $key=\"$val\"";
}
}
return $str;
}
public function addInput($type, $name, $value, $attr_ar = array())
{
if (isset($attr_ar['type']) && $attr_ar['type'] == 'text_array') {
$str = "<input type=\"$type\" name=\"" . esc_attr($name) . "[]\" value=\"" . esc_attr($value) . "\"";
} else {
$str = "<input type=\"$type\" name=\"" . esc_attr($name) . "\" value=\"" . esc_attr($value) . "\"";
}
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= $this->xhtml ? ' />' : '>';
return $str;
}
public function addTextarea($name, $rows = 4, $cols = 30, $value = '', $attr_ar = array())
{
$str = "<textarea name=\"" . esc_attr($name) . "\" rows=\"" . esc_attr($rows) . "\" cols=\"" . esc_attr($cols) . "\"";
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= ">$value</textarea>";
return $str;
}
// for attribute refers to id of associated form element
public function addLabelFor($forID, $text, $label_class, $attr_ar = array())
{
$str = "<label class='fl-label " . esc_attr($label_class) . "' for=\"$forID\"";
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= ">" . esc_attr($text) . "</label>";
return $str;
}
// from parallel arrays for option values and text
public function addSelectListArrays(
$name,
$val_list,
$txt_list,
$selected_value = null,
$header = null,
$attr_ar = array()
) {
$option_list = array_combine($val_list, $txt_list);
$str = $this->addSelectList($name, $option_list, true, $selected_value, $header, $attr_ar);
return $str;
}
// option values and text come from one array (can be assoc)
// $bVal false if text serves as value (no value attr)
public function addSelectList(
$name,
$option_list,
$bVal = true,
$selected_value = null,
$header = null,
$attr_ar = array()
) {
$str = "<select name=\"$name\"";
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= ">\n";
if (isset($header)) {
$str .= " <option value=\"\">$header</option>\n";
}
foreach ($option_list as $val => $text) {
$str .= $bVal ? " <option value=\"$val\"" : " <option";
if (isset($selected_value) && ($selected_value === $val || $selected_value === $text)) {
$str .= $this->xhtml ? ' selected="selected"' : ' selected';
}
$str .= ">$text</option>\n";
}
$str .= "</select>";
return $str;
}
public function endForm()
{
return "</form>";
}
public function startTag($tag, $attr_ar = array())
{
$this->tag = esc_attr($tag);
$str = "<$tag";
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= '>';
return $str;
}
public function endTag($tag = '')
{
$str = esc_attr($tag) ? "</$tag>" : "</$this->tag>";
$this->tag = '';
return $str;
}
public function addEmptyTag($tag, $attr_ar = array())
{
$str = "<$tag";
if ($attr_ar) {
$str .= $this->addAttributes($attr_ar);
}
$str .= $this->xhtml ? ' />' : '>';
return $str;
}
}