![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Controller/Adminhtml/PriceRules/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\PriceRules; use Exception; use Extmag\Shiplab\Api\PriceRulesRepositoryInterface; use Extmag\Shiplab\Model\PriceRules; use Extmag\Shiplab\Model\PriceRulesFactory; use Magento\Backend\App\Action; use Magento\Backend\Model\View\Result\Redirect; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Stdlib\DateTime\DateTime; class Save extends Action { /** * Authorization level of a basic admin session * * @see _isAllowed() */ public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_price_rules_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var PriceRulesFactory */ protected $modelFactory; /** * @var PriceRulesRepositoryInterface */ protected $modelRepository; /** * @var DateTime */ protected $dateTime; /** * @param Action\Context $context * @param PostDataProcessor $dataProcessor * @param DataPersistorInterface $dataPersistor * @param PriceRulesFactory $modelFactory * @param PriceRulesRepositoryInterface $modelRepository * @param DateTime $dateTime */ public function __construct( Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, PriceRulesFactory $modelFactory, PriceRulesRepositoryInterface $modelRepository, DateTime $dateTime ) { $this->dataProcessor = $dataProcessor; $this->dataPersistor = $dataPersistor; $this->modelFactory = $modelFactory; $this->modelRepository = $modelRepository; parent::__construct($context); $this->dateTime = $dateTime; } /** * Save action * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @return ResultInterface */ public function execute() { $data = $this->getRequest()->getPostValue(); /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($data) { $data = $this->dataProcessor->filter($data); if (empty($data['entity_id'])) { $data['entity_id'] = null; } if (isset($data['rule']['conditions'])) { $data['conditions'] = $data['rule']['conditions']; } unset($data['rule']); if (!empty($data['day_week'])) { $data['day_week'] = implode(",", $data['day_week']); } else { $data['day_week'] = ""; } if (empty($data['date_from']) && isset($data['date_to'])) { unset($data['date_from']); } if (empty($data['date_to']) && isset($data['date_to'])) { unset($data['date_to']); } if (!empty($data['frequency'])) { if (!empty($data['date_to'])) { if (!empty($data['date_from']) && $data['date_from'] >= $data['date_to']) { $this->messageManager->addWarningMessage(__('Date From cannot be greater than Date To.')); } elseif ($data['date_to'] < $this->dateTime->gmtDate('Y-m-d\TH:i:s.000\Z')) { $this->messageManager->addWarningMessage(__('Date To cannot be less than the current date.')); } } } $session = $this->_getSession(); /** @var PriceRules $model */ $id = $this->getRequest()->getParam('entity_id'); if ($id) { try { $model = $this->modelRepository->getById($id); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage(__('This rule no longer exists.')); return $resultRedirect->setPath('*/*/'); } } else { $model = $this->modelFactory->create(); } try { $model->loadPost($data); $session->setPageData($model->getData()); if (!$this->dataProcessor->validateRequireEntry($data)) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } $this->modelRepository->save($model); $this->_eventManager->dispatch( 'extmag_shiplab_pricerules_prepare_save', ['model' => $model, 'status' => 'success'] ); $this->messageManager->addSuccessMessage(__('You saved the rule.')); $session->setPageData(false); return $this->processResultRedirect($model, $resultRedirect, $data); } catch (LocalizedException $e) { $this->messageManager->addExceptionMessage($e->getPrevious() ?: $e); } catch (Exception $e) { $this->messageManager->addExceptionMessage( $e, __('Something went wrong while saving the rule.') ); } $this->dataPersistor->set('extmag_shiplab_pricerules', $data); return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('entity_id')]); } return $resultRedirect->setPath('*/*/'); } /** * Process result redirect * * @param PriceRules $model * @param Redirect $resultRedirect * @param array $data * @return Redirect * @throws LocalizedException */ private function processResultRedirect($model, $resultRedirect, $data) { if ($this->getRequest()->getParam('back', false) === 'duplicate') { $data['title'] = $data['title'] . " (duplicate)"; $newPage = $this->modelFactory->create(); $newPage->setData($data); $newPage->setId(null); $newPage = $this->modelRepository->save($newPage); $this->messageManager->addSuccessMessage(__('You duplicated the rule.')); return $resultRedirect->setPath( '*/*/edit', [ 'entity_id' => $newPage->getId(), '_current' => true, ] ); } $this->dataPersistor->clear('extmag_shiplab_pricerules'); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/*/'); } }