-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from patrick477/master
Do not rely on admin's locale for product autocomplete
- Loading branch information
Showing
10 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |