<?php declare(strict_types=1);
namespace Tds\MerwareDiscounts;
use Shopware\Core\Checkout\Customer\CustomerDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class TdsMerwareDiscounts extends Plugin
{
public function install(InstallContext $installContext): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($this->getCustomFieldSets(), $installContext->getContext());
}
/**
* {@inheritDoc}
*
* @param UninstallContext $uninstallContext
*
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
$this->deleteCustomFieldSet($this->getCustomFieldSets(), $uninstallContext->getContext());
$this->deleteCustomFields($this->getCustomFieldSets(), $uninstallContext->getContext());
}
/**
* Helper-function to trigger the deletion of plugin custom field sets
*
* @param $customFieldSets
* @param $context
* @throws InconsistentCriteriaIdsException
*/
private function deleteCustomFieldSet($customFieldSets, $context): void
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$ids = $this->getCustomFieldSetIds($customFieldSets, $context);
if (!empty($ids)) {
$customFieldSetRepository->delete($ids, $context);
}
}
/**
* Helper-function to get custom field set ids
*
* @param $customFieldSets
* @param $context
*
* @return array
*
* @throws InconsistentCriteriaIdsException
*/
private function getCustomFieldSetIds($customFieldSets, $context)
{
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$ids = [];
foreach ($customFieldSets as $customFieldSet) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $customFieldSet['name'])
);
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldSetId)) {
$ids[] = [
'id' => $customFieldSetId
];
}
}
return $ids;
}
/**
* Helper-function to delete custom fields
*
* @param $customFieldSets
* @param $context
*
* @throws InconsistentCriteriaIdsException
*/
private function deleteCustomFields($customFieldSets, $context): void
{
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldRepository = $this->container->get('custom_field.repository');
$ids = $this->getCustomFieldIds($customFieldSets, $context);
if (!empty($ids)) {
$customFieldRepository->delete($ids, $context);
}
}
/**
* Helper-function to get a list of custom field ids
*
* @param $customFieldSets
* @param $context
*
* @return array
*
* @throws InconsistentCriteriaIdsException
*/
private function getCustomFieldIds($customFieldSets, $context)
{
/**
* @var EntityRepositoryInterface $customFieldRepository
*/
$customFieldRepository = $this->container->get('custom_field.repository');
$ids = [];
foreach ($customFieldSets as $customFieldSet) {
if (!empty($customFieldSet['customFields'])) {
foreach ($customFieldSet['customFields'] as $customField) {
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $customField['name'])
);
$customFieldId = $customFieldRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldId)) {
$ids[] = [
'id' => $customFieldId
];
}
}
}
}
return $ids;
}
private function getCustomFieldSets()
{
return [
[
'name' => 'tds_merware_product_discount',
'config' => [
'label' => [
'de-DE' => 'Rabatt',
'en-GB' => 'Discount',
]
],
'customFields' => [
[
'name' => 'tds_merware_product_discount_activate',
'type' => 'bool',
'config' => [
'customFieldPosition' => 1,
'numberType' => 'integer',
'label' => [
'de-DE' => 'Rabattfähig',
'en-GB' => 'Discountable',
],
'helpText' => [
'de-DE' => null,
'en-GB' => null,
],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
]
]
],
'relations' => [
[
'entityName' => $this->container->get(ProductDefinition::class)->getEntityName()
]
]
],
[
'name' => 'tds_merware_customer_discount',
'config' => [
'label' => [
'de-DE' => 'Rabatte',
'en-GB' => 'Discounts',
],
'translated' => false,
],
'customFields' => [
[
'name' => 'tds_merware_customer_discount_general',
'type' => 'number',
'config' => [
'customFieldPosition' => 1,
'numberType' => 'float',
'label' => [
'de-DE' => 'Generalrabatt',
'en-GB' => 'General discount',
],
'componentName' => 'sw-field',
'customFieldType' => 'number',
]
],
[
'name' => 'tds_merware_customer_discount_products',
'type' => 'text',
'config' => [
'customFieldPosition' => 2,
'label' => [
'de-DE' => 'Sonderpreise',
'en-GB' => 'Special prices',
],
'componentName' => 'sw-field',
'customFieldType' => 'text',
]
]
],
'relations' => [
[
'entityName' => $this->container->get(CustomerDefinition::class)->getEntityName()
]
]
]
];
}
}