custom/plugins/CbaxModulExternalOrder/src/CbaxModulExternalOrder.php line 30

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulExternalOrder;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  5. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  9. use Doctrine\DBAL\Connection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  15. use Cbax\ModulExternalOrder\Bootstrap\Attributes;
  16. use Cbax\ModulExternalOrder\Bootstrap\DefaultConfig;
  17. use Cbax\ModulExternalOrder\Bootstrap\Updater;
  18. use Cbax\ModulExternalOrder\Bootstrap\Uninstaller;
  19. use Cbax\ModulExternalOrder\Components\DatabaseHelper;
  20. use Cbax\ModulExternalOrder\Components\Helper;
  21. use Cbax\ModulExternalOrder\Core\Exception\PluginCustomError;
  22. class CbaxModulExternalOrder extends Plugin
  23. {
  24.     private function getServices() {
  25.         $services = array();
  26.         /* Standard Services */
  27.         $services['systemConfigService'] = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  28.         $services['systemConfigRepository'] = $this->container->get('system_config.repository');
  29.         $services['connectionService'] =  $this->container->get(Connection::class);
  30.         $services['translator'] = $this->container->get('translator');
  31.         $services['pluginRepository'] = $this->container->get('plugin.repository');
  32.         $services['userRepository'] = $this->container->get('user.repository');
  33.         /* Attribute */
  34.         $services['customFieldSetRepository'] =  $this->container->get('custom_field_set.repository');
  35.         $services['customFieldSetRelation'] =  $this->container->get('custom_field_set_relation.repository');
  36.         $services['customFieldRepository'] =  $this->container->get('custom_field.repository');
  37.         $services['snippetRepository'] =  $this->container->get('snippet.repository');
  38.         /* spezifische Services */
  39.         $services['databaseHelper'] = new DatabaseHelper();
  40.         $services['stateMachineRepository'] =  $this->container->get('state_machine.repository');
  41.         $services['stateMachineStateRepository'] =  $this->container->get('state_machine_state.repository');
  42.         return $services;
  43.     }
  44.     public function install(InstallContext $context): void
  45.     {
  46.         $services $this->getServices();
  47.         // Attribut Felder anlegen
  48.         $builder = new Attributes();
  49.         $builder->install($services$context);
  50.         parent::install($context);
  51.     }
  52.     public function preuninstall (UninstallContext $context): void {
  53.     }
  54.     public function update(UpdateContext $context) : void {
  55.         $services $this->getServices();
  56.         $builder = new Attributes();
  57.         $builder->update($services$context);
  58.         $updater = new Updater();
  59.         $updater->updateDatabaseTables($services$context);
  60.         $updater->updateSystemConfig($services$context);
  61.         parent::update($context);
  62.     }
  63.     public function postUpdate(UpdateContext $context) : void {
  64.         $services $this->getServices();
  65.         
  66.         $updater = new Updater();
  67.         $updater->afterUpdateSteps($services$context);
  68.     }
  69.     public function uninstall(UninstallContext $context): void
  70.     {
  71.         if ($context->keepUserData()) {
  72.             parent::uninstall($context);
  73.             return;
  74.         }
  75.         $services $this->getServices();
  76.         // Attribut Felder löschen
  77.         $builder = new Attributes();
  78.         $builder->uninstall($services$context);
  79.         // Datenbank Tabellen löschen
  80.         $db = new Uninstaller();
  81.         $db->removeDatabaseTables($services);
  82.         $this->removePluginConfig($services$context->getContext());
  83.         parent::uninstall($context);
  84.     }
  85.     public function activate(ActivateContext $context): void
  86.     {
  87.         $services $this->getServices();
  88.         // Attribut Felder einblenden
  89.         $builder = new Attributes();
  90.         $builder->activate($services$context);
  91.         $builder = new DefaultConfig();
  92.         $builder->activate($services$context->getContext());
  93.         parent::activate($context);
  94.     }
  95.     public function deactivate(DeactivateContext $context): void
  96.     {
  97.         $services $this->getServices();
  98.         $this->checkIfConnectorsAreUninstalled($services$context);
  99.         // Attribut Felder ausblenden
  100.         $builder = new Attributes();
  101.         $builder->deactivate($services$context);
  102.         parent::deactivate($context);
  103.     }
  104.     private function removePluginConfig($services$context)
  105.     {
  106.         $systemConfigRepository $services['systemConfigRepository'];
  107.         $criteria = new Criteria();
  108.         $criteria->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  109.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  110.         $ids array_map(static function ($id) {
  111.             return ['id' => $id];
  112.         }, $idSearchResult->getIds());
  113.         if ($ids === []) {
  114.             return;
  115.         }
  116.         $systemConfigRepository->delete($ids$context);
  117.     }
  118.     /**
  119.      * wenn noch nicht alle Importe deinstalliert sind, wird eine Fehlermeldung mit den noch installierten Importen geworfen
  120.      * @param $services
  121.      * @param $context
  122.      * @throws PluginCustomError
  123.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  124.      */
  125.     private function checkIfConnectorsAreUninstalled($services$context) {
  126.         $locale Helper::getLocaleFromContext($services$context->getContext());
  127.         $repository $services['pluginRepository'];
  128.         $criteria = new Criteria();
  129.         $criteria->addFilter(new ContainsFilter('name''cbax'));
  130.         $criteria->addFilter(new ContainsFilter('name''orderimport'));
  131.         $criteria->addFilter(new NotFilter(
  132.             NotFilter::CONNECTION_AND,
  133.             [new EqualsFilter('installedAt'NULL)]
  134.         ));
  135.         $criteria->addAssociation('translations');
  136.         $criteria->addAssociation('translations.language');
  137.         $pluginsResult $repository->search($criteria$context->getContext())->getElements();
  138.         if (count($pluginsResult) > 0) {
  139.             $plugins = array();
  140.             foreach ($pluginsResult as $plugin) {
  141.                 $translations $plugin->get('translations')->getElements();
  142.                 foreach ($translations as $key => $translationItem) {
  143.                     if ($translationItem->get('language')->get('localeId') === $locale->getId()) {
  144.                         $plugins[] = $translationItem->get('label');
  145.                     }
  146.                 }
  147.             }
  148.             $cbaxCatalog $services['translator']->getCatalogue($locale->getCode())->all('cbax');
  149.             $message $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectorsFirst'];
  150.             $message .= '<br>';
  151.             $message .= $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectors'];
  152.             $message .= '<br><br>';
  153.             $message .= implode(', '$plugins);
  154.             throw new PluginCustomError($message);
  155.         }
  156.     }
  157. }