PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.14
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.14, at app/Modules/Form/FormDataParser.php

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