![]() 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/Address/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\Address; use Extmag\Shiplab\Api\AddressRepositoryInterface; use Extmag\Shiplab\Model\Address; use Extmag\Shiplab\Model\AddressFactory; 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; use Magento\Framework\Exception\NoSuchEntityException; class Save extends Action { /** * Authorization level of a basic admin session * * @see _isAllowed() */ public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_address_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var DataPersistorInterface */ protected $dataPersistor; /** * @var AddressFactory */ private $modelFactory; /** * @var AddressRepositoryInterface */ private $modelRepository; /** * @param Action\Context $context * @param PostDataProcessor $dataProcessor * @param DataPersistorInterface $dataPersistor * @param AddressFactory $modelFactory * @param AddressRepositoryInterface $modelRepository */ public function __construct( Action\Context $context, PostDataProcessor $dataProcessor, DataPersistorInterface $dataPersistor, AddressFactory $modelFactory, AddressRepositoryInterface $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; } /** @var Address $model */ $id = $this->getRequest()->getParam('entity_id'); if ($id) { try { $model = $this->modelRepository->getById($id); } catch (NoSuchEntityException $e) { $this->messageManager->addErrorMessage(__('This address no longer exists.')); return $resultRedirect->setPath('*/*/'); } } else { $model = $this->modelFactory->create(); } try { if (!$this->dataProcessor->validateRequireEntry($data)) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $id, '_current' => true]); } $model->setData($data); $this->modelRepository->save($model); $this->_eventManager->dispatch( 'extmag_shiplab_address_after_save', ['model' => $model, 'status' => 'success'] ); $this->messageManager->addSuccessMessage(__('You saved the address.')); 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 address.')); } $this->dataPersistor->set('extmag_shiplab_address', $data); return $resultRedirect->setPath('*/*/edit', ['entity_id' => $this->getRequest()->getParam('entity_id')]); } return $resultRedirect->setPath('*/*/'); } /** * @param Address $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); $this->modelRepository->save($newPage); $this->messageManager->addSuccessMessage(__('You duplicated the address.')); return $resultRedirect->setPath( '*/*/edit', [ 'entity_id' => $newPage->getId(), '_current' => true, ] ); } $this->dataPersistor->clear('extmag_shiplab_address'); if ($this->getRequest()->getParam('back')) { return $resultRedirect->setPath('*/*/edit', ['entity_id' => $model->getId(), '_current' => true]); } return $resultRedirect->setPath('*/*/'); } }