Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Table.php
Go to the documentation of this file.
1 <?php
7 
10 
15 class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
16 {
22  protected $_optionsDefault = [];
23 
28 
33 
37  private $storeManager;
38 
44  public function __construct(
45  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
46  \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
47  ) {
48  $this->_attrOptionCollectionFactory = $attrOptionCollectionFactory;
49  $this->_attrOptionFactory = $attrOptionFactory;
50  }
51 
59  public function getAllOptions($withEmpty = true, $defaultValues = false)
60  {
61  $storeId = $this->getAttribute()->getStoreId();
62  if ($storeId === null) {
63  $storeId = $this->getStoreManager()->getStore()->getId();
64  }
65  if (!is_array($this->_options)) {
66  $this->_options = [];
67  }
68  if (!is_array($this->_optionsDefault)) {
69  $this->_optionsDefault = [];
70  }
71  $attributeId = $this->getAttribute()->getId();
72  if (!isset($this->_options[$storeId][$attributeId])) {
73  $collection = $this->_attrOptionCollectionFactory->create()->setPositionOrder(
74  'asc'
75  )->setAttributeFilter(
76  $attributeId
77  )->setStoreFilter(
78  $storeId
79  )->load();
80  $this->_options[$storeId][$attributeId] = $collection->toOptionArray();
81  $this->_optionsDefault[$storeId][$attributeId] = $collection->toOptionArray('default_value');
82  }
83  $options = $defaultValues
84  ? $this->_optionsDefault[$storeId][$attributeId]
85  : $this->_options[$storeId][$attributeId];
86  if ($withEmpty) {
87  $options = $this->addEmptyOption($options);
88  }
89 
90  return $options;
91  }
92 
99  private function getStoreManager()
100  {
101  if ($this->storeManager === null) {
102  $this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
103  }
104  return $this->storeManager;
105  }
106 
114  public function getSpecificOptions($ids, $withEmpty = true)
115  {
116  $options = $this->_attrOptionCollectionFactory->create()
117  ->setPositionOrder('asc')
118  ->setAttributeFilter($this->getAttribute()->getId())
119  ->addFieldToFilter('main_table.option_id', ['in' => $ids])
120  ->setStoreFilter($this->getAttribute()->getStoreId())
121  ->load()
122  ->toOptionArray();
123  if ($withEmpty) {
124  $options = $this->addEmptyOption($options);
125  }
126  return $options;
127  }
128 
133  private function addEmptyOption(array $options)
134  {
135  array_unshift($options, ['label' => $this->getAttribute()->getIsRequired() ? '' : ' ', 'value' => '']);
136  return $options;
137  }
138 
145  public function getOptionText($value)
146  {
147  $isMultiple = false;
148  if (strpos($value, ',') !== false) {
149  $isMultiple = true;
150  $value = explode(',', $value);
151  }
152 
153  $options = $this->getSpecificOptions($value, false);
154 
155  if ($isMultiple) {
156  $values = [];
157  foreach ($options as $item) {
158  if (in_array($item['value'], $value)) {
159  $values[] = $item['label'];
160  }
161  }
162  return $values;
163  }
164 
165  foreach ($options as $item) {
166  if ($item['value'] == $value) {
167  return $item['label'];
168  }
169  }
170  return false;
171  }
172 
181  public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
182  {
183  $attribute = $this->getAttribute();
184  $valueTable1 = $attribute->getAttributeCode() . '_t1';
185  $valueTable2 = $attribute->getAttributeCode() . '_t2';
186  $linkField = $attribute->getEntity()->getLinkField();
187  $collection->getSelect()->joinLeft(
188  [$valueTable1 => $attribute->getBackend()->getTable()],
189  "e.{$linkField}={$valueTable1}." . $linkField .
190  " AND {$valueTable1}.attribute_id='{$attribute->getId()}'" .
191  " AND {$valueTable1}.store_id=0",
192  []
193  )->joinLeft(
194  [$valueTable2 => $attribute->getBackend()->getTable()],
195  "e.{$linkField}={$valueTable2}." . $linkField .
196  " AND {$valueTable2}.attribute_id='{$attribute->getId()}'" .
197  " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
198  []
199  );
200  $valueExpr = $collection->getSelect()->getConnection()->getCheckSql(
201  "{$valueTable2}.value_id > 0",
202  "{$valueTable2}.value",
203  "{$valueTable1}.value"
204  );
205 
206  $this->_attrOptionFactory->create()->addOptionValueToCollection(
207  $collection,
208  $attribute,
209  $valueExpr
210  );
211 
212  $collection->getSelect()->order("{$attribute->getAttributeCode()} {$dir}");
213 
214  return $this;
215  }
216 
222  public function getFlatColumns()
223  {
224  $columns = [];
225  $attributeCode = $this->getAttribute()->getAttributeCode();
226  $isMulti = $this->getAttribute()->getFrontend()->getInputType() == 'multiselect';
227 
230  'type' => $type,
231  'length' => $isMulti ? '255' : null,
232  'unsigned' => false,
233  'nullable' => true,
234  'default' => null,
235  'extra' => null,
236  'comment' => $attributeCode . ' column',
237  ];
238  if (!$isMulti) {
239  $columns[$attributeCode . '_value'] = [
241  'length' => 255,
242  'unsigned' => false,
243  'nullable' => true,
244  'default' => null,
245  'extra' => null,
246  'comment' => $attributeCode . ' column',
247  ];
248  }
249 
250  return $columns;
251  }
252 
258  public function getFlatIndexes()
259  {
260  $indexes = [];
261 
262  $index = sprintf('IDX_%s', strtoupper($this->getAttribute()->getAttributeCode()));
263  $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
264 
265  $sortable = $this->getAttribute()->getUsedForSortBy();
266  if ($sortable && $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
267  $index = sprintf('IDX_%s_VALUE', strtoupper($this->getAttribute()->getAttributeCode()));
268 
269  $indexes[$index] = [
270  'type' => 'index',
271  'fields' => [$this->getAttribute()->getAttributeCode() . '_value'],
272  ];
273  }
274 
275  return $indexes;
276  }
277 
284  public function getFlatUpdateSelect($store)
285  {
286  return $this->_attrOptionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store);
287  }
288 }
__construct(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory, \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory)
Definition: Table.php:44
getSpecificOptions($ids, $withEmpty=true)
Definition: Table.php:114
getAllOptions($withEmpty=true, $defaultValues=false)
Definition: Table.php:59
$values
Definition: options.phtml:88
addValueSortToCollection($collection, $dir=\Magento\Framework\DB\Select::SQL_ASC)
Definition: Table.php:181
$columns
Definition: default.phtml:15
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$index
Definition: list.phtml:44
const SQL_ASC
Definition: Select.php:81