Skip to content

Commit b214c7e

Browse files
category automated filters (#3672)
2 parents 88b3ac4 + 2ab2d7b commit b214c7e

32 files changed

+451
-131
lines changed

src/DependencyInjection/ShopsysFrameworkExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Shopsys\FrameworkBundle\Component\Grid\InlineEdit\GridInlineEditInterface;
1111
use Shopsys\FrameworkBundle\Component\HttpFoundation\TransactionalMasterRequestConditionProviderInterface;
1212
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;
13+
use Shopsys\FrameworkBundle\Model\Category\AutomatedFilter\CategoryAutomatedFilterInterface;
1314
use Shopsys\FrameworkBundle\Model\Mail\MailTemplateSender\MailTemplateSenderInterface;
1415
use Shopsys\FrameworkBundle\Model\Order\Processing\OrderProcessingStack;
1516
use Shopsys\FrameworkBundle\Twig\NoVarDumperExtension;
@@ -73,6 +74,9 @@ public function load(array $configs, ContainerBuilder $container): void
7374

7475
$container->registerForAutoconfiguration(MailTemplateSenderInterface::class)
7576
->addTag('shopsys.mail_template_sender');
77+
78+
$container->registerForAutoconfiguration(CategoryAutomatedFilterInterface::class)
79+
->addTag('shopsys.category_automated_filter');
7680
}
7781

7882
/**

src/Form/Admin/Advert/AdvertFormType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
8585
t('HTML code') => Advert::TYPE_CODE,
8686
t('Image with link') => Advert::TYPE_IMAGE,
8787
],
88+
'choice_translation_domain' => false,
8889
'expanded' => true,
8990
'multiple' => false,
9091
'constraints' => [

src/Form/Admin/Article/ArticleFormType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
106106
t('Site') => Article::TYPE_SITE,
107107
t('Link') => Article::TYPE_LINK,
108108
],
109+
'choice_translation_domain' => false,
109110
'expanded' => true,
110111
'multiple' => false,
111112
'label' => t('Type'),

src/Form/Admin/Category/CategoryFormType.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Shopsys\FrameworkBundle\Form\Locale\LocalizedType;
1818
use Shopsys\FrameworkBundle\Form\SortableValuesType;
1919
use Shopsys\FrameworkBundle\Form\UrlListType;
20+
use Shopsys\FrameworkBundle\Model\Category\AutomatedFilter\CategoryAutomatedFilterFacade;
2021
use Shopsys\FrameworkBundle\Model\Category\Category;
2122
use Shopsys\FrameworkBundle\Model\Category\CategoryData;
2223
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
@@ -44,6 +45,7 @@ class CategoryFormType extends AbstractType
4445
* @param \Shopsys\FrameworkBundle\Component\Plugin\PluginCrudExtensionFacade $pluginCrudExtensionFacade
4546
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
4647
* @param \Shopsys\FrameworkBundle\Model\Product\Parameter\ParameterRepository $parameterRepository
48+
* @param \Shopsys\FrameworkBundle\Model\Category\AutomatedFilter\CategoryAutomatedFilterFacade $categoryAutomatedFilterFacade
4749
*/
4850
public function __construct(
4951
private readonly CategoryFacade $categoryFacade,
@@ -52,6 +54,7 @@ public function __construct(
5254
private readonly PluginCrudExtensionFacade $pluginCrudExtensionFacade,
5355
private readonly Localization $localization,
5456
private readonly ParameterRepository $parameterRepository,
57+
private readonly CategoryAutomatedFilterFacade $categoryAutomatedFilterFacade,
5558
) {
5659
}
5760

@@ -110,6 +113,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
110113
]);
111114
}
112115

116+
$categoryAutomatedFiltersNotesIndexedByValue = $this->categoryAutomatedFilterFacade->getNotesIndexedByValue();
117+
113118
$builderSettingsGroup
114119
->add('name', LocalizedType::class, [
115120
'main_constraints' => [
@@ -139,6 +144,28 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
139144
->add('enabled', DomainsType::class, [
140145
'required' => false,
141146
'label' => t('Display on'),
147+
])
148+
->add('automatedFilters', ChoiceType::class, [
149+
'label' => 'Automated filters',
150+
'required' => false,
151+
'multiple' => true,
152+
'expanded' => true,
153+
'choice_translation_domain' => false,
154+
'choices' => $this->categoryAutomatedFilterFacade->getAllValuesIndexedByLabel(),
155+
'choice_attr' => function ($choice, $key, $value) use ($categoryAutomatedFiltersNotesIndexedByValue) {
156+
$iconTitle = $categoryAutomatedFiltersNotesIndexedByValue[$value] ?? null;
157+
158+
if ($iconTitle === null) {
159+
return [];
160+
}
161+
162+
return [
163+
'icon' => true,
164+
'iconTitle' => $iconTitle,
165+
'iconPlacement' => 'right',
166+
'iconClass' => 'margin-left-10',
167+
];
168+
},
142169
]);
143170

144171
$builderSeoGroup = $builder->create('seo', GroupType::class, [

src/Form/Admin/Customer/RoleGroup/CustomerUserRoleGroupFormType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
4747
'multiple' => true,
4848
'expanded' => true,
4949
'choices' => $this->customerUserRole->getAvailableRoles(),
50+
'choice_translation_domain' => false,
5051
]);
5152

5253
$builder->add('save', SubmitType::class);

src/Form/Admin/PromoCode/PromoCodeFlagType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
4848
t('Products with this flag') => PromoCodeFlag::TYPE_INCLUSIVE,
4949
t('Products without this flag') => PromoCodeFlag::TYPE_EXCLUSIVE,
5050
],
51+
'choice_translation_domain' => false,
5152
'expanded' => true,
5253
'multiple' => false,
5354
'constraints' => [

src/Form/Admin/PromoCode/PromoCodeFormType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private function buildBaseGroup(FormBuilderInterface $builder): void
113113
'expanded' => true,
114114
'multiple' => false,
115115
'choices' => $this->promoCodeTypeEnum->getAllIndexedByTranslations(),
116+
'choice_translation_domain' => false,
116117
'label' => t('Discount type'),
117118
'attr' => [
118119
'class' => 'js-promo-code-discount-type',

src/Form/RolesType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function configureOptions(OptionsResolver $resolver): void
4141
'expanded' => true,
4242
'label' => false,
4343
'choices' => $this->rolesChoices,
44+
'choice_translation_domain' => false,
4445
]);
4546
}
4647

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
9+
10+
class Version20241216165539 extends AbstractMigration
11+
{
12+
/**
13+
* @param \Doctrine\DBAL\Schema\Schema $schema
14+
*/
15+
public function up(Schema $schema): void
16+
{
17+
$this->sql('ALTER TABLE categories ADD automated_filters JSON NOT NULL DEFAULT \'{}\'');
18+
$this->sql('ALTER TABLE categories ALTER automated_filters DROP DEFAULT');
19+
}
20+
21+
/**
22+
* @param \Doctrine\DBAL\Schema\Schema $schema
23+
*/
24+
public function down(Schema $schema): void
25+
{
26+
}
27+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopsys\FrameworkBundle\Model\Category\AutomatedFilter;
6+
7+
use Shopsys\FrameworkBundle\Model\Category\Category;
8+
use Shopsys\FrameworkBundle\Model\Product\Search\FilterQuery;
9+
use Traversable;
10+
11+
class CategoryAutomatedFilterFacade
12+
{
13+
/**
14+
* @param \Traversable<int, \Shopsys\FrameworkBundle\Model\Category\AutomatedFilter\CategoryAutomatedFilterInterface> $categoryAutomatedFilters
15+
*/
16+
public function __construct(
17+
protected readonly Traversable $categoryAutomatedFilters,
18+
) {
19+
}
20+
21+
/**
22+
* @return array<string, string>
23+
*/
24+
public function getAllValuesIndexedByLabel(): array
25+
{
26+
$valuesIndexedByLabel = [];
27+
28+
foreach ($this->categoryAutomatedFilters as $categoryAutomatedFilter) {
29+
$valuesIndexedByLabel[$categoryAutomatedFilter->getLabel()] = $categoryAutomatedFilter->getDatabaseValue();
30+
}
31+
32+
return $valuesIndexedByLabel;
33+
}
34+
35+
/**
36+
* @return array<string, string>
37+
*/
38+
public function getNotesIndexedByValue(): array
39+
{
40+
$notesIndexedByValue = [];
41+
42+
foreach ($this->categoryAutomatedFilters as $categoryAutomatedFilter) {
43+
$notesIndexedByValue[$categoryAutomatedFilter->getDatabaseValue()] = $categoryAutomatedFilter->getNote();
44+
}
45+
46+
return $notesIndexedByValue;
47+
}
48+
49+
/**
50+
* @param \Shopsys\FrameworkBundle\Model\Product\Search\FilterQuery $filterQuery
51+
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
52+
* @return \Shopsys\FrameworkBundle\Model\Product\Search\FilterQuery
53+
*/
54+
public function applyFiltersByCategory(FilterQuery $filterQuery, Category $category): FilterQuery
55+
{
56+
$filterQuery = $filterQuery->filterByCategory($category->getId());
57+
58+
foreach ($this->getByCategory($category) as $automatedFilter) {
59+
$filterQuery = $automatedFilter->applyFilter($filterQuery);
60+
}
61+
62+
return $filterQuery;
63+
}
64+
65+
/**
66+
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
67+
* @return \Shopsys\FrameworkBundle\Model\Category\AutomatedFilter\CategoryAutomatedFilterInterface[]
68+
*/
69+
protected function getByCategory(Category $category): array
70+
{
71+
$categoryAutomatedFilters = [];
72+
73+
foreach ($this->categoryAutomatedFilters as $categoryAutomatedFilter) {
74+
if ($category->isUsingAutomatedFilter($categoryAutomatedFilter)) {
75+
$categoryAutomatedFilters[] = $categoryAutomatedFilter;
76+
}
77+
}
78+
79+
return $categoryAutomatedFilters;
80+
}
81+
}

0 commit comments

Comments
 (0)