custom/plugins/confirado-apto-shopware6-connector-14fd856cf11e/src/AptoShopware6Connector.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cnf\AptoShopware6Connector;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  14. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  15. class AptoShopware6Connector extends Plugin
  16. {
  17.     /**
  18.      * @param InstallContext $installContext
  19.      * @throws InconsistentCriteriaIdsException
  20.      */
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         parent::install($installContext);
  24.         $this->initDefaults($installContext->getContext());
  25.         $this->addCustomFields($installContext->getContext());
  26.     }
  27.     /**
  28.      * @param ActivateContext $activateContext
  29.      */
  30.     public function activate(ActivateContext $activateContext): void
  31.     {
  32.         parent::activate($activateContext);
  33.     }
  34.     /**
  35.      * @param DeactivateContext $deactivateContext
  36.      * @throws InconsistentCriteriaIdsException
  37.      */
  38.     public function deactivate(DeactivateContext $deactivateContext): void
  39.     {
  40.         $this->repairCustomFields($deactivateContext->getContext());
  41.         parent::deactivate($deactivateContext);
  42.     }
  43.     /**
  44.      * @param UninstallContext $uninstallContext
  45.      */
  46.     public function uninstall(UninstallContext $uninstallContext): void
  47.     {
  48.         $this->removeCustomFields($uninstallContext->getContext());
  49.         parent::uninstall($uninstallContext);
  50.     }
  51.     /**
  52.      * @param Context $context
  53.      */
  54.     private function removeCustomFields(Context $context)
  55.     {
  56.         /** @var EntityRepository $customFieldSetRepository */
  57.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  58.         $customFieldSetId $this->getCustomFieldSetId('cnf_apto_connector_set'$context$customFieldSetRepository);
  59.         if ($customFieldSetId !== null) {
  60.             $customFieldSetRepository->delete([['id' => $customFieldSetId]], $context);
  61.         }
  62.     }
  63.     /**
  64.      * @param Context $context
  65.      * @throws InconsistentCriteriaIdsException
  66.      */
  67.     private function addCustomFields(Context $context) : void
  68.     {
  69.         /** @var EntityRepository $customFieldSetRepository */
  70.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  71.         /** todo: Lock Customfields in Backend */
  72.         $customFields = [];
  73.         $newCustomFieldSet $this->makeCustomFieldSet(
  74.             'cnf_apto_connector_set',
  75.             true,
  76.             [
  77.                 'en-GB' => 'Apto Connector',
  78.                 'de-DE' => 'Apto Connector'
  79.             ],
  80.             [
  81.                 [
  82.                 'entityName' => 'product'
  83.                 ]
  84.             ]
  85.         );
  86.         $customFields[] = $newCustomFieldSet;
  87.         foreach ($customFields as $customFieldSet) {
  88.             $customFieldSetId $this->getCustomFieldSetId($customFieldSet['name'], $context$customFieldSetRepository);
  89.             if ($customFieldSetId !== null) {
  90.                 $customFieldSetRepository->delete([['id' => $customFieldSetId]], $context);
  91.             }
  92.             if (!$this->checkIfCustomFieldExists('cnf_apto_connector_enable_apto'$context$customFieldSetRepository)) {
  93.                 $this->addCustomField(
  94.                     $customFieldSet,
  95.                     'cnf_apto_connector_enable_apto',
  96.                     \Shopware\Core\System\CustomField\CustomFieldTypes::BOOL,
  97.                     [
  98.                         'en-GB' => 'enable Apto.ONE',
  99.                         'de-DE' => 'Apto.ONE benutzen'
  100.                     ],1
  101.                 );
  102.             }
  103.             if (!$this->checkIfCustomFieldExists('cnf_apto_connector_use_inline'$context$customFieldSetRepository)) {
  104.                 $this->addCustomField(
  105.                     $customFieldSet,
  106.                     'cnf_apto_connector_use_inline',
  107.                     \Shopware\Core\System\CustomField\CustomFieldTypes::BOOL,
  108.                     [
  109.                         'en-GB' => 'include Apto.ONE on product page',
  110.                         'de-DE' => 'Apto.ONE direkt auf Produktseite anzeigen'
  111.                     ], 2
  112.                 );
  113.             }
  114.             if (!$this->checkIfCustomFieldExists('cnf_apto_connector_product_id'$context$customFieldSetRepository)) {
  115.                 $this->addCustomField(
  116.                     $customFieldSet,
  117.                     'cnf_apto_connector_product_id',
  118.                     \Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
  119.                     [
  120.                         'en-GB' => 'Apto.ONE product id or product-url',
  121.                         'de-DE' => 'Apto.ONE Produkt-ID oder Produkt-URL',
  122.                     ], 3
  123.                 );
  124.             }
  125.             if (!$this->checkIfCustomFieldExists('cnf_apto_connector_config_id'$context$customFieldSetRepository)) {
  126.                 $this->addCustomField(
  127.                     $customFieldSet,
  128.                     'cnf_apto_connector_config_id',
  129.                     \Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
  130.                     [
  131.                         'en-GB' => 'Apto.ONE config id',
  132.                         'de-DE' => 'Apto.ONE Config-ID',
  133.                     ], 4
  134.                 );
  135.             }
  136.             $customFieldSetRepository->create([$customFieldSet], $context);
  137.         }
  138.     }
  139.     /**
  140.      * @param Context $context
  141.      * @throws InconsistentCriteriaIdsException
  142.      */
  143.     private function repairCustomFields(Context $context): void
  144.     {
  145.         //TODO: need a proper repair function or switch from custom fields to a custom model extension
  146.         //$this->addCustomFields($context);
  147.     }
  148.     /**
  149.      * @param string $name
  150.      * @param bool $active
  151.      * @param array $labels
  152.      * @param array $relations
  153.      * @return array
  154.      */
  155.     private function makeCustomFieldSet(string $namebool $active, array $labels, array $relations): array
  156.     {
  157.         return [
  158.             'name' => $name,
  159.             'active' => $active,
  160.             'config' => [
  161.                 'label' => $labels,
  162.             ],
  163.             'customFields' => [],
  164.             'relations' => $relations
  165.         ];
  166.     }
  167.     /**
  168.      * @param array $fieldSet
  169.      * @param string $name
  170.      * @param string $type
  171.      * @param array $labels
  172.      * @param int $position
  173.      */
  174.     private function addCustomField(array &$fieldSetstring $namestring $type, array $labelsint $position 1): void
  175.     {
  176.         $fieldSet['customFields'][] = [
  177.             'name' => $name,
  178.             'type' => $type,
  179.             'config' => [
  180.                 'label' => $labels,
  181.                 'customFieldPosition' => $position,
  182.             ]
  183.         ];
  184.     }
  185.     /**
  186.      * @param string $name
  187.      * @param Context $context
  188.      * @param EntityRepository $customFieldSetRepository
  189.      * @return bool
  190.      * @throws InconsistentCriteriaIdsException
  191.      */
  192.     private function checkIfCustomFieldExists(string $nameContext $contextEntityRepository $customFieldSetRepository): bool
  193.     {
  194.         $criteria = new Criteria();
  195.         $criteria->addAssociation('customFields');
  196.         $customFieldSetsFound $customFieldSetRepository->search($criteria$context);
  197.         if ($customFieldSetsFound->getTotal() > 0) {
  198.             $customFieldSetsFound $customFieldSetsFound->getEntities();
  199.             /** @var CustomFieldSetEntity $customFieldSetFound */
  200.             foreach ($customFieldSetsFound->getElements() as $customFieldSetFound) {
  201.                 foreach ($customFieldSetFound->getCustomFields() as $customField )
  202.                     if ($customField->getName() === $name) {
  203.                         return true;
  204.                     }
  205.             }
  206.         }
  207.         return false;
  208.     }
  209.     /**
  210.      * @param string $name
  211.      * @param Context $context
  212.      * @param EntityRepository $customFieldSetRepository
  213.      * @return string|null
  214.      */
  215.     private function getCustomFieldSetId(string $nameContext $contextEntityRepository $customFieldSetRepository)
  216.     {
  217.         $criteria = new Criteria();
  218.         $criteria->addFilter(
  219.             new EqualsFilter('name'$name)
  220.         );
  221.         $customFieldSetsFound $customFieldSetRepository->search($criteria$context);
  222.         if ($customFieldSetsFound->getTotal() > 0) {
  223.             $customFieldSetsFound $customFieldSetsFound->getEntities();
  224.             /** @var CustomFieldSetEntity $customFieldSetFound */
  225.             foreach ($customFieldSetsFound->getElements() as $customFieldSetFound) {
  226.                 return $customFieldSetFound->getId();
  227.             }
  228.         }
  229.         return null;
  230.     }
  231.     /**
  232.      * @param Context $context
  233.      * @throws InconsistentCriteriaIdsException
  234.      */
  235.     private function initDefaults(Context $context): void
  236.     {
  237.         $this->setDefaultConfigValue(
  238.             $context,
  239.             'AptoShopware6Connector.config.securityToken',
  240.             Uuid::randomHex(),
  241.             false
  242.         );
  243.     }
  244.     /**
  245.      * @param Context $context
  246.      * @param string $key
  247.      * @param $value
  248.      * @param bool $renew
  249.      */
  250.     private function setDefaultConfigValue(Context $contextstring $key$valuebool $renew false): void
  251.     {
  252.         $data = [
  253.             'configurationKey' => $key,
  254.             'configurationValue' => $value
  255.         ];
  256.         /** @var EntityRepository $repository */
  257.         $repository $this->container->get('system_config.repository');
  258.         $criteria = new Criteria();
  259.         $criteria->addFilter(
  260.             new EqualsFilter('configurationKey'$key)
  261.         );
  262.         $config $repository->search($criteria$context);
  263.         if ($config->getTotal() === 0) {
  264.             $repository->create([$data], $context);
  265.             return;
  266.         }
  267.         if ($config->getTotal() > && $renew === true) {
  268.             $data['id'] = $config->first()->getId();
  269.             $repository->update([$data], $context);
  270.         }
  271.     }
  272. }