<?php
declare(strict_types=1);
namespace VioRepresentativeLogin;
use Doctrine\DBAL\Connection;
use RuntimeException;
use Shopware\Core\Checkout\Order\OrderDefinition;
use Shopware\Core\Framework\Context;
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\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetDefinition;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use VioRepresentativeLogin\Entity\Agent\AgentDefinition;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class VioRepresentativeLogin extends Plugin
{
public const CUSTOM_FIELDSET_NAME = 'vio_representative';
public const AGENT_FIELD = 'vio_representative_agent';
public function install(InstallContext $installContext): void
{
$this->updateCustomFields($installContext->getContext());
parent::install($installContext);
}
public function update(UpdateContext $updateContext): void
{
$this->updateCustomFields($updateContext->getContext());
parent::update($updateContext);
}
public function postUpdate(UpdateContext $updateContext): void
{
parent::postUpdate($updateContext);
if (version_compare($updateContext->getCurrentPluginVersion(), '1.2.0', '<')) {
$this->setDefaultDisplayFields($updateContext->getContext());
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$connection = $this->container->get(Connection::class);
if ($connection) {
$connection->executeStatement('DROP TABLE IF EXISTS vio_representative_customer_agent');
$connection->executeStatement('DROP TABLE IF EXISTS vio_representative_agent');
}
// drop custom field
$this->removeCustomFields($uninstallContext->getContext());
}
/**
* @param Context $context
*/
private function updateCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (!$customFieldSetRepository) {
throw new RuntimeException("Couldn't resolve service 'custom_field_set.repository'");
}
$fieldSetName = static::CUSTOM_FIELDSET_NAME;
$fieldSetId = $customFieldSetRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $fieldSetName)), $context)->firstId();
if ($fieldSetId === null) {
$fieldSetData = [
'name' => $fieldSetName,
'config' => [
'label' => [
'de-DE' => 'Vertreter Daten',
'en-GB' => 'Representative Data'
]
],
'relations' => [
['entityName' => OrderDefinition::ENTITY_NAME]
]
];
$fieldSetData['id'] = $fieldSetId;
/** @var string[] $primaryKeys */
$primaryKeys = $customFieldSetRepository
->upsert([$fieldSetData], $context)
->getPrimaryKeys(CustomFieldSetDefinition::ENTITY_NAME);
$fieldSetId = array_shift($primaryKeys);
}
if ($fieldSetId !== null) {
$this->updateCustomField(
$fieldSetId,
static::AGENT_FIELD,
[
'name' => static::AGENT_FIELD,
'type' => CustomFieldTypes::ENTITY,
'config' => [
'label' => [
'de-DE' => 'Verursachender Vertreter',
'en-GB' => 'Initiating Representative'
],
'entity' => AgentDefinition::ENTITY_NAME,
'componentName' => 'sw-entity-single-select',
'labelProperty' => ['firstName', 'lastName']
],
],
$context
);
}
}
/**
* @param string $fieldSetId
* @param string $fieldName
* @param array $fieldData
* @param Context $context
* @noinspection DuplicatedCode
* @noinspection PhpSameParameterValueInspection
*/
private function updateCustomField(string $fieldSetId, string $fieldName, array $fieldData, Context $context): void
{
$customFieldRepository = $this->container->get('custom_field.repository');
if (!$customFieldRepository) {
throw new RuntimeException("Couldn't resolve service 'custom_field.repository'");
}
if (!array_key_exists('name', $fieldData)) {
$fieldData['name'] = $fieldName;
}
if (!array_key_exists('customFieldSetId', $fieldData)) {
$fieldData['customFieldSetId'] = $fieldSetId;
}
$fieldId = $customFieldRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $fieldName)), $context)->firstId();
if ($fieldId !== null) {
$fieldData['id'] = $fieldId;
}
$customFieldRepository->upsert([$fieldData], $context);
}
private function removeCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (!$customFieldSetRepository) {
throw new RuntimeException("Couldn't resolve service 'custom_field_set.repository'");
}
$fieldSetName = self::CUSTOM_FIELDSET_NAME;
$fieldSetId = $customFieldSetRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $fieldSetName)), $context)->firstId();
if ($fieldSetId) {
$customFieldSetRepository->delete([
[
'id' => $fieldSetId
]
], $context);
}
}
private function setDefaultDisplayFields(Context $getContext)
{
$config = $this->container->get(SystemConfigService::class);
if ($config) {
$config->set('VioRepresentativeLogin.config.displayContent', [
'fullname',
'email',
'lastlogin'
]);
}
}
}