3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Catalog \Controller \Adminhtml ;
7
9
10
+ use Magento \Store \Model \Store ;
11
+ use Magento \Backend \App \Action \Context ;
12
+ use Magento \Framework \Stdlib \DateTime \Filter \Date ;
13
+ use Magento \Catalog \Model \Category as CategoryModel ;
14
+ use Magento \Backend \App \Action ;
15
+ use Magento \Store \Model \StoreManagerInterface ;
16
+ use Magento \Framework \Registry ;
17
+ use Magento \Cms \Model \Wysiwyg \Config ;
18
+ use Magento \Backend \Model \View \Result \Page ;
19
+ use Magento \Framework \Controller \Result \Json ;
20
+ use Magento \Backend \Model \Auth \Session ;
21
+ use Magento \Framework \DataObject ;
22
+
8
23
/**
9
24
* Catalog category controller
10
25
*/
11
- abstract class Category extends \ Magento \ Backend \ App \ Action
26
+ abstract class Category extends Action
12
27
{
13
28
/**
14
29
* Authorization level of a basic admin session
@@ -18,18 +33,16 @@ abstract class Category extends \Magento\Backend\App\Action
18
33
const ADMIN_RESOURCE = 'Magento_Catalog::categories ' ;
19
34
20
35
/**
21
- * @var \Magento\Framework\Stdlib\DateTime\Filter\ Date
36
+ * @var Date
22
37
*/
23
38
protected $ dateFilter ;
24
39
25
40
/**
26
- * @param \Magento\Backend\App\Action\ Context $context
27
- * @param \Magento\Framework\Stdlib\DateTime\Filter\ Date|null $dateFilter
41
+ * @param Context $context
42
+ * @param Date|null $dateFilter
28
43
*/
29
- public function __construct (
30
- \Magento \Backend \App \Action \Context $ context ,
31
- \Magento \Framework \Stdlib \DateTime \Filter \Date $ dateFilter = null
32
- ) {
44
+ public function __construct (Context $ context , Date $ dateFilter = null )
45
+ {
33
46
$ this ->dateFilter = $ dateFilter ;
34
47
parent ::__construct ($ context );
35
48
}
@@ -39,20 +52,20 @@ public function __construct(
39
52
* Root category can be returned, if inappropriate store/category is specified
40
53
*
41
54
* @param bool $getRootInstead
42
- * @return \Magento\Catalog\Model\Category |false
55
+ * @return CategoryModel |false
43
56
*/
44
- protected function _initCategory ($ getRootInstead = false )
57
+ protected function _initCategory (bool $ getRootInstead = false )
45
58
{
46
59
$ categoryId = $ this ->resolveCategoryId ();
47
- $ storeId = ( int ) $ this ->getRequest ()-> getParam ( ' store ' );
48
- $ category = $ this ->_objectManager ->create (\ Magento \ Catalog \ Model \Category ::class);
60
+ $ storeId = $ this ->resolveStoreId ( );
61
+ $ category = $ this ->_objectManager ->create (CategoryModel ::class);
49
62
$ category ->setStoreId ($ storeId );
50
63
51
64
if ($ categoryId ) {
52
65
$ category ->load ($ categoryId );
53
66
if ($ storeId ) {
54
67
$ rootId = $ this ->_objectManager ->get (
55
- \ Magento \ Store \ Model \ StoreManagerInterface::class
68
+ StoreManagerInterface::class
56
69
)->getStore (
57
70
$ storeId
58
71
)->getRootCategoryId ();
@@ -67,9 +80,9 @@ protected function _initCategory($getRootInstead = false)
67
80
}
68
81
}
69
82
70
- $ this ->_objectManager ->get (\ Magento \ Framework \ Registry::class)->register ('category ' , $ category );
71
- $ this ->_objectManager ->get (\ Magento \ Framework \ Registry::class)->register ('current_category ' , $ category );
72
- $ this ->_objectManager ->get (\ Magento \ Cms \ Model \ Wysiwyg \ Config::class)
83
+ $ this ->_objectManager ->get (Registry::class)->register ('category ' , $ category );
84
+ $ this ->_objectManager ->get (Registry::class)->register ('current_category ' , $ category );
85
+ $ this ->_objectManager ->get (Config::class)
73
86
->setStoreId ($ this ->getRequest ()->getParam ('store ' ));
74
87
return $ category ;
75
88
}
@@ -79,31 +92,46 @@ protected function _initCategory($getRootInstead = false)
79
92
*
80
93
* @return int
81
94
*/
82
- private function resolveCategoryId ()
95
+ private function resolveCategoryId (): int
83
96
{
84
97
$ categoryId = (int )$ this ->getRequest ()->getParam ('id ' , false );
85
98
86
99
return $ categoryId ?: (int )$ this ->getRequest ()->getParam ('entity_id ' , false );
87
100
}
88
101
102
+ /**
103
+ * Resolve store id
104
+ *
105
+ * Tries to take store id from store HTTP parameter
106
+ * @see Store
107
+ *
108
+ * @return int
109
+ */
110
+ private function resolveStoreId (): int
111
+ {
112
+ $ storeId = (int )$ this ->getRequest ()->getParam ('store ' , false );
113
+
114
+ return $ storeId ?: (int )$ this ->getRequest ()->getParam ('store_id ' , Store::DEFAULT_STORE_ID );
115
+ }
116
+
89
117
/**
90
118
* Build response for ajax request
91
119
*
92
- * @param \Magento\Catalog\Model\Category $category
93
- * @param \Magento\Backend\Model\View\Result\ Page $resultPage
120
+ * @param CategoryModel $category
121
+ * @param Page $resultPage
94
122
*
95
- * @return \Magento\Framework\Controller\Result\ Json
123
+ * @return Json
96
124
*
97
125
* @deprecated 101.0.0
98
126
*/
99
- protected function ajaxRequestResponse ($ category , $ resultPage )
127
+ protected function ajaxRequestResponse (CategoryModel $ category , Page $ resultPage ): Json
100
128
{
101
129
// prepare breadcrumbs of selected category, if any
102
130
$ breadcrumbsPath = $ category ->getPath ();
103
131
if (empty ($ breadcrumbsPath )) {
104
132
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
105
133
$ breadcrumbsPath = $ this ->_objectManager ->get (
106
- \ Magento \ Backend \ Model \ Auth \ Session::class
134
+ Session::class
107
135
)->getDeletedPath (
108
136
true
109
137
);
@@ -119,7 +147,7 @@ protected function ajaxRequestResponse($category, $resultPage)
119
147
}
120
148
}
121
149
122
- $ eventResponse = new \ Magento \ Framework \ DataObject ([
150
+ $ eventResponse = new DataObject ([
123
151
'content ' => $ resultPage ->getLayout ()->getUiComponent ('category_form ' )->getFormHtml ()
124
152
. $ resultPage ->getLayout ()->getBlock ('category.tree ' )
125
153
->getBreadcrumbsJavascript ($ breadcrumbsPath , 'editingCategoryBreadcrumbs ' ),
@@ -130,22 +158,23 @@ protected function ajaxRequestResponse($category, $resultPage)
130
158
'category_prepare_ajax_response ' ,
131
159
['response ' => $ eventResponse , 'controller ' => $ this ]
132
160
);
133
- /** @var \Magento\Framework\Controller\Result\ Json $resultJson */
134
- $ resultJson = $ this ->_objectManager ->get (\ Magento \ Framework \ Controller \ Result \ Json::class);
161
+ /** @var Json $resultJson */
162
+ $ resultJson = $ this ->_objectManager ->get (Json::class);
135
163
$ resultJson ->setHeader ('Content-type ' , 'application/json ' , true );
136
164
$ resultJson ->setData ($ eventResponse ->getData ());
165
+
137
166
return $ resultJson ;
138
167
}
139
168
140
169
/**
141
170
* Datetime data preprocessing
142
171
*
143
- * @param \Magento\Catalog\Model\Category $category
172
+ * @param CategoryModel $category
144
173
* @param array $postData
145
174
*
146
175
* @return array
147
176
*/
148
- protected function dateTimePreprocessing ($ category , $ postData )
177
+ protected function dateTimePreprocessing (CategoryModel $ category , array $ postData ): array
149
178
{
150
179
$ dateFieldFilters = [];
151
180
$ attributes = $ category ->getAttributes ();
0 commit comments