00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Eav_Model_Convert_Adapter_Grid
00029 extends Mage_Dataflow_Model_Convert_Adapter_Abstract
00030 {
00031 protected $_entity;
00032
00033 public function getEntity()
00034 {
00035 if (!$this->_entityType) {
00036 if (!($entityType = $this->getVar('entity_type'))
00037 || !(($entity = Mage::getResourceSingleton($entityType)) instanceof Mage_Eav_Model_Entity_Interface)) {
00038 $this->addException(Mage::helper('eav')->__('Invalid entity specified'), Varien_Convert_Exception::FATAL);
00039 }
00040 $this->_entity = $entity;
00041 }
00042 return $this->_entity;
00043 }
00044
00045 public function load()
00046 {
00047 try {
00048 $collection = Mage::getResourceModel($this->getEntity().'_collection');
00049 $collection->load();
00050 } catch (Exception $e) {
00051 $this->addException(Mage::helper('eav')->__('Problem loading the collection, aborting. Error: %s', $e->getMessage()), Varien_Convert_Exception::FATAL);
00052 }
00053
00054 $data = array();
00055 foreach ($collection->getIterator() as $entity) {
00056 $data[] = $entity->getData();
00057 }
00058 $this->setData($data);
00059 return $this;
00060 }
00061
00062 public function save()
00063 {
00064 foreach ($this->getData() as $i=>$row) {
00065 $this->setExceptionLocation('Line: '.$i);
00066 $entity = Mage::getResourceModel($this->getEntity());
00067 if (!empty($row['entity_id'])) {
00068 try {
00069 $entity->load($row['entity_id']);
00070 $this->setPosition('Line: '.$i.(isset($row['entity_id']) ? ', entity_id: '.$row['entity_id'] : ''));
00071 } catch (Exception $e) {
00072 $this->addException(Mage::helper('eav')->__('Problem loading a record, aborting. Error: %s', $e->getMessage()), Varien_Convert_Exception::FATAL);
00073 }
00074 if (!$entity->getId()) {
00075 $this->addException(Mage::helper('eav')->__('Invalid entity_id, skipping the record'), Varien_Convert_Exception::ERROR);
00076 continue;
00077 }
00078 }
00079 try {
00080 $entity->addData($row)->save();
00081 } catch (Exception $e) {
00082 $this->addException(Mage::helper('eav')->__('Problem saving a record, aborting. Error: ', $e->getMessage()), Varien_Convert_Exception::FATAL);
00083 }
00084 }
00085 return $this;
00086 }
00087 }