1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Media\Observers\MediaGalleryValueObserver |
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-product-media |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Media\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
24
|
|
|
use TechDivision\Import\Product\Media\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Product\Media\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Product\Observers\AbstractProductImportObserver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Observer that creates/updates the product's media gallery value information. |
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-product-media |
35
|
|
|
* @link http://www.techdivision.com |
36
|
|
|
*/ |
37
|
|
|
class MediaGalleryValueObserver extends AbstractProductImportObserver |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Process the observer's business logic. |
42
|
|
|
* |
43
|
|
|
* @return array The processed row |
44
|
|
|
*/ |
45
|
|
|
protected function process() |
46
|
|
|
{ |
47
|
|
|
|
48
|
|
|
// query whether or not, the image changed |
49
|
|
|
if ($this->isParentImage($imagePath = $this->getValue(ColumnKeys::IMAGE_PATH))) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// initialize and persist the product media gallery value |
54
|
|
|
$productMediaGalleryValue = $this->initializeProductMediaGalleryValue($this->prepareAttributes()); |
55
|
|
|
$this->persistProductMediaGalleryValue($productMediaGalleryValue); |
56
|
|
|
|
57
|
|
|
// temporarily persist the image name |
58
|
|
|
$this->setParentImage($imagePath); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Prepare the product media gallery value that has to be persisted. |
63
|
|
|
* |
64
|
|
|
* @return array The prepared product media gallery value attributes |
65
|
|
|
*/ |
66
|
|
|
protected function prepareAttributes() |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
// load the product SKU and map it the entity ID |
70
|
|
|
$parentId = $this->getValue(ColumnKeys::IMAGE_PARENT_SKU, null, array($this, 'mapParentSku')); |
71
|
|
|
|
72
|
|
|
// load the store ID |
73
|
|
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
74
|
|
|
|
75
|
|
|
// load the value ID and the position counter |
76
|
|
|
$valueId = $this->getParentValueId(); |
77
|
|
|
$position = $this->raisePositionCounter(); |
78
|
|
|
|
79
|
|
|
// load the image label |
80
|
|
|
$imageLabel = $this->getValue(ColumnKeys::IMAGE_LABEL); |
81
|
|
|
|
82
|
|
|
// prepare the media gallery value |
83
|
|
|
return $this->initializeEntity( |
84
|
|
|
array( |
85
|
|
|
MemberNames::VALUE_ID => $valueId, |
86
|
|
|
MemberNames::STORE_ID => $storeId, |
87
|
|
|
MemberNames::ENTITY_ID => $parentId, |
88
|
|
|
MemberNames::LABEL => $imageLabel, |
89
|
|
|
MemberNames::POSITION => $position, |
90
|
|
|
MemberNames::DISABLED => 0 |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Initialize the product media gallery value with the passed attributes and returns an instance. |
97
|
|
|
* |
98
|
|
|
* @param array $attr The product media gallery value attributes |
99
|
|
|
* |
100
|
|
|
* @return array The initialized product media gallery value |
101
|
|
|
*/ |
102
|
|
|
protected function initializeProductMediaGalleryValue(array $attr) |
103
|
|
|
{ |
104
|
|
|
return $attr; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return's the store ID of the actual row, or of the default store |
109
|
|
|
* if no store view code is set in the CSV file. |
110
|
|
|
* |
111
|
|
|
* @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
112
|
|
|
* |
113
|
|
|
* @return integer The ID of the actual store |
114
|
|
|
* @throws \Exception Is thrown, if the store with the actual code is not available |
115
|
|
|
*/ |
116
|
|
|
protected function getRowStoreId($default = null) |
117
|
|
|
{ |
118
|
|
|
return $this->getSubject()->getRowStoreId($default); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Map's the passed SKU of the parent product to it's PK. |
123
|
|
|
* |
124
|
|
|
* @param string $parentSku The SKU of the parent product |
125
|
|
|
* |
126
|
|
|
* @return integer The primary key used to create relations |
127
|
|
|
*/ |
128
|
|
|
protected function mapParentSku($parentSku) |
129
|
|
|
{ |
130
|
|
|
return $this->mapSkuToEntityId($parentSku); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the entity ID for the passed SKU. |
135
|
|
|
* |
136
|
|
|
* @param string $sku The SKU to return the entity ID for |
137
|
|
|
* |
138
|
|
|
* @return integer The mapped entity ID |
139
|
|
|
* @throws \Exception Is thrown if the SKU is not mapped yet |
140
|
|
|
*/ |
141
|
|
|
protected function mapSkuToEntityId($sku) |
142
|
|
|
{ |
143
|
|
|
return $this->getSubject()->mapSkuToEntityId($sku); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set's the name of the created image. |
148
|
|
|
* |
149
|
|
|
* @param string $parentImage The name of the created image |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
protected function setParentImage($parentImage) |
154
|
|
|
{ |
155
|
|
|
$this->getSubject()->setParentImage($parentImage); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Return's the name of the created image. |
160
|
|
|
* |
161
|
|
|
* @return string The name of the created image |
162
|
|
|
*/ |
163
|
|
|
protected function getParentImage() |
164
|
|
|
{ |
165
|
|
|
return $this->getSubject()->getParentImage(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Return's TRUE if the passed image is the parent one. |
170
|
|
|
* |
171
|
|
|
* @param string $image The imageD to check |
172
|
|
|
* |
173
|
|
|
* @return boolean TRUE if the passed image is the parent one |
174
|
|
|
*/ |
175
|
|
|
protected function isParentImage($image) |
176
|
|
|
{ |
177
|
|
|
return $this->getParentImage() === $image; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return's the value ID of the created media gallery entry. |
182
|
|
|
* |
183
|
|
|
* @return integer The ID of the created media gallery entry |
184
|
|
|
*/ |
185
|
|
|
protected function getParentValueId() |
186
|
|
|
{ |
187
|
|
|
return $this->getSubject()->getParentValueId(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return's the store for the passed store code. |
192
|
|
|
* |
193
|
|
|
* @param string $storeCode The store code to return the store for |
194
|
|
|
* |
195
|
|
|
* @return array The requested store |
196
|
|
|
* @throws \Exception Is thrown, if the requested store is not available |
197
|
|
|
*/ |
198
|
|
|
protected function getStoreByStoreCode($storeCode) |
199
|
|
|
{ |
200
|
|
|
return $this->getSubject()->getStoreByStoreCode($storeCode); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the acutal value of the position counter and raise's it by one. |
205
|
|
|
* |
206
|
|
|
* @return integer The actual value of the position counter |
207
|
|
|
*/ |
208
|
|
|
protected function raisePositionCounter() |
209
|
|
|
{ |
210
|
|
|
return $this->getSubject()->raisePositionCounter(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Persist's the passed product media gallery value data. |
215
|
|
|
* |
216
|
|
|
* @param array $productMediaGalleryValue The product media gallery value data to persist |
217
|
|
|
* |
218
|
|
|
* @return void |
219
|
|
|
*/ |
220
|
|
|
protected function persistProductMediaGalleryValue($productMediaGalleryValue) |
221
|
|
|
{ |
222
|
|
|
$this->getSubject()->persistProductMediaGalleryValue($productMediaGalleryValue); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|