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: abstract class Mage_Eav_Model_Resource_Attribute_Collection
36: extends Mage_Eav_Model_Resource_Entity_Attribute_Collection
37: {
38: 39: 40:
41: const EAV_CODE_PASSWORD_HASH = 'password_hash';
42:
43: 44: 45: 46: 47:
48: protected $_website;
49:
50: 51: 52: 53: 54:
55: protected $_entityType;
56:
57: 58: 59: 60: 61:
62: abstract protected function _getEntityTypeCode();
63:
64: 65: 66: 67: 68: 69: 70: 71:
72: abstract protected function _getEavWebsiteTable();
73:
74: 75: 76: 77: 78:
79: public function getEntityType()
80: {
81: if ($this->_entityType === null) {
82: $this->_entityType = Mage::getSingleton('eav/config')->getEntityType($this->_getEntityTypeCode());
83: }
84: return $this->_entityType;
85: }
86:
87: 88: 89: 90: 91: 92:
93: public function setWebsite($website)
94: {
95: $this->_website = Mage::app()->getWebsite($website);
96: $this->addBindParam('scope_website_id', $this->_website->getId());
97: return $this;
98: }
99:
100: 101: 102: 103: 104:
105: public function getWebsite()
106: {
107: if ($this->_website === null) {
108: $this->_website = Mage::app()->getStore()->getWebsite();
109: }
110: return $this->_website;
111: }
112:
113: 114: 115: 116: 117:
118: protected function _initSelect()
119: {
120: $select = $this->getSelect();
121: $connection = $this->getConnection();
122: $entityType = $this->getEntityType();
123: $extraTable = $entityType->getAdditionalAttributeTable();
124: $mainDescribe = $this->getConnection()->describeTable($this->getResource()->getMainTable());
125: $mainColumns = array();
126:
127: foreach (array_keys($mainDescribe) as $columnName) {
128: $mainColumns[$columnName] = $columnName;
129: }
130:
131: $select->from(array('main_table' => $this->getResource()->getMainTable()), $mainColumns);
132:
133:
134: $extraDescribe = $connection->describeTable($this->getTable($extraTable));
135: $extraColumns = array();
136: foreach (array_keys($extraDescribe) as $columnName) {
137: if (isset($mainColumns[$columnName])) {
138: continue;
139: }
140: $extraColumns[$columnName] = $columnName;
141: }
142:
143: $this->addBindParam('mt_entity_type_id', (int)$entityType->getId());
144: $select
145: ->join(
146: array('additional_table' => $this->getTable($extraTable)),
147: 'additional_table.attribute_id = main_table.attribute_id',
148: $extraColumns)
149: ->where('main_table.entity_type_id = :mt_entity_type_id');
150:
151:
152:
153: $scopeDescribe = $connection->describeTable($this->_getEavWebsiteTable());
154: unset($scopeDescribe['attribute_id']);
155: $scopeColumns = array();
156: foreach (array_keys($scopeDescribe) as $columnName) {
157: if ($columnName == 'website_id') {
158: $scopeColumns['scope_website_id'] = $columnName;
159: } else {
160: if (isset($mainColumns[$columnName])) {
161: $alias = sprintf('scope_%s', $columnName);
162: $expression = $connection->getCheckSql('main_table.%s IS NULL',
163: 'scope_table.%s', 'main_table.%s');
164: $expression = sprintf($expression, $columnName, $columnName, $columnName);
165: $this->addFilterToMap($columnName, $expression);
166: $scopeColumns[$alias] = $columnName;
167: } elseif (isset($extraColumns[$columnName])) {
168: $alias = sprintf('scope_%s', $columnName);
169: $expression = $connection->getCheckSql('additional_table.%s IS NULL',
170: 'scope_table.%s', 'additional_table.%s');
171: $expression = sprintf($expression, $columnName, $columnName, $columnName);
172: $this->addFilterToMap($columnName, $expression);
173: $scopeColumns[$alias] = $columnName;
174: }
175: }
176: }
177:
178: $select->joinLeft(
179: array('scope_table' => $this->_getEavWebsiteTable()),
180: 'scope_table.attribute_id = main_table.attribute_id AND scope_table.website_id = :scope_website_id',
181: $scopeColumns
182: );
183: $websiteId = $this->getWebsite() ? (int)$this->getWebsite()->getId() : 0;
184: $this->addBindParam('scope_website_id', $websiteId);
185:
186: return $this;
187: }
188:
189: 190: 191: 192: 193: 194: 195:
196: public function setEntityTypeFilter($type)
197: {
198: return $this;
199: }
200:
201: 202: 203: 204: 205:
206: public function addVisibleFilter()
207: {
208: return $this->addFieldToFilter('is_visible', 1);
209: }
210:
211: 212: 213: 214: 215:
216: public function addSystemHiddenFilter()
217: {
218: $field = '(CASE WHEN additional_table.is_system = 1 AND additional_table.is_visible = 0 THEN 1 ELSE 0 END)';
219: return $this->addFieldToFilter($field, 0);
220: }
221:
222: 223: 224: 225: 226:
227: public function addSystemHiddenFilterWithPasswordHash()
228: {
229: $field = '(CASE WHEN additional_table.is_system = 1 AND additional_table.is_visible = 0
230: AND main_table.attribute_code != "' . self::EAV_CODE_PASSWORD_HASH . '" THEN 1 ELSE 0 END)';
231: $this->addFieldToFilter($field, 0);
232: return $this;
233: }
234:
235: 236: 237: 238: 239:
240: public function addExcludeHiddenFrontendFilter()
241: {
242: return $this->addFieldToFilter('main_table.frontend_input', array('neq' => 'hidden'));
243: }
244: }
245: