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

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. use Cbax\ModulExternalOrder\Bootstrap\Assets as ExternalOrderAssets;
  23. class CbaxModulExternalOrder extends Plugin
  24. {
  25.     private function getServices($step null) {
  26.         $services = array();
  27.         /* Standard Services */
  28.         $services['systemConfigService'] = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  29.         $services['systemConfigRepository'] = $this->container->get('system_config.repository');
  30.         $services['connectionService'] =  $this->container->get(Connection::class);
  31.         $services['translator'] = $this->container->get('translator');
  32.         $services['pluginRepository'] = $this->container->get('plugin.repository');
  33.         $services['userRepository'] = $this->container->get('user.repository');
  34.         /* Attribute */
  35.         $services['customFieldSetRepository'] =  $this->container->get('custom_field_set.repository');
  36.         $services['customFieldSetRelation'] =  $this->container->get('custom_field_set_relation.repository');
  37.         $services['customFieldRepository'] =  $this->container->get('custom_field.repository');
  38.         $services['snippetRepository'] =  $this->container->get('snippet.repository');
  39.         /* spezifische Services */
  40.         $services['databaseHelper'] = new DatabaseHelper();
  41.         $services['stateMachineRepository'] =  $this->container->get('state_machine.repository');
  42.         $services['stateMachineStateRepository'] =  $this->container->get('state_machine_state.repository');
  43.         if ($step === 'activate' || $step === 'update') {
  44.             $services['externalOrderAssets'] =  $this->container->get(ExternalOrderAssets::class);
  45.         }
  46.         return $services;
  47.     }
  48.     public function install(InstallContext $context): void
  49.     {
  50.         $services $this->getServices();
  51.         // Attribut Felder anlegen
  52.         $builder = new Attributes();
  53.         $builder->install($services$context);
  54.         parent::install($context);
  55.     }
  56.     public function preuninstall (UninstallContext $context): void {
  57.     }
  58.     public function update(UpdateContext $context) : void {
  59.         $services $this->getServices('update');
  60.         $builder = new Attributes();
  61.         $builder->update($services$context);
  62.         $updater = new Updater();
  63.         $updater->updateDatabaseTables($services$context);
  64.         $updater->updateSystemConfig($services$context);
  65.         parent::update($context);
  66.     }
  67.     public function postUpdate(UpdateContext $context) : void {
  68.         $services $this->getServices();
  69.         $updater = new Updater();
  70.         $updater->afterUpdateSteps($services$context);
  71.     }
  72.     public function uninstall(UninstallContext $context): void
  73.     {
  74.         if ($context->keepUserData()) {
  75.             parent::uninstall($context);
  76.             return;
  77.         }
  78.         $services $this->getServices();
  79.         // Attribut Felder löschen
  80.         $builder = new Attributes();
  81.         $builder->uninstall($services$context);
  82.         // Datenbank Tabellen löschen
  83.         $db = new Uninstaller();
  84.         $db->removeDatabaseTables($services);
  85.         $this->removePluginConfig($services$context->getContext());
  86.         parent::uninstall($context);
  87.     }
  88.     public function activate(ActivateContext $context): void
  89.     {
  90.         $services $this->getServices('activate');
  91.         // Attribut Felder einblenden
  92.         $builder = new Attributes();
  93.         $builder->activate($services$context);
  94.         // Standard-Werte
  95.         $builder = new DefaultConfig();
  96.         $builder->activate($services$this->getName(), $context->getContext());
  97.         // Marktplätze Tabelle befüllen
  98.         $settingsTableInitValues Helper::getSettingsTableInitValues();
  99.         Helper::setSettingsTableInitValues($settingsTableInitValues$services['connectionService']);
  100.         // Marktplatzbilder
  101.         $services['externalOrderAssets']->installImages($services$context);
  102.         $updater = new Updater();
  103.         $updater->activateNotice($services);
  104.         parent::activate($context);
  105.     }
  106.     public function deactivate(DeactivateContext $context): void
  107.     {
  108.         $services $this->getServices();
  109.         $this->checkIfConnectorsAreUninstalled($services$context);
  110.         // Attribut Felder ausblenden
  111.         $builder = new Attributes();
  112.         $builder->deactivate($services$context);
  113.         // Migration1572516634InsertCbaxMailTemplate Datenbankeintrag löschen
  114.         $db = new Uninstaller();
  115.         $db->removeDatabaseRow($services);
  116.         parent::deactivate($context);
  117.     }
  118.     private function removePluginConfig($services$context)
  119.     {
  120.         $systemConfigRepository $services['systemConfigRepository'];
  121.         $criteria = new Criteria();
  122.         $criteria->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  123.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  124.         $ids array_map(static function ($id) {
  125.             return ['id' => $id];
  126.         }, $idSearchResult->getIds());
  127.         if ($ids === []) {
  128.             return;
  129.         }
  130.         $systemConfigRepository->delete($ids$context);
  131.     }
  132.     /**
  133.      * wenn noch nicht alle Importe deinstalliert sind, wird eine Fehlermeldung mit den noch installierten Importen geworfen
  134.      * @param $services
  135.      * @param $context
  136.      * @throws PluginCustomError
  137.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  138.      */
  139.     private function checkIfConnectorsAreUninstalled($services$context) {
  140.         $locale Helper::getLocaleFromContext($services$context->getContext());
  141.         $repository $services['pluginRepository'];
  142.         $criteria = new Criteria();
  143.         $criteria->addFilter(new ContainsFilter('name''cbax'));
  144.         $criteria->addFilter(new ContainsFilter('name''orderimport'));
  145.         $criteria->addFilter(new NotFilter(
  146.             NotFilter::CONNECTION_AND,
  147.             [new EqualsFilter('installedAt'NULL)]
  148.         ));
  149.         $criteria->addAssociation('translations');
  150.         $criteria->addAssociation('translations.language');
  151.         $pluginsResult $repository->search($criteria$context->getContext())->getElements();
  152.         if (count($pluginsResult) > 0) {
  153.             $plugins = array();
  154.             foreach ($pluginsResult as $plugin) {
  155.                 $translations $plugin->get('translations')->getElements();
  156.                 foreach ($translations as $key => $translationItem) {
  157.                     if ($translationItem->get('language')->get('localeId') === $locale->getId()) {
  158.                         $plugins[$plugin->get('id')] = $translationItem->get('label');
  159.                     }
  160.                 }
  161.                 if (empty($plugins[$plugin->get('id')])) {
  162.                     $plugins[$plugin->get('id')] = $plugin->getTranslated()['label'];
  163.                 }
  164.             }
  165.             $code 'en-GB';
  166.             if ($locale->getCode() === 'de-DE') {
  167.                 $code 'de-DE';
  168.             }
  169.             $cbaxCatalog $services['translator']->getCatalogue($code)->all('cbax');
  170.             $message $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectorsFirst'];
  171.             $message .= '<br>';
  172.             $message .= $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectors'];
  173.             $message .= '<br><br>';
  174.             $message .= implode(', '$plugins);
  175.             throw new PluginCustomError($message);
  176.         }
  177.     }
  178. }