<?php declare(strict_types=1);
namespace Pixel\LexikonSW6\Component;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilder;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FrontHelpers
{
private $systemConfigService;
private $lexikonRepository;
private $lexikonCatRepository;
private $salesChannelProductRepository;
private $productStreamBuilder;
private $connection;
private $seoUrlRewriter;
private $container;
public function __construct(
SeoUrlPlaceholderHandlerInterface $seoUrlRewriter,
SystemConfigService $systemConfigService,
EntityRepositoryInterface $lexikonRepository,
EntityRepositoryInterface $lexikonCatRepository,
SalesChannelRepositoryInterface $salesChannelProductRepository,
ProductStreamBuilder $productStreamBuilder,
Connection $connection,
ContainerInterface $container
) {
$this->systemConfigService = $systemConfigService;
$this->lexikonRepository = $lexikonRepository;
$this->lexikonCatRepository = $lexikonCatRepository;
$this->salesChannelProductRepository = $salesChannelProductRepository;
$this->productStreamBuilder = $productStreamBuilder;
$this->connection = $connection;
$this->seoUrlRewriter = $seoUrlRewriter;
$this->container = $container;
}
public function getEntries($context, $sales_channel_id, $categoryId, $systemConfig, $limit = null, $offset = null)
{
$catEntries = array();
$criteria = new Criteria();
$criteria->addAssociation('channels');
$criteria->getAssociation('channels');
if ($systemConfig['LEXIKONLISTIMAGES'] ) {
$criteria->addAssociation('media');
$criteria->getAssociation('media');
}
$criteria->addFilter(
new EqualsFilter('categoryid', $categoryId)
);
$criteria->addFilter(new EqualsFilter('active', true));
if ($systemConfig['LEXIKONSHOW'] == 'settings') {
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('pixel_lexikon.channels.id', null),
new EqualsFilter('pixel_lexikon.channels.id', $sales_channel_id),
]));
}
$criteria->addSorting(new FieldSorting('position', FieldSorting::ASCENDING));
if ($limit) {
$criteria->setLimit($limit);
}
if ($offset) {
$criteria->setOffset(($offset - 1) * $limit);
}
$criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
$lexikonEntries = $this->lexikonRepository->search($criteria, $context);
if (count($lexikonEntries) > 0) {
$catEntries = $lexikonEntries;
}
return $catEntries;
}
public function getAllLexikon(
$context,
$sales_channel_id,
$categoryId,
$systemConfig,
$limit = null,
$offset = null
) {
$letters = $this->getCategories($sales_channel_id, $context, $systemConfig);
$catEntries = array('categories' => array());
foreach ($letters as $catentry) {
$id = $catentry->get('id');
$this->lexikonCatRepository->update(
[
['id' => $id, 'shows' => $catentry->get('shows') + 1],
],
$context
);
$catEntries['categories'][$id] = $catentry;
if ($id == $categoryId) {
$catEntries['categories'][$id]->nav_active = true;
} else {
$catEntries['categories'][$id]->nav_active = false;
}
$lexikonEntries = $this->getCountOfCat($id, $systemConfig,$sales_channel_id);
$catEntries['categories'][$id]->countItems = (int)$lexikonEntries;
}
return $catEntries;
}
public function getCountOfCat($categoryId, $systemConfig,$sales_channel_id)
{
if ($systemConfig['LEXIKONSHOW'] == 'settings') {
$count = $this->connection->fetchColumn('SELECT count(pixel_lexikon_channels.pixel_lexikon_id) FROM pixel_lexikon LEFT JOIN pixel_lexikon_channels ON pixel_lexikon_channels.pixel_lexikon_id = pixel_lexikon.id WHERE HEX(pixel_lexikon.categoryid) = ? AND HEX(pixel_lexikon_channels.sales_channel_id) = ?',
[
$categoryId,
$sales_channel_id
]);
} else {
$count = $this->connection->fetchColumn('SELECT count(id) FROM pixel_lexikon WHERE HEX(pixel_lexikon.categoryid) = ? ',
[
$categoryId
]);
}
return $count;
}
public function getLexikonById($context, $id)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $id));
$criteria->addAssociation('media');
$criteria->getAssociation('media');
$criteria->addAssociation('products');
$criteria->getAssociation('products');
$lexikonEntry = $this->lexikonRepository->search($criteria, $context)->first();
return $lexikonEntry;
}
public function getLexikonItem($context, $id)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $id));
$criteria->addAssociation('media');
$criteria->getAssociation('media');
$lexikonEntry = $this->lexikonRepository->search($criteria, $context)->first();
return $lexikonEntry;
}
public function getCategory($context, $id)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $id));
$criteria->addFilter(new EqualsFilter('active', true));
$lexikonCatEntry = $this->lexikonCatRepository->search($criteria, $context)->first();
return $lexikonCatEntry;
}
public function getCategories($sales_channel_id, $context, $systemConfig)
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('notshow', false));
if ($systemConfig['LEXIKONSHOW'] == 'settings') {
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('pixel_lexikon_categories.channels.id', $sales_channel_id),
new EqualsFilter('pixel_lexikon_categories.channels.id', null),
]));
}
$criteria->addAssociation('seoUrls');
$criteria->getAssociation('seoUrls');
$criteria->addAssociation('channels');
$criteria->getAssociation('channels');
$criteria->addSorting(new FieldSorting('position', FieldSorting::ASCENDING));
$lexikonEntries = $this->lexikonCatRepository->search($criteria, $context)->getElements();
return $lexikonEntries;
}
public function getItemsFromCategory(Criteria $criteria, SalesChannelContext $salesChannelContext, ?string $categoryId)
{
$repo = $this->container->get('pixel_lexikon_categories.repository');
$criteria->addAssociation('items');
$criteria->getAssociation('items');
if($categoryId != '00000000000000000000000000000000') {
$criteria->addFilter(new EqualsFilter('id', $categoryId));
}
$criteria->addSorting(new FieldSorting('title', FieldSorting::ASCENDING));
$results = $repo->search($criteria, $salesChannelContext->getContext());
return $results;
}
}