Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
8 
10 {
18  public function execute()
19  {
20  if ($this->getRequest()->getPostValue()) {
21  try {
23  $model = $this->_objectManager->create(\Magento\SalesRule\Model\Rule::class);
24  $this->_eventManager->dispatch(
25  'adminhtml_controller_salesrule_prepare_save',
26  ['request' => $this->getRequest()]
27  );
28  $data = $this->getRequest()->getPostValue();
29 
30  $filterValues = ['from_date' => $this->_dateFilter];
31  if ($this->getRequest()->getParam('to_date')) {
32  $filterValues['to_date'] = $this->_dateFilter;
33  }
34  $inputFilter = new \Zend_Filter_Input(
35  $filterValues,
36  [],
37  $data
38  );
39  $data = $inputFilter->getUnescaped();
40  $id = $this->getRequest()->getParam('rule_id');
41  if ($id) {
42  $model->load($id);
43  if ($id != $model->getId()) {
44  throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
45  }
46  }
47 
48  $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
49 
50  $validateResult = $model->validateData(new \Magento\Framework\DataObject($data));
51  if ($validateResult !== true) {
52  foreach ($validateResult as $errorMessage) {
53  $this->messageManager->addErrorMessage($errorMessage);
54  }
55  $session->setPageData($data);
56  $this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
57  return;
58  }
59 
60  if (isset(
61  $data['simple_action']
62  ) && $data['simple_action'] == 'by_percent' && isset(
63  $data['discount_amount']
64  )
65  ) {
66  $data['discount_amount'] = min(100, $data['discount_amount']);
67  }
68  if (isset($data['rule']['conditions'])) {
69  $data['conditions'] = $data['rule']['conditions'];
70  }
71  if (isset($data['rule']['actions'])) {
72  $data['actions'] = $data['rule']['actions'];
73  }
74  unset($data['rule']);
75  $model->loadPost($data);
76 
77  $useAutoGeneration = (int)(
78  !empty($data['use_auto_generation']) && $data['use_auto_generation'] !== 'false'
79  );
80  $model->setUseAutoGeneration($useAutoGeneration);
81 
82  $session->setPageData($model->getData());
83 
84  $model->save();
85  $this->messageManager->addSuccessMessage(__('You saved the rule.'));
86  $session->setPageData(false);
87  if ($this->getRequest()->getParam('back')) {
88  $this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
89  return;
90  }
91  $this->_redirect('sales_rule/*/');
92  return;
93  } catch (\Magento\Framework\Exception\LocalizedException $e) {
94  $this->messageManager->addErrorMessage($e->getMessage());
95  $id = (int)$this->getRequest()->getParam('rule_id');
96  if (!empty($id)) {
97  $this->_redirect('sales_rule/*/edit', ['id' => $id]);
98  } else {
99  $this->_redirect('sales_rule/*/new');
100  }
101  return;
102  } catch (\Exception $e) {
103  $this->messageManager->addErrorMessage(
104  __('Something went wrong while saving the rule data. Please review the error log.')
105  );
106  $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
107  $this->_objectManager->get(\Magento\Backend\Model\Session::class)->setPageData($data);
108  $this->_redirect('sales_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
109  return;
110  }
111  }
112  $this->_redirect('sales_rule/*/');
113  }
114 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13