Form.php
206 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDM\Form; |
| 4 | |
| 5 | use WPDM\__\__; |
| 6 | |
| 7 | class Form |
| 8 | { |
| 9 | private $fieldGroups = []; |
| 10 | private $formFields; |
| 11 | private $method = 'post'; |
| 12 | private $action = ''; |
| 13 | private $name = 'form'; |
| 14 | private $id = 'form'; |
| 15 | private $class = 'form'; |
| 16 | public $submit_button = []; |
| 17 | public $error = ''; |
| 18 | public $noForm = false; |
| 19 | |
| 20 | function __construct($formFields, $attrs = array()) |
| 21 | { |
| 22 | /*if (!isset($attrs['id'])) { |
| 23 | $this->error = '<div class="alert alert-danger">Form ID is require, field id is missing in $attrs<br/><pre style="border-radius: 0;margin-top: 10px;margin-bottom: 5px">' . print_r($attrs, 1) . '</pre></div>'; |
| 24 | return; |
| 25 | }*/ |
| 26 | if (isset($attrs['groups'])) |
| 27 | $this->fieldGroups = $attrs['groups']; |
| 28 | foreach ($attrs as $name => $value) { |
| 29 | $this->$name = $value; |
| 30 | } |
| 31 | $this->formFields = apply_filters("{$this->id}_fields", $formFields, $attrs); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function div($class = '', $id = '') |
| 36 | { |
| 37 | return "<div class='{$class}' id='row_{$id}'>"; |
| 38 | } |
| 39 | |
| 40 | function divClose() |
| 41 | { |
| 42 | return "</div>"; |
| 43 | } |
| 44 | |
| 45 | function label($label, $for = '') |
| 46 | { |
| 47 | return "<label form='{$for}'>{$label}</label>"; |
| 48 | } |
| 49 | |
| 50 | function row($id, $fields) |
| 51 | { |
| 52 | $row = $this->div("row", $id); |
| 53 | if (isset($fields['label'])) |
| 54 | $this->label($fields['label'], $id); |
| 55 | |
| 56 | foreach ($fields['cols'] as $id => $field) { |
| 57 | $row .= $this->formGroup($id, $field); |
| 58 | } |
| 59 | |
| 60 | $row .= $this->divClose(); |
| 61 | return $row; |
| 62 | } |
| 63 | |
| 64 | function fieldGroup($id, $name, $fields_html) |
| 65 | { |
| 66 | $group = $this->div('card', $id); |
| 67 | $group .= $this->div('card-header', $id) . $name . $this->divClose(); |
| 68 | $group .= $this->div('card-body') . $fields_html . $this->divClose(); |
| 69 | $group .= $this->divClose(); |
| 70 | return $group; |
| 71 | } |
| 72 | |
| 73 | function formGroup($id, $field) |
| 74 | { |
| 75 | $grid_class = isset($field['grid_class']) ? $field['grid_class'] : ''; |
| 76 | $field_html = $this->div("form-group {$grid_class}", $id); |
| 77 | $type = $field['type']; |
| 78 | $field_html .= $this->div("input-wrapper {$type}-input-wrapper", $id . "_wrapper"); |
| 79 | if (isset($field['label'])) |
| 80 | $field_html .= $this->label($field['label'], $id); |
| 81 | |
| 82 | $input = $type === 'custom' ? Field::custom($field['custom_control'], $field['attrs']) : Field::$type($field['attrs'], __::valueof($field, 'value')); |
| 83 | if (in_array($type, ['reCaptcha', 'hidden'])) return $input; |
| 84 | |
| 85 | $prepend = isset($field['prepend']) ? $field['prepend'] : null; |
| 86 | $append = isset($field['append']) ? $field['append'] : null; |
| 87 | $input = $this->inputGroup($input, $prepend, $append); |
| 88 | $field_html .= $this->note(__::valueof($field, 'note_before')); |
| 89 | $field_html .= $input; |
| 90 | $field_html .= $this->note(__::valueof($field, 'note')); |
| 91 | $field_html .= $this->note(__::valueof($field, 'note_after')); |
| 92 | $field_html .= $this->divClose(); |
| 93 | $field_html .= $this->divClose(); |
| 94 | return $field_html; |
| 95 | } |
| 96 | |
| 97 | function note($note) |
| 98 | { |
| 99 | if ($note) return "<em class='note'>{$note}</em>"; |
| 100 | } |
| 101 | |
| 102 | function inputGroup($input, $prepend = null, $append = null) |
| 103 | { |
| 104 | if (!$prepend && !$prepend) return $input; |
| 105 | $input_group = "<div class='input-group'>"; |
| 106 | $input_group .= $prepend ? "<div class='input-group-prepend'><span class='input-group-text'>{$prepend}</span></div>" : ""; |
| 107 | $input_group .= $input; |
| 108 | $input_group .= $append ? "<div class='input-group-append'><span class='input-group-text'>{$append}</span></div>" : ""; |
| 109 | $input_group .= "</div>"; |
| 110 | return $input_group; |
| 111 | } |
| 112 | |
| 113 | function heading($attrs) |
| 114 | { |
| 115 | $_attrs = ""; |
| 116 | $text = $attrs['text']; |
| 117 | unset($attrs['text']); |
| 118 | foreach ($attrs as $key => $value) { |
| 119 | $_attrs .= "{$key}='{$value}' "; |
| 120 | } |
| 121 | return "<div class=''>{$text}</div>"; |
| 122 | } |
| 123 | |
| 124 | function reCaptcha($attrs) |
| 125 | { |
| 126 | ob_start(); |
| 127 | ?> |
| 128 | <div class="form-group row"> |
| 129 | <div class="col-sm-12"> |
| 130 | <input type="hidden" id="<?php echo $attrs['id'] ?>" name="<?php echo $attrs['name'] ?>" value=""/> |
| 131 | <script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" |
| 132 | async defer></script> |
| 133 | <div id="<?php echo $attrs['id'] ?>_field"></div> |
| 134 | <style> |
| 135 | #wpdmlogin #<?php echo $attrs['id'] ?>_field iframe { |
| 136 | transform: scale(1.16); |
| 137 | margin-left: 24px; |
| 138 | margin-top: 5px; |
| 139 | margin-bottom: 5px; |
| 140 | } |
| 141 | |
| 142 | #wpdmlogin #<?php echo $attrs['id'] ?>_field { |
| 143 | padding-bottom: 10px !important; |
| 144 | } |
| 145 | </style> |
| 146 | <script type="text/javascript"> |
| 147 | var verifyCallback = function (response) { |
| 148 | jQuery('#<?php echo $attrs['id'] ?>').val(response); |
| 149 | }; |
| 150 | var widgetId2; |
| 151 | var onloadCallback = function () { |
| 152 | grecaptcha.render('<?php echo $attrs['id'] ?>_field', { |
| 153 | 'sitekey': '<?php echo get_option('_wpdm_recaptcha_site_key'); ?>', |
| 154 | 'callback': verifyCallback, |
| 155 | 'theme': 'light' |
| 156 | }); |
| 157 | }; |
| 158 | </script> |
| 159 | </div> |
| 160 | |
| 161 | </div> |
| 162 | <?php |
| 163 | $captcha = ob_get_clean(); |
| 164 | return $captcha; |
| 165 | } |
| 166 | |
| 167 | function render() |
| 168 | { |
| 169 | if ($this->error) return $this->error; |
| 170 | $form_html = $this->noForm ? "" : "<form method='{$this->method}' action='{$this->action}' name='{$this->name}' id='{$this->id}' class='{$this->class}'>"; |
| 171 | $before_form_fields = ""; |
| 172 | $form_html .= apply_filters("wpdm_form_{$this->id}_before_fields", $before_form_fields, $this); |
| 173 | |
| 174 | //Initial field groups |
| 175 | $_field_group_html = []; |
| 176 | foreach ($this->fieldGroups as $group_id => $group_name){ |
| 177 | $_field_group_html[$group_id] = ''; |
| 178 | } |
| 179 | |
| 180 | //Generate form fields |
| 181 | foreach ($this->formFields as $id => $field) { |
| 182 | $field_html = ''; |
| 183 | if (isset($field['cols'])) |
| 184 | $field_html = $this->row($id, $field); |
| 185 | else |
| 186 | $field_html = $this->formGroup($id, $field); |
| 187 | if(isset($field['group']) && isset($this->fieldGroups[$field['group']])) |
| 188 | $_field_group_html[$field['group']] .= $field_html; |
| 189 | else |
| 190 | $form_html .= $field_html; |
| 191 | } |
| 192 | |
| 193 | foreach ($this->fieldGroups as $group_id => $group_name){ |
| 194 | $form_html .= $this->fieldGroup($group_id, $group_name, $_field_group_html[$group_id]); |
| 195 | } |
| 196 | |
| 197 | if ($this->submit_button) { |
| 198 | $form_html .= "<button class='{$this->submit_button['class']}'>{$this->submit_button['label']}</button>"; |
| 199 | } |
| 200 | $after_form_fields = ""; |
| 201 | $form_html .= apply_filters("wpdm_form_{$this->id}_before_fields", $after_form_fields, $this); |
| 202 | $form_html .= $this->noForm ? "" : "</form>"; |
| 203 | return $form_html; |
| 204 | } |
| 205 | } |
| 206 |