Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 6, 2024
1 parent bd3edce commit 6764f9d
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions src/Tasks/SubsiteCopyPagesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace SilverStripe\Subsites\Tasks;

use InvalidArgumentException;
use Closure;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\BuildTask;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\ORM\DataObject;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\Pages\SubsitesVirtualPage;
use SilverStripe\Versioned\Versioned;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
* Handy alternative to copying pages when creating a subsite through the UI.
Expand All @@ -22,32 +26,37 @@
*/
class SubsiteCopyPagesTask extends BuildTask
{
protected $title = 'Copy pages to different subsite';
protected $description = '';
protected string $title = 'Copy pages to different subsite';

private static $segment = 'SubsiteCopyPagesTask';
protected static string $description = 'Handy alternative to copying pages when creating a subsite through the UI';

public function run($request)
protected static string $commandName = 'SubsiteCopyPagesTask';

protected function execute(InputInterface $input, HybridOutput $output): int
{
$subsiteFromId = $request->getVar('from');
$subsiteFromId = $input->getOption('from');
if (!is_numeric($subsiteFromId)) {
throw new InvalidArgumentException('Missing "from" parameter');
$output->writeln('<error>Missing "from" parameter</>');
return Command::INVALID;
}
$subsiteFrom = DataObject::get_by_id(Subsite::class, $subsiteFromId);
if (!$subsiteFrom) {
throw new InvalidArgumentException('Subsite not found');
$output->writeln('<error>Subsite not found</>');
return Command::FAILURE;
}

$subsiteToId = $request->getVar('to');
$subsiteToId = $input->getOption('to');
if (!is_numeric($subsiteToId)) {
throw new InvalidArgumentException('Missing "to" parameter');
$output->writeln('<error>Missing "to" parameter</>');
return Command::INVALID;
}
$subsiteTo = DataObject::get_by_id(Subsite::class, $subsiteToId);
if (!$subsiteTo) {
throw new InvalidArgumentException('Subsite not found');
$output->writeln('<error>Subsite not found</>');
return Command::FAILURE;
}

$useVirtualPages = (bool)$request->getVar('virtual');
$useVirtualPages = $input->getOption('virtual');

Subsite::changeSubsite($subsiteFrom);

Expand Down Expand Up @@ -77,16 +86,50 @@ public function run($request)
$childClone->copyVersionToStage('Stage', 'Live');
array_push($stack, [$child->ID, $childClone->ID]);

$this->log(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link()));
$output->writeln(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link()));
}
}

unset($children);
}

return Command::SUCCESS;
}

public function getOptions(): array
{
$subsiteSuggestionClosure = Closure::fromCallable([static::class, 'getSubsiteCompletion']);
return [
new InputOption(
'from',
null,
InputOption::VALUE_REQUIRED,
'ID of the subsite to copy from',
suggestedValues: $subsiteSuggestionClosure
),
new InputOption(
'to',
null,
InputOption::VALUE_REQUIRED,
'ID of the subsite to copy to',
suggestedValues: $subsiteSuggestionClosure
),
new InputOption(
'virtual',
null,
InputOption::VALUE_NONE,
'Create virtual pages instead of duplicating pages'
),
];
}

public function log($msg)
public static function getSubsiteCompletion(): array
{
echo $msg . "\n";
$subsites = Subsite::get()->map('ID', 'Title');
$suggestions = [];
foreach ($subsites as $id => $title) {
$suggestions[] = "{$id}\t{$title}";
}
return $suggestions;
}
}

0 comments on commit 6764f9d

Please sign in to comment.