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_Eav_Model_Resource_Entity_Attribute_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_addSetInfoFlag = false;
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: $this->_init('eav/entity_attribute');
51: }
52:
53: 54: 55: 56: 57:
58: protected function _getLoadDataFields()
59: {
60: return array(
61: 'attribute_id',
62: 'entity_type_id',
63: 'attribute_code',
64: 'attribute_model',
65: 'backend_model',
66: 'backend_type',
67: 'backend_table',
68: 'frontend_input',
69: 'source_model',
70: );
71: }
72:
73: 74: 75: 76: 77:
78: public function useLoadDataFields()
79: {
80: $this->getSelect()->reset(Zend_Db_Select::COLUMNS);
81: $this->getSelect()->columns($this->_getLoadDataFields());
82:
83: return $this;
84: }
85:
86: 87: 88: 89: 90: 91:
92: public function setEntityTypeFilter($type)
93: {
94: if ($type instanceof Mage_Eav_Model_Entity_Type) {
95: $additionalTable = $type->getAdditionalAttributeTable();
96: $id = $type->getId();
97: } else {
98: $additionalTable = $this->getResource()->getAdditionalAttributeTable($type);
99: $id = $type;
100: }
101: $this->addFieldToFilter('main_table.entity_type_id', $id);
102: if ($additionalTable) {
103: $this->join(
104: array('additional_table' => $additionalTable),
105: 'additional_table.attribute_id = main_table.attribute_id'
106: );
107: }
108:
109: return $this;
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function setAttributeSetFilter($setId)
119: {
120: if (is_array($setId)) {
121: if (!empty($setId)) {
122: $this->join(
123: 'entity_attribute',
124: 'entity_attribute.attribute_id = main_table.attribute_id',
125: 'attribute_id'
126: );
127: $this->addFieldToFilter('entity_attribute.attribute_set_id', array('in' => $setId));
128: $this->addAttributeGrouping();
129: $this->_useAnalyticFunction = true;
130: }
131: } elseif ($setId) {
132: $this->join(
133: 'entity_attribute',
134: 'entity_attribute.attribute_id = main_table.attribute_id'
135: );
136: $this->addFieldToFilter('entity_attribute.attribute_set_id', $setId);
137: $this->setOrder('sort_order', self::SORT_ORDER_ASC);
138: }
139:
140: return $this;
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function setAttributeSetsFilter(array $setIds)
151: {
152: $this->getSelect()->distinct(true);
153: $this->join(
154: array('entity_attribute' => $this->getTable('eav/entity_attribute')),
155: 'entity_attribute.attribute_id = main_table.attribute_id',
156: 'attribute_id'
157: );
158: $this->addFieldToFilter('entity_attribute.attribute_set_id', array('in' => $setIds));
159: $this->setOrder('sort_order', self::SORT_ORDER_ASC);
160:
161: return $this;
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function setInAllAttributeSetsFilter(array $setIds)
171: {
172: foreach ($setIds as $setId) {
173: $setId = (int) $setId;
174: if (!$setId) {
175: continue;
176: }
177: $alias = sprintf('entity_attribute_%d', $setId);
178: $joinCondition = $this->getConnection()
179: ->quoteInto("{$alias}.attribute_id = main_table.attribute_id AND {$alias}.attribute_set_id =?", $setId);
180: $this->join(
181: array($alias => 'eav/entity_attribute'),
182: $joinCondition,
183: 'attribute_id'
184: );
185: }
186:
187:
188: $this->setOrder('is_user_defined', self::SORT_ORDER_ASC);
189:
190: return $this;
191: }
192:
193: 194: 195: 196: 197: 198:
199: public function setAttributeSetExcludeFilter($setId)
200: {
201: $this->join(
202: 'entity_attribute',
203: 'entity_attribute.attribute_id = main_table.attribute_id'
204: );
205: $this->addFieldToFilter('entity_attribute.attribute_set_id', array('neq' => $setId));
206: $this->setOrder('sort_order', self::SORT_ORDER_ASC);
207:
208: return $this;
209: }
210:
211: 212: 213: 214: 215: 216:
217: public function setAttributesExcludeFilter($attributes)
218: {
219: return $this->addFieldToFilter('main_table.attribute_id', array('nin' => $attributes));
220: }
221:
222: 223: 224: 225: 226: 227:
228: public function setAttributeGroupFilter($groupId)
229: {
230: $this->join(
231: 'entity_attribute',
232: 'entity_attribute.attribute_id = main_table.attribute_id'
233: );
234: $this->addFieldToFilter('entity_attribute.attribute_group_id', $groupId);
235: $this->setOrder('sort_order', self::SORT_ORDER_ASC);
236:
237: return $this;
238: }
239:
240: 241: 242: 243: 244:
245: public function addAttributeGrouping()
246: {
247: $this->getSelect()->group('entity_attribute.attribute_id');
248: return $this;
249: }
250:
251: 252: 253: 254: 255:
256: public function addIsUniqueFilter()
257: {
258: return $this->addFieldToFilter('is_unique', array('gt' => 0));
259: }
260:
261: 262: 263: 264: 265:
266: public function addIsNotUniqueFilter()
267: {
268: return $this->addFieldToFilter('is_unique', 0);
269: }
270:
271: 272: 273: 274: 275:
276: public function addHasOptionsFilter()
277: {
278: $adapter = $this->getConnection();
279: $orWhere = implode(' OR ', array(
280: $adapter->quoteInto('(main_table.frontend_input = ? AND ao.option_id > 0)', 'select'),
281: $adapter->quoteInto('(main_table.frontend_input <> ?)', 'select'),
282: '(main_table.is_user_defined = 0)'
283: ));
284:
285: $this->getSelect()
286: ->joinLeft(
287: array('ao' => $this->getTable('eav/attribute_option')),
288: 'ao.attribute_id = main_table.attribute_id',
289: 'option_id')
290: ->group('main_table.attribute_id')
291: ->where($orWhere);
292:
293: $this->_useAnalyticFunction = true;
294:
295: return $this;
296: }
297:
298: 299: 300: 301: 302: 303:
304: public function setFrontendInputTypeFilter($frontendInputType)
305: {
306: return $this->addFieldToFilter('frontend_input', $frontendInputType);
307: }
308:
309: 310: 311: 312: 313: 314:
315: public function addSetInfo($flag = true)
316: {
317: $this->_addSetInfoFlag = (bool)$flag;
318: return $this;
319: }
320:
321: 322: 323: 324: 325:
326: protected function _addSetInfo()
327: {
328: if ($this->_addSetInfoFlag) {
329: $attributeIds = array();
330: foreach ($this->_data as &$dataItem) {
331: $attributeIds[] = $dataItem['attribute_id'];
332: }
333: $attributeToSetInfo = array();
334:
335: $adapter = $this->getConnection();
336: if (count($attributeIds) > 0) {
337: $select = $adapter->select()
338: ->from(
339: array('entity' => $this->getTable('eav/entity_attribute')),
340: array('attribute_id', 'attribute_set_id', 'attribute_group_id', 'sort_order')
341: )
342: ->joinLeft(
343: array('group' => $this->getTable('eav/attribute_group')),
344: 'entity.attribute_group_id = group.attribute_group_id',
345: array('group_sort_order' => 'sort_order')
346: )
347: ->where('attribute_id IN (?)', $attributeIds);
348: $result = $adapter->fetchAll($select);
349:
350: foreach ($result as $row) {
351: $data = array(
352: 'group_id' => $row['attribute_group_id'],
353: 'group_sort' => $row['group_sort_order'],
354: 'sort' => $row['sort_order']
355: );
356: $attributeToSetInfo[$row['attribute_id']][$row['attribute_set_id']] = $data;
357: }
358: }
359:
360: foreach ($this->_data as &$attributeData) {
361: $setInfo = array();
362: if (isset($attributeToSetInfo[$attributeData['attribute_id']])) {
363: $setInfo = $attributeToSetInfo[$attributeData['attribute_id']];
364: }
365:
366: $attributeData['attribute_set_info'] = $setInfo;
367: }
368:
369: unset($attributeToSetInfo);
370: unset($attributeIds);
371: }
372: return $this;
373: }
374:
375: 376: 377: 378: 379:
380: protected function _afterLoadData()
381: {
382: $this->_addSetInfo();
383:
384: return parent::_afterLoadData();
385: }
386:
387: 388: 389: 390: 391: 392:
393: public function checkConfigurableProducts()
394: {
395: return $this;
396: }
397:
398: 399: 400: 401: 402: 403:
404: public function setCodeFilter($code)
405: {
406: if (empty($code)) {
407: return $this;
408: }
409: if (!is_array($code)) {
410: $code = array($code);
411: }
412:
413: return $this->addFieldToFilter('attribute_code', array('in' => $code));
414: }
415:
416: 417: 418: 419: 420: 421:
422: public function addStoreLabel($storeId)
423: {
424: $adapter = $this->getConnection();
425: $joinExpression = $adapter
426: ->quoteInto('al.attribute_id = main_table.attribute_id AND al.store_id = ?', (int) $storeId);
427: $this->getSelect()->joinLeft(
428: array('al' => $this->getTable('eav/attribute_label')),
429: $joinExpression,
430: array('store_label' => $adapter->getIfNullSql('al.value', 'main_table.frontend_label'))
431: );
432:
433: return $this;
434: }
435: }
436: