<?php declare(strict_types=1);
namespace Acris\CmsPreview;
use Acris\CmsPreview\DependencyInjection\CompilerPass\CmsPreviewCacheCompilerPass;
use Doctrine\DBAL\Connection;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class AcrisCmsPreviewCS extends Plugin
{
public function activate(ActivateContext $context): void
{
$this->insertDefaultPreviewProfiles($context->getContext());
}
public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new CmsPreviewCacheCompilerPass());
}
private function insertDefaultPreviewProfiles(Context $context): void
{
/** @var EntityRepositoryInterface $previewRepository */
$previewRepository = $this->container->get('acris_cms_preview_profile.repository');
$salesChannelRepository = $this->container->get('sales_channel.repository');
$criteria = new Criteria();
$criteria->addAssociation('domains');
$salesChannelEntities = $salesChannelRepository->search($criteria, $context)->getEntities();
$profileData = [];
foreach ($salesChannelEntities as $channel) {
if ($channel->getTypeId() !== Defaults::SALES_CHANNEL_TYPE_API) {
if (!empty($channel->getDomains()->getElements())) {
$profileData[] = [
'internalName' => $channel->getName(),
'active' => true,
'position' => 10,
'salesChannelId' => $channel->getId(),
'salesChannelDomainId' => $channel->getDomains()->first()->getId(),
'currencyId' => $channel->getCurrencyId(),
'customerGroupId' => $channel->getCustomerGroupId()
];
}
}
}
foreach ($profileData as $profile) {
$this->createIfNotExists($previewRepository, [['name' => 'internalName', 'value' => $profile['internalName']]], $profile, $context);
}
}
private function createIfNotExists(EntityRepositoryInterface $repository, array $equalFields, array $data, Context $context): void
{
$filters = [];
foreach ($equalFields as $equalField) {
$filters[] = new EqualsFilter($equalField['name'], $equalField['value']);
}
if (sizeof($filters) > 1) {
$filter = new MultiFilter(MultiFilter::CONNECTION_OR, $filters);
} else {
$filter = array_shift($filters);
}
$searchResult = $repository->search((new Criteria())->addFilter($filter), $context);
if (empty($searchResult->first())) {
$repository->upsert([$data], $context);
}
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->cleanupDatabase();
}
private function cleanupDatabase(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS acris_cms_preview_profile');
}
}