Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Content type edition examples #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions Command/AddTextLineFieldDefinitionToContentTypeDraftCommand.php
Original file line number Diff line number Diff line change
@@ -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 );
}
}
}
76 changes: 76 additions & 0 deletions Command/CreateContentTypeDraftCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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 CreateContentTypeDraftCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->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( "<info>Content type draft created with ID $contentTypeDraft->id" );
}
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>" );
}
}
}
57 changes: 57 additions & 0 deletions Command/DeleteContentTypeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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 eZ\Publish\API\Repository\Exceptions\BadStateException;
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 DeleteContentTypeCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->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( "<info>Content type $contentType->id was deleted" );
}
catch ( \eZ\Publish\API\Repository\Exceptions\NotFoundException $e )
{
$output->writeln( "<error>Content type with identifier not found</error>" );
return;
}
catch ( BadStateException $e )
{
$output->writeln( "<error>This content type can not be deleted as there are objects of this type</error>" );
return;
}
}
}
92 changes: 92 additions & 0 deletions Command/UpdateContentTypeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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 UpdateContentTypeCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->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: <title>' ),
)
);
}

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>" );
}
}
}