diff --git a/Controller/FormController.php b/Controller/FormController.php index c16d5787..4d05d9f3 100644 --- a/Controller/FormController.php +++ b/Controller/FormController.php @@ -24,6 +24,7 @@ use Sulu\Component\Rest\ListBuilder\ListRepresentation; use Sulu\Component\Rest\ListBuilder\ListRestHelperInterface; use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface; +use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation; use Sulu\Component\Rest\RestHelperInterface; use Sulu\Component\Security\SecuredControllerInterface; use Symfony\Component\HttpFoundation\Request; @@ -139,11 +140,9 @@ public function cgetAction(Request $request): Response } // create list representation - $representation = new ListRepresentation( + $representation = new PaginatedRepresentation( $list, $this->getListName(), - $request->get('_route'), - $request->query->all(), $page, $limit, $total diff --git a/Controller/FormWebsiteController.php b/Controller/FormWebsiteController.php deleted file mode 100644 index 6cba6c4a..00000000 --- a/Controller/FormWebsiteController.php +++ /dev/null @@ -1,248 +0,0 @@ -getAttributes([], $structure, $preview); - - $template = $structure->getKey(); - - $typeClass = $this->getTypeClass($template); - /** @var AbstractType $type */ - $type = $this->container->get('form.registry')->getType($typeClass)->getInnerType(); - $type->setAttributes($attributes); - - $this->form = $this->container->get('form.factory')->create($typeClass, [], [ - 'csrf_token_manager' => new DisabledCsrfTokenManager($this->container->get('security.csrf.token_manager')), - ]); - $this->form->handleRequest($request); - - if ($this->form->isSubmitted() - && $this->form->isValid() - && $response = $this->handleFormSubmit($request, $type, $attributes) - ) { - // success form submit - - return $response; - } - - return parent::indexAction($structure, $preview, $partial); - } - - /** - * Form only action. - * - * @return RedirectResponse|Response - */ - public function onlyAction(Request $request, string $key): Response - { - $ajaxTemplates = $this->getParameter('sulu_form.ajax_templates'); - - if (!$ajaxTemplates[$key]) { - throw new NotFoundHttpException(); - } - - $typeClass = $this->getTypeClass($key); - /** @var AbstractType $type */ - $type = $this->container->get('form.registry')->getType($typeClass)->getInnerType(); - $this->form = $this->container->get('form.factory')->create($typeClass); - $this->form->handleRequest($request); - - if ($this->form->isSubmitted() - && $this->form->isValid() - && $response = $this->handleFormOnlySubmit($request, $type) - ) { - // success form submit - - return $response; - } - - return $this->render($ajaxTemplates[$key], ['form' => $this->form->createView()]); - } - - /** - * Handle form submit. - * - * @param mixed[] $attributes - * - * @return JsonResponse|RedirectResponse|null - */ - private function handleFormSubmit(Request $request, AbstractType $type, array $attributes): ?Response - { - // handle form submit - $configuration = $this->container->get('sulu_form.configuration.form_configuration_factory')->buildByType( - $type, - $this->form->getData(), - $request->getLocale(), - $attributes - ); - - $success = $this->container->get('sulu_form.handler')->handle($this->form, $configuration); - - if ($success) { - if ($request->isXmlHttpRequest()) { - return new JsonResponse(['send' => $success]); - } - - return new RedirectResponse('?send=true'); - } - - if ($request->isXmlHttpRequest()) { - return new JsonResponse( - [ - 'send' => false, - 'errors' => $this->getErrors(), - ], - 400 - ); - } - - return null; - } - - private function handleFormOnlySubmit(Request $request, AbstractType $type): ?RedirectResponse - { - // handle form submit - $configuration = $this->container->get('sulu_form.configuration.form_configuration_factory')->buildByType( - $type, - $this->form->getData(), - $request->getLocale(), - [] - ); - - if ($this->container->get('sulu_form.handler')->handle($this->form, $configuration)) { - return new RedirectResponse('?send=true'); - } - - return null; - } - - /** - * Get errors. - * - * @return array - */ - protected function getErrors(): array - { - $errors = []; - - $generalErrors = []; - /** @var FormError $error */ - foreach ($this->form->getErrors() as $error) { - $generalErrors[] = $error->getMessage(); - } - - if (!empty($generalErrors)) { - $errors['general'] = $generalErrors; - } - - foreach ($this->form->all() as $field) { - $fieldErrors = []; - - /** @var FormError $error */ - foreach ($field->getErrors() as $error) { - $fieldErrors[] = $error->getMessage(); - } - - if (!empty($fieldErrors)) { - $errors[$field->getName()] = $fieldErrors; - } - } - - return $errors; - } - - /** - * @param mixed[] $attributes - * @param bool $preview - * - * @return mixed[] - */ - protected function getAttributes($attributes, ?StructureInterface $structure = null, $preview = false) - { - if (null === $this->attributes) { // for performance only called once - $this->attributes = parent::getAttributes($attributes, $structure, $preview); - } - - if ($this->form) { - $this->attributes['form'] = $this->form->createView(); - } - - return $this->attributes; - } - - /** - * @return class-string - */ - private function getTypeClass(string $key): string - { - return $this->getParameter('sulu_form.static_forms')[$key]['class']; - } -} diff --git a/Controller/ListController.php b/Controller/ListController.php index 0f8a50b1..d255d7d7 100644 --- a/Controller/ListController.php +++ b/Controller/ListController.php @@ -17,6 +17,7 @@ use Sulu\Component\Rest\AbstractRestController; use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactoryInterface; use Sulu\Component\Rest\ListBuilder\ListRepresentation; +use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation; use Sulu\Component\Rest\RestHelperInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -109,11 +110,9 @@ public function cgetAction(Request $request): Response $limit = $listBuilder->getLimit(); // create list representation - $representation = new ListRepresentation( + $representation = new PaginatedRepresentation( $list, 'entries', - $request->get('_route'), - $request->query->all(), $page, $limit, $total diff --git a/DependencyInjection/SuluFormExtension.php b/DependencyInjection/SuluFormExtension.php index f8db7e9e..1df2003a 100644 --- a/DependencyInjection/SuluFormExtension.php +++ b/DependencyInjection/SuluFormExtension.php @@ -228,8 +228,6 @@ public function load(array $configs, ContainerBuilder $container): void } if (SuluKernel::CONTEXT_WEBSITE === $container->getParameter('sulu.context')) { - $container->setAlias(FormWebsiteController::class, 'sulu_form.form_website_controller') - ->setPublic(true); $container->setAlias(FormTokenController::class, 'sulu_form.form_token_controller') ->setPublic(true); } diff --git a/Metadata/DynamicFormMetadataLoader.php b/Metadata/DynamicFormMetadataLoader.php index e1b0bb7e..73034671 100644 --- a/Metadata/DynamicFormMetadataLoader.php +++ b/Metadata/DynamicFormMetadataLoader.php @@ -12,7 +12,7 @@ namespace Sulu\Bundle\FormBundle\Metadata; use Sulu\Bundle\AdminBundle\FormMetadata\FormMetadataMapper; -use Sulu\Bundle\AdminBundle\FormMetadata\FormXmlLoader; +use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\Loader\FormXmlLoader; use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata; use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata; use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadataLoaderInterface; @@ -45,6 +45,8 @@ class DynamicFormMetadataLoader implements FormMetadataLoaderInterface, CacheWar /** * @var FormMetadataMapper + * + * TODO REMOVE */ private $formMetadataMapper; @@ -90,7 +92,7 @@ public function warmUp($cacheDir, ?string $buildDir = null): array $formMetadataCollection = $this->formXmlLoader->load($resource); foreach ($formMetadataCollection->getItems() as $locale => $formMetadata) { $section = new SectionMetadata('formFields'); - $section->setLabel($this->translator->trans('sulu_form.form_fields', [], 'admin', $locale)); + $section->setLabel($this->translator->trans('sulu_form.form_fields', [], 'admin', $locale), $locale); $fields = new FieldMetadata('fields'); $fields->setType('block'); @@ -101,8 +103,8 @@ public function warmUp($cacheDir, ?string $buildDir = null): array $fieldTypeMetaDataCollection[] = $this->loadFieldTypeMetadata($typeKey, $type, $locale); } - \usort($fieldTypeMetaDataCollection, static function(FormMetadata $a, FormMetadata $b): int { - return \strcmp($a->getTitle(), $b->getTitle()); + \usort($fieldTypeMetaDataCollection, static function(FormMetadata $a, FormMetadata $b)use($locale): int { + return \strcmp($a->getTitle($locale), $b->getTitle($locale)); }); foreach ($fieldTypeMetaDataCollection as $fieldTypeMetaData) { @@ -111,11 +113,13 @@ public function warmUp($cacheDir, ?string $buildDir = null): array $fields->setDefaultType(\current($fields->getTypes())->getName()); $section->addItem($fields); + /* TODO HOW? $formItems = $formMetadata->getItems(); $this->arrayInsertAtPosition($formItems, 1, [$section->getName() => $section]); $formMetadata->setItems($formItems); $configCache = $this->getConfigCache($formMetadata->getKey(), $locale); $configCache->write(\serialize($formMetadata), [new FileResource($resource)]); + */ } return []; @@ -151,11 +155,12 @@ private function loadFieldTypeMetadata(string $typeKey, FormFieldTypeInterface $ { $form = new FormMetadata(); $configuration = $type->getConfiguration(); + $properties = $this->propertiesXmlLoader->load($configuration->getXmlPath()); - $form->setItems($this->formMetadataMapper->mapChildren($properties->getProperties(), $locale)); - $form->setName($typeKey); - $form->setTitle($this->translator->trans($configuration->getTitle(), [], 'admin', $locale)); + $form->setItems($properties); + $form->setKey($typeKey); + $form->setTitle($this->translator->trans($configuration->getTitle(), [], 'admin', $locale), $locale); return $form; } diff --git a/Metadata/PropertiesXmlLoader.php b/Metadata/PropertiesXmlLoader.php index fb6521b9..0fde27ac 100644 --- a/Metadata/PropertiesXmlLoader.php +++ b/Metadata/PropertiesXmlLoader.php @@ -11,8 +11,8 @@ namespace Sulu\Bundle\FormBundle\Metadata; -use Sulu\Component\Content\Metadata\Loader\AbstractLoader; -use Sulu\Component\Content\Metadata\Parser\PropertiesXmlParser; +use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\Loader\AbstractLoader; +use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\Parser\PropertiesXmlParser; use Sulu\Component\Content\Metadata\PropertiesMetadata; class PropertiesXmlLoader extends AbstractLoader @@ -40,25 +40,17 @@ public function __construct( * @param string $resource * @param string $type */ - protected function parse($resource, \DOMXPath $xpath, $type): PropertiesMetadata + protected function parse($resource, \DOMXPath $xpath, $type): array { - $tags = []; - $propertiesMetadata = new PropertiesMetadata(); $propertiesMetadata->setResource($resource); $propertiesNode = $xpath->query('/x:properties')->item(0); $properties = $this->propertiesXmlParser->load( - $tags, $xpath, - $propertiesNode + $propertiesNode, ); - foreach ($properties as $property) { - $propertiesMetadata->addChild($property); - } - $propertiesMetadata->burnProperties(); - - return $propertiesMetadata; + return $properties; } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index f1f69cdd..3af04f6d 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -174,7 +174,7 @@ id="sulu_form.metadata.properties_xml_loader" class="Sulu\Bundle\FormBundle\Metadata\PropertiesXmlLoader" > - + @@ -226,19 +226,6 @@ - - - - - - - - - - -