<?php declare(strict_types=1);
namespace SemesManufacturerPlus;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
class SemesManufacturerPlus extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCmsPages($installContext->getContext());
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
// clean everything up
// delete cms pages
$this->unlockCmsPage();
$this->deleteManufacturerPages($context->getContext());
// delete tag table
$this->deleteTagTable();
// delete translation table
$this->deleteTranslationTable();
// delete entity
$this->deleteEntityTable();
// delete seo url template(s)
$this->deleteSeoUrlTemplate($context->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext);
}
private function createCmsPages(Context $context): void
{
if ($this->checkForPluginData($context)) return;
$manufacutrerOverviewCmsPageId = Uuid::randomHex();
$manufacutrerDetailCmsPageId = Uuid::randomHex();
$cmsPage[] = $this->getOverviewCmsPageConfig($manufacutrerOverviewCmsPageId);
$cmsPage[] = $this->getDetailCmsPageConfig($manufacutrerDetailCmsPageId);
// save pages to repository
$this->writeCmsPages($cmsPage, $context);
// set default detail page to plugin config
$this->setConfigDetailPage($manufacutrerDetailCmsPageId);
// lock pages to avoid losing them due DAU
$this->lockCmsPage();
}
private function deleteManufacturerPages(Context $context): void
{
/** @var EntityRepository $cmsPageRepository */
$cmsPageRepository = $this->container->get('cms_page.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('type', 'semes_manufacturer_plus'));
$ids = $cmsPageRepository->searchIds($criteria, $context)->getIds();
$ids = \array_map(static function ($id) {
return ['id' => $id];
}, $ids);
$cmsPageRepository->delete($ids, $context);
}
private function deleteSeoUrlTemplate(Context $context): void
{
/** @var EntityRepository $seoUrlTemplateRepository */
$seoUrlTemplateRepository = $this->container->get('seo_url_template.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('routeName', 'frontend.semes.manufacturer.plus.detail'));
$ids = $seoUrlTemplateRepository->searchIds($criteria, $context)->getIds();
$ids = \array_map(static function ($id) {
return ['id' => $id];
}, $ids);
$seoUrlTemplateRepository->delete($ids, $context);
}
private function deleteTagTable(): void
{
$connection = $this->container->get(Connection::class);
$sql = "DROP TABLE IF EXISTS `semes_manufacturer_plus_tag`";
$connection->executeUpdate($sql);
}
private function deleteTranslationTable(): void
{
$connection = $this->container->get(Connection::class);
$sql = "DROP TABLE IF EXISTS `semes_manufacturer_plus_translation`";
$connection->executeUpdate($sql);
}
private function deleteEntityTable(): void
{
$connection = $this->container->get(Connection::class);
$sql = "DROP TABLE IF EXISTS `semes_manufacturer_plus`";
$connection->executeUpdate($sql);
}
/**
* default overview cms page
*/
private function getOverviewCmsPageConfig(string $id): array
{
$cmsPage = [
'id' => $id,
'type' => 'semes_manufacturer_plus',
"name"=> "Overview manufacturer",
'translations' => [
"de-DE" => [
"name"=> "Übersicht Hersteller"
]
],
'sections' => [
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 0,
'blocks' => [
[
'position' => 1,
'type' => 'text',
'name'=> 'Manufacturer Listing Text',
'marginTop' => '20px',
'marginBottom' => '20px',
'marginLeft' => '20px',
'marginRight' => '20px',
'slots' => [
[
'type' => 'text',
'slot' => 'content',
'config' => [
'content' => [
'source' => 'static',
'value' => '<div class="h2" style="text-align: center;">Lorem Ipsum dolor sit amet</div><hr><p style="text-align: center; margin-bottom: 40px">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>'
],
'verticalAlign' => [
'source' => 'static',
'value' => null
]
]
],
],
],
],
],
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 1,
'blocks' => [
[
'position' => 1,
'name'=> 'Manufacturer Listing',
'type' => 'manufacturer-plus-listing',
'marginTop' => '20px',
'marginBottom' => '20px',
'marginLeft' => '20px',
'marginRight' => '20px',
'slots' => [
['type' => 'manufacturer-plus-listing', 'slot' => 'listing'],
],
],
],
],
]
];
return $cmsPage;
}
/**
* default detail cms page
*/
private function getDetailCmsPageConfig(string $id): array
{
$cmsPage = [
'id' => $id,
'type' => 'semes_manufacturer_plus',
"name"=> "Detail manufacturer",
'translations' => [
"de-DE" => [
"name"=> "Detail Hersteller"
]
],
'sections' => [
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 0,
'blocks' => [
[
'position' => 1,
'type' => 'image-cover',
'name'=> 'Manufacturer Banner',
'slots' => [
[
'type' => 'image',
'slot' => 'image',
'config' => [
'media' => [
'value' => 'product_manufacturer.extensions.manufacturerPlus.bannerImageMedia',
'source' => 'mapped',
],
],
],
],
],
],
],
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 1,
'blocks' => [
[
'position' => 1,
'type' => 'text',
'name'=> 'Manufacturer Heading',
'marginTop' => '20px',
'marginBottom' => '20px',
'marginLeft' => '20px',
'marginRight' => '20px',
'slots' => [
[
'type' => 'text',
'slot' => 'content',
'config' => ['content' => ['source' => 'static', 'value' => '<h1>{{ product_manufacturer.name }}</h1> <p>{{ product_manufacturer.description }}</p>']]
],
],
],
],
],
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 2,
'blocks' => [
[
'position' => 1,
'type' => 'manufacturer-plus-product-listing-filter',
'name'=> 'Manufacturer Listing Filter',
'slots' => [
[
'type' => 'manufacturer-plus-product-listing-filter',
'slot' => 'content'
],
]
],
[
'position' => 2,
'type' => 'manufacturer-plus-product-listing',
'name'=> 'Manufacturer Listing Products',
'slots' => [
[
'type' => 'manufacturer-plus-product-listing',
'slot' => 'content'
],
],
],
],
],
[
'id' => Uuid::randomHex(),
'type' => 'default',
'position' => 3,
'blocks' => [
[
'position' => 1,
'type' => 'text',
'name'=> 'Manufacturer Footer',
'marginTop' => '20px',
'marginBottom' => '20px',
'marginLeft' => '20px',
'marginRight' => '20px',
'slots' => [
[
'type' => 'text',
'slot' => 'content',
'config' => ['content' => ['source' => 'static', 'value' => '<p>{{ product_manufacturer.extensions.manufacturerPlus.footerDescription }}</p>']]
],
],
],
],
],
]
];
return $cmsPage;
}
/**
* write pages to repository
*/
private function writeCmsPages($pages, $context): void
{
/** @var EntityRepository $cmsPageRepository */
$cmsPageRepository = $this->container->get('cms_page.repository');
$cmsPageRepository->create($pages, $context);
}
private function setConfigDetailPage($id): void
{
/** @var SystemConfigService $systemConfig */
$systemConfig = $this->container->get(SystemConfigService::class);
$systemConfig->set('SemesManufacturerPlus.config.cmsPageId', $id);
}
/**
* Setting CMS Page locked via DB Connection because it is not working via DAL
*/
private function lockCmsPage(): void
{
$connection = $this->container->get(Connection::class);
$sql = "UPDATE `cms_page` SET `locked` = 1 WHERE `type` = 'semes_manufacturer_plus' AND `updated_at` IS NULL;";
$connection->executeUpdate($sql);
}
/**
* Setting CMS Page unlocked via DB Connection because it is not working via DAL
*/
private function unlockCmsPage(): void
{
$connection = $this->container->get(Connection::class);
$sql = "UPDATE `cms_page` SET `locked` = 0 WHERE `type` = 'semes_manufacturer_plus'";
$connection->executeUpdate($sql);
}
/**
* check if plugin was installed previously to avoid adding default data again
*/
private function checkForPluginData($context): bool
{
$result = false;
/** @var EntityRepository $cmsPageRepository */
$cmsPageRepository = $this->container->get('cms_page.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('type', 'semes_manufacturer_plus'));
$ids = $cmsPageRepository->searchIds($criteria, $context)->getIds();
if (count($ids) > 0) $result = true;
return $result;
}
}