ExportTablerates.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\OfflineShipping\Controller\Adminhtml\System\Config;
  8. use Magento\Framework\App\ResponseInterface;
  9. use Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker;
  10. use Magento\Framework\App\Filesystem\DirectoryList;
  11. class ExportTablerates extends \Magento\Config\Controller\Adminhtml\System\AbstractConfig
  12. {
  13. /**
  14. * @var \Magento\Framework\App\Response\Http\FileFactory
  15. */
  16. protected $_fileFactory;
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. protected $_storeManager;
  21. /**
  22. * @param \Magento\Backend\App\Action\Context $context
  23. * @param \Magento\Config\Model\Config\Structure $configStructure
  24. * @param \Magento\Config\Controller\Adminhtml\System\ConfigSectionChecker $sectionChecker
  25. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  26. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  27. */
  28. public function __construct(
  29. \Magento\Backend\App\Action\Context $context,
  30. \Magento\Config\Model\Config\Structure $configStructure,
  31. ConfigSectionChecker $sectionChecker,
  32. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  33. \Magento\Store\Model\StoreManagerInterface $storeManager
  34. ) {
  35. $this->_storeManager = $storeManager;
  36. $this->_fileFactory = $fileFactory;
  37. parent::__construct($context, $configStructure, $sectionChecker);
  38. }
  39. /**
  40. * Export shipping table rates in csv format
  41. *
  42. * @return ResponseInterface
  43. */
  44. public function execute()
  45. {
  46. $fileName = 'tablerates.csv';
  47. /** @var $gridBlock \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid */
  48. $gridBlock = $this->_view->getLayout()->createBlock(
  49. \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid::class
  50. );
  51. $website = $this->_storeManager->getWebsite($this->getRequest()->getParam('website'));
  52. if ($this->getRequest()->getParam('conditionName')) {
  53. $conditionName = $this->getRequest()->getParam('conditionName');
  54. } else {
  55. $conditionName = $website->getConfig('carriers/tablerate/condition_name');
  56. }
  57. $gridBlock->setWebsiteId($website->getId())->setConditionName($conditionName);
  58. $content = $gridBlock->getCsvFile();
  59. return $this->_fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
  60. }
  61. }