PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/rgranadino/magento-mirror
PHP | 297 lines | 191 code | 38 blank | 68 comment | 33 complexity | f1497710454547217c37cd0a6ab13d90 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  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_Convert_Parser_Csv extends Mage_Dataflow_Model_Convert_Parser_Abstract
  34. {
  35. protected $_fields;
  36. protected $_mapfields = array();
  37. public function parse()
  38. {
  39. // fixed for multibyte characters
  40. setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
  41. $fDel = $this->getVar('delimiter', ',');
  42. $fEnc = $this->getVar('enclose', '"');
  43. if ($fDel == '\t') {
  44. $fDel = "\t";
  45. }
  46. $adapterName = $this->getVar('adapter', null);
  47. $adapterMethod = $this->getVar('method', 'saveRow');
  48. if (!$adapterName || !$adapterMethod) {
  49. $message = Mage::helper('dataflow')->__('Please declare "adapter" and "method" nodes first.');
  50. $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
  51. return $this;
  52. }
  53. try {
  54. $adapter = Mage::getModel($adapterName);
  55. }
  56. catch (Exception $e) {
  57. $message = Mage::helper('dataflow')->__('Declared adapter %s was not found.', $adapterName);
  58. $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
  59. return $this;
  60. }
  61. if (!is_callable(array($adapter, $adapterMethod))) {
  62. $message = Mage::helper('dataflow')->__('Method "%s" not defined in adapter %s.', $adapterMethod, $adapterName);
  63. $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
  64. return $this;
  65. }
  66. $batchModel = $this->getBatchModel();
  67. $batchIoAdapter = $this->getBatchModel()->getIoAdapter();
  68. if (Mage::app()->getRequest()->getParam('files')) {
  69. $file = Mage::app()->getConfig()->getTempVarDir().'/import/'
  70. . urldecode(Mage::app()->getRequest()->getParam('files'));
  71. $this->_copy($file);
  72. }
  73. $batchIoAdapter->open(false);
  74. $isFieldNames = $this->getVar('fieldnames', '') == 'true' ? true : false;
  75. if (!$isFieldNames && is_array($this->getVar('map'))) {
  76. $fieldNames = $this->getVar('map');
  77. }
  78. else {
  79. $fieldNames = array();
  80. foreach ($batchIoAdapter->read(true, $fDel, $fEnc) as $v) {
  81. $fieldNames[$v] = $v;
  82. }
  83. }
  84. $countRows = 0;
  85. while (($csvData = $batchIoAdapter->read(true, $fDel, $fEnc)) !== false) {
  86. if (count($csvData) == 1 && $csvData[0] === null) {
  87. continue;
  88. }
  89. $itemData = array();
  90. $countRows ++; $i = 0;
  91. foreach ($fieldNames as $field) {
  92. $itemData[$field] = isset($csvData[$i]) ? $csvData[$i] : null;
  93. $i ++;
  94. }
  95. $batchImportModel = $this->getBatchImportModel()
  96. ->setId(null)
  97. ->setBatchId($this->getBatchModel()->getId())
  98. ->setBatchData($itemData)
  99. ->setStatus(1)
  100. ->save();
  101. }
  102. $this->addException(Mage::helper('dataflow')->__('Found %d rows.', $countRows));
  103. $this->addException(Mage::helper('dataflow')->__('Starting %s :: %s', $adapterName, $adapterMethod));
  104. $batchModel->setParams($this->getVars())
  105. ->setAdapter($adapterName)
  106. ->save();
  107. //$adapter->$adapterMethod();
  108. return $this;
  109. // // fix for field mapping
  110. // if ($mapfields = $this->getProfile()->getDataflowProfile()) {
  111. // $this->_mapfields = array_values($mapfields['gui_data']['map'][$mapfields['entity_type']]['db']);
  112. // } // end
  113. //
  114. // if (!$this->getVar('fieldnames') && !$this->_mapfields) {
  115. // $this->addException('Please define field mapping', Mage_Dataflow_Model_Convert_Exception::FATAL);
  116. // return;
  117. // }
  118. //
  119. // if ($this->getVar('adapter') && $this->getVar('method')) {
  120. // $adapter = Mage::getModel($this->getVar('adapter'));
  121. // }
  122. //
  123. // $i = 0;
  124. // while (($line = fgetcsv($fh, null, $fDel, $fEnc)) !== FALSE) {
  125. // $row = $this->parseRow($i, $line);
  126. //
  127. // if (!$this->getVar('fieldnames') && $i == 0 && $row) {
  128. // $i = 1;
  129. // }
  130. //
  131. // if ($row) {
  132. // $loadMethod = $this->getVar('method');
  133. // $adapter->$loadMethod(compact('i', 'row'));
  134. // }
  135. // $i++;
  136. // }
  137. //
  138. // return $this;
  139. }
  140. public function parseRow($i, $line)
  141. {
  142. if (sizeof($line) == 1) return false;
  143. if (0==$i) {
  144. if ($this->getVar('fieldnames')) {
  145. $this->_fields = $line;
  146. return;
  147. } else {
  148. foreach ($line as $j=>$f) {
  149. $this->_fields[$j] = $this->_mapfields[$j];
  150. }
  151. }
  152. }
  153. $resultRow = array();
  154. foreach ($this->_fields as $j=>$f) {
  155. $resultRow[$f] = isset($line[$j]) ? $line[$j] : '';
  156. }
  157. return $resultRow;
  158. }
  159. /**
  160. * Read data collection and write to temporary file
  161. *
  162. * @return Mage_Dataflow_Model_Convert_Parser_Csv
  163. */
  164. public function unparse()
  165. {
  166. $batchExport = $this->getBatchExportModel()
  167. ->setBatchId($this->getBatchModel()->getId());
  168. $fieldList = $this->getBatchModel()->getFieldList();
  169. $batchExportIds = $batchExport->getIdCollection();
  170. $io = $this->getBatchModel()->getIoAdapter();
  171. $io->open();
  172. if (!$batchExportIds) {
  173. $io->write("");
  174. $io->close();
  175. return $this;
  176. }
  177. if ($this->getVar('fieldnames')) {
  178. $csvData = $this->getCsvString($fieldList);
  179. $io->write($csvData);
  180. }
  181. foreach ($batchExportIds as $batchExportId) {
  182. $csvData = array();
  183. $batchExport->load($batchExportId);
  184. $row = $batchExport->getBatchData();
  185. foreach ($fieldList as $field) {
  186. $csvData[] = isset($row[$field]) ? $row[$field] : '';
  187. }
  188. $csvData = $this->getCsvString($csvData);
  189. $io->write($csvData);
  190. }
  191. $io->close();
  192. return $this;
  193. }
  194. public function unparseRow($args)
  195. {
  196. $i = $args['i'];
  197. $row = $args['row'];
  198. $fDel = $this->getVar('delimiter', ',');
  199. $fEnc = $this->getVar('enclose', '"');
  200. $fEsc = $this->getVar('escape', '\\');
  201. $lDel = "\r\n";
  202. if ($fDel == '\t') {
  203. $fDel = "\t";
  204. }
  205. $line = array();
  206. foreach ($this->_fields as $f) {
  207. $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEnc.'"', $fEsc.'\\'), $row[$f]) : '';
  208. $line[] = $fEnc.$v.$fEnc;
  209. }
  210. return join($fDel, $line);
  211. }
  212. /**
  213. * Retrieve csv string from array
  214. *
  215. * @param array $fields
  216. * @return sting
  217. */
  218. public function getCsvString($fields = array()) {
  219. $delimiter = $this->getVar('delimiter', ',');
  220. $enclosure = $this->getVar('enclose', '');
  221. $escapeChar = $this->getVar('escape', '\\');
  222. if ($delimiter == '\t') {
  223. $delimiter = "\t";
  224. }
  225. $str = '';
  226. foreach ($fields as $value) {
  227. if (strpos($value, $delimiter) !== false ||
  228. empty($enclosure) ||
  229. strpos($value, $enclosure) !== false ||
  230. strpos($value, "\n") !== false ||
  231. strpos($value, "\r") !== false ||
  232. strpos($value, "\t") !== false ||
  233. strpos($value, ' ') !== false) {
  234. $str2 = $enclosure;
  235. $escaped = 0;
  236. $len = strlen($value);
  237. for ($i=0;$i<$len;$i++) {
  238. if ($value[$i] == $escapeChar) {
  239. $escaped = 1;
  240. } else if (!$escaped && $value[$i] == $enclosure) {
  241. $str2 .= $enclosure;
  242. } else {
  243. $escaped = 0;
  244. }
  245. $str2 .= $value[$i];
  246. }
  247. $str2 .= $enclosure;
  248. $str .= $str2.$delimiter;
  249. } else {
  250. $str .= $enclosure.$value.$enclosure.$delimiter;
  251. }
  252. }
  253. return substr($str, 0, -1) . "\n";
  254. }
  255. }