-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSave.php
125 lines (105 loc) · 4.73 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
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future.
*
* @category Smile
* @package Smile\Retailer
* @author Romain Ruaud <[email protected]>
* @copyright 2016 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\Retailer\Controller\Adminhtml\Retailer;
use Smile\Retailer\Controller\Adminhtml\AbstractRetailer;
use Smile\Seller\Api\Data\SellerInterface;
/**
* Retailer Adminhtml Save controller.
*
* @category Smile
* @package Smile\Retailer
* @author Romain Ruaud <[email protected]>
*/
class Save extends AbstractRetailer
{
/**
* @var \Smile\Retailer\Model\Retailer\PostDataHandlerInterface[]
*/
private $postDataHandlers;
/**
* Constructor.
*
* @param \Magento\Backend\App\Action\Context $context Application context.
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory Result Page factory.
* @param \Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory Result forward factory.
* @param \Magento\Framework\Registry $coreRegistry Application registry.
* @param \Smile\Retailer\Api\RetailerRepositoryInterface $retailerRepository Retailer Repository
* @param \Smile\Retailer\Api\Data\RetailerInterfaceFactory $retailerFactory Retailer Factory.
* @param \Smile\Retailer\Model\Retailer\PostDataHandlerInterface[] $postDataHandlers Form data handlers.
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\Magento\Framework\Controller\Result\ForwardFactory $resultForwardFactory,
\Magento\Framework\Registry $coreRegistry,
\Smile\Retailer\Api\RetailerRepositoryInterface $retailerRepository,
\Smile\Retailer\Api\Data\RetailerInterfaceFactory $retailerFactory,
array $postDataHandlers = []
) {
parent::__construct($context, $resultPageFactory, $resultForwardFactory, $coreRegistry, $retailerRepository, $retailerFactory);
$this->postDataHandlers = $postDataHandlers;
}
/**
* {@inheritdoc}
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
$redirectBack = $this->getRequest()->getParam('back', false);
if ($data) {
$identifier = $this->getRequest()->getParam('id');
$storeId = $this->getRequest()->getParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID);
$model = $this->retailerFactory->create();
$media = '';
if(isset($data[SellerInterface::MEDIA_PATH][0]['name'])) {
$media = $data[SellerInterface::MEDIA_PATH][0]['name'];
}
unset($data[SellerInterface::MEDIA_PATH]);
if ($identifier) {
$model = $this->retailerRepository->get($identifier);
if (!$model->getId()) {
$this->messageManager->addError(__('This retailer no longer exists.'));
return $resultRedirect->setPath('*/*/');
}
}
foreach ($this->postDataHandlers as $handler) {
$data = $handler->getData($model, $data);
}
$model->setData($data);
$model->setStoreId($storeId);
$model->setMediaPath($media);
try {
$this->retailerRepository->save($model);
$this->messageManager->addSuccessMessage(__('You saved the retailer %1.', $model->getName()));
$this->_objectManager->get('Magento\Backend\Model\Session')->setFormData(false);
if ($redirectBack) {
$redirectParams = ['id' => $model->getId()];
if (null !== $storeId) {
$redirectParams['store'] = $storeId;
}
return $resultRedirect->setPath('*/*/edit', $redirectParams);
}
return $resultRedirect->setPath('*/*/');
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->_objectManager->get('Magento\Backend\Model\Session')->setFormData($data);
$returnParams = ['id' => $model->getId()];
return $resultRedirect->setPath('*/*/edit', $returnParams);
}
}
return $resultRedirect->setPath('*/*/');
}
}