5
5
*/
6
6
namespace Magento \Ui \Controller \Adminhtml \Bookmark ;
7
7
8
- use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
9
8
use Magento \Authorization \Model \UserContextInterface ;
10
9
use Magento \Backend \App \Action \Context ;
10
+ use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \Exception \LocalizedException ;
11
13
use Magento \Framework \Json \DecoderInterface ;
14
+ use Magento \Framework \Serialize \Serializer \Json ;
12
15
use Magento \Framework \View \Element \UiComponentFactory ;
13
16
use Magento \Ui \Api \BookmarkManagementInterface ;
14
17
use Magento \Ui \Api \BookmarkRepositoryInterface ;
@@ -55,11 +58,12 @@ class Save extends AbstractAction implements HttpPostActionInterface
55
58
/**
56
59
* @var DecoderInterface
57
60
* @deprecated 101.1.0
61
+ * @see Replaced the usage of Magento\Framework\Json\DecoderInterface by Magento\Framework\Serialize\Serializer\Json
58
62
*/
59
63
protected $ jsonDecoder ;
60
64
61
65
/**
62
- * @var \Magento\Framework\Serialize\Serializer\ Json
66
+ * @var Json
63
67
*/
64
68
private $ serializer ;
65
69
@@ -71,7 +75,7 @@ class Save extends AbstractAction implements HttpPostActionInterface
71
75
* @param BookmarkInterfaceFactory $bookmarkFactory
72
76
* @param UserContextInterface $userContext
73
77
* @param DecoderInterface $jsonDecoder
74
- * @param \Magento\Framework\Serialize\Serializer\ Json|null $serializer
78
+ * @param Json|null $serializer
75
79
* @throws \RuntimeException
76
80
*/
77
81
public function __construct (
@@ -82,24 +86,24 @@ public function __construct(
82
86
BookmarkInterfaceFactory $ bookmarkFactory ,
83
87
UserContextInterface $ userContext ,
84
88
DecoderInterface $ jsonDecoder ,
85
- \ Magento \ Framework \ Serialize \ Serializer \ Json $ serializer = null
89
+ Json $ serializer = null
86
90
) {
87
91
parent ::__construct ($ context , $ factory );
88
92
$ this ->bookmarkRepository = $ bookmarkRepository ;
89
93
$ this ->bookmarkManagement = $ bookmarkManagement ;
90
94
$ this ->bookmarkFactory = $ bookmarkFactory ;
91
95
$ this ->userContext = $ userContext ;
92
96
$ this ->jsonDecoder = $ jsonDecoder ;
93
- $ this ->serializer = $ serializer ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
94
- ->get (\ Magento \ Framework \ Serialize \ Serializer \ Json::class);
97
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()
98
+ ->get (Json::class);
95
99
}
96
100
97
101
/**
98
102
* Action for AJAX request
99
103
*
100
104
* @return void
101
105
* @throws \InvalidArgumentException
102
- * @throws \LogicException
106
+ * @throws \LogicException|LocalizedException
103
107
*/
104
108
public function execute ()
105
109
{
@@ -126,6 +130,7 @@ public function execute()
126
130
$ bookmark ->getTitle (),
127
131
$ jsonData
128
132
);
133
+ $ this ->updateCurrentBookmarkConfig ($ data );
129
134
130
135
break ;
131
136
@@ -134,7 +139,7 @@ public function execute()
134
139
$ this ->updateBookmark (
135
140
$ bookmark ,
136
141
$ identifier ,
137
- isset ( $ data ['label ' ]) ? $ data [ ' label ' ] : '' ,
142
+ $ data ['label ' ] ?? '' ,
138
143
$ jsonData
139
144
);
140
145
$ this ->updateCurrentBookmark ($ identifier );
@@ -176,32 +181,31 @@ protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $tit
176
181
*
177
182
* @param string $identifier
178
183
* @return void
184
+ * @throws LocalizedException
179
185
*/
180
186
protected function updateCurrentBookmark ($ identifier )
181
187
{
182
188
$ bookmarks = $ this ->bookmarkManagement ->loadByNamespace ($ this ->_request ->getParam ('namespace ' ));
183
189
$ currentConfig = null ;
184
190
foreach ($ bookmarks ->getItems () as $ bookmark ) {
185
- if ($ bookmark ->getIdentifier () === self :: CURRENT_IDENTIFIER ) {
191
+ if ($ bookmark ->getIdentifier () == $ identifier ) {
186
192
$ current = $ bookmark ->getConfig ();
187
- $ currentConfig = $ current [self ::CURRENT_IDENTIFIER ];
188
- break ;
193
+ $ currentConfig = $ current ['views ' ][$ bookmark ->getIdentifier ()]['data ' ];
194
+ $ bookmark ->setCurrent (true );
195
+ } else {
196
+ $ bookmark ->setCurrent (false );
189
197
}
198
+ $ this ->bookmarkRepository ->save ($ bookmark );
190
199
}
191
200
192
201
foreach ($ bookmarks ->getItems () as $ bookmark ) {
193
- if ($ bookmark ->getCurrent () && $ currentConfig !== null ) {
202
+ if ($ bookmark ->getIdentifier () === self :: CURRENT_IDENTIFIER && $ currentConfig !== null ) {
194
203
$ bookmarkConfig = $ bookmark ->getConfig ();
195
- $ bookmarkConfig [' views ' ][ $ bookmark -> getIdentifier ()][ ' data ' ] = $ currentConfig ;
204
+ $ bookmarkConfig [self :: CURRENT_IDENTIFIER ] = $ currentConfig ;
196
205
$ bookmark ->setConfig ($ this ->serializer ->serialize ($ bookmarkConfig ));
206
+ $ this ->bookmarkRepository ->save ($ bookmark );
207
+ break ;
197
208
}
198
-
199
- if ($ bookmark ->getIdentifier () == $ identifier ) {
200
- $ bookmark ->setCurrent (true );
201
- } else {
202
- $ bookmark ->setCurrent (false );
203
- }
204
- $ this ->bookmarkRepository ->save ($ bookmark );
205
209
}
206
210
}
207
211
@@ -226,4 +230,33 @@ protected function checkBookmark($identifier)
226
230
227
231
return $ result ;
228
232
}
233
+
234
+ /**
235
+ * Update current bookmark config data
236
+ *
237
+ * @param array $data
238
+ * @return void
239
+ * @throws LocalizedException
240
+ */
241
+ private function updateCurrentBookmarkConfig (array $ data ): void
242
+ {
243
+ $ bookmarks = $ this ->bookmarkManagement ->loadByNamespace ($ this ->_request ->getParam ('namespace ' ));
244
+ foreach ($ bookmarks ->getItems () as $ bookmark ) {
245
+ if ($ bookmark ->getCurrent ()) {
246
+ $ bookmarkConfig = $ bookmark ->getConfig ();
247
+ $ existingConfig = $ bookmarkConfig ['views ' ][$ bookmark ->getIdentifier ()]['data ' ] ?? null ;
248
+ $ currentConfig = $ data [self ::CURRENT_IDENTIFIER ] ?? null ;
249
+ if ($ existingConfig && $ currentConfig ) {
250
+ if ($ existingConfig ['filters ' ] === $ currentConfig ['filters ' ]
251
+ && $ existingConfig ['positions ' ] !== $ currentConfig ['positions ' ]
252
+ ) {
253
+ $ bookmarkConfig ['views ' ][$ bookmark ->getIdentifier ()]['data ' ] = $ data [self ::CURRENT_IDENTIFIER ];
254
+ $ bookmark ->setConfig ($ this ->serializer ->serialize ($ bookmarkConfig ));
255
+ $ this ->bookmarkRepository ->save ($ bookmark );
256
+ }
257
+ }
258
+ break ;
259
+ }
260
+ }
261
+ }
229
262
}
0 commit comments