-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFormFields.php
More file actions
executable file
·206 lines (177 loc) · 6 KB
/
AbstractFormFields.php
File metadata and controls
executable file
·206 lines (177 loc) · 6 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* @Package: MaplePHP - Form builder template
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Form;
use MaplePHP\Form\Interfaces\FormFieldsInterface;
use MaplePHP\Form\Arguments;
abstract class AbstractFormFields extends Arguments implements FormFieldsInterface
{
/**
* The input field HTML container
* @param callable $callback return output
* @return string
*/
public function container(callable $callback): string
{
$length = count($this->items);
$out = "";
$out .= "<div class=\"mb-15\" data-count=\"{$length}\">";
if (!is_null($this->label)) {
$boolLength = (isset($this->validate['length'][0]) && $this->validate['length'][0] > 0);
$req = ($boolLength) ? "*" : "";
$out .= "<label>{$this->label}<span class=\"req\">{$req}</span><div class=\"message hide\"></div></label>";
}
if (!is_null($this->description)) {
$out .= "<div class=\"description legend\">{$this->description}</div>";
}
$out .= $callback();
$out .= "</div>";
return $out;
}
/**
* Input text
* @return string
*/
public function text(): string
{
if (isset($this->attrArr['data-clear'])) {
$this->attr(["required" => "required"]);
}
return $this->container(function () {
$typeAdd = (isset($this->attrArr['type']) ? "" : "type=\"text\" ");
return "<input {$typeAdd}{$this->attr}name=\"{$this->name}\" data-name=\"{$this->dataName}\" value=\"{$this->value}\">";
});
}
/**
* Input hidden
* @return string
*/
public function hidden(): string
{
return "<input type=\"hidden\" {$this->attr}name=\"{$this->name}\" data-name=\"{$this->dataName}\" value=\"{$this->value}\">";
}
/**
* Input text
* @return string
*/
public function date(): string
{
if (isset($this->attrArr['data-clear'])) {
$this->attr(["required" => "required"]);
}
return $this->container(function () {
$typeAdd = (isset($this->attrArr['type']) ? null : "type=\"date\" ");
return "<input {$typeAdd}{$this->attr}name=\"{$this->name}\" data-name=\"{$this->dataName}\" value=\"{$this->value}\">";
});
}
/**
* Input text
* @return string
*/
public function datetime(): string
{
if (isset($this->attrArr['data-clear'])) {
$this->attr(["required" => "required"]);
}
return $this->container(function () {
$typeAdd = (isset($this->attrArr['type']) ? null : "type=\"datetime-local\" ");
return "<input {$typeAdd}{$this->attr}name=\"{$this->name}\" data-name=\"{$this->dataName}\" value=\"{$this->value}\">";
});
}
/**
* Input textarea
* @return string
*/
public function textarea(): string
{
return $this->container(function () {
return "<textarea {$this->attr}name=\"{$this->name}\" data-name=\"{$this->dataName}\">{$this->value}</textarea>";
});
}
/**
* Input select list
* @return string
*/
public function select(): string
{
return $this->container(function () {
$name = $this->name;
if (isset($this->attrArr['multiple'])) {
$name .= "[]";
}
$out = "<select {$this->attr}name=\"{$name}\" data-name=\"{$this->dataName}\" autocomplete=\"off\">";
foreach ($this->items as $val => $item) {
$selected = ($this->isChecked($val)) ? "selected=\"selected\" " : null;
$out .= "<option {$selected}value=\"{$val}\">{$item}</option>";
}
$out .= "</select>";
return $out;
});
}
/**
* Input radio
* @return string
*/
public function radio(): string
{
return $this->container(function () {
$out = "";
foreach ($this->items as $val => $item) {
$checked = ($this->isChecked($val)) ? "checked=\"checked\" " : null;
$out .= "<label class=\"radio item small\">";
$out .= "<input {$checked}type=\"radio\" {$this->attr}name=\"{$this->name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
$out .= "</label>";
}
return $out;
});
}
/**
* Input checkbox
* @return string
*/
public function checkbox(): string
{
return $this->container(function () {
$out = "";
$length = count($this->items);
foreach ($this->items as $val => $item) {
$name = ($length > 1) ? "{$this->name}[]" : $this->name;
$checked = ($this->isChecked($val)) ? "checked=\"checked\" " : null;
$out .= "<label class=\"checkbox item small\">";
$out .= "<input {$checked}type=\"checkbox\" {$this->attr}name=\"{$name}\" value=\"{$val}\"><span class=\"title\">{$item}</span>";
$out .= "</label>";
}
return $out;
});
}
/**
* Group fields
* With some know how you can make it dynamical
* @return string
*/
public function group()
{
$lastKey = $this->lastKey();
$out = "<div class=\"mb-20 group\" {$this->attr}data-key=\"{$lastKey}\">";
if (!is_null($this->label)) {
$out .= "<label>{$this->label}</label>";
}
if (!is_null($this->description)) {
$out .= "<div class=\"legend mb-20 v3\">{$this->description}</div>";
}
$out .= "<ul>";
$out .= $this->groupFields(function ($o, $_val) {
$out = "<li>";
$out .= $o;
$out .= "</li>";
return $out;
});
$out .= "</ul>";
$out .= "</div>";
return $out;
}
}