-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiFormImplementation.php
115 lines (101 loc) · 3.85 KB
/
ApiFormImplementation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Garagist\Mautic\FusionObjects;
use Garagist\Mautic\Service\ApiService;
use Neos\Flow\Annotations as Flow;
use Neos\Fusion\FusionObjects\AbstractFusionObject;
class ApiFormImplementation extends AbstractFusionObject
{
#[Flow\Inject]
protected ApiService $apiService;
/**
* @return string
*/
public function evaluate()
{
$id = (int)$this->fusionValue('id');
$url = $this->fusionValue('url');
if (!isset($id) || !$url) {
return [];
}
$data = $this->apiService->getForm($id);
if (!$data || !isset($data['fields'])) {
return [];
}
$parentsMap = [];
foreach ($data['fields'] as $field) {
$parentsMap[$field['id']] = $field['alias'];
}
$page = 1;
$fields = [
1 => []
];
$hiddenFields = [
['formId', $data['id']],
['formName', $data['alias']],
['messenger', 1]
];
$defaultValues = [];
foreach ($data['fields'] as $field) {
$type = $field['type'];
$name = $field['alias'];
$value = $field['defaultValue'];
if ($type === 'hidden') {
$hiddenFields[] = [$name, $value];
continue;
}
$tagName = null;
if (in_array($type, ['email', 'password', 'text', 'file', 'date', 'datetime', 'number', 'captcha', 'url', 'tel'])) {
$tagName = 'input';
} elseif (in_array($type, ['select', 'country'])) {
$tagName = 'select';
} elseif (in_array($type, ['radiogrp', 'checkboxgrp'])) {
$tagName = 'inputGroup';
$value = $value ? array_map('trim', explode(',', $value)) : [];
} elseif ($type == 'textarea') {
$tagName = $type;
}
if ($tagName) {
$defaultValues[] = [$name, $value];
}
$fields[$page][] = array_filter([
'name' => $name,
'label' => $field['label'],
'showLabel' => $field['showLabel'],
'type' => $type == 'datetime' ? 'datetime-local' : $type,
'tagName' => $tagName,
'value' => $field['defaultValue'],
'required' => $field['isRequired'],
'validation' => $field['validationMessage'],
'help' => $field['helpMessage'],
'placeholder' => $field['properties']['placeholder'] ?? null,
'options' => $field['properties']['list']['list'] ?? $field['properties']['optionlist']['list'] ?? [],
'multiple' => !!($field['properties']['multiple'] ?? null),
'text' => $field['properties']['text'] ?? null,
'nextPageLabel' => $field['properties']['next_page_label'] ?? null,
'prevPageLabel' => $field['properties']['prev_page_label'] ?? null,
'captcha' => $field['properties']['captcha'] ?? null,
'errorMessage' => $field['properties']['errorMessage'] ?? null,
'dependOn' => $field['parent'] ? [
'name' => $parentsMap[$field['parent']],
'conditions' => $field['conditions']
] : null
]);
if ($type === 'pagebreak') {
$page++;
$fields[$page] = [];
}
}
return [
'form' => [
'id' => $data['id'],
'name' => $data['alias'],
'action' => $url . '/form/submit',
'origin' => $url,
'showMessage' => $data['postAction'] === 'message',
],
'fields' => $fields,
'hiddenFields' => $hiddenFields,
'defaults' => $defaultValues,
];
}
}