![]() 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\Model\ShippingMethods; use Exception; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Extmag\Shiplab\Api\ShippingMethodsRepositoryInterface as ShippingMethodsRepository; use Magento\Framework\Controller\Result\JsonFactory; use Extmag\Shiplab\Api\Data\ShippingMethodsInterface; use Magento\Framework\Controller\ResultInterface; use Magento\Framework\Exception\LocalizedException; use RuntimeException; class InlineEdit extends Action { /** * Authorization level of a basic admin session */ public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_shippingmethods_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var ShippingMethodsRepository */ protected $shippingMethodsRepository; /** * @var JsonFactory */ protected $jsonFactory; /** * @param Context $context * @param PostDataProcessor $dataProcessor * @param ShippingMethodsRepository $shippingMethodsRepository * @param JsonFactory $jsonFactory */ public function __construct( Context $context, PostDataProcessor $dataProcessor, ShippingMethodsRepository $shippingMethodsRepository, JsonFactory $jsonFactory ) { parent::__construct($context); $this->dataProcessor = $dataProcessor; $this->shippingMethodsRepository = $shippingMethodsRepository; $this->jsonFactory = $jsonFactory; } /** * @return ResultInterface * @throws LocalizedException */ public function execute() { $resultJson = $this->jsonFactory->create(); $error = false; $messages = []; $postItems = $this->getRequest()->getParam('items', []); if (!($this->getRequest()->getParam('isAjax') && count($postItems))) { return $resultJson->setData([ 'messages' => [__('Please correct the data sent.')], 'error' => true, ]); } foreach (array_keys($postItems) as $pageId) { /** @var ShippingMethods $address */ $address = $this->shippingMethodsRepository->getById($pageId); try { $itemData = $postItems[$pageId]; $extendedData = $address->getData(); $this->setEntityData($address, $extendedData, $itemData); $this->shippingMethodsRepository->save($address); } catch (LocalizedException $e) { $messages[] = $this->getErrorWithItemId($address, $e->getMessage()); $error = true; } catch (RuntimeException $e) { $messages[] = $this->getErrorWithItemId($address, $e->getMessage()); $error = true; } catch (Exception $e) { $messages[] = $this->getErrorWithItemId( $address, __('Something went wrong while saving the shipping method.') ); $error = true; } } return $resultJson->setData([ 'messages' => $messages, 'error' => $error ]); } /** * Add item title to error message * * @param ShippingMethodsInterface $address * @param string $errorText * @return string */ protected function getErrorWithItemId(ShippingMethodsInterface $address, $errorText) { return '[Shipping Method ID: ' . $address->getId() . '] ' . $errorText; } /** * @param ShippingMethods $address * @param array $extendedData * @param array $data * @return $this */ public function setEntityData(ShippingMethods $address, array $extendedData, array $data) { $address->setData(array_merge($address->getData(), $extendedData, $data)); return $this; } }