| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Form; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
|
| 7 |
class FormDataParser |
| 8 |
{ |
| 9 |
protected static $data = null; |
| 10 |
protected static $submissionId = 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 |
// Sometimes submission will change inside loop. So we need to parse submission data for new one |
| 40 |
$newSubmission = $submission->id != static::$submissionId; |
| 41 |
|
| 42 |
if (is_null(static::$data) || $newSubmission) { |
| 43 |
static::$data = static::parseData( |
| 44 |
json_decode($submission->response), |
| 45 |
$fields, |
| 46 |
$form->id, |
| 47 |
$isHtml |
| 48 |
); |
| 49 |
static::$submissionId = $submission->id; |
| 50 |
} |
| 51 |
|
| 52 |
$submission->user_inputs = static::$data; |
| 53 |
|
| 54 |
return $submission; |
| 55 |
} |
| 56 |
|
| 57 |
public static function parseData($response, $fields, $formId, $isHtml = false) |
| 58 |
{ |
| 59 |
$trans = []; |
| 60 |
foreach ($fields as $field_key => $field) { |
| 61 |
if (isset($response->{$field_key})) { |
| 62 |
$value = $response->{$field_key}; |
| 63 |
|
| 64 |
$value = apply_filters_deprecated( |
| 65 |
'fluentform_response_render_' . $field['element'], |
| 66 |
[ |
| 67 |
$value, |
| 68 |
$field, |
| 69 |
$formId, |
| 70 |
$isHtml |
| 71 |
], |
| 72 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 73 |
'fluentform/response_render_' . $field['element'], |
| 74 |
'Use fluentform/response_render_' . $field['element'] . ' instead of fluentform_response_render_' . $field['element'] |
| 75 |
); |
| 76 |
|
| 77 |
$value = apply_filters( |
| 78 |
'fluentform/response_render_' . $field['element'], |
| 79 |
$value, |
| 80 |
$field, |
| 81 |
$formId, |
| 82 |
$isHtml |
| 83 |
); |
| 84 |
$trans[$field_key] = $value; |
| 85 |
} else { |
| 86 |
$trans[$field_key] = ''; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $trans; |
| 91 |
} |
| 92 |
|
| 93 |
public static function formatValue($value) |
| 94 |
{ |
| 95 |
if (is_array($value) || is_object($value)) { |
| 96 |
return fluentImplodeRecursive(', ', array_filter(array_values((array) $value))); |
| 97 |
} |
| 98 |
|
| 99 |
return $value; |
| 100 |
} |
| 101 |
|
| 102 |
public static function formatFileValues($values, $isHtml, $form_id = null) |
| 103 |
{ |
| 104 |
if (!$values) { |
| 105 |
return $values; |
| 106 |
} |
| 107 |
|
| 108 |
if (is_string($values)) { |
| 109 |
return $values; |
| 110 |
} |
| 111 |
|
| 112 |
if (!$isHtml) { |
| 113 |
return fluentImplodeRecursive(', ', array_filter(array_values((array) $values))); |
| 114 |
} |
| 115 |
if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) { |
| 116 |
return ''; |
| 117 |
} |
| 118 |
|
| 119 |
$html = '<ul class="ff_entry_list">'; |
| 120 |
foreach ($values as $value) { |
| 121 |
if (!$value) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
$html .= '<li><a href="' . $value . '" target="_blank">' . basename($value) . '</a></li>'; |
| 125 |
} |
| 126 |
|
| 127 |
$html .= '</ul>'; |
| 128 |
return $html; |
| 129 |
} |
| 130 |
|
| 131 |
public static function formatImageValues($values, $isHtml, $form_id = null) |
| 132 |
{ |
| 133 |
if (!$values) { |
| 134 |
return $values; |
| 135 |
} |
| 136 |
|
| 137 |
if (is_string($values)) { |
| 138 |
return $values; |
| 139 |
} |
| 140 |
|
| 141 |
$isHtml = apply_filters('fluentform/render_field_as_html', $isHtml, $values, $form_id); |
| 142 |
|
| 143 |
|
| 144 |
if (!$isHtml) { |
| 145 |
return fluentImplodeRecursive(', ', array_filter(array_values((array) $values))); |
| 146 |
} |
| 147 |
if ($form_id && \FluentForm\App\Helpers\Helper::isEntryAutoDeleteEnabled($form_id)) { |
| 148 |
return ''; |
| 149 |
} |
| 150 |
if (1 == count($values)) { |
| 151 |
$value = $values[0]; |
| 152 |
if (!$value) { |
| 153 |
return ''; |
| 154 |
} |
| 155 |
return '<a href="' . $value . '" target="_blank"><img style="max-width:180px" src="' . $value . '" /></a>'; |
| 156 |
} |
| 157 |
|
| 158 |
$html = '<ul class="ff_entry_list ff_entry_images">'; |
| 159 |
foreach ($values as $value) { |
| 160 |
if (!$value) { |
| 161 |
continue; |
| 162 |
} |
| 163 |
$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>'; |
| 164 |
} |
| 165 |
|
| 166 |
$html .= '</ul>'; |
| 167 |
return $html; |
| 168 |
} |
| 169 |
|
| 170 |
public static function formatRepeatFieldValue($value, $field, $form_id) |
| 171 |
{ |
| 172 |
|
| 173 |
if (defined('FLUENTFORM_RENDERING_ENTRIES')) { |
| 174 |
return __('....', 'fluentform'); |
| 175 |
} |
| 176 |
|
| 177 |
if (is_string($value)) { |
| 178 |
return $value; |
| 179 |
} |
| 180 |
|
| 181 |
try { |
| 182 |
$repeatColumns = ArrayHelper::get($field, 'raw.fields'); |
| 183 |
$rows = count($value[0]); |
| 184 |
$columns = count($value); |
| 185 |
|
| 186 |
ob_start(); |
| 187 |
if ($repeatColumns) { |
| 188 |
?> |
| 189 |
<div class="ff_entry_table_wrapper"> |
| 190 |
<table class="ff_entry_table_field ff-table"> |
| 191 |
<thead> |
| 192 |
<tr> |
| 193 |
<?php foreach ($repeatColumns as $repeatColumn) : ?> |
| 194 |
<th><?php echo fluentform_sanitize_html(ArrayHelper::get($repeatColumn, 'settings.label')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fluentform_sanitize_html() removes XSS vectors and uses wp_kses() with allowed tags ?> |
| 195 |
</th> |
| 196 |
<?php endforeach; ?> |
| 197 |
</tr> |
| 198 |
</thead> |
| 199 |
|
| 200 |
<tbody> |
| 201 |
<?php for ($i = 0; $i < $rows; $i++) : ?> |
| 202 |
<tr> |
| 203 |
<?php for ($j = 0; $j < $columns; $j++) : ?> |
| 204 |
<td> |
| 205 |
<?php echo fluentform_sanitize_html($value[$j][$i]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- fluentform_sanitize_html() removes XSS vectors and uses wp_kses() with allowed tags ?> |
| 206 |
</td> |
| 207 |
<?php endfor; ?> |
| 208 |
</tr> |
| 209 |
<?php endfor; ?> |
| 210 |
</tbody> |
| 211 |
</table> |
| 212 |
</div> |
| 213 |
<?php |
| 214 |
} |
| 215 |
return ob_get_clean(); |
| 216 |
} catch (\Exception $e) { |
| 217 |
} |
| 218 |
|
| 219 |
return $value; |
| 220 |
} |
| 221 |
|
| 222 |
public static function formatTabularGridFieldValue($value, $field, $form_id, $isHtml = false) |
| 223 |
{ |
| 224 |
if (defined('FLUENTFORM_RENDERING_ENTRIES')) { |
| 225 |
return __('....', 'fluentform'); |
| 226 |
} |
| 227 |
|
| 228 |
if (is_string($value)) { |
| 229 |
return $value; |
| 230 |
} |
| 231 |
|
| 232 |
if (is_array($value)) { |
| 233 |
$value = (object) $value; |
| 234 |
} |
| 235 |
try { |
| 236 |
if (empty($field['raw'])) { |
| 237 |
return $value; |
| 238 |
} |
| 239 |
$columnLabels = $field['raw']['settings']['grid_columns']; |
| 240 |
$fieldType = $field['raw']['settings']['tabular_field_type']; |
| 241 |
$columnHeaders = implode('</th><th style="text-align: center;">', array_values($columnLabels)); |
| 242 |
|
| 243 |
$elMarkup = "<table class='ff-table'><thead><tr><th></th><th style='text-align: center;'>{$columnHeaders}</th></tr></thead><tbody>"; |
| 244 |
|
| 245 |
foreach (static::makeTabularData($field['raw']) as $row) { |
| 246 |
$elMarkup .= '<tr>'; |
| 247 |
$elMarkup .= "<td>{$row['label']}</td>"; |
| 248 |
foreach ($row['columns'] as $column) { |
| 249 |
$isChecked = ''; |
| 250 |
if ('radio' == $fieldType) { |
| 251 |
if (isset($value->{$row['name']})) { |
| 252 |
$isChecked = $value->{$row['name']} == $column['name'] ? 'checked' : ''; |
| 253 |
} |
| 254 |
} else { |
| 255 |
if (isset($value->{$row['name']})) { |
| 256 |
$isChecked = in_array($column['name'], $value->{$row['name']}) ? 'checked' : ''; |
| 257 |
} |
| 258 |
} |
| 259 |
$icon = "<input disabled type='{$fieldType}' {$isChecked}>"; |
| 260 |
if ($isChecked) { |
| 261 |
$icon = '✔'; |
| 262 |
} |
| 263 |
$elMarkup .= "<td style='text-align: center;'>" . $icon . '</td>'; |
| 264 |
} |
| 265 |
$elMarkup .= '</tr>'; |
| 266 |
} |
| 267 |
|
| 268 |
$elMarkup .= '</tbody></table>'; |
| 269 |
|
| 270 |
return $elMarkup; |
| 271 |
} catch (\Exception $e) { |
| 272 |
} |
| 273 |
return ''; |
| 274 |
} |
| 275 |
|
| 276 |
public static function makeTabularData($data) |
| 277 |
{ |
| 278 |
$table = []; |
| 279 |
$rows = $data['settings']['grid_rows']; |
| 280 |
$columns = $data['settings']['grid_columns']; |
| 281 |
|
| 282 |
foreach ($rows as $rowKey => $rowValue) { |
| 283 |
$rowKey = trim(sanitize_text_field($rowKey)); |
| 284 |
$table[$rowKey] = [ |
| 285 |
'name' => $rowKey, |
| 286 |
'label' => $rowValue, |
| 287 |
'columns' => [], |
| 288 |
]; |
| 289 |
|
| 290 |
foreach ($columns as $columnKey => $columnValue) { |
| 291 |
$table[$rowKey]['columns'][] = [ |
| 292 |
'name' => trim(sanitize_text_field($columnKey)), |
| 293 |
'label' => $columnValue, |
| 294 |
]; |
| 295 |
} |
| 296 |
} |
| 297 |
return $table; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Format input_name field value by concatenating all name fields. |
| 302 |
* |
| 303 |
* @param array|object $value |
| 304 |
* |
| 305 |
* @return string $value |
| 306 |
*/ |
| 307 |
public static function formatName($value) |
| 308 |
{ |
| 309 |
if (is_array($value) || is_object($value)) { |
| 310 |
$value = (array) $value; |
| 311 |
$order = ['first_name', 'middle_name', 'last_name']; |
| 312 |
uksort($value, function($a, $b) use ($order) { |
| 313 |
$posA = array_search($a, $order); |
| 314 |
$posB = array_search($b, $order); |
| 315 |
return $posA - $posB; |
| 316 |
}); |
| 317 |
return fluentImplodeRecursive(' ', array_filter(array_values($value))); |
| 318 |
} |
| 319 |
|
| 320 |
return $value; |
| 321 |
} |
| 322 |
|
| 323 |
public static function formatCheckBoxValues($values, $field, $isHtml = false) |
| 324 |
{ |
| 325 |
if (!$isHtml) { |
| 326 |
if ( |
| 327 |
defined('FLUENTFORM_RENDERING_ENTRIES') && |
| 328 |
$values && is_array($values) && |
| 329 |
$options = ArrayHelper::get($field, 'raw.settings.advanced_options', []) |
| 330 |
) { |
| 331 |
$options = \FluentForm\App\Helpers\Helper::advancedOptionsValueLabelMap($options); |
| 332 |
foreach ($values as &$value) { |
| 333 |
if ($label = ArrayHelper::get($options, $value)) { |
| 334 |
$value = $label; |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return self::formatValue($values); |
| 339 |
} |
| 340 |
|
| 341 |
if (!is_array($values)) { |
| 342 |
return $values; |
| 343 |
} |
| 344 |
|
| 345 |
if (empty($values)) { |
| 346 |
return ''; |
| 347 |
} |
| 348 |
|
| 349 |
if (!isset($field['options'])) { |
| 350 |
$field['options'] = \FluentForm\App\Helpers\Helper::advancedOptionsValueLabelMap( |
| 351 |
ArrayHelper::get($field, 'raw.settings.advanced_options', []) |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
$html = '<ul style="white-space: normal;">'; |
| 356 |
foreach ($values as $value) { |
| 357 |
$item = $value; |
| 358 |
if ($itemLabel = ArrayHelper::get($field, 'options.' . $item)) { |
| 359 |
$item = $itemLabel; |
| 360 |
} |
| 361 |
$html .= '<li>' . $item . '</li>'; |
| 362 |
} |
| 363 |
|
| 364 |
return $html . '</ul>'; |
| 365 |
} |
| 366 |
|
| 367 |
public static function resetData() |
| 368 |
{ |
| 369 |
static::$data = null; |
| 370 |
} |
| 371 |
} |
| 372 |
|