Skip to content

Commit

Permalink
Merge pull request #124 from patrick477/master
Browse files Browse the repository at this point in the history
Do not rely on admin's locale for product autocomplete
  • Loading branch information
bitbager authored Mar 19, 2018
2 parents 9437bff + 173a23f commit 4a8ffb1
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/Controller/Action/Admin/ProductSearchAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin;

use BitBag\SyliusCmsPlugin\Repository\ProductRepositoryInterface;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class ProductSearchAction
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var ViewHandler
*/
private $viewHandler;

/**
* @param ProductRepositoryInterface $productRepository
* @param ViewHandler $viewHandler
*/
public function __construct(ProductRepositoryInterface $productRepository, ViewHandler $viewHandler)
{
$this->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);
}
}
54 changes: 54 additions & 0 deletions src/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Repository;

use Doctrine\ORM\QueryBuilder;
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;

class ProductRepository extends BaseProductRepository implements ProductRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function findByNamePart(string $phrase, ?string $locale = null): array
{
return $this->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;
}
}
27 changes: 27 additions & 0 deletions src/Repository/ProductRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Repository;

use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Repository\ProductRepositoryInterface as BaseProductRepositoryInterface;

interface ProductRepositoryInterface extends BaseProductRepositoryInterface
{
/**
* @param string $phrase
* @param null|string $locale
*
* @return array|ProductInterface[]
*/
public function findByNamePart(string $phrase, ?string $locale = null): array;
}
1 change: 1 addition & 0 deletions src/Repository/SectionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function findByNamePart(string $phrase, ?string $locale = null): array
private function createTranslationBasedQueryBuilder($locale): QueryBuilder
{
$queryBuilder = $this->createQueryBuilder('o')
->addSelect('translation')
->leftJoin('o.translations', 'translation')
;

Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/routing/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/routing/admin/product.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/services/controller.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
5 changes: 5 additions & 0 deletions src/Resources/config/services/repository.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
bitbag_sylius_cms_plugin.repository.product:
class: BitBag\SyliusCmsPlugin\Repository\ProductRepository
parent: sylius.repository.product

6 changes: 5 additions & 1 deletion src/Resources/views/Form/theme.html.twig
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit 4a8ffb1

Please sign in to comment.