1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?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;
}
}