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