Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate content to Pagedesigner format. #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions Job/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Magenerds\PageDesigner\Job;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Cms\Api\Data\BlockSearchResultsInterface;
use Magento\Cms\Api\Data\PageSearchResultsInterface;
use Magento\Cms\Api\PageRepositoryInterface;
use Magento\Cms\Model\Block;
use Magento\Cms\Model\Page;
use Magento\Framework\Api\SearchCriteriaBuilder;

class Migration
{
/**
* Construct.
* @param PageRepositoryInterface $pageRepositoryInterface
* @param BlockRepositoryInterface $blockRepositoryInterface
* @param SearchCriteriaBuilder $searchCriteriaBuilder
*/
public function __construct(
PageRepositoryInterface $pageRepositoryInterface,
BlockRepositoryInterface $blockRepositoryInterface,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->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('<div class="pd-row row"><div class="pd-col col-md-12">' . $widget . '</div></div>');
$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;
}
}
}
}
14 changes: 14 additions & 0 deletions Setup/InstallSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,17 @@
*/
class InstallSchema implements InstallSchemaInterface
{
/**
* InstallSchema constructor.
*
* @param Migration $migration
*/
public function __construct(
Migration $migration
) {
$this->migration = $migration;
}

/**
* Install schema
*
Expand All @@ -42,6 +54,8 @@ public function install(SchemaSetupInterface $setup, ModuleContextInterface $con
$this->addPageDesignerJsonColumn($setup, $setup->getTable($table));
}

$this->migration->migrate();

$setup->endSetup();
}

Expand Down
14 changes: 14 additions & 0 deletions Setup/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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));
}
Expand Down