<?php declare(strict_types=1);
namespace TigerMedia\TigerConnect;
use Exception;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use TigerMedia\Base\Core\Framework\TigerPlugin;
class TigerConnect extends TigerPlugin
{
const CONFIG_PREFIX = 'TigerConnect.config.';
public function executeComposerCommands(): bool
{
return true;
}
/**
* @throws Exception
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$locator = new FileLocator('Resources/config');
$resolver = new LoaderResolver([
new YamlFileLoader($container, $locator),
new GlobFileLoader($container, $locator),
new DirectoryLoader($container, $locator)
]);
$configLoader = new DelegatingLoader($resolver);
$configDirectory = rtrim($this->getPath(), '/') . '/Resources/config';
$configLoader->load($configDirectory . '/{packages}/*.yaml', 'glob');
}
public function activate(ActivateContext $activateContext): void
{
$this->createCustomFieldSets($this->container->get('custom_field_set.repository'), $activateContext);
parent::activate($activateContext);
}
public function uninstall(UninstallContext $uninstallContext): void
{
$this->removeCustomFieldSets(
$this->container->get('custom_field_set.repository'),
Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
$uninstallContext->getContext()
);
parent::uninstall($uninstallContext);
}
public function deactivate(DeactivateContext $deactivateContext): void
{
$this->removeCustomFieldSets(
$this->container->get('custom_field_set.repository'),
Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
$deactivateContext->getContext()
);
parent::deactivate($deactivateContext);
}
/**
* @param EntityRepository $customFieldSetRepository
* @param ActivateContext $activateContext
* @return void
*/
private function createCustomFieldSets(EntityRepository $customFieldSetRepository, ActivateContext $activateContext): void
{
$this->removeCustomFieldSets(
$customFieldSetRepository,
Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
$activateContext->getContext()
);
$customFieldSetRepository->upsert([
[
'id' => Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
'name' => 'tiger_connect_custom_field_set',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'TigerConnect'
]
],
'customFields' => $this->getCustomFields(),
'relations' => [
[
'id' => Uuid::randomHex(),
'entityName' => OrderDefinition::ENTITY_NAME
]
]
]
], $activateContext->getContext());
}
/**
* @param EntityRepository $customFieldSetRepository
* @param string $customFieldSetId
* @param Context $context
* @return void
*/
private function removeCustomFieldSets(EntityRepository $customFieldSetRepository, string $customFieldSetId, Context $context): void
{
$customFieldSetRepository->delete([
['id' => $customFieldSetId]
], $context);
}
/**
* @return mixed[]
*/
private function getCustomFields(): array
{
return [
[
'id' => Uuid::fromStringToHex('TigerConnectProcessed'),
'name' => 'tiger_connect_custom_field_set_processed',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'en-GB' => 'Order Processed'
],
'componentName' => 'sw-field',
'customFieldPosition' => 0
]
],
[
'id' => Uuid::fromStringToHex('TigerConnectErpOrderNumber'),
'name' => 'tiger_connect_custom_field_set_erp_order_number',
'type' => CustomFieldTypes::TEXT,
'config' => [
'label' => [
'en-GB' => 'ERP Order Number'
],
'componentName' => 'sw-field',
'customFieldPosition' => 1
]
]
];
}
}