PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Dataflow/Model/Session/Parser/Csv.php

https://bitbucket.org/kdms/sh-magento
PHP | 142 lines | 91 code | 9 blank | 42 comment | 5 complexity | 292546f6db210d02888561db6421675e MD5 | raw file
  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Dataflow
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Convert csv parser
  28. *
  29. * @category Mage
  30. * @package Mage_Dataflow
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Dataflow_Model_Session_Parser_Csv extends Mage_Dataflow_Model_Convert_Parser_Abstract
  34. {
  35. public function parse()
  36. {
  37. $fDel = $this->getVar('delimiter', ',');
  38. $fEnc = $this->getVar('enclose', '"');
  39. if ($fDel=='\\t') {
  40. $fDel = "\t";
  41. }
  42. // fixed for multibyte characters
  43. setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
  44. $fp = tmpfile();
  45. fputs($fp, $this->getData());
  46. fseek($fp, 0);
  47. $data = array();
  48. $sessionId = Mage::registry('current_dataflow_session_id');
  49. $import = Mage::getModel('dataflow/import');
  50. $map = new Varien_Convert_Mapper_Column();
  51. for ($i=0; $line = fgetcsv($fp, 4096, $fDel, $fEnc); $i++) {
  52. if (0==$i) {
  53. if ($this->getVar('fieldnames')) {
  54. $fields = $line;
  55. continue;
  56. } else {
  57. foreach ($line as $j=>$f) {
  58. $fields[$j] = 'column'.($j+1);
  59. }
  60. }
  61. }
  62. $row = array();
  63. foreach ($fields as $j=>$f) {
  64. $row[$f] = $line[$j];
  65. }
  66. /*
  67. if ($i <= 100)
  68. {
  69. $data[] = $row;
  70. }
  71. */
  72. //$map = new Varien_Convert_Mapper_Column();
  73. $map->setData(array($row));
  74. $map->map();
  75. $row = $map->getData();
  76. //$import = Mage::getModel('dataflow/import');
  77. $import->setImportId(0);
  78. $import->setSessionId($sessionId);
  79. $import->setSerialNumber($i);
  80. $import->setValue(serialize($row[0]));
  81. $import->save();
  82. //unset($import);
  83. }
  84. fclose($fp);
  85. unset($sessionId);
  86. //$this->setData($data);
  87. return $this;
  88. } // end
  89. public function unparse()
  90. {
  91. $csv = '';
  92. $fDel = $this->getVar('delimiter', ',');
  93. $fEnc = $this->getVar('enclose', '"');
  94. $fEsc = $this->getVar('escape', '\\');
  95. $lDel = "\r\n";
  96. if ($fDel=='\\t') {
  97. $fDel = "\t";
  98. }
  99. $data = $this->getData();
  100. $fields = $this->getGridFields($data);
  101. $lines = array();
  102. if ($this->getVar('fieldnames')) {
  103. $line = array();
  104. foreach ($fields as $f) {
  105. $line[] = $fEnc.str_replace(array('"', '\\'), array($fEsc.'"', $fEsc.'\\'), $f).$fEnc;
  106. }
  107. $lines[] = join($fDel, $line);
  108. }
  109. foreach ($data as $i=>$row) {
  110. $line = array();
  111. foreach ($fields as $f) {
  112. /*
  113. if (isset($row[$f]) && (preg_match('\"', $row[$f]) || preg_match('\\', $row[$f]))) {
  114. $tmp = str_replace('\\', '\\\\',$row[$f]);
  115. echo str_replace('"', '\"',$tmp).'<br>';
  116. }
  117. */
  118. $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEsc.'"', $fEsc.'\\'), $row[$f]) : '';
  119. $line[] = $fEnc.$v.$fEnc;
  120. }
  121. $lines[] = join($fDel, $line);
  122. }
  123. $result = join($lDel, $lines);
  124. $this->setData($result);
  125. return $this;
  126. }
  127. }