Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Visibility.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
20 
22 
24 
25  const VISIBILITY_BOTH = 4;
26 
32  protected $_attribute;
33 
40 
47  public function __construct(
48  \Magento\Eav\Model\ResourceModel\Entity\Attribute $eavEntityAttribute,
49  array $data = []
50  ) {
51  $this->_eavEntityAttribute = $eavEntityAttribute;
52  parent::__construct($data);
53  }
54 
60  public function getVisibleInCatalogIds()
61  {
63  }
64 
70  public function getVisibleInSearchIds()
71  {
73  }
74 
80  public function getVisibleInSiteIds()
81  {
83  }
84 
90  public static function getOptionArray()
91  {
92  return [
93  self::VISIBILITY_NOT_VISIBLE => __('Not Visible Individually'),
94  self::VISIBILITY_IN_CATALOG => __('Catalog'),
95  self::VISIBILITY_IN_SEARCH => __('Search'),
96  self::VISIBILITY_BOTH => __('Catalog, Search')
97  ];
98  }
99 
105  public static function getAllOption()
106  {
108  array_unshift($options, ['value' => '', 'label' => '']);
109  return $options;
110  }
111 
117  public static function getAllOptions()
118  {
119  $res = [];
120  foreach (self::getOptionArray() as $index => $value) {
121  $res[] = ['value' => $index, 'label' => $value];
122  }
123  return $res;
124  }
125 
132  public static function getOptionText($optionId)
133  {
135  return isset($options[$optionId]) ? $options[$optionId] : null;
136  }
137 
143  public function getFlatColumns()
144  {
145  $attributeCode = $this->getAttribute()->getAttributeCode();
146 
147  return [
148  $attributeCode => [
149  'unsigned' => true,
150  'default' => null,
151  'extra' => null,
152  'type' => Table::TYPE_SMALLINT,
153  'nullable' => true,
154  'comment' => 'Catalog Product Visibility ' . $attributeCode . ' column',
155  ],
156  ];
157  }
158 
164  public function getFlatIndexes()
165  {
166  return [];
167  }
168 
175  public function getFlatUpdateSelect($store)
176  {
177  return $this->_eavEntityAttribute->getFlatUpdateSelect($this->getAttribute(), $store);
178  }
179 
186  public function setAttribute($attribute)
187  {
188  $this->_attribute = $attribute;
189  return $this;
190  }
191 
197  public function getAttribute()
198  {
199  return $this->_attribute;
200  }
201 
209  public function addValueSortToCollection($collection, $dir = 'asc')
210  {
211  $attributeCode = $this->getAttribute()->getAttributeCode();
212  $attributeId = $this->getAttribute()->getId();
213  $attributeTable = $this->getAttribute()->getBackend()->getTable();
214  $linkField = $this->getAttribute()->getEntity()->getLinkField();
215 
216  if ($this->getAttribute()->isScopeGlobal()) {
217  $tableName = $attributeCode . '_t';
218  $collection->getSelect()->joinLeft(
219  [$tableName => $attributeTable],
220  "e.{$linkField}={$tableName}.{$linkField}" .
221  " AND {$tableName}.attribute_id='{$attributeId}'" .
222  " AND {$tableName}.store_id='0'",
223  []
224  );
225  $valueExpr = $tableName . '.value';
226  } else {
227  $valueTable1 = $attributeCode . '_t1';
228  $valueTable2 = $attributeCode . '_t2';
229  $collection->getSelect()->joinLeft(
230  [$valueTable1 => $attributeTable],
231  "e.{$linkField}={$valueTable1}.{$linkField}" .
232  " AND {$valueTable1}.attribute_id='{$attributeId}'" .
233  " AND {$valueTable1}.store_id='0'",
234  []
235  )->joinLeft(
236  [$valueTable2 => $attributeTable],
237  "e.{$linkField}={$valueTable2}.{$linkField}" .
238  " AND {$valueTable2}.attribute_id='{$attributeId}'" .
239  " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
240  []
241  );
242  $valueExpr = $collection->getConnection()->getCheckSql(
243  $valueTable2 . '.value_id > 0',
244  $valueTable2 . '.value',
245  $valueTable1 . '.value'
246  );
247  }
248 
249  $collection->getSelect()->order($valueExpr . ' ' . $dir);
250  return $this;
251  }
252 
256  public function toOptionArray()
257  {
258  return $this->getAllOptions();
259  }
260 }
__construct(\Magento\Eav\Model\ResourceModel\Entity\Attribute $eavEntityAttribute, array $data=[])
Definition: Visibility.php:47
$tableName
Definition: trigger.php:13
addValueSortToCollection($collection, $dir='asc')
Definition: Visibility.php:209
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$index
Definition: list.phtml:44