Skip to content

Instantly share code, notes, and snippets.

@chanthornngeth
Last active December 19, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chanthornngeth/5963968 to your computer and use it in GitHub Desktop.
Save chanthornngeth/5963968 to your computer and use it in GitHub Desktop.
<?php
namespace My\Demo\Command;
/* *
* This script belongs to the TYPO3 Flow package "My.Demo". *
* *
* */
use TYPO3\Flow\Error\Exception;
use TYPO3\Flow\Annotations as Flow;
/**
* NeosCommand command controller for the My.Demo package
*
* @Flow\Scope("singleton")
*/
class NeosCommandController extends \TYPO3\Flow\Cli\CommandController {
/**
* @Flow\Inject
* @var \TYPO3\TYPO3CR\Domain\Repository\NodeRepository
*/
protected $nodeRepository;
/**
* Command to copy page trees
*
* The page tree is at the moment only available to copy using the command line since the copying through
* Web Interface is not done yet.
*
* Please apply the correct parameters as in the Usage and Arguments section above
*
* @param string $copyTree The path of page tree you want to copy
* @param string $targetTree The path of page tree you want to paste to
* @param string optional $position where the node should be added (allowed: before, into, after) default: into
* @return void
*/
public function copyCommand($copyTree, $targetTree, $position = NULL) {
$position = ($position === NULL) ? 'into' : $position;
$contentContext = new \TYPO3\Neos\Domain\Service\ContentContext('live');
$workspace = $contentContext->getWorkspace();
$this->nodeRepository->setContext($contentContext);
$targetNode = $contentContext->getNode($targetTree);
// check position for pastes the nodes
if ($position == 'before' || $position == 'after') {
$newNodes = $this->nodeRepository->findOneByPath($targetTree, $workspace);
$targetTree = $newNodes->getParentPath();
}
$newNodes = $this->nodeRepository->findByParentAndContentType($targetTree, NULL, $workspace);
$nodes = $this->nodeRepository->findByParentAndContentType($copyTree, NULL, $workspace);
// loop to copy nodes to new target tree
foreach ($nodes as $node) {
$copyNode = $contentContext->getNode($node->getPath());
try {
switch ($position) {
case 'before':
$copyNode->copyBefore($targetNode, $copyNode->getName());
break;
case 'into':
$copyNode->copyInto($targetNode, $copyNode->getName());
break;
case 'after':
$copyNode->copyAfter($targetNode, $copyNode->getName());
}
} catch (\Exception $e) {
// loop to check if new node name exist in target tree
foreach ($newNodes as $newNode) {
$newNode = $contentContext->getNode($newNode->getPath());
if ($copyNode->getName() == $newNode->getName()) {
$newNode->remove();
switch ($position) {
case 'before':
$copyNode->copyBefore($targetNode, $copyNode->getName());
break;
case 'into':
$copyNode->copyInto($targetNode, $copyNode->getName());
break;
case 'after':
$copyNode->copyAfter($targetNode, $copyNode->getName());
}
}
}
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment