/
etsis_CustomField.php
235 lines (220 loc) · 10.3 KB
/
etsis_CustomField.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php namespace app\src\Core;
if (!defined('BASE_PATH'))
exit('No direct script access allowed');
class etsis_CustomField
{
private $_fields = [];
private $_required = false;
private $_field_id = 1;
private $_request_variables = [];
private $_location = '';
public function __construct($location = 'dashboard')
{
$this->_location = $location;
}
/**
* Adds field elements.
*
* @since 6.1.10
* @param string $field_type Custom field type (text, textarea, select, checkbox, radio).
* @param string $db_column Custom database column or field name.
* @param string $label Label tag.
* @param string $default_value Value pulled from the database.
* @param bool $required Determines if field is required.
* @param array $options Key value pairs for select options, checkbox options, or radio options.
*/
public function add($field_type, $db_column, $label, $default_value = '', $required = false, $options = '')
{
$this->_fields[$this->_field_id] = [
'type' => $field_type,
'column' => $db_column,
'label' => $label,
'value' => $default_value,
'required' => $required,
'options' => $options
];
if ($required) {
$this->_required[] = $this->_field_id;
}
$this->_field_id++;
return $this;
}
public function build()
{
$this->field_build();
}
private function field_build()
{
if ($this->_location == 'dashboard') {
foreach ($this->_fields as $key => $field) {
echo $this->custom_field($key, $field);
}
} else {
foreach ($this->_fields as $key => $field) {
echo $this->myetsis_appl_field($key, $field);
}
}
}
/**
* Text Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function text_field($key, $field)
{
$text = '<div class="form-group">' . "\n";
$text .= '<label class="col-md-3 control-label">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>' . "\n";
$text .= '<div class="col-md-8"><input class="form-control" type="text" name="' . $field['column'] . '" value="' . (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']) . '"' . ($field['required'] == true ? ' required' : ' ') . '/></div>' . "\n";
$text .= '</div>' . "\n";
return $text;
}
/**
* Textarea Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function textarea_field($key, $field)
{
$textarea = '<div class="form-group">';
$textarea .= '<label class="col-md-3 control-label">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
$textarea .= '<div class="col-md-8"><textarea class="form-control"' . ($field['required'] == true ? ' required' : '') . '>' . (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']) . '</textarea></div>';
$textarea .= '</div>';
return $textarea;
}
/**
* Select Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function select_field($key, $field)
{
$select = '<div class="form-group">' . "\n";
$select .= '<label class="col-md-3 control-label">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>' . "\n";
$select .= '<div class="col-md-8"><select name="' . $field['column'] . '" class="selectpicker form-control" data-style="btn-info" data-size="10" data-live-search="true"' . ($field['required'] == true ? ' required' : '') . '>' . "\n";
$selected = (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
$select .= '<option value="' . $ikey . '" ' . ($ikey == $selected ? 'selected' : '') . '>' . $ival . '</option>' . "\n";
}
$select .= '</select>' . "\n";
$select .= '</div>' . "\n";
$select .= '</div>' . "\n";
return $select;
}
/**
* Radio Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function radio_field($key, $field)
{
$radio = '<div class="form-group">' . "\n";
$radio .= '<label class="col-md-3 control-label">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>' . "\n";
$radio .= '<div class="col-md-8">' . "\n";
$selected = (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
$radio .= '<input type="radio" name="' . $field['column'] . '" class="radio" value="' . $ikey . '" ' . ($ikey == $selected ? 'checked' : '') . ' /> ' . $ival . '<br />';
}
$radio .= '</div>' . "\n";
$radio .= '</div>' . "\n";
return $radio;
}
/**
* Checkbox Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function checkbox_field($key, $field)
{
$checkbox = '<div class="form-group">' . "\n";
$checkbox .= '<label class="col-md-3 control-label">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>' . "\n";
$checkbox .= '<div class="col-md-8">' . "\n";
$selected = (array) (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
$checkbox .= '<input type="checkbox" name="' . $field['column'] . '[]" class="checkbox" value="' . $ikey . '" ' . (in_array($ikey, $selected) ? 'checked' : '') . ' /> ' . $ival . '<br />';
}
$checkbox .= '</div>' . "\n";
$checkbox .= '</div>' . "\n";
return $checkbox;
}
/**
* Default Custom Field
*
* @since 6.1.10
* @param type $key The key to the field.
* @param type $field The value of the key.
*/
private function custom_field($key, $field)
{
switch ($field['type']) {
case 'text':
echo $this->text_field($key, $field);
break;
case 'textarea':
echo $this->textarea_field($key, $field);
break;
case 'select':
echo $this->select_field($key, $field);
break;
case 'radio':
echo $this->radio_field($key, $field);
break;
case 'checkbox':
echo $this->checkbox_field($key, $field);
break;
}
}
/**
* Custom form field for application form
* via myetSIS self service.
*
* @since 6.1.10
*/
private function myetsis_appl_field($key, $field)
{
switch ($field['type']) {
case 'text':
echo '<div class="col-md-6"><label class="strong">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
echo '<input type="text" class="form-control" name="' . $field['column'] . '" readonly value="' . (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']) . '"' . ($field['required'] == true ? ' required' : ' ') . '/></div>';
break;
case 'textarea':
echo '<div class="col-md-6"><label class="strong">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
echo '<textarea name="' . $field['column'] . '" readonly class="form-control"' . ($field['required'] == true ? ' required' : '') . '>' . (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']) . '</textarea></div>';
break;
case 'select':
echo '<div class="col-md-6"><label class="strong">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
echo '<select name="' . $field['column'] . '" readonly class="select"' . ($field['required'] == true ? ' required' : '') . '>';
$selected = (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
echo '<option value="' . $ikey . '" ' . ($ikey == $selected ? 'selected' : '') . '>' . $ival . '</option>';
}
echo '</select></div>';
break;
case 'radio':
echo '<div class="col-md-6"><label class="strong">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
$selected = (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
echo '<input type="radio" name="' . $field['column'] . '" class="radio" readonly value="' . $ikey . '" ' . ($ikey == $selected ? 'checked' : '') . ' /> ' . $ival . '<br />';
}
echo '</div>';
break;
case 'checkbox':
echo '<div class="col-md-6"><label class="strong">' . ($field['required'] == true ? '<font color="red">*</font> ' : '') . $field['label'] . '</label>';
$selected = (array) (isset($this->_request_variables[$key]) ? $this->_request_variables[$key] : $field['value']);
foreach ($field['options'] as $ikey => $ival) {
echo '<input type="checkbox" name="' . $field['column'] . '[]" class="checkbox" readonly value="' . $ikey . '" ' . (in_array($ikey, $selected) ? 'checked' : '') . ' /> ' . $ival . '<br />';
}
echo '</div>';
break;
}
}
}