|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2019 |
|
6
|
|
|
* @package Controller |
|
7
|
|
|
* @subpackage Common |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Controller\Common\Common\Import\Xml\Processor\Catalog; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Catalog processor for product XML imports |
|
16
|
|
|
* |
|
17
|
|
|
* @package Controller |
|
18
|
|
|
* @subpackage Common |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Controller\Common\Common\Import\Xml\Processor\Base |
|
22
|
|
|
implements \Aimeos\Controller\Common\Common\Import\Xml\Processor\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** controller/common/common/import/xml/processor/catalog/name |
|
25
|
|
|
* Name of the catalog processor implementation |
|
26
|
|
|
* |
|
27
|
|
|
* Use "Myname" if your class is named "\Aimeos\Controller\Common\Common\Import\Xml\Processor\Catalog\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 2019.04 |
|
32
|
|
|
* @category Developer |
|
33
|
|
|
*/ |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
private $listTypes = []; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Updates the given item using the data from the DOM node |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Aimeos\MShop\Common\Item\Iface $item Item which should be updated |
|
43
|
|
|
* @param \DOMNode $node XML document node containing a list of nodes to process |
|
44
|
|
|
* @return \Aimeos\MShop\Common\Item\Iface Updated item |
|
45
|
|
|
*/ |
|
46
|
|
|
public function process( \Aimeos\MShop\Common\Item\Iface $item, \DOMNode $node ) |
|
47
|
|
|
{ |
|
48
|
|
|
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Common\Item\ListRef\Iface::class, $item ); |
|
49
|
|
|
|
|
50
|
|
|
$listManager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' ); |
|
51
|
|
|
$listItems = $this->getListItems( $item->getResourceType(), $item->getId() ); |
|
52
|
|
|
$catItems = $this->getCatalogItems( $node ); |
|
53
|
|
|
$map = []; |
|
54
|
|
|
|
|
55
|
|
|
foreach( $listItems as $listItem ) { |
|
56
|
|
|
$map[$listItem->getParentId()][$listItem->getType()] = $listItem; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
foreach( $node->childNodes as $node ) |
|
61
|
|
|
{ |
|
62
|
|
|
if( $node->nodeName !== 'catalogitem' |
|
63
|
|
|
|| ( $refattr = $node->attributes->getNamedItem( 'ref' ) ) === null |
|
64
|
|
|
|| !isset( $catItems[$refattr->nodeValue] ) |
|
65
|
|
|
) { |
|
66
|
|
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$list = []; |
|
70
|
|
|
$catcode = $refattr->nodeValue; |
|
|
|
|
|
|
71
|
|
|
$parentid = $catItems[$refattr->nodeValue]->getId(); |
|
72
|
|
|
$typeattr = $node->attributes->getNamedItem( 'lists.type' ); |
|
73
|
|
|
$type = ( $typeattr !== null ? $typeattr->nodeValue : 'default' ); |
|
74
|
|
|
|
|
75
|
|
|
foreach( $node->attributes as $attrName => $attrNode ) { |
|
76
|
|
|
$list['catalog.' . $attrName] = $attrNode->nodeValue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$name = 'catalog.lists.config'; |
|
80
|
|
|
$list[$name] = ( isset( $list[$name] ) ? (array) json_decode( $list[$name] ) : [] ); |
|
81
|
|
|
$list['catalog.lists.type'] = $type; |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if( isset( $map[$parentid][$type] ) ) { |
|
85
|
|
|
$listItem = $map[$parentid][$type]; unset( $map[$parentid][$type] ); |
|
86
|
|
|
} else { |
|
87
|
|
|
$listItem = $listManager->createItem(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$listItem = $listItem->fromArray( $list )->setDomain( $item->getResourceType() ) |
|
91
|
|
|
->setRefId( $item->getId() )->setParentId( $parentid ); |
|
92
|
|
|
$listManager->saveItem( $listItem, false ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$listManager->deleteItems( array_keys( $listItems ) ); |
|
96
|
|
|
|
|
97
|
|
|
return $item; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the catalog items referenced in the DOM node |
|
103
|
|
|
* |
|
104
|
|
|
* @param \DOMNode $node XML document node containing a list of nodes to process |
|
105
|
|
|
* @return \Aimeos\MShop\Catalog\Item\Iface[] List of referenced catalog items |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getCatalogItems( \DOMNode $node ) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach( $node->childNodes as $node ) |
|
110
|
|
|
{ |
|
111
|
|
|
if( $node->nodeName === 'catalogitem' |
|
112
|
|
|
&& ( $refAttr = $node->attributes->getNamedItem( 'ref' ) ) !== null |
|
113
|
|
|
) { |
|
114
|
|
|
$codes[] = $refAttr->nodeValue; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$items = []; |
|
119
|
|
|
|
|
120
|
|
|
if( !empty( $codes ) ) |
|
121
|
|
|
{ |
|
122
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog' ); |
|
123
|
|
|
|
|
124
|
|
|
$search = $manager->createSearch()->setSlice( 0, count( $codes ) ); |
|
125
|
|
|
$search->setConditions( $search->compare( '==', 'catalog.code', $codes ) ); |
|
126
|
|
|
|
|
127
|
|
|
foreach( $manager->searchItems( $search ) as $item ) { |
|
128
|
|
|
$items[$item->getCode()] = $item; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $items; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns the catalog list items for the given referenced ID |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $domain Domain name the referenced ID belongs to |
|
140
|
|
|
* @param string $id ID of the referenced domain item |
|
141
|
|
|
* @return array List of catalog list items |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function getListItems( $domain, $id ) |
|
144
|
|
|
{ |
|
145
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' ); |
|
146
|
|
|
$search = $manager->createSearch()->setSlice( 0, 10000 ); |
|
147
|
|
|
$expr = []; |
|
148
|
|
|
|
|
149
|
|
|
foreach( $this->getListTypes( $domain ) as $type ) { |
|
150
|
|
|
$expr[] = $search->compare( '==', 'catalog.lists.key', $domain . '|' . $type . '|' . $id ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $manager->searchItems( $search->setConditions( $search->combine( '||', $expr ) ) ); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the available catalog list types for the given domain |
|
159
|
|
|
* |
|
160
|
|
|
* @param $domain Domain name the list types belong to |
|
|
|
|
|
|
161
|
|
|
* @return string[] List of list type codes |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getListTypes( $domain ) |
|
164
|
|
|
{ |
|
165
|
|
|
if( !isset( $this->getListTypes[$domain] ) ) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->getListTypes[$domain] = []; |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists/type' ); |
|
170
|
|
|
|
|
171
|
|
|
$search = $manager->createSearch()->setSlice( 0, 10000 ); |
|
172
|
|
|
$search->setConditions( $search->compare( '==', 'catalog.lists.type.domain', $domain ) ); |
|
173
|
|
|
|
|
174
|
|
|
foreach( $manager->searchItems( $search ) as $item ) { |
|
175
|
|
|
$this->listTypes[$domain][] = $item->getCode(); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->listTypes[$domain]; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|