Skip to content

Commit

Permalink
ENGCOM-4215: Entered data missing when entering the wrong date for fr…
Browse files Browse the repository at this point in the history
…om, to in cart rule #20895
  • Loading branch information
sidolov authored Feb 14, 2019
2 parents a41c3ea + 97c853b commit 3b761f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\SalesRule\Controller\Adminhtml\Promo\Quote;

use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

/**
Expand All @@ -20,25 +21,35 @@ class Save extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote implement
* @var TimezoneInterface
*/
private $timezone;

/**
* @var DataPersistorInterface
*/
private $dataPersistor;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
* @param TimezoneInterface $timezone
* @param DataPersistorInterface $dataPersistor
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
TimezoneInterface $timezone = null
TimezoneInterface $timezone = null,
DataPersistorInterface $dataPersistor = null
) {
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
$this->timezone = $timezone ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
TimezoneInterface::class
);
$this->dataPersistor = $dataPersistor ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
DataPersistorInterface::class
);
}

/**
Expand Down Expand Up @@ -73,12 +84,8 @@ public function execute()
$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.'));
}
if (!$this->checkRuleExists($model)) {
throw new \Magento\Framework\Exception\LocalizedException(__('The wrong rule is specified.'));
}

$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
Expand All @@ -89,6 +96,7 @@ public function execute()
$this->messageManager->addErrorMessage($errorMessage);
}
$session->setPageData($data);
$this->dataPersistor->set('sale_rule', $data);
$this->_redirect('sales_rule/*/edit', ['id' => $model->getId()]);
return;
}
Expand Down Expand Up @@ -147,4 +155,22 @@ public function execute()
}
$this->_redirect('sales_rule/*/');
}

/**
* Check if Cart Price Rule with provided id exists.
*
* @param \Magento\SalesRule\Model\Rule $model
* @return bool
*/
private function checkRuleExists(\Magento\SalesRule\Model\Rule $model): bool
{
$id = $this->getRequest()->getParam('rule_id');
if ($id) {
$model->load($id);
if ($model->getId() != $id) {
return false;
}
}
return true;
}
}
22 changes: 20 additions & 2 deletions app/code/Magento/SalesRule/Model/Rule/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\SalesRule\Model\Rule;

use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\SalesRule\Model\ResourceModel\Rule\Collection;
use Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory;
use Magento\SalesRule\Model\Rule;
Expand Down Expand Up @@ -36,6 +37,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
*/
protected $metadataValueProvider;

/**
* @var DataPersistorInterface
*/
private $dataPersistor;

/**
* Initialize dependencies.
*
Expand All @@ -47,6 +53,7 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
* @param Metadata\ValueProvider $metadataValueProvider
* @param array $meta
* @param array $data
* @param DataPersistorInterface $dataPersistor
*/
public function __construct(
$name,
Expand All @@ -56,12 +63,16 @@ public function __construct(
\Magento\Framework\Registry $registry,
\Magento\SalesRule\Model\Rule\Metadata\ValueProvider $metadataValueProvider,
array $meta = [],
array $data = []
array $data = [],
DataPersistorInterface $dataPersistor = null
) {
$this->collection = $collectionFactory->create();
$this->coreRegistry = $registry;
$this->metadataValueProvider = $metadataValueProvider;
$meta = array_replace_recursive($this->getMetadataValues(), $meta);
$this->dataPersistor = $dataPersistor ?? \Magento\Framework\App\ObjectManager::getInstance()->get(
DataPersistorInterface::class
);
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

Expand All @@ -77,7 +88,7 @@ protected function getMetadataValues()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getData()
{
Expand All @@ -93,6 +104,13 @@ public function getData()

$this->loadedData[$rule->getId()] = $rule->getData();
}
$data = $this->dataPersistor->get('sale_rule');
if (!empty($data)) {
$rule = $this->collection->getNewEmptyItem();
$rule->setData($data);
$this->loadedData[$rule->getId()] = $rule->getData();
$this->dataPersistor->clear('sale_rule');
}

return $this->loadedData;
}
Expand Down

0 comments on commit 3b761f1

Please sign in to comment.