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