Varien_Convert_Parser_Csv Class Reference

Inheritance diagram for Varien_Convert_Parser_Csv:

Varien_Convert_Parser_Abstract Varien_Convert_Container_Abstract Varien_Convert_Parser_Interface Varien_Convert_Container_Interface

List of all members.

Public Member Functions

 parse ()
 parseTest ()
 unparse ()


Detailed Description

Definition at line 35 of file Csv.php.


Member Function Documentation

parse (  ) 

Implements Varien_Convert_Parser_Interface.

Definition at line 37 of file Csv.php.

00038     {
00039         $fDel = $this->getVar('delimiter', ',');
00040         $fEnc = $this->getVar('enclose', '"');
00041 
00042         if ($fDel=='\\t') {
00043             $fDel = "\t";
00044         }
00045 
00046         // fixed for multibyte characters
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     }

parseTest (  ) 

Definition at line 77 of file Csv.php.

00078     {
00079         $fDel = $this->getVar('delimiter', ',');
00080         $fEnc = $this->getVar('enclose', '"');
00081 
00082         if ($fDel=='\\t') {
00083             $fDel = "\t";
00084         }
00085 
00086         // fixed for multibyte characters
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     } // end

unparse (  ) 

Implements Varien_Convert_Parser_Interface.

Definition at line 126 of file Csv.php.

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                 if (isset($row[$f]) && (preg_match('\"', $row[$f]) || preg_match('\\', $row[$f]))) {
00155                     $tmp = str_replace('\\', '\\\\',$row[$f]);
00156                     echo str_replace('"', '\"',$tmp).'<br>';
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     }


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:58 2009 for Magento by  doxygen 1.5.8