|
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
|
|
|
public function hashToFieldValue($fieldValue, array $context = array()) |
|
18
|
|
|
{ |
|
19
|
|
|
$altText = ''; |
|
20
|
|
|
$fileName = ''; |
|
21
|
|
|
|
|
22
|
|
|
if ($fieldValue === null) { |
|
23
|
|
|
return new ImageValue(); |
|
24
|
|
View Code Duplication |
} else if (is_string($fieldValue)) { |
|
|
|
|
|
|
25
|
|
|
$filePath = $fieldValue; |
|
26
|
|
|
} else { |
|
27
|
|
|
$filePath = $fieldValue['path']; |
|
28
|
|
|
if (isset($fieldValue['alt_text'])) { |
|
29
|
|
|
$altText = $fieldValue['alt_text']; |
|
30
|
|
|
} |
|
31
|
|
|
if (isset($fieldValue['filename'])) { |
|
32
|
|
|
$fileName = $fieldValue['filename']; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// default format: path is relative to the 'images' dir |
|
37
|
|
|
$realFilePath = dirname($context['path']) . '/images/' . $filePath; |
|
38
|
|
|
|
|
39
|
|
|
// but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well |
|
40
|
|
|
if (!is_file($realFilePath) && is_file($filePath)) { |
|
41
|
|
|
$realFilePath = $filePath; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new ImageValue( |
|
45
|
|
|
array( |
|
46
|
|
|
'path' => $realFilePath, |
|
47
|
|
|
'fileSize' => filesize($realFilePath), |
|
48
|
|
|
'fileName' => $fileName != '' ? $fileName : basename($realFilePath), |
|
49
|
|
|
'alternativeText' => $altText |
|
50
|
|
|
) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param \eZ\Publish\Core\FieldType\Image\Value $fieldValue |
|
56
|
|
|
* @param array $context |
|
57
|
|
|
* @return array |
|
58
|
|
|
* |
|
59
|
|
|
* @todo check out if this works in ezplatform |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function fieldValueToHash($fieldValue, array $context = array()) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
return array( |
|
64
|
|
|
'path' => realpath($this->ioRootDir) . '/' . ($this->ioDecorator ? $this->ioDecorator->undecorate($fieldValue->uri) : $fieldValue->uri), |
|
65
|
|
|
'filename'=> $fieldValue->fileName, |
|
66
|
|
|
'alternativeText' => $fieldValue->alternativeText |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.