-
Notifications
You must be signed in to change notification settings - Fork 4
/
forms_B4-5.php
158 lines (158 loc) · 5.89 KB
/
forms_B4-5.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
<?php
class BtrpsForm
{
private $fields = [];
private $submitText = "Send";
private static $k = 0;
public function __construct($sText = "Send") {
$this->submitText = $sText;
}
public function addField($name, $label, $type = "text", $val = NULL, $placeholder = NULL, $required = false, $autocomplete = true)
{
$this->fields[] = (object) ['name' => $name, 'label' => $label, 'type' => $type, 'val' => $val, 'placeholder' => $placeholder, 'required' => $required, 'autocomplete' => $autocomplete];
return true;
}
public function __toString()
{
$r = '<form method="POST" role="form">';
foreach($this->fields as $k => $v)
{
$n = $v->name;
$l = $v->label;
$t = $v->type;
$val = $v->val;
$p = $v->placeholder;
$req = $v->required;
$autocomplete = $v->autocomplete;
$r.= '
<div class="form-group row">
<label for="input' . self::$k . '" class="control-label col-2"' . (in_array($t, ["radio", "select"]) ? NULL : ' style="cursor: pointer"') . '>' . $l . '</label>';
if(in_array($t, ["radio", "select"]))
{
if(!is_array($val))
die("Error: Values in radio or select control must be array!");
if($t=="radio")
{
$isFirst = true;
foreach($val as $k2 => $v2)
{
if($isFirst)
{
$r.= '
<div class="radio col-10"><label><input type="radio" name="' . $n . '" value="' . $k2 . '" checked> ' . $v2 . '</label></div><div>';
$isFirst = false;
} else
{
$r.= '
<div class="radio offset-2 col-10"><label><input type="radio" name="' . $n . '" value="' . $k2 . '"> ' . $v2 . '</label></div></div>';
if($k==count((array) $this->fields)-1)
{
$r.= '
<div class="form-group row">
<input type="submit" value="' . $this->submitText . '" class="btn btn-outline-secondary offset-2">
</div>';
};
};
};
} else
{
$r.= '<select name="' . $n . '" class="form-control col-10">';
foreach($val as $k2 => $v2)
{
$r.= '
<option value="' . $k2 . '">' . $v2 . '</option>
';
};
$r.= "</select></div>";
if($k==count((array) $this->fields)-1)
{
$r.= '</div>
<div class="form-group row">
<input type="submit" value="' . $this->submitText . '" class="btn btn-outline-secondary offset-2">
</div>';
}
};
} else
{
$r.= '
<input type="' . $t . '" name="' . $n . '" value="' . $val . '" placeholder="' . $p . '" autocomplete="' . ($autocomplete ? "on" : "off") . '" class="form-control col-10" id="input' . self::$k . '"' . (self::$k==0 ? ' autofocus' : NULL) . ($req ? ' required' : NULL) . '>
</div>
';
};
if($k==count((array) $this->fields)-1)
{
$r.= '
<div class="form-group row">
<input type="submit" value="' . $this->submitText . '" class="btn btn-outline-secondary offset-2">
</div>';
}
self::$k++;
};
return $r . "\n</form>\n";
}
public function get()
{
$r = '<form method="GET" role="form">';
foreach($this->fields as $k => $v)
{
$n = $v->name;
$l = $v->label;
$t = $v->type;
$val = $v->val;
$p = $v->placeholder;
$req = $v->required;
$autocomplete = $v->autocomplete;
$r.= '
<div class="form-group row">
<label for="input' . self::$k . '" class="control-label col-2"' . (in_array($t, ["radio", "select"]) ? NULL : ' style="cursor: pointer"') . '>' . $l . '</label>';
if(in_array($t, ["radio", "select"]))
{
if(!is_array($val))
die("Error: Values in radio or select control must be array!");
if($t=="radio")
{
$isFirst = true;
foreach($val as $k2 => $v2)
{
if($isFirst)
{
$r.= '
<div class="radio col-10"><label><input type="radio" name="' . $n . '" value="' . $k2 . '" checked> ' . $v2 . '</label></div><div>';
$isFirst = false;
} else
{
$r.= '
<div class="radio offset-2 col-10"><label><input type="radio" name="' . $n . '" value="' . $k2 . '"> ' . $v2 . '</label></div></div>';
};
};
} else
{
$r.= '<select name="' . $n . '" class="form-control col-10">';
foreach($val as $k2 => $v2)
{
$r.= '
<option value="' . $k2 . '">' . $v2 . '</option>
';
};
$r.= "</select></div>";
};
} else
{
$r.= '
<input type="' . $t . '" name="' . $n . '" value="' . $val . '" placeholder="' . $p . '" autocomplete="' . ($autocomplete ? "on" : "off") . '" class="form-control col-10" id="input' . self::$k . '"' . (self::$k==0 ? ' autofocus' : NULL) . ($req ? ' required' : NULL) . '>
</div>
';
};
if($k==count((array) $this->fields)-1)
{
$r.= '
<div class="form-group row">
<input type="submit" value="' . $this->submitText . '" class="btn btn-outline-secondary offset-2">
</div>';
}
self::$k++;
};
return $r . "\n</form>\n";
}
};
?>