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 extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected static $_entityAttributes = array();
43:
44: 45: 46: 47:
48: protected function _construct()
49: {
50: $this->_init('eav/attribute', 'attribute_id');
51: }
52:
53: 54: 55: 56: 57:
58: protected function _initUniqueFields()
59: {
60: $this->_uniqueFields = array(array(
61: 'field' => array('attribute_code', 'entity_type_id'),
62: 'title' => Mage::helper('eav')->__('Attribute with the same code')
63: ));
64: return $this;
65: }
66:
67: 68: 69: 70: 71: 72:
73: protected function _loadTypeAttributes($entityTypeId)
74: {
75: if (!isset(self::$_entityAttributes[$entityTypeId])) {
76: $adapter = $this->_getReadAdapter();
77: $bind = array(':entity_type_id' => $entityTypeId);
78: $select = $adapter->select()
79: ->from($this->getMainTable())
80: ->where('entity_type_id = :entity_type_id');
81:
82: $data = $adapter->fetchAll($select, $bind);
83: foreach ($data as $row) {
84: self::$_entityAttributes[$entityTypeId][$row['attribute_code']] = $row;
85: }
86: }
87:
88: return $this;
89: }
90:
91: 92: 93: 94: 95: 96: 97: 98:
99: public function loadByCode(Mage_Core_Model_Abstract $object, $entityTypeId, $code)
100: {
101: $bind = array(':entity_type_id' => $entityTypeId);
102: $select = $this->_getLoadSelect('attribute_code', $code, $object)
103: ->where('entity_type_id = :entity_type_id');
104: $data = $this->_getReadAdapter()->fetchRow($select, $bind);
105:
106: if ($data) {
107: $object->setData($data);
108: $this->_afterLoad($object);
109: return true;
110: }
111: return false;
112: }
113:
114: 115: 116: 117: 118: 119:
120: private function _getMaxSortOrder(Mage_Core_Model_Abstract $object)
121: {
122: if (intval($object->getAttributeGroupId()) > 0) {
123: $adapter = $this->_getReadAdapter();
124: $bind = array(
125: ':attribute_set_id' => $object->getAttributeSetId(),
126: ':attribute_group_id' => $object->getAttributeGroupId()
127: );
128: $select = $adapter->select()
129: ->from($this->getTable('eav/entity_attribute'), new Zend_Db_Expr("MAX(sort_order)"))
130: ->where('attribute_set_id = :attribute_set_id')
131: ->where('attribute_group_id = :attribute_group_id');
132:
133: return $adapter->fetchOne($select, $bind);
134: }
135:
136: return 0;
137: }
138:
139: 140: 141: 142: 143: 144:
145: public function deleteEntity(Mage_Core_Model_Abstract $object)
146: {
147: if (!$object->getEntityAttributeId()) {
148: return $this;
149: }
150:
151: $this->_getWriteAdapter()->delete($this->getTable('eav/entity_attribute'), array(
152: 'entity_attribute_id = ?' => $object->getEntityAttributeId()
153: ));
154:
155: return $this;
156: }
157:
158: 159: 160: 161: 162: 163:
164: protected function _beforeSave(Mage_Core_Model_Abstract $object)
165: {
166: $frontendLabel = $object->getFrontendLabel();
167: if (is_array($frontendLabel)) {
168: if (!isset($frontendLabel[0]) || is_null($frontendLabel[0]) || $frontendLabel[0] == '') {
169: Mage::throwException(Mage::helper('eav')->__('Frontend label is not defined'));
170: }
171: $object->setFrontendLabel($frontendLabel[0])
172: ->setStoreLabels($frontendLabel);
173: }
174:
175: 176: 177:
178: if (!$object->getId()) {
179: if ($object->getFrontendInput() == 'select') {
180: $object->setSourceModel('eav/entity_attribute_source_table');
181: }
182: }
183:
184: return parent::_beforeSave($object);
185: }
186:
187: 188: 189: 190: 191: 192:
193: protected function _afterSave(Mage_Core_Model_Abstract $object)
194: {
195: $this->_saveStoreLabels($object)
196: ->_saveAdditionalAttributeData($object)
197: ->saveInSetIncluding($object)
198: ->_saveOption($object);
199:
200: return parent::_afterSave($object);
201: }
202:
203: 204: 205: 206: 207: 208:
209: protected function _saveStoreLabels(Mage_Core_Model_Abstract $object)
210: {
211: $storeLabels = $object->getStoreLabels();
212: if (is_array($storeLabels)) {
213: $adapter = $this->_getWriteAdapter();
214: if ($object->getId()) {
215: $condition = array('attribute_id =?' => $object->getId());
216: $adapter->delete($this->getTable('eav/attribute_label'), $condition);
217: }
218: foreach ($storeLabels as $storeId => $label) {
219: if ($storeId == 0 || !strlen($label)) {
220: continue;
221: }
222: $bind = array (
223: 'attribute_id' => $object->getId(),
224: 'store_id' => $storeId,
225: 'value' => $label
226: );
227: $adapter->insert($this->getTable('eav/attribute_label'), $bind);
228: }
229: }
230:
231: return $this;
232: }
233:
234: 235: 236: 237: 238: 239:
240: protected function _saveAdditionalAttributeData(Mage_Core_Model_Abstract $object)
241: {
242: $additionalTable = $this->getAdditionalAttributeTable($object->getEntityTypeId());
243: if ($additionalTable) {
244: $adapter = $this->_getWriteAdapter();
245: $data = $this->_prepareDataForTable($object, $this->getTable($additionalTable));
246: $bind = array(':attribute_id' => $object->getId());
247: $select = $adapter->select()
248: ->from($this->getTable($additionalTable), array('attribute_id'))
249: ->where('attribute_id = :attribute_id');
250: $result = $adapter->fetchOne($select, $bind);
251: if ($result) {
252: $where = array('attribute_id = ?' => $object->getId());
253: $adapter->update($this->getTable($additionalTable), $data, $where);
254: } else {
255: $adapter->insert($this->getTable($additionalTable), $data);
256: }
257: }
258:
259: return $this;
260: }
261:
262: 263: 264: 265: 266: 267:
268: public function saveInSetIncluding(Mage_Core_Model_Abstract $object)
269: {
270: $attributeId = (int) $object->getId();
271: $setId = (int) $object->getAttributeSetId();
272: $groupId = (int) $object->getAttributeGroupId();
273:
274: if ($setId && $groupId && $object->getEntityTypeId()) {
275: $adapter = $this->_getWriteAdapter();
276: $table = $this->getTable('eav/entity_attribute');
277:
278: $sortOrder = (($object->getSortOrder()) ? $object->getSortOrder() : $this->_getMaxSortOrder($object) + 1);
279: $data = array(
280: 'entity_type_id' => $object->getEntityTypeId(),
281: 'attribute_set_id' => $setId,
282: 'attribute_group_id' => $groupId,
283: 'attribute_id' => $attributeId,
284: 'sort_order' => $sortOrder
285: );
286:
287: $where = array(
288: 'attribute_id =?' => $attributeId,
289: 'attribute_set_id =?' => $setId
290: );
291:
292: $adapter->delete($table, $where);
293: $adapter->insert($table, $data);
294: }
295:
296: return $this;
297: }
298:
299: 300: 301: 302: 303: 304:
305: protected function _saveOption(Mage_Core_Model_Abstract $object)
306: {
307: $option = $object->getOption();
308: if (is_array($option)) {
309: $adapter = $this->_getWriteAdapter();
310: $optionTable = $this->getTable('eav/attribute_option');
311: $optionValueTable = $this->getTable('eav/attribute_option_value');
312:
313: $stores = Mage::app()->getStores(true);
314: if (isset($option['value'])) {
315: $attributeDefaultValue = array();
316: if (!is_array($object->getDefault())) {
317: $object->setDefault(array());
318: }
319:
320: foreach ($option['value'] as $optionId => $values) {
321: $intOptionId = (int) $optionId;
322: if (!empty($option['delete'][$optionId])) {
323: if ($intOptionId) {
324: $adapter->delete($optionTable, array('option_id = ?' => $intOptionId));
325: }
326: continue;
327: }
328:
329: $sortOrder = !empty($option['order'][$optionId]) ? $option['order'][$optionId] : 0;
330: if (!$intOptionId) {
331: $data = array(
332: 'attribute_id' => $object->getId(),
333: 'sort_order' => $sortOrder
334: );
335: $adapter->insert($optionTable, $data);
336: $intOptionId = $adapter->lastInsertId($optionTable);
337: } else {
338: $data = array('sort_order' => $sortOrder);
339: $where = array('option_id =?' => $intOptionId);
340: $adapter->update($optionTable, $data, $where);
341: }
342:
343: if (in_array($optionId, $object->getDefault())) {
344: if ($object->getFrontendInput() == 'multiselect') {
345: $attributeDefaultValue[] = $intOptionId;
346: } elseif ($object->getFrontendInput() == 'select') {
347: $attributeDefaultValue = array($intOptionId);
348: }
349: }
350:
351:
352: if (!isset($values[0])) {
353: Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
354: }
355:
356: $adapter->delete($optionValueTable, array('option_id =?' => $intOptionId));
357: foreach ($stores as $store) {
358: if (isset($values[$store->getId()])
359: && (!empty($values[$store->getId()])
360: || $values[$store->getId()] == "0")
361: ) {
362: $data = array(
363: 'option_id' => $intOptionId,
364: 'store_id' => $store->getId(),
365: 'value' => $values[$store->getId()],
366: );
367: $adapter->insert($optionValueTable, $data);
368: }
369: }
370: }
371: $bind = array('default_value' => implode(',', $attributeDefaultValue));
372: $where = array('attribute_id =?' => $object->getId());
373: $adapter->update($this->getMainTable(), $bind, $where);
374: }
375: }
376:
377: return $this;
378: }
379:
380:
381: 382: 383: 384: 385: 386: 387:
388: public function getIdByCode($entityType, $code)
389: {
390: $adapter = $this->_getReadAdapter();
391: $bind = array(
392: ':entity_type_code' => $entityType,
393: ':attribute_code' => $code
394: );
395: $select = $adapter->select()
396: ->from(array('a' => $this->getTable('eav/attribute')), array('a.attribute_id'))
397: ->join(
398: array('t' => $this->getTable('eav/entity_type')),
399: 'a.entity_type_id = t.entity_type_id',
400: array())
401: ->where('t.entity_type_code = :entity_type_code')
402: ->where('a.attribute_code = :attribute_code');
403:
404: return $adapter->fetchOne($select, $bind);
405: }
406:
407: 408: 409: 410: 411: 412:
413: public function getAttributeCodesByFrontendType($frontendType)
414: {
415: $adapter = $this->_getReadAdapter();
416: $bind = array(':frontend_input' => $frontendType);
417: $select = $adapter->select()
418: ->from($this->getTable('eav/attribute'), 'attribute_code')
419: ->where('frontend_input = :frontend_input');
420:
421: return $adapter->fetchCol($select, $bind);
422: }
423:
424: 425: 426: 427: 428: 429: 430:
431: public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $storeId)
432: {
433: $adapter = $this->_getReadAdapter();
434: $joinConditionTemplate = "%s.entity_id=%s.entity_id"
435: ." AND %s.entity_type_id = ".$attribute->getEntityTypeId()
436: ." AND %s.attribute_id = ".$attribute->getId()
437: ." AND %s.store_id = %d";
438: $joinCondition = sprintf($joinConditionTemplate,
439: 'e', 't1', 't1', 't1', 't1',
440: Mage_Core_Model_App::ADMIN_STORE_ID);
441: if ($attribute->getFlatAddChildData()) {
442: $joinCondition .= ' AND e.child_id = t1.entity_id';
443: }
444:
445: $valueExpr = $adapter->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
446:
447:
448: $select = $adapter->select()
449: ->joinLeft(
450: array('t1' => $attribute->getBackend()->getTable()),
451: $joinCondition,
452: array())
453: ->joinLeft(
454: array('t2' => $attribute->getBackend()->getTable()),
455: sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $storeId),
456: array($attribute->getAttributeCode() => $valueExpr));
457: if ($attribute->getFlatAddChildData()) {
458: $select->where("e.is_child = ?", 0);
459: }
460:
461: return $select;
462: }
463:
464: 465: 466: 467: 468: 469:
470: public function describeTable($table)
471: {
472: return $this->_getReadAdapter()->describeTable($table);
473: }
474:
475: 476: 477: 478: 479: 480:
481: public function getAdditionalAttributeTable($entityTypeId)
482: {
483: return Mage::getResourceSingleton('eav/entity_type')->getAdditionalAttributeTable($entityTypeId);
484: }
485:
486: 487: 488: 489: 490: 491: 492:
493: protected function _afterLoad(Mage_Core_Model_Abstract $object)
494: {
495:
496: $entityType = $object->getData('entity_type');
497: if ($entityType) {
498: $additionalTable = $entityType->getAdditionalAttributeTable();
499: } else {
500: $additionalTable = $this->getAdditionalAttributeTable($object->getEntityTypeId());
501: }
502:
503: if ($additionalTable) {
504: $adapter = $this->_getReadAdapter();
505: $bind = array(':attribute_id' => $object->getId());
506: $select = $adapter->select()
507: ->from($this->getTable($additionalTable))
508: ->where('attribute_id = :attribute_id');
509:
510: $result = $adapter->fetchRow($select, $bind);
511: if ($result) {
512: $object->addData($result);
513: }
514: }
515:
516: return $this;
517: }
518:
519: 520: 521: 522: 523: 524:
525: public function getStoreLabelsByAttributeId($attributeId)
526: {
527: $adapter = $this->_getReadAdapter();
528: $bind = array(':attribute_id' => $attributeId);
529: $select = $adapter->select()
530: ->from($this->getTable('eav/attribute_label'), array('store_id', 'value'))
531: ->where('attribute_id = :attribute_id');
532:
533: return $adapter->fetchPairs($select, $bind);
534: }
535:
536: 537: 538: 539: 540: 541:
542: public function getValidAttributeIds($attributeIds)
543: {
544: $adapter = $this->_getReadAdapter();
545: $select = $adapter->select()
546: ->from($this->getMainTable(), array('attribute_id'))
547: ->where('attribute_id IN (?)', $attributeIds);
548:
549: return $adapter->fetchCol($select);
550: }
551: }
552: