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/app/code/Cnc/AdminHistory/Plugin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/AdminHistory/Plugin/HelperPlugin.php
<?php
/**
 * Copyright (c) 20201 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Krzysztof Majkowski <[email protected]> <[email protected]>
 */
declare(strict_types=1);

namespace Cnc\AdminHistory\Plugin;

use Cnc\AdminHistory\Api\Data\ProductInterface as CncHistoryProductInterface;
use Cnc\AdminHistory\Model\ProductFactory;
use Cnc\AdminHistory\Model\ResourceModel\Product as ProductResource;
use Cnc\Catalog\Model\Attribute\MsiAttributes;
use Magento\Backend\Model\Auth\Session;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Framework\Stdlib\DateTime\DateTime;

class HelperPlugin
{
    /**
     * @var MsiAttributes
     */
    private $msiAttributes;

    /**
     * @var RequestInterface
     */
    private $request;

    /**
     * @var ProductFactory
     */
    private $productHistory;

    /**
     * @var ProductResource
     */
    private $resource;

    /**
     * @var Session
     */
    private $authSession;

    /**
     * @var DateTime
     */
    private $date;

    public function __construct(
        MsiAttributes $msiAttributes,
        RequestInterface $request,
        ProductFactory $productHistory,
        ProductResource $resource,
        Session $authSession,
        DateTime $date
    ) {
        $this->msiAttributes = $msiAttributes;
        $this->request = $request;
        $this->productHistory = $productHistory;
        $this->resource = $resource;
        $this->authSession = $authSession;
        $this->date = $date;
    }

    /**
     * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject
     * @param Product $product
     * @return array
     */
    public function beforeInitialize(
        \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject,
        Product $product
    ) {
        $this->saveChanges($product);

        return [$product];
    }

    public function saveChanges(Product $product)
    {
        $changedValues = [];
        $sources = $this->request->getParam('sources');
        if (is_array($sources) && isset($sources['assigned_sources'])) {
            foreach ($sources['assigned_sources'] as $source) {
                $originAvailability = null;
                $originAvailabilityText = '';

                $attributeId = $this->msiAttributes->getAttributeId('cnc_availability');
                $attributeValue = $this->msiAttributes->getAttributeValue($attributeId, $product->getSku(), $source['source_code']);
                if ($attributeValue && $attributeValue->getValue()) {
                    $originAvailability = $attributeValue->getValue();
                    $textValue = $this->msiAttributes->getDropdownValue($originAvailability);
                    $originAvailabilityText = ($textValue && $textValue->getValue() ?
                        $textValue->getValue() : ''
                    );
                }

                $currentAvailability = $source['cnc_availability'];
                $currentAvailabilityText = '';
                if ($currentAvailability && $currentAvailability > 0) {
                    $textValue = $this->msiAttributes->getDropdownValue($currentAvailability);
                    $currentAvailabilityText = ($textValue && $textValue->getValue() ?
                        $textValue->getValue() : ''
                    );
                }

                if ($currentAvailability != $originAvailability) {
                    $changedValues['cnc_availability_' . $source['source_code']] = [
                        CncHistoryProductInterface::OLD_VALUE => $originAvailabilityText,
                        CncHistoryProductInterface::NEW_VALUE => $currentAvailabilityText
                    ];
                }
            }
        }

        if (count($changedValues)) {
            $userName = $this->authSession->getUser()->getName();
            $productId = $product->getId();
            $updatedAt = $this->date->gmtDate();
            foreach ($changedValues as $attribute => $values) {
                try {
                    /** @var \Cnc\AdminHistory\Model\Product $productHistory */
                    $productHistory = $this->productHistory->create();
                    $productHistory->setAttributeCode($attribute);
                    if (is_string($values[CncHistoryProductInterface::OLD_VALUE])) {
                        $productHistory->setOldValue($values[CncHistoryProductInterface::OLD_VALUE]);
                    }
                    if (is_string($values[CncHistoryProductInterface::NEW_VALUE])) {
                        $productHistory->setNewValue($values[CncHistoryProductInterface::NEW_VALUE]);
                    }
                    $productHistory->setProductId($productId);
                    $productHistory->setUser($userName);
                    $productHistory->setUpdatedAt($updatedAt);
                    $this->resource->save($productHistory);
                } catch (AlreadyExistsException $e) {
                    throw new AlreadyExistsException(__('Could not save product history log: %1', $e->getMessage()));
                }
            }
        }
    }
}

Spamworldpro Mini