Delete.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Controller\Adminhtml\Sitemap;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\App\ObjectManager;
  10. /**
  11. * Controller class Delete. Represents adminhtml request flow for a sitemap deletion
  12. */
  13. class Delete extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpPostActionInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\Filesystem
  17. */
  18. private $filesystem;
  19. /**
  20. * @var \Magento\Sitemap\Model\SitemapFactory
  21. */
  22. private $sitemapFactory;
  23. /**
  24. * Constructor
  25. *
  26. * @param \Magento\Backend\App\Action\Context $context
  27. * @param \Magento\Sitemap\Model\SitemapFactory|null $sitemapFactory
  28. */
  29. public function __construct(
  30. \Magento\Backend\App\Action\Context $context,
  31. \Magento\Sitemap\Model\SitemapFactory $sitemapFactory = null
  32. ) {
  33. parent::__construct($context);
  34. $this->sitemapFactory = $sitemapFactory ?: ObjectManager::getInstance()
  35. ->get(\Magento\Sitemap\Model\SitemapFactory::class);
  36. }
  37. /**
  38. * Delete action
  39. *
  40. * @return void
  41. */
  42. public function execute()
  43. {
  44. $directory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
  45. // check if we know what should be deleted
  46. $id = $this->getRequest()->getParam('sitemap_id');
  47. if ($id) {
  48. try {
  49. // init model and delete
  50. /** @var \Magento\Sitemap\Model\Sitemap $sitemap */
  51. $sitemap = $this->sitemapFactory->create();
  52. $sitemap->load($id);
  53. // delete file
  54. $sitemapPath = $sitemap->getSitemapPath();
  55. if ($sitemapPath && $sitemapPath[0] === DIRECTORY_SEPARATOR) {
  56. $sitemapPath = mb_substr($sitemapPath, 1);
  57. }
  58. $sitemapFilename = $sitemap->getSitemapFilename();
  59. $path = $directory->getRelativePath(
  60. $sitemapPath .$sitemapFilename
  61. );
  62. if ($sitemap->getSitemapFilename() && $directory->isFile($path)) {
  63. $directory->delete($path);
  64. }
  65. $sitemap->delete();
  66. // display success message
  67. $this->messageManager->addSuccessMessage(__('You deleted the sitemap.'));
  68. // go to grid
  69. $this->_redirect('adminhtml/*/');
  70. return;
  71. } catch (\Exception $e) {
  72. // display error message
  73. $this->messageManager->addErrorMessage($e->getMessage());
  74. // go back to edit form
  75. $this->_redirect('adminhtml/*/edit', ['sitemap_id' => $id]);
  76. return;
  77. }
  78. }
  79. // display error message
  80. $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to delete.'));
  81. // go to grid
  82. $this->_redirect('adminhtml/*/');
  83. }
  84. /**
  85. * The getter function to get Filesystem object for real application code
  86. *
  87. * @return \Magento\Framework\Filesystem
  88. * @deprecated 100.2.0
  89. */
  90. private function getFilesystem()
  91. {
  92. if (null === $this->filesystem) {
  93. $this->filesystem = \Magento\Framework\App\ObjectManager::getInstance()->get(
  94. \Magento\Framework\Filesystem::class
  95. );
  96. }
  97. return $this->filesystem;
  98. }
  99. }