<?php declare(strict_types=1);
namespace Scop\ScopProductlistBanner;
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\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSetRelation\CustomFieldSetRelationCollection;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSetRelation\CustomFieldSetRelationEntity;
use Shopware\Core\System\CustomField\CustomFieldEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class ScopProductlistBanner extends Plugin
{
/**
* @param InstallContext $installContext
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createAttributes($installContext->getContext());
}
/**
* @param UpdateContext $updateContext
*/
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
$this->createAttributes($updateContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if (!$uninstallContext->keepUserData()) {
$this->deleteAttributes($uninstallContext->getContext());
}
}
/**
* @param Context $installUpdateContext
*/
protected function createAttributes(Context $installUpdateContext): void
{
$context = Context::createDefaultContext();
$cfSet = $this->createFieldsSet('scop_productlist_banner', ['Produktlisten Banner', 'productlist banner'], 'category', $context);
/** @var EntityRepositoryInterface $customFieldRepository */
$customFieldRepository = $this->container->get('custom_field.repository');
$this->createField([
'name' => 'scop_productlist_banner_active',
'type' => CustomFieldTypes::SWITCH,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 1,
'label' => [
'de-DE' => 'Produktlisten Banner aktiv',
'en-GB' => 'productlist banner active'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_image',
'type' => CustomFieldTypes::MEDIA,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 2,
"componentName" => "sw-media-field",
"customFieldType" => "media",
'label' => [
'de-DE' => 'Banner Bild',
'en-GB' => 'banner image'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_headline',
'type' => CustomFieldTypes::HTML,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 3,
'label' => [
'de-DE' => 'Banner Überschrift',
'en-GB' => 'banner headline'
],
'helpText' => [
'de-DE' => htmlspecialchars('Die Überschrift des Banners. Kann <br> enthalten, um eine neue Zeile anzufangen.'),
'en-GB' => htmlspecialchars('The headline of the banner. Could contain <br> for a new line.')
]
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_headline_color',
'type' => CustomFieldTypes::TEXT,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 4,
'type' => 'colorpicker',
'customFieldType' => 'colorpicker',
'label' => [
'de-DE' => 'Banner Überschrift Farbe',
'en-GB' => 'banner headline color'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_text',
'type' => CustomFieldTypes::HTML,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 5,
'label' => [
'de-DE' => 'Banner Text',
'en-GB' => 'banner text'
],
'helpText' => [
'de-DE' => htmlspecialchars('Der Text des Banners. Kann <br> enthalten, um eine neue Zeile anzufangen.'),
'en-GB' => htmlspecialchars('The text of the banner. Could contain <br> for a new line.')
]
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_text_color',
'type' => CustomFieldTypes::TEXT,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 6,
'type' => 'colorpicker',
'customFieldType' => 'colorpicker',
'label' => [
'de-DE' => 'Banner Text Farbe',
'en-GB' => 'banner text color'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_button_link',
'type' => CustomFieldTypes::TEXT,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 7,
'label' => [
'de-DE' => 'Button Link',
'en-GB' => 'button link'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_button_text',
'type' => CustomFieldTypes::TEXT,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 8,
'label' => [
'de-DE' => 'Button Text',
'en-GB' => 'button text'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_button_open_in_new_tab',
'type' => CustomFieldTypes::SWITCH,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 9,
'label' => [
'de-DE' => 'Link in neuem Tab öffnen',
'en-GB' => 'open link in new tab'
],
],
], $customFieldRepository, $context);
$this->createField([
'name' => 'scop_productlist_banner_position',
'type' => CustomFieldTypes::INT,
'customFieldSetId' => $cfSet,
'config' => [
'customFieldPosition' => 10,
'type' => 'number',
'numberType' => 'int',
'min' => 1,
'label' => [
'de-DE' => 'Banner Position',
'en-GB' => 'banner position'
],
],
], $customFieldRepository, $context);
}
/**
* @param string $setName
* @param array $setLabel
* @param string $entityName
* @param Context $context
* @return string
*/
protected function createFieldsSet(string $setName, array $setLabel, string $entityName, Context $context): string
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $setName))
->addAssociation('relations');
/** @var CustomFieldSetEntity|null $cfSet */
$cfSet = $customFieldSetRepository->search($criteria, $context)->first();
$assignedToOrder = false;
if (null !== $cfSet) {
$cfSetId = $cfSet->getId();
/** @var CustomFieldSetRelationCollection<CustomFieldSetRelationEntity> $relations */
$relations = $cfSet->getRelations();
if (null !== $relations) {
/** @var CustomFieldSetRelationEntity $relation */
foreach ($relations as $relation) {
if ($relation->getEntityName() === $entityName) {
$assignedToOrder = true;
break;
}
}
}
} else {
$cfSetId = Uuid::randomHex();
}
$setData = [
'name' => $setName,
'id' => $cfSetId,
'config' => [
'label' => [
'de-DE' => $setLabel[0],
'en-GB' => $setLabel[1]
]
],
];
if (false === $assignedToOrder) {
$setData['relations'] = [['entityName' => $entityName]];
}
$customFieldSetRepository->upsert([$setData], $context);
return $cfSetId;
}
/**
* @param array<string, mixed> $data
* @param EntityRepositoryInterface $customFieldRepository
* @param Context $context
*/
protected function createField(array $data, EntityRepositoryInterface $customFieldRepository, Context $context): void
{
$criteria = new Criteria();
$criteria->addFilter(
new EqualsFilter('name', $data['name'])
);
/** @var CustomFieldEntity|null $fieldEntity */
$fieldEntity = $customFieldRepository->search($criteria, $context)->first();
$data['id'] = $fieldEntity ? $fieldEntity->getId() : Uuid::randomHex();
$customFieldRepository->upsert([$data], $context);
}
/**
* @param Context $uninstallUpdateContext
*/
protected function deleteAttributes(Context $uninstallUpdateContext): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$context = Context::createDefaultContext();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', 'scop_productlist_banner'));
/** @var array<string> $ids */
$ids = $customFieldSetRepository->searchIds($criteria, $context)->getIds();
if (!empty($ids)) {
$ids = array_map(static function (string $id): array {
return ['id' => $id];
}, $ids);
$customFieldSetRepository->delete($ids, $context);
}
/**
*
* @var Connection $connection
*/
$connection = $this->container->get(Connection::class);
$sql = "DELETE FROM snippet WHERE translation_key LIKE 'customFields.scop_productlist_banner_%'";
$connection->executeUpdate($sql);
}
}