Created
July 11, 2015 03:38
-
-
Save wizhippo/69fc46671d9446e4c859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace SiteBundle\Command; | |
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; | |
use eZ\Publish\API\Repository\Values\Content\Query; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Tocom\Bundle\ConsumerSiteBundle\Helper\SearchHelper; | |
class CreateSiteCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this->setName( 'consumer_site:create-site' ) | |
->setDescription( "Create site from template" ) | |
->setDefinition( | |
array( | |
new InputArgument( | |
'site', InputArgument::REQUIRED, 'An new site name' | |
), | |
new InputArgument( | |
'template', | |
InputArgument::REQUIRED, 'An existing site for template' | |
), | |
) | |
); | |
} | |
protected function execute( InputInterface $input, OutputInterface $output ) | |
{ | |
// $sa = $this->getContainer()->get( 'ezpublish.siteaccess' ); | |
// $output->write(print_r($sa), true); | |
$siteName = $input->getArgument( 'site' ); | |
$templateSiteName = $input->getArgument( 'template' ); | |
/* @var $repository \eZ\Publish\API\Repository\Repository */ | |
$repository = $this->getContainer()->get( 'ezpublish.api.repository' ); | |
$userService = $repository->getUserService(); | |
$user = $userService->loadUser( 14 ); | |
$repository->setCurrentUser( $user ); | |
$locationService = $repository->getLocationService(); | |
$searchService = $repository->getSearchService(); | |
$contentService = $repository->getContentService(); | |
$contentTypeService = $repository->getContentTypeService(); | |
$searchHelper = new SearchHelper( $searchService ); | |
$valueObject = $searchHelper->findLocationByPath( $siteName ); | |
if ( $valueObject ) | |
{ | |
throw new \Exception( 'Site already exists' ); | |
} | |
try | |
{ | |
$templateLocation = $searchHelper->findLocationByPath( $templateSiteName ); | |
$output->writeln( $templateLocation->contentInfo->name ); | |
} | |
catch ( \Exception $e ) | |
{ | |
throw new \Exception( 'Template does not exist' ); | |
} | |
$repository->beginTransaction(); | |
$contentType = $contentTypeService->loadContentTypeByIdentifier( 'folder' ); | |
$contentCreateStruct = $contentService->newContentCreateStruct( $contentType, 'eng-GB' ); | |
$contentCreateStruct->setField( 'name', $siteName ); | |
$locationCreateStruct = $locationService->newLocationCreateStruct( 2 ); | |
$draft = $contentService->createContent( | |
$contentCreateStruct, | |
array( $locationCreateStruct ) | |
); | |
$content = $contentService->publishVersion( $draft->versionInfo ); | |
$destinationLocation = $locationService->loadLocation( | |
$content->contentInfo->mainLocationId | |
); | |
$childLocationList = $locationService->loadLocationChildren( $templateLocation ); | |
foreach ( $childLocationList->locations as $childLocation ) | |
{ | |
$locationService->copySubtree( $childLocation, $destinationLocation ); | |
} | |
$repository->commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment