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_Product 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/product', 'entity_id');
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
00070 {
00071 if (!isset($this->_attributesCache[$attributeCode])) {
00072 $attribute = Mage::getSingleton('catalog/product')->getResource()->getAttribute($attributeCode);
00073
00074 $this->_attributesCache[$attributeCode] = array(
00075 'entity_type_id' => $attribute->getEntityTypeId(),
00076 'attribute_id' => $attribute->getId(),
00077 'table' => $attribute->getBackend()->getTable(),
00078 'is_global' => $attribute->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
00079 'backend_type' => $attribute->getBackendType()
00080 );
00081 }
00082
00083 $attribute = $this->_attributesCache[$attributeCode];
00084
00085 if (!$this->_select instanceof Zend_Db_Select) {
00086 return false;
00087 }
00088
00089 switch ($type) {
00090 case '=':
00091 $conditionRule = '=?';
00092 break;
00093 case 'in':
00094 $conditionRule = ' IN(?)';
00095 break;
00096 default:
00097 return false;
00098 break;
00099 }
00100
00101 if ($attribute['backend_type'] == 'static') {
00102 $this->_select->where('e.' . $attributeCode . $conditionRule, $value);
00103 }
00104 else {
00105 if ($attribute['is_global']) {
00106
00107 }
00108 else {
00109 $this->_select->join(
00110 array('t1_'.$attributeCode => $attribute['table']),
00111 'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0',
00112 array()
00113 )
00114 ->joinLeft(
00115 array('t2_'.$attributeCode => $attribute['table']),
00116 $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),
00117 array()
00118 )
00119 ->where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id'])
00120 ->where('IFNULL(t2_'.$attributeCode.'.value, t1_'.$attributeCode.'.value)'.$conditionRule, $value);
00121 }
00122 }
00123
00124 return $this->_select;
00125 }
00126
00127
00128
00129
00130
00131
00132
00133 public function getCollection($storeId)
00134 {
00135 $products = array();
00136
00137 $store = Mage::app()->getStore($storeId);
00138
00139
00140 if (!$store) {
00141 return false;
00142 }
00143
00144 $urCondions = array(
00145 'e.entity_id=ur.product_id',
00146 'ur.category_id IS NULL',
00147 $this->_getWriteAdapter()->quoteInto('ur.store_id=?', $store->getId()),
00148 $this->_getWriteAdapter()->quoteInto('ur.is_system=?', 1),
00149 );
00150 $this->_select = $this->_getWriteAdapter()->select()
00151 ->from(array('e' => $this->getMainTable()), array($this->getIdFieldName()))
00152 ->join(
00153 array('w' => $this->getTable('catalog/product_website')),
00154 'e.entity_id=w.product_id',
00155 array()
00156 )
00157 ->where('w.website_id=?', $store->getWebsiteId())
00158 ->joinLeft(
00159 array('ur' => $this->getTable('core/url_rewrite')),
00160 join(' AND ', $urCondions),
00161 array('url' => 'request_path')
00162 )
00163 ;
00164
00165 $this->_addFilter($storeId, 'visibility', Mage::getSingleton('catalog/product_visibility')->getVisibleInSearchIds(), 'in');
00166 $this->_addFilter($storeId, 'status', Mage::getSingleton('catalog/product_status')->getVisibleStatusIds(), 'in');
00167
00168 $query = $this->_getWriteAdapter()->query($this->_select);
00169 while ($row = $query->fetch()) {
00170 $product = $this->_prepareProduct($row);
00171 $products[$product->getId()] = $product;
00172 }
00173
00174 return $products;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183 protected function _prepareProduct(array $productRow)
00184 {
00185 $product = new Varien_Object();
00186 $product->setId($productRow[$this->getIdFieldName()]);
00187 $productUrl = !empty($productRow['url']) ? $productRow['url'] : 'catalog/product/view/id/' . $product->getId();
00188 $product->setUrl($productUrl);
00189 return $product;
00190 }
00191 }