Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Core_Model_Convert
  • Mage_Dataflow_Helper_Data
  • Mage_Dataflow_Model_Batch
  • Mage_Dataflow_Model_Batch_Abstract
  • Mage_Dataflow_Model_Batch_Export
  • Mage_Dataflow_Model_Batch_Import
  • Mage_Dataflow_Model_Batch_Io
  • Mage_Dataflow_Model_Convert
  • Mage_Dataflow_Model_Convert_Action
  • Mage_Dataflow_Model_Convert_Action_Abstract
  • Mage_Dataflow_Model_Convert_Adapter_Abstract
  • Mage_Dataflow_Model_Convert_Adapter_Db_Table
  • Mage_Dataflow_Model_Convert_Adapter_Http
  • Mage_Dataflow_Model_Convert_Adapter_Http_Curl
  • Mage_Dataflow_Model_Convert_Adapter_Io
  • Mage_Dataflow_Model_Convert_Adapter_Soap
  • Mage_Dataflow_Model_Convert_Adapter_Std
  • Mage_Dataflow_Model_Convert_Adapter_Zend_Cache
  • Mage_Dataflow_Model_Convert_Adapter_Zend_Db
  • Mage_Dataflow_Model_Convert_Container_Abstract
  • Mage_Dataflow_Model_Convert_Container_Collection
  • Mage_Dataflow_Model_Convert_Container_Generic
  • Mage_Dataflow_Model_Convert_Exception
  • Mage_Dataflow_Model_Convert_Iterator_File_Csv
  • Mage_Dataflow_Model_Convert_Iterator_Http
  • Mage_Dataflow_Model_Convert_Mapper_Abstract
  • Mage_Dataflow_Model_Convert_Mapper_Column
  • Mage_Dataflow_Model_Convert_Parser_Abstract
  • Mage_Dataflow_Model_Convert_Parser_Csv
  • Mage_Dataflow_Model_Convert_Parser_Serialize
  • Mage_Dataflow_Model_Convert_Parser_Xml_Excel
  • Mage_Dataflow_Model_Convert_Profile
  • Mage_Dataflow_Model_Convert_Profile_Abstract
  • Mage_Dataflow_Model_Convert_Profile_Collection
  • Mage_Dataflow_Model_Convert_Validator_Abstract
  • Mage_Dataflow_Model_Convert_Validator_Column
  • Mage_Dataflow_Model_Convert_Validator_Dryrun
  • Mage_Dataflow_Model_Import
  • Mage_Dataflow_Model_Mysql4_Batch
  • Mage_Dataflow_Model_Mysql4_Batch_Abstract
  • Mage_Dataflow_Model_Mysql4_Batch_Collection
  • Mage_Dataflow_Model_Mysql4_Batch_Export
  • Mage_Dataflow_Model_Mysql4_Batch_Import
  • Mage_Dataflow_Model_Mysql4_Import
  • Mage_Dataflow_Model_Mysql4_Import_Collection
  • Mage_Dataflow_Model_Mysql4_Profile
  • Mage_Dataflow_Model_Mysql4_Profile_Collection
  • Mage_Dataflow_Model_Mysql4_Profile_History
  • Mage_Dataflow_Model_Mysql4_Profile_History_Collection
  • Mage_Dataflow_Model_Mysql4_Session
  • Mage_Dataflow_Model_Profile
  • Mage_Dataflow_Model_Profile_History
  • Mage_Dataflow_Model_Resource_Batch
  • Mage_Dataflow_Model_Resource_Batch_Abstract
  • Mage_Dataflow_Model_Resource_Batch_Collection
  • Mage_Dataflow_Model_Resource_Batch_Export
  • Mage_Dataflow_Model_Resource_Batch_Import
  • Mage_Dataflow_Model_Resource_Import
  • Mage_Dataflow_Model_Resource_Import_Collection
  • Mage_Dataflow_Model_Resource_Profile
  • Mage_Dataflow_Model_Resource_Profile_Collection
  • Mage_Dataflow_Model_Resource_Profile_History
  • Mage_Dataflow_Model_Resource_Profile_History_Collection
  • Mage_Dataflow_Model_Resource_Session
  • Mage_Dataflow_Model_Session
  • Mage_Dataflow_Model_Session_Adapter_Http
  • Mage_Dataflow_Model_Session_Adapter_Iterator
  • Mage_Dataflow_Model_Session_Parser_Csv

Interfaces

  • Mage_Dataflow_Model_Convert_Action_Interface
  • Mage_Dataflow_Model_Convert_Adapter_Interface
  • Mage_Dataflow_Model_Convert_Container_Interface
  • Mage_Dataflow_Model_Convert_Iterator_Interface
  • Mage_Dataflow_Model_Convert_Mapper_Interface
  • Mage_Dataflow_Model_Convert_Parser_Interface
  • Mage_Dataflow_Model_Convert_Profile_Interface
  • Mage_Dataflow_Model_Convert_Validator_Interface
  • Overview
  • Package
  • Class
  • Tree
  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) 2012 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: 
 28: /**
 29:  * Convert csv parser
 30:  *
 31:  * @category   Mage
 32:  * @package    Mage_Dataflow
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 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:         // fixed for multibyte characters
 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:         //$adapter->$adapterMethod();
127: 
128:         return $this;
129: 
130: //        // fix for field mapping
131: //        if ($mapfields = $this->getProfile()->getDataflowProfile()) {
132: //            $this->_mapfields = array_values($mapfields['gui_data']['map'][$mapfields['entity_type']]['db']);
133: //        } // end
134: //
135: //        if (!$this->getVar('fieldnames') && !$this->_mapfields) {
136: //            $this->addException('Please define field mapping', Mage_Dataflow_Model_Convert_Exception::FATAL);
137: //            return;
138: //        }
139: //
140: //        if ($this->getVar('adapter') && $this->getVar('method')) {
141: //            $adapter = Mage::getModel($this->getVar('adapter'));
142: //        }
143: //
144: //        $i = 0;
145: //        while (($line = fgetcsv($fh, null, $fDel, $fEnc)) !== FALSE) {
146: //            $row = $this->parseRow($i, $line);
147: //
148: //            if (!$this->getVar('fieldnames') && $i == 0 && $row) {
149: //                $i = 1;
150: //            }
151: //
152: //            if ($row) {
153: //                $loadMethod = $this->getVar('method');
154: //                $adapter->$loadMethod(compact('i', 'row'));
155: //            }
156: //            $i++;
157: //        }
158: //
159: //        return $this;
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:      * Read data collection and write to temporary file
187:      *
188:      * @return Mage_Dataflow_Model_Convert_Parser_Csv
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:      * Retrieve csv string from array
253:      *
254:      * @param array $fields
255:      * @return sting
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0