Guest User

config.php

a guest
Dec 30th, 2014
513
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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) 2014 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26.  
  27.  
  28. class Mage_Catalog_Model_Config extends Mage_Eav_Model_Config
  29. {
  30. const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
  31.  
  32. protected $_attributeSetsById;
  33. protected $_attributeSetsByName;
  34.  
  35. protected $_attributeGroupsById;
  36. protected $_attributeGroupsByName;
  37.  
  38. protected $_productTypesById;
  39.  
  40. /**
  41. * Array of attributes codes needed for product load
  42. *
  43. * @var array
  44. */
  45. protected $_productAttributes;
  46.  
  47. /**
  48. * Product Attributes used in product listing
  49. *
  50. * @var array
  51. */
  52. protected $_usedInProductListing;
  53.  
  54. /**
  55. * Product Attributes For Sort By
  56. *
  57. * @var array
  58. */
  59. protected $_usedForSortBy;
  60.  
  61. protected $_storeId = null;
  62.  
  63. const XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES = 'frontend/product/collection/attributes';
  64.  
  65. /**
  66. * Initialize resource model
  67. *
  68. */
  69. protected function _construct()
  70. {
  71. $this->_init('catalog/config');
  72. }
  73.  
  74. /**
  75. * Set store id
  76. *
  77. * @param integer $storeId
  78. * @return Mage_Catalog_Model_Config
  79. */
  80. public function setStoreId($storeId)
  81. {
  82. $this->_storeId = $storeId;
  83. return $this;
  84. }
  85.  
  86. /**
  87. * Return store id, if is not set return current app store
  88. *
  89. * @return integer
  90. */
  91. public function getStoreId()
  92. {
  93. if ($this->_storeId === null) {
  94. return Mage::app()->getStore()->getId();
  95. }
  96. return $this->_storeId;
  97. }
  98.  
  99. public function loadAttributeSets()
  100. {
  101. if ($this->_attributeSetsById) {
  102. return $this;
  103. }
  104.  
  105. $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
  106. ->load();
  107.  
  108. $this->_attributeSetsById = array();
  109. $this->_attributeSetsByName = array();
  110. foreach ($attributeSetCollection as $id=>$attributeSet) {
  111. $entityTypeId = $attributeSet->getEntityTypeId();
  112. $name = $attributeSet->getAttributeSetName();
  113. $this->_attributeSetsById[$entityTypeId][$id] = $name;
  114. $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
  115. }
  116. return $this;
  117. }
  118.  
  119. public function getAttributeSetName($entityTypeId, $id)
  120. {
  121. if (!is_numeric($id)) {
  122. return $id;
  123. }
  124. $this->loadAttributeSets();
  125.  
  126. if (!is_numeric($entityTypeId)) {
  127. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  128. }
  129. return isset($this->_attributeSetsById[$entityTypeId][$id]) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
  130. }
  131.  
  132. public function getAttributeSetId($entityTypeId, $name)
  133. {
  134. if (is_numeric($name)) {
  135. return $name;
  136. }
  137. $this->loadAttributeSets();
  138.  
  139. if (!is_numeric($entityTypeId)) {
  140. $entityTypeId = $this->getEntityType($entityTypeId)->getId();
  141. }
  142. $name = strtolower($name);
  143. return isset($this->_attributeSetsByName[$entityTypeId][$name]) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
  144. }
  145.  
  146. public function loadAttributeGroups()
  147. {
  148. if ($this->_attributeGroupsById) {
  149. return $this;
  150. }
  151.  
  152. $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
  153. ->load();
  154.  
  155. $this->_attributeGroupsById = array();
  156. $this->_attributeGroupsByName = array();
  157. foreach ($attributeSetCollection as $id=>$attributeGroup) {
  158. $attributeSetId = $attributeGroup->getAttributeSetId();
  159. $name = $attributeGroup->getAttributeGroupName();
  160. $this->_attributeGroupsById[$attributeSetId][$id] = $name;
  161. $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
  162. }
  163. return $this;
  164. }
  165.  
  166. public function getAttributeGroupName($attributeSetId, $id)
  167. {
  168. if (!is_numeric($id)) {
  169. return $id;
  170. }
  171.  
  172. $this->loadAttributeGroups();
  173.  
  174. if (!is_numeric($attributeSetId)) {
  175. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  176. }
  177. return isset($this->_attributeGroupsById[$attributeSetId][$id]) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
  178. }
  179.  
  180. public function getAttributeGroupId($attributeSetId, $name)
  181. {
  182. if (is_numeric($name)) {
  183. return $name;
  184. }
  185.  
  186. $this->loadAttributeGroups();
  187.  
  188. if (!is_numeric($attributeSetId)) {
  189. $attributeSetId = $this->getAttributeSetId($attributeSetId);
  190. }
  191. $name = strtolower($name);
  192. return isset($this->_attributeGroupsByName[$attributeSetId][$name]) ? $this->_attributeGroupsByName[$attributeSetId][$name] : false;
  193. }
  194.  
  195. public function loadProductTypes()
  196. {
  197. if ($this->_productTypesById) {
  198. return $this;
  199. }
  200.  
  201. /*
  202. $productTypeCollection = Mage::getResourceModel('catalog/product_type_collection')
  203. ->load();
  204. */
  205. $productTypeCollection = Mage::getModel('catalog/product_type')
  206. ->getOptionArray();
  207.  
  208. $this->_productTypesById = array();
  209. $this->_productTypesByName = array();
  210. foreach ($productTypeCollection as $id=>$type) {
  211. //$name = $type->getCode();
  212. $name = $type;
  213. $this->_productTypesById[$id] = $name;
  214. $this->_productTypesByName[strtolower($name)] = $id;
  215. }
  216. return $this;
  217. }
  218.  
  219. public function getProductTypeId($name)
  220. {
  221. if (is_numeric($name)) {
  222. return $name;
  223. }
  224.  
  225. $this->loadProductTypes();
  226.  
  227. $name = strtolower($name);
  228. return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
  229. }
  230.  
  231. public function getProductTypeName($id)
  232. {
  233. if (!is_numeric($id)) {
  234. return $id;
  235. }
  236.  
  237. $this->loadProductTypes();
  238.  
  239. return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false;
  240. }
  241.  
  242. public function getSourceOptionId($source, $value)
  243. {
  244. foreach ($source->getAllOptions() as $option) {
  245. if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
  246. return $option['value'];
  247. }
  248. }
  249. return null;
  250. }
  251.  
  252. /**
  253. * Load Product attributes
  254. *
  255. * @return array
  256. */
  257. public function getProductAttributes()
  258. {
  259. if (is_null($this->_productAttributes)) {
  260. $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
  261. }
  262. return $this->_productAttributes;
  263. }
  264.  
  265. /**
  266. * Retrieve Product Collection Attributes from XML config file
  267. * Used only for install/upgrade
  268. *
  269. * @return array
  270. */
  271. public function getProductCollectionAttributes() {
  272. $attributes = Mage::getConfig()
  273. ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES)
  274. ->asArray();
  275. return array_keys($attributes);;
  276. }
  277.  
  278. /**
  279. * Retrieve resource model
  280. *
  281. * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Config
  282. */
  283. protected function _getResource()
  284. {
  285. return Mage::getResourceModel('catalog/config');
  286. }
  287.  
  288. /**
  289. * Retrieve Attributes used in product listing
  290. *
  291. * @return array
  292. */
  293. public function getAttributesUsedInProductListing() {
  294. if (is_null($this->_usedInProductListing)) {
  295. $this->_usedInProductListing = array();
  296. $entityType = Mage_Catalog_Model_Product::ENTITY;
  297. $attributesData = $this->_getResource()
  298. ->setStoreId($this->getStoreId())
  299. ->getAttributesUsedInListing();
  300. Mage::getSingleton('eav/config')
  301. ->importAttributesData($entityType, $attributesData);
  302. foreach ($attributesData as $attributeData) {
  303. $attributeCode = $attributeData['attribute_code'];
  304. $this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config')
  305. ->getAttribute($entityType, $attributeCode);
  306. }
  307. }
  308. return $this->_usedInProductListing;
  309. }
  310.  
  311. /**
  312. * Retrieve Attributes array used for sort by
  313. *
  314. * @return array
  315. */
  316. public function getAttributesUsedForSortBy() {
  317. if (is_null($this->_usedForSortBy)) {
  318. $this->_usedForSortBy = array();
  319. $entityType = Mage_Catalog_Model_Product::ENTITY;
  320. $attributesData = $this->_getResource()
  321. ->getAttributesUsedForSortBy();
  322. Mage::getSingleton('eav/config')
  323. ->importAttributesData($entityType, $attributesData);
  324. foreach ($attributesData as $attributeData) {
  325. $attributeCode = $attributeData['attribute_code'];
  326. $this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config')
  327. ->getAttribute($entityType, $attributeCode);
  328. }
  329. }
  330. return $this->_usedForSortBy;
  331. }
  332.  
  333. /**
  334. * Retrieve Attributes Used for Sort by as array
  335. * key = code, value = name
  336. *
  337. * @return array
  338. */
  339. public function getAttributeUsedForSortByArray()
  340. {
  341. $options = array(
  342. 'position' => Mage::helper('catalog')->__('Position')
  343. 'rating_summary' => Mage::helper('catalog')->__('Rating')
  344. );
  345. foreach ($this->getAttributesUsedForSortBy() as $attribute) {
  346. /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
  347. $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
  348. }
  349.  
  350. return $options;
  351. }
  352.  
  353. /**
  354. * Retrieve Product List Default Sort By
  355. *
  356. * @param mixed $store
  357. * @return string
  358. */
  359. public function getProductListDefaultSortBy($store = null) {
  360. return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
  361. }
  362. }
RAW Paste Data