Save.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\CheckoutAgreements\Controller\Adminhtml\Agreement;
  8. use Magento\CheckoutAgreements\Controller\Adminhtml\Agreement;
  9. use Magento\CheckoutAgreements\Model\AgreementFactory;
  10. use Magento\Backend\App\Action\Context;
  11. use Magento\Framework\Registry;
  12. use Magento\Framework\App\ObjectManager;
  13. use Magento\Framework\DataObject;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Framework\App\Action\HttpPostActionInterface;
  16. class Save extends Agreement implements HttpPostActionInterface
  17. {
  18. /**
  19. * @var AgreementFactory
  20. */
  21. private $agreementFactory;
  22. /**
  23. * @param Context $context
  24. * @param Registry $coreRegistry
  25. * @param AgreementFactory $agreementFactory
  26. */
  27. public function __construct(
  28. Context $context,
  29. Registry $coreRegistry,
  30. AgreementFactory $agreementFactory = null
  31. ) {
  32. $this->agreementFactory = $agreementFactory ?:
  33. ObjectManager::getInstance()->get(AgreementFactory::class);
  34. parent::__construct($context, $coreRegistry);
  35. }
  36. /**
  37. * @return void
  38. */
  39. public function execute()
  40. {
  41. $postData = $this->getRequest()->getPostValue();
  42. if ($postData) {
  43. $model = $this->agreementFactory->create();
  44. $model->setData($postData);
  45. try {
  46. $validationResult = $model->validateData(new DataObject($postData));
  47. if ($validationResult !== true) {
  48. foreach ($validationResult as $message) {
  49. $this->messageManager->addError($message);
  50. }
  51. } else {
  52. $model->save();
  53. $this->messageManager->addSuccess(__('You saved the condition.'));
  54. $this->_redirect('checkout/*/');
  55. return;
  56. }
  57. } catch (LocalizedException $e) {
  58. $this->messageManager->addError($e->getMessage());
  59. } catch (\Exception $e) {
  60. $this->messageManager->addError(__('Something went wrong while saving this condition.'));
  61. }
  62. $this->_session->setAgreementData($postData);
  63. $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
  64. }
  65. }
  66. }