1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Subjects\AbstractSubject |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\MemberNames; |
24
|
|
|
use TechDivision\Import\Utils\RegistryKeys; |
25
|
|
|
use TechDivision\Import\Utils\BackendTypeKeys; |
26
|
|
|
use TechDivision\Import\Utils\EntityTypeCodes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* An abstract EAV subject implementation. |
30
|
|
|
* |
31
|
|
|
* @author Tim Wagner <[email protected]> |
32
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
33
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
34
|
|
|
* @link https://github.com/techdivision/import |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
abstract class AbstractEavSubject extends AbstractSubject implements EavSubjectInterface |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The available EAV attributes, grouped by their attribute set and the attribute set name as keys. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $attributes = array(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The available user defined EAV attributes, grouped by their entity type. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $userDefinedAttributes = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The attribute set of the entity that has to be created. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $attributeSet = array(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The available EAV attribute sets. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $attributeSets = array(); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The mapping for the supported backend types (for the EAV entity) => persist methods. |
70
|
|
|
* |
71
|
|
|
* @var array |
72
|
|
|
*/ |
73
|
|
|
protected $backendTypes = array( |
74
|
|
|
BackendTypeKeys::BACKEND_TYPE_DATETIME => array('persistDatetimeAttribute', 'loadDatetimeAttribute', 'deleteDatetimeAttribute'), |
75
|
|
|
BackendTypeKeys::BACKEND_TYPE_DECIMAL => array('persistDecimalAttribute', 'loadDecimalAttribute', 'deleteDecimalAttribute'), |
76
|
|
|
BackendTypeKeys::BACKEND_TYPE_INT => array('persistIntAttribute', 'loadIntAttribute', 'deleteIntAttribute'), |
77
|
|
|
BackendTypeKeys::BACKEND_TYPE_TEXT => array('persistTextAttribute', 'loadTextAttribute', 'deleteTextAttribute'), |
78
|
|
|
BackendTypeKeys::BACKEND_TYPE_VARCHAR => array('persistVarcharAttribute', 'loadVarcharAttribute', 'deleteVarcharAttribute') |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The mappings for the entity type code to attribute set. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $entityTypeCodeToAttributeSetMappings = array( |
87
|
|
|
EntityTypeCodes::CATALOG_PRODUCT => EntityTypeCodes::CATALOG_PRODUCT, |
88
|
|
|
EntityTypeCodes::CATALOG_PRODUCT_PRICE => EntityTypeCodes::CATALOG_PRODUCT, |
89
|
|
|
EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => EntityTypeCodes::CATALOG_PRODUCT, |
90
|
|
|
EntityTypeCodes::CATALOG_CATEGORY => EntityTypeCodes::CATALOG_CATEGORY, |
91
|
|
|
EntityTypeCodes::EAV_ATTRIBUTE => EntityTypeCodes::EAV_ATTRIBUTE, |
92
|
|
|
EntityTypeCodes::NONE => EntityTypeCodes::NONE |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The default mappings for the user defined attributes, based on the attributes frontend input type. |
97
|
|
|
* |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
protected $defaultFrontendInputCallbackMappings = array(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return's the default callback frontend input mappings for the user defined attributes. |
104
|
|
|
* |
105
|
|
|
* @return array The default frontend input callback mappings |
106
|
|
|
*/ |
107
|
22 |
|
public function getDefaultFrontendInputCallbackMappings() |
108
|
|
|
{ |
109
|
22 |
|
return $this->defaultFrontendInputCallbackMappings; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Intializes the previously loaded global data for exactly one bunch. |
114
|
|
|
* |
115
|
|
|
* @param string $serial The serial of the actual import |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
22 |
|
public function setUp($serial) |
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
// load the status of the actual import |
123
|
22 |
|
$status = $this->getRegistryProcessor()->getAttribute($serial); |
124
|
|
|
|
125
|
|
|
// load the global data we've prepared initially |
126
|
22 |
|
$this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES]; |
127
|
22 |
|
$this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS]; |
128
|
22 |
|
$this->userDefinedAttributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_USER_DEFINED_ATTRIBUTES]; |
129
|
|
|
|
130
|
|
|
// load the default frontend callback mappings from the child instance and merge with the one from the configuration |
131
|
22 |
|
$defaultFrontendInputCallbackMappings = $this->getDefaultFrontendInputCallbackMappings(); |
132
|
|
|
|
133
|
|
|
// load the available frontend input callbacks from the configuration |
134
|
22 |
|
$availableFrontendInputCallbacks = $this->getConfiguration()->getFrontendInputCallbacks(); |
135
|
|
|
|
136
|
|
|
// merge the default mappings with the one's found in the configuration |
137
|
22 |
|
foreach ($availableFrontendInputCallbacks as $frontendInputCallbackMappings) { |
138
|
|
|
foreach ($frontendInputCallbackMappings as $frontendInput => $frontentInputMappings) { |
139
|
|
|
$defaultFrontendInputCallbackMappings[$frontendInput] = $frontentInputMappings; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// load the user defined EAV attributes |
144
|
22 |
|
$eavUserDefinedAttributes = $this->getEavUserDefinedAttributes(); |
145
|
|
|
|
146
|
|
|
// load the user defined attributes and add the callback mappings |
147
|
22 |
|
foreach ($eavUserDefinedAttributes as $eavAttribute) { |
148
|
|
|
// load attribute code and frontend input type |
149
|
1 |
|
$attributeCode = $eavAttribute[MemberNames::ATTRIBUTE_CODE]; |
150
|
1 |
|
$frontendInput = $eavAttribute[MemberNames::FRONTEND_INPUT]; |
151
|
|
|
|
152
|
|
|
// query whether or not the array for the mappings has been initialized |
153
|
1 |
|
if (!isset($this->callbackMappings[$attributeCode])) { |
154
|
1 |
|
$this->callbackMappings[$attributeCode] = array(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// set the appropriate callback mapping for the attributes input type |
158
|
1 |
|
if (isset($defaultFrontendInputCallbackMappings[$frontendInput])) { |
159
|
1 |
|
foreach ($defaultFrontendInputCallbackMappings[$frontendInput] as $defaultFrontendInputCallbackMapping) { |
160
|
1 |
|
$this->callbackMappings[$attributeCode][] = $defaultFrontendInputCallbackMapping; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// prepare the callbacks |
166
|
22 |
|
parent::setUp($serial); |
167
|
22 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return's mapping for the supported backend types (for the product entity) => persist methods. |
171
|
|
|
* |
172
|
|
|
* @return array The mapping for the supported backend types |
173
|
|
|
*/ |
174
|
1 |
|
public function getBackendTypes() |
175
|
|
|
{ |
176
|
1 |
|
return $this->backendTypes; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set's the attribute set of the product that has to be created. |
181
|
|
|
* |
182
|
|
|
* @param array $attributeSet The attribute set |
183
|
|
|
* |
184
|
|
|
* @return void |
185
|
|
|
*/ |
186
|
5 |
|
public function setAttributeSet(array $attributeSet) |
187
|
|
|
{ |
188
|
5 |
|
$this->attributeSet = $attributeSet; |
189
|
5 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return's the attribute set of the product that has to be created. |
193
|
|
|
* |
194
|
|
|
* @return array The attribute set |
195
|
|
|
*/ |
196
|
1 |
|
public function getAttributeSet() |
197
|
|
|
{ |
198
|
1 |
|
return $this->attributeSet; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Cast's the passed value based on the backend type information. |
203
|
|
|
* |
204
|
|
|
* @param string $backendType The backend type to cast to |
205
|
|
|
* @param mixed $value The value to be casted |
206
|
|
|
* |
207
|
|
|
* @return mixed The casted value |
208
|
|
|
*/ |
209
|
9 |
|
public function castValueByBackendType($backendType, $value) |
210
|
|
|
{ |
211
|
|
|
|
212
|
|
|
// cast the value to a valid timestamp |
213
|
9 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_DATETIME) { |
214
|
2 |
|
return \DateTime::createFromFormat($this->getConfiguration()->getDateConverter()->getSourceDateFormat(), $value)->format('Y-m-d H:i:s'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// cast the value to a float value |
218
|
7 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_FLOAT) { |
219
|
2 |
|
return (float) $value; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// cast the value to an integer |
223
|
5 |
|
if ($backendType === BackendTypeKeys::BACKEND_TYPE_INT) { |
224
|
3 |
|
return (int) $value; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// we don't need to cast strings |
228
|
2 |
|
return $value; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return's the entity type code to be used. |
233
|
|
|
* |
234
|
|
|
* @return string The entity type code to be used |
235
|
|
|
*/ |
236
|
22 |
|
public function getEntityTypeCode() |
237
|
|
|
{ |
238
|
|
|
|
239
|
|
|
// load the entity type code from the configuration |
240
|
22 |
|
$entityTypeCode = $this->getConfiguration()->getConfiguration()->getEntityTypeCode(); |
241
|
|
|
|
242
|
|
|
// try to map the entity type code |
243
|
22 |
|
if (isset($this->entityTypeCodeToAttributeSetMappings[$entityTypeCode])) { |
244
|
8 |
|
$entityTypeCode = $this->entityTypeCodeToAttributeSetMappings[$entityTypeCode]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// return the (mapped) entity type code |
248
|
22 |
|
return $entityTypeCode; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return's the attribute set with the passed attribute set name. |
253
|
|
|
* |
254
|
|
|
* @param string $attributeSetName The name of the requested attribute set |
255
|
|
|
* |
256
|
|
|
* @return array The attribute set data |
257
|
|
|
* @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available |
258
|
|
|
*/ |
259
|
3 |
|
public function getAttributeSetByAttributeSetName($attributeSetName) |
260
|
|
|
{ |
261
|
|
|
|
262
|
|
|
// query whether or not attribute sets for the actualy entity type code are available |
263
|
3 |
|
if (isset($this->attributeSets[$entityTypeCode = $this->getEntityTypeCode()])) { |
264
|
|
|
// load the attribute sets for the actualy entity type code |
265
|
2 |
|
$attributSets = $this->attributeSets[$entityTypeCode]; |
266
|
|
|
|
267
|
|
|
// query whether or not, the requested attribute set is available |
268
|
2 |
|
if (isset($attributSets[$attributeSetName])) { |
269
|
1 |
|
return $attributSets[$attributeSetName]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
// throw an exception, if not |
273
|
1 |
|
throw new \Exception( |
274
|
1 |
|
$this->appendExceptionSuffix( |
275
|
1 |
|
sprintf('Found invalid attribute set name "%s"', $attributeSetName) |
276
|
|
|
) |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// throw an exception, if not |
281
|
1 |
|
throw new \Exception( |
282
|
1 |
|
$this->appendExceptionSuffix( |
283
|
1 |
|
sprintf('Found invalid entity type code "%s"', $entityTypeCode) |
284
|
|
|
) |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Return's the attributes for the attribute set of the product that has to be created. |
290
|
|
|
* |
291
|
|
|
* @return array The attributes |
292
|
|
|
* @throws \Exception Is thrown, if the attribute set or the given entity type with the passed name is not available |
293
|
|
|
*/ |
294
|
5 |
|
public function getAttributes() |
295
|
|
|
{ |
296
|
|
|
|
297
|
|
|
// query whether or not, the requested EAV attributes are available |
298
|
5 |
|
if (isset($this->attributes[$entityTypeCode = $this->getEntityTypeCode()])) { |
299
|
|
|
// load the attributes for the entity type code |
300
|
4 |
|
$attributes = $this->attributes[$entityTypeCode]; |
301
|
|
|
|
302
|
|
|
// query whether or not an attribute set has been loaded from the source file |
303
|
4 |
|
if (is_array($this->attributeSet) && isset($this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME])) { |
304
|
|
|
// load the attribute set name |
305
|
4 |
|
$attributeSetName = $this->attributeSet[MemberNames::ATTRIBUTE_SET_NAME]; |
306
|
|
|
|
307
|
|
|
// query whether or not attributes for the actual attribute set name |
308
|
4 |
|
if ($attributeSetName && isset($attributes[$attributeSetName])) { |
309
|
3 |
|
return $attributes[$attributeSetName]; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
// throw an exception, if not |
313
|
1 |
|
throw new \Exception( |
314
|
1 |
|
$this->appendExceptionSuffix( |
315
|
1 |
|
sprintf('Found invalid attribute set name "%s"', $attributeSetName) |
316
|
|
|
) |
317
|
|
|
); |
318
|
|
|
} else { |
319
|
|
|
return call_user_func_array('array_merge', $attributes); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
// throw an exception, if not |
324
|
1 |
|
throw new \Exception( |
325
|
1 |
|
$this->appendExceptionSuffix( |
326
|
1 |
|
sprintf('Found invalid entity type code "%s"', $entityTypeCode) |
327
|
|
|
) |
328
|
|
|
); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Return's an array with the available user defined EAV attributes for the actual entity type. |
333
|
|
|
* |
334
|
|
|
* @return array The array with the user defined EAV attributes |
335
|
|
|
*/ |
336
|
22 |
|
public function getEavUserDefinedAttributes() |
337
|
|
|
{ |
338
|
|
|
|
339
|
|
|
// initialize the array with the user defined EAV attributes |
340
|
22 |
|
$eavUserDefinedAttributes = array(); |
341
|
|
|
|
342
|
|
|
// query whether or not user defined EAV attributes for the actualy entity type are available |
343
|
22 |
|
if (isset($this->userDefinedAttributes[$entityTypeCode = $this->getEntityTypeCode()])) { |
344
|
2 |
|
$eavUserDefinedAttributes = $this->userDefinedAttributes[$entityTypeCode]; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
// return the array with the user defined EAV attributes |
348
|
22 |
|
return $eavUserDefinedAttributes; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Return's the EAV attribute with the passed attribute code. |
353
|
|
|
* |
354
|
|
|
* @param string $attributeCode The attribute code |
355
|
|
|
* |
356
|
|
|
* @return array The array with the EAV attribute |
357
|
|
|
* @throws \Exception Is thrown if the attribute with the passed code is not available |
358
|
|
|
*/ |
359
|
2 |
|
public function getEavAttributeByAttributeCode($attributeCode) |
360
|
|
|
{ |
361
|
|
|
|
362
|
|
|
// load the attributes |
363
|
2 |
|
$attributes = $this->getAttributes(); |
364
|
|
|
|
365
|
|
|
// query whether or not the attribute exists |
366
|
2 |
|
if (isset($attributes[$attributeCode])) { |
367
|
1 |
|
return $attributes[$attributeCode]; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
// throw an exception if the requested attribute is not available |
371
|
1 |
|
throw new \Exception( |
372
|
1 |
|
$this->appendExceptionSuffix( |
373
|
1 |
|
sprintf('Can\'t load attribute with code "%s"', $attributeCode) |
374
|
|
|
) |
375
|
|
|
); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|