<?php declare(strict_types=1);
namespace Cnf\AptoShopware6Connector;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
class AptoShopware6Connector extends Plugin
{
/**
* @param InstallContext $installContext
* @throws InconsistentCriteriaIdsException
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->initDefaults($installContext->getContext());
$this->addCustomFields($installContext->getContext());
}
/**
* @param ActivateContext $activateContext
*/
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext);
}
/**
* @param DeactivateContext $deactivateContext
* @throws InconsistentCriteriaIdsException
*/
public function deactivate(DeactivateContext $deactivateContext): void
{
$this->repairCustomFields($deactivateContext->getContext());
parent::deactivate($deactivateContext);
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
$this->removeCustomFields($uninstallContext->getContext());
parent::uninstall($uninstallContext);
}
/**
* @param Context $context
*/
private function removeCustomFields(Context $context)
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetId = $this->getCustomFieldSetId('cnf_apto_connector_set', $context, $customFieldSetRepository);
if ($customFieldSetId !== null) {
$customFieldSetRepository->delete([['id' => $customFieldSetId]], $context);
}
}
/**
* @param Context $context
* @throws InconsistentCriteriaIdsException
*/
private function addCustomFields(Context $context) : void
{
/** @var EntityRepository $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
/** todo: Lock Customfields in Backend */
$customFields = [];
$newCustomFieldSet = $this->makeCustomFieldSet(
'cnf_apto_connector_set',
true,
[
'en-GB' => 'Apto Connector',
'de-DE' => 'Apto Connector'
],
[
[
'entityName' => 'product'
]
]
);
$customFields[] = $newCustomFieldSet;
foreach ($customFields as $customFieldSet) {
$customFieldSetId = $this->getCustomFieldSetId($customFieldSet['name'], $context, $customFieldSetRepository);
if ($customFieldSetId !== null) {
$customFieldSetRepository->delete([['id' => $customFieldSetId]], $context);
}
if (!$this->checkIfCustomFieldExists('cnf_apto_connector_enable_apto', $context, $customFieldSetRepository)) {
$this->addCustomField(
$customFieldSet,
'cnf_apto_connector_enable_apto',
\Shopware\Core\System\CustomField\CustomFieldTypes::BOOL,
[
'en-GB' => 'enable Apto.ONE',
'de-DE' => 'Apto.ONE benutzen'
],1
);
}
if (!$this->checkIfCustomFieldExists('cnf_apto_connector_use_inline', $context, $customFieldSetRepository)) {
$this->addCustomField(
$customFieldSet,
'cnf_apto_connector_use_inline',
\Shopware\Core\System\CustomField\CustomFieldTypes::BOOL,
[
'en-GB' => 'include Apto.ONE on product page',
'de-DE' => 'Apto.ONE direkt auf Produktseite anzeigen'
], 2
);
}
if (!$this->checkIfCustomFieldExists('cnf_apto_connector_product_id', $context, $customFieldSetRepository)) {
$this->addCustomField(
$customFieldSet,
'cnf_apto_connector_product_id',
\Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
[
'en-GB' => 'Apto.ONE product id or product-url',
'de-DE' => 'Apto.ONE Produkt-ID oder Produkt-URL',
], 3
);
}
if (!$this->checkIfCustomFieldExists('cnf_apto_connector_config_id', $context, $customFieldSetRepository)) {
$this->addCustomField(
$customFieldSet,
'cnf_apto_connector_config_id',
\Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
[
'en-GB' => 'Apto.ONE config id',
'de-DE' => 'Apto.ONE Config-ID',
], 4
);
}
$customFieldSetRepository->create([$customFieldSet], $context);
}
}
/**
* @param Context $context
* @throws InconsistentCriteriaIdsException
*/
private function repairCustomFields(Context $context): void
{
//TODO: need a proper repair function or switch from custom fields to a custom model extension
//$this->addCustomFields($context);
}
/**
* @param string $name
* @param bool $active
* @param array $labels
* @param array $relations
* @return array
*/
private function makeCustomFieldSet(string $name, bool $active, array $labels, array $relations): array
{
return [
'name' => $name,
'active' => $active,
'config' => [
'label' => $labels,
],
'customFields' => [],
'relations' => $relations
];
}
/**
* @param array $fieldSet
* @param string $name
* @param string $type
* @param array $labels
* @param int $position
*/
private function addCustomField(array &$fieldSet, string $name, string $type, array $labels, int $position = 1): void
{
$fieldSet['customFields'][] = [
'name' => $name,
'type' => $type,
'config' => [
'label' => $labels,
'customFieldPosition' => $position,
]
];
}
/**
* @param string $name
* @param Context $context
* @param EntityRepository $customFieldSetRepository
* @return bool
* @throws InconsistentCriteriaIdsException
*/
private function checkIfCustomFieldExists(string $name, Context $context, EntityRepository $customFieldSetRepository): bool
{
$criteria = new Criteria();
$criteria->addAssociation('customFields');
$customFieldSetsFound = $customFieldSetRepository->search($criteria, $context);
if ($customFieldSetsFound->getTotal() > 0) {
$customFieldSetsFound = $customFieldSetsFound->getEntities();
/** @var CustomFieldSetEntity $customFieldSetFound */
foreach ($customFieldSetsFound->getElements() as $customFieldSetFound) {
foreach ($customFieldSetFound->getCustomFields() as $customField )
if ($customField->getName() === $name) {
return true;
}
}
}
return false;
}
/**
* @param string $name
* @param Context $context
* @param EntityRepository $customFieldSetRepository
* @return string|null
*/
private function getCustomFieldSetId(string $name, Context $context, EntityRepository $customFieldSetRepository)
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $name)
);
$customFieldSetsFound = $customFieldSetRepository->search($criteria, $context);
if ($customFieldSetsFound->getTotal() > 0) {
$customFieldSetsFound = $customFieldSetsFound->getEntities();
/** @var CustomFieldSetEntity $customFieldSetFound */
foreach ($customFieldSetsFound->getElements() as $customFieldSetFound) {
return $customFieldSetFound->getId();
}
}
return null;
}
/**
* @param Context $context
* @throws InconsistentCriteriaIdsException
*/
private function initDefaults(Context $context): void
{
$this->setDefaultConfigValue(
$context,
'AptoShopware6Connector.config.securityToken',
Uuid::randomHex(),
false
);
}
/**
* @param Context $context
* @param string $key
* @param $value
* @param bool $renew
*/
private function setDefaultConfigValue(Context $context, string $key, $value, bool $renew = false): void
{
$data = [
'configurationKey' => $key,
'configurationValue' => $value
];
/** @var EntityRepository $repository */
$repository = $this->container->get('system_config.repository');
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('configurationKey', $key)
);
$config = $repository->search($criteria, $context);
if ($config->getTotal() === 0) {
$repository->create([$data], $context);
return;
}
if ($config->getTotal() > 0 && $renew === true) {
$data['id'] = $config->first()->getId();
$repository->update([$data], $context);
}
}
}