![]() 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/ShippingMethods/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\ShippingMethods; use Extmag\Shiplab\Api\ShippingMethodsRepositoryInterface; use Extmag\Shiplab\Api\ShippingMethodsRepositoryRepositoryInterface; use Extmag\Shiplab\Model\ShippingMethods; use Extmag\Shiplab\Model\ShippingMethodsFactory; use Exception; 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; class Save extends Action { /** * Authorization level of a basic admin session * * @see _isAllowed() */ public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_shippingmethods_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var ShippingMethodsFactory */ private $modelFactory; /** * @var ShippingMethodsRepositoryInterface */ private $modelRepository; /** * @param Action\Context $context * @param PostDataProcessor $dataProcessor * @param DataPersistorInterface $dataPersistor * @param ShippingMethodsFactory $modelFactory * @param ShippingMethodsRepositoryInterface $modelRepository */ public function __construct( Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, ShippingMethodsFactory $modelFactory, ShippingMethodsRepositoryInterface $modelRepository ) { $this->dataProcessor = $dataProcessor; $this->dataPersistor = $dataPersistor; $this->modelFactory = $modelFactory; $this->modelRepository = $modelRepository; parent::__construct($context); } /** * @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 (!empty($data['carrier_methods'])) { $data['carrier_methods'] = implode(",", array_unique($data['carrier_methods'])); } if (!empty($data['website_ids'])) { $data['website_ids'] = implode(",", $data['website_ids']); } if (!empty($data['customer_group_ids'])) { $data['customer_group_ids'] = implode(",", $data['customer_group_ids']); } if (isset($data['rule']['conditions'])) { $data['conditions'] = $data['rule']['conditions']; } if (isset($data['rule']['actions'])) { $data['actions'] = $data['rule']['actions']; } if (isset($data['rule'])) { unset($data['rule']); } if (!empty($data['price_rule_ids'])) { $data['price_rule_ids'] = json_encode($data['price_rule_ids']); } else { $data['price_rule_ids'] = ""; } if (!empty($data['methods_names_mapping'])) { $methodsNamesMapping = $data['methods_names_mapping']; $methodsNames = []; foreach ($methodsNamesMapping as $method) { $methodsNames[$method['origin_name']] = $method['custom_name']; } $data['methods_names_mapping'] = json_encode($methodsNames); } else { $data['methods_names_mapping'] = "[]"; } 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['day_week'])) { $data['day_week'] = implode(",", $data['day_week']); } else { $data['day_week'] = ""; } if (!empty($data['logo']) && is_array($data['logo'])) { $data['logo'] = json_encode($data['logo']); } $data = $this->dataProcessorByCarrier($data); $session = $this->_getSession(); /** @var ShippingMethods $model */ $id = $this->getRequest()->getParam('entity_id'); if ($id) { try { $model = $this->modelRepository->getById($id); if (!empty($data['carrier'])) { $data['carrier'] = $model->getCarrier(); } } catch (LocalizedException $e) { $this->messageManager->addErrorMessage(__('This shipping method 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_shippingmethods_prepare_save', ['model' => $model, 'status' => 'success'] ); $this->messageManager->addSuccessMessage(__('You saved the shipping method.')); $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 shipping method.') ); } $this->dataPersistor->set('extmag_shiplab_shippingmethods', $data); return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('entity_id')]); } return $resultRedirect->setPath('*/*/'); } public function dataProcessorByCarrier($data) { return $data; } /** * @param ShippingMethods $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['admin_title'] = $data['admin_title'] . " (duplicate)"; $newPage = $this->modelFactory->create(['data' => $data]); $newPage->setId(null); $this->modelRepository->save($newPage); $this->messageManager->addSuccessMessage(__('You duplicated the shipping method.')); return $resultRedirect->setPath( '*/*/edit', [ 'entity_id' => $newPage->getId(), '_current' => true, ] ); } $this->dataPersistor->clear('extmag_shiplab_shippingmethods'); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/*/'); } }