1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Dataflow_Model_Convert_Parser_Csv extends Mage_Dataflow_Model_Convert_Parser_Abstract
36: {
37: protected $_fields;
38:
39: protected $_mapfields = array();
40:
41: public function parse()
42: {
43:
44: setlocale(LC_ALL, Mage::app()->getLocale()->getLocaleCode().'.UTF-8');
45:
46: $fDel = $this->getVar('delimiter', ',');
47: $fEnc = $this->getVar('enclose', '"');
48: if ($fDel == '\t') {
49: $fDel = "\t";
50: }
51:
52: $adapterName = $this->getVar('adapter', null);
53: $adapterMethod = $this->getVar('method', 'saveRow');
54:
55: if (!$adapterName || !$adapterMethod) {
56: $message = Mage::helper('dataflow')->__('Please declare "adapter" and "method" nodes first.');
57: $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
58: return $this;
59: }
60:
61: try {
62: $adapter = Mage::getModel($adapterName);
63: }
64: catch (Exception $e) {
65: $message = Mage::helper('dataflow')->__('Declared adapter %s was not found.', $adapterName);
66: $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
67: return $this;
68: }
69:
70: if (!is_callable(array($adapter, $adapterMethod))) {
71: $message = Mage::helper('dataflow')->__('Method "%s" not defined in adapter %s.', $adapterMethod, $adapterName);
72: $this->addException($message, Mage_Dataflow_Model_Convert_Exception::FATAL);
73: return $this;
74: }
75:
76: $batchModel = $this->getBatchModel();
77: $batchIoAdapter = $this->getBatchModel()->getIoAdapter();
78:
79: if (Mage::app()->getRequest()->getParam('files')) {
80: $file = Mage::app()->getConfig()->getTempVarDir().'/import/'
81: . urldecode(Mage::app()->getRequest()->getParam('files'));
82: $this->_copy($file);
83: }
84:
85: $batchIoAdapter->open(false);
86:
87: $isFieldNames = $this->getVar('fieldnames', '') == 'true' ? true : false;
88: if (!$isFieldNames && is_array($this->getVar('map'))) {
89: $fieldNames = $this->getVar('map');
90: }
91: else {
92: $fieldNames = array();
93: foreach ($batchIoAdapter->read(true, $fDel, $fEnc) as $v) {
94: $fieldNames[$v] = $v;
95: }
96: }
97:
98: $countRows = 0;
99: while (($csvData = $batchIoAdapter->read(true, $fDel, $fEnc)) !== false) {
100: if (count($csvData) == 1 && $csvData[0] === null) {
101: continue;
102: }
103:
104: $itemData = array();
105: $countRows ++; $i = 0;
106: foreach ($fieldNames as $field) {
107: $itemData[$field] = isset($csvData[$i]) ? $csvData[$i] : null;
108: $i ++;
109: }
110:
111: $batchImportModel = $this->getBatchImportModel()
112: ->setId(null)
113: ->setBatchId($this->getBatchModel()->getId())
114: ->setBatchData($itemData)
115: ->setStatus(1)
116: ->save();
117: }
118:
119: $this->addException(Mage::helper('dataflow')->__('Found %d rows.', $countRows));
120: $this->addException(Mage::helper('dataflow')->__('Starting %s :: %s', $adapterName, $adapterMethod));
121:
122: $batchModel->setParams($this->getVars())
123: ->setAdapter($adapterName)
124: ->save();
125:
126:
127:
128: return $this;
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160: }
161:
162: public function parseRow($i, $line)
163: {
164: if (sizeof($line) == 1) return false;
165:
166: if (0==$i) {
167: if ($this->getVar('fieldnames')) {
168: $this->_fields = $line;
169: return;
170: } else {
171: foreach ($line as $j=>$f) {
172: $this->_fields[$j] = $this->_mapfields[$j];
173: }
174: }
175: }
176:
177: $resultRow = array();
178:
179: foreach ($this->_fields as $j=>$f) {
180: $resultRow[$f] = isset($line[$j]) ? $line[$j] : '';
181: }
182: return $resultRow;
183: }
184:
185: 186: 187: 188: 189:
190: public function unparse()
191: {
192: $batchExport = $this->getBatchExportModel()
193: ->setBatchId($this->getBatchModel()->getId());
194: $fieldList = $this->getBatchModel()->getFieldList();
195: $batchExportIds = $batchExport->getIdCollection();
196:
197: $io = $this->getBatchModel()->getIoAdapter();
198: $io->open();
199:
200: if (!$batchExportIds) {
201: $io->write("");
202: $io->close();
203: return $this;
204: }
205:
206: if ($this->getVar('fieldnames')) {
207: $csvData = $this->getCsvString($fieldList);
208: $io->write($csvData);
209: }
210:
211: foreach ($batchExportIds as $batchExportId) {
212: $csvData = array();
213: $batchExport->load($batchExportId);
214: $row = $batchExport->getBatchData();
215:
216: foreach ($fieldList as $field) {
217: $csvData[] = isset($row[$field]) ? $row[$field] : '';
218: }
219: $csvData = $this->getCsvString($csvData);
220: $io->write($csvData);
221: }
222:
223: $io->close();
224:
225: return $this;
226: }
227:
228: public function unparseRow($args)
229: {
230: $i = $args['i'];
231: $row = $args['row'];
232:
233: $fDel = $this->getVar('delimiter', ',');
234: $fEnc = $this->getVar('enclose', '"');
235: $fEsc = $this->getVar('escape', '\\');
236: $lDel = "\r\n";
237:
238: if ($fDel == '\t') {
239: $fDel = "\t";
240: }
241:
242: $line = array();
243: foreach ($this->_fields as $f) {
244: $v = isset($row[$f]) ? str_replace(array('"', '\\'), array($fEnc.'"', $fEsc.'\\'), $row[$f]) : '';
245: $line[] = $fEnc.$v.$fEnc;
246: }
247:
248: return join($fDel, $line);
249: }
250:
251: 252: 253: 254: 255: 256:
257: public function getCsvString($fields = array()) {
258: $delimiter = $this->getVar('delimiter', ',');
259: $enclosure = $this->getVar('enclose', '');
260: $escapeChar = $this->getVar('escape', '\\');
261:
262: if ($delimiter == '\t') {
263: $delimiter = "\t";
264: }
265:
266: $str = '';
267:
268: foreach ($fields as $value) {
269: if (strpos($value, $delimiter) !== false ||
270: empty($enclosure) ||
271: strpos($value, $enclosure) !== false ||
272: strpos($value, "\n") !== false ||
273: strpos($value, "\r") !== false ||
274: strpos($value, "\t") !== false ||
275: strpos($value, ' ') !== false) {
276: $str2 = $enclosure;
277: $escaped = 0;
278: $len = strlen($value);
279: for ($i=0;$i<$len;$i++) {
280: if ($value[$i] == $escapeChar) {
281: $escaped = 1;
282: } else if (!$escaped && $value[$i] == $enclosure) {
283: $str2 .= $enclosure;
284: } else {
285: $escaped = 0;
286: }
287: $str2 .= $value[$i];
288: }
289: $str2 .= $enclosure;
290: $str .= $str2.$delimiter;
291: } else {
292: $str .= $enclosure.$value.$enclosure.$delimiter;
293: }
294: }
295: return substr($str, 0, -1) . "\n";
296: }
297: }
298: