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:
36: class Mage_Catalog_Model_Resource_Collection_Abstract extends Mage_Eav_Model_Entity_Collection_Abstract
37: {
38: 39: 40: 41: 42:
43: protected $_storeId;
44:
45: 46: 47: 48: 49: 50:
51: public function setStore($store)
52: {
53: $this->setStoreId(Mage::app()->getStore($store)->getId());
54: return $this;
55: }
56:
57: 58: 59: 60: 61: 62:
63: public function setStoreId($storeId)
64: {
65: if ($storeId instanceof Mage_Core_Model_Store) {
66: $storeId = $storeId->getId();
67: }
68: $this->_storeId = (int)$storeId;
69: return $this;
70: }
71:
72: 73: 74: 75: 76:
77: public function getStoreId()
78: {
79: if (is_null($this->_storeId)) {
80: $this->setStoreId(Mage::app()->getStore()->getId());
81: }
82: return $this->_storeId;
83: }
84:
85: 86: 87: 88: 89:
90: public function getDefaultStoreId()
91: {
92: return Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID;
93: }
94:
95: 96: 97: 98: 99: 100: 101:
102: protected function _getLoadAttributesSelect($table, $attributeIds = array())
103: {
104: if (empty($attributeIds)) {
105: $attributeIds = $this->_selectAttributes;
106: }
107: $storeId = $this->getStoreId();
108:
109: if ($storeId) {
110:
111: $adapter = $this->getConnection();
112: $entityIdField = $this->getEntity()->getEntityIdField();
113: $joinCondition = array(
114: 't_s.attribute_id = t_d.attribute_id',
115: 't_s.entity_id = t_d.entity_id',
116: $adapter->quoteInto('t_s.store_id = ?', $storeId)
117: );
118: $select = $adapter->select()
119: ->from(array('t_d' => $table), array($entityIdField, 'attribute_id'))
120: ->joinLeft(
121: array('t_s' => $table),
122: implode(' AND ', $joinCondition),
123: array())
124: ->where('t_d.entity_type_id = ?', $this->getEntity()->getTypeId())
125: ->where("t_d.{$entityIdField} IN (?)", array_keys($this->_itemsById))
126: ->where('t_d.attribute_id IN (?)', $attributeIds)
127: ->where('t_d.store_id = ?', 0);
128: } else {
129: $select = parent::_getLoadAttributesSelect($table)
130: ->where('store_id = ?', $this->getDefaultStoreId());
131: }
132:
133: return $select;
134: }
135:
136: 137: 138: 139: 140: 141:
142: protected function _addLoadAttributesSelectValues($select, $table, $type)
143: {
144: $storeId = $this->getStoreId();
145: if ($storeId) {
146: $helper = Mage::getResourceHelper('eav');
147: $adapter = $this->getConnection();
148: $valueExpr = $adapter->getCheckSql(
149: 't_s.value_id IS NULL',
150: $helper->prepareEavAttributeValue('t_d.value', $type),
151: $helper->prepareEavAttributeValue('t_s.value', $type)
152: );
153:
154: $select->columns(array(
155: 'default_value' => $helper->prepareEavAttributeValue('t_d.value', $type),
156: 'store_value' => $helper->prepareEavAttributeValue('t_s.value', $type),
157: 'value' => $valueExpr
158: ));
159: } else {
160: $select = parent::_addLoadAttributesSelectValues($select, $table, $type);
161: }
162: return $select;
163: }
164:
165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: protected function _joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
177: {
178: if (isset($this->_joinAttributes[$fieldCode]['store_id'])) {
179: $store_id = $this->_joinAttributes[$fieldCode]['store_id'];
180: } else {
181: $store_id = $this->getStoreId();
182: }
183:
184: $adapter = $this->getConnection();
185:
186: if ($store_id != $this->getDefaultStoreId() && !$attribute->isScopeGlobal()) {
187: 188: 189: 190:
191: $defCondition = '('.implode(') AND (', $condition).')';
192: $defAlias = $tableAlias . '_default';
193: $defAlias = $this->getConnection()->getTableName($defAlias);
194: $defFieldAlias= str_replace($tableAlias, $defAlias, $fieldAlias);
195: $tableAlias = $this->getConnection()->getTableName($tableAlias);
196:
197: $defCondition = str_replace($tableAlias, $defAlias, $defCondition);
198: $defCondition.= $adapter->quoteInto(
199: " AND " . $adapter->quoteColumnAs("$defAlias.store_id", null) . " = ?",
200: $this->getDefaultStoreId());
201:
202: $this->getSelect()->$method(
203: array($defAlias => $attribute->getBackend()->getTable()),
204: $defCondition,
205: array()
206: );
207:
208: $method = 'joinLeft';
209: $fieldAlias = $this->getConnection()->getCheckSql("{$tableAlias}.value_id > 0",
210: $fieldAlias, $defFieldAlias);
211: $this->_joinAttributes[$fieldCode]['condition_alias'] = $fieldAlias;
212: $this->_joinAttributes[$fieldCode]['attribute'] = $attribute;
213: } else {
214: $store_id = $this->getDefaultStoreId();
215: }
216: $condition[] = $adapter->quoteInto(
217: $adapter->quoteColumnAs("$tableAlias.store_id", null) . ' = ?', $store_id);
218: return parent::_joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias);
219: }
220: }
221: