/app/code/core/Mage/Catalog/Model/Config.php

https://bitbucket.org/jokusafet/magento2 · PHP · 356 lines · 216 code · 47 blank · 93 comment · 20 complexity · f771a299a94014c10318991d6ff7ed04 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Catalog_Model_Config extends Mage_Eav_Model_Config
  27. {
  28. const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
  29. protected $_attributeSetsById;
  30. protected $_attributeSetsByName;
  31. protected $_attributeGroupsById;
  32. protected $_attributeGroupsByName;
  33. protected $_productTypesById;
  34. /**
  35. * Array of attributes codes needed for product load
  36. *
  37. * @var array
  38. */
  39. protected $_productAttributes;
  40. /**
  41. * Product Attributes used in product listing
  42. *
  43. * @var array
  44. */
  45. protected $_usedInProductListing;
  46. /**
  47. * Product Attributes For Sort By
  48. *
  49. * @var array
  50. */
  51. protected $_usedForSortBy;
  52. protected $_storeId = null;
  53. const XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES = 'frontend/product/collection/attributes';
  54. /**
  55. * Initialize resource model
  56. *
  57. */
  58. protected function _construct()
  59. {
  60. $this->_init('Mage_Catalog_Model_Resource_Config');
  61. }
  62. /**
  63. * Set store id
  64. *
  65. * @param integer $storeId
  66. * @return Mage_Catalog_Model_Config
  67. */
  68. public function setStoreId($storeId)
  69. {
  70. $this->_storeId = $storeId;
  71. return $this;
  72. }
  73. /**
  74. * Return store id, if is not set return current app store
  75. *
  76. * @return integer
  77. */
  78. public function getStoreId()
  79. {
  80. if ($this->_storeId === null) {
  81. return Mage::app()->getStore()->getId();
  82. }
  83. return $this->_storeId;
  84. }
  85. public function loadAttributeSets()
  86. {
  87. if ($this->_attributeSetsById) {
  88. return $this;
  89. }
  90. $attributeSetCollection = Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection')
  91. ->load();
  92. $this->_attributeSetsById = array();
  93. $this->_attributeSetsByName = array();
  94. foreach ($attributeSetCollection as $id=>$attributeSet) {
  95. $entityTypeId = $attributeSet->getEntityTypeId();
  96. $name = $attributeSet->getAttributeSetName();
  97. $this->_attributeSetsById[$entityTypeId][$id] = $name;
  98. $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
  99. }
  100. return $this;
  101. }
  102. public function getAttributeSetName($entityTypeId, $id)
  103. {
  104. if (!is_numeric($id)) {
  105. return $id;
  106. }
  107. $this->loadAttributeSets();
  108. if (!is_numeric($entityTypeId)) {
  109. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  110. }
  111. return isset($this->_attributeSetsById[$entityTypeId][$id]) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
  112. }
  113. public function getAttributeSetId($entityTypeId, $name)
  114. {
  115. if (is_numeric($name)) {
  116. return $name;
  117. }
  118. $this->loadAttributeSets();
  119. if (!is_numeric($entityTypeId)) {
  120. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  121. }
  122. $name = strtolower($name);
  123. return isset($this->_attributeSetsByName[$entityTypeId][$name]) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
  124. }
  125. public function loadAttributeGroups()
  126. {
  127. if ($this->_attributeGroupsById) {
  128. return $this;
  129. }
  130. $attributeSetCollection = Mage::getResourceModel('Mage_Eav_Model_Resource_Entity_Attribute_Group_Collection')
  131. ->load();
  132. $this->_attributeGroupsById = array();
  133. $this->_attributeGroupsByName = array();
  134. foreach ($attributeSetCollection as $id=>$attributeGroup) {
  135. $attributeSetId = $attributeGroup->getAttributeSetId();
  136. $name = $attributeGroup->getAttributeGroupName();
  137. $this->_attributeGroupsById[$attributeSetId][$id] = $name;
  138. $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
  139. }
  140. return $this;
  141. }
  142. public function getAttributeGroupName($attributeSetId, $id)
  143. {
  144. if (!is_numeric($id)) {
  145. return $id;
  146. }
  147. $this->loadAttributeGroups();
  148. if (!is_numeric($attributeSetId)) {
  149. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  150. }
  151. return isset($this->_attributeGroupsById[$attributeSetId][$id]) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
  152. }
  153. public function getAttributeGroupId($attributeSetId, $name)
  154. {
  155. if (is_numeric($name)) {
  156. return $name;
  157. }
  158. $this->loadAttributeGroups();
  159. if (!is_numeric($attributeSetId)) {
  160. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  161. }
  162. $name = strtolower($name);
  163. return isset($this->_attributeGroupsByName[$attributeSetId][$name]) ? $this->_attributeGroupsByName[$attributeSetId][$name] : false;
  164. }
  165. public function loadProductTypes()
  166. {
  167. if ($this->_productTypesById) {
  168. return $this;
  169. }
  170. $productTypeCollection = Mage::getModel('Mage_Catalog_Model_Product_Type')
  171. ->getOptionArray();
  172. $this->_productTypesById = array();
  173. $this->_productTypesByName = array();
  174. foreach ($productTypeCollection as $id=>$type) {
  175. $name = $type;
  176. $this->_productTypesById[$id] = $name;
  177. $this->_productTypesByName[strtolower($name)] = $id;
  178. }
  179. return $this;
  180. }
  181. public function getProductTypeId($name)
  182. {
  183. if (is_numeric($name)) {
  184. return $name;
  185. }
  186. $this->loadProductTypes();
  187. $name = strtolower($name);
  188. return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
  189. }
  190. public function getProductTypeName($id)
  191. {
  192. if (!is_numeric($id)) {
  193. return $id;
  194. }
  195. $this->loadProductTypes();
  196. return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false;
  197. }
  198. public function getSourceOptionId($source, $value)
  199. {
  200. foreach ($source->getAllOptions() as $option) {
  201. if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
  202. return $option['value'];
  203. }
  204. }
  205. return null;
  206. }
  207. /**
  208. * Load Product attributes
  209. *
  210. * @return array
  211. */
  212. public function getProductAttributes()
  213. {
  214. if (is_null($this->_productAttributes)) {
  215. $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
  216. }
  217. return $this->_productAttributes;
  218. }
  219. /**
  220. * Retrieve Product Collection Attributes from XML config file
  221. * Used only for install/upgrade
  222. *
  223. * @return array
  224. */
  225. public function getProductCollectionAttributes() {
  226. $attributes = Mage::getConfig()
  227. ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES)
  228. ->asArray();
  229. return array_keys($attributes);;
  230. }
  231. /**
  232. * Retrieve resource model
  233. *
  234. * @return Mage_Catalog_Model_Resource_Config
  235. */
  236. protected function _getResource()
  237. {
  238. return Mage::getResourceModel('Mage_Catalog_Model_Resource_Config');
  239. }
  240. /**
  241. * Retrieve Attributes used in product listing
  242. *
  243. * @return array
  244. */
  245. public function getAttributesUsedInProductListing() {
  246. if (is_null($this->_usedInProductListing)) {
  247. $this->_usedInProductListing = array();
  248. $entityType = Mage_Catalog_Model_Product::ENTITY;
  249. $attributesData = $this->_getResource()
  250. ->setStoreId($this->getStoreId())
  251. ->getAttributesUsedInListing();
  252. Mage::getSingleton('Mage_Eav_Model_Config')
  253. ->importAttributesData($entityType, $attributesData);
  254. foreach ($attributesData as $attributeData) {
  255. $attributeCode = $attributeData['attribute_code'];
  256. $this->_usedInProductListing[$attributeCode] = Mage::getSingleton('Mage_Eav_Model_Config')
  257. ->getAttribute($entityType, $attributeCode);
  258. }
  259. }
  260. return $this->_usedInProductListing;
  261. }
  262. /**
  263. * Retrieve Attributes array used for sort by
  264. *
  265. * @return array
  266. */
  267. public function getAttributesUsedForSortBy() {
  268. if (is_null($this->_usedForSortBy)) {
  269. $this->_usedForSortBy = array();
  270. $entityType = Mage_Catalog_Model_Product::ENTITY;
  271. $attributesData = $this->_getResource()
  272. ->getAttributesUsedForSortBy();
  273. Mage::getSingleton('Mage_Eav_Model_Config')
  274. ->importAttributesData($entityType, $attributesData);
  275. foreach ($attributesData as $attributeData) {
  276. $attributeCode = $attributeData['attribute_code'];
  277. $this->_usedForSortBy[$attributeCode] = Mage::getSingleton('Mage_Eav_Model_Config')
  278. ->getAttribute($entityType, $attributeCode);
  279. }
  280. }
  281. return $this->_usedForSortBy;
  282. }
  283. /**
  284. * Retrieve Attributes Used for Sort by as array
  285. * key = code, value = name
  286. *
  287. * @return array
  288. */
  289. public function getAttributeUsedForSortByArray()
  290. {
  291. $options = array(
  292. 'position' => Mage::helper('Mage_Catalog_Helper_Data')->__('Position')
  293. );
  294. foreach ($this->getAttributesUsedForSortBy() as $attribute) {
  295. /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
  296. $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
  297. }
  298. return $options;
  299. }
  300. /**
  301. * Retrieve Product List Default Sort By
  302. *
  303. * @param mixed $store
  304. * @return string
  305. */
  306. public function getProductListDefaultSortBy($store = null) {
  307. return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
  308. }
  309. }