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_Form_Attribute_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_moduleName = '';
43:
44: 45: 46: 47: 48:
49: protected $_entityTypeCode = '';
50:
51: 52: 53: 54: 55:
56: protected $_store;
57:
58: 59: 60: 61: 62:
63: protected $_entityType;
64:
65: 66: 67: 68: 69:
70: protected function _construct()
71: {
72: if (empty($this->_moduleName)) {
73: Mage::throwException(Mage::helper('eav')->__('Current module pathname is undefined'));
74: }
75: if (empty($this->_entityTypeCode)) {
76: Mage::throwException(Mage::helper('eav')->__('Current module EAV entity is undefined'));
77: }
78: }
79:
80: 81: 82: 83: 84: 85: 86: 87:
88: protected function _getEavWebsiteTable()
89: {
90: return null;
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function setStore($store)
100: {
101: $this->_store = Mage::app()->getStore($store);
102: return $this;
103: }
104:
105: 106: 107: 108: 109:
110: public function getStore()
111: {
112: if ($this->_store === null) {
113: $this->_store = Mage::app()->getStore();
114: }
115: return $this->_store;
116: }
117:
118: 119: 120: 121: 122: 123:
124: public function setEntityType($entityType)
125: {
126: $this->_entityType = Mage::getSingleton('eav/config')->getEntityType($entityType);
127: return $this;
128: }
129:
130: 131: 132: 133: 134:
135: public function getEntityType()
136: {
137: if ($this->_entityType === null) {
138: $this->setEntityType($this->_entityTypeCode);
139: }
140: return $this->_entityType;
141: }
142:
143: 144: 145: 146: 147: 148:
149: public function addFormCodeFilter($code)
150: {
151: return $this->addFieldToFilter('main_table.form_code', $code);
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function setSortOrder($direction = self::SORT_ORDER_ASC)
161: {
162: $this->setOrder('ea.is_user_defined', self::SORT_ORDER_ASC);
163: return $this->setOrder('ca.sort_order', $direction);
164: }
165:
166: 167: 168: 169: 170:
171: protected function _beforeLoad()
172: {
173: $select = $this->getSelect();
174: $connection = $this->getConnection();
175: $entityType = $this->getEntityType();
176: $this->setItemObjectClass($entityType->getAttributeModel());
177:
178: $eaColumns = array();
179: $caColumns = array();
180: $saColumns = array();
181:
182: $eaDescribe = $connection->describeTable($this->getTable('eav/attribute'));
183: unset($eaDescribe['attribute_id']);
184: foreach (array_keys($eaDescribe) as $columnName) {
185: $eaColumns[$columnName] = $columnName;
186: }
187:
188: $select->join(
189: array('ea' => $this->getTable('eav/attribute')),
190: 'main_table.attribute_id = ea.attribute_id',
191: $eaColumns
192: );
193:
194:
195: $additionalTable = $entityType->getAdditionalAttributeTable();
196: if ($additionalTable) {
197: $caDescribe = $connection->describeTable($this->getTable($additionalTable));
198: unset($caDescribe['attribute_id']);
199: foreach (array_keys($caDescribe) as $columnName) {
200: $caColumns[$columnName] = $columnName;
201: }
202:
203: $select->join(
204: array('ca' => $this->getTable($additionalTable)),
205: 'main_table.attribute_id = ca.attribute_id',
206: $caColumns
207: );
208: }
209:
210:
211: if ($this->_getEavWebsiteTable()) {
212: $saDescribe = $connection->describeTable($this->_getEavWebsiteTable());
213: unset($saDescribe['attribute_id']);
214: foreach (array_keys($saDescribe) as $columnName) {
215: if ($columnName == 'website_id') {
216: $saColumns['scope_website_id'] = $columnName;
217: } else {
218: if (isset($eaColumns[$columnName])) {
219: $code = sprintf('scope_%s', $columnName);
220: $expression = $connection->getCheckSql('sa.%s IS NULL', 'ea.%s', 'sa.%s');
221: $saColumns[$code] = new Zend_Db_Expr(sprintf($expression,
222: $columnName, $columnName, $columnName));
223: } elseif (isset($caColumns[$columnName])) {
224: $code = sprintf('scope_%s', $columnName);
225: $expression = $connection->getCheckSql('sa.%s IS NULL', 'ca.%s', 'sa.%s');
226: $saColumns[$code] = new Zend_Db_Expr(sprintf($expression,
227: $columnName, $columnName, $columnName));
228: }
229: }
230: }
231:
232: $store = $this->getStore();
233: $joinWebsiteExpression = $connection
234: ->quoteInto(
235: 'sa.attribute_id = main_table.attribute_id AND sa.website_id = ?', (int)$store->getWebsiteId()
236: );
237: $select->joinLeft(
238: array('sa' => $this->_getEavWebsiteTable()),
239: $joinWebsiteExpression,
240: $saColumns
241: );
242: }
243:
244:
245:
246: if ($store->isAdmin()) {
247: $select->columns(array('store_label' => 'ea.frontend_label'));
248: } else {
249: $storeLabelExpr = $connection->getCheckSql('al.value IS NULL', 'ea.frontend_label', 'al.value');
250: $joinExpression = $connection
251: ->quoteInto('al.attribute_id = main_table.attribute_id AND al.store_id = ?', (int)$store->getId());
252: $select->joinLeft(
253: array('al' => $this->getTable('eav/attribute_label')),
254: $joinExpression,
255: array('store_label' => $storeLabelExpr)
256: );
257: }
258:
259:
260: $select->where('ea.entity_type_id = ?', (int)$entityType->getId());
261:
262: return parent::_beforeLoad();
263: }
264: }
265: