From 6e344e930693ed0e9dd9e48e3b781095851da6db Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Tue, 21 Apr 2015 14:12:35 +0200 Subject: [PATCH 1/3] Fixed CreateXmlContentCommand filename --- .../{CreateXMLContentCommand.php => CreateXmlContentCommand.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Command/{CreateXMLContentCommand.php => CreateXmlContentCommand.php} (100%) diff --git a/Command/CreateXMLContentCommand.php b/Command/CreateXmlContentCommand.php similarity index 100% rename from Command/CreateXMLContentCommand.php rename to Command/CreateXmlContentCommand.php From 5c3eaf0f7bec24ed9e126059b17175ddbf00d423 Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier Date: Tue, 21 Apr 2015 14:12:53 +0200 Subject: [PATCH 2/3] Began work on ContentType commands - Added update_content_type_draft cookbook command --- Command/CreateContentTypeDraftCommand.php | 76 +++++++++++++++++++ Command/DeleteContentTypeCommand.php | 57 ++++++++++++++ Command/UpdateContentTypeCommand.php | 92 +++++++++++++++++++++++ 3 files changed, 225 insertions(+) create mode 100644 Command/CreateContentTypeDraftCommand.php create mode 100644 Command/DeleteContentTypeCommand.php create mode 100644 Command/UpdateContentTypeCommand.php diff --git a/Command/CreateContentTypeDraftCommand.php b/Command/CreateContentTypeDraftCommand.php new file mode 100644 index 0000000..8beaedd --- /dev/null +++ b/Command/CreateContentTypeDraftCommand.php @@ -0,0 +1,76 @@ +setName( 'ezpublish:cookbook:create_content_type_draft' )->setDefinition( + array( + new InputArgument( 'identifier', InputArgument::REQUIRED, 'a content type identifier' ), + new InputArgument( 'group_identifier', InputArgument::REQUIRED, 'a content type group identifier' ) + ) + ); + } + + protected function execute( InputInterface $input, OutputInterface $output ) + { + /** @var $repository \eZ\Publish\API\Repository\Repository */ + $repository = $this->getContainer()->get( 'ezpublish.api.repository' ); + $contentTypeService = $repository->getContentTypeService(); + + $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); + + // fetch command line arguments + $groupIdentifier = $input->getArgument( 'group_identifier' ); + $contentTypeIdentifier = $input->getArgument( 'identifier' ); + + try + { + $contentTypeGroup = $contentTypeService->loadContentTypeGroupByIdentifier( $groupIdentifier ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) + { + $output->writeln( "content type group with identifier $groupIdentifier not found" ); + return; + } + + // instantiate a ContentTypeCreateStruct with the given content type identifier and set parameters + $contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct( $contentTypeIdentifier ); + $contentTypeCreateStruct->mainLanguageCode = 'eng-GB'; + + // set names for the content type + $contentTypeCreateStruct->names = array( + 'eng-GB' => 'New content type', + // 'ger-DE' => $contentTypeIdentifier . 'ger-DE', + ); + + try + { + $contentTypeDraft = $contentTypeService->createContentType( $contentTypeCreateStruct, array( $contentTypeGroup ) ); + $output->writeln( "Content type draft created with ID $contentTypeDraft->id" ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) + { + $output->writeln( "" . $e->getMessage() . "" ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e ) + { + $output->writeln( "" . $e->getMessage() . "" ); + } + } +} diff --git a/Command/DeleteContentTypeCommand.php b/Command/DeleteContentTypeCommand.php new file mode 100644 index 0000000..1ca7950 --- /dev/null +++ b/Command/DeleteContentTypeCommand.php @@ -0,0 +1,57 @@ +setName( 'ezpublish:cookbook:delete_content_type' )->setDefinition( + array( + new InputArgument( 'identifier', InputArgument::REQUIRED, 'a content type identifier' ) + ) + ); + } + + protected function execute( InputInterface $input, OutputInterface $output ) + { + /** @var $repository \eZ\Publish\API\Repository\Repository */ + $repository = $this->getContainer()->get( 'ezpublish.api.repository' ); + $contentTypeService = $repository->getContentTypeService(); + + $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); + + $contentTypeIdentifier = $input->getArgument( 'identifier' ); + + try + { + $contentType = $contentTypeService->loadContentType( $contentTypeIdentifier ); + $contentTypeService->deleteContentType( $contentType ); + $output->writeln( "Content type $contentType->id was deleted" ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) + { + $output->writeln( "Content type with identifier not found" ); + return; + } + catch ( BadStateException $e ) + { + $output->writeln( "This content type can not be deleted as there are objects of this type" ); + return; + } + } +} diff --git a/Command/UpdateContentTypeCommand.php b/Command/UpdateContentTypeCommand.php new file mode 100644 index 0000000..4c97fb3 --- /dev/null +++ b/Command/UpdateContentTypeCommand.php @@ -0,0 +1,92 @@ +setName( 'ezpublish:cookbook:update_content_type_draft' )->setDefinition( + array( + new InputArgument( 'id', InputArgument::REQUIRED, 'a content type draft id' ), + new InputOption( 'identifier', null, InputOption::VALUE_OPTIONAL, 'Updated identifier' ), + new InputOption( 'name', null, InputOption::VALUE_OPTIONAL, 'Updated name (in eng-GB)' ), + new InputOption( 'description', null, InputOption::VALUE_OPTIONAL, 'Updated description (in eng-GB)' ), + new InputOption( 'nameSchema', null, InputOption::VALUE_OPTIONAL, 'New name schema. Example: ' ), + ) + ); + } + + protected function execute( InputInterface $input, OutputInterface $output ) + { + /** @var $repository \eZ\Publish\API\Repository\Repository */ + $repository = $this->getContainer()->get( 'ezpublish.api.repository' ); + $contentTypeService = $repository->getContentTypeService(); + + $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); + + $contentTypeId = $input->getArgument( 'id' ); + + try + { + $contentTypeDraft = $contentTypeService->loadContentTypeDraft( $contentTypeId ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) + { + $output->writeln( "<error>Content type draft with identifier $contentTypeId not found</error>" ); + return; + } + + // instantiate a ContentTypeCreateStruct with the given content type identifier and set parameters + $contentTypeUpdateStruct = $contentTypeService->newContentTypeUpdateStruct(); + $contentTypeUpdateStruct->mainLanguageCode = 'eng-GB'; + + // We set the Content Type naming pattern to the title's value + if ( $input->hasOption( 'nameSchema' ) ) + { + $contentTypeUpdateStruct->nameSchema = $input->getOption( 'nameSchema' ); + } + + // set names for the content type + if ( $input->hasOption( 'name' ) ) + { + $contentTypeUpdateStruct->names = array( 'eng-GB' => $input->getOption( 'name' ) ); + } + + // set description for the content type + if ( $input->hasOption( 'description' ) ) + { + $contentTypeUpdateStruct->descriptions = array( 'eng-GB' => $input->getOption( 'description' ) ); + } + + try + { + $contentTypeService->updateContentTypeDraft( + $contentTypeDraft, $contentTypeUpdateStruct + ); + $output->writeln( "<info>Content type $contentTypeDraft->id was updated</info>" ); + $output->writeln( print_r( $contentTypeService->loadContentTypeDraft( $contentTypeId ), true ) ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) + { + $output->writeln( "<error>" . $e->getMessage() . "</error>" ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e ) + { + $output->writeln( "<error>" . $e->getMessage() . "</error>" ); + } + } +} From 6ca3d98e1d29aa2c992754f87f3fd2a5f07bc73b Mon Sep 17 00:00:00 2001 From: Bertrand Dunogier <bd@ez.no> Date: Tue, 21 Apr 2015 16:16:05 +0200 Subject: [PATCH 3/3] Added add_textline_field_definition cookbook command --- ...eldDefinitionToContentTypeDraftCommand.php | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 Command/AddTextLineFieldDefinitionToContentTypeDraftCommand.php diff --git a/Command/AddTextLineFieldDefinitionToContentTypeDraftCommand.php b/Command/AddTextLineFieldDefinitionToContentTypeDraftCommand.php new file mode 100644 index 0000000..14f557c --- /dev/null +++ b/Command/AddTextLineFieldDefinitionToContentTypeDraftCommand.php @@ -0,0 +1,113 @@ +<?php +/** + * File containing the CreateContentTypeCommand class. + * + * @copyright Copyright (C) eZ Systems AS. All rights reserved. + * @license For full copyright and license information view LICENSE file distributed with this source code. + * @version //autogentag// + */ +namespace EzSystems\CookbookBundle\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; +use Symfony\Component\Console\Input\InputOption; +use eZ\Publish\Core\Base\Exceptions\NotFoundException; + +class AddTextLineFieldDefinitionToContentTypeDraftCommand extends ContainerAwareCommand +{ + protected function configure() + { + $this->setName( 'ezpublish:cookbook:add_textline_field_definition' )->setDefinition( + array( + new InputArgument( 'id', InputArgument::REQUIRED, 'Content type draft id' ), + new InputOption( 'identifier', null, InputOption::VALUE_REQUIRED, 'Field definition identifier' ), + new InputOption( 'name', null, InputOption::VALUE_OPTIONAL, 'Field definition name (in eng-GB)' ), + new InputOption( 'description', null, InputOption::VALUE_OPTIONAL, 'Field definition description (in eng-GB)' ), + new InputOption( 'default-value', null, InputOption::VALUE_OPTIONAL ), + new InputOption( 'is-required', null, InputOption::VALUE_NONE ), + new InputOption( 'is-searchable', null, InputOption::VALUE_NONE ), + new InputOption( 'is-translatable', null, InputOption::VALUE_NONE ), + new InputOption( 'min-length', null, InputOption::VALUE_OPTIONAL, 'Minimum string length' ), + new InputOption( 'max-length', null, InputOption::VALUE_OPTIONAL, 'Maximent string length' ), + ) + ); + } + + protected function execute( InputInterface $input, OutputInterface $output ) + { + /** @var $repository \eZ\Publish\API\Repository\Repository */ + $repository = $this->getContainer()->get( 'ezpublish.api.repository' ); + $contentTypeService = $repository->getContentTypeService(); + + $repository->setCurrentUser( $repository->getUserService()->loadUser( 14 ) ); + + $contentTypeId = $input->getArgument( 'id' ); + + try + { + $contentTypeDraft = $contentTypeService->loadContentTypeDraft( $contentTypeId ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e ) + { + $output->writeln( "<error>Content type draft with identifier $contentTypeId not found</error>" ); + return; + } + + // instantiate a ContentTypeCreateStruct with the given content type identifier and set parameters + $fieldDefinitionCreateStruct = $contentTypeService->newFieldDefinitionCreateStruct( + $input->getOption( 'identifier' ), 'ezstring' + ); + + if ( $input->getOption( 'name' ) ) + { + $fieldDefinitionCreateStruct->names = array( 'eng-GB' => $input->getOption( 'name' ) ); + } + + if ( $input->getOption( 'description' ) ) + { + $fieldDefinitionCreateStruct->descriptions = array( 'eng-GB' => $input->getOption( 'description' ) ); + } + + if ( $input->getOption( 'default-value' ) ) + { + $fieldDefinitionCreateStruct->defaultValue = array( 'eng-GB' => $input->getOption( 'default-value' ) ); + } + + $fieldDefinitionCreateStruct->isRequired = $input->getOption( 'is-required' ); + $fieldDefinitionCreateStruct->isSearchable = $input->getOption( 'is-searchable' ); + $fieldDefinitionCreateStruct->isTranslatable = $input->getOption( 'is-translatable' ); + + // set description for the content type + if ( $input->hasOption( 'min-length' ) ) + { + $fieldDefinitionCreateStruct->validatorConfiguration['StringLengthValidator']['minStringLength'] = + (int)$input->getOption( 'min-length' ); + } + + if ( $input->hasOption( 'max-length' ) ) + { + $fieldDefinitionCreateStruct->validatorConfiguration['StringLengthValidator']['maxStringLength'] = + (int)$input->getOption( 'max-length' ); + } + + try + { + $contentTypeService->addFieldDefinition( + $contentTypeDraft, $fieldDefinitionCreateStruct + ); + $output->writeln( "<info>Added field definition to Content Type Draft #$contentTypeDraft->id</info>" ); + $output->writeln( print_r( $contentTypeService->loadContentTypeDraft( $contentTypeId ), true ) ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\UnauthorizedException $e ) + { + $output->writeln( "<error>" . $e->getMessage() . "</error>" ); + } + catch ( \eZ\Publish\API\Repository\Exceptions\ForbiddenException $e ) + { + $output->writeln( "<error>BLAH\n" . $e->getMessage() . "</error>" ); + print_r( $e ); + } + } +}