1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018-2020 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Jobs |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Supplier\Import\Csv; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Job controller for CSV supplier imports. |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Jobs |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Common\Supplier\Import\Csv\Base |
22
|
|
|
implements \Aimeos\Controller\Jobs\Iface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Returns the localized name of the job. |
26
|
|
|
* |
27
|
|
|
* @return string Name of the job |
28
|
|
|
*/ |
29
|
|
|
public function getName(): string |
30
|
|
|
{ |
31
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Supplier import CSV' ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the localized description of the job. |
37
|
|
|
* |
38
|
|
|
* @return string Description of the job |
39
|
|
|
*/ |
40
|
|
|
public function getDescription(): string |
41
|
|
|
{ |
42
|
|
|
return $this->getContext()->getI18n()->dt( 'controller/jobs', 'Imports new and updates existing suppliers from CSV files' ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Executes the job. |
48
|
|
|
* |
49
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If an error occurs |
50
|
|
|
*/ |
51
|
|
|
public function run() |
52
|
|
|
{ |
53
|
|
|
$total = $errors = 0; |
54
|
|
|
$context = $this->getContext(); |
55
|
|
|
$config = $context->getConfig(); |
56
|
|
|
$logger = $context->getLogger(); |
57
|
|
|
$domains = array('media', 'text', 'address'); |
58
|
|
|
$mappings = $this->getDefaultMapping(); |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
if( file_exists( $config->get( 'controller/jobs/supplier/import/csv/location' ) ) === false ) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** controller/common/supplier/import/csv/domains |
67
|
|
|
* List of item domain names that should be retrieved along with the supplier items |
68
|
|
|
* |
69
|
|
|
* For efficient processing, the items associated to the suppliers can be |
70
|
|
|
* fetched to, minimizing the number of database queries required. To be |
71
|
|
|
* most effective, the list of item domain names should be used in the |
72
|
|
|
* mapping configuration too, so the retrieved items will be used during |
73
|
|
|
* the import. |
74
|
|
|
* |
75
|
|
|
* @param array Associative list of MShop item domain names |
76
|
|
|
* @since 2020.07 |
77
|
|
|
* @category Developer |
78
|
|
|
* @see controller/common/supplier/import/csv/mapping |
79
|
|
|
* @see controller/common/supplier/import/csv/converter |
80
|
|
|
* @see controller/common/supplier/import/csv/max-size |
81
|
|
|
*/ |
82
|
|
|
$domains = $config->get( 'controller/common/supplier/import/csv/domains', $domains ); |
83
|
|
|
|
84
|
|
|
/** controller/jobs/supplier/import/csv/domains |
85
|
|
|
* List of item domain names that should be retrieved along with the supplier items |
86
|
|
|
* |
87
|
|
|
* This configuration setting overwrites the shared option |
88
|
|
|
* "controller/common/supplier/import/csv/domains" if you need a |
89
|
|
|
* specific setting for the job controller. Otherwise, you should |
90
|
|
|
* use the shared option for consistency. |
91
|
|
|
* |
92
|
|
|
* @param array Associative list of MShop item domain names |
93
|
|
|
* @since 2020.07 |
94
|
|
|
* @category Developer |
95
|
|
|
* @see controller/jobs/supplier/import/csv/mapping |
96
|
|
|
* @see controller/jobs/supplier/import/csv/skip-lines |
97
|
|
|
* @see controller/jobs/supplier/import/csv/converter |
98
|
|
|
* @see controller/jobs/supplier/import/csv/strict |
99
|
|
|
* @see controller/jobs/supplier/import/csv/backup |
100
|
|
|
* @see controller/common/supplier/import/csv/max-size |
101
|
|
|
*/ |
102
|
|
|
$domains = $config->get( 'controller/jobs/supplier/import/csv/domains', $domains ); |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** controller/common/supplier/import/csv/mapping |
106
|
|
|
* List of mappings between the position in the CSV file and item keys |
107
|
|
|
* |
108
|
|
|
* The importer have to know which data is at which position in the CSV |
109
|
|
|
* file. Therefore, you need to specify a mapping between each position |
110
|
|
|
* and the MShop domain item key (e.g. "supplier.code") it represents. |
111
|
|
|
* |
112
|
|
|
* You can use all domain item keys which are used in the fromArray() |
113
|
|
|
* methods of the item classes. |
114
|
|
|
* |
115
|
|
|
* These mappings are grouped together by their processor names, which |
116
|
|
|
* are responsible for importing the data, e.g. all mappings in "item" |
117
|
|
|
* will be processed by the base supplier importer while the mappings in |
118
|
|
|
* "text" will be imported by the text processor. |
119
|
|
|
* |
120
|
|
|
* @param array Associative list of processor names and lists of key/position pairs |
121
|
|
|
* @since 2020.07 |
122
|
|
|
* @category Developer |
123
|
|
|
* @see controller/common/supplier/import/csv/domains |
124
|
|
|
* @see controller/common/supplier/import/csv/converter |
125
|
|
|
* @see controller/common/supplier/import/csv/max-size |
126
|
|
|
*/ |
127
|
|
|
$mappings = $config->get( 'controller/common/supplier/import/csv/mapping', $mappings ); |
128
|
|
|
|
129
|
|
|
/** controller/jobs/supplier/import/csv/mapping |
130
|
|
|
* List of mappings between the position in the CSV file and item keys |
131
|
|
|
* |
132
|
|
|
* This configuration setting overwrites the shared option |
133
|
|
|
* "controller/common/supplier/import/csv/mapping" if you need a |
134
|
|
|
* specific setting for the job controller. Otherwise, you should |
135
|
|
|
* use the shared option for consistency. |
136
|
|
|
* |
137
|
|
|
* @param array Associative list of processor names and lists of key/position pairs |
138
|
|
|
* @since 2020.07 |
139
|
|
|
* @category Developer |
140
|
|
|
* @see controller/jobs/supplier/import/csv/domains |
141
|
|
|
* @see controller/jobs/supplier/import/csv/skip-lines |
142
|
|
|
* @see controller/jobs/supplier/import/csv/converter |
143
|
|
|
* @see controller/jobs/supplier/import/csv/strict |
144
|
|
|
* @see controller/jobs/supplier/import/csv/backup |
145
|
|
|
* @see controller/common/supplier/import/csv/max-size |
146
|
|
|
*/ |
147
|
|
|
$mappings = $config->get( 'controller/jobs/supplier/import/csv/mapping', $mappings ); |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** controller/common/supplier/import/csv/converter |
151
|
|
|
* List of converter names for the values at the position in the CSV file |
152
|
|
|
* |
153
|
|
|
* Not all data in the CSV file is already in the required format. Maybe |
154
|
|
|
* the text encoding isn't UTF-8, the date is not in ISO format or something |
155
|
|
|
* similar. In order to convert the data before it's imported, you can |
156
|
|
|
* specify a list of converter objects that should be applied to the data |
157
|
|
|
* from the CSV file. |
158
|
|
|
* |
159
|
|
|
* To each field in the CSV file, you can apply one or more converters, |
160
|
|
|
* e.g. to encode a Latin text to UTF8 for the second CSV field: |
161
|
|
|
* |
162
|
|
|
* array( 1 => 'Text/LatinUTF8' ) |
163
|
|
|
* |
164
|
|
|
* Similarly, you can also apply several converters at once to the same |
165
|
|
|
* field: |
166
|
|
|
* |
167
|
|
|
* array( 1 => array( 'Text/LatinUTF8', 'DateTime/EnglishISO' ) ) |
168
|
|
|
* |
169
|
|
|
* It would convert the data of the second CSV field first to UTF-8 and |
170
|
|
|
* afterwards try to translate it to an ISO date format. |
171
|
|
|
* |
172
|
|
|
* The available converter objects are named "\Aimeos\MW\Convert\<type>_<conversion>" |
173
|
|
|
* where <type> is the data type and <conversion> the way of the conversion. |
174
|
|
|
* In the configuration, the type and conversion must be separated by a |
175
|
|
|
* slash (<type>/<conversion>). |
176
|
|
|
* |
177
|
|
|
* '''Note:''' Keep in mind that the position of the CSV fields start at |
178
|
|
|
* zero (0). If you only need to convert a few fields, you don't have to |
179
|
|
|
* configure all fields. Only specify the positions in the array you |
180
|
|
|
* really need! |
181
|
|
|
* |
182
|
|
|
* @param array Associative list of position/converter name (or list of names) pairs |
183
|
|
|
* @since 2020.07 |
184
|
|
|
* @category Developer |
185
|
|
|
* @see controller/common/supplier/import/csv/domains |
186
|
|
|
* @see controller/common/supplier/import/csv/mapping |
187
|
|
|
* @see controller/common/supplier/import/csv/max-size |
188
|
|
|
*/ |
189
|
|
|
$converters = $config->get( 'controller/common/supplier/import/csv/converter', [] ); |
190
|
|
|
|
191
|
|
|
/** controller/jobs/supplier/import/csv/converter |
192
|
|
|
* List of converter names for the values at the position in the CSV file |
193
|
|
|
* |
194
|
|
|
* This configuration setting overwrites the shared option |
195
|
|
|
* "controller/common/supplier/import/csv/converter" if you need a |
196
|
|
|
* specific setting for the job controller. Otherwise, you should |
197
|
|
|
* use the shared option for consistency. |
198
|
|
|
* |
199
|
|
|
* @param array Associative list of position/converter name (or list of names) pairs |
200
|
|
|
* @since 2020.07 |
201
|
|
|
* @category Developer |
202
|
|
|
* @see controller/jobs/supplier/import/csv/domains |
203
|
|
|
* @see controller/jobs/supplier/import/csv/mapping |
204
|
|
|
* @see controller/jobs/supplier/import/csv/skip-lines |
205
|
|
|
* @see controller/jobs/supplier/import/csv/strict |
206
|
|
|
* @see controller/jobs/supplier/import/csv/backup |
207
|
|
|
* @see controller/common/supplier/import/csv/max-size |
208
|
|
|
*/ |
209
|
|
|
$converters = $config->get( 'controller/jobs/supplier/import/csv/converter', $converters ); |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** controller/common/supplier/import/csv/max-size |
213
|
|
|
* Maximum number of CSV rows to import at once |
214
|
|
|
* |
215
|
|
|
* It's more efficient to read and import more than one row at a time |
216
|
|
|
* to speed up the import. Usually, the bigger the chunk that is imported |
217
|
|
|
* at once, the less time the importer will need. The downside is that |
218
|
|
|
* the amount of memory required by the import process will increase as |
219
|
|
|
* well. Therefore, it's a trade-off between memory consumption and |
220
|
|
|
* import speed. |
221
|
|
|
* |
222
|
|
|
* @param integer Number of rows |
223
|
|
|
* @since 2020.07 |
224
|
|
|
* @category Developer |
225
|
|
|
* @see controller/common/supplier/import/csv/domains |
226
|
|
|
* @see controller/common/supplier/import/csv/mapping |
227
|
|
|
* @see controller/common/supplier/import/csv/converter |
228
|
|
|
*/ |
229
|
|
|
$maxcnt = (int)$config->get( 'controller/common/supplier/import/csv/max-size', 1000 ); |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** controller/jobs/supplier/import/csv/skip-lines |
233
|
|
|
* Number of rows skipped in front of each CSV files |
234
|
|
|
* |
235
|
|
|
* Some CSV files contain header information describing the content of |
236
|
|
|
* the column values. These data is for informational purpose only and |
237
|
|
|
* can't be imported into the database. Using this option, you can |
238
|
|
|
* define the number of lines that should be left out before the import |
239
|
|
|
* begins. |
240
|
|
|
* |
241
|
|
|
* @param integer Number of rows |
242
|
|
|
* @since 2020.07 |
243
|
|
|
* @category Developer |
244
|
|
|
* @see controller/jobs/supplier/import/csv/domains |
245
|
|
|
* @see controller/jobs/supplier/import/csv/mapping |
246
|
|
|
* @see controller/jobs/supplier/import/csv/converter |
247
|
|
|
* @see controller/jobs/supplier/import/csv/strict |
248
|
|
|
* @see controller/jobs/supplier/import/csv/backup |
249
|
|
|
* @see controller/common/supplier/import/csv/max-size |
250
|
|
|
*/ |
251
|
|
|
$skiplines = (int)$config->get( 'controller/jobs/supplier/import/csv/skip-lines', 0 ); |
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** controller/jobs/supplier/import/csv/strict |
255
|
|
|
* Log all columns from the file that are not mapped and therefore not imported |
256
|
|
|
* |
257
|
|
|
* Depending on the mapping, there can be more columns in the CSV file |
258
|
|
|
* than those which will be imported. This can be by purpose if you want |
259
|
|
|
* to import only selected columns or if you've missed to configure one |
260
|
|
|
* or more columns. This configuration option will log all columns that |
261
|
|
|
* have not been imported if set to true. Otherwise, the left over fields |
262
|
|
|
* in the imported line will be silently ignored. |
263
|
|
|
* |
264
|
|
|
* @param boolen True if not imported columns should be logged, false if not |
265
|
|
|
* @since 2020.07 |
266
|
|
|
* @category User |
267
|
|
|
* @category Developer |
268
|
|
|
* @see controller/jobs/supplier/import/csv/domains |
269
|
|
|
* @see controller/jobs/supplier/import/csv/mapping |
270
|
|
|
* @see controller/jobs/supplier/import/csv/skip-lines |
271
|
|
|
* @see controller/jobs/supplier/import/csv/converter |
272
|
|
|
* @see controller/jobs/supplier/import/csv/backup |
273
|
|
|
* @see controller/common/supplier/import/csv/max-size |
274
|
|
|
*/ |
275
|
|
|
$strict = (bool)$config->get( 'controller/jobs/supplier/import/csv/strict', true ); |
276
|
|
|
|
277
|
|
|
|
278
|
|
|
/** controller/jobs/supplier/import/csv/backup |
279
|
|
|
* Name of the backup for sucessfully imported files |
280
|
|
|
* |
281
|
|
|
* After a CSV file was imported successfully, you can move it to another |
282
|
|
|
* location, so it won't be imported again and isn't overwritten by the |
283
|
|
|
* next file that is stored at the same location in the file system. |
284
|
|
|
* |
285
|
|
|
* You should use an absolute path to be sure but can be relative path |
286
|
|
|
* if you absolutely know from where the job will be executed from. The |
287
|
|
|
* name of the new backup location can contain placeholders understood |
288
|
|
|
* by the PHP strftime() function to create dynamic paths, e.g. "backup/%Y-%m-%d" |
289
|
|
|
* which would create "backup/2000-01-01". For more information about the |
290
|
|
|
* strftime() placeholders, please have a look into the PHP documentation of |
291
|
|
|
* the {@link http://php.net/manual/en/function.strftime.php strftime() function}. |
292
|
|
|
* |
293
|
|
|
* '''Note:''' If no backup name is configured, the file or directory |
294
|
|
|
* won't be moved away. Please make also sure that the parent directory |
295
|
|
|
* and the new directory are writable so the file or directory could be |
296
|
|
|
* moved. |
297
|
|
|
* |
298
|
|
|
* @param integer Name of the backup file, optionally with date/time placeholders |
299
|
|
|
* @since 2020.07 |
300
|
|
|
* @category Developer |
301
|
|
|
* @see controller/jobs/supplier/import/csv/domains |
302
|
|
|
* @see controller/jobs/supplier/import/csv/mapping |
303
|
|
|
* @see controller/jobs/supplier/import/csv/skip-lines |
304
|
|
|
* @see controller/jobs/supplier/import/csv/converter |
305
|
|
|
* @see controller/jobs/supplier/import/csv/strict |
306
|
|
|
* @see controller/common/supplier/import/csv/max-size |
307
|
|
|
*/ |
308
|
|
|
$backup = $config->get( 'controller/jobs/supplier/import/csv/backup' ); |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
if( !isset( $mappings['item'] ) || !is_array( $mappings['item'] ) ) { |
312
|
|
|
$msg = sprintf( 'Required mapping key "%1$s" is missing or contains no array', 'item' ); |
313
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
try { |
317
|
|
|
$procMappings = $mappings; |
318
|
|
|
unset( $procMappings['item'] ); |
319
|
|
|
|
320
|
|
|
$codePos = $this->getCodePosition( $mappings['item'] ); |
321
|
|
|
$convlist = $this->getConverterList( $converters ); |
322
|
|
|
$processor = $this->getProcessors( $procMappings ); |
323
|
|
|
$supplierMap = $this->getSupplierMap( $domains ); |
324
|
|
|
$container = $this->getContainer(); |
325
|
|
|
$path = $container->getName(); |
326
|
|
|
|
327
|
|
|
|
328
|
|
|
$msg = sprintf( 'Started supplier import from "%1$s" (%2$s)', $path, __CLASS__ ); |
329
|
|
|
$logger->log( $msg, \Aimeos\MW\Logger\Base::NOTICE ); |
330
|
|
|
|
331
|
|
|
foreach( $container as $content ) { |
332
|
|
|
$name = $content->getName(); |
333
|
|
|
|
334
|
|
|
for( $i = 0; $i < $skiplines; $i++ ) { |
335
|
|
|
$content->next(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
while ( ($data = $this->getData( $content, $maxcnt, $codePos )) !== [] ) { |
339
|
|
|
$data = $this->convertData( $convlist, $data ); |
340
|
|
|
$errcnt = $this->import( $supplierMap, $data, $mappings['item'], $processor, $strict ); |
341
|
|
|
$chunkcnt = count( $data ); |
342
|
|
|
|
343
|
|
|
$msg = 'Imported supplier lines from "%1$s": %2$d/%3$d (%4$s)'; |
344
|
|
|
$logger->log( sprintf( $msg, $name, $chunkcnt - $errcnt, $chunkcnt, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
345
|
|
|
|
346
|
|
|
$errors += $errcnt; |
347
|
|
|
$total += $chunkcnt; |
348
|
|
|
unset( $data ); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$container->close(); |
353
|
|
|
} catch ( \Exception $e ) { |
354
|
|
|
$logger->log( 'Supplier import error: ' . $e->getMessage() . "\n" . $e->getTraceAsString() ); |
355
|
|
|
$this->mail( 'Supplier CSV import error', $e->getMessage() . "\n" . $e->getTraceAsString() ); |
356
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $e->getMessage() ); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$msg = 'Finished supplier import from "%1$s": %2$d successful, %3$s errors, %4$s total (%5$s)'; |
360
|
|
|
$logger->log( sprintf( $msg, $path, $total - $errors, $errors, $total, __CLASS__ ), \Aimeos\MW\Logger\Base::NOTICE ); |
361
|
|
|
|
362
|
|
|
if( $errors > 0 ) { |
363
|
|
|
$msg = sprintf( 'Invalid supplier lines in "%1$s": %2$d/%3$d', $path, $errors, $total ); |
364
|
|
|
$this->mail( 'Supplier CSV import error', $msg ); |
365
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
if( !empty( $backup ) && @rename( $path, strftime( $backup ) ) === false ) { |
369
|
|
|
$msg = sprintf( 'Unable to move imported file "%1$s" to "%2$s"', $path, strftime( $backup ) ); |
370
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Returns the position of the "supplier.code" column from the supplier item mapping |
377
|
|
|
* |
378
|
|
|
* @param array $mapping Mapping of the "item" columns with position as key and code as value |
379
|
|
|
* @return int Position of the "supplier.code" column |
380
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If no mapping for "supplier.code" is found |
381
|
|
|
*/ |
382
|
|
|
protected function getCodePosition( array $mapping ): int |
383
|
|
|
{ |
384
|
|
|
foreach( $mapping as $pos => $key ) { |
385
|
|
|
if( $key === 'supplier.code' ) { |
386
|
|
|
return $pos; |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'No "supplier.code" column in CSV mapping found' ) ); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Opens and returns the container which includes the supplier data |
396
|
|
|
* |
397
|
|
|
* @return \Aimeos\MW\Container\Iface Container object |
398
|
|
|
*/ |
399
|
|
|
protected function getContainer(): \Aimeos\MW\Container\Iface |
400
|
|
|
{ |
401
|
|
|
$config = $this->getContext()->getConfig(); |
402
|
|
|
|
403
|
|
|
/** controller/jobs/supplier/import/csv/location |
404
|
|
|
* File or directory where the content is stored which should be imported |
405
|
|
|
* |
406
|
|
|
* You need to configure the file or directory that acts as container |
407
|
|
|
* for the CSV files that should be imported. It should be an absolute |
408
|
|
|
* path to be sure but can be relative path if you absolutely know from |
409
|
|
|
* where the job will be executed from. |
410
|
|
|
* |
411
|
|
|
* The path can point to any supported container format as long as the |
412
|
|
|
* content is in CSV format, e.g. |
413
|
|
|
* * Directory container / CSV file |
414
|
|
|
* * Zip container / compressed CSV file |
415
|
|
|
* * PHPExcel container / PHPExcel sheet |
416
|
|
|
* |
417
|
|
|
* @param string Absolute file or directory path |
418
|
|
|
* @since 2020.07 |
419
|
|
|
* @category Developer |
420
|
|
|
* @category User |
421
|
|
|
* @see controller/jobs/supplier/import/csv/container/type |
422
|
|
|
* @see controller/jobs/supplier/import/csv/container/content |
423
|
|
|
* @see controller/jobs/supplier/import/csv/container/options |
424
|
|
|
*/ |
425
|
|
|
$location = $config->get( 'controller/jobs/supplier/import/csv/location' ); |
426
|
|
|
|
427
|
|
|
/** controller/jobs/supplier/import/csv/container/type |
428
|
|
|
* Nave of the container type to read the data from |
429
|
|
|
* |
430
|
|
|
* The container type tells the importer how it should retrieve the data. |
431
|
|
|
* There are currently three container types that support the necessary |
432
|
|
|
* CSV content: |
433
|
|
|
* * Directory |
434
|
|
|
* * Zip |
435
|
|
|
* * PHPExcel |
436
|
|
|
* |
437
|
|
|
* '''Note:''' For the PHPExcel container, you need to install the |
438
|
|
|
* "ai-container" extension. |
439
|
|
|
* |
440
|
|
|
* @param string Container type name |
441
|
|
|
* @since 2020.07 |
442
|
|
|
* @category Developer |
443
|
|
|
* @category User |
444
|
|
|
* @see controller/jobs/supplier/import/csv/location |
445
|
|
|
* @see controller/jobs/supplier/import/csv/container/content |
446
|
|
|
* @see controller/jobs/supplier/import/csv/container/options |
447
|
|
|
*/ |
448
|
|
|
$container = $config->get( 'controller/jobs/supplier/import/csv/container/type', 'Directory' ); |
449
|
|
|
|
450
|
|
|
/** controller/jobs/supplier/import/csv/container/content |
451
|
|
|
* Name of the content type inside the container to read the data from |
452
|
|
|
* |
453
|
|
|
* The content type must always be a CSV-like format and there are |
454
|
|
|
* currently two format types that are supported: |
455
|
|
|
* * CSV |
456
|
|
|
* * PHPExcel |
457
|
|
|
* |
458
|
|
|
* '''Note:''' for the PHPExcel content type, you need to install the |
459
|
|
|
* "ai-container" extension. |
460
|
|
|
* |
461
|
|
|
* @param array Content type name |
462
|
|
|
* @since 2020.07 |
463
|
|
|
* @category Developer |
464
|
|
|
* @category User |
465
|
|
|
* @see controller/jobs/supplier/import/csv/location |
466
|
|
|
* @see controller/jobs/supplier/import/csv/container/type |
467
|
|
|
* @see controller/jobs/supplier/import/csv/container/options |
468
|
|
|
*/ |
469
|
|
|
$content = $config->get( 'controller/jobs/supplier/import/csv/container/content', 'CSV' ); |
470
|
|
|
|
471
|
|
|
/** controller/jobs/supplier/import/csv/container/options |
472
|
|
|
* List of file container options for the supplier import files |
473
|
|
|
* |
474
|
|
|
* Some container/content type allow you to hand over additional settings |
475
|
|
|
* for configuration. Please have a look at the article about |
476
|
|
|
* {@link http://aimeos.org/docs/Developers/Utility/Create_and_read_files container/content files} |
477
|
|
|
* for more information. |
478
|
|
|
* |
479
|
|
|
* @param array Associative list of option name/value pairs |
480
|
|
|
* @since 2020.07 |
481
|
|
|
* @category Developer |
482
|
|
|
* @category User |
483
|
|
|
* @see controller/jobs/supplier/import/csv/location |
484
|
|
|
* @see controller/jobs/supplier/import/csv/container/content |
485
|
|
|
* @see controller/jobs/supplier/import/csv/container/type |
486
|
|
|
*/ |
487
|
|
|
$options = $config->get( 'controller/jobs/supplier/import/csv/container/options', [] ); |
488
|
|
|
|
489
|
|
|
if( $location === null ) { |
490
|
|
|
$msg = sprintf( 'Required configuration for "%1$s" is missing', 'controller/jobs/supplier/import/csv/location' ); |
491
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( $msg ); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return \Aimeos\MW\Container\Factory::getContainer( $location, $container, $content, $options ); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Returns the supplier items building the tree as list |
500
|
|
|
* |
501
|
|
|
* @param array $domains List of domain names whose items should be fetched too |
502
|
|
|
* @return array Associative list of supplier codes as keys and items implementing \Aimeos\MShop\Supplier\Item\Iface as values |
503
|
|
|
*/ |
504
|
|
|
protected function getSupplierMap( array $domains ): array |
505
|
|
|
{ |
506
|
|
|
$map = []; |
507
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'supplier' ); |
508
|
|
|
$search = $manager->createSearch()->setSlice( 0, 0x7fffffff ); |
509
|
|
|
|
510
|
|
|
foreach( $manager->searchItems( $search, $domains ) as $item ) { |
511
|
|
|
$map[$item->getCode()] = $item; |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
return $map; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
|
518
|
|
|
/** |
519
|
|
|
* Imports the CSV data and creates new suppliers or updates existing ones |
520
|
|
|
* |
521
|
|
|
* @param array &$supplierMap Associative list of supplier items with codes as keys and items implementing \Aimeos\MShop\Supplier\Item\Iface as values |
522
|
|
|
* @param array $data Associative list of import data as index/value pairs |
523
|
|
|
* @param array $mapping Associative list of positions and domain item keys |
524
|
|
|
* @param \Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $processor Processor object |
525
|
|
|
* @param bool $strict Log columns not mapped or silently ignore them |
526
|
|
|
* @return int Number of suppliers that couldn't be imported |
527
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception |
528
|
|
|
*/ |
529
|
|
|
protected function import( array &$supplierMap, array $data, array $mapping, |
530
|
|
|
\Aimeos\Controller\Common\Supplier\Import\Csv\Processor\Iface $processor, bool $strict ): int |
531
|
|
|
{ |
532
|
|
|
$errors = 0; |
533
|
|
|
$context = $this->getContext(); |
534
|
|
|
$manager = \Aimeos\MShop::create( $context, 'supplier' ); |
535
|
|
|
|
536
|
|
|
foreach( $data as $code => $list ) { |
537
|
|
|
$manager->begin(); |
538
|
|
|
|
539
|
|
|
try { |
540
|
|
|
$code = trim( $code ); |
541
|
|
|
|
542
|
|
|
if( isset( $supplierMap[$code] ) ) { |
543
|
|
|
$item = $supplierMap[$code]; |
544
|
|
|
} else { |
545
|
|
|
$item = $manager->createItem(); |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
$map = $this->getMappedChunk( $list, $mapping ); |
549
|
|
|
|
550
|
|
|
if( isset( $map[0] ) ) { |
551
|
|
|
$map = $map[0]; // there can only be one chunk for the base supplier data |
552
|
|
|
$item->fromArray( $map, true ); |
553
|
|
|
|
554
|
|
|
$list = $processor->process( $item, $list ); |
555
|
|
|
$supplierMap[$code] = $item; |
556
|
|
|
|
557
|
|
|
$manager->saveItem( $item ); |
|
|
|
|
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
$manager->commit(); |
561
|
|
|
} catch ( \Exception $e ) { |
562
|
|
|
$manager->rollback(); |
563
|
|
|
|
564
|
|
|
$msg = sprintf( 'Unable to import supplier with code "%1$s": %2$s', $code, $e->getMessage() ); |
565
|
|
|
$context->getLogger()->log( $msg ); |
566
|
|
|
|
567
|
|
|
$errors++; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
if( $strict && !empty( $list ) ) { |
571
|
|
|
$context->getLogger()->log( 'Not imported: ' . print_r( $list, true ) ); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
return $errors; |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
|
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.