Generate.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Backend\App\Action;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Store\Model\App\Emulation;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * Controller class Generate. Represents requests flow logic for sitemap generation
  13. */
  14. class Generate extends \Magento\Sitemap\Controller\Adminhtml\Sitemap implements HttpGetActionInterface
  15. {
  16. /** @var \Magento\Store\Model\App\Emulation $appEmulation */
  17. private $appEmulation;
  18. /**
  19. * Generate constructor.
  20. * @param Action\Context $context
  21. * @param \Magento\Store\Model\App\Emulation|null $appEmulation
  22. */
  23. public function __construct(
  24. Action\Context $context,
  25. Emulation $appEmulation = null
  26. ) {
  27. parent::__construct($context);
  28. $this->appEmulation = $appEmulation ?: ObjectManager::getInstance()
  29. ->get(\Magento\Store\Model\App\Emulation::class);
  30. }
  31. /**
  32. * Generate sitemap
  33. *
  34. * @return void
  35. */
  36. public function execute()
  37. {
  38. // init and load sitemap model
  39. $id = $this->getRequest()->getParam('sitemap_id');
  40. $sitemap = $this->_objectManager->create(\Magento\Sitemap\Model\Sitemap::class);
  41. /* @var $sitemap \Magento\Sitemap\Model\Sitemap */
  42. $sitemap->load($id);
  43. // if sitemap record exists
  44. if ($sitemap->getId()) {
  45. try {
  46. //We need to emulate to get the correct frontend URL for the product images
  47. $this->appEmulation->startEnvironmentEmulation(
  48. $sitemap->getStoreId(),
  49. \Magento\Framework\App\Area::AREA_FRONTEND,
  50. true
  51. );
  52. $sitemap->generateXml();
  53. $this->messageManager->addSuccessMessage(
  54. __('The sitemap "%1" has been generated.', $sitemap->getSitemapFilename())
  55. );
  56. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  57. $this->messageManager->addErrorMessage($e->getMessage());
  58. } catch (\Exception $e) {
  59. $this->messageManager->addExceptionMessage($e, __('We can\'t generate the sitemap right now.'));
  60. } finally {
  61. $this->appEmulation->stopEnvironmentEmulation();
  62. }
  63. } else {
  64. $this->messageManager->addErrorMessage(__('We can\'t find a sitemap to generate.'));
  65. }
  66. // go to grid
  67. $this->_redirect('adminhtml/*/');
  68. }
  69. }