Boolean.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\Entity\Attribute\Source;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Boolean extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
  12. {
  13. /**
  14. * Option values
  15. */
  16. const VALUE_YES = 1;
  17. const VALUE_NO = 0;
  18. /**
  19. * @var \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory
  20. */
  21. protected $_eavAttrEntity;
  22. /**
  23. * @param \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity
  24. * @codeCoverageIgnore
  25. */
  26. public function __construct(
  27. \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity
  28. ) {
  29. $this->_eavAttrEntity = $eavAttrEntity;
  30. }
  31. /**
  32. * Retrieve all options array
  33. *
  34. * @return array
  35. */
  36. public function getAllOptions()
  37. {
  38. if ($this->_options === null) {
  39. $this->_options = [
  40. ['label' => __('Yes'), 'value' => self::VALUE_YES],
  41. ['label' => __('No'), 'value' => self::VALUE_NO],
  42. ];
  43. }
  44. return $this->_options;
  45. }
  46. /**
  47. * Retrieve option array
  48. *
  49. * @return array
  50. */
  51. public function getOptionArray()
  52. {
  53. $_options = [];
  54. foreach ($this->getAllOptions() as $option) {
  55. $_options[$option['value']] = $option['label'];
  56. }
  57. return $_options;
  58. }
  59. /**
  60. * Get a text for option value
  61. *
  62. * @param string|int $value
  63. * @return string|false
  64. */
  65. public function getOptionText($value)
  66. {
  67. $options = $this->getAllOptions();
  68. foreach ($options as $option) {
  69. if ($option['value'] == $value) {
  70. return $option['label'];
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * Retrieve flat column definition
  77. *
  78. * @return array
  79. */
  80. public function getFlatColumns()
  81. {
  82. $attributeCode = $this->getAttribute()->getAttributeCode();
  83. return [
  84. $attributeCode => [
  85. 'unsigned' => false,
  86. 'default' => null,
  87. 'extra' => null,
  88. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
  89. 'length' => 1,
  90. 'nullable' => true,
  91. 'comment' => $attributeCode . ' column',
  92. ],
  93. ];
  94. }
  95. /**
  96. * Retrieve Indexes(s) for Flat
  97. *
  98. * @return array
  99. */
  100. public function getFlatIndexes()
  101. {
  102. $indexes = [];
  103. $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
  104. $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
  105. return $indexes;
  106. }
  107. /**
  108. * Retrieve Select For Flat Attribute update
  109. *
  110. * @param int $store
  111. * @return \Magento\Framework\DB\Select|null
  112. */
  113. public function getFlatUpdateSelect($store)
  114. {
  115. return $this->_eavAttrEntity->create()->getFlatUpdateSelect($this->getAttribute(), $store);
  116. }
  117. /**
  118. * Get a text for index option value
  119. *
  120. * @param string|int $value
  121. * @return string|bool
  122. */
  123. public function getIndexOptionText($value)
  124. {
  125. switch ($value) {
  126. case self::VALUE_YES:
  127. return 'Yes';
  128. case self::VALUE_NO:
  129. return 'No';
  130. }
  131. return parent::getIndexOptionText($value);
  132. }
  133. /**
  134. * Add Value Sort To Collection Select
  135. *
  136. * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
  137. * @param string $dir
  138. *
  139. * @return \Magento\Eav\Model\Entity\Attribute\Source\Boolean
  140. */
  141. public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
  142. {
  143. $attributeCode = $this->getAttribute()->getAttributeCode();
  144. $attributeId = $this->getAttribute()->getId();
  145. $attributeTable = $this->getAttribute()->getBackend()->getTable();
  146. $linkField = $this->getAttribute()->getEntity()->getLinkField();
  147. if ($this->getAttribute()->isScopeGlobal()) {
  148. $tableName = $attributeCode . '_t';
  149. $collection->getSelect()
  150. ->joinLeft(
  151. [$tableName => $attributeTable],
  152. "e.{$linkField}={$tableName}.{$linkField}"
  153. . " AND {$tableName}.attribute_id='{$attributeId}'"
  154. . " AND {$tableName}.store_id='0'",
  155. []
  156. );
  157. $valueExpr = $tableName . '.value';
  158. } else {
  159. $valueTable1 = $attributeCode . '_t1';
  160. $valueTable2 = $attributeCode . '_t2';
  161. $collection->getSelect()
  162. ->joinLeft(
  163. [$valueTable1 => $attributeTable],
  164. "e.{$linkField}={$valueTable1}.{$linkField}"
  165. . " AND {$valueTable1}.attribute_id='{$attributeId}'"
  166. . " AND {$valueTable1}.store_id='0'",
  167. []
  168. )
  169. ->joinLeft(
  170. [$valueTable2 => $attributeTable],
  171. "e.{$linkField}={$valueTable2}.{$linkField}"
  172. . " AND {$valueTable2}.attribute_id='{$attributeId}'"
  173. . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
  174. []
  175. );
  176. $valueExpr = $collection->getConnection()->getCheckSql(
  177. $valueTable2 . '.value_id > 0',
  178. $valueTable2 . '.value',
  179. $valueTable1 . '.value'
  180. );
  181. }
  182. $collection->getSelect()->order($valueExpr . ' ' . $dir);
  183. return $this;
  184. }
  185. }