![]() 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/wyomind/msicustomattributes/Helper/ |
<?php /** * Copyright © 2020 Wyomind. All rights reserved. * See LICENSE.txt for license details. */ namespace Wyomind\MsiCustomAttributes\Helper; use Magento\Framework\App\Helper\Context; use Wyomind\MsiCustomAttributes\Model\CustomAttribute\Repository; /** * Class CustomAttribute * @package Wyomind\MsiCustomAttributes\Helper */ class CustomAttribute extends \Magento\Framework\App\Helper\AbstractHelper { /** * Reserved attribute codes */ const RESERVED_ATTRIBUTE_CODES = ["quantity" => ["type" => self::TYPE_INT], "sku" => ["type" => self::TYPE_TEXT], "status" => ["type" => self::TYPE_SELECT, "options" => [1 => "In Stock", 0 => "Out of Stock"]], "notify_stock_qty" => ["type" => self::TYPE_INT]]; /** * Base index for cloumn sorting */ const BASE_SORT_ORDER_INDEX = 80; /** * Boolean type */ const TYPE_BOOL = "bool"; /** * Number type */ const TYPE_INT = "int"; /** * Date type */ const TYPE_DATE = "date"; /** * Text type */ const TYPE_TEXT = "text"; /** * Dropdown type */ const TYPE_SELECT = "select"; public $repository; /** * @var \Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOption\CollectionFactory */ public $optionCollectionFactory; public function __construct( \Wyomind\MsiCustomAttributes\Helper\Delegate $wyomind, Context $context, /** @delegation off */ \Wyomind\MsiCustomAttributes\Model\ResourceModel\CustomAttributeDropdownOption\CollectionFactory $optionCollectionFactory ) { $wyomind->constructor($this, $wyomind, __CLASS__); parent::__construct($context); $this->optionCollectionFactory = $optionCollectionFactory; } /** * @param $structure * @return mixed */ public function mergeStructure($structure) { if (isset($structure["components"]["product_form"]["children"]["product_form"]["children"]["sources"])) { foreach ($this->repository->list() as $position => $customAttribute) { /** @var \Wyomind\MsiCustomAttributes\Model\CustomAttribute $customAttribute */ $additionalInput = $this->getCustomAttributeUiLayout($customAttribute, $position); $structure["components"]["product_form"]["children"]["product_form"]["children"]["sources"]["children"]["assigned_sources"]["children"]["record"]["children"][$customAttribute->getCode()] = $additionalInput; } } return $structure; } /** * @param $customAttribute * @return array */ public function getCustomAttributeUiLayout(\Wyomind\MsiCustomAttributes\Model\CustomAttribute $customAttribute, $position = 0) { switch ($customAttribute->getType()) { case self::TYPE_TEXT: $validation = []; if ($customAttribute->getIsRequired()) { $validation["required-entry"] = $customAttribute->getIsRequired(); } return ['type' => 'form.input', 'name' => $customAttribute->getCode(), 'dataScope' => $customAttribute->getCode(), 'config' => ['component' => 'Magento_Ui/js/form/element/abstract', 'template' => 'ui/form/field', 'formElement' => 'input', 'label' => $customAttribute->getLabel(), 'dataType' => $customAttribute->getType(), 'sortOrder' => self::BASE_SORT_ORDER_INDEX + $customAttribute->getSortOrder() + $position, "validation" => $validation, "default" => $customAttribute->getDefault()]]; break; case self::TYPE_INT: $validation = ["less-than-equals-to" => "99999999", "validate-number" => true]; if ($customAttribute->getIsRequired()) { $validation["required-entry"] = $customAttribute->getIsRequired(); } return ['type' => 'form.input', 'name' => $customAttribute->getCode(), 'dataScope' => $customAttribute->getCode(), 'config' => ['component' => 'Magento_Ui/js/form/element/abstract', 'template' => "ui/form/field", 'formElement' => 'input', 'label' => $customAttribute->getLabel(), 'dataType' => $customAttribute->getType(), 'sortOrder' => self::BASE_SORT_ORDER_INDEX + $customAttribute->getSortOrder() + $position, "validation" => $validation, "default" => $customAttribute->getDefault()]]; break; case self::TYPE_DATE: $validation = ["validate-date" => true]; if ($customAttribute->getIsRequired()) { $validation["required-entry"] = $customAttribute->getIsRequired(); } return ['type' => 'form.input', 'name' => $customAttribute->getCode(), 'dataScope' => $customAttribute->getCode(), 'config' => ['component' => 'Magento_Ui/js/form/element/date', 'template' => 'ui/form/field', 'formElement' => 'date', 'label' => $customAttribute->getLabel(), 'dataType' => "text", 'sortOrder' => self::BASE_SORT_ORDER_INDEX + $customAttribute->getSortOrder() + $position, "validation" => $validation]]; break; case self::TYPE_BOOL: return ["type" => "form.checkbox", 'name' => $customAttribute->getCode(), 'dataScope' => $customAttribute->getCode(), "config" => ["component" => "Magento_Ui/js/form/element/single-checkbox", "template" => "ui/form/field", "dataType" => "boolean", "componentType" => "field", "prefer" => "toggle", "formElement" => "checkbox", "valueMap" => ["false" => "0", "true" => "1"], 'label' => $customAttribute->getLabel(), 'sortOrder' => self::BASE_SORT_ORDER_INDEX + $customAttribute->getSortOrder() + $position, "default" => "0"]]; break; case self::TYPE_SELECT: $options = []; $optionCollection = $this->optionCollectionFactory->create()->getOptionByAttributeIdAndStoreId($customAttribute->getAttributeId()); $default = null; foreach ($optionCollection as $option) { $options[] = ["value" => $option->getOptionId(), "label" => $option->getValue()]; if ($option->getIsDefault() == 1) { $default = $option->getOptionId(); } } return ['type' => 'form.select', 'name' => $customAttribute->getCode(), 'dataScope' => $customAttribute->getCode(), 'config' => ['component' => 'Magento_Ui/js/form/element/select', 'template' => 'ui/form/field', 'dataType' => 'select', 'formElement' => 'select', 'label' => $customAttribute->getLabel(), 'dataType' => $customAttribute->getType(), 'sortOrder' => self::BASE_SORT_ORDER_INDEX + $customAttribute->getSortOrder() + $position, "options" => $options, "default" => $default]]; break; } } public function getCustomAttributes() { $attributes = []; foreach ($this->repository->list() as $position => $customAttribute) { $options = []; $optionCollection = $this->optionCollectionFactory->create()->getOptionByAttributeIdAndStoreId($customAttribute->getAttributeId()); $default = null; foreach ($optionCollection as $option) { $options[$option->getOptionId()] = $option->getValue(); } $attributes[$customAttribute->getCode()] = ["type" => $customAttribute->getType(), "default" => $customAttribute->getDefault(), "options" => $options]; } return $attributes; } }