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