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