Skip to content

Commit

Permalink
[shopsys] moved category parameters to framework package (#3336)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Staňo <milan.stano@shopsys.com>
  • Loading branch information
stanoMilan and msshopsys authored Sep 2, 2024
1 parent c786172 commit af697df
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Form/Admin/Category/CategoryFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
use Shopsys\FrameworkBundle\Form\GroupType;
use Shopsys\FrameworkBundle\Form\ImageUploadType;
use Shopsys\FrameworkBundle\Form\Locale\LocalizedType;
use Shopsys\FrameworkBundle\Form\SortableValuesType;
use Shopsys\FrameworkBundle\Form\UrlListType;
use Shopsys\FrameworkBundle\Model\Category\Category;
use Shopsys\FrameworkBundle\Model\Category\CategoryData;
use Shopsys\FrameworkBundle\Model\Category\CategoryFacade;
use Shopsys\FrameworkBundle\Model\Localization\Localization;
use Shopsys\FrameworkBundle\Model\Product\Parameter\ParameterRepository;
use Shopsys\FrameworkBundle\Model\Seo\SeoSettingFacade;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand All @@ -41,13 +43,15 @@ class CategoryFormType extends AbstractType
* @param \Shopsys\FrameworkBundle\Model\Seo\SeoSettingFacade $seoSettingFacade
* @param \Shopsys\FrameworkBundle\Component\Plugin\PluginCrudExtensionFacade $pluginCrudExtensionFacade
* @param \Shopsys\FrameworkBundle\Model\Localization\Localization $localization
* @param \Shopsys\FrameworkBundle\Model\Product\Parameter\ParameterRepository $parameterRepository
*/
public function __construct(
private readonly CategoryFacade $categoryFacade,
private readonly Domain $domain,
private readonly SeoSettingFacade $seoSettingFacade,
private readonly PluginCrudExtensionFacade $pluginCrudExtensionFacade,
private readonly Localization $localization,
private readonly ParameterRepository $parameterRepository,
) {
}

Expand Down Expand Up @@ -229,6 +233,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('save', SubmitType::class);

$this->pluginCrudExtensionFacade->extendForm($builder, 'category', 'pluginData');

$this->buildFilterParameters($builder, $options['category']);
}

/**
Expand Down Expand Up @@ -257,4 +263,42 @@ private function getCategoryNameForPlaceholder(DomainConfig $domainConfig, ?Cate

return $category === null ? '' : $category->getName($domainLocale);
}

/**
* @param \Symfony\Component\Form\FormBuilderInterface $builder
* @param \Shopsys\FrameworkBundle\Model\Category\Category|null $category
*/
protected function buildFilterParameters(
FormBuilderInterface $builder,
?Category $category,
): void {
if ($category === null) {
return;
}
$parametersFilterBuilder = $builder->create('parametersGroup', GroupType::class, ['label' => t('Filter parameters')]);

$parameterNamesById = [];

$parametersUsedByProductsInCategory = $this->parameterRepository->getParametersUsedByProductsInCategory($category, $this->domain->getDomainConfigById(Domain::FIRST_DOMAIN_ID));

foreach ($parametersUsedByProductsInCategory as $parameter) {
$parameterNamesById[$parameter->getId()] = $parameter->getName();
}

$parametersFilterBuilder->add('parametersPosition', SortableValuesType::class, [
'labels_by_value' => $parameterNamesById,
'label' => t('Parameters order in category'),
'required' => false,
]);

$parametersFilterBuilder->add('parametersCollapsed', ChoiceType::class, [
'required' => false,
'label' => t('Filter parameters closed by default'),
'choices' => $parametersUsedByProductsInCategory,
'expanded' => true,
'choice_label' => 'name',
'choice_value' => 'id',
'multiple' => true,
]);
}
}
12 changes: 12 additions & 0 deletions src/Model/Category/CategoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class CategoryData
*/
public $uuid;

/**
* @var int[]|null[]
*/
public $parametersPosition;

/**
* @var \Shopsys\FrameworkBundle\Model\Product\Parameter\Parameter[]
*/
public $parametersCollapsed;

public function __construct()
{
$this->name = [];
Expand All @@ -73,5 +83,7 @@ public function __construct()
$this->enabled = [];
$this->urls = new UrlListData();
$this->pluginData = [];
$this->parametersPosition = [];
$this->parametersCollapsed = [];
}
}
22 changes: 22 additions & 0 deletions src/Model/Category/CategoryDataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ class CategoryDataFactory implements CategoryDataFactoryInterface
* @param \Shopsys\FrameworkBundle\Component\Plugin\PluginCrudExtensionFacade $pluginCrudExtensionFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Component\FileUpload\ImageUploadDataFactory $imageUploadDataFactory
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryParameterRepository $categoryParameterRepository
*/
public function __construct(
protected readonly FriendlyUrlFacade $friendlyUrlFacade,
protected readonly PluginCrudExtensionFacade $pluginCrudExtensionFacade,
protected readonly Domain $domain,
protected readonly ImageUploadDataFactory $imageUploadDataFactory,
protected readonly CategoryParameterRepository $categoryParameterRepository,
) {
}

Expand Down Expand Up @@ -101,7 +103,27 @@ protected function fillFromCategory(CategoryData $categoryData, Category $catego
$categoryData->urls->mainFriendlyUrlsByDomainId[$domainId] = $mainFriendlyUrl;
}

$parameters = $this->categoryParameterRepository->getParametersCollapsedByCategory($category);
$categoryData->parametersCollapsed = $parameters;
$categoryData->parametersPosition = $this->getParametersSortedByPositionFilteredByCategory($category);

$categoryData->pluginData = $this->pluginCrudExtensionFacade->getAllData('category', $category->getId());
$categoryData->image = $this->imageUploadDataFactory->createFromEntityAndType($category);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
* @return int[]
*/
protected function getParametersSortedByPositionFilteredByCategory(Category $category): array
{
$parameterIdsSortedByPosition = [];
$categoryParameters = $this->categoryParameterRepository->getCategoryParametersByCategorySortedByPosition($category);

foreach ($categoryParameters as $categoryParameter) {
$parameterIdsSortedByPosition[] = $categoryParameter->getParameter()->getId();
}

return $parameterIdsSortedByPosition;
}
}
6 changes: 6 additions & 0 deletions src/Model/Category/CategoryFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CategoryFacade
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryFactoryInterface $categoryFactory
* @param \Shopsys\FrameworkBundle\Model\Product\Recalculation\ProductRecalculationDispatcher $productRecalculationDispatcher
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Shopsys\FrameworkBundle\Model\Category\CategoryParameterFacade $categoryParameterFacade
*/
public function __construct(
protected readonly EntityManagerInterface $em,
Expand All @@ -49,6 +50,7 @@ public function __construct(
protected readonly CategoryFactoryInterface $categoryFactory,
protected readonly ProductRecalculationDispatcher $productRecalculationDispatcher,
protected readonly EventDispatcherInterface $eventDispatcher,
protected readonly CategoryParameterFacade $categoryParameterFacade,
) {
}

Expand Down Expand Up @@ -110,6 +112,8 @@ public function create(CategoryData $categoryData)
$this->friendlyUrlFacade->createFriendlyUrls('front_product_list', $category->getId(), $category->getNames());
$this->imageFacade->manageImages($category, $categoryData->image);

$this->categoryParameterFacade->saveRelation($category, $categoryData->parametersPosition, $categoryData->parametersCollapsed);

$this->pluginCrudExtensionFacade->saveAllData('category', $category->getId(), $categoryData->pluginData);

$this->categoryVisibilityRecalculationScheduler->scheduleRecalculation();
Expand Down Expand Up @@ -141,6 +145,8 @@ public function edit($categoryId, CategoryData $categoryData)

$this->imageFacade->manageImages($category, $categoryData->image);

$this->categoryParameterFacade->saveRelation($category, $categoryData->parametersPosition, $categoryData->parametersCollapsed);

$this->pluginCrudExtensionFacade->saveAllData('category', $category->getId(), $categoryData->pluginData);

$this->categoryVisibilityRecalculationScheduler->scheduleRecalculation();
Expand Down
40 changes: 40 additions & 0 deletions src/Model/Product/Parameter/ParameterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Shopsys\FrameworkBundle\Component\Doctrine\OrderByCollationHelper;
use Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig;
use Shopsys\FrameworkBundle\Model\Category\Category;
use Shopsys\FrameworkBundle\Model\Product\Parameter\Exception\ParameterNotFoundException;
use Shopsys\FrameworkBundle\Model\Product\Parameter\Exception\ParameterValueNotFoundException;
use Shopsys\FrameworkBundle\Model\Product\Product;
use Shopsys\FrameworkBundle\Model\Product\ProductCategoryDomain;

class ParameterRepository
{
Expand Down Expand Up @@ -79,6 +83,42 @@ public function getById($parameterId)
return $parameter;
}

/**
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
* @param \Shopsys\FrameworkBundle\Component\Domain\Config\DomainConfig $domainConfig
* @return \Shopsys\FrameworkBundle\Model\Product\Parameter\Parameter[]
*/
public function getParametersUsedByProductsInCategory(Category $category, DomainConfig $domainConfig): array
{
$queryBuilder = $this->getParameterRepository()->createQueryBuilder('p')
->select('p')
->join(ProductParameterValue::class, 'ppv', Join::WITH, 'p = ppv.parameter')
->join('p.translations', 'pt', Join::WITH, 'pt.locale = :locale')
->setParameter('locale', $domainConfig->getLocale())
->orderBy(OrderByCollationHelper::createOrderByForLocale('pt.name', $domainConfig->getLocale()))
->groupBy('p, pt');

$this->applyCategorySeoConditions($queryBuilder, $category, $domainConfig->getId());

return $queryBuilder->getQuery()->execute();
}

/**
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
* @param \Shopsys\FrameworkBundle\Model\Category\Category $category
* @param int $domainId
*/
protected function applyCategorySeoConditions(QueryBuilder $queryBuilder, Category $category, int $domainId): void
{
$queryBuilder
->join(Product::class, 'product', Join::WITH, 'ppv.product = product')
->join(ProductCategoryDomain::class, 'pcd', Join::WITH, 'product = pcd.product')
->andWhere('pcd.category = :category')
->andWhere('pcd.domainId = :domainId')
->setParameter('category', $category)
->setParameter('domainId', $domainId);
}

/**
* @param string $uuid
* @return \Shopsys\FrameworkBundle\Model\Product\Parameter\Parameter
Expand Down
9 changes: 9 additions & 0 deletions src/Resources/translations/messages.cs.po
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,12 @@ msgstr "Pro vytvoření produktu doplňte chybějící nastavení. Více informa
msgid "Filter by"
msgstr "Filtrovat podle"

msgid "Filter parameters"
msgstr "Parametry filtru"

msgid "Filter parameters closed by default"
msgstr "Defaultně zavřené parametry ve filtru"

msgid "Finish the assigning"
msgstr "Dokončit přiřazování"

Expand Down Expand Up @@ -2005,6 +2011,9 @@ msgstr "Parametr <strong>{{ name }}</strong> byl smazán"
msgid "Parameters"
msgstr "Parametry"

msgid "Parameters order in category"
msgstr "Pořadí parametrů v kategorii"

msgid "Parameters overview"
msgstr "Přehled parametrů"

Expand Down
9 changes: 9 additions & 0 deletions src/Resources/translations/messages.en.po
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,12 @@ msgstr ""
msgid "Filter by"
msgstr ""

msgid "Filter parameters"
msgstr ""

msgid "Filter parameters closed by default"
msgstr ""

msgid "Finish the assigning"
msgstr ""

Expand Down Expand Up @@ -2005,6 +2011,9 @@ msgstr ""
msgid "Parameters"
msgstr ""

msgid "Parameters order in category"
msgstr ""

msgid "Parameters overview"
msgstr ""

Expand Down

0 comments on commit af697df

Please sign in to comment.