PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/blingbang2016/shop
PHP | 361 lines | 216 code | 47 blank | 98 comment | 20 complexity | d2b67ebef5018fbe4806da55480fdf58 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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('catalog/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('eav/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('eav/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. /*
  171. $productTypeCollection = Mage::getResourceModel('catalog/product_type_collection')
  172. ->load();
  173. */
  174. $productTypeCollection = Mage::getModel('catalog/product_type')
  175. ->getOptionArray();
  176. $this->_productTypesById = array();
  177. $this->_productTypesByName = array();
  178. foreach ($productTypeCollection as $id=>$type) {
  179. //$name = $type->getCode();
  180. $name = $type;
  181. $this->_productTypesById[$id] = $name;
  182. $this->_productTypesByName[strtolower($name)] = $id;
  183. }
  184. return $this;
  185. }
  186. public function getProductTypeId($name)
  187. {
  188. if (is_numeric($name)) {
  189. return $name;
  190. }
  191. $this->loadProductTypes();
  192. $name = strtolower($name);
  193. return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
  194. }
  195. public function getProductTypeName($id)
  196. {
  197. if (!is_numeric($id)) {
  198. return $id;
  199. }
  200. $this->loadProductTypes();
  201. return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false;
  202. }
  203. public function getSourceOptionId($source, $value)
  204. {
  205. foreach ($source->getAllOptions() as $option) {
  206. if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
  207. return $option['value'];
  208. }
  209. }
  210. return null;
  211. }
  212. /**
  213. * Load Product attributes
  214. *
  215. * @return array
  216. */
  217. public function getProductAttributes()
  218. {
  219. if (is_null($this->_productAttributes)) {
  220. $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
  221. }
  222. return $this->_productAttributes;
  223. }
  224. /**
  225. * Retrieve Product Collection Attributes from XML config file
  226. * Used only for install/upgrade
  227. *
  228. * @return array
  229. */
  230. public function getProductCollectionAttributes() {
  231. $attributes = Mage::getConfig()
  232. ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES)
  233. ->asArray();
  234. return array_keys($attributes);;
  235. }
  236. /**
  237. * Retrieve resource model
  238. *
  239. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Config
  240. */
  241. protected function _getResource()
  242. {
  243. return Mage::getResourceModel('catalog/config');
  244. }
  245. /**
  246. * Retrieve Attributes used in product listing
  247. *
  248. * @return array
  249. */
  250. public function getAttributesUsedInProductListing() {
  251. if (is_null($this->_usedInProductListing)) {
  252. $this->_usedInProductListing = array();
  253. $entityType = Mage_Catalog_Model_Product::ENTITY;
  254. $attributesData = $this->_getResource()
  255. ->setStoreId($this->getStoreId())
  256. ->getAttributesUsedInListing();
  257. Mage::getSingleton('eav/config')
  258. ->importAttributesData($entityType, $attributesData);
  259. foreach ($attributesData as $attributeData) {
  260. $attributeCode = $attributeData['attribute_code'];
  261. $this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config')
  262. ->getAttribute($entityType, $attributeCode);
  263. }
  264. }
  265. return $this->_usedInProductListing;
  266. }
  267. /**
  268. * Retrieve Attributes array used for sort by
  269. *
  270. * @return array
  271. */
  272. public function getAttributesUsedForSortBy() {
  273. if (is_null($this->_usedForSortBy)) {
  274. $this->_usedForSortBy = array();
  275. $entityType = Mage_Catalog_Model_Product::ENTITY;
  276. $attributesData = $this->_getResource()
  277. ->getAttributesUsedForSortBy();
  278. Mage::getSingleton('eav/config')
  279. ->importAttributesData($entityType, $attributesData);
  280. foreach ($attributesData as $attributeData) {
  281. $attributeCode = $attributeData['attribute_code'];
  282. $this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config')
  283. ->getAttribute($entityType, $attributeCode);
  284. }
  285. }
  286. return $this->_usedForSortBy;
  287. }
  288. /**
  289. * Retrieve Attributes Used for Sort by as array
  290. * key = code, value = name
  291. *
  292. * @return array
  293. */
  294. public function getAttributeUsedForSortByArray()
  295. {
  296. $options = array(
  297. 'position' => Mage::helper('catalog')->__('Position')
  298. );
  299. foreach ($this->getAttributesUsedForSortBy() as $attribute) {
  300. /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
  301. $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
  302. }
  303. return $options;
  304. }
  305. /**
  306. * Retrieve Product List Default Sort By
  307. *
  308. * @param mixed $store
  309. * @return string
  310. */
  311. public function getProductListDefaultSortBy($store = null) {
  312. return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
  313. }
  314. }