|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dynamic\Salsify\TypeHandler\Asset; |
|
4
|
|
|
|
|
5
|
|
|
use Dynamic\Salsify\Task\ImportTask; |
|
6
|
|
|
use SilverStripe\Assets\Image; |
|
7
|
|
|
use SilverStripe\ORM\DataObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ImageHandler |
|
11
|
|
|
* @package Dynamic\Salsify\TypeHandler |
|
12
|
|
|
* |
|
13
|
|
|
* @property-read \Dynamic\Salsify\Model\Mapper|ImageHandler $owner |
|
14
|
|
|
*/ |
|
15
|
|
|
class ImageHandler extends AssetHandler |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $field_types = [ |
|
|
|
|
|
|
22
|
|
|
'Image' => [ |
|
23
|
|
|
'requiresWrite' => true, |
|
24
|
|
|
'requiresSalsifyObjects' => false, |
|
25
|
|
|
'allowsModification' => true, |
|
26
|
|
|
], |
|
27
|
|
|
'ManyImages' => [ |
|
28
|
|
|
'requiresWrite' => true, |
|
29
|
|
|
'requiresSalsifyObjects' => false, |
|
30
|
|
|
'allowsModification' => true, |
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $defaultImageType = 'png'; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $url |
|
41
|
|
|
* @param array|string $transformations |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getImageTransformation($url, $transformations) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
if (!is_array($transformations)) { |
|
46
|
|
|
$transformations = [$transformations]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return implode(',', $transformations); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $url |
|
54
|
|
|
* @param string $transform |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getImageTransformationURL($url, $transform) |
|
57
|
|
|
{ |
|
58
|
|
|
$filePath = preg_split('|^(.*[\\\/])|', $url, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY)[0]; |
|
59
|
|
|
$fileName = basename($url); |
|
60
|
|
|
|
|
61
|
|
|
return "{$filePath}{$transform}/{$fileName}"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string|DataObject $class |
|
66
|
|
|
* @param $data |
|
67
|
|
|
* @param $dataField |
|
68
|
|
|
* @param $config |
|
69
|
|
|
* @param $dbField |
|
70
|
|
|
* @return string|int |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \Exception |
|
73
|
|
|
*/ |
|
74
|
|
|
public function handleImageType($class, $data, $dataField, $config, $dbField) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$assetData = $this->getAssetBySalsifyID($data[$dataField]); |
|
77
|
|
|
if (!$assetData) { |
|
78
|
|
|
return ''; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$url = $this->fixExtension($assetData['salsify:url']); |
|
82
|
|
|
$name = $this->fixExtension($assetData['salsify:name']); |
|
83
|
|
|
$transformation = ''; |
|
84
|
|
|
|
|
85
|
|
|
if (array_key_exists('transform', $config)) { |
|
86
|
|
|
$transformation = $this->getImageTransformation($url, $config['transform']); |
|
87
|
|
|
$url = $this->getImageTransformationURL($url, $transformation); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$asset = $this->updateFile( |
|
91
|
|
|
$assetData['salsify:id'], |
|
92
|
|
|
$assetData['salsify:updated_at'], |
|
93
|
|
|
$url, |
|
94
|
|
|
$name, |
|
95
|
|
|
$config['type'], |
|
96
|
|
|
Image::class, |
|
97
|
|
|
$transformation |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
return preg_match('/ID$/', $dbField) ? $asset->ID : $asset; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function fixExtension($string) |
|
104
|
|
|
{ |
|
105
|
|
|
$supportedImageExtensions = Image::get_category_extensions( |
|
106
|
|
|
Image::singleton()->File->getAllowedCategories() |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$pathinfo = pathinfo($string); |
|
110
|
|
|
$defualtExtension = $this->owner->config()->get('defaultImageType'); |
|
|
|
|
|
|
111
|
|
|
if (!array_key_exists('extension', $pathinfo)) { |
|
|
|
|
|
|
112
|
|
|
return "{$string}.{$defualtExtension}"; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$extension = pathinfo($string)['extension']; |
|
116
|
|
|
|
|
117
|
|
|
if (!in_array($extension, $supportedImageExtensions)) { |
|
118
|
|
|
return str_replace( |
|
119
|
|
|
".{$extension}", |
|
120
|
|
|
".{$defualtExtension}", |
|
121
|
|
|
$string |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $string; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string|DataObject $class |
|
130
|
|
|
* @param $data |
|
131
|
|
|
* @param $dataField |
|
132
|
|
|
* @param $config |
|
133
|
|
|
* @param $dbField |
|
134
|
|
|
* @return array |
|
135
|
|
|
* |
|
136
|
|
|
* @throws \Exception |
|
137
|
|
|
*/ |
|
138
|
|
|
public function handleManyImagesType($class, $data, $dataField, $config, $dbField) |
|
139
|
|
|
{ |
|
140
|
|
|
$files = []; |
|
141
|
|
|
$fieldData = $data[$dataField]; |
|
142
|
|
|
// convert to array to prevent problems |
|
143
|
|
|
if (!is_array($fieldData)) { |
|
144
|
|
|
$fieldData = [$fieldData]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
foreach ($this->owner->yieldSingle($fieldData) as $fileID) { |
|
|
|
|
|
|
148
|
|
|
$entryData = array_merge($data, [ |
|
149
|
|
|
$dataField => $fileID |
|
150
|
|
|
]); |
|
151
|
|
|
$files[] = $this->owner->handleImageType($class, $entryData, $dataField, $config, $dbField, $class); |
|
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
return $files; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|