![]() 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/SerialNumber/Ui/Component/Listing/Column/ |
<?php /** * Copyright (c) 2020 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]> */ declare(strict_types=1); namespace Cnc\SerialNumber\Ui\Component\Listing\Column; 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; use Wyomind\MsiCustomAttributes\Model\CustomAttribute; use Wyomind\MsiCustomAttributes\Model\CustomAttributeDropdownOptionValue; use Wyomind\MsiCustomAttributes\Model\CustomAttributeDropdownOptionValueFactory; use Wyomind\MsiCustomAttributes\Model\CustomAttributeFactory; use Wyomind\MsiCustomAttributes\Model\CustomAttributeValue; use Wyomind\MsiCustomAttributes\Model\CustomAttributeValueFactory; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttribute as CustomAttributeResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOptionValue as CustomAttributeDropdownOptionValueResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue as CustomAttributeValueResource; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue\Collection as CustomAttributeValueCollection; use Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeValue\CollectionFactory as CustomAttributeValueCollectionFactory; abstract class MsiCustomAttribute extends Column { /** * @var string */ protected $attributeCode = ''; /** * @var array */ private $cacheInstance = []; /** * @var CustomAttributeValueFactory */ private $customAttributeValueFactory; /** * @var CustomAttributeValueResource */ private $customAttributeValueResource; /** * @var CustomAttributeValueCollectionFactory */ private $customAttributeValueCollectionFactory; /** * @var CustomAttributeFactory */ private $customAttributeFactory; /** * @var CustomAttributeResource */ private $customAttributeResource; /** * @var CustomAttributeDropdownOptionValueFactory */ private $customAttributeDropdownOptionValueFactory; /** * @var CustomAttributeDropdownOptionValueResource */ private $customAttributeDropdownOptionValueResource; /** * ProductId constructor. * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param CustomAttributeFactory $customAttributeFactory * @param CustomAttributeResource $customAttributeResource * @param CustomAttributeValueFactory $customAttributeValueFactory * @param CustomAttributeValueResource $customAttributeValueResource * @param CustomAttributeValueCollectionFactory $customAttributeValueCollectionFactory * @param CustomAttributeDropdownOptionValueFactory $customAttributeDropdownOptionValueFactory * @param CustomAttributeDropdownOptionValueResource $customAttributeDropdownOptionValueResource * @param array $components * @param array $data */ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, CustomAttributeFactory $customAttributeFactory, CustomAttributeResource $customAttributeResource, CustomAttributeValueFactory $customAttributeValueFactory, CustomAttributeValueResource $customAttributeValueResource, CustomAttributeValueCollectionFactory $customAttributeValueCollectionFactory, CustomAttributeDropdownOptionValueFactory $customAttributeDropdownOptionValueFactory, CustomAttributeDropdownOptionValueResource $customAttributeDropdownOptionValueResource, array $components = [], array $data = [] ) { parent::__construct($context, $uiComponentFactory, $components, $data); $this->customAttributeValueFactory = $customAttributeValueFactory; $this->customAttributeValueResource = $customAttributeValueResource; $this->customAttributeValueCollectionFactory = $customAttributeValueCollectionFactory; $this->customAttributeFactory = $customAttributeFactory; $this->customAttributeResource = $customAttributeResource; $this->customAttributeDropdownOptionValueFactory = $customAttributeDropdownOptionValueFactory; $this->customAttributeDropdownOptionValueResource = $customAttributeDropdownOptionValueResource; } /** * @param array $dataSource * @return array */ public function prepareDataSource(array $dataSource): array { foreach ($dataSource['data']['items'] as $index => $item) { try { $attribute = $this->getAttribute($this->attributeCode); if (!$attribute) { continue; } $attributeId = $attribute->getAttributeId(); $attributeValue = $this->getAttributeValue( $attributeId, $item['sku'], isset($item['origin_source_code']) ? $item['origin_source_code'] : $item['source_code'] ); if ($attributeValue && $attributeValue->getId()) { if ($attribute->getType() == 'select') { $valueId = $attributeValue->getValue(); $dropdownValue = $this->getDropdownValue($valueId); if ($dropdownValue && $dropdownValue->getId()) { $dataSource['data']['items'][$index][$this->attributeCode] = __($dropdownValue->getValue()); } } else { $dataSource['data']['items'][$index][$this->attributeCode] = __($attributeValue->getValue()); } } } catch (NoSuchEntityException $e) { $dataSource['data']['items'][$index][$this->attributeCode] = ''; } } return $dataSource; } /** * @param $attributeId * @param $sku * @param $sourceCode * @return CustomAttributeValue|null */ private function getAttributeValue($attributeId, $sku, $sourceCode): ?CustomAttributeValue { $key = implode('_', ['custom-attribute-value', $attributeId, $sku, $sourceCode]); if (!isset($this->cacheInstance[$key])) { /** @var CustomAttributeValueCollection $collection */ $collection = $this->customAttributeValueCollectionFactory->create(); $collection ->addFilter('attribute_id', $attributeId) ->addFilter('sku', $sku) ->addFilter('source_code', $sourceCode); /** @var CustomAttributeValue $item */ $item = $collection->getFirstItem(); $this->cacheInstance[$key] = $item; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param $attributeCode * @return CustomAttribute|null */ private function getAttribute($attributeCode): ?CustomAttribute { $key = implode('_', ['custom-attribute', $attributeCode]); if (!isset($this->cacheInstance[$key])) { $customAttribute = $this->customAttributeFactory->create(); $this->customAttributeResource->load($customAttribute, $attributeCode, 'code'); $customAttribute->load($customAttribute->getId()); $this->cacheInstance[$key] = $customAttribute; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } /** * @param string|null $optionId * @return CustomAttributeDropdownOptionValue|null */ private function getDropdownValue(?string $optionId): ?CustomAttributeDropdownOptionValue { $key = implode('_', ['custom-attribute-dropdown-option-value', $optionId]); if (!isset($this->cacheInstance[$key])) { $dropdownValue = $this->customAttributeDropdownOptionValueFactory->create(); $this->customAttributeDropdownOptionValueResource->load($dropdownValue, $optionId, 'option_id'); $this->cacheInstance[$key] = $dropdownValue; } return isset($this->cacheInstance[$key]) ? $this->cacheInstance[$key] : null; } }