1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Category\Subjects\BunchSubject |
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 2019 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-category |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Category\Subjects; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Subjects\ExportableTrait; |
24
|
|
|
use TechDivision\Import\Subjects\FileUploadTrait; |
25
|
|
|
use TechDivision\Import\Subjects\ExportableSubjectInterface; |
26
|
|
|
use TechDivision\Import\Subjects\FileUploadSubjectInterface; |
27
|
|
|
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface; |
28
|
|
|
use TechDivision\Import\Subjects\CleanUpColumnsSubjectInterface; |
29
|
|
|
use TechDivision\Import\Category\Utils\PageLayoutKeys; |
30
|
|
|
use TechDivision\Import\Category\Utils\DisplayModeKeys; |
31
|
|
|
use TechDivision\Import\Category\Utils\ConfigurationKeys; |
32
|
|
|
use TechDivision\Import\Category\Utils\MemberNames; |
33
|
|
|
use TechDivision\Import\Category\Utils\RegistryKeys; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The subject implementation that handles the business logic to persist products. |
37
|
|
|
* |
38
|
|
|
* @author Tim Wagner <[email protected]> |
39
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
40
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
41
|
|
|
* @link https://github.com/techdivision/import-category |
42
|
|
|
* @link http://www.techdivision.com |
43
|
|
|
*/ |
44
|
|
|
class BunchSubject extends AbstractCategorySubject implements ExportableSubjectInterface, FileUploadSubjectInterface, UrlKeyAwareSubjectInterface, CleanUpColumnsSubjectInterface |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The trait that implements the export functionality. |
49
|
|
|
* |
50
|
|
|
* @var \TechDivision\Import\Subjects\ExportableTrait |
51
|
|
|
*/ |
52
|
|
|
use ExportableTrait; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The trait that provides file upload functionality. |
56
|
|
|
* |
57
|
|
|
* @var \TechDivision\Import\Subjects\FileUploadTrait |
58
|
|
|
*/ |
59
|
|
|
use FileUploadTrait; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* The array with the available display mode keys. |
63
|
|
|
* |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
protected $availableDisplayModes = array( |
67
|
|
|
'Products only' => DisplayModeKeys::DISPLAY_MODE_PRODUCTS_ONLY, |
68
|
|
|
'Static block only' => DisplayModeKeys::DISPLAY_MODE_STATIC_BLOCK_ONLY, |
69
|
|
|
'Static block and products' => DisplayModeKeys::DISPLAY_MODE_BOTH |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The array with the available page layout keys. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $availablePageLayouts = array( |
78
|
|
|
'1 column' => PageLayoutKeys::PAGE_LAYOUT_1_COLUMN, |
79
|
|
|
'2 columns with left bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_LEFT, |
80
|
|
|
'2 columns with right bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_RIGHT, |
81
|
|
|
'3 columns' => PageLayoutKeys::PAGE_LAYOUT_3_COLUMNS, |
82
|
|
|
'Empty' => PageLayoutKeys::PAGE_LAYOUT_EMPTY |
83
|
|
|
); |
84
|
|
|
/** |
85
|
|
|
* The default callback mappings for the Magento standard category attributes. |
86
|
|
|
* |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $defaultCallbackMappings = array( |
90
|
|
|
'display_mode' => array('import_category.callback.display.mode'), |
91
|
|
|
'page_layout' => array('import_category.callback.page.layout'), |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The available entity types. |
96
|
|
|
* |
97
|
|
|
* @var array |
98
|
|
|
*/ |
99
|
|
|
protected $entityTypes = array(); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Intializes the previously loaded global data for exactly one bunch. |
103
|
|
|
* |
104
|
|
|
* @param string $serial The serial of the actual import |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setUp($serial) |
109
|
|
|
{ |
110
|
|
|
|
111
|
|
|
// load the status of the actual import |
112
|
|
|
$status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
113
|
|
|
|
114
|
|
|
// load the global data we've prepared initially |
115
|
|
|
$this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES]; |
116
|
|
|
|
117
|
|
|
// initialize the flag whether to copy images or not |
118
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) { |
119
|
|
|
$this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// initialize the flag whether to override images or not |
123
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::OVERRIDE_IMAGES)) { |
124
|
|
|
$this->setOverrideImages($this->getConfiguration()->getParam(ConfigurationKeys::OVERRIDE_IMAGES)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// initialize media directory => can be absolute or relative |
128
|
|
View Code Duplication |
if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
|
|
|
|
129
|
|
|
try { |
130
|
|
|
$this->setMediaDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY))); |
131
|
|
|
} catch (\InvalidArgumentException $iae) { |
132
|
|
|
$this->getSystemLogger()->debug($iae->getMessage()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// initialize images directory => can be absolute or relative |
137
|
|
View Code Duplication |
if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
|
|
|
|
138
|
|
|
try { |
139
|
|
|
$this->setImagesFileDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY))); |
140
|
|
|
} catch (\InvalidArgumentException $iae) { |
141
|
|
|
$this->getSystemLogger()->debug($iae->getMessage()); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// prepare the callbacks |
146
|
|
|
parent::setUp($serial); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return's the default callback mappings. |
151
|
|
|
* |
152
|
|
|
* @return array The default callback mappings |
153
|
|
|
*/ |
154
|
|
|
public function getDefaultCallbackMappings() |
155
|
|
|
{ |
156
|
|
|
return $this->defaultCallbackMappings; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return's the display mode for the passed display mode string. |
161
|
|
|
* |
162
|
|
|
* @param string $displayMode The display mode string to return the key for |
163
|
|
|
* |
164
|
|
|
* @return integer The requested display mode |
165
|
|
|
* @throws \Exception Is thrown, if the requested display mode is not available |
166
|
|
|
*/ |
167
|
|
|
public function getDisplayModeByValue($displayMode) |
168
|
|
|
{ |
169
|
|
|
|
170
|
|
|
// query whether or not, the requested display mode is available |
171
|
|
|
if (isset($this->availableDisplayModes[$displayMode])) { |
172
|
|
|
return $this->availableDisplayModes[$displayMode]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// throw an exception, if not |
176
|
|
|
throw new \Exception( |
177
|
|
|
$this->appendExceptionSuffix( |
178
|
|
|
sprintf('Found invalid display mode %s', $displayMode) |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return's the page layout for the passed page layout string. |
185
|
|
|
* |
186
|
|
|
* @param string $pageLayout The page layout string to return the key for |
187
|
|
|
* |
188
|
|
|
* @return integer The requested page layout |
189
|
|
|
* @throws \Exception Is thrown, if the requested page layout is not available |
190
|
|
|
*/ |
191
|
|
|
public function getPageLayoutByValue($pageLayout) |
192
|
|
|
{ |
193
|
|
|
|
194
|
|
|
// query whether or not, the requested display mode is available |
195
|
|
|
if (isset($this->availablePageLayouts[$pageLayout])) { |
196
|
|
|
return $this->availablePageLayouts[$pageLayout]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
// throw an exception, if not |
200
|
|
|
throw new \Exception( |
201
|
|
|
$this->appendExceptionSuffix( |
202
|
|
|
sprintf('Found invalid page layout %s', $pageLayout) |
203
|
|
|
) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return's the available store view codes of the available stores. |
209
|
|
|
* |
210
|
|
|
* @return array The array with the available store view codes |
211
|
|
|
*/ |
212
|
|
|
public function getStoreViewCodes() |
213
|
|
|
{ |
214
|
|
|
return array_keys($this->stores); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Returns the store view codes relevant to the category represented by the current row. |
219
|
|
|
* |
220
|
|
|
* @param string $path The path to return the root category's store view codes for |
221
|
|
|
* |
222
|
|
|
* @return array The store view codes for the given root category |
223
|
|
|
* @throws \Exception Is thrown, if the root category of the passed path is NOT available |
224
|
|
|
*/ |
225
|
|
|
public function getRootCategoryStoreViewCodes($path) |
226
|
|
|
{ |
227
|
|
|
|
228
|
|
|
// explode the path of the root category |
229
|
|
|
list ($rootCategoryPath, ) = explode('/', $path); |
230
|
|
|
|
231
|
|
|
// query whether or not a root category with the given path exists |
232
|
|
|
if ($rootCategory = $this->getCategoryByPath($rootCategoryPath)) { |
233
|
|
|
// initialize the array with the store view codes |
234
|
|
|
$storeViewCodes = array(); |
235
|
|
|
|
236
|
|
|
// try to assemble the store view codes by iterating over the available root categories |
237
|
|
|
foreach ($this->rootCategories as $storeViewCode => $category) { |
238
|
|
|
// query whether or not the entity ID of the root category matches |
239
|
|
|
if ((integer) $category[$this->getPrimaryKeyMemberName()] === (integer) $rootCategory[$this->getPrimaryKeyMemberName()]) { |
240
|
|
|
$storeViewCodes[] = $storeViewCode; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
// return the array with the store view codes |
245
|
|
|
return $storeViewCodes; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// throw an exception, if the root category is NOT available |
249
|
|
|
throw new \Exception( |
250
|
|
|
$this->appendExceptionSuffix( |
251
|
|
|
sprintf('Can\'t load root category "%s" for path "%s"', $rootCategoryPath, $path) |
252
|
|
|
) |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Return's the PK column name to create the product => attribute relation. |
258
|
|
|
* |
259
|
|
|
* @return string The PK column name |
260
|
|
|
*/ |
261
|
|
|
protected function getPrimaryKeyMemberName() |
262
|
|
|
{ |
263
|
|
|
return MemberNames::ENTITY_ID; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Return's the entity type for the configured entity type code. |
268
|
|
|
* |
269
|
|
|
* @return array The requested entity type |
270
|
|
|
* @throws \Exception Is thrown, if the requested entity type is not available |
271
|
|
|
*/ |
272
|
|
View Code Duplication |
public function getEntityType() |
|
|
|
|
273
|
|
|
{ |
274
|
|
|
|
275
|
|
|
// query whether or not the entity type with the passed code is available |
276
|
|
|
if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) { |
277
|
|
|
return $this->entityTypes[$entityTypeCode]; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
// throw a new exception |
281
|
|
|
throw new \Exception( |
282
|
|
|
$this->appendExceptionSuffix( |
283
|
|
|
sprintf('Requested entity type "%s" is not available', $entityTypeCode) |
284
|
|
|
) |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Merge the columns from the configuration with all image type columns to define which |
290
|
|
|
* columns should be cleaned-up. |
291
|
|
|
* |
292
|
|
|
* @return array The columns that has to be cleaned-up |
293
|
|
|
*/ |
294
|
|
|
public function getCleanUpColumns() |
295
|
|
|
{ |
296
|
|
|
|
297
|
|
|
// initialize the array for the columns that has to be cleaned-up |
298
|
|
|
$cleanUpColumns = array(); |
299
|
|
|
|
300
|
|
|
// query whether or not an array has been specified in the configuration |
301
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) { |
302
|
|
|
$cleanUpColumns = $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// return the array with the column names |
306
|
|
|
return $cleanUpColumns; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|