<?php
declare(strict_types=1);
namespace WebLa_HideWithoutStock;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
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\ContainsFilter;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class WebLa_HideWithoutStock extends Plugin
{
const CUSTOM_FIELD_SHOW_EMPTY_STOCK = 'webla_show_empty_stock';
// plugin
public function install(InstallContext $context): void
{
parent::install($context);
$prevID = $this->getCustomFieldSetID($context->getContext());
$this->upsertCustomField($context->getContext(), $prevID);
}
public function update(UpdateContext $context): void
{
parent::update($context);
$prevID = $this->getCustomFieldSetID($context->getContext());
$this->upsertCustomField($context->getContext(), $prevID);
}
public function activate(ActivateContext $context): void
{
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
parent::deactivate($context);
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeConfiguration($context->getContext());
$this->removeCustomField($context);
}
// Configuration
private function removeConfiguration(Context $context): void
{
/** @var EntityRepositoryInterface $systemConfigRepository */
$systemConfigRepository = $this->container->get('system_config.repository');
$criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey', $this->getName() . '.config.'));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
if ($ids === []) {
return;
}
$systemConfigRepository->delete($ids, $context);
}
// custom field
private function upsertCustomField($context, $id)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$newID = $id ?? Uuid::randomHex();
$customFieldSetRepository->upsert([
[
'id' => $newID,
'name' => 'webla_empty_stock',
'config' => [
'label' => [
'de-DE' => 'Leerer Lagerbestand',
'en-GB' => 'Empty stock'
]
],
'customFields' => [
[
'id' => $this->getCustomFieldID($context, self::CUSTOM_FIELD_SHOW_EMPTY_STOCK) ?? Uuid::randomHex(),
'name' => self::CUSTOM_FIELD_SHOW_EMPTY_STOCK,
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'de-DE' => 'Anzeigen',
'en-GB' => 'Show'
],
// 'type' => 'switch',
// 'customFieldType' => 'switch',
'customFieldPosition' => 1
]
]
],
'relations' => [
[
'id' => $id == null ? Uuid::randomHex() : $this->getCustomFieldRelationID($context, $id),
'entityName' => 'product'
]
]
]
], $context);
}
private function removeCustomField(UninstallContext $uninstallContext)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldRepository = $this->container->get('custom_field.repository');
$fieldSetIds = $this->customFieldsExist($uninstallContext);
if ($fieldSetIds) {
$fieldSetId = $this->getCustomFieldSetID($uninstallContext->getContext());
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('customFieldSetId', $fieldSetId));
$fieldIds = $customFieldRepository->searchIds($criteria, $uninstallContext->getContext());
foreach($fieldIds->getData() as $id){
$customFieldRepository->delete([['id' => $id['id']]], $uninstallContext->getContext());
}
$customFieldSetRepository->delete(array_values($fieldSetIds->getData()), $uninstallContext->getContext());
}
}
private function getCustomFieldID($context, $name)
{
$customFieldRepository = $this->container->get('custom_field.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', [$name]));
$ids = $customFieldRepository->search($criteria, $context)->first();
return $ids == null ? null : $ids->getId();
}
private function getCustomFieldRelationID(Context $context, $id)
{
$customFieldRepository = $this->container->get('custom_field_set_relation.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('customFieldSetId', $id));
$ids = $customFieldRepository->search($criteria, $context)->first();
return $ids == null ? null : $ids->getId();
}
private function getCustomFieldSetID(Context $context)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', [self::CUSTOM_FIELD_SHOW_EMPTY_STOCK]));
$ids = $customFieldSetRepository->search($criteria, $context)->first();
return $ids == null ? null : $ids->getId();
}
private function customFieldsExist(UninstallContext $context)
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SHOW_EMPTY_STOCK));
$ids = $customFieldSetRepository->searchIds($criteria, $context->getContext());
return $ids->getTotal() > 0 ? $ids : false;
}
}