diff --git a/src/core/Controller/Admin/Crud/CrudController.php b/src/core/Controller/Admin/Crud/CrudController.php index 825de13..34d3d9d 100644 --- a/src/core/Controller/Admin/Crud/CrudController.php +++ b/src/core/Controller/Admin/Crud/CrudController.php @@ -127,6 +127,51 @@ abstract class CrudController extends AdminController ]); } + protected function doInlineEdit(string $context, string $label, EntityInterface $entity, EntityManager $entityManager, Request $request, callable $beforeUpdate = null): Response + { + $configuration = $this->getConfiguration(); + + $this->prepareEntity($entity); + + $builder = $this->createFormBuilder($entity); + $callback = $configuration->getFields($context)[$label]['options']['inline_form'] ?? null; + + if (null === $callback) { + throw $this->createNotFoundException(); + } + + call_user_func_array($callback, [$builder, $entity]); + + $form = $builder->getForm(); + $redirectTo = $request->query->get('redirectTo'); + + if ($request->isMethod('POST')) { + $form->handleRequest($request); + + if ($form->isValid()) { + if (null !== $beforeUpdate) { + call_user_func_array($beforeUpdate, [$entity, $form, $request]); + } + + $entityManager->update($entity); + $this->addFlash('success', 'The data has been saved.'); + } else { + $this->addFlash('warning', 'The form is not valid.'); + } + + return $this->redirect($redirectTo); + } + + return $this->render('@Core/admin/crud/inline_edit.html.twig', [ + 'form' => $form->createView(), + 'configuration' => $configuration, + 'entity' => $entity, + 'context' => $context, + 'label' => $label, + 'redirectTo' => $redirectTo, + ]); + } + protected function doSort(int $page, RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session): Response { $configuration = $this->getConfiguration(); diff --git a/src/core/Crud/CrudConfiguration.php b/src/core/Crud/CrudConfiguration.php index f25ee94..f1b51ad 100644 --- a/src/core/Crud/CrudConfiguration.php +++ b/src/core/Crud/CrudConfiguration.php @@ -17,6 +17,7 @@ class CrudConfiguration protected array $actionTitles = []; protected array $forms = []; protected array $formOptions = []; + protected array $inlineForms = []; protected array $views = []; protected array $viewDatas = []; protected array $fields = []; @@ -252,7 +253,7 @@ class CrudConfiguration /* -- */ - public function setMaxPerPage(string $page, int $max) + public function setMaxPerPage(string $page, int $max): self { $this->maxPerPage[$page] = $max; @@ -266,6 +267,20 @@ class CrudConfiguration /* -- */ + public function setDoubleClick(string $page, bool $enabled): bool + { + $this->doubleClick[$page] = $enabled; + + return $this; + } + + public function getDoubleClick(string $page): bool + { + return $this->doubleClick[$page] ?? false; + } + + /* -- */ + public function setI18n(array $locales, string $defaultLocale): self { $this->locales = $locales; diff --git a/src/core/Crud/Field/Field.php b/src/core/Crud/Field/Field.php index 42256e8..ea8a102 100644 --- a/src/core/Crud/Field/Field.php +++ b/src/core/Crud/Field/Field.php @@ -29,18 +29,22 @@ abstract class Field 'property' => null, 'property_builder' => null, 'view' => null, + 'action' => null, 'raw' => false, 'sort' => null, 'href' => null, 'href_attr' => [], 'attr' => [], + 'inline_form' => null, ]); $resolver->setRequired('view'); $resolver->setAllowedTypes('property', ['null', 'string']); $resolver->setAllowedTypes('view', 'string'); + $resolver->setAllowedTypes('action', ['null', 'string']); $resolver->setAllowedTypes('attr', 'array'); $resolver->setAllowedTypes('href', ['null', 'string', 'callable']); + $resolver->setAllowedTypes('inline_form', ['null', 'callable']); $resolver->setAllowedTypes('href_attr', 'array', 'callable'); $resolver->setAllowedTypes('raw', 'boolean'); $resolver->setAllowedTypes('property_builder', ['null', 'callable']); diff --git a/src/core/Resources/maker/controller/CrudController.tpl.php b/src/core/Resources/maker/controller/CrudController.tpl.php index d92cee0..6cb4c49 100644 --- a/src/core/Resources/maker/controller/CrudController.tpl.php +++ b/src/core/Resources/maker/controller/CrudController.tpl.php @@ -50,6 +50,12 @@ class extends CrudController return $this->doEdit($entity, $entityManager, $request); } + #[Route(path: "/admin//inline_edit/{entity}/{context}/{label}", name: 'admin__inline_edit', methods: ['GET', 'POST'])] + public function inlineEdit(string $context, string $label, Entity $entity, EntityManager $entityManager, Request $request): Response + { + return $this->doInlineEdit($context, $label, $entity, $entityManager, $request); + } + #[Route(path: "/admin//sort/{page}", name: "admin__sort", methods: ['POST'], requirements: ['page' => '\d+'])] public function sort(RepositoryQuery $query, EntityManager $entityManager, Request $request, Session $session, int $page = 1): Response { @@ -79,6 +85,7 @@ class extends CrudController ->setPageRoute('index', 'admin__index') ->setPageRoute('new', 'admin__new') ->setPageRoute('edit', 'admin__edit') + ->setPageRoute('inline_edit', 'admin__inline_edit') ->setPageRoute('show', 'admin__show') ->setPageRoute('sort', 'admin__sort') ->setPageRoute('batch', 'admin__batch') @@ -98,6 +105,7 @@ class extends CrudController // ->setAction('index', 'show', true) // ->setAction('index', 'edit', true) // ->setAction('index', 'delete', true) + // ->setDoubleClick('index', false) // ->setAction('edit', 'back', true) // ->setAction('edit', 'show', true) diff --git a/src/core/Resources/views/admin/crud/index.html.twig b/src/core/Resources/views/admin/crud/index.html.twig index 352adcd..d989ee3 100644 --- a/src/core/Resources/views/admin/crud/index.html.twig +++ b/src/core/Resources/views/admin/crud/index.html.twig @@ -185,25 +185,36 @@ {% endif %} {% endset -%} - + {% if configuration.hasBatchAction(context) %} {% endif %} - {% for config in configuration.fields(context) %} - {% set attr = config.options.attr is defined ? config.options.attr : [] %} - {% set action = config.options.action is defined ? config.options.action : null %} + {% for label, config in configuration.fields(context) %} + {% set attr = config.options.attr ?? [] %} + {% set action = config.options.action ?? null %} + {% set inlineForm = config.options.inline_form ?? null %} - {% if action == 'show' %} - + {% if inlineForm and configuration.action(context, 'edit', true, [item]) %} + + {{ render_field(item, config, configuration.defaultlocale) }} + + {% elseif action == 'show' and configuration.action(context, 'show', true, [item]) %} + {{ render_field(item, config, configuration.defaultLocale) }} - {% elseif action == 'edit' %} - - {{ render_field(item, config, configuration.defaultLocale) }} + {% elseif action == 'edit' and configuration.action(context, 'edit', true, [item]) %} + + {{ render_field(item, config, configuration.defaultlocale) }} {% else %} {{ render_field(item, config, configuration.defaultLocale) }} diff --git a/src/core/Resources/views/admin/crud/inline_edit.html.twig b/src/core/Resources/views/admin/crud/inline_edit.html.twig new file mode 100644 index 0000000..44d49ba --- /dev/null +++ b/src/core/Resources/views/admin/crud/inline_edit.html.twig @@ -0,0 +1,20 @@ + +