Save.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Controller\Adminhtml\Settings\Advanced;
  6. use Magento\Backend\App\Action;
  7. use Magento\Backend\App\Action\Context;
  8. use Magento\Framework\Exception\LocalizedException;
  9. use Temando\Shipping\Model\Config\ModuleConfigInterface;
  10. use Temando\Shipping\Model\ResourceModel\EventStream\StreamRepositoryInterface;
  11. /**
  12. * Temando Save Advanced Settings Action
  13. *
  14. * @package Temando\Shipping\Controller
  15. * @author Max Melzer <max.melzer@netresearch.de>
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. * @link http://www.temando.com/
  18. */
  19. class Save extends Action
  20. {
  21. /**
  22. * @var ModuleConfigInterface
  23. */
  24. private $config;
  25. /**
  26. * @var StreamRepositoryInterface
  27. */
  28. private $streamRepository;
  29. /**
  30. * Save constructor.
  31. *
  32. * @param Context $context
  33. * @param ModuleConfigInterface $config
  34. * @param StreamRepositoryInterface $streamRepository
  35. */
  36. public function __construct(
  37. Context $context,
  38. ModuleConfigInterface $config,
  39. StreamRepositoryInterface $streamRepository
  40. ) {
  41. $this->config = $config;
  42. $this->streamRepository = $streamRepository;
  43. parent::__construct($context);
  44. }
  45. /**
  46. * @return \Magento\Framework\Controller\Result\Redirect
  47. */
  48. public function execute()
  49. {
  50. $resultRedirect = $this->resultRedirectFactory->create();
  51. $request = $this->getRequest();
  52. $sync = (bool) $request->getParam('sync_enable', false);
  53. $syncShipment = (bool) $request->getParam('sync_shipment', false);
  54. $syncOrder = (bool) $request->getParam('sync_order', false);
  55. try {
  56. if ($sync && !$this->config->isSyncEnabled()) {
  57. // sync was switched on, save everything
  58. $this->streamRepository->save($this->config->getStreamId());
  59. $this->config->saveSyncEnabled($sync);
  60. $this->config->saveSyncShipmentEnabled($syncShipment);
  61. $this->config->saveSyncOrderEnabled($syncOrder);
  62. $this->messageManager->addSuccessMessage(__('Settings saved successfully, stream created.'));
  63. } elseif ($sync && $this->config->isSyncEnabled()) {
  64. // sync is still on, save sync types only
  65. $this->config->saveSyncShipmentEnabled($syncShipment);
  66. $this->config->saveSyncOrderEnabled($syncOrder);
  67. $this->messageManager->addSuccessMessage(__('Settings saved successfully.'));
  68. } elseif (!$sync && $this->config->isSyncEnabled()) {
  69. // sync was switched off, save state only
  70. $this->streamRepository->delete($this->config->getStreamId());
  71. $this->config->saveSyncEnabled($sync);
  72. $this->messageManager->addSuccessMessage(__('Settings saved successfully, stream removed.'));
  73. }
  74. } catch (LocalizedException $e) {
  75. $this->messageManager->addExceptionMessage($e);
  76. $resultRedirect->setPath('temando/settings_advanced/edit');
  77. return $resultRedirect;
  78. }
  79. $resultRedirect->setPath('adminhtml/system_config/edit', [
  80. 'section' => 'carriers',
  81. '_fragment' => 'carriers_temando-link',
  82. ]);
  83. return $resultRedirect;
  84. }
  85. }