|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\FieldHandler; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\Core\FieldType\Image\Value as ImageValue; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\FieldValueConverterInterface; |
|
7
|
|
|
|
|
8
|
|
|
class EzImage extends FileFieldHandler implements FieldValueConverterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Creates a value object to use as the field value when setting an image field type. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array|string $fieldValue The path to the file or an array with 'path' and 'alt_text' keys |
|
14
|
|
|
* @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration |
|
15
|
|
|
* @return ImageValue |
|
16
|
|
|
* |
|
17
|
|
|
* @todo resolve refs more |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function hashToFieldValue($fieldValue, array $context = array()) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$altText = ''; |
|
22
|
1 |
|
$fileName = ''; |
|
23
|
|
|
|
|
24
|
1 |
|
if ($fieldValue === null) { |
|
|
|
|
|
|
25
|
1 |
|
return new ImageValue(); |
|
|
|
|
|
|
26
|
1 |
|
} else if (is_string($fieldValue)) { |
|
27
|
1 |
|
$filePath = $fieldValue; |
|
28
|
|
|
} else { |
|
29
|
1 |
|
$filePath = $fieldValue['path']; |
|
30
|
1 |
|
if (isset($fieldValue['alt_text'])) { |
|
31
|
1 |
|
$altText = $fieldValue['alt_text']; |
|
32
|
|
|
} |
|
33
|
1 |
|
if (isset($fieldValue['filename'])) { |
|
34
|
1 |
|
$fileName = $fieldValue['filename']; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// default format: path is relative to the 'images' dir |
|
39
|
1 |
|
$realFilePath = dirname($context['path']) . '/images/' . $filePath; |
|
40
|
|
|
|
|
41
|
|
|
// but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well |
|
42
|
1 |
|
if (!is_file($realFilePath) && is_file($filePath)) { |
|
43
|
|
|
$realFilePath = $filePath; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
return new ImageValue( |
|
|
|
|
|
|
47
|
|
|
array( |
|
48
|
1 |
|
'path' => $realFilePath, |
|
49
|
1 |
|
'fileSize' => filesize($realFilePath), |
|
50
|
1 |
|
'fileName' => $fileName != '' ? $fileName : basename($realFilePath), |
|
51
|
1 |
|
'alternativeText' => $altText |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue |
|
58
|
|
|
* @param array $context |
|
59
|
|
|
* @return array |
|
60
|
|
|
* |
|
61
|
|
|
* @todo check out if this works in ezplatform |
|
62
|
|
|
*/ |
|
63
|
|
|
public function fieldValueToHash($fieldValue, array $context = array()) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($fieldValue->uri == null) { |
|
66
|
|
|
return null; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return array( |
|
70
|
|
|
'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri), |
|
71
|
|
|
'filename'=> $fieldValue->fileName, |
|
72
|
|
|
'alternativeText' => $fieldValue->alternativeText |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|