From 173a23f4c895f845c02c9572f1ae80377e04a347 Mon Sep 17 00:00:00 2001 From: Patryk Date: Mon, 19 Mar 2018 13:00:17 +0100 Subject: [PATCH] Do not rely on admin's locale for product autocomplete --- .../Action/Admin/ProductSearchAction.php | 60 +++++++++++++++++++ src/Repository/ProductRepository.php | 54 +++++++++++++++++ src/Repository/ProductRepositoryInterface.php | 27 +++++++++ src/Repository/SectionRepository.php | 1 + src/Resources/config/routing/admin.yml | 3 + .../config/routing/admin/product.yml | 6 ++ src/Resources/config/services.yml | 1 + src/Resources/config/services/controller.yml | 6 ++ src/Resources/config/services/repository.yml | 5 ++ src/Resources/views/Form/theme.html.twig | 6 +- 10 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/Controller/Action/Admin/ProductSearchAction.php create mode 100755 src/Repository/ProductRepository.php create mode 100755 src/Repository/ProductRepositoryInterface.php create mode 100644 src/Resources/config/routing/admin/product.yml create mode 100644 src/Resources/config/services/repository.yml diff --git a/src/Controller/Action/Admin/ProductSearchAction.php b/src/Controller/Action/Admin/ProductSearchAction.php new file mode 100644 index 000000000..252643876 --- /dev/null +++ b/src/Controller/Action/Admin/ProductSearchAction.php @@ -0,0 +1,60 @@ +productRepository = $productRepository; + $this->viewHandler = $viewHandler; + } + + /** + * @param Request $request + * + * @return Response + */ + public function __invoke(Request $request): Response + { + $resource = $this->productRepository->findByNamePart($request->get('phrase', '')); + + $view = View::create($resource); + + $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']); + + $view->getContext()->enableMaxDepth(); + + return $this->viewHandler->handle($view); + } +} diff --git a/src/Repository/ProductRepository.php b/src/Repository/ProductRepository.php new file mode 100755 index 000000000..e22aa71ad --- /dev/null +++ b/src/Repository/ProductRepository.php @@ -0,0 +1,54 @@ +createTranslationBasedQueryBuilder($locale) + ->andWhere('translation.name LIKE :name') + ->setParameter('name', '%' . $phrase . '%') + ->getQuery() + ->getResult() + ; + } + + /** + * @param $locale + * + * @return QueryBuilder + */ + private function createTranslationBasedQueryBuilder($locale): QueryBuilder + { + $queryBuilder = $this->createQueryBuilder('o') + ->addSelect('translation') + ->leftJoin('o.translations', 'translation') + ; + + if (null !== $locale) { + $queryBuilder + ->andWhere('translation.locale = :locale') + ->setParameter('locale', $locale) + ; + } + + return $queryBuilder; + } +} diff --git a/src/Repository/ProductRepositoryInterface.php b/src/Repository/ProductRepositoryInterface.php new file mode 100755 index 000000000..493ee04ee --- /dev/null +++ b/src/Repository/ProductRepositoryInterface.php @@ -0,0 +1,27 @@ +createQueryBuilder('o') + ->addSelect('translation') ->leftJoin('o.translations', 'translation') ; diff --git a/src/Resources/config/routing/admin.yml b/src/Resources/config/routing/admin.yml index baa5b48c5..5550ebfc9 100755 --- a/src/Resources/config/routing/admin.yml +++ b/src/Resources/config/routing/admin.yml @@ -10,6 +10,9 @@ bitbag_sylius_cms_plugin_admin_frequently_asked_question: bitbag_sylius_cms_plugin_admin_section: resource: "@BitBagSyliusCmsPlugin/Resources/config/routing/admin/section.yml" +bitbag_sylius_cms_plugin_admin_product: + resource: "@BitBagSyliusCmsPlugin/Resources/config/routing/admin/product.yml" + bitbag_sylius_cms_plugin_admin_ajax_generate_page_slug: path: /page/generate-slug methods: [GET] diff --git a/src/Resources/config/routing/admin/product.yml b/src/Resources/config/routing/admin/product.yml new file mode 100644 index 000000000..ef296f342 --- /dev/null +++ b/src/Resources/config/routing/admin/product.yml @@ -0,0 +1,6 @@ +bitbag_sylius_cms_plugin_admin_ajax_product_by_name_phrase: + path: /ajax/products/search-by-name + methods: [GET] + defaults: + _format: json + _controller: bitbag_sylius_cms_plugin.controller.action.admin.product_search diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 368f3b4cb..d35b6ad9d 100755 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -5,6 +5,7 @@ imports: - { resource: "@BitBagSyliusCmsPlugin/Resources/config/services/event_listener.yml" } - { resource: "@BitBagSyliusCmsPlugin/Resources/config/services/controller.yml" } - { resource: "@BitBagSyliusCmsPlugin/Resources/config/services/fixture.yml" } + - { resource: "@BitBagSyliusCmsPlugin/Resources/config/services/repository.yml" } services: bitbag_sylius_cms_plugin.resolver.block_resource: diff --git a/src/Resources/config/services/controller.yml b/src/Resources/config/services/controller.yml index 2f581172b..a5cba670b 100755 --- a/src/Resources/config/services/controller.yml +++ b/src/Resources/config/services/controller.yml @@ -3,3 +3,9 @@ services: class: BitBag\SyliusCmsPlugin\Controller\PageSlugController arguments: - "@sylius.generator.slug" + + bitbag_sylius_cms_plugin.controller.action.admin.product_search: + class: BitBag\SyliusCmsPlugin\Controller\Action\Admin\ProductSearchAction + arguments: + - "@bitbag_sylius_cms_plugin.repository.product" + - "@fos_rest.view_handler.default" diff --git a/src/Resources/config/services/repository.yml b/src/Resources/config/services/repository.yml new file mode 100644 index 000000000..248a55e66 --- /dev/null +++ b/src/Resources/config/services/repository.yml @@ -0,0 +1,5 @@ +services: + bitbag_sylius_cms_plugin.repository.product: + class: BitBag\SyliusCmsPlugin\Repository\ProductRepository + parent: sylius.repository.product + diff --git a/src/Resources/views/Form/theme.html.twig b/src/Resources/views/Form/theme.html.twig index 6b921ae7a..8d551daa0 100755 --- a/src/Resources/views/Form/theme.html.twig +++ b/src/Resources/views/Form/theme.html.twig @@ -1,5 +1,9 @@ -{% extends '@SyliusAdmin/Form/theme.html.twig' %} +{% extends '@SyliusUi/Form/theme.html.twig' %} {% block bitbag_section_autocomplete_choice_row %} {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_section_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_section_by_code')}) }} {% endblock %} + +{% block sylius_product_autocomplete_choice_row %} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_product_by_name_phrase'), 'load_edit_url': path('sylius_admin_ajax_product_by_code')}) }} +{% endblock %} \ No newline at end of file