Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Delete.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
16  private $filesystem;
17 
21  private $sitemapFactory;
22 
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 
43  public function execute()
44  {
45  $directory = $this->getFilesystem()->getDirectoryWrite(DirectoryList::ROOT);
46  // check if we know what should be deleted
47  $id = $this->getRequest()->getParam('sitemap_id');
48  if ($id) {
49  try {
50  // init model and delete
52  $sitemap = $this->sitemapFactory->create();
53  $sitemap->load($id);
54  // delete file
55  $sitemapPath = $sitemap->getSitemapPath();
56  if ($sitemapPath && $sitemapPath[0] === DIRECTORY_SEPARATOR) {
57  $sitemapPath = mb_substr($sitemapPath, 1);
58  }
59  $sitemapFilename = $sitemap->getSitemapFilename();
60  $path = $directory->getRelativePath(
61  $sitemapPath .$sitemapFilename
62  );
63  if ($sitemap->getSitemapFilename() && $directory->isFile($path)) {
64  $directory->delete($path);
65  }
66  $sitemap->delete();
67  // display success message
68  $this->messageManager->addSuccessMessage(__('You deleted the sitemap.'));
69  // go to grid
70  $this->_redirect('adminhtml/*/');
71  return;
72  } catch (\Exception $e) {
73  // display error message
74  $this->messageManager->addErrorMessage($e->getMessage());
75  // go back to edit form
76  $this->_redirect('adminhtml/*/edit', ['sitemap_id' => $id]);
77  return;
78  }
79  }
80  // display error message
81  $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to delete.'));
82  // go to grid
83  $this->_redirect('adminhtml/*/');
84  }
85 
92  private function getFilesystem()
93  {
94  if (null === $this->filesystem) {
95  $this->filesystem = \Magento\Framework\App\ObjectManager::getInstance()->get(
96  \Magento\Framework\Filesystem::class
97  );
98  }
99  return $this->filesystem;
100  }
101 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Sitemap\Model\SitemapFactory $sitemapFactory=null)
Definition: Delete.php:29