Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Boolean.php
Go to the documentation of this file.
1 <?php
7 
12 class Boolean extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
13 {
17  const VALUE_YES = 1;
18 
19  const VALUE_NO = 0;
20 
24  protected $_eavAttrEntity;
25 
30  public function __construct(
31  \Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity
32  ) {
33  $this->_eavAttrEntity = $eavAttrEntity;
34  }
35 
41  public function getAllOptions()
42  {
43  if ($this->_options === null) {
44  $this->_options = [
45  ['label' => __('Yes'), 'value' => self::VALUE_YES],
46  ['label' => __('No'), 'value' => self::VALUE_NO],
47  ];
48  }
49  return $this->_options;
50  }
51 
57  public function getOptionArray()
58  {
59  $_options = [];
60  foreach ($this->getAllOptions() as $option) {
61  $_options[$option['value']] = $option['label'];
62  }
63  return $_options;
64  }
65 
72  public function getOptionText($value)
73  {
74  $options = $this->getAllOptions();
75  foreach ($options as $option) {
76  if ($option['value'] == $value) {
77  return $option['label'];
78  }
79  }
80  return false;
81  }
82 
88  public function getFlatColumns()
89  {
90  $attributeCode = $this->getAttribute()->getAttributeCode();
91 
92  return [
93  $attributeCode => [
94  'unsigned' => false,
95  'default' => null,
96  'extra' => null,
98  'length' => 1,
99  'nullable' => true,
100  'comment' => $attributeCode . ' column',
101  ],
102  ];
103  }
104 
110  public function getFlatIndexes()
111  {
112  $indexes = [];
113 
114  $index = 'IDX_' . strtoupper($this->getAttribute()->getAttributeCode());
115  $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
116 
117  return $indexes;
118  }
119 
126  public function getFlatUpdateSelect($store)
127  {
128  return $this->_eavAttrEntity->create()->getFlatUpdateSelect($this->getAttribute(), $store);
129  }
130 
137  public function getIndexOptionText($value)
138  {
139  switch ($value) {
140  case self::VALUE_YES:
141  return 'Yes';
142  case self::VALUE_NO:
143  return 'No';
144  }
145 
146  return parent::getIndexOptionText($value);
147  }
148 
157  public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
158  {
159  $attributeCode = $this->getAttribute()->getAttributeCode();
160  $attributeId = $this->getAttribute()->getId();
161  $attributeTable = $this->getAttribute()->getBackend()->getTable();
162  $linkField = $this->getAttribute()->getEntity()->getLinkField();
163 
164  if ($this->getAttribute()->isScopeGlobal()) {
165  $tableName = $attributeCode . '_t';
166  $collection->getSelect()
167  ->joinLeft(
168  [$tableName => $attributeTable],
169  "e.{$linkField}={$tableName}.{$linkField}"
170  . " AND {$tableName}.attribute_id='{$attributeId}'"
171  . " AND {$tableName}.store_id='0'",
172  []
173  );
174  $valueExpr = $tableName . '.value';
175  } else {
176  $valueTable1 = $attributeCode . '_t1';
177  $valueTable2 = $attributeCode . '_t2';
178  $collection->getSelect()
179  ->joinLeft(
180  [$valueTable1 => $attributeTable],
181  "e.{$linkField}={$valueTable1}.{$linkField}"
182  . " AND {$valueTable1}.attribute_id='{$attributeId}'"
183  . " AND {$valueTable1}.store_id='0'",
184  []
185  )
186  ->joinLeft(
187  [$valueTable2 => $attributeTable],
188  "e.{$linkField}={$valueTable2}.{$linkField}"
189  . " AND {$valueTable2}.attribute_id='{$attributeId}'"
190  . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
191  []
192  );
193  $valueExpr = $collection->getConnection()->getCheckSql(
194  $valueTable2 . '.value_id > 0',
195  $valueTable2 . '.value',
196  $valueTable1 . '.value'
197  );
198  }
199 
200  $collection->getSelect()->order($valueExpr . ' ' . $dir);
201  return $this;
202  }
203 }
$tableName
Definition: trigger.php:13
__construct(\Magento\Eav\Model\ResourceModel\Entity\AttributeFactory $eavAttrEntity)
Definition: Boolean.php:30
__()
Definition: __.php:13
addValueSortToCollection($collection, $dir=\Magento\Framework\DB\Select::SQL_ASC)
Definition: Boolean.php:157
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$index
Definition: list.phtml:44
const SQL_ASC
Definition: Select.php:81