Last active
August 29, 2015 14:03
-
-
Save joaoinacio/13b80bb10b5905f5e6cf 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 JI\TestBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Input\InputArgument; | |
class TestCreateAndUpdateImageCommand extends ContainerAwareCommand | |
{ | |
/** | |
* @see Command | |
*/ | |
protected function configure() | |
{ | |
$this->setName( 'test:createAndUpdateImage' ) | |
->addArgument( 'parentLocationId', InputArgument::REQUIRED, 'Parent location Id where new content will be created.' ) | |
->addArgument( 'file', InputArgument::REQUIRED, 'full path for image file.' ) | |
->addArgument( 'name', InputArgument::OPTIONAL, 'object name.' ); | |
} | |
/** | |
* @param InputInterface $input Predefined parameter for commands | |
* @param OutputInterface $output Predefined parameter for commands | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$parentLocationId = $input->getArgument( 'parentLocationId' ); | |
$fullPathToFile = $input->getArgument( 'file' ); | |
$objectName = $input->getArgument( 'name' ); | |
if ( $objectName === null ) | |
{ | |
$objectName = 'New content'; | |
} | |
$fields = array( | |
'name' => $objectName, | |
'image' => \eZ\Publish\Core\FieldType\Image\Value::fromString( $fullPathToFile ) | |
); | |
$output->writeln( 'Creating content...' ); | |
$object = $this->createContent( $parentLocationId, $fields ); | |
$output->writeln( 'Done.' ); | |
$contentId = $object->versionInfo->contentInfo->id; | |
$fields['name'] = 'New ' . $objectName; | |
$output->writeln( 'Updating content ' . $contentId ); | |
$this->updateContent( $contentId, $fields ); | |
$output->writeln( 'Done.' ); | |
} | |
protected function createContent( $parentLocationId, $fields, $translation = 'eng-GB') | |
{ | |
$repository = $this->getContainer()->get( 'ezpublish.api.repository' ); | |
$repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); | |
$locationService = $repository->getLocationService(); | |
$contentService = $repository->getContentService(); | |
$contentTypeService = $repository->getContentTypeService(); | |
$contentType = $contentTypeService->loadContentTypeByIdentifier( 'image' ); | |
$contentStruct = $contentService->newContentCreateStruct( $contentType, $translation ); | |
foreach ( $fields as $name => $value ) | |
{ | |
$contentStruct->setField( $name, $value ); | |
} | |
$locationCreateStruct = $locationService->newLocationCreateStruct( $parentLocationId ); | |
$draft = $contentService->createContent( $contentStruct, array( $locationCreateStruct ) ); | |
return $contentService->publishVersion( $draft->versionInfo ); | |
} | |
protected function updateContent( $contentId, $fields, $translation = 'fre-FR') | |
{ | |
$repository = $this->getContainer()->get( 'ezpublish.api.repository' ); | |
$repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); | |
$contentService = $repository->getContentService(); | |
$contentInfo = $contentService->loadContentInfo( $contentId ); | |
$contentDraft = $contentService->createContentDraft( $contentInfo ); | |
$contentStruct = $contentService->newContentUpdateStruct(); | |
$contentStruct->initialLanguageCode = $translation; | |
foreach ( $fields as $name => $value ) | |
{ | |
$contentStruct->setField( $name, $value ); | |
} | |
try | |
{ | |
$contentDraft = $contentService->updateContent( $contentDraft->versionInfo, $contentStruct ); | |
return $contentService->publishVersion( $contentDraft->versionInfo ); | |
} | |
catch ( \Exception $e ) | |
{ | |
print_r( $e->getTraceAsString() ); | |
throw $e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment