/** * Parses csv file * * @param string $file * @return Mage_Core_Model_Resource_Abstract */ public function parseCsv($file) { $info = pathinfo($file); $io = new Varien_Io_File(); $io->open(array('path' => $info['dirname'])); $io->streamOpen($info['basename'], 'r'); $headers = $io->streamReadCsv(); if ($headers === false) { $io->streamClose(); Mage::throwException(Mage::helper('pyro_licenses')->__('You must specify valid headers in the first row')); } if (false === ($columns = $this->_prepareColumns($headers))) { $io->streamClose(); Mage::throwException(Mage::helper('pyro_licenses')->__("Invalid header: 'License' is missed")); } $this->_write->beginTransaction(); try { $rowNumber = 1; $importData = array(); while (false !== ($csvLine = $io->streamReadCsv())) { $rowNumber++; // check for empty lines $emptyLine = array_unique($csvLine); if (count($emptyLine) == 1 && $emptyLine[0] == "") { continue; } $row = $this->_getImportRow($csvLine, $columns, $rowNumber); if ($row !== false) { $importData[] = $row; } if (count($importData) == 5000) { $this->_saveImportData($importData); $importData = array(); } } $this->_saveImportData($importData); $io->streamClose(); } catch (Mage_Core_Exception $e) { $this->_write->rollback(); $io->streamClose(); Mage::throwException($e->getMessage()); } catch (Exception $e) { $this->_write->rollback(); $io->streamClose(); Mage::logException($e); Mage::throwException(Mage::helper('adminhtml')->__($e->getMessage())); } $this->_write->commit(); if ($this->_importErrors) { $error = sprintf('ImportSubscribers: "%1$s"', implode('", "', $this->_importErrors)); Mage::log($error, 3, '', true); } return $this; }
public function rollback() { $this->_connection->rollback(); }