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
00029
00030
00031
00032
00033
00034
00035 class Mage_Sitemap_Model_Mysql4_Catalog_Category extends Mage_Core_Model_Mysql4_Abstract
00036 {
00037
00038
00039
00040
00041
00042 protected $_select;
00043
00044
00045
00046
00047
00048
00049 protected $_attributesCache = array();
00050
00051
00052
00053
00054 protected function _construct()
00055 {
00056 $this->_init('catalog/category', 'entity_id');
00057 }
00058
00059
00060
00061
00062
00063
00064 public function getCollection($storeId)
00065 {
00066 $categories = array();
00067
00068 $store = Mage::app()->getStore($storeId);
00069
00070
00071 if (!$store) {
00072 return false;
00073 }
00074
00075 $this->_select = $this->_getWriteAdapter()->select()
00076 ->from($this->getMainTable())
00077 ->where($this->getIdFieldName() . '=?', $store->getRootCategoryId());
00078 $categoryRow = $this->_getWriteAdapter()->fetchRow($this->_select);
00079
00080 if (!$categoryRow) {
00081 return false;
00082 }
00083
00084 $urConditions = array(
00085 'e.entity_id=ur.category_id',
00086 $this->_getWriteAdapter()->quoteInto('ur.store_id=?', $store->getId()),
00087 'ur.product_id IS NULL',
00088 $this->_getWriteAdapter()->quoteInto('ur.is_system=?', 1),
00089 );
00090 $this->_select = $this->_getWriteAdapter()->select()
00091 ->from(array('e' => $this->getMainTable()), array($this->getIdFieldName()))
00092 ->joinLeft(
00093 array('ur' => $this->getTable('core/url_rewrite')),
00094 join(' AND ', $urConditions),
00095 array('url'=>'request_path')
00096 )
00097 ->where('e.path LIKE ?', $categoryRow['path'] . '/%');
00098
00099 $this->_addFilter($storeId, 'is_active', 1);
00100
00101 $query = $this->_getWriteAdapter()->query($this->_select);
00102 while ($row = $query->fetch()) {
00103 $category = $this->_prepareCategory($row);
00104 $categories[$category->getId()] = $category;
00105 }
00106
00107 return $categories;
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 protected function _prepareCategory(array $categoryRow)
00117 {
00118 $category = new Varien_Object();
00119 $category->setId($categoryRow[$this->getIdFieldName()]);
00120 $categoryUrl = !empty($categoryRow['url']) ? $categoryRow['url'] : 'catalog/category/view/id/' . $category->getId();
00121 $category->setUrl($categoryUrl);
00122 return $category;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
00136 {
00137 if (!isset($this->_attributesCache[$attributeCode])) {
00138 $attribute = Mage::getSingleton('catalog/category')->getResource()->getAttribute($attributeCode);
00139
00140 $this->_attributesCache[$attributeCode] = array(
00141 'entity_type_id' => $attribute->getEntityTypeId(),
00142 'attribute_id' => $attribute->getId(),
00143 'table' => $attribute->getBackend()->getTable(),
00144 'is_global' => $attribute->getIsGlobal(),
00145 'backend_type' => $attribute->getBackendType()
00146 );
00147 }
00148
00149 $attribute = $this->_attributesCache[$attributeCode];
00150
00151 if (!$this->_select instanceof Zend_Db_Select) {
00152 return false;
00153 }
00154
00155 switch ($type) {
00156 case '=':
00157 $conditionRule = '=?';
00158 break;
00159 case 'in':
00160 $conditionRule = ' IN(?)';
00161 break;
00162 default:
00163 return false;
00164 break;
00165 }
00166
00167 if ($attribute['backend_type'] == 'static') {
00168 $this->_select->where('e.' . $attributeCode . $conditionRule, $value);
00169 }
00170 else {
00171 if ($attribute['is_global']) {
00172
00173 }
00174 else {
00175 $this->_select->join(
00176 array('t1_'.$attributeCode => $attribute['table']),
00177 'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0',
00178 array()
00179 )
00180 ->joinLeft(
00181 array('t2_'.$attributeCode => $attribute['table']),
00182 $this->_getWriteAdapter()->quoteInto('t1_'.$attributeCode.'.entity_id = t2_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.attribute_id = t2_'.$attributeCode.'.attribute_id AND t2_'.$attributeCode.'.store_id=?', $storeId),
00183 array()
00184 )
00185 ->where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id'])
00186 ->where('IFNULL(t2_'.$attributeCode.'.value, t1_'.$attributeCode.'.value)'.$conditionRule, $value);
00187 }
00188 }
00189
00190 return $this->_select;
00191 }
00192 }