Rewrite.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Controller\Adminhtml\Url;
  7. use Magento\Backend\App\Action;
  8. use Magento\Catalog\Model\Category;
  9. use Magento\Catalog\Model\Product;
  10. /**
  11. * URL rewrite adminhtml controller
  12. */
  13. abstract class Rewrite extends Action
  14. {
  15. /**
  16. * Authorization level of a basic admin session
  17. *
  18. * @see _isAllowed()
  19. */
  20. const ADMIN_RESOURCE = 'Magento_UrlRewrite::urlrewrite';
  21. /**#@+
  22. * Entity types
  23. */
  24. const ENTITY_TYPE_CUSTOM = 'custom';
  25. const ENTITY_TYPE_PRODUCT = 'product';
  26. const ENTITY_TYPE_CATEGORY = 'category';
  27. const ENTITY_TYPE_CMS_PAGE = 'cms-page';
  28. /**#@-*/
  29. /**#@-*/
  30. protected $_product;
  31. /**
  32. * @var Category
  33. */
  34. protected $_category;
  35. /**
  36. * @var \Magento\Cms\Model\Page
  37. */
  38. protected $_cmsPage;
  39. /**
  40. * @var \Magento\UrlRewrite\Model\UrlRewrite
  41. */
  42. protected $_urlRewrite;
  43. /**
  44. * Get Category from request
  45. *
  46. * @return Category
  47. */
  48. protected function _getCategory()
  49. {
  50. if (!$this->_category) {
  51. $this->_category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
  52. $categoryId = (int)$this->getRequest()->getParam('category', 0);
  53. $urlRewrite = $this->_getUrlRewrite();
  54. if (!$categoryId && $urlRewrite->getId()) {
  55. $metaData = $urlRewrite->getMetadata();
  56. if ($urlRewrite->getEntityType() === self::ENTITY_TYPE_CATEGORY) {
  57. $categoryId = $urlRewrite->getEntityId();
  58. } elseif (!empty($metaData['category_id'])) {
  59. $categoryId = $metaData['category_id'];
  60. }
  61. }
  62. if ($categoryId) {
  63. $this->_category->load($categoryId);
  64. }
  65. }
  66. return $this->_category;
  67. }
  68. /**
  69. * Get Product from request
  70. *
  71. * @return Product
  72. */
  73. protected function _getProduct()
  74. {
  75. if (!$this->_product) {
  76. $this->_product = $this->_objectManager->create(\Magento\Catalog\Model\Product::class);
  77. $productId = (int)$this->getRequest()->getParam('product', 0);
  78. $urlRewrite = $this->_getUrlRewrite();
  79. if (!$productId && $urlRewrite->getId() && $urlRewrite->getEntityType() === self::ENTITY_TYPE_PRODUCT) {
  80. $productId = $this->_getUrlRewrite()->getEntityId();
  81. }
  82. if ($productId) {
  83. $this->_product->load($productId);
  84. }
  85. }
  86. return $this->_product;
  87. }
  88. /**
  89. * Get CMS page from request
  90. *
  91. * @return \Magento\Cms\Model\Page
  92. */
  93. protected function _getCmsPage()
  94. {
  95. if (!$this->_cmsPage) {
  96. $this->_cmsPage = $this->_objectManager->create(\Magento\Cms\Model\Page::class);
  97. $cmsPageId = (int)$this->getRequest()->getParam('cms_page', 0);
  98. $urlRewrite = $this->_getUrlRewrite();
  99. if (!$cmsPageId && $urlRewrite->getId() && $urlRewrite->getEntityType() === self::ENTITY_TYPE_CMS_PAGE) {
  100. $cmsPageId = $this->_getUrlRewrite()->getEntityId();
  101. }
  102. if ($cmsPageId) {
  103. $this->_cmsPage->load($cmsPageId);
  104. }
  105. }
  106. return $this->_cmsPage;
  107. }
  108. /**
  109. * Get URL rewrite from request
  110. *
  111. * @return \Magento\UrlRewrite\Model\UrlRewrite
  112. */
  113. protected function _getUrlRewrite()
  114. {
  115. if (!$this->_urlRewrite) {
  116. $this->_urlRewrite = $this->_objectManager->create(\Magento\UrlRewrite\Model\UrlRewrite::class);
  117. $urlRewriteId = (int)$this->getRequest()->getParam('id', 0);
  118. if ($urlRewriteId) {
  119. $this->_urlRewrite->load($urlRewriteId);
  120. }
  121. }
  122. return $this->_urlRewrite;
  123. }
  124. }