00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Catalog_Model_Config extends Mage_Eav_Model_Config
00029 {
00030 const XML_PATH_LIST_DEFAULT_SORT_BY = 'catalog/frontend/default_sort_by';
00031
00032 protected $_attributeSetsById;
00033 protected $_attributeSetsByName;
00034
00035 protected $_attributeGroupsById;
00036 protected $_attributeGroupsByName;
00037
00038 protected $_productTypesById;
00039
00040
00041
00042
00043
00044
00045 protected $_productAttributes;
00046
00047
00048
00049
00050
00051
00052 protected $_usedInProductListing;
00053
00054
00055
00056
00057
00058
00059 protected $_usedForSortBy;
00060
00061 const XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES = 'frontend/product/collection/attributes';
00062
00063
00064
00065
00066
00067 protected function _construct()
00068 {
00069 $this->_init('catalog/config');
00070 }
00071
00072 public function loadAttributeSets()
00073 {
00074 if ($this->_attributeSetsById) {
00075 return $this;
00076 }
00077
00078 $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')
00079 ->load();
00080
00081 $this->_attributeSetsById = array();
00082 $this->_attributeSetsByName = array();
00083 foreach ($attributeSetCollection as $id=>$attributeSet) {
00084 $entityTypeId = $attributeSet->getEntityTypeId();
00085 $name = $attributeSet->getAttributeSetName();
00086 $this->_attributeSetsById[$entityTypeId][$id] = $name;
00087 $this->_attributeSetsByName[$entityTypeId][strtolower($name)] = $id;
00088 }
00089 return $this;
00090 }
00091
00092 public function getAttributeSetName($entityTypeId, $id)
00093 {
00094 if (!is_numeric($id)) {
00095 return $id;
00096 }
00097 $this->loadAttributeSets();
00098
00099 if (!is_numeric($entityTypeId)) {
00100 $entityTypeId = $this->getEntityType($entityTypeId)->getId();
00101 }
00102 return isset($this->_attributeSetsById[$entityTypeId][$id]) ? $this->_attributeSetsById[$entityTypeId][$id] : false;
00103 }
00104
00105 public function getAttributeSetId($entityTypeId, $name)
00106 {
00107 if (is_numeric($name)) {
00108 return $name;
00109 }
00110 $this->loadAttributeSets();
00111
00112 if (!is_numeric($entityTypeId)) {
00113 $entityTypeId = $this->getEntityType($entityTypeId)->getId();
00114 }
00115 $name = strtolower($name);
00116 return isset($this->_attributeSetsByName[$entityTypeId][$name]) ? $this->_attributeSetsByName[$entityTypeId][$name] : false;
00117 }
00118
00119 public function loadAttributeGroups()
00120 {
00121 if ($this->_attributeGroupsById) {
00122 return $this;
00123 }
00124
00125 $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_group_collection')
00126 ->load();
00127
00128 $this->_attributeGroupsById = array();
00129 $this->_attributeGroupsByName = array();
00130 foreach ($attributeSetCollection as $id=>$attributeGroup) {
00131 $attributeSetId = $attributeGroup->getAttributeSetId();
00132 $name = $attributeGroup->getAttributeGroupName();
00133 $this->_attributeGroupsById[$attributeSetId][$id] = $name;
00134 $this->_attributeGroupsByName[$attributeSetId][strtolower($name)] = $id;
00135 }
00136 return $this;
00137 }
00138
00139 public function getAttributeGroupName($attributeSetId, $id)
00140 {
00141 if (!is_numeric($id)) {
00142 return $id;
00143 }
00144
00145 $this->loadAttributeGroups();
00146
00147 if (!is_numeric($attributeSetId)) {
00148 $attributeSetId = $this->getAttributeSetId($attributeSetId);
00149 }
00150 return isset($this->_attributeGroupsById[$attributeSetId][$id]) ? $this->_attributeGroupsById[$attributeSetId][$id] : false;
00151 }
00152
00153 public function getAttributeGroupId($attributeSetId, $name)
00154 {
00155 if (is_numeric($name)) {
00156 return $name;
00157 }
00158
00159 $this->loadAttributeGroups();
00160
00161 if (!is_numeric($attributeSetId)) {
00162 $attributeSetId = $this->getAttributeSetId($attributeSetId);
00163 }
00164 $name = strtolower($name);
00165 return isset($this->_attributeGroupsById[$attributeSetId][$name]) ? $this->_attributeGroupsById[$attributeSetId][$name] : false;
00166 }
00167
00168 public function loadProductTypes()
00169 {
00170 if ($this->_productTypesById) {
00171 return $this;
00172 }
00173
00174
00175
00176
00177
00178 $productTypeCollection = Mage::getModel('catalog/product_type')
00179 ->getOptionArray();
00180
00181 $this->_productTypesById = array();
00182 $this->_productTypesByName = array();
00183 foreach ($productTypeCollection as $id=>$type) {
00184
00185 $name = $type;
00186 $this->_productTypesById[$id] = $name;
00187 $this->_productTypesByName[strtolower($name)] = $id;
00188 }
00189 return $this;
00190 }
00191
00192 public function getProductTypeId($name)
00193 {
00194 if (is_numeric($name)) {
00195 return $name;
00196 }
00197
00198 $this->loadProductTypes();
00199
00200 $name = strtolower($name);
00201 return isset($this->_productTypesByName[$name]) ? $this->_productTypesByName[$name] : false;
00202 }
00203
00204 public function getProductTypeName($id)
00205 {
00206 if (!is_numeric($id)) {
00207 return $id;
00208 }
00209
00210 $this->loadProductTypes();
00211
00212 return isset($this->_productTypesById[$id]) ? $this->_productTypesById[$id] : false;
00213 }
00214
00215 public function getSourceOptionId($source, $value)
00216 {
00217 foreach ($source->getAllOptions() as $option) {
00218 if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
00219 return $option['value'];
00220 }
00221 }
00222 return null;
00223 }
00224
00225
00226
00227
00228
00229
00230 public function getProductAttributes()
00231 {
00232 if (is_null($this->_productAttributes)) {
00233 $this->_productAttributes = array_keys($this->getAttributesUsedInProductListing());
00234 }
00235 return $this->_productAttributes;
00236 }
00237
00238
00239
00240
00241
00242
00243
00244 public function getProductCollectionAttributes() {
00245 $attributes = Mage::getConfig()
00246 ->getNode(self::XML_PATH_PRODUCT_COLLECTION_ATTRIBUTES)
00247 ->asArray();
00248 return array_keys($attributes);;
00249 }
00250
00251
00252
00253
00254
00255
00256 protected function _getResource()
00257 {
00258 return Mage::getResourceModel('catalog/config');
00259 }
00260
00261
00262
00263
00264
00265
00266 public function getAttributesUsedInProductListing() {
00267 if (is_null($this->_usedInProductListing)) {
00268 $this->_usedInProductListing = array();
00269 $entityType = 'catalog_product';
00270 $attributesData = $this->_getResource()
00271 ->getAttributesUsedInListing();
00272 Mage::getSingleton('eav/config')
00273 ->importAttributesData($entityType, $attributesData);
00274 foreach ($attributesData as $attributeData) {
00275 $attributeCode = $attributeData['attribute_code'];
00276 $this->_usedInProductListing[$attributeCode] = Mage::getSingleton('eav/config')
00277 ->getAttribute($entityType, $attributeCode);
00278 }
00279 }
00280 return $this->_usedInProductListing;
00281 }
00282
00283
00284
00285
00286
00287
00288 public function getAttributesUsedForSortBy() {
00289 if (is_null($this->_usedForSortBy)) {
00290 $this->_usedForSortBy = array();
00291 $entityType = 'catalog_product';
00292 $attributesData = $this->_getResource()
00293 ->getAttributesUsedForSortBy();
00294 Mage::getSingleton('eav/config')
00295 ->importAttributesData($entityType, $attributesData);
00296 foreach ($attributesData as $attributeData) {
00297 $attributeCode = $attributeData['attribute_code'];
00298 $this->_usedForSortBy[$attributeCode] = Mage::getSingleton('eav/config')
00299 ->getAttribute($entityType, $attributeCode);
00300 }
00301 }
00302 return $this->_usedForSortBy;
00303 }
00304
00305
00306
00307
00308
00309
00310
00311 public function getAttributeUsedForSortByArray()
00312 {
00313 $options = array(
00314 'position' => Mage::helper('catalog')->__('Position')
00315 );
00316 foreach ($this->getAttributesUsedForSortBy() as $attribute) {
00317
00318 $options[$attribute->getAttributeCode()] = Mage::helper('catalog')->__($attribute->getFrontendLabel());
00319 }
00320
00321 return $options;
00322 }
00323
00324
00325
00326
00327
00328
00329
00330 public function getProductListDefaultSortBy($store = null) {
00331 return Mage::getStoreConfig(self::XML_PATH_LIST_DEFAULT_SORT_BY, $store);
00332 }
00333 }