Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
16 use Magento\Ui\Api\Data\BookmarkInterfaceFactory;
18 
24 class Save extends AbstractAction implements HttpPostActionInterface
25 {
29  const CURRENT_IDENTIFIER = 'current';
30 
31  const ACTIVE_IDENTIFIER = 'activeIndex';
32 
33  const VIEWS_IDENTIFIER = 'views';
34 
39 
44 
48  protected $bookmarkFactory;
49 
53  protected $userContext;
54 
59  protected $jsonDecoder;
60 
64  private $serializer;
65 
77  public function __construct(
78  Context $context,
82  BookmarkInterfaceFactory $bookmarkFactory,
85  \Magento\Framework\Serialize\Serializer\Json $serializer = null
86  ) {
87  parent::__construct($context, $factory);
88  $this->bookmarkRepository = $bookmarkRepository;
89  $this->bookmarkManagement = $bookmarkManagement;
90  $this->bookmarkFactory = $bookmarkFactory;
91  $this->userContext = $userContext;
92  $this->jsonDecoder = $jsonDecoder;
93  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
94  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
95  }
96 
104  public function execute()
105  {
106  $bookmark = $this->bookmarkFactory->create();
107  $jsonData = $this->_request->getParam('data');
108  if (!$jsonData) {
109  throw new \InvalidArgumentException('Invalid parameter "data"');
110  }
111  $data = $this->serializer->unserialize($jsonData);
112  $action = key($data);
113  switch ($action) {
115  $this->updateCurrentBookmark($data[$action]);
116  break;
117 
119  $this->updateBookmark(
120  $bookmark,
121  $action,
122  $bookmark->getTitle(),
123  $jsonData
124  );
125 
126  break;
127 
129  foreach ($data[$action] as $identifier => $data) {
130  $this->updateBookmark(
131  $bookmark,
132  $identifier,
133  isset($data['label']) ? $data['label'] : '',
134  $jsonData
135  );
136  $this->updateCurrentBookmark($identifier);
137  }
138 
139  break;
140 
141  default:
142  throw new \LogicException(__('Unsupported bookmark action.'));
143  }
144  }
145 
155  protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $title, $config)
156  {
157  $updateBookmark = $this->checkBookmark($identifier);
158  if ($updateBookmark !== false) {
159  $bookmark = $updateBookmark;
160  }
161 
162  $bookmark->setUserId($this->userContext->getUserId())
163  ->setNamespace($this->_request->getParam('namespace'))
164  ->setIdentifier($identifier)
165  ->setTitle($title)
166  ->setConfig($config);
167  $this->bookmarkRepository->save($bookmark);
168  }
169 
176  protected function updateCurrentBookmark($identifier)
177  {
178  $bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace'));
179  foreach ($bookmarks->getItems() as $bookmark) {
180  if ($bookmark->getIdentifier() == $identifier) {
181  $bookmark->setCurrent(true);
182  } else {
183  $bookmark->setCurrent(false);
184  }
185  $this->bookmarkRepository->save($bookmark);
186  }
187  }
188 
195  protected function checkBookmark($identifier)
196  {
197  $result = false;
198 
199  $updateBookmark = $this->bookmarkManagement->getByIdentifierNamespace(
200  $identifier,
201  $this->_request->getParam('namespace')
202  );
203 
204  if ($updateBookmark) {
205  $result = $updateBookmark;
206  }
207 
208  return $result;
209  }
210 }
$title
Definition: default.phtml:14
$config
Definition: fraud_order.php:17
$bookmarks
Definition: bookmarks.php:10
__()
Definition: __.php:13
__construct(Context $context, UiComponentFactory $factory, BookmarkRepositoryInterface $bookmarkRepository, BookmarkManagementInterface $bookmarkManagement, BookmarkInterfaceFactory $bookmarkFactory, UserContextInterface $userContext, DecoderInterface $jsonDecoder, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Save.php:77
updateBookmark(BookmarkInterface $bookmark, $identifier, $title, $config)
Definition: Save.php:155