Skip to content

Commit

Permalink
New: Add API embed type
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed May 26, 2023
1 parent 1a42afd commit d5b4d42
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
113 changes: 113 additions & 0 deletions Classes/FusionObjects/ApiFormImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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
* @var ApiService
*/
protected $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);
$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';
} else if (in_array($type, ['select', 'country'])) {
$tagName = 'select';
} else if (in_array($type, ['radiogrp', 'checkboxgrp'])) {
$tagName = 'inputGroup';
$value = $value ? array_map('trim', explode(',', $value)) : [];
} else if ($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,
];
}
}
16 changes: 16 additions & 0 deletions Classes/Service/ApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ public function getAllSegments(): array
return $this->validateResponse($this->contactApi->getSegments());
}

/**
* Get a single form. Fails gracefully
*
* @param integer $id
* @return array
*/
public function getForm(int $id): array
{
$data = $this->validateResponse($this->formApi->get($id), null, false);
if (isset($data['form']) && $data['form']['isPublished']) {
return $data['form'];
}

return [];
}

/**
* Get the list of all forms
*
Expand Down
5 changes: 5 additions & 0 deletions Resources/Private/Fusion/Component/API/Form.fusion
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prototype(Garagist.Mautic:API.Form) {
@class = 'Garagist\\Mautic\\FusionObjects\\ApiFormImplementation'

id = null
}
3 changes: 2 additions & 1 deletion Resources/Private/Fusion/Component/Form/Form.fusion
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ prototype(Garagist.Mautic:Component.Form) < prototype(Neos.Fusion:Component) {
id = ${PropTypes.integer}
url = ${PropTypes.string}
apiUrl = ${PropTypes.string}
embedType = ${PropTypes.oneOf(['javascript', 'iframe', 'plain'])}
embedType = ${PropTypes.oneOf(['javascript', 'iframe', 'plain', 'api'])}
iframeClass = ${PropTypes.anyOf( PropTypes.string, PropTypes.arrayOf( PropTypes.string ) )}
waitMessage = ${PropTypes.string}
}
Expand All @@ -19,5 +19,6 @@ prototype(Garagist.Mautic:Component.Form) < prototype(Neos.Fusion:Component) {
<Garagist.Mautic:Component.Form.Fragment.Javascript @if={props.embedType == 'javascript'} {...props} />
<Garagist.Mautic:Component.Form.Fragment.Iframe @if={props.embedType == 'iframe'} {...props} />
<Garagist.Mautic:Component.Form.Fragment.Plain @if={props.embedType == 'plain'} {...props} />
{props.content}
`
}

0 comments on commit d5b4d42

Please sign in to comment.