<?php declare(strict_types=1);
namespace Ew\CustomRegister;
use Doctrine\DBAL\DBALException;
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 EwCustomRegister extends Plugin
{
protected const BODY_WEIGHT = 'bodyWeight';
protected const BODY_DESIRED_WEIGHT = 'bodyDesiredWeight';
protected const BODY_HEIGHT = 'bodyHeight';
protected const GENDER_TYPE = 'genderType';
protected const BIRTHDAY = 'birthDay';
protected const HOUSE_NUMBER = 'houseNumber';
/**
* @param InstallContext $context
* @throws DBALException
*/
public function install(InstallContext $context) : void
{
parent::install($context);
$customerFieldsRepository = $this->container->get('custom_field_set.repository');
$this->addCustomFieldsToCustomer($context, $customerFieldsRepository);
}
/**
* @return array[]
*/
private function getCustomFields(): array
{
return [
[
'name' => self::BODY_WEIGHT,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Body weight',
'en-GB' => 'Body weight'
],
'customFieldType' => 'text',
'customFieldPosition' => 1
]
],
[
'name' => self::BODY_HEIGHT,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Body height',
'en-GB' => 'Body height'
],
'customFieldType' => 'text',
'customFieldPosition' => 2
]
],
[
'name' => self::GENDER_TYPE,
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Gender type',
'en-GB' => 'Gender type'
],
'customFieldType' => 'text',
'customFieldPosition' => 3
]
],
[
'name' => self::BIRTHDAY,
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Birthday',
'en-GB' => 'Birthday'
],
'customFieldType' => 'text',
'customFieldPosition' => 4
]
],
[
'name' => self::BODY_DESIRED_WEIGHT,
'type' => CustomFieldTypes::FLOAT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Desired weight',
'en-GB' => 'Desired weight'
],
'customFieldType' => 'text',
'customFieldPosition' => 5
]
],
];
}
private function getCustomFieldsAddress(): array
{
return [
[
'name' => self::HOUSE_NUMBER,
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => [
'de-DE' => 'Hausnummer',
'en-GB' => 'House No.'
],
'customFieldType' => 'text',
'customFieldPosition' => 5
]
],
];
}
/**
* @throws DBALException
*/
private function addCustomFieldsToCustomer($context, ?object $customFieldSetRepository): void
{
$this->uninstallCustomFieldset($context->getContext());
$customFieldSetRepository->create([[
'name' => 'ew-custom-register',
'position' => 0,
'global' => true,
'active' => true,
'config' => [
'label' => [
'en-GB' => 'EW Customer Fields',
'de-DE' => 'EW Customer Fields'
],
],
'customFields' => $this->getCustomFields(),
'relations' => [
[
'entityName' => 'customer'
]
]
]], $context->getContext());
$customFieldSetRepository->create([[
'name' => 'ew-custom-register-address',
'position' => 0,
'global' => true,
'active' => true,
'config' => [
'label' => [
'en-GB' => 'EW Customer Address Fields',
'de-DE' => 'EW Customer Address Fields'
],
],
'customFields' => $this->getCustomFieldsAddress(),
'relations' => [
[
'entityName' => 'customer_address'
]
]
]], $context->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
parent::uninstall($uninstallContext);
return;
}
$this->uninstallCustomFieldset($uninstallContext->getContext());
}
private function uninstallCustomFieldset($context){
$cfsRepo = $this->container->get('custom_field_set.repository');
$cfRepo = $this->container->get('custom_field.repository');
foreach (['ew-custom-register', 'ew-custom-register-address'] as $value) {
//delete custom_field_set entry
$cfsId = $cfsRepo->search((new Criteria())
->addFilter(new EqualsFilter('name', $value)), $context
)->first();
if ($cfsId) {
$cfsRepo->delete([
['id' => $cfsId->getId()]
], $context);
//delete custom_field entries
$cfIds = $cfRepo->search((new Criteria())
->addFilter(
new EqualsFilter('customFieldSetId', $cfsId->getId())
), $context)->getIds();
$ids = [];
foreach ($cfIds as $id) {
$ids[] = ['id' => $id];
}
$cfRepo->delete($ids, $context);
}
}
}
}