opnform/app/Http/Resources/FormSubmissionResource.php

66 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2022-09-20 19:59:52 +00:00
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class FormSubmissionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
$this->generateFileLinks();
$this->addExtraData();
2022-09-20 19:59:52 +00:00
return [
'data' => $this->data,
'form_id' => $this->form_id,
'id' => $this->id,
];
}
private function addExtraData()
2022-09-20 19:59:52 +00:00
{
$this->data = array_merge($this->data, [
2024-02-23 10:54:12 +00:00
'created_at' => $this->created_at->toDateTimeString(),
'id' => $this->id,
2022-09-20 19:59:52 +00:00
]);
}
/**
* Link to the file (generating signed s3 URL)
2024-02-23 10:54:12 +00:00
*
2022-09-20 19:59:52 +00:00
* @return void
*/
private function generateFileLinks()
{
$data = $this->data;
$formFields = collect($this->form->properties)->concat(collect($this->form->removed_properties));
$fileFields = $formFields->filter(function ($field) {
return in_array($field['type'], ['files', 'signature']);
2022-09-20 19:59:52 +00:00
});
foreach ($fileFields as $field) {
2024-02-23 10:54:12 +00:00
if (isset($data[$field['id']]) && ! empty($data[$field['id']])) {
$data[$field['id']] = collect($data[$field['id']])->filter(function ($file) {
return $file !== null && $file;
})->map(function ($file) {
2022-09-20 19:59:52 +00:00
return [
2024-01-13 18:57:39 +00:00
'file_url' => \URL::signedRoute(
'open.forms.submissions.file',
[$this->form_id, $file],
now()->addMinutes(10)
),
2022-09-20 19:59:52 +00:00
'file_name' => $file,
];
});
}
}
$this->data = $data;
}
}