Skip to content

Instantly share code, notes, and snippets.

@dpobel
Created October 13, 2011 07:51
Show Gist options
  • Select an option

  • Save dpobel/1283679 to your computer and use it in GitHub Desktop.

Select an option

Save dpobel/1283679 to your computer and use it in GitHub Desktop.
<?php
require_once 'extension/api/testsBootstrap.php';
try
{
$handler = new \ezp\Persistence\Storage\Legacy\Handler( array( 'dsn' => 'mysql://muser:mpasswd@localhost/mdatabase' ) );
$sc = new ezp\Base\ServiceContainer( \ezp\Base\Configuration::getInstance( 'service' )->getAll(), array( '@persistence_handler' => $handler ) );
$repository = $sc->getRepository();
$converter = $handler->getFieldValueConverterRegistry();
$converter->register( 'ezstring', new \ezp\Persistence\Storage\Legacy\Content\FieldValue\Converter\TextLine() );
$converter->register( 'ezxmltext', new \ezp\Persistence\Storage\Legacy\Content\FieldValue\Converter\XmlText() );
$converter->register( 'ezboolean', new \ezp\Persistence\Storage\Legacy\Content\FieldValue\Converter\Checkbox() );
$converter->register( 'ezinteger', new \ezp\Persistence\Storage\Legacy\Content\FieldValue\Converter\Integer() );
$userService = $repository->getUserService();
$repository->setUser( $userService->load( 14 ) );
$contentService = $repository->getContentService();
$typeService = $repository->getContentTypeService();
$section = $repository->getSectionService()->load( 1 );
$contentType = new \ezp\Content\Type\Concrete();
$contentType->identifier = 'an_integer_class';
$contentType->name = array( 'eng-GB' => 'An IntegerClass' );
$contentType->description = array( 'eng-GB' => 'This is an Integer class test' );
$contentType->nameSchema = '<name>';
$contentType->urlAliasSchema = '';
$contentType->isContainer = false;
$contentType->initialLanguageId = 2;
$contentType->creatorId = $contentType->modifierId = 14;
$contentType->sortField = \ezp\Content\Location::SORT_FIELD_PATH;
$contentType->sortOrder = \ezp\Content\Location::SORT_ORDER_ASC;
$fields = array(
'name' => array(
'fieldType' => 'ezstring',
'name' => array( 'eng-GB' => 'Name' ),
'description' => array( 'eng-GB' => 'This is a test' ),
'position' => 0,
'fieldGroup' => '',
'isRequired' => false,
'isSearchable' => true,
'isInfoCollector' => false,
'isTranslatable' => true,
'fieldSettings' => array(),
'validators' => array()
),
'int' => array(
'fieldType' => 'ezinteger',
'name' => array( 'eng-GB' => 'Integer' ),
'description' => array( 'eng-GB' => 'This is an Integer test' ),
'position' => 1,
'fieldGroup' => 'meta',
'isRequired' => true,
'isSearchable' => true,
'isInfoCollector' => false,
'isTranslatable' => true,
'defaultValue' => new \ezp\Content\FieldType\Integer\Value( 0 ),
'fieldSettings' => array(),
'validators' => array(
'\ezp\Content\FieldType\Integer\IntegerValueValidator' => array(
'minIntegerValue' => -6,
'maxIntegerValue' => 6
)
)
)
);
$addFields = array();
foreach ( $fields as $identifier => $data )
{
$field = new \ezp\Content\Type\FieldDefinition( $contentType, $data['fieldType'] );
$field->identifier = $identifier;
$field->name = $data['name'];
$field->description = $data['description'];
$field->position = $data['position'];
$field->fieldGroup = $data['fieldGroup'];
$field->isRequired = $data['isRequired'];
$field->isSearchable = $data['isSearchable'];
$field->isInfoCollector = $data['isInfoCollector'];
$field->isTranslatable = $data['isTranslatable'];
if ( isset( $data['defaultValue'] ) )
$field->setDefaultValue( $data['defaultValue'] );
foreach ( $data['fieldSettings'] as $fieldSetting => $value )
{
$field->setFieldSetting( $fieldSetting, $value );
}
if ( isset( $data['validators'] ) )
{
foreach ( $data['validators'] as $validatorName => $constraints )
{
$validator = new $validatorName;
$validator->initializeWithConstraints( $constraints );
$field->addValidator( $validator );
}
}
$addFields[] = $field;
}
$linkGroups = array( $typeService->loadGroup( 1 ) );
$contentType = $typeService->createAndPublish( $contentType, $linkGroups, $addFields );
$type = $typeService->load( $contentType->id );
//$type->fields[1]->validators[0]->initializeWithConstraints( array( 'minIntegerValue' => -5 ) );
//$type->fields[1]->validators[0]->minIntegerValue = -5;
$typeService->updateFieldDefinition( $type, $type->fields[1] );
$typeService->update( $type );
}
catch( Exception $e )
{
$cli->error( 'Exception: ' . get_class( $e ) );
$cli->error( $e->getMessage() );
$cli->error();
$cli->error( $e->getTraceAsString() );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment