1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
6
|
|
|
* @package Controller |
7
|
|
|
* @subpackage Common |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Common\Product\Import\Csv\Processor\Price; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Price processor for CSV imports |
16
|
|
|
* |
17
|
|
|
* @package Controller |
18
|
|
|
* @subpackage Common |
19
|
|
|
*/ |
20
|
|
|
class Standard |
21
|
|
|
extends \Aimeos\Controller\Common\Product\Import\Csv\Processor\Base |
|
|
|
|
22
|
|
|
implements \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** controller/common/product/import/csv/processor/price/name |
25
|
|
|
* Name of the price processor implementation |
26
|
|
|
* |
27
|
|
|
* Use "Myname" if your class is named "\Aimeos\Controller\Common\Product\Import\Csv\Processor\Price\Myname". |
28
|
|
|
* The name is case-sensitive and you should avoid camel case names like "MyName". |
29
|
|
|
* |
30
|
|
|
* @param string Last part of the processor class name |
31
|
|
|
* @since 2015.10 |
32
|
|
|
* @category Developer |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
private $listTypes; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes the object |
40
|
|
|
* |
41
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
42
|
|
|
* @param array $mapping Associative list of field position in CSV as key and domain item key as value |
43
|
|
|
* @param \Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object Decorated processor |
44
|
|
|
*/ |
45
|
|
|
public function __construct( \Aimeos\MShop\Context\Item\Iface $context, array $mapping, |
46
|
|
|
\Aimeos\Controller\Common\Product\Import\Csv\Processor\Iface $object = null ) |
47
|
|
|
{ |
48
|
|
|
parent::__construct( $context, $mapping, $object ); |
49
|
|
|
|
50
|
|
|
/** controller/common/product/import/csv/processor/price/listtypes |
51
|
|
|
* Names of the product list types for prices that are updated or removed |
52
|
|
|
* |
53
|
|
|
* If you want to associate price items manually via the administration |
54
|
|
|
* interface to products and don't want these to be touched during the |
55
|
|
|
* import, you can specify the product list types for these prices |
56
|
|
|
* that shouldn't be updated or removed. |
57
|
|
|
* |
58
|
|
|
* @param array|null List of product list type names or null for all |
59
|
|
|
* @since 2015.05 |
60
|
|
|
* @category Developer |
61
|
|
|
* @category User |
62
|
|
|
* @see controller/common/product/import/csv/domains |
63
|
|
|
* @see controller/common/product/import/csv/processor/attribute/listtypes |
64
|
|
|
* @see controller/common/product/import/csv/processor/catalog/listtypes |
65
|
|
|
* @see controller/common/product/import/csv/processor/media/listtypes |
66
|
|
|
* @see controller/common/product/import/csv/processor/product/listtypes |
67
|
|
|
* @see controller/common/product/import/csv/processor/text/listtypes |
68
|
|
|
*/ |
69
|
|
|
$this->listTypes = $context->getConfig()->get( 'controller/common/product/import/csv/processor/price/listtypes' ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Saves the product related data to the storage |
75
|
|
|
* |
76
|
|
|
* @param \Aimeos\MShop\Product\Item\Iface $product Product item with associated items |
77
|
|
|
* @param array $data List of CSV fields with position as key and data as value |
78
|
|
|
* @return array List of data which hasn't been imported |
79
|
|
|
*/ |
80
|
|
|
public function process( \Aimeos\MShop\Product\Item\Iface $product, array $data ) |
81
|
|
|
{ |
82
|
|
|
$listManager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'product/lists' ); |
83
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $this->getContext(), 'price' ); |
84
|
|
|
$manager->begin(); |
85
|
|
|
|
86
|
|
|
try |
87
|
|
|
{ |
88
|
|
|
$delete = $listMap = array(); |
89
|
|
|
$map = $this->getMappedChunk( $data ); |
90
|
|
|
$listItems = $product->getListItems( 'price', $this->listTypes ); |
91
|
|
|
|
92
|
|
|
foreach( $listItems as $listItem ) |
93
|
|
|
{ |
94
|
|
|
if( ( $refItem = $listItem->getRefItem() ) !== null ) { |
95
|
|
|
$listMap[ $refItem->getValue() ][ $refItem->getType() ][ $listItem->getType() ] = $listItem; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach( $map as $pos => $list ) |
100
|
|
|
{ |
101
|
|
|
if( $this->checkEntry( $list ) === false ) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$value = ( isset( $list['price.value'] ) ? $list['price.value'] : '0.00' ); |
106
|
|
|
$type = ( isset( $list['price.type'] ) ? $list['price.type'] : 'default' ); |
107
|
|
|
$typecode = ( isset( $list['product.lists.type'] ) ? $list['product.lists.type'] : 'default' ); |
108
|
|
|
|
109
|
|
|
if( isset( $listMap[$value][$type][$typecode] ) ) |
110
|
|
|
{ |
111
|
|
|
$listItem = $listMap[$value][$type][$typecode]; |
112
|
|
|
$refItem = $listItem->getRefItem(); |
113
|
|
|
unset( $listItems[ $listItem->getId() ] ); |
114
|
|
|
} |
115
|
|
|
else |
116
|
|
|
{ |
117
|
|
|
$listItem = $listManager->createItem(); |
118
|
|
|
$refItem = $manager->createItem(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$list['price.typeid'] = $this->getTypeId( 'price/type', 'product', $type ); |
122
|
|
|
$list['price.domain'] = 'product'; |
123
|
|
|
|
124
|
|
|
$refItem->fromArray( $this->addItemDefaults( $list ) ); |
125
|
|
|
$manager->saveItem( $refItem ); |
126
|
|
|
|
127
|
|
|
$list['product.lists.typeid'] = $this->getTypeId( 'product/lists/type', 'price', $typecode ); |
128
|
|
|
$list['product.lists.parentid'] = $product->getId(); |
129
|
|
|
$list['product.lists.refid'] = $refItem->getId(); |
130
|
|
|
$list['product.lists.domain'] = 'price'; |
131
|
|
|
|
132
|
|
|
$listItem->fromArray( $this->addListItemDefaults( $list, $pos ) ); |
133
|
|
|
$listManager->saveItem( $listItem ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach( $listItems as $listItem ) { |
137
|
|
|
$delete[] = $listItem->getRefId(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$manager->deleteItems( $delete ); |
141
|
|
|
$listManager->deleteItems( array_keys( $listItems ) ); |
142
|
|
|
|
143
|
|
|
$remaining = $this->getObject()->process( $product, $data ); |
144
|
|
|
|
145
|
|
|
$manager->commit(); |
146
|
|
|
} |
147
|
|
|
catch( \Exception $e ) |
148
|
|
|
{ |
149
|
|
|
$manager->rollback(); |
150
|
|
|
throw $e; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $remaining; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Adds the text item default values and returns the resulting array |
159
|
|
|
* |
160
|
|
|
* @param array $list Associative list of domain item keys and their values, e.g. "price.status" => 1 |
161
|
|
|
* @return array Given associative list enriched by default values if they were not already set |
162
|
|
|
*/ |
163
|
|
|
protected function addItemDefaults( array $list ) |
164
|
|
|
{ |
165
|
|
|
if( !isset( $list['price.currencyid'] ) ) { |
166
|
|
|
$list['price.currencyid'] = $this->getContext()->getLocale()->getCurrencyId(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if( !isset( $list['price.label'] ) ) { |
170
|
|
|
$list['price.label'] = $list['price.currencyid'] . ' ' . $list['price.value']; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if( !isset( $list['price.status'] ) ) { |
174
|
|
|
$list['price.status'] = 1; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $list; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Checks if an entry can be used for updating a media item |
183
|
|
|
* |
184
|
|
|
* @param array $list Associative list of key/value pairs from the mapping |
185
|
|
|
* @return boolean True if valid, false if not |
186
|
|
|
*/ |
187
|
|
|
protected function checkEntry( array $list ) |
188
|
|
|
{ |
189
|
|
|
if( !isset( $list['price.value'] ) || $list['price.value'] === '' || isset( $list['product.lists.type'] ) |
190
|
|
|
&& $this->listTypes !== null && !in_array( $list['product.lists.type'], (array) $this->listTypes ) |
191
|
|
|
) { |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|