InlineEdit.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Controller\Adminhtml\Page;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Cms\Api\PageRepositoryInterface as PageRepository;
  9. use Magento\Framework\Controller\Result\JsonFactory;
  10. use Magento\Cms\Api\Data\PageInterface;
  11. /**
  12. * Cms page grid inline edit controller
  13. *
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class InlineEdit extends \Magento\Backend\App\Action
  17. {
  18. /**
  19. * Authorization level of a basic admin session
  20. */
  21. const ADMIN_RESOURCE = 'Magento_Cms::save';
  22. /**
  23. * @var \Magento\Cms\Controller\Adminhtml\Page\PostDataProcessor
  24. */
  25. protected $dataProcessor;
  26. /**
  27. * @var \Magento\Cms\Api\PageRepositoryInterface
  28. */
  29. protected $pageRepository;
  30. /**
  31. * @var \Magento\Framework\Controller\Result\JsonFactory
  32. */
  33. protected $jsonFactory;
  34. /**
  35. * @param Context $context
  36. * @param PostDataProcessor $dataProcessor
  37. * @param PageRepository $pageRepository
  38. * @param JsonFactory $jsonFactory
  39. */
  40. public function __construct(
  41. Context $context,
  42. PostDataProcessor $dataProcessor,
  43. PageRepository $pageRepository,
  44. JsonFactory $jsonFactory
  45. ) {
  46. parent::__construct($context);
  47. $this->dataProcessor = $dataProcessor;
  48. $this->pageRepository = $pageRepository;
  49. $this->jsonFactory = $jsonFactory;
  50. }
  51. /**
  52. * @return \Magento\Framework\Controller\ResultInterface
  53. * @throws \Magento\Framework\Exception\LocalizedException
  54. */
  55. public function execute()
  56. {
  57. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  58. $resultJson = $this->jsonFactory->create();
  59. $error = false;
  60. $messages = [];
  61. $postItems = $this->getRequest()->getParam('items', []);
  62. if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
  63. return $resultJson->setData([
  64. 'messages' => [__('Please correct the data sent.')],
  65. 'error' => true,
  66. ]);
  67. }
  68. foreach (array_keys($postItems) as $pageId) {
  69. /** @var \Magento\Cms\Model\Page $page */
  70. $page = $this->pageRepository->getById($pageId);
  71. try {
  72. $pageData = $this->filterPost($postItems[$pageId]);
  73. $this->validatePost($pageData, $page, $error, $messages);
  74. $extendedPageData = $page->getData();
  75. $this->setCmsPageData($page, $extendedPageData, $pageData);
  76. $this->pageRepository->save($page);
  77. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  78. $messages[] = $this->getErrorWithPageId($page, $e->getMessage());
  79. $error = true;
  80. } catch (\RuntimeException $e) {
  81. $messages[] = $this->getErrorWithPageId($page, $e->getMessage());
  82. $error = true;
  83. } catch (\Exception $e) {
  84. $messages[] = $this->getErrorWithPageId(
  85. $page,
  86. __('Something went wrong while saving the page.')
  87. );
  88. $error = true;
  89. }
  90. }
  91. return $resultJson->setData([
  92. 'messages' => $messages,
  93. 'error' => $error
  94. ]);
  95. }
  96. /**
  97. * Filtering posted data.
  98. *
  99. * @param array $postData
  100. * @return array
  101. */
  102. protected function filterPost($postData = [])
  103. {
  104. $pageData = $this->dataProcessor->filter($postData);
  105. $pageData['custom_theme'] = isset($pageData['custom_theme']) ? $pageData['custom_theme'] : null;
  106. $pageData['custom_root_template'] = isset($pageData['custom_root_template'])
  107. ? $pageData['custom_root_template']
  108. : null;
  109. return $pageData;
  110. }
  111. /**
  112. * Validate post data
  113. *
  114. * @param array $pageData
  115. * @param \Magento\Cms\Model\Page $page
  116. * @param bool $error
  117. * @param array $messages
  118. * @return void
  119. */
  120. protected function validatePost(array $pageData, \Magento\Cms\Model\Page $page, &$error, array &$messages)
  121. {
  122. if (!($this->dataProcessor->validate($pageData) && $this->dataProcessor->validateRequireEntry($pageData))) {
  123. $error = true;
  124. foreach ($this->messageManager->getMessages(true)->getItems() as $error) {
  125. $messages[] = $this->getErrorWithPageId($page, $error->getText());
  126. }
  127. }
  128. }
  129. /**
  130. * Add page title to error message
  131. *
  132. * @param PageInterface $page
  133. * @param string $errorText
  134. * @return string
  135. */
  136. protected function getErrorWithPageId(PageInterface $page, $errorText)
  137. {
  138. return '[Page ID: ' . $page->getId() . '] ' . $errorText;
  139. }
  140. /**
  141. * Set cms page data
  142. *
  143. * @param \Magento\Cms\Model\Page $page
  144. * @param array $extendedPageData
  145. * @param array $pageData
  146. * @return $this
  147. */
  148. public function setCmsPageData(\Magento\Cms\Model\Page $page, array $extendedPageData, array $pageData)
  149. {
  150. $page->setData(array_merge($page->getData(), $extendedPageData, $pageData));
  151. return $this;
  152. }
  153. }