<?php declare(strict_types=1);
namespace Wexo\ProductDownloads;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\Context;
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\ContainsFilter;
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;
class WexoProductDownloads extends Plugin
{
public function getParent()
{
// TODO: Implement getParent() method.
}
public function install(InstallContext $installContext): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());
}
/**
* {@inheritDoc}
*
* @param UninstallContext $uninstallContext
* @throws InconsistentCriteriaIdsException
*/
public function uninstall(UninstallContext $uninstallContext): void
{
$this->deleteCustomFieldSet($uninstallContext->getContext());
$this->deleteCustomFields($uninstallContext->getContext());
}
/**
* Helper-function to delete the advantage custom field set
*
* @param Context $context
*
* @throws InconsistentCriteriaIdsException
*/
protected function deleteCustomFieldSet($context): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', 'wexo_product_downloads')
);
$customFieldSetId = $customFieldSetRepository->searchIds($criteria, $context)->firstId();
if (!empty($customFieldSetId)) {
$customFieldSetRepository->delete(
[
[
'id' => $customFieldSetId
]
],
$context
);
}
}
/**
* Helper-function to delete all relevant plugin custom fields
*
* @param Context $context
*
* @throws InconsistentCriteriaIdsException
*/
protected function deleteCustomFields($context): void
{
/** @var EntityRepositoryInterface $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
$criteria = new Criteria();
$criteria->addFilter(
new ContainsFilter('name', 'wexo_product_downloads')
);
$ids = $customFieldRepository->searchIds($criteria, $context)->getIds();
if (!empty($ids)) {
$data = [];
foreach ($ids as $id) {
$data[] = [
'id' => $id
];
}
$customFieldRepository->delete($data, $context);
}
}
/**
* Helper-function to get the required custom field set with it's fields
*
* @return array
*/
protected function getCustomFieldSet(): array
{
return [
[
'name' => 'wexo_product_downloads',
'config' => [
'label' => [
'da-DK' => 'Downloads',
'en-GB' => 'Downloads',
],
],
'customFields' => [
[
'name' => 'wexo_product_download_1',
'type' => 'media',
'config' => [
'customFieldPosition' => 1,
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'da-DK' => 'Download 1',
'en-GB' => 'Download 1',
],
],
],
[
'name' => 'wexo_product_download_2',
'type' => 'media',
'config' => [
'customFieldPosition' => 2,
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'da-DK' => 'Download 2',
'en-GB' => 'Download 2',
],
],
],
[
'name' => 'wexo_product_download_3',
'type' => 'media',
'config' => [
'customFieldPosition' => 3,
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'da-DK' => 'Download 3',
'en-GB' => 'Download 3',
],
],
],
[
'name' => 'wexo_product_download_4',
'type' => 'media',
'config' => [
'customFieldPosition' => 4,
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'da-DK' => 'Download 4',
'en-GB' => 'Download 4',
],
],
],
[
'name' => 'wexo_product_download_5',
'type' => 'media',
'config' => [
'customFieldPosition' => 5,
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'da-DK' => 'Download 5',
'en-GB' => 'Download 5',
],
],
],
],
'relations' => [
[
'entityName' => $this->container->get(ProductDefinition::class)->getEntityName()
]
]
]
];
}
}