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
00029
00030
00031
00032
00033
00034
00035 class Mage_Dataflow_Model_Convert_Parser_Csv extends Mage_Dataflow_Model_Convert_Parser_Abstract
00036 {
00037 protected $_fields;
00038
00039 protected $_mapfields = array();
00040
00041 public function parse()
00042 {
00043
00044 setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
00045
00046 $fDel = $this->getVar('delimiter', ',');
00047 $fEnc = $this->getVar('enclose', '"');
00048 if ($fDel == '\t') {
00049 $fDel = "\t";
00050 }
00051
00052 $adapterName = $this->getVar('adapter', null);
00053 $adapterMethod = $this->getVar('method', 'saveRow');
00054
00055 if (!$adapterName || !$adapterMethod) {
00056 $message = Mage::helper('dataflow')->__('Please declare "adapter" and "method" node first');
00057 $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
00058 return $this;
00059 }
00060
00061 try {
00062 $adapter = Mage::getModel($adapterName);
00063 }
00064 catch (Exception $e) {
00065 $message = Mage::helper('dataflow')->__('Declared adapter %s not found', $adapterName);
00066 $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
00067 return $this;
00068 }
00069
00070 if (!is_callable(array($adapter, $adapterMethod))) {
00071 $message = Mage::helper('dataflow')->__('Method "%s" not defined in adapter %s', $adapterMethod, $adapterName);
00072 $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
00073 return $this;
00074 }
00075
00076 $batchModel = $this->getBatchModel();
00077 $batchIoAdapter = $this->getBatchModel()->getIoAdapter();
00078
00079 if (Mage::app()->getRequest()->getParam('files')) {
00080 $file = Mage::app()->getConfig()->getTempVarDir().'/import/'
00081 . urldecode(Mage::app()->getRequest()->getParam('files'));
00082 $this->_copy($file);
00083 }
00084
00085 $batchIoAdapter->open(false);
00086
00087 $isFieldNames = $this->getVar('fieldnames', '') == 'true' ? true : false;
00088 if (!$isFieldNames && is_array($this->getVar('map'))) {
00089 $fieldNames = $this->getVar('map');
00090 }
00091 else {
00092 $fieldNames = array();
00093 foreach ($batchIoAdapter->read(true, $fDel, $fEnc) as $v) {
00094 $fieldNames[$v] = $v;
00095 }
00096 }
00097
00098 $countRows = 0;
00099 while (($csvData = $batchIoAdapter->read(true, $fDel, $fEnc)) !== false) {
00100 if (count($csvData) == 1 && $csvData[0] === null) {
00101 continue;
00102 }
00103
00104 $itemData = array();
00105 $countRows ++; $i = 0;
00106 foreach ($fieldNames as $field) {
00107 $itemData[$field] = isset($csvData[$i]) ? $csvData[$i] : null;
00108 $i ++;
00109 }
00110
00111 $batchImportModel = $this->getBatchImportModel()
00112 ->setId(null)
00113 ->setBatchId($this->getBatchModel()->getId())
00114 ->setBatchData($itemData)
00115 ->setStatus(1)
00116 ->save();
00117 }
00118
00119 $this->addException(Mage::helper('dataflow')->__('Found %d rows', $countRows));
00120 $this->addException(Mage::helper('dataflow')->__('Starting %s :: %s', $adapterName, $adapterMethod));
00121
00122 $batchModel->setParams($this->getVars())
00123 ->setAdapter($adapterName)
00124 ->save();
00125
00126
00127
00128 return $this;
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 }
00161
00162 public function parseRow($i, $line)
00163 {
00164 if (sizeof($line) == 1) return false;
00165
00166 if (0==$i) {
00167 if ($this->getVar('fieldnames')) {
00168 $this->_fields = $line;
00169 return;
00170 } else {
00171 foreach ($line as $j=>$f) {
00172
00173 $this->_fields[$j] = $this->_mapfields[$j];
00174 }
00175 }
00176 }
00177
00178 $resultRow = array();
00179
00180 foreach ($this->_fields as $j=>$f) {
00181 $resultRow[$f] = isset($line[$j]) ? $line[$j] : '';
00182 }
00183 return $resultRow;
00184 }
00185
00186
00187
00188
00189
00190
00191 public function unparse()
00192 {
00193 $batchExport = $this->getBatchExportModel()
00194 ->setBatchId($this->getBatchModel()->getId());
00195 $fieldList = $this->getBatchModel()->getFieldList();
00196 $batchExportIds = $batchExport->getIdCollection();
00197
00198 if (!$batchExportIds) {
00199 return $this;
00200 }
00201
00202 $io = $this->getBatchModel()->getIoAdapter();
00203 $io->open();
00204
00205 if ($this->getVar('fieldnames')) {
00206 $csvData = $this->getCsvString($fieldList);
00207 $io->write($csvData);
00208 }
00209
00210 foreach ($batchExportIds as $batchExportId) {
00211 $csvData = array();
00212 $batchExport->load($batchExportId);
00213 $row = $batchExport->getBatchData();
00214
00215 foreach ($fieldList as $field) {
00216 $csvData[] = isset($row[$field]) ? $row[$field] : '';
00217 }
00218 $csvData = $this->getCsvString($csvData);
00219 $io->write($csvData);
00220 }
00221
00222 $io->close();
00223
00224 return $this;
00225 }
00226
00227 public function unparseRow($args)
00228 {
00229 $i = $args['i'];
00230 $row = $args['row'];
00231
00232 $fDel = $this->getVar('delimiter', ',');
00233 $fEnc = $this->getVar('enclose', '"');
00234 $fEsc = $this->getVar('escape', '\\');
00235 $lDel = "\r\n";
00236
00237 if ($fDel == '\t') {
00238 $fDel = "\t";
00239 }
00240
00241 $line = array();
00242 foreach ($this->_fields as $f) {
00243 $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEnc.'"', $fEsc.'\\'), $row[$f]) : '';
00244 $line[] = $fEnc.$v.$fEnc;
00245 }
00246
00247 return join($fDel, $line);
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 public function getCsvString($fields = array()) {
00257 $delimiter = $this->getVar('delimiter', ',');
00258 $enclosure = $this->getVar('enclose', '');
00259 $escapeChar = $this->getVar('escape', '\\');
00260
00261 if ($delimiter == '\t') {
00262 $delimiter = "\t";
00263 }
00264
00265 $str = '';
00266
00267 foreach ($fields as $value) {
00268 if (strpos($value, $delimiter) !== false ||
00269 empty($enclosure) ||
00270 strpos($value, $enclosure) !== false ||
00271 strpos($value, "\n") !== false ||
00272 strpos($value, "\r") !== false ||
00273 strpos($value, "\t") !== false ||
00274 strpos($value, ' ') !== false) {
00275 $str2 = $enclosure;
00276 $escaped = 0;
00277 $len = strlen($value);
00278 for ($i=0;$i<$len;$i++) {
00279 if ($value[$i] == $escapeChar) {
00280 $escaped = 1;
00281 } else if (!$escaped && $value[$i] == $enclosure) {
00282 $str2 .= $enclosure;
00283 } else {
00284 $escaped = 0;
00285 }
00286 $str2 .= $value[$i];
00287 }
00288 $str2 .= $enclosure;
00289 $str .= $str2.$delimiter;
00290 } else {
00291 $str .= $enclosure.$value.$enclosure.$delimiter;
00292 }
00293 }
00294 return substr($str, 0, -1) . "\n";
00295 }
00296 }