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_Session_Parser_Csv extends Mage_Dataflow_Model_Convert_Parser_Abstract
00036 {
00037
00038 public function parse()
00039 {
00040 $fDel = $this->getVar('delimiter', ',');
00041 $fEnc = $this->getVar('enclose', '"');
00042
00043 if ($fDel=='\\t') {
00044 $fDel = "\t";
00045 }
00046
00047
00048 setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
00049
00050 $fp = tmpfile();
00051 fputs($fp, $this->getData());
00052 fseek($fp, 0);
00053
00054 $data = array();
00055 $sessionId = Mage::registry('current_dataflow_session_id');
00056 $import = Mage::getModel('dataflow/import');
00057 $map = new Varien_Convert_Mapper_Column();
00058 for ($i=0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
00059 if (0==$i) {
00060 if ($this->getVar('fieldnames')) {
00061 $fields = $line;
00062 continue;
00063 } else {
00064 foreach ($line as $j=>$f) {
00065 $fields[$j] = 'column'.($j+1);
00066 }
00067 }
00068 }
00069 $row = array();
00070 foreach ($fields as $j=>$f) {
00071 $row[$f] = $line[$j];
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 $map->setData(array($row));
00081 $map->map();
00082 $row = $map->getData();
00083
00084 $import->setImportId(0);
00085 $import->setSessionId($sessionId);
00086 $import->setSerialNumber($i);
00087 $import->setValue(serialize($row[0]));
00088 $import->save();
00089
00090 }
00091 fclose($fp);
00092 unset($sessionId);
00093
00094 return $this;
00095 }
00096
00097 public function unparse()
00098 {
00099 $csv = '';
00100
00101 $fDel = $this->getVar('delimiter', ',');
00102 $fEnc = $this->getVar('enclose', '"');
00103 $fEsc = $this->getVar('escape', '\\');
00104 $lDel = "\r\n";
00105
00106 if ($fDel=='\\t') {
00107 $fDel = "\t";
00108 }
00109
00110 $data = $this->getData();
00111 $fields = $this->getGridFields($data);
00112 $lines = array();
00113
00114 if ($this->getVar('fieldnames')) {
00115 $line = array();
00116 foreach ($fields as $f) {
00117 $line[] = $fEnc.str_replace(array('"', '\\'), array($fEsc.'"', $fEsc.'\\'), $f).$fEnc;
00118 }
00119 $lines[] = join($fDel, $line);
00120 }
00121 foreach ($data as $i=>$row) {
00122 $line = array();
00123 foreach ($fields as $f) {
00124
00125
00126
00127
00128
00129
00130 $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEsc.'"', $fEsc.'\\'), $row[$f]) : '';
00131
00132 $line[] = $fEnc.$v.$fEnc;
00133 }
00134 $lines[] = join($fDel, $line);
00135 }
00136 $result = join($lDel, $lines);
00137 $this->setData($result);
00138
00139 return $this;
00140 }
00141
00142 }