Edit.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\CheckoutAgreements\Block\Adminhtml\Agreement\Edit as BlockEdit;
  14. use Magento\Framework\App\Action\HttpGetActionInterface;
  15. class Edit extends Agreement implements HttpGetActionInterface
  16. {
  17. /**
  18. * @var AgreementFactory
  19. */
  20. private $agreementFactory;
  21. /**
  22. * @param Context $context
  23. * @param Registry $coreRegistry
  24. * @param AgreementFactory $agreementFactory
  25. */
  26. public function __construct(
  27. Context $context,
  28. Registry $coreRegistry,
  29. AgreementFactory $agreementFactory = null
  30. ) {
  31. $this->agreementFactory = $agreementFactory ?:
  32. ObjectManager::getInstance()->get(AgreementFactory::class);
  33. parent::__construct($context, $coreRegistry);
  34. }
  35. /**
  36. * @return void
  37. * @SuppressWarnings(PHPMD.NPathComplexity)
  38. */
  39. public function execute()
  40. {
  41. $id = $this->getRequest()->getParam('id');
  42. $agreementModel = $this->agreementFactory->create();
  43. if ($id) {
  44. $agreementModel->load($id);
  45. if (!$agreementModel->getId()) {
  46. $this->messageManager->addError(__('This condition no longer exists.'));
  47. $this->_redirect('checkout/*/');
  48. return;
  49. }
  50. }
  51. $data = $this->_session->getAgreementData(true);
  52. if (!empty($data)) {
  53. $agreementModel->setData($data);
  54. }
  55. $this->_coreRegistry->register('checkout_agreement', $agreementModel);
  56. $this->_initAction()->_addBreadcrumb(
  57. $id ? __('Edit Condition') : __('New Condition'),
  58. $id ? __('Edit Condition') : __('New Condition')
  59. )->_addContent(
  60. $this->_view->getLayout()->createBlock(
  61. BlockEdit::class
  62. )->setData(
  63. 'action',
  64. $this->getUrl('checkout/*/save')
  65. )
  66. );
  67. $this->_view->getPage()->getConfig()->getTitle()->prepend(__('Terms and Conditions'));
  68. $this->_view->getPage()->getConfig()->getTitle()->prepend(
  69. $agreementModel->getId() ? $agreementModel->getName() : __('New Condition')
  70. );
  71. $this->_view->renderLayout();
  72. }
  73. }