| 1 |
lars |
1 |
<?php
|
|
|
2 |
require_once 'GetAttributesCSRequestType.php';
|
|
|
3 |
require_once 'GetAttributesCSResponseType.php';
|
|
|
4 |
|
|
|
5 |
class EbatNs_CharacteristicSetDependencyLinker
|
|
|
6 |
{
|
|
|
7 |
private $_CharactericsSet = array();
|
|
|
8 |
private $_AttributeSystemVersion;
|
|
|
9 |
|
|
|
10 |
private $_proxy;
|
|
|
11 |
|
|
|
12 |
private $_csIdent;
|
|
|
13 |
private $_attribIdent;
|
|
|
14 |
private $_valueIdent;
|
|
|
15 |
|
|
|
16 |
public function __construct($proxy)
|
|
|
17 |
{
|
|
|
18 |
$this->_proxy = $proxy;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
public function getCharactericsSetArray()
|
|
|
22 |
{
|
|
|
23 |
return $this->_CharactericsSet;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
public function getCharactericsSet($index)
|
|
|
27 |
{
|
|
|
28 |
return $this->_CharactericsSet[$index];
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function getAttVersion()
|
|
|
32 |
{
|
|
|
33 |
return $this->_AttributeSystemVersion;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function getCharactericsSetCount()
|
|
|
37 |
{
|
|
|
38 |
return count($this->_CharactericsSet);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// $csSetId could be either a single csSet-id or an array of ids
|
|
|
42 |
public function fetchByCsSetId($csSetId, $VersionOnly = false)
|
|
|
43 |
{
|
|
|
44 |
if (is_array($csSetId))
|
|
|
45 |
{
|
|
|
46 |
foreach($csSetId as $setId)
|
|
|
47 |
$this->_fetchCsSets($setId, $VersionOnly);
|
|
|
48 |
}
|
|
|
49 |
else
|
|
|
50 |
$this->_fetchCsSets($csSetId, $VersionOnly);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
// use $VersionOnly to populate only the version fields
|
|
|
54 |
public function fetchAll($VersionOnly = false)
|
|
|
55 |
{
|
|
|
56 |
$this->_fetchCsSets(null, $VersionOnly);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// function to link the dependent attributes into their parent attribute values
|
|
|
60 |
private function linkDependencies($attribList, $dependencies)
|
|
|
61 |
{
|
|
|
62 |
// get each parent attributeid and the dependencies for each value
|
|
|
63 |
foreach($dependencies as $parentId => $parentValueArray)
|
|
|
64 |
{
|
|
|
65 |
// reference to the parent attribute object
|
|
|
66 |
$parentAttribObj = &$attribList[$parentId];
|
|
|
67 |
// get each value of the parent attribute and the array of childs
|
|
|
68 |
foreach($parentValueArray as $parentValue => $childIdArray)
|
|
|
69 |
// get each attributeid of the childs and their arrays of values
|
|
|
70 |
foreach($childIdArray as $childId => $childValueArrays)
|
|
|
71 |
// sometimes we have multiple arrays of values with the same child attributeid
|
|
|
72 |
// linking to valueid of the parent's attribute
|
|
|
73 |
foreach($childValueArrays as $outerKey => $childValueArray)
|
|
|
74 |
{
|
|
|
75 |
// pick a copy of the attribute for the child attributeid
|
|
|
76 |
if (is_object($attribList[$childId]))
|
|
|
77 |
// we have to use the clone keyword with brackets,
|
|
|
78 |
// as PHP4 doesn't know this keyword. a function clone()
|
|
|
79 |
// has to be defined in global scope. PHP5 ignores the
|
|
|
80 |
// brackets and executes the internal "clone"
|
|
|
81 |
$childAttribObj = clone($attribList[$childId]);
|
|
|
82 |
else
|
|
|
83 |
$childAttribObj = $attribList[$childId];
|
|
|
84 |
// empty the values of the child attribute, since we don't need all
|
|
|
85 |
// valueobjects of in the child attribute for the specific parent attribute
|
|
|
86 |
$childAttribObj->getValueList()->setValue(array());
|
|
|
87 |
// get the dependency type for a later rendering of the csset
|
|
|
88 |
$dependencyType = $childValueArray[0];
|
|
|
89 |
// get each valueobject to be linked to the parent attributes value
|
|
|
90 |
foreach($childValueArray[1] as $key => $childValueObj)
|
|
|
91 |
{
|
|
|
92 |
// get the valueid of the childs valueonject
|
|
|
93 |
$childValue = $childValueObj->getTypeAttribute('id');
|
|
|
94 |
// pick the childs attribute value and reference it to the
|
|
|
95 |
// parents value array
|
|
|
96 |
$childAttribObj->getValueList()->setValue($attribList[$childId]->getValueList()->getValue($this->_valueIdent[$childId][$childValue]), $key);
|
|
|
97 |
// unset the dependency object of the child attribute,
|
|
|
98 |
// as we don't need it anymore
|
|
|
99 |
$childAttribObj->setDependency(null);
|
|
|
100 |
}
|
|
|
101 |
// update the count value for the inserted child attribute values
|
|
|
102 |
$childAttribObj->getValueList()->attributeValues['count'] = count($childAttribObj->getValueList()->getValue());
|
|
|
103 |
// insert the dependency type to the child attribute object for a later rendering
|
|
|
104 |
$childAttribObj->attributeValues['dependencyType'] = $dependencyType;
|
|
|
105 |
// copy the child attribute object to the parent's attribute value object
|
|
|
106 |
$parentAttribObj->getValueList()->getValue($this->_valueIdent[$parentId][$parentValue])->setDependencyList($childAttribObj, $outerKey);
|
|
|
107 |
// unset the dependency object of the parent attribute,
|
|
|
108 |
// as we don't need it anymore
|
|
|
109 |
$parentAttribObj->setDependency(null);
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
return $attribList;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
// combine the working array of the linked attribute with the complete attributeData-section
|
|
|
117 |
// of the GetAttributesCS-call
|
|
|
118 |
private function combineCharacteristics($attributeData, $linkedAttribs)
|
|
|
119 |
{
|
|
|
120 |
// get each linked attribute object and the cssetid it belongs to
|
|
|
121 |
foreach($linkedAttribs as $linkedCharacteristicSetId => $linkedAttributeArray)
|
|
|
122 |
{
|
|
|
123 |
// reference to the attribute array of the csset in the attributeData-section
|
|
|
124 |
// of the GetAttributesCS-call
|
|
|
125 |
$characteristicsListInitial = &$attributeData->getCharacteristics(0)->getCharacteristicsSet($this->_csIdent[$linkedCharacteristicSetId])->getCharacteristicsList()->getInitial();
|
|
|
126 |
// empty the attribute array, since we have rebuilt all attributes in the working array
|
|
|
127 |
$characteristicsListInitial = array();
|
|
|
128 |
// get each linked attribute to copy it to the reference of the attributeData
|
|
|
129 |
foreach($linkedAttributeArray as $linkedAttribute)
|
|
|
130 |
// don't copy attributes which are a child (have a parent attribute id),
|
|
|
131 |
// since we have linked/inserted them to the parent attributes
|
|
|
132 |
if (!isset($linkedAttribute->attributeValues['parentAttrId']))
|
|
|
133 |
$characteristicsListInitial[] = $linkedAttribute;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return $attributeData;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
// - build some arrays for an easy location of the sets, attributes and value
|
|
|
140 |
// deep inside the characteristic sets
|
|
|
141 |
// - copy all attributes and their dependencies to link them together
|
|
|
142 |
private function manageDependencies($attributeData)
|
|
|
143 |
{
|
|
|
144 |
// array to identify and locate the cssets
|
|
|
145 |
$this->_csIdent = array();
|
|
|
146 |
|
|
|
147 |
// pick up all cssets to link the dependent attributes of each set
|
|
|
148 |
$characteristics = $attributeData->getCharacteristics();
|
|
|
149 |
$CharacteristicsSets = $characteristics[0]->getCharacteristicsSet();
|
|
|
150 |
foreach($CharacteristicsSets as $CharacteristicsSetKey => $CharacteristicsSet)
|
|
|
151 |
{
|
|
|
152 |
// working array of all attributes in a set
|
|
|
153 |
$copyAttribs = array();
|
|
|
154 |
// working array of all dependencies between all attributes in a set
|
|
|
155 |
$depArray = array();
|
|
|
156 |
// array to identify and locate the attributes
|
|
|
157 |
$this->_attribIdent = array();
|
|
|
158 |
// array to identify and locate the attributevalues
|
|
|
159 |
$this->_valueIdent = array();
|
|
|
160 |
|
|
|
161 |
// save the array-keyids of each csset for an easy location
|
|
|
162 |
$this->_csIdent[$CharacteristicsSet->attributeValues['id']] = $CharacteristicsSetKey;
|
|
|
163 |
|
|
|
164 |
// pick up all attributes and dependencies in the set and copy them
|
|
|
165 |
// to the working arrays
|
|
|
166 |
$csAttribs = $CharacteristicsSet->getCharacteristicsList()->getInitial();
|
|
|
167 |
foreach($csAttribs as $csAttribKey => $csAttrib)
|
|
|
168 |
{
|
|
|
169 |
// save the array-keyids of each attribute for an easy location
|
|
|
170 |
$this->_attribIdent[$csAttrib->attributeValues['id']] = $csAttribKey;
|
|
|
171 |
// copy each attribute to the working array
|
|
|
172 |
$copyAttribs[$csAttrib->attributeValues['id']] = $csAttrib;
|
|
|
173 |
|
|
|
174 |
// pick up all attributevalues and dependencies of the attribute
|
|
|
175 |
if ($csAttrib->getValueList())
|
|
|
176 |
{
|
|
|
177 |
// save the array-keyids of each value of the attribute
|
|
|
178 |
// for an easy location
|
|
|
179 |
foreach($csAttrib->getValueList()->getValue() as $valueKey => $valueList)
|
|
|
180 |
$this->_valueIdent[$csAttrib->attributeValues['id']][$valueList->attributeValues['id']] = $valueKey;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if ($csAttrib->getDependency())
|
|
|
184 |
{
|
|
|
185 |
// build the working array of all dependencies of the attribute
|
|
|
186 |
foreach($csAttrib->getDependency() as $dependency)
|
|
|
187 |
$depArray[$csAttrib->attributeValues['id']][$dependency->attributeValues['parentValueId']][$dependency->attributeValues['childAttrId']][] = array($dependency->attributeValues['type'], $dependency->getValue());
|
|
|
188 |
}
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// link all dependencies of a single csset to the attributes
|
|
|
192 |
$linkedAttribs[$CharacteristicsSet->attributeValues['id']] = $this->linkDependencies($copyAttribs, $depArray);
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
// combine the linked working arrays of all cssets with the attributeData-section
|
|
|
196 |
// of the GetAttributesCS-call
|
|
|
197 |
$combinedCharacteristicSets = $this->combineCharacteristics($attributeData, $linkedAttribs);
|
|
|
198 |
|
|
|
199 |
return $combinedCharacteristicSets;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
// $CsIdToGet can be null (and so loading ALL CsSets)
|
|
|
203 |
// use $VersionOnly to populate only the version fields
|
|
|
204 |
private function _fetchCsSets($CsIdToGet, $VersionOnly = false)
|
|
|
205 |
{
|
|
|
206 |
// so query the CharactericsSet ...
|
|
|
207 |
$request = new GetAttributesCSRequestType();
|
|
|
208 |
|
|
|
209 |
if ($CsIdToGet)
|
|
|
210 |
$request->setAttributeSetID($CsIdToGet);
|
|
|
211 |
|
|
|
212 |
if (!$VersionOnly)
|
|
|
213 |
{
|
|
|
214 |
$request->setDetailLevel('ReturnAll');
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
$response = $this->_proxy->GetAttributesCS($request);
|
|
|
218 |
|
|
|
219 |
$this->_AttributeSystemVersion = $response->getAttributeSystemVersion();
|
|
|
220 |
|
|
|
221 |
if (!$VersionOnly)
|
|
|
222 |
{
|
|
|
223 |
$attributeData = $response->getAttributeData();
|
|
|
224 |
|
|
|
225 |
$parser = $this->_proxy->getParser('http://www.intradesys.com/Schemas/ebay/AttributeData_Extension.xsd');
|
|
|
226 |
$parser->setExtensionPrefix('EbatNsCsSetExt_');
|
|
|
227 |
|
|
|
228 |
$parserResponse = $parser->decode('eBay', '<?xml version="1.0" encoding="utf-8" ?>' . utf8_encode($attributeData), 3, 'EbatNsCsSetExt_AttributeDataType');
|
|
|
229 |
|
|
|
230 |
$attributeDependencyData = $this->manageDependencies($parserResponse);
|
|
|
231 |
|
|
|
232 |
$characteristics = $attributeDependencyData->getCharacteristics();
|
|
|
233 |
$characteristics = $characteristics[0];
|
|
|
234 |
|
|
|
235 |
$characteristicSets = $characteristics->getCharacteristicsSet();
|
|
|
236 |
foreach($characteristicSets as $characteristicSet)
|
|
|
237 |
{
|
|
|
238 |
$this->_CharactericsSet[$characteristicSet->attributeValues['id']] = $characteristicSet;
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
}
|
|
|
243 |
?>
|