Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C0 3604 Добавить компонент формы в CMS #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Assembler;

use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto\GetPageComponentFormResultDto;
use Skyeng\MarketingCmsBundle\Domain\Entity\PageComponent;
use Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\PageComponentType;
use Symfony\Component\Form\FormFactoryInterface;

class GetPageComponentFormV1ResultAssembler implements GetPageComponentFormV1ResultAssemblerInterface
{
/**
* @var FormFactoryInterface
*/
private $formBuilder;

public function __construct(FormFactoryInterface $formBuilder)
{
$this->formBuilder = $formBuilder;
}

public function assemble(PageComponent $component): GetPageComponentFormResultDto
{
$form = $this->formBuilder->create(PageComponentType::class, $component);

$resultDto = new GetPageComponentFormResultDto();
$resultDto->result = $form;

return $resultDto;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Assembler;

use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto\GetPageComponentFormResultDto;
use Skyeng\MarketingCmsBundle\Domain\Entity\PageComponent;

interface GetPageComponentFormV1ResultAssemblerInterface
{
public function assemble(PageComponent $component): GetPageComponentFormResultDto;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto;

class GetPageComponentFormRequestDto
{
/**
* @var string
*/
public $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto;

use Symfony\Component\Form\FormInterface;

class GetPageComponentFormResultDto
{
/**
* Форма компонента страницы
* @var FormInterface
*/
public $result;
}
38 changes: 38 additions & 0 deletions src/Application/Cms/PageComponentForm/PageComponentFormService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm;

use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Assembler\GetPageComponentFormV1ResultAssemblerInterface;
use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto\GetPageComponentFormRequestDto;
use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto\GetPageComponentFormResultDto;
use Skyeng\MarketingCmsBundle\Domain\Entity\PageComponent;
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\Id;
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\PageComponentName;

class PageComponentFormService
{
/**
* @var GetPageComponentFormV1ResultAssemblerInterface
*/
private $assembler;

public function __construct(GetPageComponentFormV1ResultAssemblerInterface $assembler)
{
$this->assembler = $assembler;
}

public function getPageComponentForm(GetPageComponentFormRequestDto $dto): GetPageComponentFormResultDto
{
$pageComponent = new PageComponent(
new Id('1'),
null,
new PageComponentName($dto->name),
[],
1
);

return $this->assembler->assemble($pageComponent);
}
}
7 changes: 7 additions & 0 deletions src/Domain/Entity/ValueObject/PageComponentName.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
class PageComponentName
{
public const HTML_COMPONENT = 'html-component';
public const SIMPLE_FORM_COMPONENT = 'simple-form-component';

public const AVAILABLE_COMPONENTS = [
self::HTML_COMPONENT => self::HTML_COMPONENT,
self::SIMPLE_FORM_COMPONENT => self::SIMPLE_FORM_COMPONENT,
];

use ValueObjectTrait;
Expand All @@ -28,4 +30,9 @@ public function isHtml(): bool
{
return $this->value === self::HTML_COMPONENT;
}

public function isSimpleForm(): bool
{
return $this->value === self::SIMPLE_FORM_COMPONENT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class HTMLComponentType extends AbstractType implements DataTransformerInterface
{
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('html', TextareaType::class, [
'attr' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\ComponentTypes;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class SimpleFormComponentType extends AbstractType implements DataTransformerInterface
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('field', TextType::class)
->add('stk', IntegerType::class);

$builder->resetViewTransformers();
$builder->addViewTransformer($this);
}

public function transform($value): array
{
$field = $value['field'] ?? null;
$stk = $value['stk'] ?? null;

return [
'field' => (string)$field,
'stk' => (int)$stk,
];
}

public function reverseTransform($value): array
{
return $value;
}
}
4 changes: 2 additions & 2 deletions src/Infrastructure/Symfony/Form/ContentTypeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->addViewTransformer($this);
}

public function transform($choice)
public function transform($choice): string
{
return (string)$choice;
}

public function reverseTransform($value)
public function reverseTransform($value): ContentType
{
if (!is_string($value)) {
throw new TransformationFailedException('Expected a string.');
Expand Down
46 changes: 44 additions & 2 deletions src/Infrastructure/Symfony/Form/PageComponentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\PageComponentName;
use Skyeng\MarketingCmsBundle\Domain\Repository\PageComponentRepository\PageComponentRepositoryInterface;
use Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\ComponentTypes\HTMLComponentType;
use Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\ComponentTypes\SimpleFormComponentType;
use Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\PageComponentNameType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PageComponentType extends AbstractType
{
private const COMPONENT_CLASS = [
PageComponentName::HTML_COMPONENT => HTMLComponentType::class,
PageComponentName::SIMPLE_FORM_COMPONENT => SimpleFormComponentType::class,
];

private const COMPONENTS_LIST = [
'HTML' => PageComponentName::HTML_COMPONENT,
'Simple form' => PageComponentName::SIMPLE_FORM_COMPONENT,
];

/**
* @var PageComponentRepositoryInterface
*/
Expand All @@ -32,11 +45,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', PageComponentNameType::class, [
'choices' => ['HTML' => PageComponentName::HTML_COMPONENT],
'choices' => self::COMPONENTS_LIST,
'required' => true,
'label' => 'Компонент',
'attr' => [
'class' => 'field-select',
'class' => 'field-select page-component-name-select',
'data-widget' => 'select2',
]
])
Expand All @@ -53,6 +66,35 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('data', HTMLComponentType::class, [
'label' => 'Данные'
]);

$builder->addEventListener(FormEvents::PRE_SET_DATA, static function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();

if ($data instanceof PageComponent) {
$form->add('data', self::COMPONENT_CLASS[$data->getName()->getValue()], [
'label' => 'Данные'
]);
}
});

$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();

if (!array_key_exists('name', $data)) {
return;
}

if (!in_array($data['name'], self::COMPONENTS_LIST, true)) {
return;
}

$pageComponentName = new PageComponentName($data['name']);
$form->add('data', self::COMPONENT_CLASS[$pageComponentName->getValue()], [
'label' => 'Данные'
]);
});
}

public function configureOptions(OptionsResolver $resolver): void
Expand Down
21 changes: 21 additions & 0 deletions src/Resources/public/dynamic-page-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$('body').on('change', '.page-component-name-select', function (event) {
var data = {'name': $(this).val()};

var idPrefix = $(this).attr('id');
idPrefix = idPrefix.substr(0, idPrefix.length - 5); //name
var namePrefix = $(this).attr('name');
namePrefix = namePrefix.substr(0, namePrefix.length - 6); //[name]

$.ajax({
url : '/admin/page-component/form',
type: 'GET',
data : data,
success: function(html) {
html = html.replaceAll('name="page_component', 'name="' + namePrefix);
html = html.replaceAll('id="page_component_', 'id="' + idPrefix + '_');

$dataObj = $('#' + idPrefix).find('#' + idPrefix + '_data');
$dataObj.closest('.form-widget-compound').replaceWith(html);
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% form_theme form with themes only %}

{{ form_widget(form.data) }}


Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Skyeng\MarketingCmsBundle\UI\Controller\Admin\PageComponentForm;

use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\Dto\GetPageComponentFormRequestDto;
use Skyeng\MarketingCmsBundle\Application\Cms\PageComponentForm\PageComponentFormService;
use Skyeng\MarketingCmsBundle\Application\Exception\ValidationException;
use Skyeng\MarketingCmsBundle\UI\Controller\Admin\PageComponentForm\Validation\GetPageComponentFormV1Form;
use Skyeng\MarketingCmsBundle\UI\Service\Response\ResponseFactory;
use Skyeng\MarketingCmsBundle\UI\Service\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class GetPageComponentFormController extends AbstractController
{
/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var PageComponentFormService
*/
private $pageComponentFormService;

/**
* @var ResponseFactory
*/
private $responseFactory;

public function __construct(
ValidatorInterface $validator,
PageComponentFormService $pageComponentFormService,
ResponseFactory $responseFactory
) {
$this->validator = $validator;
$this->pageComponentFormService = $pageComponentFormService;
$this->responseFactory = $responseFactory;
}

/**
* Получить виджет формы для компонента
*
* @Route("/admin/page-component/form", methods={"GET"})
*
* @param Request $request
* @throws ValidationException
*/
public function __invoke(Request $request): Response
{
$dto = new GetPageComponentFormRequestDto();

try {
$this->validator->validate(GetPageComponentFormV1Form::class, $request->query->all(), $dto);
} catch (ValidationException $exception) {
return ResponseFactory::createErrorResponse($request, $exception->getMessage(), $exception->getErrors());
}

$result = $this->pageComponentFormService->getPageComponentForm($dto);

return $this->responseFactory->createViewResponse(
'@MarketingCms/page_component/page_component_data_form.html.twig',
[
'form' => $result->result->createView(),
'themes' => [
'@EasyAdmin/crud/form_theme.html.twig',
],
]
);
}
}
Loading