<?php declare(strict_types=1);
namespace Cbax\ModulExternalOrder;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Cbax\ModulExternalOrder\Bootstrap\Attributes;
use Cbax\ModulExternalOrder\Bootstrap\DefaultConfig;
use Cbax\ModulExternalOrder\Bootstrap\Updater;
use Cbax\ModulExternalOrder\Bootstrap\Uninstaller;
use Cbax\ModulExternalOrder\Components\DatabaseHelper;
use Cbax\ModulExternalOrder\Components\Helper;
use Cbax\ModulExternalOrder\Core\Exception\PluginCustomError;
use Cbax\ModulExternalOrder\Bootstrap\Assets as ExternalOrderAssets;
class CbaxModulExternalOrder extends Plugin
{
private function getServices($step = null) {
$services = array();
/* Standard Services */
$services['systemConfigService'] = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
$services['systemConfigRepository'] = $this->container->get('system_config.repository');
$services['connectionService'] = $this->container->get(Connection::class);
$services['translator'] = $this->container->get('translator');
$services['pluginRepository'] = $this->container->get('plugin.repository');
$services['userRepository'] = $this->container->get('user.repository');
/* Attribute */
$services['customFieldSetRepository'] = $this->container->get('custom_field_set.repository');
$services['customFieldSetRelation'] = $this->container->get('custom_field_set_relation.repository');
$services['customFieldRepository'] = $this->container->get('custom_field.repository');
$services['snippetRepository'] = $this->container->get('snippet.repository');
/* spezifische Services */
$services['databaseHelper'] = new DatabaseHelper();
$services['stateMachineRepository'] = $this->container->get('state_machine.repository');
$services['stateMachineStateRepository'] = $this->container->get('state_machine_state.repository');
if ($step === 'activate' || $step === 'update') {
$services['externalOrderAssets'] = $this->container->get(ExternalOrderAssets::class);
}
return $services;
}
public function install(InstallContext $context): void
{
$services = $this->getServices();
// Attribut Felder anlegen
$builder = new Attributes();
$builder->install($services, $context);
parent::install($context);
}
public function preuninstall (UninstallContext $context): void {
}
public function update(UpdateContext $context) : void {
$services = $this->getServices('update');
$builder = new Attributes();
$builder->update($services, $context);
$updater = new Updater();
$updater->updateDatabaseTables($services, $context);
$updater->updateSystemConfig($services, $context);
parent::update($context);
}
public function postUpdate(UpdateContext $context) : void {
$services = $this->getServices();
$updater = new Updater();
$updater->afterUpdateSteps($services, $context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
parent::uninstall($context);
return;
}
$services = $this->getServices();
// Attribut Felder löschen
$builder = new Attributes();
$builder->uninstall($services, $context);
// Datenbank Tabellen löschen
$db = new Uninstaller();
$db->removeDatabaseTables($services);
$this->removePluginConfig($services, $context->getContext());
parent::uninstall($context);
}
public function activate(ActivateContext $context): void
{
$services = $this->getServices('activate');
// Attribut Felder einblenden
$builder = new Attributes();
$builder->activate($services, $context);
// Standard-Werte
$builder = new DefaultConfig();
$builder->activate($services, $this->getName(), $context->getContext());
// Marktplätze Tabelle befüllen
$settingsTableInitValues = Helper::getSettingsTableInitValues();
Helper::setSettingsTableInitValues($settingsTableInitValues, $services['connectionService']);
// Marktplatzbilder
$services['externalOrderAssets']->installImages($services, $context);
$updater = new Updater();
$updater->activateNotice($services);
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
$services = $this->getServices();
$this->checkIfConnectorsAreUninstalled($services, $context);
// Attribut Felder ausblenden
$builder = new Attributes();
$builder->deactivate($services, $context);
// Migration1572516634InsertCbaxMailTemplate Datenbankeintrag löschen
$db = new Uninstaller();
$db->removeDatabaseRow($services);
parent::deactivate($context);
}
private function removePluginConfig($services, $context)
{
$systemConfigRepository = $services['systemConfigRepository'];
$criteria = new Criteria();
$criteria->addFilter(new ContainsFilter('configurationKey', $this->getName() . '.config.'));
$idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
$ids = array_map(static function ($id) {
return ['id' => $id];
}, $idSearchResult->getIds());
if ($ids === []) {
return;
}
$systemConfigRepository->delete($ids, $context);
}
/**
* wenn noch nicht alle Importe deinstalliert sind, wird eine Fehlermeldung mit den noch installierten Importen geworfen
* @param $services
* @param $context
* @throws PluginCustomError
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
*/
private function checkIfConnectorsAreUninstalled($services, $context) {
$locale = Helper::getLocaleFromContext($services, $context->getContext());
$repository = $services['pluginRepository'];
$criteria = new Criteria();
$criteria->addFilter(new ContainsFilter('name', 'cbax'));
$criteria->addFilter(new ContainsFilter('name', 'orderimport'));
$criteria->addFilter(new NotFilter(
NotFilter::CONNECTION_AND,
[new EqualsFilter('installedAt', NULL)]
));
$criteria->addAssociation('translations');
$criteria->addAssociation('translations.language');
$pluginsResult = $repository->search($criteria, $context->getContext())->getElements();
if (count($pluginsResult) > 0) {
$plugins = array();
foreach ($pluginsResult as $plugin) {
$translations = $plugin->get('translations')->getElements();
foreach ($translations as $key => $translationItem) {
if ($translationItem->get('language')->get('localeId') === $locale->getId()) {
$plugins[$plugin->get('id')] = $translationItem->get('label');
}
}
if (empty($plugins[$plugin->get('id')])) {
$plugins[$plugin->get('id')] = $plugin->getTranslated()['label'];
}
}
$code = 'en-GB';
if ($locale->getCode() === 'de-DE') {
$code = 'de-DE';
}
$cbaxCatalog = $services['translator']->getCatalogue($code)->all('cbax');
$message = $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectorsFirst'];
$message .= '<br>';
$message .= $cbaxCatalog['cbax-external-order.exceptions.errorUninstallConnectors'];
$message .= '<br><br>';
$message .= implode(', ', $plugins);
throw new PluginCustomError($message);
}
}
}