custom/plugins/EcocodeTrustpilot/src/EcocodeTrustpilot.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ecocode\Trustpilot;
  4. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  5. use Ecocode\Trustpilot\Service\TrustpilotConfigurationService;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. class EcocodeTrustpilot extends Plugin
  16. {
  17.     const INVITATION_SERVICE_SENT_COLUMN_NAME    'trustpilot_invitation_sent';
  18.     const INVITATION_PRODUCT_SENT_COLUMN_NAME    'trustpilot_invitation_product_sent';
  19.     const INVITATION_SERVICE_SENT_AT_COLUMN_NAME 'trustpilot_invitation_service_sent_at';
  20.     const INVITATION_PRODUCT_SENT_AT_COLUMN_NAME 'trustpilot_invitation_product_sent_at';
  21.     const INVITATION_BUSINESS_UNIT_COLUMN_NAME   'trustpilot_invitation_business_unit';
  22.     const INVITATION_TRANSMITTED_AT_COLUMN_NAME  'trustpilot_invitation_transmitted_at';
  23.     // fixed ids so we can reliable reuse kept user data
  24.     const CUSTOM_FIELDSET_NAME          'ecocodeTrustpilot';
  25.     public function install(InstallContext $installContext): void
  26.     {
  27.         parent::install($installContext);
  28.         $context $installContext->getContext();
  29.         try {
  30.             $this->createCustomFields($context);
  31.         } catch (UniqueConstraintViolationException $exception) {
  32.             // custom fieldset already present
  33.         }
  34.     }
  35.     public function uninstall(UninstallContext $uninstallContext): void
  36.     {
  37.         parent::uninstall($uninstallContext);
  38.         $context $uninstallContext->getContext();
  39.         if (!$uninstallContext->keepUserData()) {
  40.             // cleanup is requested so cleanup configuration
  41.             /** @var SystemConfigService $configService */
  42.             $configService $this->container->get(SystemConfigService::class);
  43.             foreach (TrustpilotConfigurationService::CONFIG_KEYS as $key) {
  44.                 $configService->delete($key);
  45.             }
  46.             /** @var EntityRepositoryInterface $customFieldSetRepository */
  47.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  48.             $customFieldSetId $customFieldSetRepository->searchIds(
  49.                 (new Criteria())
  50.                     ->addFilter(new EqualsFilter('name'self::CUSTOM_FIELDSET_NAME)),
  51.                 $uninstallContext
  52.                     ->getContext()
  53.             )->getIds();
  54.             //Get the Ids from the fetched Entities
  55.             $ids array_map(static function ($id) {
  56.                 return ['id' => $id];
  57.             }, $customFieldSetId);
  58.             $customFieldSetRepository->delete($ids$context);
  59.         }
  60.     }
  61.     protected function createCustomFields(Context $context): void
  62.     {
  63.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  64.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  65.         $customFieldSetRepository->create(
  66.             [
  67.                 [
  68.                     'name'         => 'ecocodeTrustpilot',
  69.                     'global'       => true,
  70.                     'config'       => [
  71.                         'label' => [
  72.                             'de-DE' => 'Trustpilot',
  73.                             'en-GB' => 'Trustpilot'
  74.                         ]
  75.                     ],
  76.                     'relations'    => [
  77.                         [
  78.                             'entityName' => 'order'
  79.                         ]
  80.                     ],
  81.                     'customFields' => [
  82.                         [
  83.                             'name'   => self::INVITATION_SERVICE_SENT_COLUMN_NAME,
  84.                             'type'   => CustomFieldTypes::BOOL,
  85.                             'config' => [
  86.                                 'type'                => 'checkbox',
  87.                                 'label'               => ['en-GB' => 'Service Invitation sent'],
  88.                                 'componentName'       => 'sw-field',
  89.                                 'customFieldType'     => 'checkbox',
  90.                                 'customFieldPosition' => 1
  91.                             ]
  92.                         ],
  93.                         [
  94.                             'name'   => self::INVITATION_SERVICE_SENT_AT_COLUMN_NAME,
  95.                             'type'   => CustomFieldTypes::DATETIME,
  96.                             'config' => [
  97.                                 'type'                => 'date',
  98.                                 'label'               => ['en-GB' => 'Service Invitation sent date'],
  99.                                 'dateType'            => 'datetime',
  100.                                 'componentName'       => 'sw-field',
  101.                                 'customFieldType'     => 'date',
  102.                                 'customFieldPosition' => 2
  103.                             ]
  104.                         ],
  105.                         [
  106.                             'name'   => self::INVITATION_TRANSMITTED_AT_COLUMN_NAME,
  107.                             'type'   => CustomFieldTypes::DATETIME,
  108.                             'config' => [
  109.                                 'type'                => 'date',
  110.                                 'label'               => ['en-GB' => 'Transmission date'],
  111.                                 'dateType'            => 'datetime',
  112.                                 'componentName'       => 'sw-field',
  113.                                 'customFieldType'     => 'date',
  114.                                 'customFieldPosition' => 6
  115.                             ]
  116.                         ],
  117.                         [
  118.                             'name'   => self::INVITATION_PRODUCT_SENT_COLUMN_NAME,
  119.                             'type'   => CustomFieldTypes::BOOL,
  120.                             'config' => [
  121.                                 'type'                => 'checkbox',
  122.                                 'label'               => ['en-GB' => 'Product Invitation sent'],
  123.                                 'componentName'       => 'sw-field',
  124.                                 'customFieldType'     => 'checkbox',
  125.                                 'customFieldPosition' => 3
  126.                             ]
  127.                         ],
  128.                         [
  129.                             'name'   => self::INVITATION_PRODUCT_SENT_AT_COLUMN_NAME,
  130.                             'type'   => CustomFieldTypes::DATETIME,
  131.                             'config' => [
  132.                                 'type'                => 'date',
  133.                                 'label'               => ['en-GB' => 'Product Invitation sent date'],
  134.                                 'dateType'            => 'datetime',
  135.                                 'componentName'       => 'sw-field',
  136.                                 'customFieldType'     => 'date',
  137.                                 'customFieldPosition' => 4
  138.                             ]
  139.                         ],
  140.                         [
  141.                             'name'   => self::INVITATION_BUSINESS_UNIT_COLUMN_NAME,
  142.                             'type'   => CustomFieldTypes::TEXT,
  143.                             'config' => [
  144.                                 'type'                => 'text',
  145.                                 'label'               => ['en-GB' => 'Invitation Businessunit'],
  146.                                 'placeholder'         => ['en-GB' => '-'],
  147.                                 'componentName'       => 'sw-field',
  148.                                 'customFieldType'     => 'text',
  149.                                 'customFieldPosition' => 5
  150.                             ]
  151.                         ],
  152.                     ]
  153.                 ]
  154.             ],
  155.             $context
  156.         );
  157.     }
  158. }