![]() 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/PriceRules/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Controller\Adminhtml\PriceRules; use Extmag\Shiplab\Model\PriceRules; use Extmag\Shiplab\Api\PriceRulesRepositoryInterface as PriceRulesRepository; use Extmag\Shiplab\Api\Data\PriceRulesInterface; use Exception; use Magento\Backend\App\Action; use Magento\Backend\App\Action\Context; use Magento\Framework\Controller\Result\Json; use Magento\Framework\Controller\Result\JsonFactory; 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_price_rules_save'; /** * @var PostDataProcessor */ protected $dataProcessor; /** * @var PriceRulesRepository */ protected $priceRulesRepository; /** * @var JsonFactory */ protected $jsonFactory; /** * @param Context $context * @param PostDataProcessor $dataProcessor * @param PriceRulesRepository $priceRulesRepository * @param JsonFactory $jsonFactory */ public function __construct( Context $context, PostDataProcessor $dataProcessor, PriceRulesRepository $priceRulesRepository, JsonFactory $jsonFactory ) { parent::__construct($context); $this->dataProcessor = $dataProcessor; $this->priceRulesRepository = $priceRulesRepository; $this->jsonFactory = $jsonFactory; } /** * @return ResultInterface * @throws LocalizedException */ public function execute() { /** @var Json $resultJson */ $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 PriceRules $address */ $address = $this->priceRulesRepository->getById($pageId); try { $itemData = $postItems[$pageId]; if (!$this->dataProcessor->validateRequireEntry($itemData)) { $messages[] = $this->getErrorWithItemId( $address, __('Something went wrong while saving the rule.') ); $error = true; } $extendedData = $address->getData(); $this->setEntityData($address, $extendedData, $itemData); $this->priceRulesRepository->save($address); } catch (LocalizedException | RuntimeException $e) { $messages[] = $this->getErrorWithItemId($address, $e->getMessage()); $error = true; } catch (Exception $e) { $messages[] = $this->getErrorWithItemId( $address, __('Something went wrong while saving the rule.') ); $error = true; } } return $resultJson->setData([ 'messages' => $messages, 'error' => $error ]); } /** * Add item title to error message * * @param PriceRulesInterface $address * @param string $errorText * @return string */ protected function getErrorWithItemId(PriceRulesInterface $address, $errorText) { return '[Price Rule ID: ' . $address->getId() . '] ' . $errorText; } /** * Set entity data * * @param PriceRules $address * @param array $extendedData * @param array $data * @return $this */ public function setEntityData(PriceRules $address, array $extendedData, array $data) { $address->setData(array_merge($address->getData(), $extendedData, $data)); return $this; } }