<?php declare(strict_types=1);
namespace LenzPlatformCustomerDiscount;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
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\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class LenzPlatformCustomerDiscount extends Plugin
{
public function install(InstallContext $installContext): void
{
foreach($this->customFieldSets as $key => $val) {
$this->createOrUpdateCustomFields($key);
}
}
public function update(UpdateContext $updateContext): void
{
foreach($this->customFieldSets as $key => $val) {
$this->createOrUpdateCustomFields($key);
}
}
public function uninstall(UninstallContext $context): void
{
if (!$context->keepUserData()) {
$connection = $this->container->get(Connection::class);
$tablesToDelete = [
];
foreach ($tablesToDelete as $table) {
try {
$connection->executeStatement('DROP TABLE IF EXISTS `' . $table . '`');
} catch(\Exception $e) {
echo "Table \"" . $table . "\" not deleted.\n\r";
}
}
$customFieldsToRemoveFromDb = [
'product_translation' => [
'lenz_customer_discount_product_exclude_from_discount',
],
'customer' => [
'lenz_customer_discount_customer_discount_percent',
],
];
foreach ($customFieldsToRemoveFromDb as $table => $fields) {
foreach ($fields as $field) {
$connection->executeStatement('UPDATE `' . $table . '` SET custom_fields = JSON_REMOVE(`' . $table . '`.custom_fields, "$.' . $field . '");');
}
}
foreach($this->customFieldSets as $key => $val) {
$this->deleteCustomFieldSet($key);
}
}
}
// -- Custom fields
private array $customFieldSets = [
'lenz_customer_discount_customer' => [
'name' => 'lenz_customer_discount_customer',
'config' => [
'label' => [
'en-GB' => 'Customer Discount',
'de-DE' => 'Kundenindividuelle Rabatte',
],
],
'relations' => [
['entityName' => 'customer']
],
'customFields' => [
[
'name' => 'lenz_customer_discount_customer_discount_percent',
'type' => CustomFieldTypes::FLOAT,
'config' => [
'label' => [
'en-GB' => 'Discount in %',
'de-DE' => 'Rabatt in %'
],
'customFieldPosition' => 100,
],
],
],
],
'lenz_customer_discount_product' => [
'name' => 'lenz_customer_discount_product',
'config' => [
'label' => [
'en-GB' => 'Customer Discount',
'de-DE' => 'Kundenindividuelle Rabatte',
],
],
'relations' => [
['entityName' => 'product']
],
'customFields' => [
[
'name' => 'lenz_customer_discount_product_exclude_from_discount',
'type' => CustomFieldTypes::BOOL,
'config' => [
'label' => [
'en-GB' => 'Exclude from discount',
'de-DE' => 'Von Rabatt ausschließen'
],
'customFieldPosition' => 100,
],
],
],
],
];
private function findCustomFieldSet($name): ?CustomFieldSetEntity
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
$criteria->addAssociation('customFields');
$criteria->addAssociation('relations');
/** @var CustomFieldSetEntity|null $customFieldSet */
return $customFieldSetRepository->search($criteria, Context::createDefaultContext())->first();
}
public function createOrUpdateCustomFields($name)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSet = $this->findCustomFieldSet($name);
$customFieldSetId = null;
$customFieldName2Id = [];
$relationEntity2Id = [];
if(!empty($customFieldSet)) {
$customFieldSetId = $customFieldSet->getId();
foreach ($customFieldSet->getCustomFields() as $customField) {
$customFieldName2Id[$customField->getName()] = $customField->getId();
}
foreach ($customFieldSet->getRelations() as $relation) {
$relationEntity2Id[$relation->getEntityName()] = $relation->getId();
}
}
$customFieldSet = $this->customFieldSets[$name];
$customFieldSet['id'] = $customFieldSetId;
foreach ($customFieldSet['customFields'] as $customFieldKey => $customField) {
if(!array_key_exists($customField['name'], $customFieldName2Id)) {
continue;
}
$customFieldSet['customFields'][$customFieldKey]['id'] = $customFieldName2Id[$customField['name']];
}
foreach ($customFieldSet['relations'] as $relationKey => $relation) {
if(!array_key_exists($relation['entityName'], $relationEntity2Id)) {
continue;
}
$customFieldSet['relations'][$relationKey]['id'] = $relationEntity2Id[$relation['entityName']];
}
$customFieldSetRepository->upsert(
[ $customFieldSet ],
Context::createDefaultContext()
);
}
private function deleteCustomFieldSet(string $name)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSet = $this->findCustomFieldSet($name);
if ($customFieldSet === null) {
return;
}
$customFieldSetRepository->delete(
[
['id' => $customFieldSet->getId()],
],
Context::createDefaultContext()
);
}
}