PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.94
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.94
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Services / FormBuilder.php

FormBuilder.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.94, at app/Services/FormBuilder.php

79 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\App\Services;
4
5 class FormBuilder
6 {
7
8 private $formFields = [];
9
10 public function __construct($formFields)
11 {
12 $this->formFields = $formFields;
13 }
14
15 public function render()
16 {
17 foreach ($this->formFields as $name => $field) {
18 $field['name'] = $name;
19 $this->renderField($field);
20 }
21 }
22
23 private function renderField($field)
24 {
25 $type = $field['type'];
26 $label = $field['label'] ?? '';
27 $name = $field['name'];
28 $required = $field['required'] ?? false;
29 $options = $field['options'] ?? [];
30 $value = $field['value'] ?? '';
31 $readonly = $field['readonly'] ?? false;
32
33 $atts = array_filter([
34 'type' => in_array($type, ['text', 'email', 'password']) ? $type : '',
35 'id' => 'fcom_' . $name,
36 'name' => $name,
37 'value' => $value,
38 'required' => '',//$required ? 'required' : '',
39 'readonly' => $readonly ? 'readonly' : '',
40 'placeholder' => $field['placeholder'] ?? '',
41 'class' => $field['input_class'] ?? '',
42 ]);
43
44 echo "<div id='fcom_group_" . esc_attr($name) . "' class='fcom_form-group'>";
45 if ($label):
46 echo "<div class='fcom_form_label'><label for='" . esc_attr($atts['id']) . "'>$label</label></div>";
47 endif;
48
49 echo "<div class='fcom_form_input'>";
50
51 if (isset($atts['type'])) {
52 echo "<input " . $this->printAtts($atts) . ">";
53 } elseif ($type === 'select') {
54 echo "<select id='$name' name='$name' " . ($required ? 'required' : '') . ">";
55 foreach ($options as $option) {
56 echo "<option value='$option'>$option</option>";
57 }
58 echo "</select>";
59 } else if ($type === 'inline_checkbox') {
60 echo "<div class='fcom_inline_checkbox'>";
61 echo "<input type='checkbox' " . $this->printAtts($atts) . ">";
62 echo "<label for='" . esc_attr($atts['id']) . "'>" . wp_kses_post($field['inline_label']) . "</label>";
63 echo "</div>";
64 } else if ($type === 'textarea') {
65 echo "<textarea " . $this->printAtts($atts) . "></textarea>";
66 }
67 echo "</div></div>";
68 }
69
70 private function printAtts($atts)
71 {
72 $result = '';
73 foreach ($atts as $key => $value) {
74 $result .= " " . esc_attr($key) . "='" . esc_attr($value) . "'";
75 }
76 return $result;
77 }
78 }
79