Skip to content

Commit

Permalink
Fix importing stack structure and localized stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Jan 23, 2025
1 parent 3e8cd6d commit e459ddb
Show file tree
Hide file tree
Showing 4 changed files with 339 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,106 @@
namespace PortlandLabs\Concrete5\MigrationTool\Publisher\Command\Handler;

use Concrete\Core\Page\Stack\Stack;
use Doctrine\ORM\EntityManagerInterface;
use PortlandLabs\Concrete5\MigrationTool\Batch\BatchInterface;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\ObjectCollection;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Command\CreateStackStructureCommand;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\GlobalArea as ImportGlobalArea;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\StackFolder as ImportFolder;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\Stack as ImportStack;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Logger\LoggerInterface;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Service\StackTrait;
use Concrete\Core\Page\Stack\Folder\Folder;


defined('C5_EXECUTE') or die("Access Denied.");

/**
* @property \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\CreateStackStructureCommand $command
*/
class CreateStackStructureCommandHandler extends AbstractPageCommandHandler
{
use StackTrait;

/**
* {@inheritdoc}
*
* @see \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\Handler\AbstractPageCommandHandler::getPage()
*
* @return \PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack|null
*/
public function getPage($id)
{
$entityManager = \Database::connection()->getEntityManager();
$r = $entityManager->getRepository('\PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack');
return $r->findOneById($id);
$em = app(EntityManagerInterface::class);

return $em->find(AbstractStack::class, $id);
}

/**
* {@inheritdoc}
*
* @see \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\Handler\AbstractHandler::execute()
*/
public function execute(BatchInterface $batch, LoggerInterface $logger)
{
$parent = null;
/**
* @var $command CreateStackStructureCommand
*/
$command = $this->command;
$stack = $this->getPage($command->getPageId());
$stack = $this->getPage($this->command->getPageId());
$logger->logPublishStarted($stack);
if ($stack->getPath() || $stack->getName()) {
if ($stack->getPath() != '') {
$lastSlash = strrpos($stack->getPath(), '/');
$parentPath = substr($stack->getPath(), 0, $lastSlash);
if ($parentPath) {
$parent = \Concrete\Core\Support\Facade\StackFolder::getByPath($parentPath, 'RECENT', $batch->getSite()->getSiteTreeObject());
}
}
if ($stack instanceof ImportFolder) {
$new = $this->importFolder($stack, $batch);
} elseif ($stack instanceof ImportGlobalArea) {
$new = $this->importGlobalArea($stack, $batch);
} elseif ($stack instanceof ImportStack) {
$new = $this->importStack($stack, $batch);
} else {
$new = null;
}
if ($new === null) {
$logger->logSkipped($stack);
} else {
$logger->logPublishComplete($stack, $new);
}
}

private function importFolder(ImportFolder $folder, BatchInterface $batch): ?Folder
{
$name = (string) $folder->getName();
if ($name === '') {
return null;
}
$path = '/' . trim($folder->getPath() ?? '', '/');
$folderPath = rtrim($path, '/') . '/' . $name;
$existingFolders = $this->getExistingFolders();
if (array_key_exists($folderPath, $existingFolders)) {
return null;
}
$parentFolder = $this->getOrCreateFolderByPath($path);

return $this->createFolder($name, $folderPath, $parentFolder);
}

private function importGlobalArea(ImportGlobalArea $globalArea, BatchInterface $batch): ?Stack
{
$name = (string) $globalArea->getName();
if ($name === '') {
return null;
}
if (Stack::getByName($name, 'RECENT', $batch->getSite())) {
return null;
}

switch ($stack->getType()) {
case 'folder':
$folder = \Concrete\Core\Support\Facade\StackFolder::getByPath($stack->getName(), $batch->getSite()->getSiteTreeObject());
if (!is_object($folder)) {
$folder = \Concrete\Core\Support\Facade\StackFolder::add($stack->getName(), $parent);
$logger->logPublishComplete($stack, $folder);
} else {
$logger->logSkipped($stack);
}
break;
case 'global_area':
$s = Stack::getByName($stack->getName(), 'RECENT', $batch->getSite()->getSiteTreeObject());
if (!is_object($s)) {
if (method_exists('\Concrete\Core\Page\Stack\Stack', 'addGlobalArea')) {
$s = Stack::addGlobalArea($stack->getName(), $batch->getSite()->getSiteTreeObject());
$logger->logPublishComplete($stack, $s);
} else {
//legacy
$s = Stack::addStack($stack->getName(), 'global_area');
$logger->logPublishComplete($stack, $s);
}
}
break;
default:
//stack
if (method_exists('\Concrete\Core\Page\Stack\Stack', 'getByPath') && $stack->getPath()) {
$s = Stack::getByPath($stack->getPath(), 'RECENT', $batch->getSite()->getSiteTreeObject());
if (!is_object($s)) {
$s = Stack::addStack($stack->getName(), $parent);
$logger->logPublishComplete($stack, $s);
} else {
$logger->logSkipped($stack);
}
} else {
$s = Stack::getByName($stack->getName(), 'RECENT', $batch->getSite()->getSiteTreeObject());
if (!is_object($s)) {
// legacy, so no folder support
$s = Stack::addStack($stack->getName());
$logger->logPublishComplete($stack, $s);
} else {
$logger->logSkipped($stack);
}
}
break;
}
return Stack::addGlobalArea($name, $batch->getSite());
}

private function importStack(ImportStack $stack, BatchInterface $batch): ?Stack
{
$name = (string) $stack->getName();
if ($name === '') {
return null;
}
$parent = $this->getOrCreateFolderByPath($stack->getPath() ?? '');
if ($this->getStackIDByName($name, $parent) !== null) {
return null;
}

return Stack::addStack($name, $parent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,80 @@

use Concrete\Core\Page\Stack\Stack;
use PortlandLabs\Concrete5\MigrationTool\Batch\BatchInterface;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\Area;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Command\PublishStackContentCommand;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\Stack as ImportStack;
use PortlandLabs\Concrete5\MigrationTool\Entity\Import\GlobalArea as ImportGlobalArea;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Logger\LoggerInterface;
use PortlandLabs\Concrete5\MigrationTool\Publisher\Service\StackTrait;
use Doctrine\ORM\EntityManagerInterface;
use Concrete\Core\Multilingual\Page\Section\Section;

defined('C5_EXECUTE') or die("Access Denied.");

/**
* @property \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\PublishStackContentCommand $command
*/
class PublishStackContentCommandHandler extends AbstractPageCommandHandler
{
use StackTrait;

/**
* {@inheritdoc}
*
* @see \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\Handler\AbstractPageCommandHandler::getPage()
*
* @return \PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack|null
*/
public function getPage($id)
{
$entityManager = \Database::connection()->getEntityManager();
$r = $entityManager->getRepository('\PortlandLabs\Concrete5\MigrationTool\Entity\Import\AbstractStack');
return $r->findOneById($id);
$em = app(EntityManagerInterface::class);

return $em->find(AbstractStack::class, $id);
}

/**
* {@inheritdoc}
*
* @see \PortlandLabs\Concrete5\MigrationTool\Publisher\Command\Handler\AbstractHandler::execute()
*/
public function execute(BatchInterface $batch, LoggerInterface $logger)
{
/**
* @var $command PublishStackContentCommand
*/
$command = $this->command;
$stack = $this->getPage($command->getPageId());
if ($stack->getPath() || $stack->getName()) {
if (method_exists('\Concrete\Core\Page\Stack\Stack', 'getByPath') && $stack->getPath()) {
$s = Stack::getByPath($stack->getPath(), 'RECENT', $batch->getSite()->getSiteTreeObject());
} else {
$s = Stack::getByName($stack->getName(), 'RECENT', $batch->getSite()->getSiteTreeObject());
$importStack = $this->getPage($this->command->getPageId());
$name = (string) $importStack->getName();
if ($name === '') {
return;
}
$site = $batch->getSite();
if ($importStack instanceof ImportGlobalArea) {
$stack = Stack::getByName($name, 'RECENT', $site);
} elseif ($importStack instanceof ImportStack) {
$folder = $this->getOrCreateFolderByPath((string) $importStack->getPath());
$stackID = $this->getStackIDByName($name, $folder);
$stack = $stackID ? Stack::getByID($stackID) : null;
} else {
$stack = null;
}
if (!$stack || $stack->isError()) {
return;
}
$localeID = $importStack->getLocaleID();
if ($localeID) {
$section = Section::getByLocale($localeID, $site);
if (!$section) {
return;
}
if (is_object($s)) {
foreach ($stack->getBlocks() as $block) {
$bt = $this->getTargetItem($batch, 'block_type', $block->getType());
if (is_object($bt)) {
$value = $block->getBlockValue();
$publisher = $value->getPublisher();
$b = $publisher->publish($batch, $bt, $s, STACKS_AREA_NAME, $value);
$styleSet = $block->getStyleSet();
if (is_object($styleSet)) {
$styleSetPublisher = $styleSet->getPublisher();
$publishedStyleSet = $styleSetPublisher->publish();
$b->setCustomStyleSet($publishedStyleSet);
}
if ($block->getCustomTemplate()) {
$b->setCustomTemplate($block->getCustomTemplate());
}
}
}
$localizedStack = $stack->getLocalizedStack($section);
$stack = $localizedStack ?: $stack->addLocalizedStack($section, ['copyContents' => false]);
}
foreach ($importStack->getBlocks() as $importBlock) {
/** @var \\PortlandLabs\Concrete5\MigrationTool\Entity\Import\StackBlock $importBlock */
$blockType = $this->getTargetItem($batch, 'block_type', $importBlock->getType());
if (!is_object($blockType)) {
continue;
}
$value = $importBlock->getBlockValue();
$publisher = $value->getPublisher();
$publisher->publish($batch, $blockType, $stack, STACKS_AREA_NAME, $value);
}
}
}
Loading

0 comments on commit e459ddb

Please sign in to comment.