123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Model\Entity\Attribute\Source;
- /**
- * @api
- * @since 100.0.2
- */
- class Boolean extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
- {
- /**
- * Option values
- */
- const VALUE_YES = 1;
- const VALUE_NO = 0;
- /**
- * @var \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory
- */
- protected $_eavAttrEntity;
- /**
- * @param \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity
- * @codeCoverageIgnore
- */
- public function __construct(
- \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity
- ) {
- $this->_eavAttrEntity = $eavAttrEntity;
- }
- /**
- * Retrieve all options array
- *
- * @return array
- */
- public function getAllOptions()
- {
- if ($this->_options === null) {
- $this->_options = [
- ['label' => __('Yes'), 'value' => self::VALUE_YES],
- ['label' => __('No'), 'value' => self::VALUE_NO],
- ];
- }
- return $this->_options;
- }
- /**
- * Retrieve option array
- *
- * @return array
- */
- public function getOptionArray()
- {
- $_options = [];
- foreach ($this->getAllOptions() as $option) {
- $_options[$option['value']] = $option['label'];
- }
- return $_options;
- }
- /**
- * Get a text for option value
- *
- * @param string|int $value
- * @return string|false
- */
- public function getOptionText($value)
- {
- $options = $this->getAllOptions();
- foreach ($options as $option) {
- if ($option['value'] == $value) {
- return $option['label'];
- }
- }
- return false;
- }
- /**
- * Retrieve flat column definition
- *
- * @return array
- */
- public function getFlatColumns()
- {
- $attributeCode = $this->getAttribute()->getAttributeCode();
- return [
- $attributeCode => [
- 'unsigned' => false,
- 'default' => null,
- 'extra' => null,
- 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
- 'length' => 1,
- 'nullable' => true,
- 'comment' => $attributeCode . ' column',
- ],
- ];
- }
- /**
- * Retrieve Indexes(s) for Flat
- *
- * @return array
- */
- public function getFlatIndexes()
- {
- $indexes = [];
- $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
- $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
- return $indexes;
- }
- /**
- * Retrieve Select For Flat Attribute update
- *
- * @param int $store
- * @return \Magento\Framework\DB\Select|null
- */
- public function getFlatUpdateSelect($store)
- {
- return $this->_eavAttrEntity->create()->getFlatUpdateSelect($this->getAttribute(), $store);
- }
- /**
- * Get a text for index option value
- *
- * @param string|int $value
- * @return string|bool
- */
- public function getIndexOptionText($value)
- {
- switch ($value) {
- case self::VALUE_YES:
- return 'Yes';
- case self::VALUE_NO:
- return 'No';
- }
- return parent::getIndexOptionText($value);
- }
- /**
- * Add Value Sort To Collection Select
- *
- * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
- * @param string $dir
- *
- * @return \Magento\Eav\Model\Entity\Attribute\Source\Boolean
- */
- public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
- {
- $attributeCode = $this->getAttribute()->getAttributeCode();
- $attributeId = $this->getAttribute()->getId();
- $attributeTable = $this->getAttribute()->getBackend()->getTable();
- $linkField = $this->getAttribute()->getEntity()->getLinkField();
- if ($this->getAttribute()->isScopeGlobal()) {
- $tableName = $attributeCode . '_t';
- $collection->getSelect()
- ->joinLeft(
- [$tableName => $attributeTable],
- "e.{$linkField}={$tableName}.{$linkField}"
- . " AND {$tableName}.attribute_id='{$attributeId}'"
- . " AND {$tableName}.store_id='0'",
- []
- );
- $valueExpr = $tableName . '.value';
- } else {
- $valueTable1 = $attributeCode . '_t1';
- $valueTable2 = $attributeCode . '_t2';
- $collection->getSelect()
- ->joinLeft(
- [$valueTable1 => $attributeTable],
- "e.{$linkField}={$valueTable1}.{$linkField}"
- . " AND {$valueTable1}.attribute_id='{$attributeId}'"
- . " AND {$valueTable1}.store_id='0'",
- []
- )
- ->joinLeft(
- [$valueTable2 => $attributeTable],
- "e.{$linkField}={$valueTable2}.{$linkField}"
- . " AND {$valueTable2}.attribute_id='{$attributeId}'"
- . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
- []
- );
- $valueExpr = $collection->getConnection()->getCheckSql(
- $valueTable2 . '.value_id > 0',
- $valueTable2 . '.value',
- $valueTable1 . '.value'
- );
- }
- $collection->getSelect()->order($valueExpr . ' ' . $dir);
- return $this;
- }
- }
|