![]() 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/magento/module-eav/Block/Adminhtml/Attribute/Edit/Options/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Eav\Block\Adminhtml\Attribute\Edit\Options; use Magento\Store\Model\ResourceModel\Store\Collection; use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; /** * Attribute add/edit form options tab * * @api * @since 100.0.2 */ class Options extends \Magento\Backend\Block\Template { /** * @var \Magento\Framework\Registry */ protected $_registry; /** * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory */ protected $_attrOptionCollectionFactory; /** * @var string */ protected $_template = 'Magento_Catalog::catalog/product/attribute/options.phtml'; /** * @var \Magento\Framework\Validator\UniversalFactory $universalFactory */ protected $_universalFactory; /** * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory * @param \Magento\Framework\Validator\UniversalFactory $universalFactory * @param array $data */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Framework\Validator\UniversalFactory $universalFactory, array $data = [] ) { parent::__construct($context, $data); $this->_registry = $registry; $this->_attrOptionCollectionFactory = $attrOptionCollectionFactory; $this->_universalFactory = $universalFactory; } /** * Is true only for system attributes which use source model * * Option labels and position for such attributes are kept in source model and thus cannot be overridden * * @return bool */ public function canManageOptionDefaultOnly() { $attribute = $this->getAttributeObject(); return !$attribute->getCanManageOptionLabels() && !$attribute->getIsUserDefined() && $attribute->getSourceModel(); } /** * Retrieve stores collection with default store * * @return array */ public function getStores() { if (!$this->hasStores()) { $this->setData('stores', $this->_storeManager->getStores(true)); } return $this->_getData('stores'); } /** * Returns stores sorted by Sort Order * * @return array * @since 100.1.0 */ public function getStoresSortedBySortOrder() { $stores = $this->getStores(); if (is_array($stores)) { usort( $stores, function ($storeA, $storeB) { if ($storeA->getSortOrder() == $storeB->getSortOrder()) { return $storeA->getId() < $storeB->getId() ? -1 : 1; } return ($storeA->getSortOrder() < $storeB->getSortOrder()) ? -1 : 1; } ); } return $stores; } /** * Retrieve attribute option values if attribute input type select or multiselect * * @return array */ public function getOptionValues() { $values = $this->_getData('option_values'); if ($values === null) { $values = []; $attribute = $this->getAttributeObject(); $optionCollection = $this->_getOptionValuesCollection($attribute); if ($optionCollection) { $values = $this->_prepareOptionValues($attribute, $optionCollection); } $this->setData('option_values', $values); } return $values; } /** * Preparing values of attribute options * * @param AbstractAttribute $attribute * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $optionCollection * @return array */ protected function _prepareOptionValues( AbstractAttribute $attribute, $optionCollection ) { $type = $attribute->getFrontendInput(); if ($type === 'select' || $type === 'multiselect') { $defaultValues = is_string($attribute->getDefaultValue()) ? explode(',', $attribute->getDefaultValue()) : []; $inputType = $type === 'select' ? 'radio' : 'checkbox'; } else { $defaultValues = []; $inputType = ''; } $values = []; $isSystemAttribute = is_array($optionCollection); if ($isSystemAttribute) { $values[] = $this->getPreparedValues($optionCollection, $isSystemAttribute, $inputType, $defaultValues); } else { $optionCollection->setPageSize(200); $pageCount = $optionCollection->getLastPageNumber(); $currentPage = 1; while ($currentPage <= $pageCount) { $optionCollection->clear(); $optionCollection->setCurPage($currentPage); $values[] = $this->getPreparedValues($optionCollection, $isSystemAttribute, $inputType, $defaultValues); $currentPage++; } } return array_merge([], ...$values); } /** * Return prepared values of system or user defined attribute options * * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $optionCollection * @param bool $isSystemAttribute * @param string $inputType * @param array $defaultValues */ private function getPreparedValues( $optionCollection, bool $isSystemAttribute, string $inputType, array $defaultValues ) { $values = []; foreach ($optionCollection as $option) { $bunch = $isSystemAttribute ? $this->_prepareSystemAttributeOptionValues( $option, $inputType, $defaultValues ) : $this->_prepareUserDefinedAttributeOptionValues( $option, $inputType, $defaultValues ); foreach ($bunch as $value) { $values[] = new \Magento\Framework\DataObject($value); } } return $values; } /** * Retrieve option values collection * * It is represented by an array in case of system attribute * * @param AbstractAttribute $attribute * @return array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */ protected function _getOptionValuesCollection(AbstractAttribute $attribute) { if ($this->canManageOptionDefaultOnly()) { $options = $this->_universalFactory->create( $attribute->getSourceModel() )->setAttribute( $attribute )->getAllOptions(); return $options; } else { return $this->_attrOptionCollectionFactory->create()->setAttributeFilter( $attribute->getId() )->setPositionOrder( 'asc', true )->load(); } } /** * Prepare option values of system attribute * * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option * @param string $inputType * @param array $defaultValues * @param string $valuePrefix * @return array */ protected function _prepareSystemAttributeOptionValues($option, $inputType, $defaultValues, $valuePrefix = '') { if (is_array($option['value'])) { $values = []; foreach ($option['value'] as $subOption) { $bunch = $this->_prepareSystemAttributeOptionValues( $subOption, $inputType, $defaultValues, $option['label'] . ' / ' ); $values[] = $bunch[0]; } return $values; } $value['checked'] = in_array($option['value'], $defaultValues) ? 'checked="checked"' : ''; $value['intype'] = $inputType; $value['id'] = $option['value']; $value['sort_order'] = 0; foreach ($this->getStores() as $store) { $storeId = $store->getId(); $value['store' . $storeId] = $storeId == \Magento\Store\Model\Store::DEFAULT_STORE_ID ? $valuePrefix . $this->escapeHtml($option['label']) : ''; } return [$value]; } /** * Prepare option values of user defined attribute * * @param array|\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option $option * @param string $inputType * @param array $defaultValues * @return array */ protected function _prepareUserDefinedAttributeOptionValues($option, $inputType, $defaultValues) { $optionId = $option->getId(); $value['checked'] = in_array($optionId, $defaultValues) ? 'checked="checked"' : ''; $value['intype'] = $inputType; $value['id'] = $optionId; $value['sort_order'] = $option->getSortOrder(); foreach ($this->getStores() as $store) { $storeId = $store->getId(); $storeValues = $this->getStoreOptionValues($storeId); $value['store' . $storeId] = isset( $storeValues[$optionId] ) ? $this->escapeHtml( $storeValues[$optionId] ) : ''; } return [$value]; } /** * Retrieve attribute option values for given store id * * @param int $storeId * @return array */ public function getStoreOptionValues($storeId) { $values = $this->getData('store_option_values_' . $storeId); if ($values === null) { $values = []; $valuesCollection = $this->_attrOptionCollectionFactory->create()->setAttributeFilter( $this->getAttributeObject()->getId() )->setStoreFilter( $storeId, false )->load(); foreach ($valuesCollection as $item) { $values[$item->getId()] = $item->getValue(); } $this->setData('store_option_values_' . $storeId, $values); } return $values; } /** * Retrieve attribute object from registry * * @return \Magento\Eav\Model\Entity\Attribute\AbstractAttribute * @codeCoverageIgnore */ protected function getAttributeObject() { return $this->_registry->registry('entity_attribute'); } }