1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Category\Observers\UrlKeyAndPathObserver |
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\Observers; |
22
|
|
|
|
23
|
|
|
use Zend\Filter\FilterInterface; |
24
|
|
|
use TechDivision\Import\Category\Utils\ColumnKeys; |
25
|
|
|
use TechDivision\Import\Category\Utils\MemberNames; |
26
|
|
|
use TechDivision\Import\Utils\StoreViewCodes; |
27
|
|
|
use TechDivision\Import\Utils\UrlKeyUtilInterface; |
28
|
|
|
use TechDivision\Import\Utils\Filter\UrlKeyFilterTrait; |
29
|
|
|
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface; |
30
|
|
|
use TechDivision\Import\Category\Services\CategoryBunchProcessorInterface; |
31
|
|
|
use TechDivision\Import\Category\Utils\ConfigurationKeys; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Observer that extracts the URL key/path from the category path |
35
|
|
|
* and adds them as two new columns with the their values. |
36
|
|
|
* |
37
|
|
|
* @author Tim Wagner <[email protected]> |
38
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
39
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
40
|
|
|
* @link https://github.com/techdivision/import-category |
41
|
|
|
* @link http://www.techdivision.com |
42
|
|
|
*/ |
43
|
|
|
class UrlKeyAndPathObserver extends AbstractCategoryImportObserver |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The trait that provides string => URL key conversion functionality. |
48
|
|
|
* |
49
|
|
|
* @var \TechDivision\Import\Utils\Filter\UrlKeyFilterTrait |
50
|
|
|
*/ |
51
|
|
|
use UrlKeyFilterTrait; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The URL key utility instance. |
55
|
|
|
* |
56
|
|
|
* @var \TechDivision\Import\Utils\UrlKeyUtilInterface |
57
|
|
|
*/ |
58
|
|
|
protected $urlKeyUtil; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The category bunch processor instance. |
62
|
|
|
* |
63
|
|
|
* @var \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface |
64
|
|
|
*/ |
65
|
|
|
protected $categoryBunchProcessor; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initialize the observer with the passed product bunch processor instance. |
69
|
|
|
* |
70
|
|
|
* @param \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface $categoryBunchProcessor The category bunch processor instance |
71
|
|
|
* @param \Zend\Filter\FilterInterface $convertLiteralUrlFilter The URL filter instance |
72
|
|
|
* @param \TechDivision\Import\Utils\UrlKeyUtilInterface $urlKeyUtil The URL key utility instance |
73
|
|
|
*/ |
74
|
|
|
public function __construct( |
75
|
|
|
CategoryBunchProcessorInterface $categoryBunchProcessor, |
76
|
|
|
FilterInterface $convertLiteralUrlFilter, |
77
|
|
|
UrlKeyUtilInterface $urlKeyUtil |
78
|
|
|
) { |
79
|
|
|
|
80
|
|
|
// set the processor and the URL filter instance |
81
|
|
|
$this->categoryBunchProcessor = $categoryBunchProcessor; |
82
|
|
|
$this->convertLiteralUrlFilter = $convertLiteralUrlFilter; |
83
|
|
|
$this->urlKeyUtil = $urlKeyUtil; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Process the observer's business logic. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
|
|
protected function process() |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
// initialize the URL key and the category |
95
|
|
|
$urlKey = null; |
96
|
|
|
$category = array(); |
97
|
|
|
|
98
|
|
|
// prepare the store view code |
99
|
|
|
$this->prepareStoreViewCode(); |
100
|
|
|
|
101
|
|
|
// set the entity ID for the category with the passed path |
102
|
|
|
try { |
103
|
|
|
$this->setIds($category = $this->getCategoryByPath($path = $this->getValue(ColumnKeys::PATH))); |
104
|
|
|
} catch (\Exception $e) { |
105
|
|
|
$this->setIds(array()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// query whether or not the URL key column has a value |
109
|
|
|
if ($this->hasValue(ColumnKeys::URL_KEY)) { |
110
|
|
|
$urlKey = $this->getValue(ColumnKeys::URL_KEY); |
111
|
|
|
} else { |
112
|
|
|
// query whether or not the existing category `url_key` should be re-created from the category name |
113
|
|
|
if ($category && !$this->getSubject()->getConfiguration()->getParam(ConfigurationKeys::UPDATE_URL_KEY_FROM_NAME, true)) { |
|
|
|
|
114
|
|
|
// if the category already exists and NO re-creation from the category name has to |
115
|
|
|
// be done, load the original `url_key`from the category and use that to proceed |
116
|
|
|
$urlKey = $this->loadUrlKey($this->getSubject(), $this->getPrimaryKey()); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// try to load the value from column `name` if URL key is still |
120
|
|
|
// empty, because we need it to process the the rewrites later on |
121
|
|
|
if ($urlKey === null || $urlKey === '' && $this->hasValue(ColumnKeys::NAME)) { |
122
|
|
|
$urlKey = $this->convertNameToUrlKey($this->getValue(ColumnKeys::NAME)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// stop processing, if no URL key is available |
127
|
|
|
if ($urlKey === null || $urlKey === '') { |
128
|
|
|
// throw an exception, that the URL key can not be |
129
|
|
|
// initialized and we're in the default store view |
130
|
|
|
if ($this->getStoreViewCode(StoreViewCodes::ADMIN) === StoreViewCodes::ADMIN) { |
131
|
|
|
throw new \Exception(sprintf('Can\'t initialize the URL key for category "%s" because columns "url_key" or "name" have a value set for default store view', $path)); |
132
|
|
|
} |
133
|
|
|
// stop processing, because we're in a store |
134
|
|
|
// view row and a URL key is not mandatory |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// load ID of the actual store view |
139
|
|
|
$storeId = $this->getRowStoreId(StoreViewCodes::ADMIN); |
140
|
|
|
|
141
|
|
|
// explode the path into the category names |
142
|
|
|
if ($categories = $this->explode($this->getValue(ColumnKeys::PATH), '/')) { |
143
|
|
|
// initialize the array for the category paths |
144
|
|
|
$categoryPaths = array(); |
145
|
|
|
// iterate over the parent category names and try |
146
|
|
|
// to load the categories to build the URL path |
147
|
|
|
for ($i = sizeof($categories) - 1; $i > 1; $i--) { |
148
|
|
|
try { |
149
|
|
|
// prepare the expected category name |
150
|
|
|
$categoryPath = implode('/', array_slice($categories, 0, $i)); |
151
|
|
|
// load the existing category and prepend the URL key the array with the category URL keys |
152
|
|
|
$existingCategory = $this->getCategoryByPkAndStoreId($this->mapPath($categoryPath), $storeId); |
153
|
|
|
// query whether or not an URL key is available or not |
154
|
|
|
if (isset($existingCategory[MemberNames::URL_KEY])) { |
155
|
|
|
array_unshift($categoryPaths, $existingCategory[MemberNames::URL_KEY]); |
156
|
|
|
} else { |
157
|
|
|
$this->getSystemLogger()->debug(sprintf('Can\'t find URL key for category "%s"', $categoryPath)); |
158
|
|
|
} |
159
|
|
|
} catch (\Exception $e) { |
160
|
|
|
$this->getSystemLogger()->debug(sprintf('Can\'t load parent category "%s"', $categoryPath)); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// update the URL key with the unique value |
166
|
|
|
$this->setValue( |
167
|
|
|
ColumnKeys::URL_KEY, |
168
|
|
|
$urlKey = $this->makeUnique($this->getSubject(), $urlKey, array(implode('/', $categoryPaths))) |
|
|
|
|
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
// finally, append the URL key as last element to the path |
172
|
|
|
array_push($categoryPaths, $urlKey); |
173
|
|
|
|
174
|
|
|
// create the virtual column for the URL path |
175
|
|
|
if ($this->hasHeader(ColumnKeys::URL_PATH) === false) { |
176
|
|
|
$this->addHeader(ColumnKeys::URL_PATH); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// set the URL path |
180
|
|
|
$this->setValue(ColumnKeys::URL_PATH, implode('/', $categoryPaths)); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Return the primary key member name. |
185
|
|
|
* |
186
|
|
|
* @return string The primary key member name |
187
|
|
|
*/ |
188
|
|
|
protected function getPkMemberName() |
189
|
|
|
{ |
190
|
|
|
return MemberNames::ENTITY_ID; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the category bunch processor instance. |
195
|
|
|
* |
196
|
|
|
* @return \TechDivision\Import\Category\Services\CategoryBunchProcessorInterface The category bunch processor instance |
197
|
|
|
*/ |
198
|
|
|
protected function getCategoryBunchProcessor() |
199
|
|
|
{ |
200
|
|
|
return $this->categoryBunchProcessor; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the URL key utility instance. |
205
|
|
|
* |
206
|
|
|
* @return \TechDivision\Import\Utils\UrlKeyUtilInterface The URL key utility instance |
207
|
|
|
*/ |
208
|
|
|
protected function getUrlKeyUtil() |
209
|
|
|
{ |
210
|
|
|
return $this->urlKeyUtil; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Return's the category with the passed path. |
215
|
|
|
* |
216
|
|
|
* @param string $path The path of the category to return |
217
|
|
|
* |
218
|
|
|
* @return array The category |
219
|
|
|
* @throws \Exception Is thrown, if the requested category is not available |
220
|
|
|
*/ |
221
|
|
|
protected function getCategoryByPath($path) |
222
|
|
|
{ |
223
|
|
|
return $this->getSubject()->getCategoryByPath($path); |
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the category with the passed primary key and the attribute values for the passed store ID. |
228
|
|
|
* |
229
|
|
|
* @param string $pk The primary key of the category to return |
230
|
|
|
* @param integer $storeId The store ID of the category values |
231
|
|
|
* |
232
|
|
|
* @return array|null The category data |
233
|
|
|
*/ |
234
|
|
|
protected function getCategoryByPkAndStoreId($pk, $storeId) |
235
|
|
|
{ |
236
|
|
|
return $this->getCategoryBunchProcessor()->getCategoryByPkAndStoreId($pk, $storeId); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Temporarily persist's the IDs of the passed category. |
241
|
|
|
* |
242
|
|
|
* @param array $category The category to temporarily persist the IDs for |
243
|
|
|
* |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
|
|
protected function setIds(array $category) |
247
|
|
|
{ |
248
|
|
|
$this->setLastEntityId(isset($category[MemberNames::ENTITY_ID]) ? $category[MemberNames::ENTITY_ID] : null); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Set's the ID of the category that has been created recently. |
253
|
|
|
* |
254
|
|
|
* @param string $lastEntityId The entity ID |
255
|
|
|
* |
256
|
|
|
* @return void |
257
|
|
|
*/ |
258
|
|
|
protected function setLastEntityId($lastEntityId) |
259
|
|
|
{ |
260
|
|
|
$this->getSubject()->setLastEntityId($lastEntityId); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Return's the PK to of the product. |
265
|
|
|
* |
266
|
|
|
* @return integer The PK to create the relation with |
267
|
|
|
*/ |
268
|
|
|
protected function getPrimaryKey() |
269
|
|
|
{ |
270
|
|
|
$this->getSubject()->getLastEntityId(); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Load's and return's the url_key with the passed primary ID. |
275
|
|
|
* |
276
|
|
|
* @param \TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface $subject The subject to load the URL key |
277
|
|
|
* @param int $primaryKeyId The ID from category |
278
|
|
|
* |
279
|
|
|
* @return string|null url_key or null |
280
|
|
|
*/ |
281
|
|
|
protected function loadUrlKey(UrlKeyAwareSubjectInterface $subject, $primaryKeyId) |
282
|
|
|
{ |
283
|
|
|
return $this->getUrlKeyUtil()->loadUrlKey($subject, $primaryKeyId); |
|
|
|
|
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Make's the passed URL key unique by adding the next number to the end. |
288
|
|
|
* |
289
|
|
|
* @param \TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface $subject The subject to make the URL key unique for |
290
|
|
|
* @param string $urlKey The URL key to make unique |
291
|
|
|
* @param array $urlPaths The URL paths to make unique |
292
|
|
|
* |
293
|
|
|
* @return string The unique URL key |
294
|
|
|
*/ |
295
|
|
|
protected function makeUnique(UrlKeyAwareSubjectInterface $subject, string $urlKey, array $urlPaths = array()) |
296
|
|
|
{ |
297
|
|
|
return $this->getUrlKeyUtil()->makeUnique($subject, $urlKey, $urlPaths); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.