1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2018-2025 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Common |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Jobs\Common\Supplier\Import\Csv; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Common class for CSV supplier import job controllers and processors. |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Common |
19
|
|
|
*/ |
20
|
|
|
class Base |
21
|
|
|
extends \Aimeos\Controller\Jobs\Base |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Returns the cache object for the given type |
25
|
|
|
* |
26
|
|
|
* @param string $type Type of the cached data |
27
|
|
|
* @param string|null $name Name of the cache implementation |
28
|
|
|
* @return \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Cache\Iface Cache object |
29
|
|
|
* @throws \LogicException If class can't be instantiated |
30
|
|
|
*/ |
31
|
|
|
protected function getCache( string $type, $name = null ) : \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Cache\Iface |
32
|
|
|
{ |
33
|
|
|
$context = $this->context(); |
34
|
|
|
$config = $context->config(); |
35
|
|
|
|
36
|
|
|
if( ctype_alnum( $type ) === false ) { |
37
|
|
|
throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $type ), 400 ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$name = $name ?: $config->get( 'controller/jobs/supplier/import/csv/cache/' . $type . '/name', 'Standard' ); |
41
|
|
|
|
42
|
|
|
if( ctype_alnum( $name ) === false ) { |
43
|
|
|
throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$classname = '\\Aimeos\\Controller\\Jobs\\Common\\Supplier\\Import\\Csv\\Cache\\' . ucfirst( $type ) . '\\' . $name; |
47
|
|
|
$interface = \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Cache\Iface::class; |
48
|
|
|
|
49
|
|
|
return \Aimeos\Utils::create( $classname, [$context], $interface ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the rows from the CSV file up to the maximum count |
55
|
|
|
* |
56
|
|
|
* @param resource $fh File handle to CSV file |
57
|
|
|
* @param int $maxcnt Maximum number of rows that should be retrieved at once |
58
|
|
|
* @param int $codePos Column position which contains the unique product code (starting from 0) |
59
|
|
|
* @return array List of arrays with product codes as keys and list of values from the CSV file |
60
|
|
|
*/ |
61
|
|
|
protected function getData( $fh, int $maxcnt, int $codePos ) : array |
62
|
|
|
{ |
63
|
|
|
$count = 0; |
64
|
|
|
$data = []; |
65
|
|
|
|
66
|
|
|
while( $count++ < $maxcnt && ( $row = fgetcsv( $fh, null, ',', '"', '' ) ) !== false ) { |
67
|
|
|
$data[$row[$codePos]] = $row; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $data; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the default mapping for the CSV fields to the domain item keys |
76
|
|
|
* |
77
|
|
|
* Example: |
78
|
|
|
* 'item' => array( |
79
|
|
|
* 0 => 'supplier.code', // e.g. unique EAN code |
80
|
|
|
* 1 => 'supplier.parent', // Code of parent supplier node |
81
|
|
|
* 2 => 'supplier.label', // UTF-8 encoded text, also used as supplier name |
82
|
|
|
* 3 => 'supplier.status', // If supplier should be shown in the frontend |
83
|
|
|
* ), |
84
|
|
|
* 'text' => array( |
85
|
|
|
* 3 => 'text.type', // e.g. "short" for short description |
86
|
|
|
* 4 => 'text.content', // UTF-8 encoded text |
87
|
|
|
* ), |
88
|
|
|
* 'media' => array( |
89
|
|
|
* 5 => 'media.url', // relative URL of the supplier image on the server |
90
|
|
|
* ), |
91
|
|
|
* 'address' => array( |
92
|
|
|
* 6 => supplier.address.countryid', // Country id by ISO 3166-1. e.g. Germany is DE |
93
|
|
|
* 6 => supplier.address.languageid', // e.g. en for English |
94
|
|
|
* 6 => supplier.address.city', // e.g. Berlin |
95
|
|
|
* ), |
96
|
|
|
* @return array Associative list of domains as keys ("item" is special for the supplier itself) and a list of |
97
|
|
|
* positions and the domain item keys as values. |
98
|
|
|
*/ |
99
|
|
|
protected function getDefaultMapping() : array |
100
|
|
|
{ |
101
|
|
|
return array( |
102
|
|
|
'item' => array( |
103
|
|
|
0 => 'supplier.code', |
104
|
|
|
1 => 'supplier.label', |
105
|
|
|
2 => 'supplier.status', |
106
|
|
|
), |
107
|
|
|
'text' => array( |
108
|
|
|
3 => 'text.languageid', |
109
|
|
|
4 => 'text.type', |
110
|
|
|
5 => 'text.content', |
111
|
|
|
), |
112
|
|
|
'media' => array( |
113
|
|
|
6 => 'media.type', |
114
|
|
|
7 => 'media.url', |
115
|
|
|
), |
116
|
|
|
'address' => array( |
117
|
|
|
8 => 'supplier.address.languageid', |
118
|
|
|
9 => 'supplier.address.countryid', |
119
|
|
|
10 => 'supplier.address.city', |
120
|
|
|
), |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the mapped data from the CSV line |
127
|
|
|
* |
128
|
|
|
* @param array $data List of CSV fields with position as key and domain item key as value (mapped data is removed) |
129
|
|
|
* @param array $mapping List of domain item keys with the CSV field position as key |
130
|
|
|
* @return array List of associative arrays containing the chunked properties |
131
|
|
|
*/ |
132
|
|
|
protected function getMappedChunk( array &$data, array $mapping ) : array |
133
|
|
|
{ |
134
|
|
|
$idx = 0; |
135
|
|
|
$map = []; |
136
|
|
|
|
137
|
|
|
foreach( $mapping as $pos => $key ) |
138
|
|
|
{ |
139
|
|
|
if( isset( $map[$idx][$key] ) ) { |
140
|
|
|
$idx++; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if( isset( $data[$pos] ) ) |
144
|
|
|
{ |
145
|
|
|
$map[$idx][$key] = $data[$pos]; |
146
|
|
|
unset( $data[$pos] ); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $map; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the processor object for saving the supplier related information |
156
|
|
|
* |
157
|
|
|
* @param array $mappings Associative list of processor types as keys and index/data mappings as values |
158
|
|
|
* @return \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Processor\Iface Processor object |
159
|
|
|
* @throws \LogicException If class can't be instantiated |
160
|
|
|
*/ |
161
|
|
|
protected function getProcessors( array $mappings ) : \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Processor\Iface |
162
|
|
|
{ |
163
|
|
|
unset( $mappings['item'] ); |
164
|
|
|
|
165
|
|
|
$context = $this->context(); |
166
|
|
|
$config = $context->config(); |
167
|
|
|
$object = new \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Processor\Done( $context, [] ); |
168
|
|
|
|
169
|
|
|
foreach( $mappings as $type => $mapping ) |
170
|
|
|
{ |
171
|
|
|
if( ctype_alnum( $type ) === false ) { |
172
|
|
|
throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $type ), 400 ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$name = $config->get( 'controller/jobs/supplier/import/csv/processor/' . $type . '/name', 'Standard' ); |
176
|
|
|
|
177
|
|
|
if( ctype_alnum( $name ) === false ) { |
178
|
|
|
throw new \LogicException( sprintf( 'Invalid characters in class name "%1$s"', $name ), 400 ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$classname = '\\Aimeos\\Controller\\Jobs\\Common\\Supplier\\Import\\Csv\\Processor\\' . ucfirst( $type ) . '\\' . $name; |
182
|
|
|
$interface = \Aimeos\Controller\Jobs\Common\Supplier\Import\Csv\Processor\Iface::class; |
183
|
|
|
|
184
|
|
|
$object = \Aimeos\Utils::create( $classname, [$context, $mapping, $object], $interface ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $object; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|