55 */
66namespace Magento \Ui \Controller \Adminhtml \Bookmark ;
77
8- use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
98use Magento \Authorization \Model \UserContextInterface ;
109use 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 ;
1113use Magento \Framework \Json \DecoderInterface ;
14+ use Magento \Framework \Serialize \Serializer \Json ;
1215use Magento \Framework \View \Element \UiComponentFactory ;
1316use Magento \Ui \Api \BookmarkManagementInterface ;
1417use Magento \Ui \Api \BookmarkRepositoryInterface ;
@@ -55,11 +58,12 @@ class Save extends AbstractAction implements HttpPostActionInterface
5558 /**
5659 * @var DecoderInterface
5760 * @deprecated 101.1.0
61+ * @see Replaced the usage of Magento\Framework\Json\DecoderInterface by Magento\Framework\Serialize\Serializer\Json
5862 */
5963 protected $ jsonDecoder ;
6064
6165 /**
62- * @var \Magento\Framework\Serialize\Serializer\ Json
66+ * @var Json
6367 */
6468 private $ serializer ;
6569
@@ -71,7 +75,7 @@ class Save extends AbstractAction implements HttpPostActionInterface
7175 * @param BookmarkInterfaceFactory $bookmarkFactory
7276 * @param UserContextInterface $userContext
7377 * @param DecoderInterface $jsonDecoder
74- * @param \Magento\Framework\Serialize\Serializer\ Json|null $serializer
78+ * @param Json|null $serializer
7579 * @throws \RuntimeException
7680 */
7781 public function __construct (
@@ -82,24 +86,24 @@ public function __construct(
8286 BookmarkInterfaceFactory $ bookmarkFactory ,
8387 UserContextInterface $ userContext ,
8488 DecoderInterface $ jsonDecoder ,
85- \ Magento \ Framework \ Serialize \ Serializer \ Json $ serializer = null
89+ Json $ serializer = null
8690 ) {
8791 parent ::__construct ($ context , $ factory );
8892 $ this ->bookmarkRepository = $ bookmarkRepository ;
8993 $ this ->bookmarkManagement = $ bookmarkManagement ;
9094 $ this ->bookmarkFactory = $ bookmarkFactory ;
9195 $ this ->userContext = $ userContext ;
9296 $ 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);
9599 }
96100
97101 /**
98102 * Action for AJAX request
99103 *
100104 * @return void
101105 * @throws \InvalidArgumentException
102- * @throws \LogicException
106+ * @throws \LogicException|LocalizedException
103107 */
104108 public function execute ()
105109 {
@@ -126,6 +130,7 @@ public function execute()
126130 $ bookmark ->getTitle (),
127131 $ jsonData
128132 );
133+ $ this ->updateCurrentBookmarkConfig ($ data );
129134
130135 break ;
131136
@@ -134,7 +139,7 @@ public function execute()
134139 $ this ->updateBookmark (
135140 $ bookmark ,
136141 $ identifier ,
137- isset ( $ data ['label ' ]) ? $ data [ ' label ' ] : '' ,
142+ $ data ['label ' ] ?? '' ,
138143 $ jsonData
139144 );
140145 $ this ->updateCurrentBookmark ($ identifier );
@@ -176,32 +181,31 @@ protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $tit
176181 *
177182 * @param string $identifier
178183 * @return void
184+ * @throws LocalizedException
179185 */
180186 protected function updateCurrentBookmark ($ identifier )
181187 {
182188 $ bookmarks = $ this ->bookmarkManagement ->loadByNamespace ($ this ->_request ->getParam ('namespace ' ));
183189 $ currentConfig = null ;
184190 foreach ($ bookmarks ->getItems () as $ bookmark ) {
185- if ($ bookmark ->getIdentifier () === self :: CURRENT_IDENTIFIER ) {
191+ if ($ bookmark ->getIdentifier () == $ identifier ) {
186192 $ 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 );
189197 }
198+ $ this ->bookmarkRepository ->save ($ bookmark );
190199 }
191200
192201 foreach ($ bookmarks ->getItems () as $ bookmark ) {
193- if ($ bookmark ->getCurrent () && $ currentConfig !== null ) {
202+ if ($ bookmark ->getIdentifier () === self :: CURRENT_IDENTIFIER && $ currentConfig !== null ) {
194203 $ bookmarkConfig = $ bookmark ->getConfig ();
195- $ bookmarkConfig [' views ' ][ $ bookmark -> getIdentifier ()][ ' data ' ] = $ currentConfig ;
204+ $ bookmarkConfig [self :: CURRENT_IDENTIFIER ] = $ currentConfig ;
196205 $ bookmark ->setConfig ($ this ->serializer ->serialize ($ bookmarkConfig ));
206+ $ this ->bookmarkRepository ->save ($ bookmark );
207+ break ;
197208 }
198-
199- if ($ bookmark ->getIdentifier () == $ identifier ) {
200- $ bookmark ->setCurrent (true );
201- } else {
202- $ bookmark ->setCurrent (false );
203- }
204- $ this ->bookmarkRepository ->save ($ bookmark );
205209 }
206210 }
207211
@@ -226,4 +230,33 @@ protected function checkBookmark($identifier)
226230
227231 return $ result ;
228232 }
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+ }
229262}
0 commit comments