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_Catalog_Model_Resource_Product_Attribute_Collection
36: extends Mage_Eav_Model_Resource_Entity_Attribute_Collection
37: {
38: 39: 40: 41:
42: protected function _construct()
43: {
44: $this->_init('catalog/resource_eav_attribute', 'eav/entity_attribute');
45: }
46:
47: 48: 49: 50: 51:
52: protected function _initSelect()
53: {
54: $entityTypeId = (int)Mage::getModel('eav/entity')->setType(Mage_Catalog_Model_Product::ENTITY)->getTypeId();
55: $columns = $this->getConnection()->describeTable($this->getResource()->getMainTable());
56: unset($columns['attribute_id']);
57: $retColumns = array();
58: foreach ($columns as $labelColumn => $columnData) {
59: $retColumns[$labelColumn] = $labelColumn;
60: if ($columnData['DATA_TYPE'] == Varien_Db_Ddl_Table::TYPE_TEXT) {
61: $retColumns[$labelColumn] = Mage::getResourceHelper('core')->castField('main_table.'.$labelColumn);
62: }
63: }
64: $this->getSelect()
65: ->from(array('main_table' => $this->getResource()->getMainTable()), $retColumns)
66: ->join(
67: array('additional_table' => $this->getTable('catalog/eav_attribute')),
68: 'additional_table.attribute_id = main_table.attribute_id'
69: )
70: ->where('main_table.entity_type_id = ?', $entityTypeId);
71: return $this;
72: }
73:
74: 75: 76: 77: 78: 79: 80:
81: public function setEntityTypeFilter($typeId)
82: {
83: return $this;
84: }
85:
86: 87: 88: 89: 90:
91: protected function _getLoadDataFields()
92: {
93: $fields = array_merge(
94: parent::_getLoadDataFields(),
95: array(
96: 'additional_table.is_global',
97: 'additional_table.is_html_allowed_on_front',
98: 'additional_table.is_wysiwyg_enabled'
99: )
100: );
101:
102: return $fields;
103: }
104:
105: 106: 107: 108: 109:
110: public function removePriceFilter()
111: {
112: return $this->addFieldToFilter('main_table.attribute_code', array('neq' => 'price'));
113: }
114:
115: 116: 117: 118: 119:
120: public function addDisplayInAdvancedSearchFilter()
121: {
122: return $this->addFieldToFilter('additional_table.is_visible_in_advanced_search', 1);
123: }
124:
125: 126: 127: 128: 129:
130: public function addIsFilterableFilter()
131: {
132: return $this->addFieldToFilter('additional_table.is_filterable', array('gt' => 0));
133: }
134:
135: 136: 137: 138: 139:
140: public function addIsFilterableInSearchFilter()
141: {
142: return $this->addFieldToFilter('additional_table.is_filterable_in_search', array('gt' => 0));
143: }
144:
145: 146: 147: 148: 149:
150: public function addVisibleFilter()
151: {
152: return $this->addFieldToFilter('additional_table.is_visible', 1);
153: }
154:
155: 156: 157: 158: 159:
160: public function addIsSearchableFilter()
161: {
162: return $this->addFieldToFilter('additional_table.is_searchable', 1);
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function addToIndexFilter($addRequiredCodes = false)
172: {
173: $conditions = array(
174: 'additional_table.is_searchable = 1',
175: 'additional_table.is_visible_in_advanced_search = 1',
176: 'additional_table.is_filterable > 0',
177: 'additional_table.is_filterable_in_search = 1',
178: 'additional_table.used_for_sort_by = 1'
179: );
180:
181: if ($addRequiredCodes) {
182: $conditions[] = $this->getConnection()->quoteInto('main_table.attribute_code IN (?)',
183: array('status', 'visibility'));
184: }
185:
186: $this->getSelect()->where(sprintf('(%s)', implode(' OR ', $conditions)));
187:
188: return $this;
189: }
190:
191: 192: 193: 194: 195:
196: public function addSearchableAttributeFilter()
197: {
198: $this->getSelect()->where(
199: 'additional_table.is_searchable = 1 OR '.
200: $this->getConnection()->quoteInto('main_table.attribute_code IN (?)', array('status', 'visibility'))
201: );
202:
203: return $this;
204: }
205: }
206: