123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Catalog\Model;
- use Magento\Framework\Serialize\SerializerInterface;
- /**
- * @SuppressWarnings(PHPMD.LongVariable)
- * @SuppressWarnings(PHPMD.TooManyFields)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Config extends \Magento\Eav\Model\Config
- {
- const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
- /**
- * @var mixed
- */
- protected $_attributeSetsById;
- /**
- * @var mixed
- */
- protected $_attributeSetsByName;
- /**
- * @var mixed
- */
- protected $_attributeGroupsById;
- /**
- * @var mixed
- */
- protected $_attributeGroupsByName;
- /**
- * @var mixed
- */
- protected $_productTypesById;
- /**
- * Array of attributes codes needed for product load
- *
- * @var array
- */
- protected $_productAttributes;
- /**
- * Product Attributes used in product listing
- *
- * @var array
- */
- protected $_usedInProductListing;
- /**
- * Product Attributes For Sort By
- *
- * @var array
- */
- protected $_usedForSortBy;
- /**
- * @var int|float|string|null
- */
- protected $_storeId = null;
- /**
- * Core store config
- *
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_scopeConfig;
- /**
- * Eav config
- *
- * @var \Magento\Eav\Model\Config
- */
- protected $_eavConfig;
- /**
- * Store manager
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * Set collection factory
- *
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory
- */
- protected $_setCollectionFactory;
- /**
- * Group collection factory
- *
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory
- */
- protected $_groupCollectionFactory;
- /**
- * Product type factory
- *
- * @var \Magento\Catalog\Model\Product\TypeFactory
- */
- protected $_productTypeFactory;
- /**
- * Config factory
- *
- * @var \Magento\Catalog\Model\ResourceModel\ConfigFactory
- */
- protected $_configFactory;
- /**
- * Constructor
- *
- * @param \Magento\Framework\App\CacheInterface $cache
- * @param \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory
- * @param \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
- * @param \Magento\Framework\App\Cache\StateInterface $cacheState
- * @param \Magento\Framework\Validator\UniversalFactory $universalFactory
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory
- * @param \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Eav\Model\Config $eavConfig
- * @param SerializerInterface $serializer
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\App\CacheInterface $cache,
- \Magento\Eav\Model\Entity\TypeFactory $entityTypeFactory,
- \Magento\Eav\Model\ResourceModel\Entity\Type\CollectionFactory $entityTypeCollectionFactory,
- \Magento\Framework\App\Cache\StateInterface $cacheState,
- \Magento\Framework\Validator\UniversalFactory $universalFactory,
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
- \Magento\Catalog\Model\ResourceModel\ConfigFactory $configFactory,
- \Magento\Catalog\Model\Product\TypeFactory $productTypeFactory,
- \Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory $groupCollectionFactory,
- \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setCollectionFactory,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Eav\Model\Config $eavConfig,
- SerializerInterface $serializer = null
- ) {
- $this->_scopeConfig = $scopeConfig;
- $this->_configFactory = $configFactory;
- $this->_productTypeFactory = $productTypeFactory;
- $this->_groupCollectionFactory = $groupCollectionFactory;
- $this->_setCollectionFactory = $setCollectionFactory;
- $this->_storeManager = $storeManager;
- $this->_eavConfig = $eavConfig;
- parent::__construct(
- $cache,
- $entityTypeFactory,
- $entityTypeCollectionFactory,
- $cacheState,
- $universalFactory,
- $serializer
- );
- }
- /**
- * Initialize resource model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init(\Magento\Catalog\Model\ResourceModel\Config::class);
- }
- /**
- * Set store id
- *
- * @param integer $storeId
- * @return \Magento\Catalog\Model\Config
- */
- public function setStoreId($storeId)
- {
- $this->_storeId = $storeId;
- return $this;
- }
- /**
- * Return store id, if is not set return current app store
- *
- * @return integer
- */
- public function getStoreId()
- {
- if ($this->_storeId === null) {
- return $this->_storeManager->getStore()->getId();
- }
- return $this->_storeId;
- }
- /**
- * @return $this
- */
- public function loadAttributeSets()
- {
- if ($this->_attributeSetsById) {
- return $this;
- }
- $attributeSetCollection = $this->_setCollectionFactory->create()->load();
- $this->_attributeSetsById = [];
- $this->_attributeSetsByName = [];
- foreach ($attributeSetCollection as $id => $attributeSet) {
- $entityTypeId = $attributeSet->getEntityTypeId();
- $name = $attributeSet->getAttributeSetName();
- $this->_attributeSetsById[$entityTypeId][$id] = $name;
- $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
- }
- return $this;
- }
- /**
- * @param string|int|float $entityTypeId
- * @param float|int $id
- * @return false|string
- */
- public function getAttributeSetName($entityTypeId, $id)
- {
- if (!is_numeric($id)) {
- return $id;
- }
- $this->loadAttributeSets();
- if (!is_numeric($entityTypeId)) {
- $entityTypeId = $this->getEntityType($entityTypeId)->getId();
- }
- return isset(
- $this->_attributeSetsById[$entityTypeId][$id]
- ) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
- }
- /**
- * @param string|int|float $entityTypeId
- * @param string|null $name
- * @return false|string|int
- */
- public function getAttributeSetId($entityTypeId, $name = null)
- {
- if (is_numeric($name)) {
- return $name;
- }
- $this->loadAttributeSets();
- if (!is_numeric($entityTypeId)) {
- $entityTypeId = $this->getEntityType($entityTypeId)->getId();
- }
- $name = strtolower($name);
- return isset(
- $this->_attributeSetsByName[$entityTypeId][$name]
- ) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
- }
- /**
- * @return $this
- */
- public function loadAttributeGroups()
- {
- if ($this->_attributeGroupsById) {
- return $this;
- }
- $attributeSetCollection = $this->_groupCollectionFactory->create()->load();
- $this->_attributeGroupsById = [];
- $this->_attributeGroupsByName = [];
- foreach ($attributeSetCollection as $id => $attributeGroup) {
- $attributeSetId = $attributeGroup->getAttributeSetId();
- $name = $attributeGroup->getAttributeGroupName();
- $this->_attributeGroupsById[$attributeSetId][$id] = $name;
- $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
- }
- return $this;
- }
- /**
- * @param float|int|string $attributeSetId
- * @param float|int|string $id
- * @return bool|string
- */
- public function getAttributeGroupName($attributeSetId, $id)
- {
- if (!is_numeric($id)) {
- return $id;
- }
- $this->loadAttributeGroups();
- if (!is_numeric($attributeSetId)) {
- $attributeSetId = $this->getAttributeSetId($attributeSetId);
- }
- return isset(
- $this->_attributeGroupsById[$attributeSetId][$id]
- ) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
- }
- /**
- * @param float|int|string $attributeSetId
- * @param string $name
- * @return bool|string|int|float
- */
- public function getAttributeGroupId($attributeSetId, $name)
- {
- if (is_numeric($name)) {
- return $name;
- }
- $this->loadAttributeGroups();
- if (!is_numeric($attributeSetId)) {
- $attributeSetId = $this->getAttributeSetId($attributeSetId);
- }
- $name = strtolower($name);
- return isset(
- $this->_attributeGroupsByName[$attributeSetId][$name]
- ) ? $this->_attributeGroupsByName[$attributeSetId][$name] : false;
- }
- /**
- * @return $this
- */
- public function loadProductTypes()
- {
- if ($this->_productTypesById) {
- return $this;
- }
- $productTypeCollection = $this->_productTypeFactory->create()->getOptionArray();
- $this->_productTypesById = [];
- $this->_productTypesByName = [];
- foreach ($productTypeCollection as $id => $type) {
- $name = $type;
- $this->_productTypesById[$id] = $name;
- $this->_productTypesByName[strtolower($name)] = $id;
- }
- return $this;
- }
- /**
- * @param string $name
- * @return false|string
- */
- public function getProductTypeId($name)
- {
- if (is_numeric($name)) {
- return $name;
- }
- $this->loadProductTypes();
- $name = strtolower($name);
- return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
- }
- /**
- * @param float|int|string $id
- * @return false|string
- */
- public function getProductTypeName($id)
- {
- if (!is_numeric($id)) {
- return $id;
- }
- $this->loadProductTypes();
- return $this->_productTypesById[$id] ?? false;
- }
- /**
- * @param \Magento\Framework\DataObject $source
- * @param string $value
- * @return null|mixed
- */
- public function getSourceOptionId($source, $value)
- {
- foreach ($source->getAllOptions() as $option) {
- if (strcasecmp($option['label'], $value) == 0 || $option['value'] == $value) {
- return $option['value'];
- }
- }
- return null;
- }
- /**
- * Load Product attributes
- *
- * @return array
- */
- public function getProductAttributes()
- {
- if ($this->_productAttributes === null) {
- $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
- }
- return $this->_productAttributes;
- }
- /**
- * Retrieve resource model
- *
- * @return \Magento\Catalog\Model\ResourceModel\Config
- */
- protected function _getResource()
- {
- return $this->_configFactory->create();
- }
- /**
- * Retrieve Attributes used in product listing
- *
- * @return array
- */
- public function getAttributesUsedInProductListing()
- {
- if ($this->_usedInProductListing === null) {
- $this->_usedInProductListing = [];
- $entityType = \Magento\Catalog\Model\Product::ENTITY;
- $attributesData = $this->_getResource()->setStoreId($this->getStoreId())->getAttributesUsedInListing();
- $this->_eavConfig->importAttributesData($entityType, $attributesData);
- foreach ($attributesData as $attributeData) {
- $attributeCode = $attributeData['attribute_code'];
- $this->_usedInProductListing[$attributeCode] = $this->_eavConfig->getAttribute(
- $entityType,
- $attributeCode
- );
- }
- }
- return $this->_usedInProductListing;
- }
- /**
- * Retrieve Attributes array used for sort by
- *
- * @return array
- */
- public function getAttributesUsedForSortBy()
- {
- if ($this->_usedForSortBy === null) {
- $this->_usedForSortBy = [];
- $entityType = \Magento\Catalog\Model\Product::ENTITY;
- $attributesData = $this->_getResource()->getAttributesUsedForSortBy();
- $this->_eavConfig->importAttributesData($entityType, $attributesData);
- foreach ($attributesData as $attributeData) {
- $attributeCode = $attributeData['attribute_code'];
- $this->_usedForSortBy[$attributeCode] = $this->_eavConfig->getAttribute($entityType, $attributeCode);
- }
- }
- return $this->_usedForSortBy;
- }
- /**
- * Retrieve Attributes Used for Sort by as array
- * key = code, value = name
- *
- * @return array
- */
- public function getAttributeUsedForSortByArray()
- {
- $options = ['position' => __('Position')];
- foreach ($this->getAttributesUsedForSortBy() as $attribute) {
- /* @var $attribute \Magento\Eav\Model\Entity\Attribute\AbstractAttribute */
- $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
- }
- return $options;
- }
- /**
- * Retrieve Product List Default Sort By
- *
- * @param mixed $store
- * @return string
- */
- public function getProductListDefaultSortBy($store = null)
- {
- return $this->_scopeConfig->getValue(
- self::XML_PATH_LIST_DEFAULT_SORT_BY,
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
- $store
- );
- }
- }
|