custom/plugins/AsbsTrustedShopsReviews/src/AsbsTrustedShopsReviews.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Asbs\TrustedShopsReviews;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class AsbsTrustedShopsReviews extends Plugin
  13. {
  14.     public function install(InstallContext $installContext): void
  15.     {
  16.         $this->createCustomFields($installContext->getContext());
  17.     }
  18.     public function uninstall(UninstallContext $uninstallContext): void
  19.     {
  20.         if ($uninstallContext->keepUserData()) {
  21.             return;
  22.         }
  23.         $this->removeCustomFields($uninstallContext->getContext());
  24.     }
  25.     private function removeCustomFields(Context $context): void
  26.     {
  27.         /** @var EntityRepository $customFieldSetRepository */
  28.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  29.         $criteria = new Criteria();
  30.         $criteria->addFilter(new EqualsFilter('name''asbs_trustedshops_reviews'));
  31.         $customFieldSetId $customFieldSetRepository->searchIds($criteria$context)->firstId();
  32.         if ($customFieldSetId) {
  33.             $customFieldSetRepository->delete([
  34.                 [
  35.                     'id' => $customFieldSetId
  36.                 ]
  37.             ], $context);
  38.         }
  39.     }
  40.     private function createCustomFields(Context $context): void
  41.     {
  42.         /** @var EntityRepository $customFieldSetRepository */
  43.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  44.         /** @var EntityRepository $customFieldRepository */
  45.         $customFieldRepository $this->container->get('custom_field.repository');
  46.         // First create the custom field set
  47.         $customFieldSetId null;
  48.         $criteria = new Criteria();
  49.         $criteria->addFilter(new EqualsFilter('name''asbs_trustedshops_reviews'));
  50.         $existingSet $customFieldSetRepository->search($criteria$context)->first();
  51.         if ($existingSet) {
  52.             $customFieldSetId $existingSet->getId();
  53.         } else {
  54.             $result $customFieldSetRepository->create([
  55.                 [
  56.                     'name' => 'asbs_trustedshops_reviews',
  57.                     'config' => [
  58.                         'label' => [
  59.                             'de-DE' => 'TrustedShops Bewertungen',
  60.                             'en-GB' => 'TrustedShops Reviews'
  61.                         ]
  62.                     ],
  63.                     'relations' => [
  64.                         [
  65.                             'entityName' => 'order'
  66.                         ]
  67.                     ]
  68.                 ]
  69.             ], $context);
  70.             $customFieldSetId $result->getEventByEntityName('custom_field_set')->getIds()[0];
  71.         }
  72.         // Then create individual custom fields
  73.         $customFieldRepository->upsert([
  74.             [
  75.                 'name' => 'asbs_review_email_sent',
  76.                 'type' => CustomFieldTypes::BOOL,
  77.                 'setId' => $customFieldSetId,
  78.                 'config' => [
  79.                     'label' => [
  80.                         'de-DE' => 'Bewertungsmail versendet',
  81.                         'en-GB' => 'Review email sent'
  82.                     ],
  83.                     'helpText' => [
  84.                         'de-DE' => 'Zeigt an, ob bereits eine TrustedShops Bewertungsmail für diese Bestellung versendet wurde',
  85.                         'en-GB' => 'Indicates whether a TrustedShops review email has been sent for this order'
  86.                     ],
  87.                     'componentName' => 'sw-field',
  88.                     'customFieldType' => 'checkbox'
  89.                 ]
  90.             ],
  91.             [
  92.                 'name' => 'asbs_review_email_sent_at',
  93.                 'type' => CustomFieldTypes::DATETIME,
  94.                 'setId' => $customFieldSetId,
  95.                 'config' => [
  96.                     'label' => [
  97.                         'de-DE' => 'Bewertungsmail versendet am',
  98.                         'en-GB' => 'Review email sent at'
  99.                     ],
  100.                     'helpText' => [
  101.                         'de-DE' => 'Zeitpunkt, zu dem die TrustedShops Bewertungsmail versendet wurde',
  102.                         'en-GB' => 'Timestamp when the TrustedShops review email was sent'
  103.                     ],
  104.                     'componentName' => 'sw-field',
  105.                     'customFieldType' => 'datetime'
  106.                 ]
  107.             ]
  108.         ], $context);
  109.     }
  110. }