1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Attribute\Observers\CatalogAttributeObserver |
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-attribute |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Attribute\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Attribute\Utils\ColumnKeys; |
24
|
|
|
use TechDivision\Import\Attribute\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
26
|
|
|
use TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Observer that create's the EAV catalog attribute itself. |
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-attribute |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class CatalogAttributeObserver extends AbstractAttributeImportObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The attribute processor instance. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface |
44
|
|
|
*/ |
45
|
|
|
protected $attributeBunchProcessor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializes the observer with the passed subject instance. |
49
|
|
|
* |
50
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The observer's subject instance |
51
|
|
|
* @param \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface $attributeBunchProcessor The attribute bunch processor instance |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
SubjectInterface $subject, |
55
|
|
|
AttributeBunchProcessorInterface $attributeBunchProcessor |
56
|
|
|
) { |
57
|
|
|
|
58
|
|
|
// pass the subject through to the parend observer |
59
|
|
|
parent::__construct($subject); |
60
|
|
|
|
61
|
|
|
// initialize the attribute bunch processor |
62
|
|
|
$this->attributeBunchProcessor = $attributeBunchProcessor; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Process the observer's business logic. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
protected function process() |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
// query whether or not, we've found a new attribute code => means we've found a new attribute |
74
|
|
|
if ($this->hasBeenProcessed($this->getValue(ColumnKeys::ATTRIBUTE_CODE))) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// initialize and persist the EAV catalog attribute |
79
|
|
|
$this->persistCatalogAttribute($this->initializeAttribute($this->prepareAttributes())); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Prepare the attributes of the entity that has to be persisted. |
84
|
|
|
* |
85
|
|
|
* @return array The prepared attributes |
86
|
|
|
*/ |
87
|
|
|
protected function prepareAttributes() |
88
|
|
|
{ |
89
|
|
|
|
90
|
|
|
// load the recently created EAV attribute ID |
91
|
|
|
$attributeId = $this->getLastAttributeId(); |
92
|
|
|
|
93
|
|
|
// load the data from the row |
94
|
|
|
$frontendInputRenderer = $this->getValue(ColumnKeys::FRONTEND_INPUT_RENDERER); |
95
|
|
|
$isGlobal = $this->getValue(ColumnKeys::IS_GLOBAL, 1); |
96
|
|
|
$isVisible = $this->getValue(ColumnKeys::IS_VISIBLE, 1); |
97
|
|
|
$isSearchable = $this->getValue(ColumnKeys::IS_SEARCHABLE, 0); |
98
|
|
|
$isFilterable = $this->getValue(ColumnKeys::IS_FILTERABLE, 0); |
99
|
|
|
$isComparable = $this->getValue(ColumnKeys::IS_COMPARABLE, 0); |
100
|
|
|
$isVisibleOnFront = $this->getValue(ColumnKeys::IS_VISIBLE_ON_FRONT, 0); |
101
|
|
|
$isHtmlAllowedOnFront = $this->getValue(ColumnKeys::IS_HTML_ALLOWED_ON_FRONT, 0); |
102
|
|
|
$isUsedForPriceRules = $this->getValue(ColumnKeys::IS_USED_FOR_PRICE_RULES, 0); |
103
|
|
|
$isFilterableInSearch = $this->getValue(ColumnKeys::IS_FILTERABLE_IN_SEARCH, 0); |
104
|
|
|
$usedInProductListing = $this->getValue(ColumnKeys::USED_IN_PRODUCT_LISTING, 0); |
105
|
|
|
$usedForSortBy = $this->getValue(ColumnKeys::USED_FOR_SORT_BY, 0); |
106
|
|
|
$applyTo = $this->getValue(ColumnKeys::APPLY_TO); |
107
|
|
|
$isVisibleInAdvancedSearch = $this->getValue(ColumnKeys::IS_VISIBLE_IN_ADVANCED_SEARCH, 0); |
108
|
|
|
$position = $this->getValue(ColumnKeys::POSITION, 0); |
109
|
|
|
$isWysiwygEnabled = $this->getValue(ColumnKeys::IS_WYSIWYG_ENABLED, 0); |
110
|
|
|
$isUsedForPromoRules = $this->getValue(ColumnKeys::IS_USED_FOR_PROMO_RULES, 0); |
111
|
|
|
$isRequiredInAdminStore = $this->getValue(ColumnKeys::IS_REQUIRED_IN_ADMIN_STORE, 0); |
112
|
|
|
$isUsedInGrid = $this->getValue(ColumnKeys::IS_USED_IN_GRID, 0); |
113
|
|
|
$isVisibleInGrid = $this->getValue(ColumnKeys::IS_VISIBLE_IN_GRID, 0); |
114
|
|
|
$isFilterableInGrid = $this->getValue(ColumnKeys::IS_FILTERABLE_IN_GRID, 0); |
115
|
|
|
$searchWeight = $this->getValue(ColumnKeys::SEARCH_WEIGHT, 1); |
116
|
|
|
|
117
|
|
|
// load and extract the additional data |
118
|
|
|
$additionalData = array(); |
119
|
|
|
$explodedAdditionalData = $this->getValue(ColumnKeys::ADDITIONAL_DATA, array(), array($this, 'explode')); |
120
|
|
|
foreach ($explodedAdditionalData as $value) { |
121
|
|
|
list ($key, $val) = $this->explode($value, '='); |
122
|
|
|
$additionalData[$key] = $val; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// return the prepared product |
126
|
|
|
return $this->initializeEntity( |
127
|
|
|
array( |
128
|
|
|
MemberNames::ATTRIBUTE_ID => $attributeId, |
129
|
|
|
MemberNames::FRONTEND_INPUT_RENDERER => $frontendInputRenderer, |
130
|
|
|
MemberNames::IS_GLOBAL => $isGlobal, |
131
|
|
|
MemberNames::IS_VISIBLE => $isVisible, |
132
|
|
|
MemberNames::IS_SEARCHABLE => $isSearchable, |
133
|
|
|
MemberNames::IS_FILTERABLE => $isFilterable, |
134
|
|
|
MemberNames::IS_COMPARABLE => $isComparable, |
135
|
|
|
MemberNames::IS_VISIBLE_ON_FRONT => $isVisibleOnFront, |
136
|
|
|
MemberNames::IS_HTML_ALLOWED_ON_FRONT => $isHtmlAllowedOnFront, |
137
|
|
|
MemberNames::IS_USED_FOR_PRICE_RULES => $isUsedForPriceRules, |
138
|
|
|
MemberNames::IS_FILTERABLE_IN_SEARCH => $isFilterableInSearch, |
139
|
|
|
MemberNames::USED_IN_PRODUCT_LISTING => $usedInProductListing, |
140
|
|
|
MemberNames::USED_FOR_SORT_BY => $usedForSortBy, |
141
|
|
|
MemberNames::APPLY_TO => $applyTo, |
142
|
|
|
MemberNames::IS_VISIBLE_IN_ADVANCED_SEARCH => $isVisibleInAdvancedSearch, |
143
|
|
|
MemberNames::POSITION => $position, |
144
|
|
|
MemberNames::IS_WYSIWYG_ENABLED => $isWysiwygEnabled, |
145
|
|
|
MemberNames::IS_USED_FOR_PROMO_RULES => $isUsedForPromoRules, |
146
|
|
|
MemberNames::IS_REQUIRED_IN_ADMIN_STORE => $isRequiredInAdminStore, |
147
|
|
|
MemberNames::IS_USED_IN_GRID => $isUsedInGrid, |
148
|
|
|
MemberNames::IS_VISIBLE_IN_GRID => $isVisibleInGrid, |
149
|
|
|
MemberNames::IS_FILTERABLE_IN_GRID => $isFilterableInGrid, |
150
|
|
|
MemberNames::SEARCH_WEIGHT => $searchWeight, |
151
|
|
|
MemberNames::ADDITIONAL_DATA => $additionalData |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Serialize the additional_data attribute of the passed array. |
158
|
|
|
* |
159
|
|
|
* @param array $attr The attribute with the data to serialize |
160
|
|
|
* |
161
|
|
|
* @return array The attribute with the serialized additional_data |
162
|
|
|
*/ |
163
|
|
|
protected function serializeAdditionalData(array $attr) |
164
|
|
|
{ |
165
|
|
|
|
166
|
|
|
// serialize the additional data value if available |
167
|
|
|
if (is_array($attr[MemberNames::ADDITIONAL_DATA])) { |
168
|
|
|
$attr[MemberNames::ADDITIONAL_DATA] = serialize($attr[MemberNames::ADDITIONAL_DATA]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// return the attribute |
172
|
|
|
return $attr; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Initialize the attribute with the passed attributes and returns an instance. |
177
|
|
|
* |
178
|
|
|
* @param array $attr The attribute attributes |
179
|
|
|
* |
180
|
|
|
* @return array The initialized attribute |
181
|
|
|
*/ |
182
|
|
|
protected function initializeAttribute(array $attr) |
183
|
|
|
{ |
184
|
|
|
return $this->serializeAdditionalData($attr); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return's the attribute bunch processor instance. |
189
|
|
|
* |
190
|
|
|
* @return \TechDivision\Import\Attribute\Services\AttributeBunchProcessorInterface The attribute bunch processor instance |
191
|
|
|
*/ |
192
|
|
|
protected function getAttributeBunchProcessor() |
193
|
|
|
{ |
194
|
|
|
return $this->attributeBunchProcessor; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Map's the passed attribute code to the attribute ID that has been created recently. |
199
|
|
|
* |
200
|
|
|
* @param string $attributeCode The attribute code that has to be mapped |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
protected function addAttributeCodeIdMapping($attributeCode) |
205
|
|
|
{ |
206
|
|
|
$this->getSubject()->addAttributeCodeIdMapping($attributeCode); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Queries whether or not the attribute with the passed code has already been processed. |
211
|
|
|
* |
212
|
|
|
* @param string $attributeCode The attribute code to check |
213
|
|
|
* |
214
|
|
|
* @return boolean TRUE if the path has been processed, else FALSE |
215
|
|
|
*/ |
216
|
|
|
protected function hasBeenProcessed($attributeCode) |
217
|
|
|
{ |
218
|
|
|
return $this->getSubject()->hasBeenProcessed($attributeCode); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Return's the ID of the attribute that has been created recently. |
223
|
|
|
* |
224
|
|
|
* @return integer The attribute ID |
225
|
|
|
*/ |
226
|
|
|
protected function getLastAttributeId() |
227
|
|
|
{ |
228
|
|
|
return $this->getSubject()->getLastAttributeId(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Persist the passed EAV catalog attribute. |
233
|
|
|
* |
234
|
|
|
* @param array $catalogAttribute The EAV catalog attribute to persist |
235
|
|
|
* |
236
|
|
|
* @return void |
237
|
|
|
*/ |
238
|
|
|
protected function persistCatalogAttribute(array $catalogAttribute) |
239
|
|
|
{ |
240
|
|
|
return $this->getAttributeBunchProcessor()->persistCatalogAttribute($catalogAttribute); |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|