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_ImportExport
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: * ImportExport import data resource model
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_ImportExport_Model_Resource_Import_Data
35: extends Mage_Core_Model_Resource_Db_Abstract
36: implements IteratorAggregate
37: {
38: /**
39: * @var IteratorIterator
40: */
41: protected $_iterator = null;
42:
43: /**
44: * Resource initialization
45: */
46: protected function _construct()
47: {
48: $this->_init('importexport/importdata', 'id');
49: }
50:
51: /**
52: * Retrieve an external iterator
53: *
54: * @return IteratorIterator
55: */
56: public function getIterator()
57: {
58: $adapter = $this->_getWriteAdapter();
59: $select = $adapter->select()
60: ->from($this->getMainTable(), array('data'))
61: ->order('id ASC');
62: $stmt = $adapter->query($select);
63:
64: $stmt->setFetchMode(Zend_Db::FETCH_NUM);
65: if ($stmt instanceof IteratorAggregate) {
66: $iterator = $stmt->getIterator();
67: } else {
68: // Statement doesn't support iterating, so fetch all records and create iterator ourself
69: $rows = $stmt->fetchAll();
70: $iterator = new ArrayIterator($rows);
71: }
72:
73: return $iterator;
74: }
75:
76: /**
77: * Clean all bunches from table.
78: *
79: * @return Varien_Db_Adapter_Interface
80: */
81: public function cleanBunches()
82: {
83: return $this->_getWriteAdapter()->delete($this->getMainTable());
84: }
85:
86: /**
87: * Return behavior from import data table.
88: *
89: * @throws Exception
90: * @return string
91: */
92: public function getBehavior()
93: {
94: $adapter = $this->_getReadAdapter();
95: $behaviors = array_unique($adapter->fetchCol(
96: $adapter->select()
97: ->from($this->getMainTable(), array('behavior'))
98: ));
99: if (count($behaviors) != 1) {
100: Mage::throwException(Mage::helper('importexport')->__('Error in data structure: behaviors are mixed'));
101: }
102: return $behaviors[0];
103: }
104:
105: /**
106: * Return entity type code from import data table.
107: *
108: * @throws Exception
109: * @return string
110: */
111: public function getEntityTypeCode()
112: {
113: $adapter = $this->_getReadAdapter();
114: $entityCodes = array_unique($adapter->fetchCol(
115: $adapter->select()
116: ->from($this->getMainTable(), array('entity'))
117: ));
118: if (count($entityCodes) != 1) {
119: Mage::throwException(Mage::helper('importexport')->__('Error in data structure: entity codes are mixed'));
120: }
121: return $entityCodes[0];
122: }
123:
124: /**
125: * Get next bunch of validated rows.
126: *
127: * @return array|null
128: */
129: public function getNextBunch()
130: {
131: if (null === $this->_iterator) {
132: $this->_iterator = $this->getIterator();
133: $this->_iterator->rewind();
134: }
135: if ($this->_iterator->valid()) {
136: $dataRow = $this->_iterator->current();
137: $dataRow = Mage::helper('core')->jsonDecode($dataRow[0]);
138: $this->_iterator->next();
139: } else {
140: $this->_iterator = null;
141: $dataRow = null;
142: }
143: return $dataRow;
144: }
145:
146: /**
147: * Save import rows bunch.
148: *
149: * @param string $entity
150: * @param string $behavior
151: * @param array $data
152: * @return int
153: */
154: public function saveBunch($entity, $behavior, array $data)
155: {
156: return $this->_getWriteAdapter()->insert(
157: $this->getMainTable(),
158: array('behavior' => $behavior, 'entity' => $entity, 'data' => Mage::helper('core')->jsonEncode($data))
159: );
160: }
161: }
162: