Config.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. /**
  9. * @SuppressWarnings(PHPMD.LongVariable)
  10. * @SuppressWarnings(PHPMD.TooManyFields)
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class Config extends \Magento\Eav\Model\Config
  14. {
  15. const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
  16. /**
  17. * @var mixed
  18. */
  19. protected $_attributeSetsById;
  20. /**
  21. * @var mixed
  22. */
  23. protected $_attributeSetsByName;
  24. /**
  25. * @var mixed
  26. */
  27. protected $_attributeGroupsById;
  28. /**
  29. * @var mixed
  30. */
  31. protected $_attributeGroupsByName;
  32. /**
  33. * @var mixed
  34. */
  35. protected $_productTypesById;
  36. /**
  37. * Array of attributes codes needed for product load
  38. *
  39. * @var array
  40. */
  41. protected $_productAttributes;
  42. /**
  43. * Product Attributes used in product listing
  44. *
  45. * @var array
  46. */
  47. protected $_usedInProductListing;
  48. /**
  49. * Product Attributes For Sort By
  50. *
  51. * @var array
  52. */
  53. protected $_usedForSortBy;
  54. /**
  55. * @var int|float|string|null
  56. */
  57. protected $_storeId = null;
  58. /**
  59. * Core store config
  60. *
  61. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  62. */
  63. protected $_scopeConfig;
  64. /**
  65. * Eav config
  66. *
  67. * @var \Magento\Eav\Model\Config
  68. */
  69. protected $_eavConfig;
  70. /**
  71. * Store manager
  72. *
  73. * @var \Magento\Store\Model\StoreManagerInterface
  74. */
  75. protected $_storeManager;
  76. /**
  77. * Set collection factory
  78. *
  79. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory
  80. */
  81. protected $_setCollectionFactory;
  82. /**
  83. * Group collection factory
  84. *
  85. * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
  86. */
  87. protected $_groupCollectionFactory;
  88. /**
  89. * Product type factory
  90. *
  91. * @var \Magento\Catalog\Model\Product\TypeFactory
  92. */
  93. protected $_productTypeFactory;
  94. /**
  95. * Config factory
  96. *
  97. * @var \Magento\Catalog\Model\ResourceModel\ConfigFactory
  98. */
  99. protected $_configFactory;
  100. /**
  101. * Constructor
  102. *
  103. * @param \Magento\Framework\App\CacheInterface $cache
  104. * @param \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory
  105. * @param \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
  106. * @param \Magento\Framework\App\Cache\StateInterface $cacheState
  107. * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
  108. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  109. * @param \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory
  110. * @param \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory
  111. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory
  112. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory
  113. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  114. * @param \Magento\Eav\Model\Config $eavConfig
  115. * @param SerializerInterface $serializer
  116. *
  117. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  118. */
  119. public function __construct(
  120. \Magento\Framework\App\CacheInterface $cache,
  121. \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory,
  122. \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
  123. \Magento\Framework\App\Cache\StateInterface $cacheState,
  124. \Magento\Framework\Validator\UniversalFactory $universalFactory,
  125. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  126. \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory,
  127. \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory,
  128. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
  129. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory,
  130. \Magento\Store\Model\StoreManagerInterface $storeManager,
  131. \Magento\Eav\Model\Config $eavConfig,
  132. SerializerInterface $serializer = null
  133. ) {
  134. $this->_scopeConfig = $scopeConfig;
  135. $this->_configFactory = $configFactory;
  136. $this->_productTypeFactory = $productTypeFactory;
  137. $this->_groupCollectionFactory = $groupCollectionFactory;
  138. $this->_setCollectionFactory = $setCollectionFactory;
  139. $this->_storeManager = $storeManager;
  140. $this->_eavConfig = $eavConfig;
  141. parent::__construct(
  142. $cache,
  143. $entityTypeFactory,
  144. $entityTypeCollectionFactory,
  145. $cacheState,
  146. $universalFactory,
  147. $serializer
  148. );
  149. }
  150. /**
  151. * Initialize resource model
  152. *
  153. * @return void
  154. */
  155. protected function _construct()
  156. {
  157. $this->_init(\Magento\Catalog\Model\ResourceModel\Config::class);
  158. }
  159. /**
  160. * Set store id
  161. *
  162. * @param integer $storeId
  163. * @return \Magento\Catalog\Model\Config
  164. */
  165. public function setStoreId($storeId)
  166. {
  167. $this->_storeId = $storeId;
  168. return $this;
  169. }
  170. /**
  171. * Return store id, if is not set return current app store
  172. *
  173. * @return integer
  174. */
  175. public function getStoreId()
  176. {
  177. if ($this->_storeId === null) {
  178. return $this->_storeManager->getStore()->getId();
  179. }
  180. return $this->_storeId;
  181. }
  182. /**
  183. * @return $this
  184. */
  185. public function loadAttributeSets()
  186. {
  187. if ($this->_attributeSetsById) {
  188. return $this;
  189. }
  190. $attributeSetCollection = $this->_setCollectionFactory->create()->load();
  191. $this->_attributeSetsById = [];
  192. $this->_attributeSetsByName = [];
  193. foreach ($attributeSetCollection as $id => $attributeSet) {
  194. $entityTypeId = $attributeSet->getEntityTypeId();
  195. $name = $attributeSet->getAttributeSetName();
  196. $this->_attributeSetsById[$entityTypeId][$id] = $name;
  197. $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
  198. }
  199. return $this;
  200. }
  201. /**
  202. * @param string|int|float $entityTypeId
  203. * @param float|int $id
  204. * @return false|string
  205. */
  206. public function getAttributeSetName($entityTypeId, $id)
  207. {
  208. if (!is_numeric($id)) {
  209. return $id;
  210. }
  211. $this->loadAttributeSets();
  212. if (!is_numeric($entityTypeId)) {
  213. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  214. }
  215. return isset(
  216. $this->_attributeSetsById[$entityTypeId][$id]
  217. ) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
  218. }
  219. /**
  220. * @param string|int|float $entityTypeId
  221. * @param string|null $name
  222. * @return false|string|int
  223. */
  224. public function getAttributeSetId($entityTypeId, $name = null)
  225. {
  226. if (is_numeric($name)) {
  227. return $name;
  228. }
  229. $this->loadAttributeSets();
  230. if (!is_numeric($entityTypeId)) {
  231. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  232. }
  233. $name = strtolower($name);
  234. return isset(
  235. $this->_attributeSetsByName[$entityTypeId][$name]
  236. ) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
  237. }
  238. /**
  239. * @return $this
  240. */
  241. public function loadAttributeGroups()
  242. {
  243. if ($this->_attributeGroupsById) {
  244. return $this;
  245. }
  246. $attributeSetCollection = $this->_groupCollectionFactory->create()->load();
  247. $this->_attributeGroupsById = [];
  248. $this->_attributeGroupsByName = [];
  249. foreach ($attributeSetCollection as $id => $attributeGroup) {
  250. $attributeSetId = $attributeGroup->getAttributeSetId();
  251. $name = $attributeGroup->getAttributeGroupName();
  252. $this->_attributeGroupsById[$attributeSetId][$id] = $name;
  253. $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
  254. }
  255. return $this;
  256. }
  257. /**
  258. * @param float|int|string $attributeSetId
  259. * @param float|int|string $id
  260. * @return bool|string
  261. */
  262. public function getAttributeGroupName($attributeSetId, $id)
  263. {
  264. if (!is_numeric($id)) {
  265. return $id;
  266. }
  267. $this->loadAttributeGroups();
  268. if (!is_numeric($attributeSetId)) {
  269. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  270. }
  271. return isset(
  272. $this->_attributeGroupsById[$attributeSetId][$id]
  273. ) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
  274. }
  275. /**
  276. * @param float|int|string $attributeSetId
  277. * @param string $name
  278. * @return bool|string|int|float
  279. */
  280. public function getAttributeGroupId($attributeSetId, $name)
  281. {
  282. if (is_numeric($name)) {
  283. return $name;
  284. }
  285. $this->loadAttributeGroups();
  286. if (!is_numeric($attributeSetId)) {
  287. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  288. }
  289. $name = strtolower($name);
  290. return isset(
  291. $this->_attributeGroupsByName[$attributeSetId][$name]
  292. ) ? $this->_attributeGroupsByName[$attributeSetId][$name] : false;
  293. }
  294. /**
  295. * @return $this
  296. */
  297. public function loadProductTypes()
  298. {
  299. if ($this->_productTypesById) {
  300. return $this;
  301. }
  302. $productTypeCollection = $this->_productTypeFactory->create()->getOptionArray();
  303. $this->_productTypesById = [];
  304. $this->_productTypesByName = [];
  305. foreach ($productTypeCollection as $id => $type) {
  306. $name = $type;
  307. $this->_productTypesById[$id] = $name;
  308. $this->_productTypesByName[strtolower($name)] = $id;
  309. }
  310. return $this;
  311. }
  312. /**
  313. * @param string $name
  314. * @return false|string
  315. */
  316. public function getProductTypeId($name)
  317. {
  318. if (is_numeric($name)) {
  319. return $name;
  320. }
  321. $this->loadProductTypes();
  322. $name = strtolower($name);
  323. return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
  324. }
  325. /**
  326. * @param float|int|string $id
  327. * @return false|string
  328. */
  329. public function getProductTypeName($id)
  330. {
  331. if (!is_numeric($id)) {
  332. return $id;
  333. }
  334. $this->loadProductTypes();
  335. return $this->_productTypesById[$id] ?? false;
  336. }
  337. /**
  338. * @param \Magento\Framework\DataObject $source
  339. * @param string $value
  340. * @return null|mixed
  341. */
  342. public function getSourceOptionId($source, $value)
  343. {
  344. foreach ($source->getAllOptions() as $option) {
  345. if (strcasecmp($option['label'], $value) == 0 || $option['value'] == $value) {
  346. return $option['value'];
  347. }
  348. }
  349. return null;
  350. }
  351. /**
  352. * Load Product attributes
  353. *
  354. * @return array
  355. */
  356. public function getProductAttributes()
  357. {
  358. if ($this->_productAttributes === null) {
  359. $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
  360. }
  361. return $this->_productAttributes;
  362. }
  363. /**
  364. * Retrieve resource model
  365. *
  366. * @return \Magento\Catalog\Model\ResourceModel\Config
  367. */
  368. protected function _getResource()
  369. {
  370. return $this->_configFactory->create();
  371. }
  372. /**
  373. * Retrieve Attributes used in product listing
  374. *
  375. * @return array
  376. */
  377. public function getAttributesUsedInProductListing()
  378. {
  379. if ($this->_usedInProductListing === null) {
  380. $this->_usedInProductListing = [];
  381. $entityType = \Magento\Catalog\Model\Product::ENTITY;
  382. $attributesData = $this->_getResource()->setStoreId($this->getStoreId())->getAttributesUsedInListing();
  383. $this->_eavConfig->importAttributesData($entityType, $attributesData);
  384. foreach ($attributesData as $attributeData) {
  385. $attributeCode = $attributeData['attribute_code'];
  386. $this->_usedInProductListing[$attributeCode] = $this->_eavConfig->getAttribute(
  387. $entityType,
  388. $attributeCode
  389. );
  390. }
  391. }
  392. return $this->_usedInProductListing;
  393. }
  394. /**
  395. * Retrieve Attributes array used for sort by
  396. *
  397. * @return array
  398. */
  399. public function getAttributesUsedForSortBy()
  400. {
  401. if ($this->_usedForSortBy === null) {
  402. $this->_usedForSortBy = [];
  403. $entityType = \Magento\Catalog\Model\Product::ENTITY;
  404. $attributesData = $this->_getResource()->getAttributesUsedForSortBy();
  405. $this->_eavConfig->importAttributesData($entityType, $attributesData);
  406. foreach ($attributesData as $attributeData) {
  407. $attributeCode = $attributeData['attribute_code'];
  408. $this->_usedForSortBy[$attributeCode] = $this->_eavConfig->getAttribute($entityType, $attributeCode);
  409. }
  410. }
  411. return $this->_usedForSortBy;
  412. }
  413. /**
  414. * Retrieve Attributes Used for Sort by as array
  415. * key = code, value = name
  416. *
  417. * @return array
  418. */
  419. public function getAttributeUsedForSortByArray()
  420. {
  421. $options = ['position' => __('Position')];
  422. foreach ($this->getAttributesUsedForSortBy() as $attribute) {
  423. /* @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
  424. $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
  425. }
  426. return $options;
  427. }
  428. /**
  429. * Retrieve Product List Default Sort By
  430. *
  431. * @param mixed $store
  432. * @return string
  433. */
  434. public function getProductListDefaultSortBy($store = null)
  435. {
  436. return $this->_scopeConfig->getValue(
  437. self::XML_PATH_LIST_DEFAULT_SORT_BY,
  438. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  439. $store
  440. );
  441. }
  442. }