Skip to content

Commit

Permalink
Fix generation of content migrations with date, datetime, file, image…
Browse files Browse the repository at this point in the history
… attributes
  • Loading branch information
gggeek committed Feb 5, 2017
1 parent 2d125de commit 510a237
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
27 changes: 25 additions & 2 deletions Core/ComplexField/EzBinaryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
namespace Kaliop\eZMigrationBundle\Core\ComplexField;

use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFileValue;
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;

class EzBinaryFile extends AbstractComplexField implements FieldValueImporterInterface
class EzBinaryFile extends AbstractComplexField implements FieldValueConverterInterface
{
protected $legacyRootDir;

public function __construct($legacyRootDir)
{
$this->legacyRootDir = $legacyRootDir;
}

/**
* @param array|string $fieldValue The path to the file or an array with 'path' key
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
Expand Down Expand Up @@ -48,4 +55,20 @@ public function hashToFieldValue($fieldValue, array $context = array())
)
);
}

/**
* @param \eZ\Publish\Core\FieldType\BinaryFile\Value $fieldValue
* @param array $context
* @return array
*
* @todo check out if this works in ezplatform
*/
public function fieldValueToHash($fieldValue, array $context = array())
{
return array(
'path' => realpath($this->legacyRootDir) . $fieldValue->uri,
'filename'=> $fieldValue->fileName,
'mimeType' => $fieldValue->mimeType
);
}
}
7 changes: 6 additions & 1 deletion Core/ComplexField/EzDateAndTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public function fieldSettingsToHash($settingsValue, array $context = array())
public function hashToFieldSettings($settingsHash, array $context = array())
{
if (is_array($settingsHash) && isset($settingsHash['dateInterval']) && is_string($settingsHash['dateInterval'])) {
$settingsHash['dateInterval'] = \DateInterval::createFromDateString($settingsHash['dateInterval']);
// ISO-8601 format, loose matching because I am lazy
if (preg_match('/^P[0-9YMDWTHS]+$/', $settingsHash['dateInterval'])) {
$settingsHash['dateInterval'] = new \DateInterval($settingsHash['dateInterval']);
} else {
$settingsHash['dateInterval'] = \DateInterval::createFromDateString($settingsHash['dateInterval']);
}
}
return $settingsHash;
}
Expand Down
27 changes: 25 additions & 2 deletions Core/ComplexField/EzImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
namespace Kaliop\eZMigrationBundle\Core\ComplexField;

use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface;

class EzImage extends AbstractComplexField implements FieldValueImporterInterface
class EzImage extends AbstractComplexField implements FieldValueConverterInterface
{
protected $legacyRootDir;

public function __construct($legacyRootDir)
{
$this->legacyRootDir = $legacyRootDir;
}

/**
* Creates a value object to use as the field value when setting an image field type.
*
Expand Down Expand Up @@ -50,4 +57,20 @@ public function hashToFieldValue($fieldValue, array $context = array())
)
);
}

/**
* @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue
* @param array $context
* @return array
*
* @todo check out if this works in ezplatform
*/
public function fieldValueToHash($fieldValue, array $context = array())
{
return array(
'path' => realpath($this->legacyRootDir) . $fieldValue->uri,
'filename'=> $fieldValue->fileName,
'alternativeText' => $fieldValue->alternativeText
);
}
}
8 changes: 8 additions & 0 deletions Core/ComplexField/EzXmlText.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function hashToFieldValue($fieldValue, array $context = array())
{
if (is_string($fieldValue)) {
$xmlText = $fieldValue;
} else if (is_array($fieldValue) && isset($fieldValue['xml'])) {
// native export format from eZ
$xmlText = $fieldValue['xml'];
} else {
$xmlText = $fieldValue['content'];
}
Expand Down Expand Up @@ -55,6 +58,11 @@ public function fieldSettingsToHash($settingsValue, array $context = array())
{
// work around https://jira.ez.no/browse/EZP-26916
if (is_array($settingsValue) && isset($settingsValue['tagPreset'])) {
/// @todo this conversion should be based on the value of TagPresets ini legacy setting in ezxml.ini,
/// keeping in mind that at migration execution time only values 0 and 1 are supported anyway...
if ($settingsValue['tagPreset'] == 'mini') {
$settingsValue['tagPreset'] = 1;
}
$settingsValue['tagPreset'] = (integer)$settingsValue['tagPreset'];
}
return $settingsValue;
Expand Down
9 changes: 5 additions & 4 deletions Core/Executor/ContentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,11 @@ public function generateMigration(array $matchCondition, $mode)

if ($mode != 'delete') {
$attributes = array();
foreach ($content->getFieldsByLanguage($this->getLanguageCode()) as $field) {
$fieldDefinition = $contentType->getFieldDefinition($field->fieldDefIdentifier);
$fieldType = $fieldTypeService->getFieldType($fieldDefinition->fieldTypeIdentifier);
$attributes[$field->fieldDefIdentifier] = $fieldType->toHash($field->value);
foreach ($content->getFieldsByLanguage($this->getLanguageCode()) as $fieldIdentifier => $field) {
$fieldDefinition = $contentType->getFieldDefinition($fieldIdentifier);
$attributes[$field->fieldDefIdentifier] = $this->complexFieldManager->fieldValueToHash(
$fieldDefinition->fieldTypeIdentifier, $contentType->identifier, $field->value
);
}

$contentData = array_merge(
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ services:
ez_migration_bundle.complex_field.ezbinaryfile:
parent: ez_migration_bundle.complex_field
class: '%ez_migration_bundle.complex_field.ezbinaryfile.class%'
arguments:
- "%ezpublish_legacy.root_dir%"
tags:
- { name: ez_migration_bundle.complex_field, fieldtype: ezbinaryfile, priority: 0 }

Expand All @@ -372,6 +374,8 @@ services:
ez_migration_bundle.complex_field.ezimage:
parent: ez_migration_bundle.complex_field
class: '%ez_migration_bundle.complex_field.ezimage.class%'
arguments:
- "%ezpublish_legacy.root_dir%"
tags:
- { name: ez_migration_bundle.complex_field, fieldtype: ezimage, priority: 0 }

Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/DSL/ManageContent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
lines, but new line is not preserved on parsing
# ezimage/ezbinaryfile file attribute example
attribute5:
path: 'path/to/image' # Relative path from MigrationsVersions/images/ or MigrationsVersions/files/
path: 'path/to/image' # Relative path from MigrationsVersions/images/ or MigrationsVersions/files/, or absolute path
alt_text: xyz # Optional, only used for images
filename: abc # Optional, taken from the actual file if not specified
mime_type: def # Optional. For ezbinaryfile only. If not specified, the actual file is analyzed using the php function mime_content_type()
# ezimage/ezbinaryfile file attribute example, using simplified form
attribute5b: 'path/to/image'
attribute5b: '/path/to/image'
# ezxmltext, showing an example of using references
attribute6: '<section><paragraph><embed view="embed" size="medium" object_id="[reference:example_reference]" /></paragraph></section>'
# Adding values to an ezauthor field
Expand Down

0 comments on commit 510a237

Please sign in to comment.