![]() 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/ConfigurationDirection/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\ConfigurationDirection; use Extmag\Shiplab\Api\ConfigurationDirectionRepositoryInterface; use Extmag\Shiplab\Api\ConfigurationScopeRepositoryInterface; use Extmag\Shiplab\Api\ConfigurationStoreRepositoryInterface; use Extmag\Shiplab\Model\ConfigurationDirection; use Extmag\Shiplab\Model\ConfigurationDirectionFactory; use Extmag\Shiplab\Model\ConfigurationScopeFactory; use Exception; use Magento\Backend\App\Action; use Magento\Backend\Model\View\Result\Redirect; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\App\ResourceConnection; 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_configurationdirection_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var ConfigurationDirectionFactory */ private $modelFactory; /** * @var ConfigurationDirectionRepositoryInterface */ private $modelRepository; /** * @var ConfigurationScopeFactory */ private $scopeFactory; /** * @var ConfigurationStoreRepositoryInterface */ private $storeRepository; /** * @var ConfigurationScopeRepositoryInterface */ private $scopeRepository; /** * @var ConfigurationDirectionRepositoryInterface */ private $saveTransaction; /** * @param Action\Context $context * @param PostDataProcessor $dataProcessor * @param DataPersistorInterface $dataPersistor * @param ResourceConnection $transactionFactory * @param ConfigurationDirectionFactory $modelFactory * @param ConfigurationDirectionRepositoryInterface $modelRepository * @param ConfigurationScopeFactory $scopeFactory * @param ConfigurationScopeRepositoryInterface $scopeRepository * @param ConfigurationStoreRepositoryInterface $storeRepository */ public function __construct( Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, ResourceConnection $transactionFactory, ConfigurationDirectionFactory $modelFactory, ConfigurationDirectionRepositoryInterface $modelRepository, ConfigurationScopeFactory $scopeFactory, ConfigurationScopeRepositoryInterface $scopeRepository, ConfigurationStoreRepositoryInterface $storeRepository ) { $this->dataProcessor = $dataProcessor; $this->dataPersistor = $dataPersistor; $this->modelFactory = $modelFactory; $this->modelRepository = $modelRepository; $this->saveTransaction = $transactionFactory->getConnection(); $this->scopeFactory = $scopeFactory; $this->scopeRepository = $scopeRepository; $this->storeRepository = $storeRepository; 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; } $searchUniqueQuery = []; if (!empty($data['directions_ids'])) { foreach ($data['directions_ids'] as $store_id) { $searchUniqueQuery[] = ['finset' => $store_id]; } $data['directions_ids'] = implode(",", $data['directions_ids']); } $modelSearchUnique = $this->modelFactory->create()->getCollection() ->addFieldToFilter('store_id', $data['store_id'] ?? -1) ->addFieldToFilter('directions_ids', $searchUniqueQuery); /** @var ConfigurationDirection $model */ $id = $this->getRequest()->getParam('entity_id'); if ($id) { try { $model = $this->modelRepository->getById($id); $modelSearchUnique->addFieldToFilter('entity_id', ['neq' => $model->getId()]); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage(__('This direction configuration scope no longer exists.')); return $resultRedirect->setPath('*/configurationScope/'); } } else { $model = $this->modelFactory->create(); } if (count($modelSearchUnique) > 0) { $this->messageManager->addExceptionMessage( new Exception('One or more directions selected already use'), __('One or more directions selected already use.') ); } else { $model->setData($data); $this->_eventManager->dispatch( 'extmag_shiplab_configurationdirection_prepare_save', ['direction' => $model, 'request' => $this->getRequest()] ); if (!$this->dataProcessor->validateRequireEntry($data)) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } try { $this->saveTransaction->beginTransaction(); $this->modelRepository->save($model); if (!$id) { $storeRepositoryModel = $this->storeRepository->getById($model->getStoreId()); if ($storeRepositoryModel) { $scopeSearch = $this->scopeFactory->create()->getCollection() ->addFieldToFilter('store', $model->getStoreId()) ->addFieldToFilter('direction', ['null' => true]); $scopeModel = null; if (count($scopeSearch) > 0) { $scopeModel = $scopeSearch->getFirstItem(); $scopeModel->setDirection($model->getId()); } else { $scopeModel = $this->scopeFactory->create(); $scopeModel->setData(['direction' => $model->getId(), "store" => $model->getStoreId()]); } $this->scopeRepository->save($scopeModel); } else { throw new Exception('Store is not found'); } } else { $scopeSearch = $this->scopeFactory->create()->getCollection() ->addFieldToFilter('direction', $model->getId()); if (count($scopeSearch) > 0) { foreach ($scopeSearch as $scope) { $scope->setStore($model->getStoreId()); $this->scopeRepository->save($scope); } } } $this->saveTransaction->commit(); $this->messageManager->addSuccessMessage(__('You saved the direction configuration scope.')); return $this->processResultRedirect($model, $resultRedirect); } catch (LocalizedException $e) { $this->saveTransaction->rollBack(); $this->messageManager->addExceptionMessage($e->getPrevious() ?: $e); } catch (Exception $e) { $this->saveTransaction->rollBack(); $this->messageManager->addExceptionMessage( $e, __( 'Something went wrong while saving the direction configuration scope.' ) ); } } $this->dataPersistor->set('extmag_shiplab_configurationdirection', $data); return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('entity_id')]); } return $resultRedirect->setPath('*/configurationScope/'); } /** * @param ConfigurationDirection $model * @param Redirect $resultRedirect * @return Redirect */ private function processResultRedirect($model, $resultRedirect) { $this->dataPersistor->clear('extmag_shiplab_configurationdirection'); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/configurationScope/'); } }