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

https://github.com/ronseigel/agent-ohm · PHP · 318 lines · 187 code · 45 blank · 86 comment · 19 complexity · f7c5d422350be90da4c9968131e338f0 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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.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. const XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES = 'frontend/product/collection/attributes';
  53. /**
  54. * Initialize resource model
  55. *
  56. */
  57. protected function _construct()
  58. {
  59. $this->_init('catalog/config');
  60. }
  61. public function loadAttributeSets()
  62. {
  63. if ($this->_attributeSetsById) {
  64. return $this;
  65. }
  66. $attributeSetCollection = AO::getResourceModel('eav/entity_attribute_set_collection')
  67. ->load();
  68. $this->_attributeSetsById = array();
  69. $this->_attributeSetsByName = array();
  70. foreach ($attributeSetCollection as $id=>$attributeSet) {
  71. $entityTypeId = $attributeSet->getEntityTypeId();
  72. $name = $attributeSet->getAttributeSetName();
  73. $this->_attributeSetsById[$entityTypeId][$id] = $name;
  74. $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
  75. }
  76. return $this;
  77. }
  78. public function getAttributeSetName($entityTypeId, $id)
  79. {
  80. if (!is_numeric($id)) {
  81. return $id;
  82. }
  83. $this->loadAttributeSets();
  84. if (!is_numeric($entityTypeId)) {
  85. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  86. }
  87. return isset($this->_attributeSetsById[$entityTypeId][$id]) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
  88. }
  89. public function getAttributeSetId($entityTypeId, $name)
  90. {
  91. if (is_numeric($name)) {
  92. return $name;
  93. }
  94. $this->loadAttributeSets();
  95. if (!is_numeric($entityTypeId)) {
  96. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  97. }
  98. $name = strtolower($name);
  99. return isset($this->_attributeSetsByName[$entityTypeId][$name]) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
  100. }
  101. public function loadAttributeGroups()
  102. {
  103. if ($this->_attributeGroupsById) {
  104. return $this;
  105. }
  106. $attributeSetCollection = AO::getResourceModel('eav/entity_attribute_group_collection')
  107. ->load();
  108. $this->_attributeGroupsById = array();
  109. $this->_attributeGroupsByName = array();
  110. foreach ($attributeSetCollection as $id=>$attributeGroup) {
  111. $attributeSetId = $attributeGroup->getAttributeSetId();
  112. $name = $attributeGroup->getAttributeGroupName();
  113. $this->_attributeGroupsById[$attributeSetId][$id] = $name;
  114. $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
  115. }
  116. return $this;
  117. }
  118. public function getAttributeGroupName($attributeSetId, $id)
  119. {
  120. if (!is_numeric($id)) {
  121. return $id;
  122. }
  123. $this->loadAttributeGroups();
  124. if (!is_numeric($attributeSetId)) {
  125. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  126. }
  127. return isset($this->_attributeGroupsById[$attributeSetId][$id]) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
  128. }
  129. public function getAttributeGroupId($attributeSetId, $name)
  130. {
  131. if (is_numeric($name)) {
  132. return $name;
  133. }
  134. $this->loadAttributeGroups();
  135. if (!is_numeric($attributeSetId)) {
  136. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  137. }
  138. $name = strtolower($name);
  139. return isset($this->_attributeGroupsById[$attributeSetId][$name]) ? $this->_attributeGroupsById[$attributeSetId][$name] : false;
  140. }
  141. public function loadProductTypes()
  142. {
  143. if ($this->_productTypesById) {
  144. return $this;
  145. }
  146. /*
  147. $productTypeCollection = AO::getResourceModel('catalog/product_type_collection')
  148. ->load();
  149. */
  150. $productTypeCollection = AO::getModel('catalog/product_type')
  151. ->getOptionArray();
  152. $this->_productTypesById = array();
  153. $this->_productTypesByName = array();
  154. foreach ($productTypeCollection as $id=>$type) {
  155. //$name = $type->getCode();
  156. $name = $type;
  157. $this->_productTypesById[$id] = $name;
  158. $this->_productTypesByName[strtolower($name)] = $id;
  159. }
  160. return $this;
  161. }
  162. public function getProductTypeId($name)
  163. {
  164. if (is_numeric($name)) {
  165. return $name;
  166. }
  167. $this->loadProductTypes();
  168. $name = strtolower($name);
  169. return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
  170. }
  171. public function getProductTypeName($id)
  172. {
  173. if (!is_numeric($id)) {
  174. return $id;
  175. }
  176. $this->loadProductTypes();
  177. return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false;
  178. }
  179. public function getSourceOptionId($source, $value)
  180. {
  181. foreach ($source->getAllOptions() as $option) {
  182. if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
  183. return $option['value'];
  184. }
  185. }
  186. return null;
  187. }
  188. /**
  189. * Load Product attributes
  190. *
  191. * @return array
  192. */
  193. public function getProductAttributes()
  194. {
  195. if (is_null($this->_productAttributes)) {
  196. $this->_productAttributes = array();
  197. foreach ($this->getAttributesUsedInProductListing() as $attribute) {
  198. $this->_productAttributes[] = $attribute['attribute_code'];
  199. }
  200. }
  201. return $this->_productAttributes;
  202. }
  203. /**
  204. * Retrieve Product Collection Attributes from XML config file
  205. * Used only for install/upgrade
  206. *
  207. * @return array
  208. */
  209. public function getProductCollectionAttributes() {
  210. $attributes = AO::getConfig()
  211. ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES)
  212. ->asArray();
  213. return array_keys($attributes);;
  214. }
  215. /**
  216. * Retrieve resource model
  217. *
  218. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Config
  219. */
  220. protected function _getResource()
  221. {
  222. return AO::getResourceModel('catalog/config');
  223. }
  224. /**
  225. * Retrieve Attributes used in product listing
  226. *
  227. * @return array
  228. */
  229. public function getAttributesUsedInProductListing() {
  230. if (is_null($this->_usedInProductListing)) {
  231. $this->_usedInProductListing = $this->_getResource()
  232. ->getAttributesUsedInListing();
  233. }
  234. return $this->_usedInProductListing;
  235. }
  236. /**
  237. * Retrieve Attributes array used for sort by
  238. *
  239. * @return array
  240. */
  241. public function getAttributesUsedForSortBy() {
  242. if (is_null($this->_usedForSortBy)) {
  243. $this->_usedForSortBy = $this->_getResource()
  244. ->getAttributesUsedForSortBy();
  245. }
  246. return $this->_usedForSortBy;
  247. }
  248. /**
  249. * Retrieve Attributes Used for Sort by as array
  250. * key = code, value = name
  251. *
  252. * @return array
  253. */
  254. public function getAttributeUsedForSortByArray()
  255. {
  256. $options = array(
  257. 'position' => AO::helper('catalog')->__('Position')
  258. );
  259. foreach ($this->getAttributesUsedForSortBy() as $attribute) {
  260. $options[$attribute['attribute_code']] = AO::helper('catalog')->__($attribute['frontend_label']);
  261. }
  262. return $options;
  263. }
  264. /**
  265. * Retrieve Product List Default Sort By
  266. *
  267. * @param mixed $store
  268. * @return string
  269. */
  270. public function getProductListDefaultSortBy($store = null) {
  271. return AO::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
  272. }
  273. }