-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Save.php
114 lines (106 loc) · 4.63 KB
/
Save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;
class Save extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote
{
/**
* Promo quote save action
*
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
if ($this->getRequest()->getPostValue()) {
try {
/** @var $model \Magento\SalesRule\Model\Rule */
$model = $this->_objectManager->create(\Magento\SalesRule\Model\Rule::class);
$this->_eventManager->dispatch(
'adminhtml_controller_salesrule_prepare_save',
['request' => $this->getRequest()]
);
$data = $this->getRequest()->getPostValue();
$filterValues = ['from_date' => $this->_dateFilter];
if ($this->getRequest()->getParam('to_date')) {
$filterValues['to_date'] = $this->_dateFilter;
}
$inputFilter = new \Zend_Filter_Input(
$filterValues,
[],
$data
);
$data = $inputFilter->getUnescaped();
$id = $this->getRequest()->getParam('rule_id');
if ($id) {
$model->load($id);
if ($id != $model->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
}
}
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
$validateResult = $model->validateData(new \Magento\Framework\DataObject($data));
if ($validateResult !== true) {
foreach ($validateResult as $errorMessage) {
$this->messageManager->addError($errorMessage);
}
$session->setPageData($data);
$this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
return;
}
if (isset(
$data['simple_action']
) && $data['simple_action'] == 'by_percent' && isset(
$data['discount_amount']
)
) {
$data['discount_amount'] = min(100, $data['discount_amount']);
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = $data['rule']['conditions'];
}
if (isset($data['rule']['actions'])) {
$data['actions'] = $data['rule']['actions'];
}
unset($data['rule']);
$model->loadPost($data);
$useAutoGeneration = (int)(
!empty($data['use_auto_generation']) && $data['use_auto_generation'] !== 'false'
);
$model->setUseAutoGeneration($useAutoGeneration);
$session->setPageData($model->getData());
$model->save();
$this->messageManager->addSuccess(__('You saved the rule.'));
$session->setPageData(false);
if ($this->getRequest()->getParam('back')) {
$this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
return;
}
$this->_redirect('sales_rule/*/');
return;
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
$id = (int)$this->getRequest()->getParam('rule_id');
if (!empty($id)) {
$this->_redirect('sales_rule/*/edit', ['id' => $id]);
} else {
$this->_redirect('sales_rule/*/new');
}
return;
} catch (\Exception $e) {
$this->messageManager->addError(
__('Something went wrong while saving the rule data. Please review the error log.')
);
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
$this->_objectManager->get(\Magento\Backend\Model\Session::class)->setPageData($data);
$this->_redirect('sales_rule/*/edit', ['id' => $this->getRequest()->getParam('rule_id')]);
return;
}
}
$this->_redirect('sales_rule/*/');
}
}