Public Member Functions | |
parse () | |
parseRow ($i, $line) | |
unparse () | |
unparseRow ($args) | |
getCsvString ($fields=array()) | |
Protected Attributes | |
$_fields | |
$_mapfields = array() |
Definition at line 35 of file Csv.php.
getCsvString | ( | $ | fields = array() |
) |
Retrieve csv string from array
array | $fields |
Definition at line 256 of file Csv.php.
00256 { 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 }
parse | ( | ) |
Implements Mage_Dataflow_Model_Convert_Parser_Interface.
Definition at line 41 of file Csv.php.
00042 { 00043 // fixed for multibyte characters 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 //$adapter->$adapterMethod(); 00127 00128 return $this; 00129 00130 // // fix for field mapping 00131 // if ($mapfields = $this->getProfile()->getDataflowProfile()) { 00132 // $this->_mapfields = array_values($mapfields['gui_data']['map'][$mapfields['entity_type']]['db']); 00133 // } // end 00134 // 00135 // if (!$this->getVar('fieldnames') && !$this->_mapfields) { 00136 // $this->addException('Please define field mapping', Mage_Dataflow_Model_Convert_Exception::FATAL); 00137 // return; 00138 // } 00139 // 00140 // if ($this->getVar('adapter') && $this->getVar('method')) { 00141 // $adapter = Mage::getModel($this->getVar('adapter')); 00142 // } 00143 // 00144 // $i = 0; 00145 // while (($line = fgetcsv($fh, null, $fDel, $fEnc)) !== FALSE) { 00146 // $row = $this->parseRow($i, $line); 00147 // 00148 // if (!$this->getVar('fieldnames') && $i == 0 && $row) { 00149 // $i = 1; 00150 // } 00151 // 00152 // if ($row) { 00153 // $loadMethod = $this->getVar('method'); 00154 // $adapter->$loadMethod(compact('i', 'row')); 00155 // } 00156 // $i++; 00157 // } 00158 // 00159 // return $this; 00160 }
parseRow | ( | $ | i, | |
$ | line | |||
) |
Definition at line 162 of file Csv.php.
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 // $this->_fields[$j] = 'column'.($j+1); 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 }
unparse | ( | ) |
Read data collection and write to temporary file
Implements Mage_Dataflow_Model_Convert_Parser_Interface.
Definition at line 191 of file Csv.php.
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 }
unparseRow | ( | $ | args | ) |
Definition at line 227 of file Csv.php.
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 }