-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 additions
and
23 deletions.
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,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Odiseo\SyliusReportPlugin\Controller\Action; | ||
|
||
use FOS\RestBundle\View\View; | ||
use FOS\RestBundle\View\ViewHandler; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
final class ProductSearchAction | ||
{ | ||
/** @var ProductRepositoryInterface */ | ||
private $productRepository; | ||
|
||
/** @var LocaleContextInterface */ | ||
private $localeContext; | ||
|
||
/** @var ViewHandlerInterface */ | ||
private $viewHandler; | ||
|
||
public function __construct( | ||
ProductRepositoryInterface $productRepository, | ||
LocaleContextInterface $localeContext, | ||
ViewHandler $viewHandler | ||
) | ||
{ | ||
$this->productRepository = $productRepository; | ||
$this->localeContext = $localeContext; | ||
$this->viewHandler = $viewHandler; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
$locale = $this->localeContext->getLocaleCode(); | ||
|
||
$products = $this->getProducts($request->get('name', ''), $locale); | ||
$view = View::create($products); | ||
|
||
$this->viewHandler->setExclusionStrategyGroups(['Autocomplete']); | ||
$view->getContext()->enableMaxDepth(); | ||
|
||
return $this->viewHandler->handle($view); | ||
} | ||
|
||
private function getProducts($query, $locale): array | ||
{ | ||
$products = []; | ||
$searchProducts = $this->productRepository->findByNamePart($query, $locale); | ||
|
||
/** @var ProductInterface $product */ | ||
foreach ($searchProducts as $product) { | ||
$productLabel = ucfirst(strtolower($product->getName())); | ||
$isNew = count(array_filter($products, function ($product) use ($productLabel) { | ||
return $product['name'] === $productLabel; | ||
})) === 0; | ||
|
||
if ($isNew) { | ||
$products[] = [ | ||
'name' => $productLabel, | ||
'id' => $product->getId(), | ||
]; | ||
} | ||
} | ||
|
||
return $products; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Odiseo\SyliusReportPlugin\Form\Type; | ||
|
||
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class ProductAutocompleteChoiceType extends AbstractType | ||
{ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->setRequired([ | ||
'remote_url', | ||
]) | ||
->setDefaults([ | ||
'resource' => 'sylius.product', | ||
'choice_value' => 'id', | ||
]) | ||
; | ||
} | ||
|
||
public function buildView(FormView $view, FormInterface $form, array $options): void | ||
{ | ||
$view->vars['remote_criteria_type'] = 'contains'; | ||
$view->vars['remote_criteria_name'] = $options['choice_name']; | ||
$view->vars['remote_url'] = $view->vars['load_edit_url'] = $options['remote_url']; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'odiseo_product_autocomplete_choice'; | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return ResourceAutocompleteChoiceType::class; | ||
} | ||
} |
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