<?php declare(strict_types=1);
namespace Cbax\ModulManufacturers;
use Doctrine\DBAL\Connection;
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 Cbax\ModulManufacturers\Bootstrap\Attributes;
use Cbax\ModulManufacturers\Bootstrap\Updater;
use Cbax\ModulManufacturers\Bootstrap\Database;
use Cbax\ModulManufacturers\Bootstrap\DefaultConfig;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
//use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
class CbaxModulManufacturers extends Plugin
{
public function install(InstallContext $context): void
{
// Attribut Felder anlegen
$builder = new Attributes();
$builder->install($context, $this->container);
parent::install($context);
}
public function update(UpdateContext $context): void
{
// Attribut Felder anlegen
$builder = new Attributes();
$builder->update($context, $this->container);
$update = new Updater();
$update->updateScheduledTask($this->container, $context);
//Default CMS Pages erstellen
$services = $this->getServices();
$db = new Database($context->getContext()->getLanguageId());
$db->createDefaultManufacturersCmsPages($services, $context->getContext());
$db->updateManufacturersCmsPages($services, $context);
//Default CMS Pages in config setzen
$dc = new DefaultConfig();
$dc->activate($services, $context->getContext());
parent::update($context);
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
parent::uninstall($context);
return;
}
// Attribut Felder löschen
$builder = new Attributes();
$builder->uninstall($context, $this->container);
//CMS Pages löschen
$services = $this->getServices();
$db = new Database($context->getContext()->getLanguageId());
$db->deleteManufacturersCmsPages($services, $context->getContext());
$this->removePluginConfig($context->getContext());
parent::uninstall($context);
}
public function activate(ActivateContext $context): void
{
// Attribut Felder set checken
$builder = new Attributes();
$builder->activate($context, $this->container);
//Default CMS Pages erstellen
$services = $this->getServices();
$db = new Database($context->getContext()->getLanguageId());
$db->createDefaultManufacturersCmsPages($services, $context->getContext());
$db->updateManufacturersCmsPages($services, $context);
//Default CMS Pages in config setzen
$dc = new DefaultConfig();
$dc->activate($services, $context->getContext());
parent::activate($context);
}
public function deactivate(DeactivateContext $context): void
{
// Attribut Felder ausblenden
//$builder = new Attributes();
//$builder->deactivate($context, $this->container);
parent::deactivate($context);
}
private function removePluginConfig($context)
{
$systemConfigRepository = $this->container->get('system_config.repository');
$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);
}
private function getServices() {
$services = array();
/* Standard Services */
$services['systemConfigService'] = $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
$services['connectionService'] = $this->container->get(Connection::class);
$services['languageRepository'] = $this->container->get('language.repository');
$services['cmsPageRepository'] = $this->container->get('cms_page.repository');
$services['cmsSlotRepository'] = $this->container->get('cms_slot.repository');
return $services;
}
}