1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2024 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Jobs |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Product\Import\Csv; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Job controller for CSV product imports. |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Jobs |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Base |
22
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
23
|
|
|
{ |
24
|
|
|
/** controller/jobs/product/import/csv/name |
25
|
|
|
* Class name of the used product suggestions scheduler controller implementation |
26
|
|
|
* |
27
|
|
|
* Each default job controller can be replace by an alternative imlementation. |
28
|
|
|
* To use this implementation, you have to set the last part of the class |
29
|
|
|
* name as configuration value so the controller factory knows which class it |
30
|
|
|
* has to instantiate. |
31
|
|
|
* |
32
|
|
|
* For example, if the name of the default class is |
33
|
|
|
* |
34
|
|
|
* \Aimeos\Controller\Jobs\Product\Import\Csv\Standard |
35
|
|
|
* |
36
|
|
|
* and you want to replace it with your own version named |
37
|
|
|
* |
38
|
|
|
* \Aimeos\Controller\Jobs\Product\Import\Csv\Mycsv |
39
|
|
|
* |
40
|
|
|
* then you have to set the this configuration option: |
41
|
|
|
* |
42
|
|
|
* controller/jobs/product/import/csv/name = Mycsv |
43
|
|
|
* |
44
|
|
|
* The value is the last part of your own class name and it's case sensitive, |
45
|
|
|
* so take care that the configuration value is exactly named like the last |
46
|
|
|
* part of the class name. |
47
|
|
|
* |
48
|
|
|
* The allowed characters of the class name are A-Z, a-z and 0-9. No other |
49
|
|
|
* characters are possible! You should always start the last part of the class |
50
|
|
|
* name with an upper case character and continue only with lower case characters |
51
|
|
|
* or numbers. Avoid chamel case names like "MyCsv"! |
52
|
|
|
* |
53
|
|
|
* @param string Last part of the class name |
54
|
|
|
* @since 2015.01 |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
/** controller/jobs/product/import/csv/decorators/excludes |
58
|
|
|
* Excludes decorators added by the "common" option from the product import CSV job controller |
59
|
|
|
* |
60
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
61
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
62
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
63
|
|
|
* modify what is returned to the caller. |
64
|
|
|
* |
65
|
|
|
* This option allows you to remove a decorator added via |
66
|
|
|
* "controller/jobs/common/decorators/default" before they are wrapped |
67
|
|
|
* around the job controller. |
68
|
|
|
* |
69
|
|
|
* controller/jobs/product/import/csv/decorators/excludes = array( 'decorator1' ) |
70
|
|
|
* |
71
|
|
|
* This would remove the decorator named "decorator1" from the list of |
72
|
|
|
* common decorators ("\Aimeos\Controller\Jobs\Common\Decorator\*") added via |
73
|
|
|
* "controller/jobs/common/decorators/default" to the job controller. |
74
|
|
|
* |
75
|
|
|
* @param array List of decorator names |
76
|
|
|
* @since 2015.01 |
77
|
|
|
* @see controller/jobs/common/decorators/default |
78
|
|
|
* @see controller/jobs/product/import/csv/decorators/global |
79
|
|
|
* @see controller/jobs/product/import/csv/decorators/local |
80
|
|
|
*/ |
81
|
|
|
|
82
|
|
|
/** controller/jobs/product/import/csv/decorators/global |
83
|
|
|
* Adds a list of globally available decorators only to the product import CSV job controller |
84
|
|
|
* |
85
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
86
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
87
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
88
|
|
|
* modify what is returned to the caller. |
89
|
|
|
* |
90
|
|
|
* This option allows you to wrap global decorators |
91
|
|
|
* ("\Aimeos\Controller\Jobs\Common\Decorator\*") around the job controller. |
92
|
|
|
* |
93
|
|
|
* controller/jobs/product/import/csv/decorators/global = array( 'decorator1' ) |
94
|
|
|
* |
95
|
|
|
* This would add the decorator named "decorator1" defined by |
96
|
|
|
* "\Aimeos\Controller\Jobs\Common\Decorator\Decorator1" only to the job controller. |
97
|
|
|
* |
98
|
|
|
* @param array List of decorator names |
99
|
|
|
* @since 2015.01 |
100
|
|
|
* @see controller/jobs/common/decorators/default |
101
|
|
|
* @see controller/jobs/product/import/csv/decorators/excludes |
102
|
|
|
* @see controller/jobs/product/import/csv/decorators/local |
103
|
|
|
*/ |
104
|
|
|
|
105
|
|
|
/** controller/jobs/product/import/csv/decorators/local |
106
|
|
|
* Adds a list of local decorators only to the product import CSV job controller |
107
|
|
|
* |
108
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
109
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
110
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
111
|
|
|
* modify what is returned to the caller. |
112
|
|
|
* |
113
|
|
|
* This option allows you to wrap local decorators |
114
|
|
|
* ("\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\*") around the job |
115
|
|
|
* controller. |
116
|
|
|
* |
117
|
|
|
* controller/jobs/product/import/csv/decorators/local = array( 'decorator2' ) |
118
|
|
|
* |
119
|
|
|
* This would add the decorator named "decorator2" defined by |
120
|
|
|
* "\Aimeos\Controller\Jobs\Product\Import\Csv\Decorator\Decorator2" |
121
|
|
|
* only to the job controller. |
122
|
|
|
* |
123
|
|
|
* @param array List of decorator names |
124
|
|
|
* @since 2015.01 |
125
|
|
|
* @see controller/jobs/common/decorators/default |
126
|
|
|
* @see controller/jobs/product/import/csv/decorators/excludes |
127
|
|
|
* @see controller/jobs/product/import/csv/decorators/global |
128
|
|
|
*/ |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
private ?array $types = null; |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns the localized name of the job. |
136
|
|
|
* |
137
|
|
|
* @return string Name of the job |
138
|
|
|
*/ |
139
|
|
|
public function getName() : string |
140
|
|
|
{ |
141
|
|
|
return $this->context()->translate( 'controller/jobs', 'Product import CSV' ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Returns the localized description of the job. |
147
|
|
|
* |
148
|
|
|
* @return string Description of the job |
149
|
|
|
*/ |
150
|
|
|
public function getDescription() : string |
151
|
|
|
{ |
152
|
|
|
return $this->context()->translate( 'controller/jobs', 'Imports new and updates existing products from CSV files' ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Executes the job. |
158
|
|
|
* |
159
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
160
|
|
|
*/ |
161
|
|
|
public function run() |
162
|
|
|
{ |
163
|
|
|
$context = $this->context(); |
164
|
|
|
$logger = $context->logger(); |
165
|
|
|
$date = date( 'Y-m-d H:i:s' ); |
166
|
|
|
|
167
|
|
|
try |
168
|
|
|
{ |
169
|
|
|
$files = $errors = 0; |
170
|
|
|
$fs = $context->fs( 'fs-import' ); |
171
|
|
|
$site = $context->locale()->getSiteCode(); |
|
|
|
|
172
|
|
|
$location = $this->location() . '/' . $site; |
173
|
|
|
|
174
|
|
|
foreach( map( $fs->scan( $location ) )->sort() as $filename ) |
175
|
|
|
{ |
176
|
|
|
if( $filename[0] === '.' ) { |
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$path = $location . '/' . $filename; |
181
|
|
|
|
182
|
|
|
if( $fs instanceof \Aimeos\Base\Filesystem\DirIface && $fs->isDir( $path ) ) { |
183
|
|
|
continue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$errors = $this->import( $path ); |
187
|
|
|
$files++; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** controller/jobs/product/import/csv/cleanup |
191
|
|
|
* Deletes all products with categories which havn't been updated |
192
|
|
|
* |
193
|
|
|
* By default, the product importer only adds new and updates existing |
194
|
|
|
* products but doesn't delete any products. If you want to remove all |
195
|
|
|
* products which haven't been updated during the import, then set this |
196
|
|
|
* configuration option to "true". This will remove all products which |
197
|
|
|
* are not assigned to any category but keep the ones without categories, |
198
|
|
|
* e.g. rebate products. |
199
|
|
|
* |
200
|
|
|
* @param bool TRUE to delete all untouched products, FALSE to keep them |
201
|
|
|
* @since 2023.10 |
202
|
|
|
* @see controller/jobs/product/import/csv/backup |
203
|
|
|
* @see controller/jobs/product/import/csv/domains |
204
|
|
|
* @see controller/jobs/product/import/csv/location |
205
|
|
|
* @see controller/jobs/product/import/csv/mapping |
206
|
|
|
* @see controller/jobs/product/import/csv/max-size |
207
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
208
|
|
|
*/ |
209
|
|
|
if( $files && $context->config()->get( 'controller/jobs/product/import/csv/cleanup', false ) ) |
210
|
|
|
{ |
211
|
|
|
$count = $this->cleanup( $date ); |
212
|
|
|
$logger->info( sprintf( 'Cleaned %1$s old products', $count ), 'import/csv/product' ); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if( $errors > 0 ) { |
216
|
|
|
$this->mail( 'Product CSV import', sprintf( 'Invalid product lines during import: %1$d', $errors ) ); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
catch( \Exception $e ) |
220
|
|
|
{ |
221
|
|
|
$logger->error( 'Product import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString(), 'import/csv/product' ); |
222
|
|
|
$this->mail( 'Product CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
223
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() ); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the directory for storing imported files |
230
|
|
|
* |
231
|
|
|
* @return string Directory for storing imported files |
232
|
|
|
*/ |
233
|
|
|
protected function backup() : string |
234
|
|
|
{ |
235
|
|
|
/** controller/jobs/product/import/csv/backup |
236
|
|
|
* Name of the backup for sucessfully imported files |
237
|
|
|
* |
238
|
|
|
* After a CSV file was imported successfully, you can move it to another |
239
|
|
|
* location, so it won't be imported again and isn't overwritten by the |
240
|
|
|
* next file that is stored at the same location in the file system. |
241
|
|
|
* |
242
|
|
|
* You should use an absolute path to be sure but can be relative path |
243
|
|
|
* if you absolutely know from where the job will be executed from. The |
244
|
|
|
* name of the new backup location can contain placeholders understood |
245
|
|
|
* by the PHP DateTime::format() method (with percent signs prefix) to |
246
|
|
|
* create dynamic paths, e.g. "backup/%Y-%m-%d" which would create |
247
|
|
|
* "backup/2000-01-01". For more information about the date() placeholders, |
248
|
|
|
* please have a look into the PHP documentation of the |
249
|
|
|
* {@link https://www.php.net/manual/en/datetime.format.php format() method}. |
250
|
|
|
* |
251
|
|
|
* **Note:** If no backup name is configured, the file will be removed! |
252
|
|
|
* |
253
|
|
|
* @param integer Name of the backup file, optionally with date/time placeholders |
254
|
|
|
* @since 2018.04 |
255
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
256
|
|
|
* @see controller/jobs/product/import/csv/domains |
257
|
|
|
* @see controller/jobs/product/import/csv/location |
258
|
|
|
* @see controller/jobs/product/import/csv/mapping |
259
|
|
|
* @see controller/jobs/product/import/csv/max-size |
260
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
261
|
|
|
*/ |
262
|
|
|
$backup = $this->context()->config()->get( 'controller/jobs/product/import/csv/backup' ); |
263
|
|
|
return \Aimeos\Base\Str::strtime( (string) $backup ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Checks the given product type for validity |
269
|
|
|
* |
270
|
|
|
* @param string|null $type Product type or null for no type |
271
|
|
|
* @return string New product type |
272
|
|
|
*/ |
273
|
|
|
protected function checkType( string $type = null ) : string |
274
|
|
|
{ |
275
|
|
|
if( !isset( $this->types ) ) |
276
|
|
|
{ |
277
|
|
|
$this->types = []; |
278
|
|
|
|
279
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product/type' ); |
280
|
|
|
$search = $manager->filter()->slice( 0, 10000 ); |
281
|
|
|
|
282
|
|
|
foreach( $manager->search( $search ) as $item ) { |
283
|
|
|
$this->types[$item->getCode()] = $item->getCode(); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return ( isset( $this->types[$type] ) ? $this->types[$type] : 'default' ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Cleans up the given list of product items |
293
|
|
|
* |
294
|
|
|
* @param \Aimeos\Map $products List of product items implementing \Aimeos\MShop\Product\Item\Iface |
295
|
|
|
*/ |
296
|
|
|
protected function clean( \Aimeos\Map $products ) |
297
|
|
|
{ |
298
|
|
|
$articles = $products->filter( fn( $item ) => $item->getType() === 'select' ) |
299
|
|
|
->getRefItems( 'product', null, 'default' )->flat( 1 ); |
300
|
|
|
|
301
|
|
|
\Aimeos\MShop::create( $this->context(), 'product' )->save( $products->merge( $articles )->setStatus( -2 ) ); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Adds conditions to the filter for fetching products that should be removed |
307
|
|
|
* |
308
|
|
|
* @param \Aimeos\Base\Criteria\Iface $filter Criteria object |
309
|
|
|
* @return \Aimeos\Base\Criteria\Iface Modified criteria object |
310
|
|
|
*/ |
311
|
|
|
protected function cleaner( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\Base\Criteria\Iface |
312
|
|
|
{ |
313
|
|
|
return $filter->add( $filter->make( 'product:has', ['catalog'] ), '!=', null ); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Removes all products which have been updated before the given date/time |
319
|
|
|
* |
320
|
|
|
* @param string $datetime Date and time in ISO format |
321
|
|
|
* @return int Number of removed products |
322
|
|
|
*/ |
323
|
|
|
protected function cleanup( string $datetime ) : int |
324
|
|
|
{ |
325
|
|
|
$count = 0; |
326
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
327
|
|
|
|
328
|
|
|
$filter = $manager->filter(); |
329
|
|
|
$filter->add( 'product.mtime', '<', $datetime ); |
330
|
|
|
$cursor = $manager->cursor( $this->call( 'cleaner', $filter ) ); |
331
|
|
|
|
332
|
|
|
while( $items = $manager->iterate( $cursor, ['product' => ['default']] ) ) |
333
|
|
|
{ |
334
|
|
|
$this->call( 'clean', $items ); |
335
|
|
|
$count += count( $items ); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $count; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns the list of domain names that should be retrieved along with the attribute items |
344
|
|
|
* |
345
|
|
|
* @return array List of domain names |
346
|
|
|
*/ |
347
|
|
|
protected function domains() : array |
348
|
|
|
{ |
349
|
|
|
/** controller/jobs/product/import/csv/domains |
350
|
|
|
* List of item domain names that should be retrieved along with the product items |
351
|
|
|
* |
352
|
|
|
* For efficient processing, the items associated to the products can be |
353
|
|
|
* fetched to, minimizing the number of database queries required. To be |
354
|
|
|
* most effective, the list of item domain names should be used in the |
355
|
|
|
* mapping configuration too, so the retrieved items will be used during |
356
|
|
|
* the import. |
357
|
|
|
* |
358
|
|
|
* @param array Associative list of MShop item domain names |
359
|
|
|
* @since 2018.04 |
360
|
|
|
* @see controller/jobs/product/import/csv/backup |
361
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
362
|
|
|
* @see controller/jobs/product/import/csv/location |
363
|
|
|
* @see controller/jobs/product/import/csv/mapping |
364
|
|
|
* @see controller/jobs/product/import/csv/max-size |
365
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
366
|
|
|
*/ |
367
|
|
|
return $this->context()->config()->get( 'controller/jobs/product/import/csv/domains', [] ); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* Returns the position of the "product.code" column from the product item mapping |
373
|
|
|
* |
374
|
|
|
* @param array $mapping Mapping of the "item" columns with position as key and code as value |
375
|
|
|
* @return int Position of the "product.code" column |
376
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If no mapping for "product.code" is found |
377
|
|
|
*/ |
378
|
|
|
protected function getCodePosition( array $mapping ) : int |
379
|
|
|
{ |
380
|
|
|
foreach( $mapping as $pos => $key ) |
381
|
|
|
{ |
382
|
|
|
if( $key === 'product.code' ) { |
383
|
|
|
return $pos; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "product.code" column in CSV mapping found' ) ); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Returns the product items for the given codes |
393
|
|
|
* |
394
|
|
|
* @param array $codes List of product codes |
395
|
|
|
* @param array $domains List of domains whose items should be fetched too |
396
|
|
|
* @return \Aimeos\Map Associative list of product codes as key and product items as value |
397
|
|
|
*/ |
398
|
|
|
protected function getProducts( array $codes, array $domains ) : \Aimeos\Map |
399
|
|
|
{ |
400
|
|
|
$manager = \Aimeos\MShop::create( $this->context(), 'product' ); |
401
|
|
|
$search = $manager->filter()->add( ['product.code' => $codes] )->slice( 0, count( $codes ) ); |
402
|
|
|
|
403
|
|
|
return $manager->search( $search, $domains )->col( null, 'product.code' ); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Imports the CSV file from the given path |
409
|
|
|
* |
410
|
|
|
* @param string $path Relative path to the CSV file |
411
|
|
|
* @return int Number of lines which couldn't be imported |
412
|
|
|
*/ |
413
|
|
|
protected function import( string $path ) : int |
414
|
|
|
{ |
415
|
|
|
$context = $this->context(); |
416
|
|
|
$logger = $context->logger(); |
417
|
|
|
|
418
|
|
|
$logger->info( sprintf( 'Started product import from "%1$s"', $path ), 'import/csv/product' ); |
419
|
|
|
|
420
|
|
|
$maxcnt = $this->max(); |
421
|
|
|
$skiplines = $this->skip(); |
422
|
|
|
$domains = $this->domains(); |
423
|
|
|
|
424
|
|
|
$mappings = $this->mapping(); |
425
|
|
|
$processor = $this->getProcessors( $mappings ); |
426
|
|
|
$codePos = $this->getCodePosition( $mappings['item'] ); |
427
|
|
|
|
428
|
|
|
$fs = $context->fs( 'fs-import' ); |
429
|
|
|
$fh = $fs->reads( $path ); |
430
|
|
|
$total = $errors = 0; |
431
|
|
|
|
432
|
|
|
for( $i = 0; $i < $skiplines; $i++ ) { |
433
|
|
|
fgetcsv( $fh ); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
while( ( $data = $this->getData( $fh, $maxcnt, $codePos ) ) !== [] ) |
437
|
|
|
{ |
438
|
|
|
$products = $this->getProducts( array_keys( $data ), $domains ); |
439
|
|
|
$errors += $this->importProducts( $products, $data, $mappings['item'], [], $processor ); |
440
|
|
|
|
441
|
|
|
$total += count( $data ); |
442
|
|
|
unset( $products, $data ); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
$processor->finish(); |
446
|
|
|
fclose( $fh ); |
447
|
|
|
|
448
|
|
|
if( !empty( $backup = $this->backup() ) ) { |
449
|
|
|
$fs->move( $path, $backup ); |
450
|
|
|
} else { |
451
|
|
|
$fs->rm( $path ); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
$str = sprintf( 'Finished product import from "%1$s" (%2$d/%3$d)', $path, $errors, $total ); |
455
|
|
|
$logger->info( $str, 'import/csv/product' ); |
456
|
|
|
|
457
|
|
|
return $errors; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Imports the CSV data and creates new products or updates existing ones |
463
|
|
|
* |
464
|
|
|
* @param \Aimeos\Map $products List of products items implementing \Aimeos\MShop\Product\Item\Iface |
465
|
|
|
* @param array $data Associative list of import data as index/value pairs |
466
|
|
|
* @param array $mapping Associative list of positions and domain item keys |
467
|
|
|
* @param array $types List of allowed product type codes |
468
|
|
|
* @param \Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $processor Processor object |
469
|
|
|
* @return int Number of products that couldn't be imported |
470
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception |
471
|
|
|
*/ |
472
|
|
|
protected function importProducts( \Aimeos\Map $products, array $data, array $mapping, array $types, |
473
|
|
|
\Aimeos\Controller\Jobs\Common\Product\Import\Csv\Processor\Iface $processor ) : int |
474
|
|
|
{ |
475
|
|
|
$items = []; |
476
|
|
|
$errors = 0; |
477
|
|
|
$context = $this->context(); |
478
|
|
|
$manager = \Aimeos\MShop::create( $context, 'index' ); |
479
|
|
|
|
480
|
|
|
foreach( $data as $code => $list ) |
481
|
|
|
{ |
482
|
|
|
$manager->begin(); |
483
|
|
|
|
484
|
|
|
try |
485
|
|
|
{ |
486
|
|
|
$code = trim( $code ); |
487
|
|
|
$product = $products[$code] ?? $manager->create(); |
488
|
|
|
$map = current( $this->getMappedChunk( $list, $mapping ) ); // there can only be one chunk for the base product data |
489
|
|
|
|
490
|
|
|
if( $map ) |
491
|
|
|
{ |
492
|
|
|
$type = $this->checkType( $this->val( $map, 'product.type', $product->getType() ) ); |
493
|
|
|
|
494
|
|
|
if( $config = $this->val( $map, 'product.config' ) ) { |
495
|
|
|
$map['product.config'] = json_decode( $config ) ?: []; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
$product = $product->fromArray( $map, true ); |
499
|
|
|
$product = $manager->save( $product->setType( $type ) ); |
500
|
|
|
|
501
|
|
|
$processor->process( $product, $list ); |
|
|
|
|
502
|
|
|
|
503
|
|
|
$product = $manager->save( $product->setModified() ); |
504
|
|
|
$items[$product->getId()] = $product; |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
$manager->commit(); |
508
|
|
|
} |
509
|
|
|
catch( \Throwable $t ) |
510
|
|
|
{ |
511
|
|
|
$manager->rollback(); |
512
|
|
|
|
513
|
|
|
$msg = sprintf( 'Unable to import product with code "%1$s": %2$s', $code, $t->getMessage() ); |
514
|
|
|
$context->logger()->error( $msg, 'import/csv/product' ); |
515
|
|
|
|
516
|
|
|
$errors++; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
return $errors; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Returns the path to the directory with the CSV file |
526
|
|
|
* |
527
|
|
|
* @return string Path to the directory with the CSV file |
528
|
|
|
*/ |
529
|
|
|
protected function location() : string |
530
|
|
|
{ |
531
|
|
|
/** controller/jobs/product/import/csv/location |
532
|
|
|
* Directory where the CSV files are stored which should be imported |
533
|
|
|
* |
534
|
|
|
* It's the relative path inside the "fs-import" virtual file system |
535
|
|
|
* configuration. The default location of the "fs-import" file system is: |
536
|
|
|
* |
537
|
|
|
* * Laravel: ./storage/import/ |
538
|
|
|
* * TYPO3: /uploads/tx_aimeos/.secure/import/ |
539
|
|
|
* |
540
|
|
|
* @param string Relative path to the CSV files |
541
|
|
|
* @since 2015.08 |
542
|
|
|
* @see controller/jobs/product/import/csv/backup |
543
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
544
|
|
|
* @see controller/jobs/product/import/csv/domains |
545
|
|
|
* @see controller/jobs/product/import/csv/location |
546
|
|
|
* @see controller/jobs/product/import/csv/mapping |
547
|
|
|
* @see controller/jobs/product/import/csv/max-size |
548
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
549
|
|
|
*/ |
550
|
|
|
return (string) $this->context()->config()->get( 'controller/jobs/product/import/csv/location', 'product' ); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* Returns the CSV column mapping |
556
|
|
|
* |
557
|
|
|
* @return array CSV column mapping |
558
|
|
|
*/ |
559
|
|
|
protected function mapping() : array |
560
|
|
|
{ |
561
|
|
|
/** controller/jobs/product/import/csv/mapping |
562
|
|
|
* List of mappings between the position in the CSV file and item keys |
563
|
|
|
* |
564
|
|
|
* The importer have to know which data is at which position in the CSV |
565
|
|
|
* file. Therefore, you need to specify a mapping between each position |
566
|
|
|
* and the MShop domain item key (e.g. "product.code") it represents. |
567
|
|
|
* |
568
|
|
|
* You can use all domain item keys which are used in the fromArray() |
569
|
|
|
* methods of the item classes. |
570
|
|
|
* |
571
|
|
|
* These mappings are grouped together by their processor names, which |
572
|
|
|
* are responsible for importing the data, e.g. all mappings in "item" |
573
|
|
|
* will be processed by the base product importer while the mappings in |
574
|
|
|
* "text" will be imported by the text processor. |
575
|
|
|
* |
576
|
|
|
* @param array Associative list of processor names and lists of key/position pairs |
577
|
|
|
* @since 2018.04 |
578
|
|
|
* @see controller/jobs/product/import/csv/backup |
579
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
580
|
|
|
* @see controller/jobs/product/import/csv/domains |
581
|
|
|
* @see controller/jobs/product/import/csv/location |
582
|
|
|
* @see controller/jobs/product/import/csv/max-size |
583
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
584
|
|
|
*/ |
585
|
|
|
$map = (array) $this->context()->config()->get( 'controller/jobs/product/import/csv/mapping', $this->getDefaultMapping() ); |
586
|
|
|
|
587
|
|
|
if( !isset( $map['item'] ) || !is_array( $map['item'] ) ) |
588
|
|
|
{ |
589
|
|
|
$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' ); |
590
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
return $map; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* Returns the maximum number of CSV rows to import at once |
599
|
|
|
* |
600
|
|
|
* @return int Maximum number of CSV rows to import at once |
601
|
|
|
*/ |
602
|
|
|
protected function max() : int |
603
|
|
|
{ |
604
|
|
|
/** controller/jobs/product/import/csv/max-size |
605
|
|
|
* Maximum number of CSV rows to import at once |
606
|
|
|
* |
607
|
|
|
* It's more efficient to read and import more than one row at a time |
608
|
|
|
* to speed up the import. Usually, the bigger the chunk that is imported |
609
|
|
|
* at once, the less time the importer will need. The downside is that |
610
|
|
|
* the amount of memory required by the import process will increase as |
611
|
|
|
* well. Therefore, it's a trade-off between memory consumption and |
612
|
|
|
* import speed. |
613
|
|
|
* |
614
|
|
|
* @param integer Number of rows |
615
|
|
|
* @since 2018.04 |
616
|
|
|
* @see controller/jobs/product/import/csv/backup |
617
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
618
|
|
|
* @see controller/jobs/product/import/csv/domains |
619
|
|
|
* @see controller/jobs/product/import/csv/location |
620
|
|
|
* @see controller/jobs/product/import/csv/mapping |
621
|
|
|
* @see controller/jobs/product/import/csv/skip-lines |
622
|
|
|
*/ |
623
|
|
|
return (int) $this->context()->config()->get( 'controller/jobs/product/import/csv/max-size', 1000 ); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Returns the number of rows skipped in front of each CSV files |
629
|
|
|
* |
630
|
|
|
* @return int Number of rows skipped in front of each CSV files |
631
|
|
|
*/ |
632
|
|
|
protected function skip() : int |
633
|
|
|
{ |
634
|
|
|
/** controller/jobs/product/import/csv/skip-lines |
635
|
|
|
* Number of rows skipped in front of each CSV files |
636
|
|
|
* |
637
|
|
|
* Some CSV files contain header information describing the content of |
638
|
|
|
* the column values. These data is for informational purpose only and |
639
|
|
|
* can't be imported into the database. Using this option, you can |
640
|
|
|
* define the number of lines that should be left out before the import |
641
|
|
|
* begins. |
642
|
|
|
* |
643
|
|
|
* @param integer Number of rows |
644
|
|
|
* @since 2015.08 |
645
|
|
|
* @see controller/jobs/product/import/csv/backup |
646
|
|
|
* @see controller/jobs/product/import/csv/cleanup |
647
|
|
|
* @see controller/jobs/product/import/csv/domains |
648
|
|
|
* @see controller/jobs/product/import/csv/location |
649
|
|
|
* @see controller/jobs/product/import/csv/mapping |
650
|
|
|
* @see controller/jobs/product/import/csv/max-size |
651
|
|
|
*/ |
652
|
|
|
return (int) $this->context()->config()->get( 'controller/jobs/product/import/csv/skip-lines', 0 ); |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
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.