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: class Mage_Eav_Model_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
29: {
30: 31: 32: 33: 34:
35: protected $_optionsDefault = array();
36:
37: 38: 39: 40: 41: 42: 43:
44: public function getAllOptions($withEmpty = true, $defaultValues = false)
45: {
46: $storeId = $this->getAttribute()->getStoreId();
47: if (!is_array($this->_options)) {
48: $this->_options = array();
49: }
50: if (!is_array($this->_optionsDefault)) {
51: $this->_optionsDefault = array();
52: }
53: if (!isset($this->_options[$storeId])) {
54: $collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
55: ->setPositionOrder('asc')
56: ->setAttributeFilter($this->getAttribute()->getId())
57: ->setStoreFilter($this->getAttribute()->getStoreId())
58: ->load();
59: $this->_options[$storeId] = $collection->toOptionArray();
60: $this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
61: }
62: $options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
63: if ($withEmpty) {
64: array_unshift($options, array('label' => '', 'value' => ''));
65: }
66:
67: return $options;
68: }
69:
70: 71: 72: 73: 74: 75:
76: public function getOptionText($value)
77: {
78: $isMultiple = false;
79: if (strpos($value, ',')) {
80: $isMultiple = true;
81: $value = explode(',', $value);
82: }
83:
84: $options = $this->getAllOptions(false);
85:
86: if ($isMultiple) {
87: $values = array();
88: foreach ($options as $item) {
89: if (in_array($item['value'], $value)) {
90: $values[] = $item['label'];
91: }
92: }
93: return $values;
94: }
95:
96: foreach ($options as $item) {
97: if ($item['value'] == $value) {
98: return $item['label'];
99: }
100: }
101: return false;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: public function addValueSortToCollection($collection, $dir = Varien_Db_Select::SQL_ASC)
113: {
114: $valueTable1 = $this->getAttribute()->getAttributeCode() . '_t1';
115: $valueTable2 = $this->getAttribute()->getAttributeCode() . '_t2';
116: $collection->getSelect()
117: ->joinLeft(
118: array($valueTable1 => $this->getAttribute()->getBackend()->getTable()),
119: "e.entity_id={$valueTable1}.entity_id"
120: . " AND {$valueTable1}.attribute_id='{$this->getAttribute()->getId()}'"
121: . " AND {$valueTable1}.store_id=0",
122: array())
123: ->joinLeft(
124: array($valueTable2 => $this->getAttribute()->getBackend()->getTable()),
125: "e.entity_id={$valueTable2}.entity_id"
126: . " AND {$valueTable2}.attribute_id='{$this->getAttribute()->getId()}'"
127: . " AND {$valueTable2}.store_id='{$collection->getStoreId()}'",
128: array()
129: );
130: $valueExpr = $collection->getSelect()->getAdapter()
131: ->getCheckSql("{$valueTable2}.value_id > 0", "{$valueTable2}.value", "{$valueTable1}.value");
132:
133: Mage::getResourceModel('eav/entity_attribute_option')
134: ->addOptionValueToCollection($collection, $this->getAttribute(), $valueExpr);
135:
136: $collection->getSelect()
137: ->order("{$this->getAttribute()->getAttributeCode()} {$dir}");
138:
139: return $this;
140: }
141:
142: 143: 144: 145: 146:
147: public function getFlatColums()
148: {
149: $columns = array();
150: $attributeCode = $this->getAttribute()->getAttributeCode();
151: $isMulti = $this->getAttribute()->getFrontend()->getInputType() == 'multiselect';
152:
153: if (Mage::helper('core')->useDbCompatibleMode()) {
154: $columns[$attributeCode] = array(
155: 'type' => $isMulti ? 'varchar(255)' : 'int',
156: 'unsigned' => false,
157: 'is_null' => true,
158: 'default' => null,
159: 'extra' => null
160: );
161: if (!$isMulti) {
162: $columns[$attributeCode . '_value'] = array(
163: 'type' => 'varchar(255)',
164: 'unsigned' => false,
165: 'is_null' => true,
166: 'default' => null,
167: 'extra' => null
168: );
169: }
170: } else {
171: $type = ($isMulti) ? Varien_Db_Ddl_Table::TYPE_TEXT : Varien_Db_Ddl_Table::TYPE_INTEGER;
172: $columns[$attributeCode] = array(
173: 'type' => $type,
174: 'length' => $isMulti ? '255' : null,
175: 'unsigned' => false,
176: 'nullable' => true,
177: 'default' => null,
178: 'extra' => null,
179: 'comment' => $attributeCode . ' column'
180: );
181: if (!$isMulti) {
182: $columns[$attributeCode . '_value'] = array(
183: 'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
184: 'length' => 255,
185: 'unsigned' => false,
186: 'nullable' => true,
187: 'default' => null,
188: 'extra' => null,
189: 'comment' => $attributeCode . ' column'
190: );
191: }
192: }
193:
194: return $columns;
195: }
196:
197: 198: 199: 200: 201:
202: public function getFlatIndexes()
203: {
204: $indexes = array();
205:
206: $index = sprintf('IDX_%s', strtoupper($this->getAttribute()->getAttributeCode()));
207: $indexes[$index] = array(
208: 'type' => 'index',
209: 'fields' => array($this->getAttribute()->getAttributeCode())
210: );
211:
212: $sortable = $this->getAttribute()->getUsedForSortBy();
213: if ($sortable && $this->getAttribute()->getFrontend()->getInputType() != 'multiselect') {
214: $index = sprintf('IDX_%s_VALUE', strtoupper($this->getAttribute()->getAttributeCode()));
215:
216: $indexes[$index] = array(
217: 'type' => 'index',
218: 'fields' => array($this->getAttribute()->getAttributeCode() . '_value')
219: );
220: }
221:
222: return $indexes;
223: }
224:
225: 226: 227: 228: 229: 230:
231: public function getFlatUpdateSelect($store)
232: {
233: return Mage::getResourceModel('eav/entity_attribute_option')
234: ->getFlatUpdateSelect($this->getAttribute(), $store);
235: }
236: }
237: