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\Category\Utils\PageLayoutKeys; |
29
|
|
|
use TechDivision\Import\Category\Utils\DisplayModeKeys; |
30
|
|
|
use TechDivision\Import\Category\Utils\ConfigurationKeys; |
31
|
|
|
use TechDivision\Import\Category\Utils\MemberNames; |
32
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
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 |
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 media directory => can be absolute or relative |
123
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) { |
124
|
|
|
$this->setMediaDir( |
125
|
|
|
$this->resolvePath( |
126
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY) |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// initialize images directory => can be absolute or relative |
132
|
|
|
if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) { |
133
|
|
|
$this->setImagesFileDir( |
134
|
|
|
$this->resolvePath( |
135
|
|
|
$this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY) |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// prepare the callbacks |
141
|
|
|
parent::setUp($serial); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return's the default callback mappings. |
146
|
|
|
* |
147
|
|
|
* @return array The default callback mappings |
148
|
|
|
*/ |
149
|
|
|
public function getDefaultCallbackMappings() |
150
|
|
|
{ |
151
|
|
|
return $this->defaultCallbackMappings; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return's the display mode for the passed display mode string. |
156
|
|
|
* |
157
|
|
|
* @param string $displayMode The display mode string to return the key for |
158
|
|
|
* |
159
|
|
|
* @return integer The requested display mode |
160
|
|
|
* @throws \Exception Is thrown, if the requested display mode is not available |
161
|
|
|
*/ |
162
|
|
|
public function getDisplayModeByValue($displayMode) |
163
|
|
|
{ |
164
|
|
|
|
165
|
|
|
// query whether or not, the requested display mode is available |
166
|
|
|
if (isset($this->availableDisplayModes[$displayMode])) { |
167
|
|
|
return $this->availableDisplayModes[$displayMode]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// throw an exception, if not |
171
|
|
|
throw new \Exception( |
172
|
|
|
$this->appendExceptionSuffix( |
173
|
|
|
sprintf('Found invalid display mode %s', $displayMode) |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return's the page layout for the passed page layout string. |
180
|
|
|
* |
181
|
|
|
* @param string $pageLayout The page layout string to return the key for |
182
|
|
|
* |
183
|
|
|
* @return integer The requested page layout |
184
|
|
|
* @throws \Exception Is thrown, if the requested page layout is not available |
185
|
|
|
*/ |
186
|
|
|
public function getPageLayoutByValue($pageLayout) |
187
|
|
|
{ |
188
|
|
|
|
189
|
|
|
// query whether or not, the requested display mode is available |
190
|
|
|
if (isset($this->availablePageLayouts[$pageLayout])) { |
191
|
|
|
return $this->availablePageLayouts[$pageLayout]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// throw an exception, if not |
195
|
|
|
throw new \Exception( |
196
|
|
|
$this->appendExceptionSuffix( |
197
|
|
|
sprintf('Found invalid page layout %s', $pageLayout) |
198
|
|
|
) |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return's the available store view codes of the available stores. |
204
|
|
|
* |
205
|
|
|
* @return array The array with the available store view codes |
206
|
|
|
*/ |
207
|
|
|
public function getStoreViewCodes() |
208
|
|
|
{ |
209
|
|
|
return array_keys($this->stores); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns the store view codes relevant to the category represented by the current row. |
214
|
|
|
* |
215
|
|
|
* @param string $path The path to return the root category's store view codes for |
216
|
|
|
* |
217
|
|
|
* @return array The store view codes for the given root category |
218
|
|
|
* @throws \Exception Is thrown, if the root category of the passed path is NOT available |
219
|
|
|
*/ |
220
|
|
|
public function getRootCategoryStoreViewCodes($path) |
221
|
|
|
{ |
222
|
|
|
|
223
|
|
|
// explode the path of the root category |
224
|
|
|
list ($rootCategoryPath, ) = explode('/', $path); |
225
|
|
|
|
226
|
|
|
// query whether or not a root category with the given path exists |
227
|
|
|
if ($rootCategory = $this->getCategoryByPath($rootCategoryPath)) { |
228
|
|
|
// initialize the array with the store view codes |
229
|
|
|
$storeViewCodes = array(); |
230
|
|
|
|
231
|
|
|
// try to assemble the store view codes by iterating over the available root categories |
232
|
|
|
foreach ($this->rootCategories as $storeViewCode => $category) { |
233
|
|
|
// query whether or not the entity ID of the root category matches |
234
|
|
|
if ((integer) $category[$this->getPrimaryKeyMemberName()] === (integer) $rootCategory[$this->getPrimaryKeyMemberName()]) { |
235
|
|
|
$storeViewCodes[] = $storeViewCode; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// return the array with the store view codes |
240
|
|
|
return $storeViewCodes; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// throw an exception, if the root category is NOT available |
244
|
|
|
throw new \Exception( |
245
|
|
|
$this->appendExceptionSuffix( |
246
|
|
|
sprintf('Can\'t load root category "%s" for path "%s"', $rootCategoryPath, $path) |
247
|
|
|
) |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Return's the PK column name to create the product => attribute relation. |
253
|
|
|
* |
254
|
|
|
* @return string The PK column name |
255
|
|
|
*/ |
256
|
|
|
protected function getPrimaryKeyMemberName() |
257
|
|
|
{ |
258
|
|
|
return MemberNames::ENTITY_ID; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return's the entity type for the configured entity type code. |
263
|
|
|
* |
264
|
|
|
* @return array The requested entity type |
265
|
|
|
* @throws \Exception Is thrown, if the requested entity type is not available |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public function getEntityType() |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
|
270
|
|
|
// query whether or not the entity type with the passed code is available |
271
|
|
|
if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) { |
272
|
|
|
return $this->entityTypes[$entityTypeCode]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// throw a new exception |
276
|
|
|
throw new \Exception( |
277
|
|
|
$this->appendExceptionSuffix( |
278
|
|
|
sprintf('Requested entity type "%s" is not available', $entityTypeCode) |
279
|
|
|
) |
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return's TRUE, if the passed URL key varchar value IS related with the actual PK. |
285
|
|
|
* |
286
|
|
|
* @param array $categoryVarcharAttribute The varchar value to check |
287
|
|
|
* |
288
|
|
|
* @return boolean TRUE if the URL key is related, else FALSE |
289
|
|
|
*/ |
290
|
|
|
public function isUrlKeyOf(array $categoryVarcharAttribute) |
291
|
|
|
{ |
292
|
|
|
return ((integer) $categoryVarcharAttribute[MemberNames::ENTITY_ID] === (integer) $this->getLastEntityId()) && |
293
|
|
|
((integer) $categoryVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN)); |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: