Mage_Dataflow_Model_Session_Parser_Csv Class Reference

Inheritance diagram for Mage_Dataflow_Model_Session_Parser_Csv:

Mage_Dataflow_Model_Convert_Parser_Abstract Mage_Dataflow_Model_Convert_Container_Abstract Mage_Dataflow_Model_Convert_Parser_Interface Mage_Dataflow_Model_Convert_Container_Interface

List of all members.

Public Member Functions

 parse ()
 unparse ()


Detailed Description

Definition at line 35 of file Csv.php.


Member Function Documentation

parse (  ) 

Implements Mage_Dataflow_Model_Convert_Parser_Interface.

Definition at line 38 of file Csv.php.

00039     {
00040         $fDel = $this->getVar('delimiter', ',');
00041         $fEnc = $this->getVar('enclose', '"');
00042 
00043         if ($fDel=='\\t') {
00044             $fDel = "\t";
00045         }
00046 
00047         // fixed for multibyte characters
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             if ($i <= 100)
00075             {
00076                 $data[] = $row;
00077             }
00078             */
00079             //$map = new Varien_Convert_Mapper_Column();
00080             $map->setData(array($row));
00081             $map->map();
00082             $row = $map->getData();
00083             //$import = Mage::getModel('dataflow/import');
00084             $import->setImportId(0);
00085             $import->setSessionId($sessionId);
00086             $import->setSerialNumber($i);
00087             $import->setValue(serialize($row[0]));
00088             $import->save();
00089             //unset($import);
00090         }
00091         fclose($fp);
00092         unset($sessionId);
00093         //$this->setData($data);
00094         return $this;
00095     } // end

unparse (  ) 

Implements Mage_Dataflow_Model_Convert_Parser_Interface.

Definition at line 97 of file Csv.php.

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


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

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