From ca606519db37059ac7dc99ffdbfc862636ae2253 Mon Sep 17 00:00:00 2001 From: indy koning Date: Fri, 7 Aug 2020 15:12:39 +0200 Subject: [PATCH] Migrate content to Pagedesigner format. --- Job/Migration.php | 140 ++++++++++++++++++++++++++++++++++++++++ Setup/InstallSchema.php | 14 ++++ Setup/Uninstall.php | 14 ++++ 3 files changed, 168 insertions(+) create mode 100644 Job/Migration.php diff --git a/Job/Migration.php b/Job/Migration.php new file mode 100644 index 0000000..5acad2a --- /dev/null +++ b/Job/Migration.php @@ -0,0 +1,140 @@ +pageRepositoryInterface = $pageRepositoryInterface; + $this->blockRepositoryInterface = $blockRepositoryInterface; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + } + + /** + * Get a list of all CMS pages. + * + * @return PageSearchResultsInterface + */ + public function getPages() { + $searchCriteria = $this->searchCriteriaBuilder->create(); + $pages = $this->pageRepositoryInterface->getList($searchCriteria)->getItems(); + return $pages; + } + + /** + * Get a list of all CMS Blocks. + * + * @return BlockSearchResultsInterface + */ + public function getBlocks() { + $searchCriteria = $this->searchCriteriaBuilder->create(); + $blocks = $this->blockRepositoryInterface->getList($searchCriteria)->getItems(); + return $blocks; + } + + /** + * Migrate all Pages and Blocks to PageDesigner. + */ + public function migrate() + { + foreach ($this->getPages() as $page) { + $this->migrateContent($page); + } + + foreach ($this->getBlocks() as $block) { + $this->migrateContent($block); + } + } + + /** + * Revert all Pages and Blocks back into default format. + */ + public function revert() + { + foreach ($this->getPages() as $page) { + $this->revertContent($page); + } + + foreach ($this->getBlocks() as $block) { + $this->revertContent($block); + } + } + + /** + * Migrate Page/Block content to a PageDesigner structure. + * + * @param Page|Block $entity + */ + public function migrateContent($entity) + { + if ($entity->getPageDesignerJson() !== null) { + return; + } + $content = $entity->getContent(); + $widget = '{{widget type="Magenerds\WysiwygWidget\Block\Widget\Editor" content="---BASE64---' . base64_encode($content) . '"}}'; + $pageDesignerJson = [ + 'version' => '1.0.0', + 'rows' => [ + [ + 'columns' => [ + [ + 'gridSize' => [ + 'md' => 12 + ], + 'content' => $widget, + 'settings' => [] + ] + ], + 'settings' => [] + ] + ] + ]; + $entity->setPageDesignerJson(json_encode($pageDesignerJson)); + $entity->setContent('
' . $widget . '
'); + $entity->getResource()->save($entity); + } + + /** + * Get first wysiwyg widget and revert it's content into content. + * + * @param Page|Block $entity + */ + public function revertContent($entity) { + if ($entity->getPageDesignerJson() === null) { + return; + } + + $pageDesignerJson = json_decode($entity->getPageDesignerJson(), true); + + foreach ($pageDesignerJson['rows'] as $row) { + foreach ($row['columns'] as $column) { + if (strpos($column['content'], 'Magenerds\WysiwygWidget\Block\Widget\Editor') === false) { + continue; + } + $content = base64_decode(str_replace('"}}', '', str_replace('{{widget type="Magenerds\WysiwygWidget\Block\Widget\Editor" content="---BASE64---', '', $column['content']))); + $entity->setPageDesignerJson(null); + $entity->setContent($content); + $entity->getResource()->save($entity); + return; + } + } + } +} diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php index 5953608..3263416 100644 --- a/Setup/InstallSchema.php +++ b/Setup/InstallSchema.php @@ -10,6 +10,7 @@ namespace Magenerds\PageDesigner\Setup; use Magenerds\PageDesigner\Constants; +use Magenerds\PageDesigner\Job\Migration; use Magento\Framework\DB\Ddl\Table; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; @@ -27,6 +28,17 @@ */ class InstallSchema implements InstallSchemaInterface { + /** + * InstallSchema constructor. + * + * @param Migration $migration + */ + public function __construct( + Migration $migration + ) { + $this->migration = $migration; + } + /** * Install schema * @@ -42,6 +54,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con $this->addPageDesignerJsonColumn($setup, $setup->getTable($table)); } + $this->migration->migrate(); + $setup->endSetup(); } diff --git a/Setup/Uninstall.php b/Setup/Uninstall.php index 0a50ece..22f5523 100644 --- a/Setup/Uninstall.php +++ b/Setup/Uninstall.php @@ -10,6 +10,7 @@ namespace Magenerds\PageDesigner\Setup; use Magenerds\PageDesigner\Constants; +use Magenerds\PageDesigner\Job\Migration; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\SetupInterface; @@ -26,6 +27,17 @@ */ class Uninstall implements UninstallInterface { + /** + * Uninstall constructor. + * + * @param Migration $migration + */ + public function __construct( + Migration $migration + ) { + $this->migration = $migration; + } + /** * Invoked when remove-data flag is set during module uninstall * @@ -36,6 +48,8 @@ public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $c { $setup->startSetup(); + $this->migration->revert(); + foreach (Constants::CONTENT_TABLES as $table) { $this->dropPageDesignerColumns($setup, $setup->getTable($table)); }