![]() 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/ConfigurationStore/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\ConfigurationStore; use Extmag\Shiplab\Api\ConfigurationScopeRepositoryInterface; use Extmag\Shiplab\Api\ConfigurationStoreRepositoryInterface; use Extmag\Shiplab\Model\ConfigurationScopeFactory; use Extmag\Shiplab\Model\ConfigurationStore; use Extmag\Shiplab\Model\ConfigurationStoreFactory; 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_configurationstore_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var ConfigurationStoreFactory */ private $modelFactory; /** * @var ConfigurationScopeFactory */ private $scopeFactory; /** * @var ConfigurationStoreRepositoryInterface */ private $modelRepository; /** * @var ConfigurationScopeRepositoryInterface */ private $scopeRepository; /** * @var ConfigurationStoreRepositoryInterface */ private $saveTransaction; /** * @param Action\Context $context * @param PostDataProcessor $dataProcessor * @param DataPersistorInterface $dataPersistor * @param ResourceConnection $transactionFactory * @param ConfigurationStoreFactory|null $modelFactory * @param ConfigurationStoreRepositoryInterface|null $modelRepository * @param ConfigurationScopeFactory|null $scopeFactory * @param ConfigurationScopeRepositoryInterface|null $scopeRepository */ public function __construct( Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, ResourceConnection $transactionFactory, ConfigurationStoreFactory $modelFactory, ConfigurationStoreRepositoryInterface $modelRepository, ConfigurationScopeFactory $scopeFactory, ConfigurationScopeRepositoryInterface $scopeRepository ) { $this->dataProcessor = $dataProcessor; $this->dataPersistor = $dataPersistor; $this->modelFactory = $modelFactory; $this->modelRepository = $modelRepository; $this->saveTransaction = $transactionFactory->getConnection(); $this->scopeFactory = $scopeFactory; $this->scopeRepository = $scopeRepository; 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['stores_ids'])) { foreach ($data['stores_ids'] as $store_id) { $searchUniqueQuery[] = ['finset' => $store_id]; } $data['stores_ids'] = implode(",", $data['stores_ids']); } $modelSearchUnique = $this->modelFactory->create()->getCollection() ->addFieldToFilter('stores_ids', $searchUniqueQuery); /** @var ConfigurationStore $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 store 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 stores selected already use'), __('One or more stores selected already use.') ); } else { $model->setData($data); $this->_eventManager->dispatch( 'extmag_shiplab_configurationstore_prepare_save', ['store' => $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) { $scopeModel = $this->scopeFactory->create(); $scopeModel->setData(['store' => $model->getId()]); $this->scopeRepository->save($scopeModel); } $this->saveTransaction->commit(); $this->messageManager->addSuccessMessage(__('You saved the store 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 store configuration scope.' ) ); } } $this->dataPersistor->set('extmag_shiplab_configurationstore', $data); return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('entity_id')]); } return $resultRedirect->setPath('*/configurationScope/'); } /** * @param ConfigurationStore $model * @param Redirect $resultRedirect * @return Redirect */ private function processResultRedirect($model, $resultRedirect) { $this->dataPersistor->clear('extmag_shiplab_configurationstore'); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/configurationScope/'); } }