1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 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: 42: 43: 44:
45: protected $_productAttributes;
46:
47: 48: 49: 50: 51:
52: protected $_usedInProductListing;
53:
54: 55: 56: 57: 58:
59: protected $_usedForSortBy;
60:
61: protected $_storeId = null;
62:
63: const XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES = 'frontend/product/collection/attributes';
64:
65: 66: 67: 68:
69: protected function _construct()
70: {
71: $this->_init('catalog/config');
72: }
73:
74: 75: 76: 77: 78: 79:
80: public function setStoreId($storeId)
81: {
82: $this->_storeId = $storeId;
83: return $this;
84: }
85:
86: 87: 88: 89: 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: 203: 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:
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: 254: 255: 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: 267: 268: 269: 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: 280: 281: 282:
283: protected function _getResource()
284: {
285: return Mage::getResourceModel('catalog/config');
286: }
287:
288: 289: 290: 291: 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: 313: 314: 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: 335: 336: 337: 338:
339: public function getAttributeUsedForSortByArray()
340: {
341: $options = array(
342: 'position' => Mage::helper('catalog')->__('Position')
343: );
344: foreach ($this->getAttributesUsedForSortBy() as $attribute) {
345:
346: $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
347: }
348:
349: return $options;
350: }
351:
352: 353: 354: 355: 356: 357:
358: public function getProductListDefaultSortBy($store = null) {
359: return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
360: }
361: }
362: