_init('catalog/config'); } /** * Set store id * * @param integer $storeId * @return Mage_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 Mage::app()->getStore()->getId(); } return $this->_storeId; } public function loadAttributeSets() { if ($this->_attributeSetsById) { return $this; } $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection') ->load(); $this->_attributeSetsById = array(); $this->_attributeSetsByName = array(); foreach ($attributeSetCollection as $id=>$attributeSet) { $entityTypeId = $attributeSet->getEntityTypeId(); $name = $attributeSet->getAttributeSetName(); $this->_attributeSetsById[$entityTypeId][$id] = $name; $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id; } return $this; } 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; } public function getAttributeSetId($entityTypeId, $name) { 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; } public function loadAttributeGroups() { if ($this->_attributeGroupsById) { return $this; } $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection') ->load(); $this->_attributeGroupsById = array(); $this->_attributeGroupsByName = array(); foreach ($attributeSetCollection as $id=>$attributeGroup) { $attributeSetId = $attributeGroup->getAttributeSetId(); $name = $attributeGroup->getAttributeGroupName(); $this->_attributeGroupsById[$attributeSetId][$id] = $name; $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id; } return $this; } 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; } 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; } public function loadProductTypes() { if ($this->_productTypesById) { return $this; } /* $productTypeCollection = Mage::getResourceModel('catalog/product_type_collection') ->load(); */ $productTypeCollection = Mage::getModel('catalog/product_type') ->getOptionArray(); $this->_productTypesById = array(); $this->_productTypesByName = array(); foreach ($productTypeCollection as $id=>$type) { //$name = $type->getCode(); $name = $type; $this->_productTypesById[$id] = $name; $this->_productTypesByName[strtolower($name)] = $id; } return $this; } public function getProductTypeId($name) { if (is_numeric($name)) { return $name; } $this->loadProductTypes(); $name = strtolower($name); return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false; } public function getProductTypeName($id) { if (!is_numeric($id)) { return $id; } $this->loadProductTypes(); return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false; } 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 (is_null($this->_productAttributes)) { $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing()); } return $this->_productAttributes; } /** * Retrieve Product Collection Attributes from XML config file * Used only for install/upgrade * * @return array */ public function getProductCollectionAttributes() { $attributes = Mage::getConfig() ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES) ->asArray(); return array_keys($attributes);; } /** * Retrieve resource model * * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Config */ protected function _getResource() { return Mage::getResourceModel('catalog/config'); } /** * Retrieve Attributes used in product listing * * @return array */ public function getAttributesUsedInProductListing() { if (is_null($this->_usedInProductListing)) { $this->_usedInProductListing = array(); $entityType = Mage_Catalog_Model_Product::ENTITY; $attributesData = $this->_getResource() ->setStoreId($this->getStoreId()) ->getAttributesUsedInListing(); Mage::getSingleton('eav/config') ->importAttributesData($entityType, $attributesData); foreach ($attributesData as $attributeData) { $attributeCode = $attributeData['attribute_code']; $this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config') ->getAttribute($entityType, $attributeCode); } } return $this->_usedInProductListing; } /** * Retrieve Attributes array used for sort by * * @return array */ public function getAttributesUsedForSortBy() { if (is_null($this->_usedForSortBy)) { $this->_usedForSortBy = array(); $entityType = Mage_Catalog_Model_Product::ENTITY; $attributesData = $this->_getResource() ->getAttributesUsedForSortBy(); Mage::getSingleton('eav/config') ->importAttributesData($entityType, $attributesData); foreach ($attributesData as $attributeData) { $attributeCode = $attributeData['attribute_code']; $this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config') ->getAttribute($entityType, $attributeCode); } } return $this->_usedForSortBy; } /** * Retrieve Attributes Used for Sort by as array * key = code, value = name * * @return array */ public function getAttributeUsedForSortByArray() { $options = array( 'position' => Mage::helper('catalog')->__('Position') ); foreach ($this->getAttributesUsedForSortBy() as $attribute) { /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */ $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel(); } return $options; } /** * Retrieve Product List Default Sort By * * @param mixed $store * @return string */ public function getProductListDefaultSortBy($store = null) { return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store); } }