<?php
namespace App\Filtration\Trait;
use App\Entity\Product;
use App\Entity\ProductCategory;
use App\Entity\ProductFamily;
use App\Enum\FilterEnum;
use App\Repository\ProductVariantRepository;
use Doctrine\ORM\QueryBuilder;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Trait FilterTrait
*
* This trait expects the following properties to be defined in the using class:
* @property QueryBuilder $qb
* @property string $webspace
* @property string $locale
* @property array|null $types
* @property array|null $fixations
* @property int|null $minLuminousFlux
* @property int|null $maxLuminousFlux
* @property array|null $cris
* @property array|null $chromaticities
* @property array|null $dispersionVariants
* @property array|null $ips
* @property array|null $radiationDirects
* @property array|null $shapes
* @property int|null $minLength
* @property int|null $maxLength
* @property int|null $minWidth
* @property int|null $maxWidth
* @property int|null $minHeight
* @property int|null $maxHeight
* @property array|null $colors
* @property array|null $usages
* @property array|null $maxTemperatures
* @property array|null $minTemperatures
* @property int|null $minInputPower
* @property int|null $maxInputPower
* @property int|null $minEfficiency
* @property int|null $maxEfficiency
* @property array|null $elProtects
* @property array|null $powerMethods
* @property array|null $resourceTypes
// * @property array|null $lightSources
* @property array|null $statuses
* @property array|null $mechanicalDurabilities
* @property array|null $dispersions
* @property array|null $drivers
* @property array|null $accessories
* @property ProductVariantRepository $productVariantRepository
* @property TranslatorInterface $translator
*/
trait FilterTrait
{
use FiltrationCriteriaTrait;
private function renderMultiSelect(string $key, string $label, array $options = []): array
{
return [
'checkbox_select' => [
'id' => $key,
'label' => $label,
'title' => 'Vybrat',
'full_name' => $key . '[]',
'choices' => $options
]
];
}
private function renderSlider(
string $key,
string $label,
int $minLimit,
int $maxLimit,
?int $min = null,
?int $max = null
): array
{
return [
'range' => [
'id' => $key,
'label' => $label,
'min' => $minLimit,
'max' => $maxLimit,
'from' => [
'full_name' => $key . '_min',
'value' => $min ?? $minLimit,
],
'to' => [
'full_name' => $key . '_max',
'value' => $max ?? $maxLimit,
],
],
];
}
// Typ
// **********************************
private function getTypeCounts(?array $types = null): array
{
if($types === null) {
$types = $this->productVariantRepository->getAllTypes($this->webspace, $this->locale);
}
$results = [];
foreach($types as $type) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addTypesToQueryBuilder($tempQb, [$type]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.type']);
$results[$type] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderTypeFilter(): array
{
$types = $this->productVariantRepository->getAllTypes($this->webspace, $this->locale);
$typeCounts = $this->getTypeCounts($types);
$options = [];
if($types) {
foreach($types as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->types ?? []),
'disabled' => $typeCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::TYPE->getQueryKey(), FilterEnum::TYPE->trans($this->translator), $options);
}
// **********************************
// Způsob montáže
// **********************************
private function getFixationCounts(?array $fixations = null): array
{
if($fixations === null) {
$fixations = $this->productVariantRepository->getAllFixations($this->webspace, $this->locale);
}
$results = [];
foreach($fixations as $fixation) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addFixationsToQueryBuilder($tempQb, [$fixation]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.fixation']);
$results[$fixation] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderFixationFilter(): array
{
$fixations = $this->productVariantRepository->getAllFixations($this->webspace, $this->locale);
$fixationCounts = $this->getFixationCounts($fixations);
$options = [];
if($fixations) {
foreach($fixations as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->fixations ?? []),
'disabled' => $fixationCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::FIXATION->getQueryKey(), FilterEnum::FIXATION->trans($this->translator), $options);
}
// **********************************
// Světelný tok [lm]
// **********************************
private function renderLuminousFluxFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minLuminousFluxLimit = $this->productVariantRepository->getMinLuminousFlux(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxLuminousFluxLimit = $this->productVariantRepository->getMaxLuminousFlux(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::LUMINOUS_FLUX->getQueryKey(),
label: FilterEnum::LUMINOUS_FLUX->trans($this->translator),
minLimit: $minLuminousFluxLimit,
maxLimit: $maxLuminousFluxLimit,
min: $this->minLuminousFlux,
max: $this->maxLuminousFlux,
);
}
// **********************************
// Index podání barev
// **********************************
private function getCriCounts(?array $cris = null): array
{
if($cris === null) {
$cris = $this->productVariantRepository->getAllCris($this->webspace, $this->locale);
}
$results = [];
foreach($cris as $cri) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addCrisToQueryBuilder($tempQb, [$cri]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.cri']);
$results[$cri] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderCriFilter(): array
{
$cris = $this->productVariantRepository->getAllCris($this->webspace, $this->locale);
$criCounts = $this->getCriCounts($cris);
$options = [];
if($cris) {
foreach($cris as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->cris ?? []),
'disabled' => $criCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::CRI->getQueryKey(), FilterEnum::CRI->trans($this->translator), $options);
}
// **********************************
// Teplota chromatičnosti
// **********************************
private function getChromaticityCounts(?array $chromaticities = null): array
{
if($chromaticities === null) {
$chromaticities = $this->productVariantRepository->getAllCris($this->webspace, $this->locale);
}
$results = [];
foreach($chromaticities as $chromaticity) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addChromaticitiesToQueryBuilder($tempQb, [$chromaticity]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.chromaticity']);
$results[$chromaticity] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderChromaticityFilter(): array
{
$chromaticities = $this->productVariantRepository->getAllChromaticities($this->webspace, $this->locale);
$chromaticityCounts = $this->getChromaticityCounts($chromaticities);
$options = [];
if($chromaticities) {
foreach($chromaticities as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->chromaticities ?? []),
'disabled' => $chromaticityCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::CHROMATICITY->getQueryKey(), FilterEnum::CHROMATICITY->trans($this->translator), $options);
}
// **********************************
// Varianta difúzoru
// **********************************
private function getDispersionVariantCounts(?array $dispersionVariants = null): array
{
if($dispersionVariants === null) {
$dispersionVariants = $this->productVariantRepository->getAllDispersionVariants($this->webspace, $this->locale);
}
$results = [];
foreach($dispersionVariants as $dispersionVariant) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addDispersionVariantsToQueryBuilder($tempQb, [$dispersionVariant]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.dispersionVariant']);
$results[$dispersionVariant] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderDispersionVariantFilter(): array
{
$dispersionVariants = $this->productVariantRepository->getAllDispersionVariants($this->webspace, $this->locale);
$dispersionVariantCounts = $this->getDispersionVariantCounts($dispersionVariants);
$options = [];
if($dispersionVariants) {
foreach($dispersionVariants as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->dispersionVariants ?? []),
'disabled' => $dispersionVariantCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::DISPERSION_VARIANT->getQueryKey(), FilterEnum::DISPERSION_VARIANT->trans($this->translator), $options);
}
// **********************************
// IP stupeň krytí
// **********************************
private function getIPCounts(?array $ips = null): array
{
if($ips === null) {
$ips = $this->productVariantRepository->getAllIps($this->webspace, $this->locale);
}
$results = [];
foreach($ips as $ip) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addIpsToQueryBuilder($tempQb, [$ip]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.protectClass']);
$results[$ip] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderIPFilter(): array
{
$ips = $this->productVariantRepository->getAllIps($this->webspace, $this->locale);
$ipCounts = $this->getIPCounts($ips);
$options = [];
if($ips) {
foreach($ips as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->ips ?? []),
'disabled' => $ipCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::IP->getQueryKey(), FilterEnum::IP->trans($this->translator), $options);
}
// **********************************
// Směr svícení
// **********************************
private function getRadiationDirectCounts(?array $radiationDirects = null): array
{
if($radiationDirects === null) {
$radiationDirects = $this->productVariantRepository->getAllRadiationDirects($this->webspace, $this->locale);
}
$results = [];
foreach($radiationDirects as $radiationDirect) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addRadiationDirectsToQueryBuilder($tempQb, [$radiationDirect]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.radiationDirect']);
$results[$radiationDirect] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderRadiationDirectFilter(): array
{
$radiationDirects = $this->productVariantRepository->getAllRadiationDirects($this->webspace, $this->locale);
$radiationDirectCounts = $this->getRadiationDirectCounts($radiationDirects);
$options = [];
if($radiationDirects) {
foreach($radiationDirects as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->radiationDirects ?? []),
'disabled' => $radiationDirectCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::RADIATION_DIRECT->getQueryKey(), FilterEnum::RADIATION_DIRECT->trans($this->translator), $options);
}
// **********************************
// Tvar
// **********************************
private function getShapeCounts(?array $shapes = null): array
{
if($shapes === null) {
$shapes = $this->productVariantRepository->getAllShapes($this->webspace, $this->locale);
}
$results = [];
foreach($shapes as $shape) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addShapesToQueryBuilder($tempQb, [$shape]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.shape']);
$results[$shape] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderShapeFilter(): array
{
$shapes = $this->productVariantRepository->getAllShapes($this->webspace, $this->locale);
$shapeCounts = $this->getShapeCounts($shapes);
$options = [];
if($shapes) {
foreach($shapes as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->shapes ?? []),
'disabled' => $shapeCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::SHAPE->getQueryKey(), FilterEnum::SHAPE->trans($this->translator), $options);
}
// **********************************
// Délka [mm]
// **********************************
private function renderLengthFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minLengthLimit = $this->productVariantRepository->getMinLength(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxLengthLimit = $this->productVariantRepository->getMaxLength(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::LENGTH->getQueryKey(),
label: FilterEnum::LENGTH->trans($this->translator),
minLimit: $minLengthLimit,
maxLimit: $maxLengthLimit,
min: $this->minLength,
max: $this->maxLength,
);
}
// **********************************
// Šířka, Průměr [mm]
// **********************************
private function renderWidthFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minWidthLimit = $this->productVariantRepository->getMinWidth(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxWidthLimit = $this->productVariantRepository->getMaxWidth(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::WIDTH->getQueryKey(),
label: FilterEnum::WIDTH->trans($this->translator),
minLimit: $minWidthLimit,
maxLimit: $maxWidthLimit,
min: $this->minWidth,
max: $this->maxWidth,
);
}
// **********************************
// Výška [mm]
// **********************************
private function renderHeightFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minHeightLimit = $this->productVariantRepository->getMinHeight(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxHeightLimit = $this->productVariantRepository->getMaxHeight(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::HEIGHT->getQueryKey(),
label: FilterEnum::HEIGHT->trans($this->translator),
minLimit: $minHeightLimit,
maxLimit: $maxHeightLimit,
min: $this->minHeight,
max: $this->maxHeight,
);
}
// **********************************
// Barva svítidla
// **********************************
private function getColorCounts(?array $colors = null): array
{
if($colors === null) {
$colors = $this->productVariantRepository->getAllColors($this->webspace, $this->locale);
}
$results = [];
foreach($colors as $color) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addColorsToQueryBuilder($tempQb, [$color]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.color']);
$results[$color] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderColorFilter(): array
{
$colors = $this->productVariantRepository->getAllColors($this->webspace, $this->locale);
$colorCounts = $this->getColorCounts($colors);
$options = [];
if($colors) {
foreach($colors as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->colors ?? []),
'disabled' => $colorCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::COLOR->getQueryKey(), FilterEnum::COLOR->trans($this->translator), $options);
}
// **********************************
// Použití
// **********************************
private function getUsageCounts(?array $usages = null): array
{
if($usages === null) {
$usages = $this->productVariantRepository->getAllUsages($this->webspace, $this->locale);
}
$results = [];
foreach($usages as $usage) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addUsagesToQueryBuilder($tempQb, [$usage]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.variantUse']);
$results[$usage] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderUsageFilter(): array
{
$usages = $this->productVariantRepository->getAllUsages($this->webspace, $this->locale);
$usageCounts = $this->getUsageCounts($usages);
$options = [];
if($usages) {
foreach($usages as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->usages ?? []),
'disabled' => $usageCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::USAGE->getQueryKey(), FilterEnum::USAGE->trans($this->translator), $options);
}
// **********************************
// Maximální teplota okolí
// **********************************
private function getMaxTemperatureCounts(?array $maxTemperatures = null): array
{
if($maxTemperatures === null) {
$maxTemperatures = $this->productVariantRepository->getAllMaxTemperatures($this->webspace, $this->locale);
}
$results = [];
foreach($maxTemperatures as $maxTemperature) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addMaxTemperaturesToQueryBuilder($tempQb, [$maxTemperature]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.tempMax']);
$results[$maxTemperature] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderMaxTemperatureFilter(): array
{
$maxTemperatures = $this->productVariantRepository->getAllMaxTemperatures($this->webspace, $this->locale);
$maxTemperatureCounts = $this->getMaxTemperatureCounts($maxTemperatures);
$options = [];
if($maxTemperatures) {
foreach($maxTemperatures as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->maxTemperatures ?? []),
'disabled' => $maxTemperatureCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::MAX_TEMPERATURE->getQueryKey(), FilterEnum::MAX_TEMPERATURE->trans($this->translator), $options);
}
// **********************************
// Minimální teplota okolí
// **********************************
private function getMinTemperatureCounts(?array $minTemperatures = null): array
{
if($minTemperatures === null) {
$minTemperatures = $this->productVariantRepository->getAllMinTemperatures($this->webspace, $this->locale);
}
$results = [];
foreach($minTemperatures as $minTemperature) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addMinTemperaturesToQueryBuilder($tempQb, [$minTemperature]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.tempMin']);
$results[$minTemperature] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderMinTemperatureFilter(): array
{
$minTemperatures = $this->productVariantRepository->getAllMinTemperatures($this->webspace, $this->locale);
$minTemperatureCounts = $this->getMinTemperatureCounts($minTemperatures);
$options = [];
if($minTemperatures) {
foreach($minTemperatures as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->minTemperatures ?? []),
'disabled' => $minTemperatureCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::MIN_TEMPERATURE->getQueryKey(), FilterEnum::MIN_TEMPERATURE->trans($this->translator), $options);
}
// **********************************
// Příkon svítidla [W]
// **********************************
private function renderInputPowerFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minInputPowerLimit = $this->productVariantRepository->getMinInputPower(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxInputPowerLimit = $this->productVariantRepository->getMaxInputPower(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::INPUT_POWER->getQueryKey(),
label: FilterEnum::INPUT_POWER->trans($this->translator),
minLimit: $minInputPowerLimit,
maxLimit: $maxInputPowerLimit,
min: $this->minInputPower,
max: $this->maxInputPower,
);
}
// **********************************
// Měrný výkon [lm/W]
// **********************************
private function renderEfficiencyFilter(
?ProductCategory $productCategory = null,
?ProductFamily $productFamily = null,
?Product $product = null,
): array
{
$minEfficiencyLimit = $this->productVariantRepository->getMinEfficiency(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
$maxEfficiencyLimit = $this->productVariantRepository->getMaxEfficiency(
webspace: $this->webspace,
locale: $this->locale,
productCategory: $productCategory,
productFamily: $productFamily,
product: $product,
);
return $this->renderSlider(
key: FilterEnum::EFFICIENCY->getQueryKey(),
label: FilterEnum::EFFICIENCY->trans($this->translator),
minLimit: $minEfficiencyLimit,
maxLimit: $maxEfficiencyLimit,
min: $this->minEfficiency,
max: $this->maxEfficiency,
);
}
// **********************************
// Elektrická třída ochrany
// **********************************
private function getElProtectCounts(?array $elProtects = null): array
{
if($elProtects === null) {
$elProtects = $this->productVariantRepository->getAllElProtects($this->webspace, $this->locale);
}
$results = [];
foreach($elProtects as $elProtect) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addElProtectsToQueryBuilder($tempQb, [$elProtect]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.protectClassEl']);
$results[$elProtect] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderElProtectFilter(): array
{
$elProtects = $this->productVariantRepository->getAllElProtects($this->webspace, $this->locale);
$elProtectCounts = $this->getElProtectCounts($elProtects);
$options = [];
if($elProtects) {
foreach($elProtects as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->elProtects ?? []),
'disabled' => $elProtectCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::EL_PROTECT->getQueryKey(), FilterEnum::EL_PROTECT->trans($this->translator), $options);
}
// **********************************
// Metoda napájení
// **********************************
private function getPowerMethodCounts(?array $powerMethods = null): array
{
if($powerMethods === null) {
$powerMethods = $this->productVariantRepository->getAllPowerMethods($this->webspace, $this->locale);
}
$results = [];
foreach($powerMethods as $powerMethod) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addPowerMethodsToQueryBuilder($tempQb, [$powerMethod]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.supplyVoltage']);
$results[$powerMethod] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderPowerMethodFilter(): array
{
$powerMethods = $this->productVariantRepository->getAllPowerMethods($this->webspace, $this->locale);
$powerMethodCounts = $this->getPowerMethodCounts($powerMethods);
$options = [];
if($powerMethods) {
foreach($powerMethods as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->powerMethods ?? []),
'disabled' => $powerMethodCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::POWER_METHOD->getQueryKey(), FilterEnum::POWER_METHOD->trans($this->translator), $options);
}
// **********************************
// Světelný zdroj
// **********************************
private function getResourceTypeCounts(?array $resourceTypes = null): array
{
if($resourceTypes === null) {
$resourceTypes = $this->productVariantRepository->getAllResourceTypes($this->webspace, $this->locale);
}
$results = [];
foreach($resourceTypes as $resourceType) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addResourceTypesToQueryBuilder($tempQb, [$resourceType]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.resourceType']);
$results[$resourceType] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderResourceTypeFilter(): array
{
$resourceTypes = $this->productVariantRepository->getAllResourceTypes($this->webspace, $this->locale);
$resourceTypeCounts = $this->getResourceTypeCounts($resourceTypes);
$options = [];
if($resourceTypes) {
foreach($resourceTypes as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->resourceTypes ?? []),
'disabled' => $resourceTypeCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::RESOURCE_TYPE->getQueryKey(), FilterEnum::RESOURCE_TYPE->trans($this->translator), $options);
}
// private function getLightSourceCounts(?array $lightSources = null): array
// {
// if($lightSources === null) {
// $lightSources = $this->productVariantRepository->getAllLightSources($this->webspace, $this->locale);
// }
//
// $results = [];
// foreach($lightSources as $lightSource) {
// $tempQb = clone $this->qb;
// $this->productVariantRepository->addLightSourcesToQueryBuilder($tempQb, [$lightSource]);
// $this->putAllCriteriaToQueryBuilder($tempQb, ['pv.luminousFluxSupply']);
// $results[$lightSource] = count($tempQb->getQuery()->getScalarResult());
// }
//
// return $results;
// }
// private function renderLightSourceFilter(): array
// {
// $lightSources = $this->productVariantRepository->getAllLightSources($this->webspace, $this->locale);
// $lightSourceCounts = $this->getLightSourceCounts($lightSources);
//
// $options = [];
// if($lightSources) {
// foreach($lightSources as $option) {
// $options[] = [
// 'label' => $option,
// 'value' => $option,
// 'checked' => $checked = in_array($option, $this->lightSources ?? []),
// 'disabled' => $lightSourceCounts[$option] === 0 && !$checked,
// ];
// }
// }
//
// return $this->renderMultiSelect(FilterEnum::LIGHT_SOURCE->getQueryKey(), FilterEnum::LIGHT_SOURCE->trans($this->translator), $options);
// }
// **********************************
// Stav produktu
// **********************************
private function getStatusCounts(?array $statuses = null): array
{
if($statuses === null) {
$statuses = $this->productVariantRepository->getAllStatuses($this->webspace, $this->locale);
}
$results = [];
foreach($statuses as $status) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addStatusesToQueryBuilder($tempQb, [$status]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.statusNew']);
$results[$status] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderStatusFilter(): array
{
$statuses = $this->productVariantRepository->getAllStatuses($this->webspace, $this->locale);
$statusCounts = $this->getStatusCounts($statuses);
$options = [];
if($statuses) {
foreach($statuses as $option) {
$options[] = [
'label' => $option == true ? 'Novinka' : $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->statuses ?? []),
'disabled' => $statusCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::STATUS->getQueryKey(), FilterEnum::STATUS->trans($this->translator), $options);
}
// **********************************
// Mechanická odolnost
// **********************************
private function getMechanicalDurabilityCounts(?array $mechanicalDurabilities = null): array
{
if($mechanicalDurabilities === null) {
$mechanicalDurabilities = $this->productVariantRepository->getAllMechanicalDurabilities($this->webspace, $this->locale);
}
$results = [];
foreach($mechanicalDurabilities as $mechanicalDurability) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addMechanicalDurabilitiesToQueryBuilder($tempQb, [$mechanicalDurability]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.mechanicalDurability']);
$results[$mechanicalDurability] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderMechanicalDurabilityFilter(): array
{
$mechanicalDurabilities = $this->productVariantRepository->getAllMechanicalDurabilities($this->webspace, $this->locale);
$mechanicalDurabilityCounts = $this->getMechanicalDurabilityCounts($mechanicalDurabilities);
$options = [];
if($mechanicalDurabilities) {
foreach($mechanicalDurabilities as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->mechanicalDurabilities ?? []),
'disabled' => $mechanicalDurabilityCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::MECHANICAL_DURABILITY->getQueryKey(), FilterEnum::MECHANICAL_DURABILITY->trans($this->translator), $options);
}
// **********************************
// Typ difúzoru
// **********************************
private function getDispersionCounts(?array $dispersions = null): array
{
if($dispersions === null) {
$dispersions = $this->productVariantRepository->getAllDispersions($this->webspace, $this->locale);
}
$results = [];
foreach($dispersions as $dispersion) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addDispersionsToQueryBuilder($tempQb, [$dispersion]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.dispersion']);
$results[$dispersion] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderDispersionFilter(): array
{
$dispersions = $this->productVariantRepository->getAllDispersions($this->webspace, $this->locale);
$dispersionCounts = $this->getDispersionCounts($dispersions);
$options = [];
if($dispersions) {
foreach($dispersions as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->dispersions ?? []),
'disabled' => $dispersionCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::DISPERSION->getQueryKey(), FilterEnum::DISPERSION->trans($this->translator), $options);
}
// **********************************
// Předřadník
// **********************************
private function getDriverCounts(?array $drivers = null): array
{
if($drivers === null) {
$drivers = $this->productVariantRepository->getAllDrivers($this->webspace, $this->locale);
}
$results = [];
foreach($drivers as $driver) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addDriversToQueryBuilder($tempQb, [$driver]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.driver']);
$results[$driver] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderDriverFilter(): array
{
$drivers = $this->productVariantRepository->getAllDrivers($this->webspace, $this->locale);
$driverCounts = $this->getDriverCounts($drivers);
$options = [];
if($drivers) {
foreach($drivers as $option) {
$options[] = [
'label' => $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->drivers ?? []),
'disabled' => $driverCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::DRIVER->getQueryKey(), FilterEnum::DRIVER->trans($this->translator), $options);
}
// **********************************
// Doplňující výbava
// **********************************
private function getAccessoryCounts(?array $accessories = null): array
{
if($accessories === null) {
$accessories = $this->productVariantRepository->getAllAccessories($this->webspace, $this->locale);
}
$results = [];
foreach($accessories as $accessory) {
$tempQb = clone $this->qb;
$this->productVariantRepository->addAccessoriesToQueryBuilder($tempQb, [$accessory]);
$this->putAllCriteriaToQueryBuilder($tempQb, ['pv.supplements']);
$results[$accessory] = count($tempQb->getQuery()->getScalarResult());
}
return $results;
}
private function renderAccessoryFilter(): array
{
$accessories = $this->productVariantRepository->getAllAccessories($this->webspace, $this->locale);
$accessoryCounts = $this->getAccessoryCounts($accessories);
$options = [];
if($accessories) {
$titles = $this->productVariantRepository->getTitlesByAccessoryCodes($this->webspace, $this->locale, $accessories);
foreach($accessories as $option) {
$options[] = [
'label' => $titles[$option] ?? $option,
'value' => $option,
'checked' => $checked = in_array($option, $this->accessories ?? []),
'disabled' => $accessoryCounts[$option] === 0 && !$checked,
];
}
}
return $this->renderMultiSelect(FilterEnum::ACCESSORY->getQueryKey(), FilterEnum::ACCESSORY->trans($this->translator), $options);
}
// **********************************
}