<?php
declare(strict_types=1);
namespace Ecocode\Trustpilot;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Ecocode\Trustpilot\Service\TrustpilotConfigurationService;
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\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class EcocodeTrustpilot extends Plugin
{
const INVITATION_SERVICE_SENT_COLUMN_NAME = 'trustpilot_invitation_sent';
const INVITATION_PRODUCT_SENT_COLUMN_NAME = 'trustpilot_invitation_product_sent';
const INVITATION_SERVICE_SENT_AT_COLUMN_NAME = 'trustpilot_invitation_service_sent_at';
const INVITATION_PRODUCT_SENT_AT_COLUMN_NAME = 'trustpilot_invitation_product_sent_at';
const INVITATION_BUSINESS_UNIT_COLUMN_NAME = 'trustpilot_invitation_business_unit';
const INVITATION_TRANSMITTED_AT_COLUMN_NAME = 'trustpilot_invitation_transmitted_at';
// fixed ids so we can reliable reuse kept user data
const CUSTOM_FIELDSET_NAME = 'ecocodeTrustpilot';
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$context = $installContext->getContext();
try {
$this->createCustomFields($context);
} catch (UniqueConstraintViolationException $exception) {
// custom fieldset already present
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
$context = $uninstallContext->getContext();
if (!$uninstallContext->keepUserData()) {
// cleanup is requested so cleanup configuration
/** @var SystemConfigService $configService */
$configService = $this->container->get(SystemConfigService::class);
foreach (TrustpilotConfigurationService::CONFIG_KEYS as $key) {
$configService->delete($key);
}
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetId = $customFieldSetRepository->searchIds(
(new Criteria())
->addFilter(new EqualsFilter('name', self::CUSTOM_FIELDSET_NAME)),
$uninstallContext
->getContext()
)->getIds();
//Get the Ids from the fetched Entities
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $customFieldSetId);
$customFieldSetRepository->delete($ids, $context);
}
}
protected function createCustomFields(Context $context): void
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create(
[
[
'name' => 'ecocodeTrustpilot',
'global' => true,
'config' => [
'label' => [
'de-DE' => 'Trustpilot',
'en-GB' => 'Trustpilot'
]
],
'relations' => [
[
'entityName' => 'order'
]
],
'customFields' => [
[
'name' => self::INVITATION_SERVICE_SENT_COLUMN_NAME,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'label' => ['en-GB' => 'Service Invitation sent'],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1
]
],
[
'name' => self::INVITATION_SERVICE_SENT_AT_COLUMN_NAME,
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'date',
'label' => ['en-GB' => 'Service Invitation sent date'],
'dateType' => 'datetime',
'componentName' => 'sw-field',
'customFieldType' => 'date',
'customFieldPosition' => 2
]
],
[
'name' => self::INVITATION_TRANSMITTED_AT_COLUMN_NAME,
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'date',
'label' => ['en-GB' => 'Transmission date'],
'dateType' => 'datetime',
'componentName' => 'sw-field',
'customFieldType' => 'date',
'customFieldPosition' => 6
]
],
[
'name' => self::INVITATION_PRODUCT_SENT_COLUMN_NAME,
'type' => CustomFieldTypes::BOOL,
'config' => [
'type' => 'checkbox',
'label' => ['en-GB' => 'Product Invitation sent'],
'componentName' => 'sw-field',
'customFieldType' => 'checkbox',
'customFieldPosition' => 3
]
],
[
'name' => self::INVITATION_PRODUCT_SENT_AT_COLUMN_NAME,
'type' => CustomFieldTypes::DATETIME,
'config' => [
'type' => 'date',
'label' => ['en-GB' => 'Product Invitation sent date'],
'dateType' => 'datetime',
'componentName' => 'sw-field',
'customFieldType' => 'date',
'customFieldPosition' => 4
]
],
[
'name' => self::INVITATION_BUSINESS_UNIT_COLUMN_NAME,
'type' => CustomFieldTypes::TEXT,
'config' => [
'type' => 'text',
'label' => ['en-GB' => 'Invitation Businessunit'],
'placeholder' => ['en-GB' => '-'],
'componentName' => 'sw-field',
'customFieldType' => 'text',
'customFieldPosition' => 5
]
],
]
]
],
$context
);
}
}