custom/plugins/SemesManufacturerPlus/src/SemesManufacturerPlus.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SemesManufacturerPlus;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  14. class SemesManufacturerPlus extends Plugin
  15. {
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         $this->createCmsPages($installContext->getContext());
  20.     }
  21.     public function uninstall(UninstallContext $context): void
  22.     {
  23.         parent::uninstall($context);
  24.         if ($context->keepUserData()) {
  25.             return;
  26.         }
  27.         // clean everything up
  28.         // delete cms pages 
  29.         $this->unlockCmsPage();
  30.         $this->deleteManufacturerPages($context->getContext());
  31.         // delete tag table 
  32.         $this->deleteTagTable();
  33.         // delete translation table 
  34.         $this->deleteTranslationTable();
  35.         
  36.         // delete entity
  37.         $this->deleteEntityTable();
  38.         // delete seo url template(s) 
  39.         $this->deleteSeoUrlTemplate($context->getContext());
  40.     }
  41.     public function update(UpdateContext $updateContext): void
  42.     {
  43.         parent::update($updateContext);
  44.     }
  45.     private function createCmsPages(Context $context): void
  46.     {
  47.         if ($this->checkForPluginData($context)) return;
  48.         $manufacutrerOverviewCmsPageId Uuid::randomHex();
  49.         $manufacutrerDetailCmsPageId Uuid::randomHex();
  50.         $cmsPage[] = $this->getOverviewCmsPageConfig($manufacutrerOverviewCmsPageId);
  51.         $cmsPage[] = $this->getDetailCmsPageConfig($manufacutrerDetailCmsPageId);
  52.         // save pages to repository
  53.         $this->writeCmsPages($cmsPage$context);
  54.         // set default detail page to plugin config 
  55.         $this->setConfigDetailPage($manufacutrerDetailCmsPageId);
  56.         // lock pages to avoid losing them due DAU
  57.         $this->lockCmsPage();
  58.     }
  59.     private function deleteManufacturerPages(Context $context): void
  60.     {
  61.         /** @var EntityRepository $cmsPageRepository */
  62.         $cmsPageRepository $this->container->get('cms_page.repository');
  63.         $criteria = new Criteria();
  64.         $criteria->addFilter(new EqualsFilter('type''semes_manufacturer_plus'));
  65.         $ids $cmsPageRepository->searchIds($criteria$context)->getIds();
  66.         $ids \array_map(static function ($id) {
  67.             return ['id' => $id];
  68.         }, $ids);
  69.         $cmsPageRepository->delete($ids$context);
  70.     }
  71.     private function deleteSeoUrlTemplate(Context $context): void
  72.     {
  73.         /** @var EntityRepository $seoUrlTemplateRepository */
  74.         $seoUrlTemplateRepository $this->container->get('seo_url_template.repository');
  75.         $criteria = new Criteria();
  76.         $criteria->addFilter(new EqualsFilter('routeName''frontend.semes.manufacturer.plus.detail'));
  77.         $ids $seoUrlTemplateRepository->searchIds($criteria$context)->getIds();
  78.         $ids \array_map(static function ($id) {
  79.             return ['id' => $id];
  80.         }, $ids);
  81.         $seoUrlTemplateRepository->delete($ids$context);
  82.     }
  83.     private function deleteTagTable(): void
  84.     {
  85.         $connection $this->container->get(Connection::class);
  86.         $sql "DROP TABLE IF EXISTS `semes_manufacturer_plus_tag`";
  87.         $connection->executeUpdate($sql);
  88.     }
  89.     private function deleteTranslationTable(): void
  90.     {
  91.         $connection $this->container->get(Connection::class);
  92.         $sql "DROP TABLE IF EXISTS `semes_manufacturer_plus_translation`";
  93.         $connection->executeUpdate($sql);
  94.     }
  95.     private function deleteEntityTable(): void
  96.     {
  97.         $connection $this->container->get(Connection::class);
  98.         $sql "DROP TABLE IF EXISTS `semes_manufacturer_plus`";
  99.         $connection->executeUpdate($sql);
  100.     }
  101.     /** 
  102.      * default overview cms page
  103.      */
  104.     private function getOverviewCmsPageConfig(string $id): array
  105.     {
  106.         $cmsPage = [
  107.             'id' => $id,
  108.             'type' => 'semes_manufacturer_plus',
  109.             "name"=> "Overview manufacturer",
  110.             'translations' => [
  111.                 "de-DE" => [
  112.                     "name"=> "Übersicht Hersteller"
  113.                 ]
  114.             ],
  115.             'sections' => [
  116.                 [
  117.                     'id' => Uuid::randomHex(),
  118.                     'type' => 'default',
  119.                     'position' => 0,
  120.                     'blocks' => [
  121.                         [
  122.                             'position' => 1,
  123.                             'type' => 'text',
  124.                             'name'=> 'Manufacturer Listing Text',
  125.                             'marginTop' => '20px',
  126.                             'marginBottom' => '20px',
  127.                             'marginLeft' => '20px',
  128.                             'marginRight' => '20px',
  129.                             'slots' => [
  130.                                 [
  131.                                     'type' => 'text',
  132.                                     'slot' => 'content',
  133.                                     'config' => [
  134.                                         'content' => [
  135.                                             'source' => 'static',
  136.                                             '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>'
  137.                                         ],
  138.                                         'verticalAlign' => [
  139.                                             'source' => 'static',
  140.                                             'value' => null
  141.                                         ]
  142.                                     ]
  143.                                 ],
  144.                             ],
  145.                         ],
  146.                     ],
  147.                 ],
  148.                 [
  149.                     'id' => Uuid::randomHex(),
  150.                     'type' => 'default',
  151.                     'position' => 1,
  152.                     'blocks' => [
  153.                         [
  154.                             'position' => 1,
  155.                             'name'=> 'Manufacturer Listing',
  156.                             'type' => 'manufacturer-plus-listing',
  157.                             'marginTop' => '20px',
  158.                             'marginBottom' => '20px',
  159.                             'marginLeft' => '20px',
  160.                             'marginRight' => '20px',
  161.                             'slots' => [
  162.                                 ['type' => 'manufacturer-plus-listing''slot' => 'listing'],
  163.                             ],
  164.                         ],
  165.                     ],
  166.                 ],
  167.             ]
  168.         ];
  169.         return $cmsPage;
  170.     }
  171.     /** 
  172.      * default detail cms page
  173.      */
  174.     private function getDetailCmsPageConfig(string $id): array
  175.     {
  176.         $cmsPage = [
  177.             'id' => $id,
  178.             'type' => 'semes_manufacturer_plus',
  179.             "name"=> "Detail manufacturer",
  180.             'translations' => [
  181.                 "de-DE" => [
  182.                     "name"=> "Detail Hersteller"
  183.                 ]
  184.             ],
  185.             'sections' => [
  186.                 [
  187.                     'id' => Uuid::randomHex(),
  188.                     'type' => 'default',
  189.                     'position' => 0,
  190.                     'blocks' => [
  191.                         [
  192.                             'position' => 1,
  193.                             'type' => 'image-cover',
  194.                             'name'=> 'Manufacturer Banner',
  195.                             'slots' => [
  196.                                 [
  197.                                     'type' => 'image',  
  198.                                     'slot' => 'image',
  199.                                     'config' => [
  200.                                         'media' => [
  201.                                             'value' => 'product_manufacturer.extensions.manufacturerPlus.bannerImageMedia',
  202.                                             'source' => 'mapped',
  203.                                         ],
  204.                                     ],
  205.                                 ],
  206.                             ],
  207.                         ],
  208.                     ],
  209.                 ],
  210.                 [
  211.                     'id' => Uuid::randomHex(),
  212.                     'type' => 'default',
  213.                     'position' => 1,
  214.                     'blocks' => [
  215.                         [
  216.                             'position' => 1,
  217.                             'type' => 'text',
  218.                             'name'=> 'Manufacturer Heading',
  219.                             'marginTop' => '20px',
  220.                             'marginBottom' => '20px',
  221.                             'marginLeft' => '20px',
  222.                             'marginRight' => '20px',
  223.                             'slots' => [
  224.                                 [
  225.                                     'type' => 'text',
  226.                                     'slot' => 'content',
  227.                                     'config' => ['content' => ['source' => 'static''value' => '<h1>{{ product_manufacturer.name }}</h1> <p>{{ product_manufacturer.description }}</p>']]
  228.                                 ],
  229.                             ],
  230.                         ],
  231.                     ],
  232.                 ],
  233.                 [
  234.                     'id' => Uuid::randomHex(),
  235.                     'type' => 'default',
  236.                     'position' => 2,
  237.                     'blocks' => [
  238.                         [
  239.                             'position' => 1,
  240.                             'type' => 'manufacturer-plus-product-listing-filter',
  241.                             'name'=> 'Manufacturer Listing Filter',
  242.                             'slots' => [
  243.                                 [
  244.                                     'type' => 'manufacturer-plus-product-listing-filter',
  245.                                     'slot' => 'content'
  246.                                 ],
  247.                             ]
  248.                         ],
  249.                         [
  250.                             'position' => 2,
  251.                             'type' => 'manufacturer-plus-product-listing',
  252.                             'name'=> 'Manufacturer Listing Products',
  253.                             'slots' => [
  254.                                 [
  255.                                     'type' => 'manufacturer-plus-product-listing',
  256.                                     'slot' => 'content'
  257.                                 ],
  258.                             ],
  259.                         ],
  260.                     ],
  261.                 ],
  262.                 [
  263.                     'id' => Uuid::randomHex(),
  264.                     'type' => 'default',
  265.                     'position' => 3,
  266.                     'blocks' => [
  267.                         [
  268.                             'position' => 1,
  269.                             'type' => 'text',
  270.                             'name'=> 'Manufacturer Footer',
  271.                             'marginTop' => '20px',
  272.                             'marginBottom' => '20px',
  273.                             'marginLeft' => '20px',
  274.                             'marginRight' => '20px',
  275.                             'slots' => [
  276.                                 [
  277.                                     'type' => 'text',
  278.                                     'slot' => 'content',
  279.                                     'config' => ['content' => ['source' => 'static''value' => '<p>{{ product_manufacturer.extensions.manufacturerPlus.footerDescription }}</p>']]
  280.                                 ],
  281.                             ],
  282.                         ],
  283.                     ],
  284.                 ],
  285.             ]
  286.         ];
  287.         return $cmsPage;
  288.     }
  289.     /** 
  290.      * write pages to repository
  291.      */
  292.     private function writeCmsPages($pages$context): void
  293.     {
  294.         /** @var EntityRepository $cmsPageRepository */
  295.         $cmsPageRepository $this->container->get('cms_page.repository');
  296.         $cmsPageRepository->create($pages$context);
  297.     }
  298.     private function setConfigDetailPage($id): void
  299.     {
  300.         /** @var SystemConfigService $systemConfig */
  301.         $systemConfig $this->container->get(SystemConfigService::class);
  302.         $systemConfig->set('SemesManufacturerPlus.config.cmsPageId'$id);
  303.     }
  304.     /** 
  305.      * Setting CMS Page locked via DB Connection because it is not working via DAL
  306.      */
  307.     private function lockCmsPage(): void
  308.     {
  309.         $connection $this->container->get(Connection::class);
  310.         $sql "UPDATE `cms_page` SET `locked` = 1 WHERE `type` = 'semes_manufacturer_plus' AND `updated_at` IS NULL;";
  311.         $connection->executeUpdate($sql);
  312.     }
  313.     /** 
  314.      * Setting CMS Page unlocked via DB Connection because it is not working via DAL
  315.      */
  316.     private function unlockCmsPage(): void
  317.     {
  318.         $connection $this->container->get(Connection::class);
  319.         $sql "UPDATE `cms_page` SET `locked` = 0 WHERE `type` = 'semes_manufacturer_plus'";
  320.         $connection->executeUpdate($sql);
  321.     }
  322.     /**
  323.      * check if plugin was installed previously to avoid adding default data again
  324.      */
  325.     private function checkForPluginData($context): bool
  326.     {
  327.         $result false;
  328.         /** @var EntityRepository $cmsPageRepository */
  329.         $cmsPageRepository $this->container->get('cms_page.repository');
  330.         $criteria = new Criteria();
  331.         $criteria->addFilter(new EqualsFilter('type''semes_manufacturer_plus'));
  332.         $ids $cmsPageRepository->searchIds($criteria$context)->getIds();
  333.         if (count($ids) > 0$result true;
  334.         return $result;
  335.     }
  336. }