Save.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Group;
  8. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  9. use Magento\Customer\Api\Data\GroupInterfaceFactory;
  10. use Magento\Customer\Api\Data\GroupInterface;
  11. use Magento\Customer\Api\GroupRepositoryInterface;
  12. class Save extends \Magento\Customer\Controller\Adminhtml\Group implements HttpPostActionInterface
  13. {
  14. /**
  15. * @var \Magento\Framework\Reflection\DataObjectProcessor
  16. */
  17. protected $dataObjectProcessor;
  18. /**
  19. *
  20. * @param \Magento\Backend\App\Action\Context $context
  21. * @param \Magento\Framework\Registry $coreRegistry
  22. * @param GroupRepositoryInterface $groupRepository
  23. * @param GroupInterfaceFactory $groupDataFactory
  24. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  25. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  26. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  27. */
  28. public function __construct(
  29. \Magento\Backend\App\Action\Context $context,
  30. \Magento\Framework\Registry $coreRegistry,
  31. GroupRepositoryInterface $groupRepository,
  32. GroupInterfaceFactory $groupDataFactory,
  33. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  34. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  35. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  36. ) {
  37. $this->dataObjectProcessor = $dataObjectProcessor;
  38. parent::__construct(
  39. $context,
  40. $coreRegistry,
  41. $groupRepository,
  42. $groupDataFactory,
  43. $resultForwardFactory,
  44. $resultPageFactory
  45. );
  46. }
  47. /**
  48. * Store Customer Group Data to session
  49. *
  50. * @param array $customerGroupData
  51. * @return void
  52. */
  53. protected function storeCustomerGroupDataToSession($customerGroupData)
  54. {
  55. if (array_key_exists('code', $customerGroupData)) {
  56. $customerGroupData['customer_group_code'] = $customerGroupData['code'];
  57. unset($customerGroupData['code']);
  58. }
  59. $this->_getSession()->setCustomerGroupData($customerGroupData);
  60. }
  61. /**
  62. * Create or save customer group.
  63. *
  64. * @return \Magento\Backend\Model\View\Result\Redirect|\Magento\Backend\Model\View\Result\Forward
  65. */
  66. public function execute()
  67. {
  68. $taxClass = (int)$this->getRequest()->getParam('tax_class');
  69. /** @var \Magento\Customer\Api\Data\GroupInterface $customerGroup */
  70. $customerGroup = null;
  71. if ($taxClass) {
  72. $id = $this->getRequest()->getParam('id');
  73. $resultRedirect = $this->resultRedirectFactory->create();
  74. try {
  75. $customerGroupCode = (string)$this->getRequest()->getParam('code');
  76. if ($id !== null) {
  77. $customerGroup = $this->groupRepository->getById((int)$id);
  78. $customerGroupCode = $customerGroupCode ?: $customerGroup->getCode();
  79. } else {
  80. $customerGroup = $this->groupDataFactory->create();
  81. }
  82. $customerGroup->setCode(!empty($customerGroupCode) ? $customerGroupCode : null);
  83. $customerGroup->setTaxClassId($taxClass);
  84. $this->groupRepository->save($customerGroup);
  85. $this->messageManager->addSuccess(__('You saved the customer group.'));
  86. $resultRedirect->setPath('customer/group');
  87. } catch (\Exception $e) {
  88. $this->messageManager->addError($e->getMessage());
  89. if ($customerGroup != null) {
  90. $this->storeCustomerGroupDataToSession(
  91. $this->dataObjectProcessor->buildOutputDataArray(
  92. $customerGroup,
  93. \Magento\Customer\Api\Data\GroupInterface::class
  94. )
  95. );
  96. }
  97. $resultRedirect->setPath('customer/group/edit', ['id' => $id]);
  98. }
  99. return $resultRedirect;
  100. } else {
  101. return $this->resultForwardFactory->create()->forward('new');
  102. }
  103. }
  104. }