99
1010/**
1111 * Class Save
12- *
12+ *
1313 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1414 */
1515class Save extends \Magento \Catalog \Controller \Adminhtml \Category
@@ -34,15 +34,13 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
3434 * @var array
3535 */
3636 protected $ stringToBoolInputs = [
37- 'general ' => [
38- 'custom_use_parent_settings ' ,
39- 'custom_apply_to_products ' ,
40- 'is_active ' ,
41- 'include_in_menu ' ,
42- 'is_anchor ' ,
43- 'use_default ' => ['url_key ' ],
44- 'use_config ' => ['available_sort_by ' , 'filter_price_range ' , 'default_sort_by ' ]
45- ]
37+ 'custom_use_parent_settings ' ,
38+ 'custom_apply_to_products ' ,
39+ 'is_active ' ,
40+ 'include_in_menu ' ,
41+ 'is_anchor ' ,
42+ 'use_default ' => ['url_key ' ],
43+ 'use_config ' => ['available_sort_by ' , 'filter_price_range ' , 'default_sort_by ' ]
4644 ];
4745
4846 /**
@@ -110,15 +108,18 @@ public function execute()
110108 }
111109
112110 $ data ['general ' ] = $ this ->getRequest ()->getPostValue ();
113- $ isNewCategory = !isset ($ data ['general ' ]['entity_id ' ]);
114- $ data = $ this ->stringToBoolConverting ($ data );
115- $ data = $ this ->imagePreprocessing ($ data );
116- $ storeId = isset ($ data ['general ' ]['store_id ' ]) ? $ data ['general ' ]['store_id ' ] : null ;
111+ $ categoryPostData = $ data ['general ' ];
112+
113+ $ isNewCategory = !isset ($ categoryPostData ['entity_id ' ]);
114+ $ categoryPostData = $ this ->stringToBoolConverting ($ categoryPostData );
115+ $ categoryPostData = $ this ->imagePreprocessing ($ categoryPostData );
116+ $ categoryPostData = $ this ->dateTimePreprocessing ($ category , $ categoryPostData );
117+ $ storeId = isset ($ categoryPostData ['store_id ' ]) ? $ categoryPostData ['store_id ' ] : null ;
117118 $ store = $ this ->storeManager ->getStore ($ storeId );
118119 $ this ->storeManager ->setCurrentStore ($ store ->getCode ());
119- $ parentId = isset ($ data [ ' general ' ][ ' parent ' ]) ? $ data [ ' general ' ] ['parent ' ] : null ;
120- if ($ data [ ' general ' ] ) {
121- $ category ->addData ($ this ->_filterCategoryPostData ($ data [ ' general ' ] ));
120+ $ parentId = isset ($ categoryPostData [ ' parent ' ]) ? $ categoryPostData ['parent ' ] : null ;
121+ if ($ categoryPostData ) {
122+ $ category ->addData ($ this ->_filterCategoryPostData ($ categoryPostData ));
122123 if ($ isNewCategory ) {
123124 $ parentCategory = $ this ->getParentCategory ($ parentId , $ storeId );
124125 $ category ->setPath ($ parentCategory ->getPath ());
@@ -128,10 +129,10 @@ public function execute()
128129 /**
129130 * Process "Use Config Settings" checkboxes
130131 */
131- $ generalPost = $ data [ ' general ' ];
132+
132133 $ useConfig = [];
133- if (isset ($ generalPost ['use_config ' ]) && !empty ($ generalPost ['use_config ' ])) {
134- foreach ($ generalPost ['use_config ' ] as $ attributeCode => $ attributeValue ) {
134+ if (isset ($ categoryPostData ['use_config ' ]) && !empty ($ categoryPostData ['use_config ' ])) {
135+ foreach ($ categoryPostData ['use_config ' ] as $ attributeCode => $ attributeValue ) {
135136 if ($ attributeValue ) {
136137 $ useConfig [] = $ attributeCode ;
137138 $ category ->setData ($ attributeCode , null );
@@ -141,11 +142,11 @@ public function execute()
141142
142143 $ category ->setAttributeSetId ($ category ->getDefaultAttributeSetId ());
143144
144- if (isset ($ data [ ' general ' ] ['category_products ' ])
145- && is_string ($ data [ ' general ' ] ['category_products ' ])
145+ if (isset ($ categoryPostData ['category_products ' ])
146+ && is_string ($ categoryPostData ['category_products ' ])
146147 && !$ category ->getProductsReadonly ()
147148 ) {
148- $ products = json_decode ($ data [ ' general ' ] ['category_products ' ], true );
149+ $ products = json_decode ($ categoryPostData ['category_products ' ], true );
149150 $ category ->setPostedProducts ($ products );
150151 }
151152 $ this ->_eventManager ->dispatch (
@@ -156,8 +157,8 @@ public function execute()
156157 /**
157158 * Check "Use Default Value" checkboxes values
158159 */
159- if (isset ($ generalPost ['use_default ' ]) && !empty ($ generalPost ['use_default ' ])) {
160- foreach ($ generalPost ['use_default ' ] as $ attributeCode => $ attributeValue ) {
160+ if (isset ($ categoryPostData ['use_default ' ]) && !empty ($ categoryPostData ['use_default ' ])) {
161+ foreach ($ categoryPostData ['use_default ' ] as $ attributeCode => $ attributeValue ) {
161162 if ($ attributeValue ) {
162163 $ category ->setData ($ attributeCode , null );
163164 }
@@ -197,15 +198,15 @@ public function execute()
197198 } catch (\Magento \Framework \Exception \AlreadyExistsException $ e ) {
198199 $ this ->messageManager ->addError ($ e ->getMessage ());
199200 $ this ->_objectManager ->get (\Psr \Log \LoggerInterface::class)->critical ($ e );
200- $ this ->_getSession ()->setCategoryData ($ data );
201+ $ this ->_getSession ()->setCategoryData ($ categoryPostData );
201202 } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
202203 $ this ->messageManager ->addError ($ e ->getMessage ());
203204 $ this ->_objectManager ->get (\Psr \Log \LoggerInterface::class)->critical ($ e );
204- $ this ->_getSession ()->setCategoryData ($ data );
205+ $ this ->_getSession ()->setCategoryData ($ categoryPostData );
205206 } catch (\Exception $ e ) {
206207 $ this ->messageManager ->addError (__ ('Something went wrong while saving the category. ' ));
207208 $ this ->_objectManager ->get (\Psr \Log \LoggerInterface::class)->critical ($ e );
208- $ this ->_getSession ()->setCategoryData ($ data );
209+ $ this ->_getSession ()->setCategoryData ($ categoryPostData );
209210 }
210211 }
211212
@@ -248,9 +249,9 @@ public function execute()
248249 */
249250 public function imagePreprocessing ($ data )
250251 {
251- if (empty ($ data ['general ' ][ ' image ' ])) {
252- unset($ data ['general ' ][ ' image ' ]);
253- $ data ['general ' ][ ' image ' ]['delete ' ] = true ;
252+ if (empty ($ data ['image ' ])) {
253+ unset($ data ['image ' ]);
254+ $ data ['image ' ]['delete ' ] = true ;
254255 }
255256 return $ data ;
256257 }
0 commit comments