PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.19
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.19
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Modules / Form / FormDataParser.php

FormDataParser.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.19, at app/Modules/Form/FormDataParser.php

329 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Form;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6 use WpFluent\Exception;
7
8 class FormDataParser
9 {
10 protected static $data = null;
11
12 public static function parseFormEntries($entries, $form, $fields = null)
13 {
14 $fields = $fields ? $fields : FormFieldsParser::getEntryInputs($form);
15
16 foreach ($entries as $entry) {
17 static::parseFormEntry($entry, $form, $fields);
18 }
19
20 return $entries;
21 }
22
23 public static function parseFormEntry($entry, $form, $fields = null, $isHtml = false)
24 {
25 $fields = $fields ? $fields : FormFieldsParser::getEntryInputs($form);
26
27 $entry->user_inputs = static::parseData(
28 json_decode($entry->response),
29 $fields,
30 $form->id,
31 $isHtml
32 );
33
34 return $entry;
35 }
36
37 public static function parseFormSubmission($submission, $form, $fields, $isHtml = false)
38 {
39 if (is_null(static::$data)) {
40 static::$data = static::parseData(
41 json_decode($submission->response),
42 $fields,
43 $form->id,
44 $isHtml
45 );
46 }
47
48 $submission->user_inputs = static::$data;
49
50 return $submission;
51 }
52
53 public static function parseData($response, $fields, $formId, $isHtml = false)
54 {
55 $trans = [];
56
57 foreach ($fields as $field_key => $field) {
58 if (isset($response->{$field_key})) {
59 $value = apply_filters(
60 'fluentform_response_render_' . $field['element'],
61 $response->{$field_key},
62 $field,
63 $formId,
64 $isHtml
65 );
66 $trans[$field_key] = $value;
67 } else {
68 $trans[$field_key] = '';
69 }
70 }
71
72 return $trans;
73 }
74
75 public static function formatValue($value)
76 {
77 if (is_array($value) || is_object($value)) {
78 return fluentImplodeRecursive(', ', array_filter(array_values((array) $value)));
79 }
80
81 return $value;
82 }
83
84 public static function formatFileValues($values, $isHtml, $form_id = null)
85 {
86 if (!$values) {
87 return $values;
88 }
89
90 if (is_string($values)) {
91 return $values;
92 }
93
94 if (!$isHtml) {
95 return fluentImplodeRecursive(', ', array_filter(array_values((array) $values)));
96 }
97 if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) {
98 return '';
99 }
100
101 $html = '<ul class="ff_entry_list">';
102 foreach ($values as $value) {
103 if (!$value) {
104 continue;
105 }
106 $html .= '<li><a href="' . $value . '" target="_blank">' . basename($value) . '</a></li>';
107 }
108
109 $html .= '</ul>';
110 return $html;
111 }
112
113 public static function formatImageValues($values, $isHtml, $form_id = null)
114 {
115 if (!$values) {
116 return $values;
117 }
118
119 if (is_string($values)) {
120 return $values;
121 }
122
123 if (!$isHtml) {
124 return fluentImplodeRecursive(', ', array_filter(array_values((array) $values)));
125 }
126 if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) {
127 return '';
128 }
129 if (1 == count($values)) {
130 $value = $values[0];
131 if (!$value) {
132 return '';
133 }
134 return '<a href="' . $value . '" target="_blank"><img style="max-width:180px" src="' . $value . '" /></a>';
135 }
136
137 $html = '<ul class="ff_entry_list ff_entry_images">';
138 foreach ($values as $value) {
139 if (!$value) {
140 continue;
141 }
142 $html .= '<li style="margin: 20px 20px 20px 0px; display: inline-block; margin-right: 20px;"><a href="' . $value . '" target="_blank"><img style="max-width:180px" src="' . $value . '" /></a></li>';
143 }
144
145 $html .= '</ul>';
146 return $html;
147 }
148
149 public static function formatRepeatFieldValue($value, $field, $form_id)
150 {
151
152 if (defined('FLUENTFORM_RENDERING_ENTRIES')) {
153 return __('....', 'fluentform');
154 }
155
156 if (is_string($value)) {
157 return $value;
158 }
159
160 try {
161 $repeatColumns = ArrayHelper::get($field, 'raw.fields');
162 $rows = count($value[0]);
163 $columns = count($value);
164
165 ob_start();
166 if ($repeatColumns) {
167 ?>
168 <div class="ff_entry_table_wrapper">
169 <table class="ff_entry_table_field ff-table">
170 <thead>
171 <tr>
172 <?php foreach ($repeatColumns as $repeatColumn) : ?>
173 <th><?php echo fluentform_sanitize_html(ArrayHelper::get($repeatColumn, 'settings.label')); ?>
174 </th>
175 <?php endforeach; ?>
176 </tr>
177 </thead>
178
179 <tbody>
180 <?php for ($i = 0; $i < $rows; $i++) : ?>
181 <tr>
182 <?php for ($j = 0; $j < $columns; $j++) : ?>
183 <td>
184 <?php echo fluentform_sanitize_html($value[$j][$i]); ?>
185 </td>
186 <?php endfor; ?>
187 </tr>
188 <?php endfor; ?>
189 </tbody>
190 </table>
191 </div>
192 <?php
193 }
194 return ob_get_clean();
195 } catch (Exception $e) {
196 }
197
198 return $value;
199 }
200
201 public static function formatTabularGridFieldValue($value, $field, $form_id, $isHtml = false)
202 {
203 if (defined('FLUENTFORM_RENDERING_ENTRIES')) {
204 return __('....', 'fluentform');
205 }
206
207 if (is_string($value)) {
208 return $value;
209 }
210
211 if (is_array($value)) {
212 $value = (object) $value;
213 }
214 try {
215 if (empty($field['raw'])) {
216 return $value;
217 }
218 $columnLabels = $field['raw']['settings']['grid_columns'];
219 $fieldType = $field['raw']['settings']['tabular_field_type'];
220 $columnHeaders = implode('</th><th style="text-align: center;">', array_values($columnLabels));
221
222 $elMarkup = "<table class='ff-table'><thead><tr><th></th><th style='text-align: center;'>{$columnHeaders}</th></tr></thead><tbody>";
223
224 foreach (static::makeTabularData($field['raw']) as $row) {
225 $elMarkup .= '<tr>';
226 $elMarkup .= "<td>{$row['label']}</td>";
227 foreach ($row['columns'] as $column) {
228 $isChecked = '';
229 if ('radio' == $fieldType) {
230 if (isset($value->{$row['name']})) {
231 $isChecked = $value->{$row['name']} == $column['name'] ? 'checked' : '';
232 }
233 } else {
234 if (isset($value->{$row['name']})) {
235 $isChecked = in_array($column['name'], $value->{$row['name']}) ? 'checked' : '';
236 }
237 }
238 $icon = "<input disabled type='{$fieldType}' {$isChecked}>";
239 if ($isChecked) {
240 $icon = '';
241 }
242 $elMarkup .= "<td style='text-align: center;'>" . $icon . '</td>';
243 }
244 $elMarkup .= '</tr>';
245 }
246
247 $elMarkup .= '</tbody></table>';
248
249 return $elMarkup;
250 } catch (Exception $e) {
251 }
252 return '';
253 }
254
255 public static function makeTabularData($data)
256 {
257 $table = [];
258 $rows = $data['settings']['grid_rows'];
259 $columns = $data['settings']['grid_columns'];
260
261 foreach ($rows as $rowKey => $rowValue) {
262 $table[$rowKey] = [
263 'name' => $rowKey,
264 'label' => $rowValue,
265 'columns' => [],
266 ];
267
268 foreach ($columns as $columnKey => $columnValue) {
269 $table[$rowKey]['columns'][] = [
270 'name' => $columnKey,
271 'label' => $columnValue,
272 ];
273 }
274 }
275
276 return $table;
277 }
278
279 /**
280 * Format input_name field value by concatenating all name fields.
281 *
282 * @param array|object $value
283 *
284 * @return string $value
285 */
286 public static function formatName($value)
287 {
288 if (is_array($value) || is_object($value)) {
289 return fluentImplodeRecursive(' ', array_filter(array_values((array) $value)));
290 }
291
292 return $value;
293 }
294
295 public static function formatCheckBoxValues($values, $field, $isHtml = false)
296 {
297 if (!$isHtml) {
298 return self::formatValue($values);
299 }
300
301 if (!is_array($values) || empty($values)) {
302 return $values;
303 }
304
305 if (!isset($field['options'])) {
306 $field['options'] = [];
307 foreach (ArrayHelper::get($field, 'raw.settings.advanced_options', []) as $option) {
308 $field['options'][$option['value']] = $option['label'];
309 }
310 }
311
312 $html = '<ul style="white-space: normal;">';
313 foreach ($values as $value) {
314 $item = $value;
315 if ($itemLabel = ArrayHelper::get($field, 'options.' . $item)) {
316 $item = $itemLabel;
317 }
318 $html .= '<li>' . $item . '</li>';
319 }
320
321 return $html . '</ul>';
322 }
323
324 public static function resetData()
325 {
326 static::$data = null;
327 }
328 }
329