123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Model\Entity\Attribute\Source;
- use Magento\Framework\App\ObjectManager;
- use Magento\Store\Model\StoreManagerInterface;
- /**
- * Eav attribute default source when values are coming from another table
- *
- * @api
- * @since 100.0.2
- */
- class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
- {
- /**
- * Default values for option cache
- *
- * @var array
- */
- protected $_optionsDefault = [];
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
- */
- protected $_attrOptionCollectionFactory;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory
- */
- protected $_attrOptionFactory;
- /**
- * @var StoreManagerInterface
- */
- private $storeManager;
- /**
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
- \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
- ) {
- $this->_attrOptionCollectionFactory = $attrOptionCollectionFactory;
- $this->_attrOptionFactory = $attrOptionFactory;
- }
- /**
- * Retrieve Full Option values array
- *
- * @param bool $withEmpty Add empty option to array
- * @param bool $defaultValues
- * @return array
- */
- public function getAllOptions($withEmpty = true, $defaultValues = false)
- {
- $storeId = $this->getAttribute()->getStoreId();
- if ($storeId === null) {
- $storeId = $this->getStoreManager()->getStore()->getId();
- }
- if (!is_array($this->_options)) {
- $this->_options = [];
- }
- if (!is_array($this->_optionsDefault)) {
- $this->_optionsDefault = [];
- }
- $attributeId = $this->getAttribute()->getId();
- if (!isset($this->_options[$storeId][$attributeId])) {
- $collection = $this->_attrOptionCollectionFactory->create()->setPositionOrder(
- 'asc'
- )->setAttributeFilter(
- $attributeId
- )->setStoreFilter(
- $storeId
- )->load();
- $this->_options[$storeId][$attributeId] = $collection->toOptionArray();
- $this->_optionsDefault[$storeId][$attributeId] = $collection->toOptionArray('default_value');
- }
- $options = $defaultValues
- ? $this->_optionsDefault[$storeId][$attributeId]
- : $this->_options[$storeId][$attributeId];
- if ($withEmpty) {
- $options = $this->addEmptyOption($options);
- }
- return $options;
- }
- /**
- * Get StoreManager dependency
- *
- * @return StoreManagerInterface
- * @deprecated 100.1.6
- */
- private function getStoreManager()
- {
- if ($this->storeManager === null) {
- $this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
- }
- return $this->storeManager;
- }
- /**
- * Retrieve Option values array by ids
- *
- * @param string|array $ids
- * @param bool $withEmpty Add empty option to array
- * @return array
- */
- public function getSpecificOptions($ids, $withEmpty = true)
- {
- $options = $this->_attrOptionCollectionFactory->create()
- ->setPositionOrder('asc')
- ->setAttributeFilter($this->getAttribute()->getId())
- ->addFieldToFilter('main_table.option_id', ['in' => $ids])
- ->setStoreFilter($this->getAttribute()->getStoreId())
- ->load()
- ->toOptionArray();
- if ($withEmpty) {
- $options = $this->addEmptyOption($options);
- }
- return $options;
- }
- /**
- * Add an empty option to the array
- *
- * @param array $options
- * @return array
- */
- private function addEmptyOption(array $options)
- {
- array_unshift($options, ['label' => ' ', 'value' => '']);
- return $options;
- }
- /**
- * Get a text for option value
- *
- * @param string|integer $value
- * @return array|string|bool
- */
- public function getOptionText($value)
- {
- $isMultiple = false;
- if (strpos($value, ',') !== false) {
- $isMultiple = true;
- $value = explode(',', $value);
- }
- $options = $this->getSpecificOptions($value, false);
- if ($isMultiple) {
- $values = [];
- foreach ($options as $item) {
- if (in_array($item['value'], $value)) {
- $values[] = $item['label'];
- }
- }
- return $values;
- }
- foreach ($options as $item) {
- if ($item['value'] == $value) {
- return $item['label'];
- }
- }
- return false;
- }
- /**
- * Add Value Sort To Collection Select
- *
- * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
- * @param string $dir
- *
- * @return $this
- */
- public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
- {
- $attribute = $this->getAttribute();
- $valueTable1 = $attribute->getAttributeCode() . '_t1';
- $valueTable2 = $attribute->getAttributeCode() . '_t2';
- $linkField = $attribute->getEntity()->getLinkField();
- $collection->getSelect()->joinLeft(
- [$valueTable1 => $attribute->getBackend()->getTable()],
- "e.{$linkField}={$valueTable1}." . $linkField .
- " AND {$valueTable1}.attribute_id='{$attribute->getId()}'" .
- " AND {$valueTable1}.store_id=0",
- []
- )->joinLeft(
- [$valueTable2 => $attribute->getBackend()->getTable()],
- "e.{$linkField}={$valueTable2}." . $linkField .
- " AND {$valueTable2}.attribute_id='{$attribute->getId()}'" .
- " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
- []
- );
- $valueExpr = $collection->getSelect()->getConnection()->getCheckSql(
- "{$valueTable2}.value_id > 0",
- "{$valueTable2}.value",
- "{$valueTable1}.value"
- );
- $this->_attrOptionFactory->create()->addOptionValueToCollection(
- $collection,
- $attribute,
- $valueExpr
- );
- $collection->getSelect()->order("{$attribute->getAttributeCode()} {$dir}");
- return $this;
- }
- /**
- * Retrieve Column(s) for Flat
- *
- * @return array
- */
- public function getFlatColumns()
- {
- $columns = [];
- $attributeCode = $this->getAttribute()->getAttributeCode();
- $isMulti = $this->getAttribute()->getFrontend()->getInputType() == 'multiselect';
- $type = $isMulti ? \Magento\Framework\DB\Ddl\Table::TYPE_TEXT : \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER;
- $columns[$attributeCode] = [
- 'type' => $type,
- 'length' => $isMulti ? '255' : null,
- 'unsigned' => false,
- 'nullable' => true,
- 'default' => null,
- 'extra' => null,
- 'comment' => $attributeCode . ' column',
- ];
- if (!$isMulti) {
- $columns[$attributeCode . '_value'] = [
- 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
- 'length' => 255,
- 'unsigned' => false,
- 'nullable' => true,
- 'default' => null,
- 'extra' => null,
- 'comment' => $attributeCode . ' column',
- ];
- }
- return $columns;
- }
- /**
- * Retrieve Indexes for Flat
- *
- * @return array
- */
- public function getFlatIndexes()
- {
- $indexes = [];
- $index = sprintf('IDX_%s', strtoupper($this->getAttribute()->getAttributeCode()));
- $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
- $sortable = $this->getAttribute()->getUsedForSortBy();
- if ($sortable && $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
- $index = sprintf('IDX_%s_VALUE', strtoupper($this->getAttribute()->getAttributeCode()));
- $indexes[$index] = [
- 'type' => 'index',
- 'fields' => [$this->getAttribute()->getAttributeCode() . '_value'],
- ];
- }
- return $indexes;
- }
- /**
- * Retrieve Select For Flat Attribute update
- *
- * @param int $store
- * @return \Magento\Framework\DB\Select|null
- */
- public function getFlatUpdateSelect($store)
- {
- return $this->_attrOptionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store);
- }
- }
|