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_Convert_Adapter_Grid
29: extends Mage_Dataflow_Model_Convert_Adapter_Abstract
30: {
31: protected $_entity;
32:
33: public function getEntity()
34: {
35: if (!$this->_entityType) {
36: if (!($entityType = $this->getVar('entity_type'))
37: || !(($entity = Mage::getResourceSingleton($entityType)) instanceof Mage_Eav_Model_Entity_Interface)) {
38: $this->addException(Mage::helper('eav')->__('Invalid entity specified'), Varien_Convert_Exception::FATAL);
39: }
40: $this->_entity = $entity;
41: }
42: return $this->_entity;
43: }
44:
45: public function load()
46: {
47: try {
48: $collection = Mage::getResourceModel($this->getEntity().'_collection');
49: $collection->load();
50: } catch (Exception $e) {
51: $this->addException(Mage::helper('eav')->__('An error occurred while loading the collection, aborting. Error: %s', $e->getMessage()), Varien_Convert_Exception::FATAL);
52: }
53:
54: $data = array();
55: foreach ($collection->getIterator() as $entity) {
56: $data[] = $entity->getData();
57: }
58: $this->setData($data);
59: return $this;
60: }
61:
62: public function save()
63: {
64: foreach ($this->getData() as $i=>$row) {
65: $this->setExceptionLocation('Line: '.$i);
66: $entity = Mage::getResourceModel($this->getEntity());
67: if (!empty($row['entity_id'])) {
68: try {
69: $entity->load($row['entity_id']);
70: $this->setPosition('Line: '.$i.(isset($row['entity_id']) ? ', entity_id: '.$row['entity_id'] : ''));
71: } catch (Exception $e) {
72: $this->addException(Mage::helper('eav')->__('An error occurred while loading a record, aborting. Error: %s', $e->getMessage()), Varien_Convert_Exception::FATAL);
73: }
74: if (!$entity->getId()) {
75: $this->addException(Mage::helper('eav')->__('Invalid entity_id, skipping the record.'), Varien_Convert_Exception::ERROR);
76: continue;
77: }
78: }
79: try {
80: $entity->addData($row)->save();
81: } catch (Exception $e) {
82: $this->addException(Mage::helper('eav')->__('An error occurred while saving a record, aborting. Error: ', $e->getMessage()), Varien_Convert_Exception::FATAL);
83: }
84: }
85: return $this;
86: }
87: }
88: