Table.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. use Magento\Framework\App\ObjectManager;
  8. use Magento\Store\Model\StoreManagerInterface;
  9. /**
  10. * Eav attribute default source when values are coming from another table
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Table extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
  16. {
  17. /**
  18. * Default values for option cache
  19. *
  20. * @var array
  21. */
  22. protected $_optionsDefault = [];
  23. /**
  24. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory
  25. */
  26. protected $_attrOptionCollectionFactory;
  27. /**
  28. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory
  29. */
  30. protected $_attrOptionFactory;
  31. /**
  32. * @var StoreManagerInterface
  33. */
  34. private $storeManager;
  35. /**
  36. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  37. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
  38. * @codeCoverageIgnore
  39. */
  40. public function __construct(
  41. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  42. \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
  43. ) {
  44. $this->_attrOptionCollectionFactory = $attrOptionCollectionFactory;
  45. $this->_attrOptionFactory = $attrOptionFactory;
  46. }
  47. /**
  48. * Retrieve Full Option values array
  49. *
  50. * @param bool $withEmpty Add empty option to array
  51. * @param bool $defaultValues
  52. * @return array
  53. */
  54. public function getAllOptions($withEmpty = true, $defaultValues = false)
  55. {
  56. $storeId = $this->getAttribute()->getStoreId();
  57. if ($storeId === null) {
  58. $storeId = $this->getStoreManager()->getStore()->getId();
  59. }
  60. if (!is_array($this->_options)) {
  61. $this->_options = [];
  62. }
  63. if (!is_array($this->_optionsDefault)) {
  64. $this->_optionsDefault = [];
  65. }
  66. $attributeId = $this->getAttribute()->getId();
  67. if (!isset($this->_options[$storeId][$attributeId])) {
  68. $collection = $this->_attrOptionCollectionFactory->create()->setPositionOrder(
  69. 'asc'
  70. )->setAttributeFilter(
  71. $attributeId
  72. )->setStoreFilter(
  73. $storeId
  74. )->load();
  75. $this->_options[$storeId][$attributeId] = $collection->toOptionArray();
  76. $this->_optionsDefault[$storeId][$attributeId] = $collection->toOptionArray('default_value');
  77. }
  78. $options = $defaultValues
  79. ? $this->_optionsDefault[$storeId][$attributeId]
  80. : $this->_options[$storeId][$attributeId];
  81. if ($withEmpty) {
  82. $options = $this->addEmptyOption($options);
  83. }
  84. return $options;
  85. }
  86. /**
  87. * Get StoreManager dependency
  88. *
  89. * @return StoreManagerInterface
  90. * @deprecated 100.1.6
  91. */
  92. private function getStoreManager()
  93. {
  94. if ($this->storeManager === null) {
  95. $this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
  96. }
  97. return $this->storeManager;
  98. }
  99. /**
  100. * Retrieve Option values array by ids
  101. *
  102. * @param string|array $ids
  103. * @param bool $withEmpty Add empty option to array
  104. * @return array
  105. */
  106. public function getSpecificOptions($ids, $withEmpty = true)
  107. {
  108. $options = $this->_attrOptionCollectionFactory->create()
  109. ->setPositionOrder('asc')
  110. ->setAttributeFilter($this->getAttribute()->getId())
  111. ->addFieldToFilter('main_table.option_id', ['in' => $ids])
  112. ->setStoreFilter($this->getAttribute()->getStoreId())
  113. ->load()
  114. ->toOptionArray();
  115. if ($withEmpty) {
  116. $options = $this->addEmptyOption($options);
  117. }
  118. return $options;
  119. }
  120. /**
  121. * Add an empty option to the array
  122. *
  123. * @param array $options
  124. * @return array
  125. */
  126. private function addEmptyOption(array $options)
  127. {
  128. array_unshift($options, ['label' => ' ', 'value' => '']);
  129. return $options;
  130. }
  131. /**
  132. * Get a text for option value
  133. *
  134. * @param string|integer $value
  135. * @return array|string|bool
  136. */
  137. public function getOptionText($value)
  138. {
  139. $isMultiple = false;
  140. if (strpos($value, ',') !== false) {
  141. $isMultiple = true;
  142. $value = explode(',', $value);
  143. }
  144. $options = $this->getSpecificOptions($value, false);
  145. if ($isMultiple) {
  146. $values = [];
  147. foreach ($options as $item) {
  148. if (in_array($item['value'], $value)) {
  149. $values[] = $item['label'];
  150. }
  151. }
  152. return $values;
  153. }
  154. foreach ($options as $item) {
  155. if ($item['value'] == $value) {
  156. return $item['label'];
  157. }
  158. }
  159. return false;
  160. }
  161. /**
  162. * Add Value Sort To Collection Select
  163. *
  164. * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
  165. * @param string $dir
  166. *
  167. * @return $this
  168. */
  169. public function addValueSortToCollection($collection, $dir = \Magento\Framework\DB\Select::SQL_ASC)
  170. {
  171. $attribute = $this->getAttribute();
  172. $valueTable1 = $attribute->getAttributeCode() . '_t1';
  173. $valueTable2 = $attribute->getAttributeCode() . '_t2';
  174. $linkField = $attribute->getEntity()->getLinkField();
  175. $collection->getSelect()->joinLeft(
  176. [$valueTable1 => $attribute->getBackend()->getTable()],
  177. "e.{$linkField}={$valueTable1}." . $linkField .
  178. " AND {$valueTable1}.attribute_id='{$attribute->getId()}'" .
  179. " AND {$valueTable1}.store_id=0",
  180. []
  181. )->joinLeft(
  182. [$valueTable2 => $attribute->getBackend()->getTable()],
  183. "e.{$linkField}={$valueTable2}." . $linkField .
  184. " AND {$valueTable2}.attribute_id='{$attribute->getId()}'" .
  185. " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
  186. []
  187. );
  188. $valueExpr = $collection->getSelect()->getConnection()->getCheckSql(
  189. "{$valueTable2}.value_id > 0",
  190. "{$valueTable2}.value",
  191. "{$valueTable1}.value"
  192. );
  193. $this->_attrOptionFactory->create()->addOptionValueToCollection(
  194. $collection,
  195. $attribute,
  196. $valueExpr
  197. );
  198. $collection->getSelect()->order("{$attribute->getAttributeCode()} {$dir}");
  199. return $this;
  200. }
  201. /**
  202. * Retrieve Column(s) for Flat
  203. *
  204. * @return array
  205. */
  206. public function getFlatColumns()
  207. {
  208. $columns = [];
  209. $attributeCode = $this->getAttribute()->getAttributeCode();
  210. $isMulti = $this->getAttribute()->getFrontend()->getInputType() == 'multiselect';
  211. $type = $isMulti ? \Magento\Framework\DB\Ddl\Table::TYPE_TEXT : \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER;
  212. $columns[$attributeCode] = [
  213. 'type' => $type,
  214. 'length' => $isMulti ? '255' : null,
  215. 'unsigned' => false,
  216. 'nullable' => true,
  217. 'default' => null,
  218. 'extra' => null,
  219. 'comment' => $attributeCode . ' column',
  220. ];
  221. if (!$isMulti) {
  222. $columns[$attributeCode . '_value'] = [
  223. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  224. 'length' => 255,
  225. 'unsigned' => false,
  226. 'nullable' => true,
  227. 'default' => null,
  228. 'extra' => null,
  229. 'comment' => $attributeCode . ' column',
  230. ];
  231. }
  232. return $columns;
  233. }
  234. /**
  235. * Retrieve Indexes for Flat
  236. *
  237. * @return array
  238. */
  239. public function getFlatIndexes()
  240. {
  241. $indexes = [];
  242. $index = sprintf('IDX_%s', strtoupper($this->getAttribute()->getAttributeCode()));
  243. $indexes[$index] = ['type' => 'index', 'fields' => [$this->getAttribute()->getAttributeCode()]];
  244. $sortable = $this->getAttribute()->getUsedForSortBy();
  245. if ($sortable && $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
  246. $index = sprintf('IDX_%s_VALUE', strtoupper($this->getAttribute()->getAttributeCode()));
  247. $indexes[$index] = [
  248. 'type' => 'index',
  249. 'fields' => [$this->getAttribute()->getAttributeCode() . '_value'],
  250. ];
  251. }
  252. return $indexes;
  253. }
  254. /**
  255. * Retrieve Select For Flat Attribute update
  256. *
  257. * @param int $store
  258. * @return \Magento\Framework\DB\Select|null
  259. */
  260. public function getFlatUpdateSelect($store)
  261. {
  262. return $this->_attrOptionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store);
  263. }
  264. }