<?php declare(strict_types=1);
namespace Cbax\ModulManufacturers;
use Doctrine\DBAL\Connection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
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;
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);
// den alten 'Topslider' updaten
$update->update($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);
$services = $this->getServices();
$db = new Database($context->getContext()->getLanguageId());
// CMS Slots löschen
$db->deleteManufacturersCmsSlots($services, $context->getContext());
// CMS Blocks löschen
$db->deleteManufacturersCmsBlocks($services, $context->getContext());
// CMS Pages löschen
$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['cmsBlockRepository'] = $this->container->get('cms_block.repository');
$services['cmsSlotRepository'] = $this->container->get('cms_slot.repository');
$services['cmsSlotTranslationRepository'] = $this->container->get('cms_slot_translation.repository');
return $services;
}
/**
* Load services.xml to add cms data resolver services
* @param ContainerBuilder $container
* @throws \Exception
*/
public function build(ContainerBuilder $container): void
{
parent::build($container);
$loader = new XmlFileLoader($container, new FileLocator($this->getPath() . '/Core/Content/DependencyInjection'));
$loader->load('services.xml');
}
}