-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Category.php
219 lines (200 loc) · 7.48 KB
/
Category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Catalog\Controller\Adminhtml;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\Store;
use Magento\Framework\Controller\ResultFactory;
/**
* Catalog category controller
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
abstract class Category extends \Magento\Backend\App\Action
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = 'Magento_Catalog::categories';
/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
*/
protected $dateFilter;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* @var \Magento\Cms\Model\Wysiwyg\Config
*/
private $wysiwigConfig;
/**
* @var \Magento\Backend\Model\Auth\Session
*/
private $authSession;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Registry $registry
* @param \Magento\Cms\Model\Wysiwyg\Config $wysiwigConfig
* @param \Magento\Backend\Model\Auth\Session $authSession
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null,
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
\Magento\Framework\Registry $registry = null,
\Magento\Cms\Model\Wysiwyg\Config $wysiwigConfig = null,
\Magento\Backend\Model\Auth\Session $authSession = null
) {
$this->dateFilter = $dateFilter;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
\Magento\Store\Model\StoreManagerInterface::class
);
$this->registry = $registry ?: ObjectManager::getInstance()->get(
\Magento\Framework\Registry::class
);
$this->wysiwigConfig = $wysiwigConfig ?: ObjectManager::getInstance()->get(
\Magento\Cms\Model\Wysiwyg\Config::class
);
$this->authSession = $authSession ?: ObjectManager::getInstance()->get(
\Magento\Backend\Model\Auth\Session::class
);
parent::__construct($context);
}
/**
* Initialize requested category and put it into registry.
*
* Root category can be returned, if inappropriate store/category is specified
*
* @param bool $getRootInstead
* @return \Magento\Catalog\Model\Category|false
*/
protected function _initCategory($getRootInstead = false)
{
$categoryId = $this->resolveCategoryId();
$storeId = $this->resolveStoreId();
$category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
$category->setStoreId($storeId);
if ($categoryId) {
$category->load($categoryId);
if ($storeId) {
$rootId = $this->storeManager->getStore($storeId)->getRootCategoryId();
if (!in_array($rootId, $category->getPathIds())) {
// load root category instead wrong one
if ($getRootInstead) {
$category->load($rootId);
} else {
return false;
}
}
}
}
$this->registry->unregister('category');
$this->registry->unregister('current_category');
$this->registry->register('category', $category);
$this->registry->register('current_category', $category);
$this->wysiwigConfig->setStoreId($storeId);
return $category;
}
/**
* Resolve Category Id (from get or from post)
*
* @return int
*/
private function resolveCategoryId() : int
{
$categoryId = (int)$this->getRequest()->getParam('id', false);
return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
}
/**
* Resolve store Id, tries to take store id from store HTTP parameter
*
* @see Store
*
* @return int
*/
private function resolveStoreId() : int
{
$storeId = (int)$this->getRequest()->getParam('store', false);
return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
}
/**
* Build response for ajax request
*
* @param \Magento\Catalog\Model\Category $category
* @param \Magento\Backend\Model\View\Result\Page $resultPage
*
* @return \Magento\Framework\Controller\Result\Json
*
* @deprecated 101.0.0
*/
protected function ajaxRequestResponse($category, $resultPage)
{
// prepare breadcrumbs of selected category, if any
$breadcrumbsPath = $category->getPath();
if (empty($breadcrumbsPath)) {
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
$breadcrumbsPath = $this->authSession->getDeletedPath(true);
if (!empty($breadcrumbsPath)) {
$breadcrumbsPath = explode('/', $breadcrumbsPath);
// no need to get parent breadcrumbs if deleting category level 1
if (count($breadcrumbsPath) <= 1) {
$breadcrumbsPath = '';
} else {
array_pop($breadcrumbsPath);
$breadcrumbsPath = implode('/', $breadcrumbsPath);
}
}
}
$eventResponse = new \Magento\Framework\DataObject(
[
'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
. $resultPage->getLayout()->getBlock('category.tree')
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(),
'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()
]
);
$this->_eventManager->dispatch(
'category_prepare_ajax_response',
['response' => $eventResponse, 'controller' => $this]
);
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setHeader('Content-type', 'application/json', true);
$resultJson->setData($eventResponse->getData());
return $resultJson;
}
/**
* Datetime data preprocessing
*
* @param \Magento\Catalog\Model\Category $category
* @param array $postData
*
* @return array
*/
protected function dateTimePreprocessing($category, $postData)
{
$dateFieldFilters = [];
$attributes = $category->getAttributes();
foreach ($attributes as $attrKey => $attribute) {
if ($attribute->getBackend()->getType() == 'datetime') {
if (array_key_exists($attrKey, $postData) && $postData[$attrKey] != '') {
$dateFieldFilters[$attrKey] = $this->dateFilter;
}
}
}
$inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $postData);
return $inputFilter->getUnescaped();
}
}