Save.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\Controller\Adminhtml\Bookmark;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Authorization\Model\UserContextInterface;
  9. use Magento\Backend\App\Action\Context;
  10. use Magento\Framework\Json\DecoderInterface;
  11. use Magento\Framework\View\Element\UiComponentFactory;
  12. use Magento\Ui\Api\BookmarkManagementInterface;
  13. use Magento\Ui\Api\BookmarkRepositoryInterface;
  14. use Magento\Ui\Api\Data\BookmarkInterface;
  15. use Magento\Ui\Api\Data\BookmarkInterfaceFactory;
  16. use Magento\Ui\Controller\Adminhtml\AbstractAction;
  17. /**
  18. * Class Save action
  19. *
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class Save extends AbstractAction implements HttpPostActionInterface
  23. {
  24. /**
  25. * Identifier for current bookmark
  26. */
  27. const CURRENT_IDENTIFIER = 'current';
  28. const ACTIVE_IDENTIFIER = 'activeIndex';
  29. const VIEWS_IDENTIFIER = 'views';
  30. /**
  31. * @var BookmarkRepositoryInterface
  32. */
  33. protected $bookmarkRepository;
  34. /**
  35. * @var BookmarkManagementInterface
  36. */
  37. protected $bookmarkManagement;
  38. /**
  39. * @var BookmarkInterfaceFactory
  40. */
  41. protected $bookmarkFactory;
  42. /**
  43. * @var UserContextInterface
  44. */
  45. protected $userContext;
  46. /**
  47. * @var DecoderInterface
  48. * @deprecated 101.1.0
  49. */
  50. protected $jsonDecoder;
  51. /**
  52. * @var \Magento\Framework\Serialize\Serializer\Json
  53. */
  54. private $serializer;
  55. /**
  56. * @param Context $context
  57. * @param UiComponentFactory $factory
  58. * @param BookmarkRepositoryInterface $bookmarkRepository
  59. * @param BookmarkManagementInterface $bookmarkManagement
  60. * @param BookmarkInterfaceFactory $bookmarkFactory
  61. * @param UserContextInterface $userContext
  62. * @param DecoderInterface $jsonDecoder
  63. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  64. * @throws \RuntimeException
  65. */
  66. public function __construct(
  67. Context $context,
  68. UiComponentFactory $factory,
  69. BookmarkRepositoryInterface $bookmarkRepository,
  70. BookmarkManagementInterface $bookmarkManagement,
  71. BookmarkInterfaceFactory $bookmarkFactory,
  72. UserContextInterface $userContext,
  73. DecoderInterface $jsonDecoder,
  74. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  75. ) {
  76. parent::__construct($context, $factory);
  77. $this->bookmarkRepository = $bookmarkRepository;
  78. $this->bookmarkManagement = $bookmarkManagement;
  79. $this->bookmarkFactory = $bookmarkFactory;
  80. $this->userContext = $userContext;
  81. $this->jsonDecoder = $jsonDecoder;
  82. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  83. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  84. }
  85. /**
  86. * Action for AJAX request
  87. *
  88. * @return void
  89. * @throws \InvalidArgumentException
  90. * @throws \LogicException
  91. */
  92. public function execute()
  93. {
  94. $bookmark = $this->bookmarkFactory->create();
  95. $jsonData = $this->_request->getParam('data');
  96. if (!$jsonData) {
  97. throw new \InvalidArgumentException('Invalid parameter "data"');
  98. }
  99. $data = $this->serializer->unserialize($jsonData);
  100. $action = key($data);
  101. switch ($action) {
  102. case self::ACTIVE_IDENTIFIER:
  103. $this->updateCurrentBookmark($data[$action]);
  104. break;
  105. case self::CURRENT_IDENTIFIER:
  106. $this->updateBookmark(
  107. $bookmark,
  108. $action,
  109. $bookmark->getTitle(),
  110. $jsonData
  111. );
  112. break;
  113. case self::VIEWS_IDENTIFIER:
  114. foreach ($data[$action] as $identifier => $data) {
  115. $this->updateBookmark(
  116. $bookmark,
  117. $identifier,
  118. isset($data['label']) ? $data['label'] : '',
  119. $jsonData
  120. );
  121. $this->updateCurrentBookmark($identifier);
  122. }
  123. break;
  124. default:
  125. throw new \LogicException(__('Unsupported bookmark action.'));
  126. }
  127. }
  128. /**
  129. * Update bookmarks based on request params
  130. *
  131. * @param BookmarkInterface $bookmark
  132. * @param string $identifier
  133. * @param string $title
  134. * @param string $config
  135. * @return void
  136. */
  137. protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $title, $config)
  138. {
  139. $updateBookmark = $this->checkBookmark($identifier);
  140. if ($updateBookmark !== false) {
  141. $bookmark = $updateBookmark;
  142. }
  143. $bookmark->setUserId($this->userContext->getUserId())
  144. ->setNamespace($this->_request->getParam('namespace'))
  145. ->setIdentifier($identifier)
  146. ->setTitle($title)
  147. ->setConfig($config);
  148. $this->bookmarkRepository->save($bookmark);
  149. }
  150. /**
  151. * Update current bookmark
  152. *
  153. * @param string $identifier
  154. * @return void
  155. */
  156. protected function updateCurrentBookmark($identifier)
  157. {
  158. $bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace'));
  159. foreach ($bookmarks->getItems() as $bookmark) {
  160. if ($bookmark->getIdentifier() == $identifier) {
  161. $bookmark->setCurrent(true);
  162. } else {
  163. $bookmark->setCurrent(false);
  164. }
  165. $this->bookmarkRepository->save($bookmark);
  166. }
  167. }
  168. /**
  169. * Check bookmark by identifier
  170. *
  171. * @param string $identifier
  172. * @return bool|BookmarkInterface
  173. */
  174. protected function checkBookmark($identifier)
  175. {
  176. $result = false;
  177. $updateBookmark = $this->bookmarkManagement->getByIdentifierNamespace(
  178. $identifier,
  179. $this->_request->getParam('namespace')
  180. );
  181. if ($updateBookmark) {
  182. $result = $updateBookmark;
  183. }
  184. return $result;
  185. }
  186. }