Spamworldpro Mini Shell
Spamworldpro


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/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/extmag/shiplab/Controller/Adminhtml/Address/InlineEdit.php
<?php
/**
 * Copyright © Extmag. All rights reserved.
 */

namespace Extmag\Shiplab\Controller\Adminhtml\Address;

use Extmag\Shiplab\Api\AddressRepositoryInterface as AddressRepository;
use Extmag\Shiplab\Api\Data\AddressInterface;
use Extmag\Shiplab\Model\Address;
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\LocalizedException;
use RuntimeException;

/**
 * Cms page grid inline edit controller
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class InlineEdit extends Action
{
    /**
     * Authorization level of a basic admin session
     */
    public const ADMIN_RESOURCE = 'Extmag_Shiplab::shiplab_address_save';

    /**
     * @var PostDataProcessor
     */
    protected $dataProcessor;

    /**
     * @var AddressRepository
     */
    protected $addressRepository;

    /**
     * @var JsonFactory
     */
    protected $jsonFactory;

    /**
     * @param Context $context
     * @param PostDataProcessor $dataProcessor
     * @param AddressRepository $modelRepository
     * @param JsonFactory $jsonFactory
     */
    public function __construct(
        Context $context,
        PostDataProcessor $dataProcessor,
        AddressRepository $modelRepository,
        JsonFactory $jsonFactory
    ) {
        parent::__construct($context);
        $this->dataProcessor = $dataProcessor;
        $this->addressRepository = $modelRepository;
        $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 Address $address */
            $address = $this->addressRepository->getById($pageId);
            try {
                $itemData = $postItems[$pageId];
                $itemData = $this->dataProcessor->filter($itemData);
                $this->validatePost($itemData, $address, $error, $messages);
                $extendedData = $address->getData();
                $this->setEntityData($address, $extendedData, $itemData);
                $this->addressRepository->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 address.')
                );
                $error = true;
            }
        }

        return $resultJson->setData(
            [
                'messages' => $messages,
                'error' => $error,
            ]
        );
    }

    /**
     * @param array $pageData
     * @param Address $model
     * @param bool $error
     * @param array $messages
     * @return void
     */
    protected function validatePost(array $pageData, Address $model, &$error, array &$messages)
    {
        if (!$this->dataProcessor->validateRequireEntry($pageData)) {
            $error = true;
            foreach ($this->messageManager->getMessages(true)->getItems() as $errorMessage) {
                $messages[] = $this->getErrorWithItemId($model, $errorMessage->getText());
            }
        }
    }

    /**
     * @param Address $address
     * @param array $extendedData
     * @param array $data
     * @return $this
     */
    public function setEntityData(Address $address, array $extendedData, array $data)
    {
        $address->setData(array_merge($address->getData(), $extendedData, $data));
        return $this;
    }

    /**
     * Add item title to error message
     *
     * @param AddressInterface $address
     * @param string $errorText
     * @return string
     */
    protected function getErrorWithItemId(AddressInterface $address, $errorText)
    {
        return '[Address ID: ' . $address->getId() . '] ' . $errorText;
    }
}

Spamworldpro Mini