|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2025 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Media processor for CSV imports |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Base |
|
22
|
|
|
implements \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** controller/jobs/product/import/csv/processor/media/name |
|
25
|
|
|
* Name of the media processor implementation |
|
26
|
|
|
* |
|
27
|
|
|
* Use "Myname" if your class is named "\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Media\Myname". |
|
28
|
|
|
* The name is case-sensitive and you should avoid camel case names like "MyName". |
|
29
|
|
|
* |
|
30
|
|
|
* @param string Last part of the processor class name |
|
31
|
|
|
* @since 2015.10 |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
private ?array $listTypes = null; |
|
35
|
|
|
private array $types = []; |
|
36
|
|
|
private array $mimes = []; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Initializes the object |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
|
43
|
|
|
* @param array $mapping Associative list of field position in CSV as key and domain item key as value |
|
44
|
|
|
* @param \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object Decorated processor |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct( \Aimeos\MShop\ContextIface $context, array $mapping, |
|
47
|
|
|
?\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $object = null ) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct( $context, $mapping, $object ); |
|
50
|
|
|
|
|
51
|
|
|
$config = $context->config(); |
|
52
|
|
|
$this->mimes = array_flip( $config->get( 'mshop/media/manager/extensions', [] ) ); |
|
53
|
|
|
|
|
54
|
|
|
/** controller/jobs/product/import/csv/media/listtypes |
|
55
|
|
|
* Names of the product list types for media that are updated or removed |
|
56
|
|
|
* |
|
57
|
|
|
* If you want to associate media items manually via the administration |
|
58
|
|
|
* interface to products and don't want these to be touched during the |
|
59
|
|
|
* import, you can specify the product list types for these media |
|
60
|
|
|
* that shouldn't be updated or removed. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array|null List of product list type names or null for all |
|
63
|
|
|
* @since 2015.05 |
|
64
|
|
|
* @see controller/jobs/product/import/csv/domains |
|
65
|
|
|
* @see controller/jobs/product/import/csv/separator |
|
66
|
|
|
* @see controller/jobs/product/import/csv/attribute/listtypes |
|
67
|
|
|
* @see controller/jobs/product/import/csv/catalog/listtypes |
|
68
|
|
|
* @see controller/jobs/product/import/csv/product/listtypes |
|
69
|
|
|
* @see controller/jobs/product/import/csv/price/listtypes |
|
70
|
|
|
* @see controller/jobs/product/import/csv/supplier/listtypes |
|
71
|
|
|
* @see controller/jobs/product/import/csv/text/listtypes |
|
72
|
|
|
*/ |
|
73
|
|
|
$default = $config->get( 'controller/jobs/product/import/csv/processor/media/listtypes' ); |
|
74
|
|
|
$this->listTypes = $config->get( 'controller/jobs/product/import/csv/media/listtypes', $default ); |
|
75
|
|
|
|
|
76
|
|
|
if( $this->listTypes === null ) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->listTypes = []; |
|
79
|
|
|
$manager = \Aimeos\MShop::create( $context, 'product/lists/type' ); |
|
80
|
|
|
$search = $manager->filter()->slice( 0, 0x7fffffff ); |
|
81
|
|
|
|
|
82
|
|
|
foreach( $manager->search( $search ) as $item ) { |
|
83
|
|
|
$this->listTypes[$item->getCode()] = $item->getCode(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
else |
|
87
|
|
|
{ |
|
88
|
|
|
$this->listTypes = array_combine( $this->listTypes, $this->listTypes ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$manager = \Aimeos\MShop::create( $context, 'media/type' ); |
|
93
|
|
|
$search = $manager->filter()->slice( 0, 0x7fffffff ); |
|
94
|
|
|
|
|
95
|
|
|
foreach( $manager->search( $search ) as $item ) { |
|
96
|
|
|
$this->types[$item->getCode()] = $item->getCode(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Saves the product related data to the storage |
|
103
|
|
|
* |
|
104
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items |
|
105
|
|
|
* @param array $data List of CSV fields with position as key and data as value |
|
106
|
|
|
* @return array List of data which hasn't been imported |
|
107
|
|
|
*/ |
|
108
|
|
|
public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) : array |
|
109
|
|
|
{ |
|
110
|
|
|
$context = $this->context(); |
|
111
|
|
|
$manager = \Aimeos\MShop::create( $context, 'product' ); |
|
112
|
|
|
$refManager = \Aimeos\MShop::create( $context, 'media' ); |
|
113
|
|
|
$separator = $context->config()->get( 'controller/jobs/product/import/csv/separator', "\n" ); |
|
114
|
|
|
|
|
115
|
|
|
$pos = 0; |
|
116
|
|
|
$listMap = []; |
|
117
|
|
|
$map = $this->getMappedChunk( $data, $this->getMapping() ); |
|
118
|
|
|
$listItems = $product->getListItems( 'media', $this->listTypes, null, false ); |
|
119
|
|
|
|
|
120
|
|
|
foreach( $listItems as $listItem ) |
|
121
|
|
|
{ |
|
122
|
|
|
if( ( $refItem = $listItem->getRefItem() ) !== null ) { |
|
123
|
|
|
$listMap[$refItem->getUrl()][$refItem->getType()][$refItem->getLanguageId()][$listItem->getType()] = $listItem; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
foreach( $map as $list ) |
|
128
|
|
|
{ |
|
129
|
|
|
if( $this->checkEntry( $list ) === false ) { |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$type = trim( $this->val( $list, 'media.type', 'default' ) ); |
|
134
|
|
|
$langId = trim( $this->val( $list, 'media.languageid', '' ) ); |
|
135
|
|
|
$listtype = trim( $this->val( $list, 'product.lists.type', 'default' ) ); |
|
136
|
|
|
$listConfig = $this->getListConfig( trim( $this->val( $list, 'product.lists.config', '' ) ) ); |
|
137
|
|
|
|
|
138
|
|
|
$urls = explode( $separator, trim( $this->val( $list, 'media.url', '' ) ) ); |
|
139
|
|
|
unset( $list['media.url'] ); |
|
140
|
|
|
|
|
141
|
|
|
$this->addType( 'product/lists/type', 'media', $listtype ); |
|
142
|
|
|
$this->addType( 'media/type', 'product', $type ); |
|
143
|
|
|
|
|
144
|
|
|
foreach( $urls as $url ) |
|
145
|
|
|
{ |
|
146
|
|
|
$url = trim( $url ); |
|
147
|
|
|
|
|
148
|
|
|
if( isset( $listMap[$url][$type][$langId][$listtype] ) ) |
|
149
|
|
|
{ |
|
150
|
|
|
$listItem = $listMap[$url][$type][$langId][$listtype]; |
|
151
|
|
|
$refItem = $listItem->getRefItem(); |
|
152
|
|
|
unset( $listItems[$listItem->getId()] ); |
|
153
|
|
|
} |
|
154
|
|
|
else |
|
155
|
|
|
{ |
|
156
|
|
|
$listItem = $manager->createListItem()->setType( $listtype ); |
|
|
|
|
|
|
157
|
|
|
$refItem = $refManager->create()->setType( $type ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$ext = strtolower( pathinfo( $url, PATHINFO_EXTENSION ) ); |
|
|
|
|
|
|
161
|
|
|
if( isset( $this->mimes[$ext] ) ) { |
|
162
|
|
|
$refItem->setMimeType( $this->mimes[$ext] ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$refItem->setDomain( 'product' ); |
|
166
|
|
|
$refItem = $this->update( $refItem, $list, $url ); |
|
167
|
|
|
$listItem = $listItem->setPosition( $pos++ )->fromArray( $list )->setConfig( $listConfig ); |
|
168
|
|
|
|
|
169
|
|
|
$product->addListItem( 'media', $listItem, $refItem ); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$product->deleteListItems( $listItems->toArray(), true ); |
|
174
|
|
|
|
|
175
|
|
|
return $this->object()->process( $product, $data ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Checks if an entry can be used for updating a media item |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $list Associative list of key/value pairs from the mapping |
|
183
|
|
|
* @return bool True if valid, false if not |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function checkEntry( array $list ) : bool |
|
186
|
|
|
{ |
|
187
|
|
|
if( $this->val( $list, 'media.url' ) === null ) { |
|
188
|
|
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if( ( $type = trim( $this->val( $list, 'product.lists.type', 'default' ) ) ) && !isset( $this->listTypes[$type] ) ) |
|
192
|
|
|
{ |
|
193
|
|
|
$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'product list' ); |
|
194
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
if( ( $type = trim( $this->val( $list, 'media.type', 'default' ) ) ) && !isset( $this->types[$type] ) ) |
|
198
|
|
|
{ |
|
199
|
|
|
$msg = sprintf( 'Invalid type "%1$s" (%2$s)', $type, 'media' ); |
|
200
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return true; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Updates the media item with the given key/value pairs |
|
209
|
|
|
* |
|
210
|
|
|
* @param \Aimeos\MShop\Media\Item\Iface $refItem Media item to update |
|
211
|
|
|
* @param array &$list Associative list of key/value pairs, matching pairs are removed |
|
212
|
|
|
* @return \Aimeos\MShop\Media\Item\Iface Updated media item |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function update( \Aimeos\MShop\Media\Item\Iface $refItem, array &$list, string $url ) : \Aimeos\MShop\Media\Item\Iface |
|
215
|
|
|
{ |
|
216
|
|
|
try |
|
217
|
|
|
{ |
|
218
|
|
|
if( isset( $list['media.previews'] ) && ( $map = json_decode( $list['media.previews'], true ) ) !== null ) { |
|
219
|
|
|
$refItem->setPreviews( $map )->setUrl( $url ); |
|
220
|
|
|
} elseif( isset( $list['media.preview'] ) ) { |
|
221
|
|
|
$refItem->setPreview( $list['media.preview'] )->setUrl( $url ); |
|
222
|
|
|
} elseif( $refItem->getUrl() !== $url ) { |
|
223
|
|
|
$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ), true ); |
|
224
|
|
|
} else { |
|
225
|
|
|
$refItem = \Aimeos\MShop::create( $this->context(), 'media' )->scale( $refItem->setUrl( $url ) ); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
unset( $list['media.previews'], $list['media.preview'] ); |
|
229
|
|
|
} |
|
230
|
|
|
catch( \Exception $e ) |
|
231
|
|
|
{ |
|
232
|
|
|
$msg = sprintf( 'Scaling image "%1$s" failed: %2$s', $url, $e->getMessage() ); |
|
233
|
|
|
$this->context()->logger()->error( $msg, 'import/csv/product' ); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $refItem->fromArray( $list ); |
|
237
|
|
|
} |
|
238
|
|
|
} |
|
239
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.