|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2018 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Abstract class with common methods for all CSV import processors |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Base |
|
21
|
|
|
extends \Aimeos\Controller\Common\Product\Import\Csv\Base |
|
22
|
|
|
{ |
|
23
|
|
|
private $context; |
|
24
|
|
|
private $mapping; |
|
25
|
|
|
private $object; |
|
26
|
|
|
private $types = []; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Initializes the object |
|
31
|
|
|
* |
|
32
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
33
|
|
|
* @param array $mapping Associative list of field position in CSV as key and domain item key as value |
|
34
|
|
|
* @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping, |
|
37
|
|
|
\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null ) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->context = $context; |
|
40
|
|
|
$this->mapping = $mapping; |
|
41
|
|
|
$this->object = $object; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Stores all types for which no type items exist yet |
|
47
|
|
|
*/ |
|
48
|
|
|
public function finish() |
|
49
|
|
|
{ |
|
50
|
|
|
if( $this->object ) { |
|
51
|
|
|
$this->object->finish(); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
foreach( $this->types as $path => $list ) |
|
55
|
|
|
{ |
|
56
|
|
|
$manager = \Aimeos\MShop::create( $this->context, $path ); |
|
57
|
|
|
$prefix = str_replace( '/', '.', $path ); |
|
58
|
|
|
|
|
59
|
|
|
foreach( $list as $domain => $codes ) |
|
60
|
|
|
{ |
|
61
|
|
|
$manager->begin(); |
|
62
|
|
|
|
|
63
|
|
|
try |
|
64
|
|
|
{ |
|
65
|
|
|
$search = $manager->createSearch()->setSlice( 0, 10000 ); |
|
66
|
|
|
$expr = [ |
|
67
|
|
|
$search->compare( '==', $prefix . '.domain', $domain ), |
|
68
|
|
|
$search->compare( '==', $prefix . '.code', $codes ) |
|
69
|
|
|
]; |
|
70
|
|
|
$search->setConditions( $search->combine( '&&', $expr ) ); |
|
71
|
|
|
|
|
72
|
|
|
$types = $items = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach( $manager->searchItems( $search ) as $item ) { |
|
75
|
|
|
$types[] = $item->getCode(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach( array_diff( $codes, $types ) as $code ) { |
|
79
|
|
|
$items[] = $manager->createItem()->setDomain( $domain )->setCode( $code )->setLabel( $code ); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$manager->saveItems( $items, false ); |
|
83
|
|
|
$manager->commit(); |
|
84
|
|
|
} |
|
85
|
|
|
catch( \Exception $e ) |
|
86
|
|
|
{ |
|
87
|
|
|
$manager->rollback(); |
|
88
|
|
|
$this->context->getLogger()->log( 'Error saving types: ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString() ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Registers a used type which is going to be saved if it doesn't exist yet |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $path Manager path, e.g. "product/lists/type" |
|
99
|
|
|
* @param string $domain Domain name the type belongs to, e.g. "attribute" |
|
100
|
|
|
* @param string $code Type code |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function addType( string $path, string $domain, string $code ) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->types[$path][$domain][$code] = $code; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Adds the list item default values and returns the resulting array |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $list Associative list of domain item keys and their values, e.g. "product.lists.status" => 1 |
|
112
|
|
|
* @param integer $pos Computed position of the list item in the associated list of items |
|
113
|
|
|
* @return array Given associative list enriched by default values if they were not already set |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function addListItemDefaults( array $list, $pos ) |
|
116
|
|
|
{ |
|
117
|
|
|
if( !isset( $list['product.lists.position'] ) ) { |
|
118
|
|
|
$list['product.lists.position'] = $pos; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if( !isset( $list['product.lists.status'] ) ) { |
|
122
|
|
|
$list['product.lists.status'] = 1; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $list; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the context item |
|
131
|
|
|
* |
|
132
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Context object |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getContext() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->context; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns the mapping list |
|
142
|
|
|
* |
|
143
|
|
|
* @return array Associative list of field positions in CSV as keys and domain item keys as values |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function getMapping() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->mapping; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns the decorated processor object |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface Processor object |
|
155
|
|
|
* @throws \Aimeos\Controller\Jobs\Exception If no processor object is available |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function getObject() |
|
158
|
|
|
{ |
|
159
|
|
|
if( $this->object === null ) { |
|
160
|
|
|
throw new \Aimeos\Controller\Jobs\Exception( 'No processor object available' ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $this->object; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|