Separated laravel app to its own folder (#540)

This commit is contained in:
Julien Nahum
2024-08-26 18:24:56 +02:00
committed by GitHub
parent 39b8df5eed
commit 5bd1dda504
546 changed files with 124 additions and 143 deletions

View File

@@ -0,0 +1,289 @@
<?php
namespace App\Service\Forms;
use App\Http\Requests\UserFormRequest;
use App\Http\Resources\FormResource;
use App\Models\Forms\Form;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Stevebauman\Purify\Facades\Purify;
use function collect;
class FormCleaner
{
/**
* All the performed cleanings
*
* @var bool
*/
private array $cleanings = [];
private array $data;
// For remove keys those have empty value
private array $customKeys = ['seo_meta'];
private array $formDefaults = [
'no_branding' => false,
'database_fields_update' => null,
'editable_submissions' => false,
'custom_code' => null,
'seo_meta' => [],
'redirect_url' => null
];
private array $formNonTrialingDefaults = [
// Custom code protection disabled for now
// 'custom_code' => null,
];
private array $fieldDefaults = [
// 'name' => '' TODO: prevent name changing, use alias for column and keep original name as it is
'file_upload' => false,
];
private array $cleaningMessages = [
// For form
'no_branding' => 'OpenForm branding is not hidden.',
'database_fields_update' => 'Form submission will only create new records (no updates).',
'editable_submissions' => 'Users will not be able to edit their submissions.',
'custom_code' => 'Custom code was disabled',
'seo_meta' => 'Custom SEO was disabled',
'redirect_url' => 'Redirect Url was disabled',
// For fields
'file_upload' => 'Link field is not a file upload.',
'custom_block' => 'The custom block was removed.',
];
/**
* Returns form data after request ingestion
*/
public function getData(): array
{
return $this->data;
}
/**
* Returns true if at least one cleaning was done
*/
public function hasCleaned(): bool
{
return count($this->cleanings) > 0;
}
/**
* Returns the messages for each cleaning step performed
*/
public function getPerformedCleanings(): array
{
$cleaningMsgs = [];
foreach ($this->cleanings as $key => $val) {
$cleaningMsgs[$key] = collect($val)->map(function ($cleaning) {
return $this->cleaningMessages[$cleaning];
});
}
return $cleaningMsgs;
}
/**
* Removes form pro features from data if user isn't pro
*/
public function processRequest(UserFormRequest $request): FormCleaner
{
$data = $request->validated();
$this->data = $this->commonCleaning($data);
return $this;
}
/**
* Create form cleaner instance from existing form
*/
public function processForm(Request $request, Form $form): FormCleaner
{
$data = (new FormResource($form))->toArray($request);
$this->data = $this->commonCleaning($data);
return $this;
}
private function isPro(Workspace $workspace)
{
return $workspace->is_pro;
}
private function isTrialing(Workspace $workspace)
{
return $workspace->is_trialing;
}
/**
* Dry run celanings
*
* @param User|null $user
*/
public function simulateCleaning(Workspace $workspace): FormCleaner
{
if ($this->isTrialing($workspace)) {
$this->data = $this->removeNonTrialingFeatures($this->data, true);
}
if (!$this->isPro($workspace)) {
$this->data = $this->removeProFeatures($this->data, true);
}
return $this;
}
/**
* Perform Cleanigns
*
* @param User|null $user
* @return $this|array
*/
public function performCleaning(Workspace $workspace): FormCleaner
{
if ($this->isTrialing($workspace)) {
$this->data = $this->removeNonTrialingFeatures($this->data, true);
}
if (!$this->isPro($workspace)) {
$this->data = $this->removeProFeatures($this->data);
}
return $this;
}
/**
* Clean all forms:
* - Escape html of custom text block
*/
private function commonCleaning(array $data)
{
foreach ($data['properties'] as &$property) {
if ($property['type'] == 'nf-text' && isset($property['content'])) {
$property['content'] = Purify::clean($property['content']);
}
}
return $data;
}
private function removeNonTrialingFeatures(array $data, $simulation = false)
{
$this->clean($data, $this->formNonTrialingDefaults);
return $data;
}
private function removeProFeatures(array $data, $simulation = false)
{
$this->cleanForm($data, $simulation);
$this->cleanProperties($data, $simulation);
return $data;
}
private function cleanForm(array &$data, $simulation = false): void
{
$this->clean($data, $this->formDefaults, $simulation);
}
private function cleanProperties(array &$data, $simulation = false): void
{
foreach ($data['properties'] as $key => &$property) {
/*
// Remove pro custom blocks
if (\Str::of($property['type'])->startsWith('nf-')) {
$this->cleanings[$property['name']][] = 'custom_block';
if (!$simulation) {
unset($data['properties'][$key]);
}
continue;
}
// Remove logic
if (($property['logic']['conditions'] ?? null) != null || ($property['logic']['actions'] ?? []) != []) {
$this->cleanings[$property['name']][] = 'logic';
if (!$simulation) {
unset($data['properties'][$key]['logic']);
}
}
*/
// Clean pro field options
$this->cleanField($property, $this->fieldDefaults, $simulation);
}
}
private function clean(array &$data, array $defaults, $simulation = false): void
{
foreach ($defaults as $key => $value) {
// Get value from form
$formVal = Arr::get($data, $key);
// Transform customkeys values
$formVal = $this->cleanCustomKeys($key, $formVal);
// Transform boolean values
$formVal = (($formVal === 0 || $formVal === '0') ? false : $formVal);
$formVal = (($formVal === 1 || $formVal === '1') ? true : $formVal);
if (!is_null($formVal) && $formVal !== $value) {
if (!isset($this->cleanings['form'])) {
$this->cleanings['form'] = [];
}
$this->cleanings['form'][] = $key;
// If not a simulation, do the cleaning
if (!$simulation) {
Arr::set($data, $key, $value);
}
}
}
}
private function cleanField(array &$data, array $defaults, $simulation = false): void
{
foreach ($defaults as $key => $value) {
if (isset($data[$key]) && Arr::get($data, $key) !== $value) {
$this->cleanings[$data['name']][] = $key;
if (!$simulation) {
Arr::set($data, $key, $value);
}
}
}
// Remove pro types columns
/*foreach (['files'] as $proType) {
if ($data['type'] == $proType && (!isset($data['hidden']) || !$data['hidden'])) {
$this->cleanings[$data['name']][] = $proType;
if (!$simulation) {
$data['hidden'] = true;
}
}
}*/
}
// Remove keys those have empty value
private function cleanCustomKeys($key, $formVal)
{
if (in_array($key, $this->customKeys) && $formVal !== null) {
$newVal = [];
foreach ($formVal as $k => $val) {
if ($val) {
$newVal[$k] = $val;
}
}
return $newVal;
}
return $formVal;
}
}

View File

@@ -0,0 +1,453 @@
<?php
namespace App\Service\Forms;
class FormLogicConditionChecker
{
public function __construct(private ?array $conditions, private ?array $formData)
{
}
public static function conditionsMet(?array $conditions, array $formData): bool
{
return (new self($conditions, $formData))->conditionsAreMet($conditions, $formData);
}
private function conditionsAreMet(?array $conditions, array $formData): bool
{
if (!$conditions) {
return false;
}
// If it's not a group, just a single condition
if (!isset($conditions['operatorIdentifier'])) {
return $this->propertyConditionMet($conditions['value'], $formData[$conditions['value']['property_meta']['id']] ?? null);
}
if ($conditions['operatorIdentifier'] === 'and') {
$isvalid = true;
foreach ($conditions['children'] as $childrenCondition) {
if (!$this->conditionsMet($childrenCondition, $formData)) {
$isvalid = false;
break;
}
}
return $isvalid;
} elseif ($conditions['operatorIdentifier'] === 'or') {
$isvalid = false;
foreach ($conditions['children'] as $childrenCondition) {
if ($this->conditionsMet($childrenCondition, $formData)) {
$isvalid = true;
break;
}
}
return $isvalid;
}
throw new \Exception('Unexcepted operatorIdentifier:' . $conditions['operatorIdentifier']);
}
private function propertyConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['property_meta']['type']) {
case 'text':
case 'url':
case 'email':
case 'phone_number':
return $this->textConditionMet($propertyCondition, $value);
case 'number':
case 'rating':
case 'scale':
case 'slider':
return $this->numberConditionMet($propertyCondition, $value);
case 'checkbox':
return $this->checkboxConditionMet($propertyCondition, $value);
case 'select':
return $this->selectConditionMet($propertyCondition, $value);
case 'date':
return $this->dateConditionMet($propertyCondition, $value);
case 'multi_select':
return $this->multiSelectConditionMet($propertyCondition, $value);
case 'files':
return $this->filesConditionMet($propertyCondition, $value);
case 'matrix':
return $this->matrixConditionMet($propertyCondition, $value);
}
return false;
}
private function checkEquals($condition, $fieldValue): bool
{
return $condition['value'] === $fieldValue;
}
private function checkContains($condition, $fieldValue): bool
{
if (is_array($fieldValue)) {
return in_array($condition['value'], $fieldValue);
}
return \Str::contains($fieldValue, $condition['value']);
}
private function checkMatrixContains($condition, $fieldValue): bool
{
foreach($condition['value'] as $key => $value) {
if(!(array_key_exists($key, $condition['value']) && array_key_exists($key, $fieldValue))) {
return false;
}
if($condition['value'][$key] == $fieldValue[$key]) {
return true;
}
}
return false;
}
private function checkMatrixEquals($condition, $fieldValue): bool
{
foreach($condition['value'] as $key => $value) {
if($condition['value'][$key] !== $fieldValue[$key]) {
return false;
}
}
return true;
}
private function checkListContains($condition, $fieldValue): bool
{
if (is_null($fieldValue)) {
return false;
}
if (!is_array($fieldValue)) {
return $this->checkEquals($condition, $fieldValue);
}
if (is_array($condition['value'])) {
return count(array_intersect($condition['value'], $fieldValue)) === count($condition['value']);
} else {
return in_array($condition['value'], $fieldValue);
}
}
private function checkStartsWith($condition, $fieldValue): bool
{
return str_starts_with($fieldValue, $condition['value']);
}
private function checkEndsWith($condition, $fieldValue): bool
{
return str_ends_with($fieldValue, $condition['value']);
}
private function checkIsEmpty($condition, $fieldValue): bool
{
if (is_array($fieldValue)) {
return count($fieldValue) === 0;
}
return $fieldValue == '' || $fieldValue == null || !$fieldValue;
}
private function checkGreaterThan($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && (float) $fieldValue > (float) $condition['value'];
}
private function checkGreaterThanEqual($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && (float) $fieldValue >= (float) $condition['value'];
}
private function checkLessThan($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && (float) $fieldValue < (float) $condition['value'];
}
private function checkLessThanEqual($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && (float) $fieldValue <= (float) $condition['value'];
}
private function checkBefore($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && $fieldValue < $condition['value'];
}
private function checkAfter($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && $fieldValue > $condition['value'];
}
private function checkOnOrBefore($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && $fieldValue <= $condition['value'];
}
private function checkOnOrAfter($condition, $fieldValue): bool
{
return $condition['value'] && $fieldValue && $fieldValue >= $condition['value'];
}
private function checkPastWeek($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subDays(7)->toDateString();
}
private function checkPastMonth($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subMonths(1)->toDateString();
}
private function checkPastYear($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate <= now()->toDateString() && $fieldDate >= now()->subYears(1)->toDateString();
}
private function checkNextWeek($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addDays(7)->toDateString();
}
private function checkNextMonth($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addMonths(1)->toDateString();
}
private function checkNextYear($condition, $fieldValue): bool
{
if (!$fieldValue) {
return false;
}
$fieldDate = date('Y-m-d', strtotime($fieldValue));
return $fieldDate >= now()->toDateString() && $fieldDate <= now()->addYears(1)->toDateString();
}
private function checkLength($condition, $fieldValue, $operator = '==='): bool
{
if (!$fieldValue || strlen($fieldValue) === 0) {
return false;
}
switch ($operator) {
case '===':
return strlen($fieldValue) === (int) $condition['value'];
case '!==':
return strlen($fieldValue) !== (int) $condition['value'];
case '>':
return strlen($fieldValue) > (int) $condition['value'];
case '>=':
return strlen($fieldValue) >= (int) $condition['value'];
case '<':
return strlen($fieldValue) < (int) $condition['value'];
case '<=':
return strlen($fieldValue) <= (int) $condition['value'];
}
return false;
}
private function textConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkEquals($propertyCondition, $value);
case 'does_not_equal':
return !$this->checkEquals($propertyCondition, $value);
case 'contains':
return $this->checkContains($propertyCondition, $value);
case 'does_not_contain':
return !$this->checkContains($propertyCondition, $value);
case 'starts_with':
return $this->checkStartsWith($propertyCondition, $value);
case 'ends_with':
return $this->checkEndsWith($propertyCondition, $value);
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'is_not_empty':
return !$this->checkIsEmpty($propertyCondition, $value);
case 'content_length_equals':
return $this->checkLength($propertyCondition, $value, '===');
case 'content_length_does_not_equal':
return $this->checkLength($propertyCondition, $value, '!==');
case 'content_length_greater_than':
return $this->checkLength($propertyCondition, $value, '>');
case 'content_length_greater_than_or_equal_to':
return $this->checkLength($propertyCondition, $value, '>=');
case 'content_length_less_than':
return $this->checkLength($propertyCondition, $value, '<');
case 'content_length_less_than_or_equal_to':
return $this->checkLength($propertyCondition, $value, '<=');
}
return false;
}
private function numberConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkEquals($propertyCondition, $value);
case 'does_not_equal':
return !$this->checkEquals($propertyCondition, $value);
case 'greater_than':
return $this->checkGreaterThan($propertyCondition, $value);
case 'less_than':
return $this->checkLessThan($propertyCondition, $value);
case 'greater_than_or_equal_to':
return $this->checkGreaterThanEqual($propertyCondition, $value);
case 'less_than_or_equal_to':
return $this->checkLessThanEqual($propertyCondition, $value);
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'is_not_empty':
return !$this->checkIsEmpty($propertyCondition, $value);
case 'content_length_equals':
return $this->checkLength($propertyCondition, $value, '===');
case 'content_length_does_not_equal':
return $this->checkLength($propertyCondition, $value, '!==');
case 'content_length_greater_than':
return $this->checkLength($propertyCondition, $value, '>');
case 'content_length_greater_than_or_equal_to':
return $this->checkLength($propertyCondition, $value, '>=');
case 'content_length_less_than':
return $this->checkLength($propertyCondition, $value, '<');
case 'content_length_less_than_or_equal_to':
return $this->checkLength($propertyCondition, $value, '<=');
}
return false;
}
private function checkboxConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkEquals($propertyCondition, $value);
case 'does_not_equal':
return !$this->checkEquals($propertyCondition, $value);
}
return false;
}
private function selectConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkEquals($propertyCondition, $value);
case 'does_not_equal':
return !$this->checkEquals($propertyCondition, $value);
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'is_not_empty':
return !$this->checkIsEmpty($propertyCondition, $value);
}
return false;
}
private function dateConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkEquals($propertyCondition, $value);
case 'before':
return $this->checkBefore($propertyCondition, $value);
case 'after':
return $this->checkAfter($propertyCondition, $value);
case 'on_or_before':
return $this->checkOnOrBefore($propertyCondition, $value);
case 'on_or_after':
return $this->checkOnOrAfter($propertyCondition, $value);
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'past_week':
return $this->checkPastWeek($propertyCondition, $value);
case 'past_month':
return $this->checkPastMonth($propertyCondition, $value);
case 'past_year':
return $this->checkPastYear($propertyCondition, $value);
case 'next_week':
return $this->checkNextWeek($propertyCondition, $value);
case 'next_month':
return $this->checkNextMonth($propertyCondition, $value);
case 'next_year':
return $this->checkNextYear($propertyCondition, $value);
}
return false;
}
private function multiSelectConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'contains':
return $this->checkListContains($propertyCondition, $value);
case 'does_not_contain':
return !$this->checkListContains($propertyCondition, $value);
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'is_not_empty':
return !$this->checkIsEmpty($propertyCondition, $value);
}
return false;
}
private function filesConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'is_empty':
return $this->checkIsEmpty($propertyCondition, $value);
case 'is_not_empty':
return !$this->checkIsEmpty($propertyCondition, $value);
}
return false;
}
private function matrixConditionMet(array $propertyCondition, $value): bool
{
switch ($propertyCondition['operator']) {
case 'equals':
return $this->checkMatrixEquals($propertyCondition, $value);
case 'does_not_equal':
return !$this->checkMatrixEquals($propertyCondition, $value);
case 'contains':
return $this->checkMatrixContains($propertyCondition, $value);
case 'does_not_contain':
return !$this->checkMatrixContains($propertyCondition, $value);
}
return false;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Service\Forms;
class FormLogicPropertyResolver
{
private $property = [];
private $formData = [];
private $logic = false;
public function __construct(private array $prop, private array $values)
{
$this->property = $prop;
$this->formData = $values;
$this->logic = isset($this->property['logic']) ? $this->property['logic'] : false;
}
public static function isRequired(array $property, array $values): bool
{
return (new self($property, $values))->shouldBeRequired();
}
public static function isHidden(array $property, array $values): bool
{
return (new self($property, $values))->shouldBeHidden();
}
public function shouldBeRequired(): bool
{
if (! isset($this->property['required'])) {
return false;
}
if (! $this->logic) {
return $this->property['required'];
}
$conditionsMet = FormLogicConditionChecker::conditionsMet($this->logic['conditions'], $this->formData);
if ($conditionsMet && $this->property['required'] && count($this->logic['actions']) > 0 && (in_array('make-it-optional', $this->logic['actions']) || in_array('hide-block', $this->logic['actions']))) {
return false;
} elseif ($conditionsMet && ! $this->property['required'] && count($this->logic['actions']) > 0 && in_array('require-answer', $this->logic['actions'])) {
return true;
} else {
return $this->property['required'];
}
}
public function shouldBeHidden(): bool
{
if (! isset($this->property['hidden'])) {
return false;
}
if (! $this->logic) {
return $this->property['hidden'];
}
$conditionsMet = FormLogicConditionChecker::conditionsMet($this->logic['conditions'], $this->formData);
if ($conditionsMet && $this->property['hidden'] && count($this->logic['actions']) > 0 && in_array('show-block', $this->logic['actions'])) {
return false;
} elseif ($conditionsMet && ! $this->property['hidden'] && count($this->logic['actions']) > 0 && in_array('hide-block', $this->logic['actions'])) {
return true;
} else {
return $this->property['hidden'];
}
}
}

View File

@@ -0,0 +1,297 @@
<?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
*
* @var bool
*/
private $createLinks = false;
/**
* If true, serialize arrays
*
* @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;
public function __construct(private Form $form, private array $formData)
{
$this->initIdFormData();
}
public function createLinks()
{
$this->createLinks = true;
return $this;
}
public function showHiddenFields()
{
$this->showHiddenFields = true;
return $this;
}
public function outputStringsOnly()
{
$this->outputStringsOnly = true;
return $this;
}
public function setEmptyForNoValue()
{
$this->setEmptyForNoValue = true;
return $this;
}
public function showRemovedFields()
{
$this->showRemovedFields = true;
return $this;
}
public function useSignedUrlForFiles()
{
$this->useSignedUrlForFiles = true;
return $this;
}
public function getMatrixString(array $val): string
{
$parts = [];
foreach ($val as $key => $value) {
if ($key !== null && $value !== null) {
$parts[] = "$key: $value";
}
}
return implode(' | ', $parts);
}
/**
* 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,
'removed' => true,
];
});
if ($this->showRemovedFields) {
$fields = $fields->merge($removeFields);
}
$fields = $fields->filter(function ($field) {
return !in_array($field['type'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']);
})->values();
$returnArray = [];
foreach ($fields as $field) {
if (in_array($field['id'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image'])) {
continue;
}
if ($field['removed'] ?? false) {
$field['name'] = $field['name'] . ' (deleted)';
}
// Add ID to avoid name clashes
$field['name'] = $field['name'] . ' (' . \Str::of($field['id']) . ')';
// If not present skip
if (!isset($data[$field['id']])) {
if ($this->setEmptyForNoValue) {
$returnArray[$field['name']] = '';
}
continue;
}
// If hide hidden fields
if (!$this->showHiddenFields) {
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData ?? [])) {
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 && is_array($val)) {
$returnArray[$field['name']] = implode(', ', $val);
} else {
$returnArray[$field['name']] = $val;
}
} elseif ($field['type'] == 'matrix' && is_array($data[$field['id']])) {
$returnArray[$field['name']] = $this->getMatrixString($data[$field['id']]);
} elseif ($field['type'] == 'files') {
if ($this->outputStringsOnly) {
$formId = $this->form->id;
$returnArray[$field['name']] = implode(
', ',
collect($data[$field['id']])->map(function ($file) use ($formId) {
return $this->getFileUrl($formId, $file);
})->toArray()
);
} else {
$formId = $this->form->id;
$returnArray[$field['name']] = collect($data[$field['id']])->map(function ($file) use ($formId) {
return [
'file_url' => $this->getFileUrl($formId, $file),
'file_name' => $file,
];
});
}
} else {
if (is_array($data[$field['id']]) && $this->outputStringsOnly) {
$data[$field['id']] = implode(', ', $data[$field['id']]);
}
$returnArray[$field['name']] = $data[$field['id']];
}
}
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) {
if (!isset($field['id']) || !isset($data[$field['id']])) {
continue;
}
// If hide hidden fields
if (!$this->showHiddenFields) {
if (FormLogicPropertyResolver::isHidden($field, $this->idFormData)) {
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') {
$dateFormat = ($field['date_format'] ?? 'dd/MM/yyyy') == 'dd/MM/yyyy' ? 'd/m/Y' : 'm/d/Y';
if (isset($field['with_time']) && $field['with_time']) {
$dateFormat .= (isset($field['time_format']) && $field['time_format'] == 24) ? ' H:i' : ' g:ia';
}
if (is_array($data[$field['id']])) {
$field['value'] = isset($data[$field['id']][1]) ? (new Carbon($data[$field['id']][0]))->format($dateFormat)
. ' - ' . (new Carbon($data[$field['id']][1]))->format($dateFormat) : (new Carbon($data[$field['id']][0]))->format($dateFormat);
} else {
$field['value'] = (new Carbon($data[$field['id']]))->format($dateFormat);
}
} elseif ($field['type'] == 'multi_select') {
$val = $data[$field['id']];
if ($this->outputStringsOnly) {
$field['value'] = implode(', ', $val);
} else {
$field['value'] = $val;
}
} elseif ($field['type'] == 'matrix') {
$field['value'] = str_replace(' | ', "\n", $this->getMatrixString($data[$field['id']]));
} elseif ($field['type'] == 'files') {
if ($this->outputStringsOnly) {
$formId = $this->form->id;
$field['value'] = implode(
', ',
collect($data[$field['id']])->map(function ($file) use ($formId) {
return $this->getFileUrl($formId, $file);
})->toArray()
);
$field['email_data'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
$splitText = explode('.', $file);
return [
'unsigned_url' => route('open.forms.submissions.file', [$formId, $file]),
'signed_url' => $this->getFileUrl($formId, $file),
'label' => \Str::limit($file, 20, '[...].' . end($splitText)),
];
})->toArray();
} else {
$formId = $this->form->id;
$field['value'] = collect($data[$field['id']])->map(function ($file) use ($formId) {
return [
'file_url' => $this->getFileUrl($formId, $file),
'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;
}
return $transformedFields;
}
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',
[$formId, $file]
) : route('open.forms.submissions.file', [$formId, $file]);
}
}