Save.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\UrlRewrite\Model\UrlFinderInterface;
  11. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  12. class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implements HttpPostActionInterface
  13. {
  14. /**
  15. * @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
  16. */
  17. protected $productUrlPathGenerator;
  18. /**
  19. * @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
  20. */
  21. protected $categoryUrlPathGenerator;
  22. /**
  23. * @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator
  24. */
  25. protected $cmsPageUrlPathGenerator;
  26. /**
  27. * @var \Magento\UrlRewrite\Model\UrlFinderInterface
  28. */
  29. protected $urlFinder;
  30. /**
  31. * @param \Magento\Backend\App\Action\Context $context
  32. * @param \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator
  33. * @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
  34. * @param \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
  35. * @param UrlFinderInterface $urlFinder
  36. */
  37. public function __construct(
  38. \Magento\Backend\App\Action\Context $context,
  39. \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
  40. \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
  41. \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
  42. UrlFinderInterface $urlFinder
  43. ) {
  44. parent::__construct($context);
  45. $this->productUrlPathGenerator = $productUrlPathGenerator;
  46. $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
  47. $this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
  48. $this->urlFinder = $urlFinder;
  49. }
  50. /**
  51. * Override urlrewrite data, basing on current category and product
  52. *
  53. * @param \Magento\UrlRewrite\Model\UrlRewrite $model
  54. * @return void
  55. * @throws \Magento\Framework\Exception\LocalizedException
  56. */
  57. protected function _handleCatalogUrlRewrite($model)
  58. {
  59. $productId = $this->_getProduct()->getId();
  60. $categoryId = $this->_getCategory()->getId();
  61. if ($productId || $categoryId) {
  62. if ($model->isObjectNew()) {
  63. $model->setEntityType($productId ? self::ENTITY_TYPE_PRODUCT : self::ENTITY_TYPE_CATEGORY)
  64. ->setEntityId($productId ?: $categoryId);
  65. if ($productId && $categoryId) {
  66. $model->setMetadata(['category_id' => $categoryId]);
  67. }
  68. }
  69. $model->setTargetPath($this->getTargetPath($model));
  70. }
  71. }
  72. /**
  73. * Get Target Path
  74. *
  75. * @param \Magento\UrlRewrite\Model\UrlRewrite $model
  76. * @return string
  77. * @throws \Magento\Framework\Exception\LocalizedException
  78. */
  79. protected function getTargetPath($model)
  80. {
  81. $targetPath = $this->getCanonicalTargetPath();
  82. if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
  83. $data = [
  84. UrlRewrite::ENTITY_ID => $model->getEntityId(),
  85. UrlRewrite::TARGET_PATH => $targetPath,
  86. UrlRewrite::ENTITY_TYPE => $model->getEntityType(),
  87. UrlRewrite::STORE_ID => $model->getStoreId(),
  88. ];
  89. $rewrite = $this->urlFinder->findOneByData($data);
  90. if (!$rewrite) {
  91. $message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
  92. ? __("The selected product isn't associated with the selected store or category.")
  93. : __("The selected category isn't associated with the selected store.");
  94. throw new LocalizedException($message);
  95. }
  96. $targetPath = $rewrite->getRequestPath();
  97. }
  98. return $targetPath;
  99. }
  100. /**
  101. * @return string
  102. */
  103. protected function getCanonicalTargetPath()
  104. {
  105. $product = $this->_getProduct()->getId() ? $this->_getProduct() : null;
  106. $category = $this->_getCategory()->getId() ? $this->_getCategory() : null;
  107. return $product
  108. ? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category)
  109. : $this->categoryUrlPathGenerator->getCanonicalUrlPath($category);
  110. }
  111. /**
  112. * Override URL rewrite data, basing on current CMS page
  113. *
  114. * @param \Magento\UrlRewrite\Model\UrlRewrite $model
  115. * @return void
  116. */
  117. private function _handleCmsPageUrlRewrite($model)
  118. {
  119. $cmsPage = $this->_getCmsPage();
  120. if ($cmsPage->getId()) {
  121. if ($model->isObjectNew()) {
  122. $model->setEntityType(self::ENTITY_TYPE_CMS_PAGE)->setEntityId($cmsPage->getId());
  123. }
  124. if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
  125. $targetPath = $this->cmsPageUrlPathGenerator->getUrlPath($cmsPage);
  126. } else {
  127. $targetPath = $this->cmsPageUrlPathGenerator->getCanonicalUrlPath($cmsPage);
  128. }
  129. $model->setTargetPath($targetPath);
  130. }
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function execute()
  136. {
  137. $data = $this->getRequest()->getPostValue();
  138. if ($data) {
  139. /** @var $session \Magento\Backend\Model\Session */
  140. $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
  141. try {
  142. $model = $this->_getUrlRewrite();
  143. $requestPath = $this->getRequest()->getParam('request_path');
  144. $this->_objectManager->get(
  145. \Magento\UrlRewrite\Helper\UrlRewrite::class
  146. )->validateRequestPath($requestPath);
  147. $model->setEntityType($this->getRequest()->getParam('entity_type') ?: self::ENTITY_TYPE_CUSTOM)
  148. ->setRequestPath($requestPath)
  149. ->setTargetPath($this->getRequest()->getParam('target_path', $model->getTargetPath()))
  150. ->setRedirectType($this->getRequest()->getParam('redirect_type'))
  151. ->setStoreId($this->getRequest()->getParam('store_id', 0))
  152. ->setDescription($this->getRequest()->getParam('description'));
  153. $this->_handleCatalogUrlRewrite($model);
  154. $this->_handleCmsPageUrlRewrite($model);
  155. $model->save();
  156. $this->messageManager->addSuccess(__('The URL Rewrite has been saved.'));
  157. $this->_redirect('adminhtml/*/');
  158. return;
  159. } catch (LocalizedException $e) {
  160. $this->messageManager->addError($e->getMessage());
  161. $session->setUrlRewriteData($data);
  162. } catch (\Exception $e) {
  163. $this->messageManager->addException(
  164. $e,
  165. __('An error occurred while saving the URL rewrite. Please try to save again.')
  166. );
  167. $session->setUrlRewriteData($data);
  168. }
  169. }
  170. $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
  171. }
  172. }