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: 29: 30: 31: 32: 33: 34:
35: class Mage_Sitemap_Model_Resource_Catalog_Product extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_select;
43:
44: 45: 46: 47: 48:
49: protected $_attributesCache = array();
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('catalog/product', 'entity_id');
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67: 68:
69: protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
70: {
71: if (!isset($this->_attributesCache[$attributeCode])) {
72: $attribute = Mage::getSingleton('catalog/product')->getResource()->getAttribute($attributeCode);
73:
74: $this->_attributesCache[$attributeCode] = array(
75: 'entity_type_id' => $attribute->getEntityTypeId(),
76: 'attribute_id' => $attribute->getId(),
77: 'table' => $attribute->getBackend()->getTable(),
78: 'is_global' => $attribute->getIsGlobal() == Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
79: 'backend_type' => $attribute->getBackendType()
80: );
81: }
82:
83: $attribute = $this->_attributesCache[$attributeCode];
84:
85: if (!$this->_select instanceof Zend_Db_Select) {
86: return false;
87: }
88:
89: switch ($type) {
90: case '=':
91: $conditionRule = '=?';
92: break;
93: case 'in':
94: $conditionRule = ' IN(?)';
95: break;
96: default:
97: return false;
98: break;
99: }
100:
101: if ($attribute['backend_type'] == 'static') {
102: $this->_select->where('e.' . $attributeCode . $conditionRule, $value);
103: } else {
104: $this->_select->join(
105: array('t1_'.$attributeCode => $attribute['table']),
106: 'e.entity_id=t1_'.$attributeCode.'.entity_id AND t1_'.$attributeCode.'.store_id=0',
107: array()
108: )
109: ->where('t1_'.$attributeCode.'.attribute_id=?', $attribute['attribute_id']);
110:
111: if ($attribute['is_global']) {
112: $this->_select->where('t1_'.$attributeCode.'.value'.$conditionRule, $value);
113: } else {
114: $ifCase = $this->_select->getAdapter()->getCheckSql('t2_'.$attributeCode.'.value_id > 0', 't2_'.$attributeCode.'.value', 't1_'.$attributeCode.'.value');
115: $this->_select->joinLeft(
116: array('t2_'.$attributeCode => $attribute['table']),
117: $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),
118: array()
119: )
120: ->where('('.$ifCase.')'.$conditionRule, $value);
121: }
122: }
123:
124: return $this->_select;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function getCollection($storeId)
134: {
135: $products = array();
136:
137: $store = Mage::app()->getStore($storeId);
138:
139:
140: if (!$store) {
141: return false;
142: }
143:
144: $urCondions = array(
145: 'e.entity_id=ur.product_id',
146: 'ur.category_id IS NULL',
147: $this->_getWriteAdapter()->quoteInto('ur.store_id=?', $store->getId()),
148: $this->_getWriteAdapter()->quoteInto('ur.is_system=?', 1),
149: );
150: $this->_select = $this->_getWriteAdapter()->select()
151: ->from(array('e' => $this->getMainTable()), array($this->getIdFieldName()))
152: ->join(
153: array('w' => $this->getTable('catalog/product_website')),
154: 'e.entity_id=w.product_id',
155: array()
156: )
157: ->where('w.website_id=?', $store->getWebsiteId())
158: ->joinLeft(
159: array('ur' => $this->getTable('core/url_rewrite')),
160: join(' AND ', $urCondions),
161: array('url' => 'request_path')
162: );
163:
164: $this->_addFilter($storeId, 'visibility', Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds(), 'in');
165: $this->_addFilter($storeId, 'status', Mage::getSingleton('catalog/product_status')->getVisibleStatusIds(), 'in');
166:
167: $query = $this->_getWriteAdapter()->query($this->_select);
168: while ($row = $query->fetch()) {
169: $product = $this->_prepareProduct($row);
170: $products[$product->getId()] = $product;
171: }
172:
173: return $products;
174: }
175:
176: 177: 178: 179: 180: 181:
182: protected function _prepareProduct(array $productRow)
183: {
184: $product = new Varien_Object();
185: $product->setId($productRow[$this->getIdFieldName()]);
186: $productUrl = !empty($productRow['url']) ? $productRow['url'] : 'catalog/product/view/id/' . $product->getId();
187: $product->setUrl($productUrl);
188: return $product;
189: }
190: }
191: