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/Sales/Ui/Component/Listing/Column/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Sales/Ui/Component/Listing/Column/CncStateOfWear.php
<?php
/**
 * @author    Radosław Łańcucki <[email protected]>
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @copyright Copyright (c) 2021 KDC (https://digitalcommerce.kaliop.com/)
 */
declare(strict_types=1);

namespace Cnc\Sales\Ui\Component\Listing\Column;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class CncStateOfWear extends Column
{
    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * @var AttributeRepositoryInterface
     */
    private $attributeRepository;

    /**
     * @var int|null
     */
    private $stateOfWearAttributeId = null;

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param ResourceConnection $resourceConnection
     * @param AttributeRepositoryInterface $attributeRepository
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        ResourceConnection $resourceConnection,
        AttributeRepositoryInterface $attributeRepository,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->resourceConnection = $resourceConnection;
        $this->attributeRepository = $attributeRepository;
    }

    /**
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $attributeId = $this->getStateOfWearAttributeId();

                $value = '';
                if ($attributeId) {
                    $sql = <<<SQL
SELECT GROUP_CONCAT(DISTINCT csow.name SEPARATOR " & ")
FROM `sales_order_item` soi
LEFT JOIN `sales_order` so ON soi.order_id = so.entity_id
LEFT JOIN `catalog_product_entity_int` cpei ON cpei.attribute_id = {$attributeId}
    AND cpei.entity_id = soi.product_id AND cpei.store_id = 0
LEFT JOIN `cnc_state_of_wear` csow ON csow.entity_id = cpei.value
WHERE soi.order_id = {$item['entity_id']}
SQL;
                    $value = $this->resourceConnection
                        ->getConnection()
                        ->fetchOne($sql);
                }

                $item[$this->getData('name')] = $value ?: '';
            }
        }
        return $dataSource;
    }

    /**
     * @return int|null
     */
    private function getStateOfWearAttributeId(): ?int
    {
        if (!$this->stateOfWearAttributeId) {
            try {
                $attributeId = $this->attributeRepository->get(
                    ProductAttributeInterface::ENTITY_TYPE_CODE,
                    \Cnc\Catalog\Model\Config::PRODUCT_ATTRIBUTE_CODE_STATE_OF_WEAR
                )->getAttributeId();

                $this->stateOfWearAttributeId = $attributeId ? (int)$attributeId : null;
            } catch (NoSuchEntityException $e) {
                return null;
            }
        }

        return $this->stateOfWearAttributeId;
    }
}

Spamworldpro Mini