opnform/app/Service/Forms/FormSubmissionFormatter.php

280 lines
9.3 KiB
PHP
Raw Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Service\Forms;
use App\Models\Forms\Form;
use Carbon\Carbon;
class FormSubmissionFormatter
{
/**
* If true, creates html <a> links for emails and urls
2024-02-23 10:54:12 +00:00
*
2022-09-20 19:59:52 +00:00
* @var bool
*/
private $createLinks = false;
/**
* If true, serialize arrays
2024-02-23 10:54:12 +00:00
*
2022-09-20 19:59:52 +00:00
* @var bool
*/
private $outputStringsOnly = false;
private $showHiddenFields = false;
private $setEmptyForNoValue = false;
private $showRemovedFields = false;
private $useSignedUrlForFiles = false;
/**
* Logic resolver needs an array id => value, so we create it here
*/
private $idFormData = null;
2022-09-20 19:59:52 +00:00
public function __construct(private Form $form, private array $formData)
{
2024-02-23 10:54:12 +00:00
$this->initIdFormData();
2022-09-20 19:59:52 +00:00
}
public function createLinks()
{
$this->createLinks = true;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this;
}
public function showHiddenFields()
{
$this->showHiddenFields = true;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this;
}
public function outputStringsOnly()
{
$this->outputStringsOnly = true;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this;
}
public function setEmptyForNoValue()
{
$this->setEmptyForNoValue = true;
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $this;
}
public function showRemovedFields()
{
$this->showRemovedFields = true;
2024-02-23 10:54:12 +00:00
return $this;
}
public function useSignedUrlForFiles()
{
$this->useSignedUrlForFiles = true;
2024-02-23 10:54:12 +00:00
return $this;
}
2022-09-20 19:59:52 +00:00
/**
* Return a nice "FieldName": "Field Response" array
* - If createLink enabled, returns html link for emails and links
* Used for CSV exports
*/
public function getCleanKeyValue()
{
$data = $this->formData;
$fields = collect($this->form->properties);
$removeFields = collect($this->form->removed_properties)->map(function ($field) {
return [
...$field,
2024-02-23 10:54:12 +00:00
'removed' => true,
];
});
if ($this->showRemovedFields) {
$fields = $fields->merge($removeFields);
}
$fields = $fields->filter(function ($field) {
2024-02-23 10:54:12 +00:00
return ! in_array($field['type'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']);
})->values();
2022-09-20 19:59:52 +00:00
$returnArray = [];
foreach ($fields as $field) {
2024-02-23 10:54:12 +00:00
if (in_array($field['id'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image'])) {
continue;
}
2024-02-23 10:54:12 +00:00
if ($field['removed'] ?? false) {
$field['name'] = $field['name'].' (deleted)';
}
// Add ID to avoid name clashes
$field['name'] = $field['name'].' ('.\Str::of($field['id']).')';
2022-09-20 19:59:52 +00:00
// If not present skip
2024-02-23 10:54:12 +00:00
if (! isset($data[$field['id']])) {
2022-09-20 19:59:52 +00:00
if ($this->setEmptyForNoValue) {
$returnArray[$field['name']] = '';
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
continue;
}
// If hide hidden fields
2024-02-23 10:54:12 +00:00
if (! $this->showHiddenFields) {
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData ?? [])) {
2022-09-20 19:59:52 +00:00
continue;
}
}
if ($this->createLinks && $field['type'] == 'url') {
$returnArray[$field['name']] = '<a href="'.$data[$field['id']].'">'.$data[$field['id']].'</a>';
} elseif ($this->createLinks && $field['type'] == 'email') {
$returnArray[$field['name']] = '<a href="mailto:'.$data[$field['id']].'">'.$data[$field['id']].'</a>';
} elseif ($field['type'] == 'multi_select') {
$val = $data[$field['id']];
if ($this->outputStringsOnly) {
$returnArray[$field['name']] = implode(', ', $val);
} else {
$returnArray[$field['name']] = $val;
}
} elseif ($field['type'] == 'files') {
if ($this->outputStringsOnly) {
$formId = $this->form->id;
2024-02-23 10:54:12 +00:00
$returnArray[$field['name']] = implode(
', ',
2022-09-20 19:59:52 +00:00
collect($data[$field['id']])->map(function ($file) use ($formId) {
return $this->getFileUrl($formId, $file);
2022-09-20 19:59:52 +00:00
})->toArray()
);
} else {
$formId = $this->form->id;
$returnArray[$field['name']] = collect($data[$field['id']])->map(function ($file) use ($formId) {
2024-02-23 10:54:12 +00:00
return [
'file_url' => $this->getFileUrl($formId, $file),
'file_name' => $file,
];
});
2022-09-20 19:59:52 +00:00
}
} else {
if (is_array($data[$field['id']])) {
$data[$field['id']] = implode(', ', $data[$field['id']]);
}
$returnArray[$field['name']] = $data[$field['id']];
}
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $returnArray;
}
/**
* Return a list of fields, with a filled value attribute.
* Used for humans.
*/
public function getFieldsWithValue()
{
$data = $this->formData;
$fields = $this->form->properties;
$transformedFields = [];
foreach ($fields as $field) {
2024-02-23 10:54:12 +00:00
if (! isset($field['id']) || ! isset($data[$field['id']])) {
2022-09-20 19:59:52 +00:00
continue;
}
// If hide hidden fields
2024-02-23 10:54:12 +00:00
if (! $this->showHiddenFields) {
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData)) {
2022-09-20 19:59:52 +00:00
continue;
}
}
if ($this->createLinks && $field['type'] == 'url') {
$field['value'] = '<a href="'.$data[$field['id']].'">'.$data[$field['id']].'</a>';
} elseif ($this->createLinks && $field['type'] == 'email') {
$field['value'] = '<a href="mailto:'.$data[$field['id']].'">'.$data[$field['id']].'</a>';
} elseif ($field['type'] == 'checkbox') {
$field['value'] = $data[$field['id']] ? 'Yes' : 'No';
} elseif ($field['type'] == 'date') {
if (is_array($data[$field['id']])) {
$field['value'] = isset($data[$field['id']][1]) ? (new Carbon($data[$field['id']][0]))->format('d/m/Y')
.' - '.(new Carbon($data[$field['id']][1]))->format('d/m/Y') : (new Carbon($data[$field['id']][0]))->format('d/m/Y');
} else {
$field['value'] = (new Carbon($data[$field['id']]))->format((isset($field['with_time']) && $field['with_time']) ? 'd/m/Y H:i' : 'd/m/Y');
}
} elseif ($field['type'] == 'multi_select') {
$val = $data[$field['id']];
if ($this->outputStringsOnly) {
$field['value'] = implode(', ', $val);
} else {
$field['value'] = $val;
}
} elseif ($field['type'] == 'files') {
if ($this->outputStringsOnly) {
$formId = $this->form->id;
2024-02-23 10:54:12 +00:00
$field['value'] = implode(
', ',
2022-09-20 19:59:52 +00:00
collect($data[$field['id']])->map(function ($file) use ($formId) {
2024-02-23 10:54:12 +00:00
return $this->getFileUrl($formId, $file);
2022-09-20 19:59:52 +00:00
})->toArray()
);
$field['email_data'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
$splitText = explode('.', $file);
2024-02-23 10:54:12 +00:00
return [
2024-02-23 10:54:12 +00:00
'unsigned_url' => route('open.forms.submissions.file', [$formId, $file]),
'signed_url' => $this->getFileUrl($formId, $file),
'label' => \Str::limit($file, 20, '[...].'.end($splitText)),
];
})->toArray();
2022-09-20 19:59:52 +00:00
} else {
$formId = $this->form->id;
$field['value'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
return [
'file_url' => $this->getFileUrl($formId, $file),
2022-09-20 19:59:52 +00:00
'file_name' => $file,
];
});
}
} else {
if (is_array($data[$field['id']]) && $this->outputStringsOnly) {
$field['value'] = implode(', ', $data[$field['id']]);
} else {
$field['value'] = $data[$field['id']];
}
}
$transformedFields[] = $field;
}
2024-02-23 10:54:12 +00:00
2022-09-20 19:59:52 +00:00
return $transformedFields;
}
2024-02-23 10:54:12 +00:00
private function initIdFormData()
{
$formProperties = collect($this->form->properties);
foreach ($this->formData as $key => $value) {
$property = $formProperties->first(function ($item) use ($key) {
return $item['id'] == $key;
});
if ($property) {
$this->idFormData[$property['id']] = $value;
}
}
}
private function getFileUrl($formId, $file)
{
return $this->useSignedUrlForFiles ? \URL::signedRoute(
'open.forms.submissions.file',
2024-02-23 10:54:12 +00:00
[$formId, $file]
) : route('open.forms.submissions.file', [$formId, $file]);
}
2022-09-20 19:59:52 +00:00
}