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: * Abstract adapter model
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_ImportExport_Model_Export_Adapter_Abstract
35: {
36: /**
37: * Destination file path.
38: *
39: * @var string
40: */
41: protected $_destination;
42:
43: /**
44: * Header columns names.
45: *
46: * @var array
47: */
48: protected $_headerCols = null;
49:
50: /**
51: * Adapter object constructor.
52: *
53: * @param string $destination OPTIONAL Destination file path.
54: * @throws Exception
55: * @return void
56: */
57: final public function __construct($destination = null)
58: {
59: if (!$destination) {
60: $destination = tempnam(sys_get_temp_dir(), 'importexport_');
61: }
62: if (!is_string($destination)) {
63: Mage::throwException(Mage::helper('importexport')->__('Destination file path must be a string'));
64: }
65: $pathinfo = pathinfo($destination);
66:
67: if (empty($pathinfo['dirname']) || !is_writable($pathinfo['dirname'])) {
68: Mage::throwException(Mage::helper('importexport')->__('Destination directory is not writable'));
69: }
70: if (is_file($destination) && !is_writable($destination)) {
71: Mage::throwException(Mage::helper('importexport')->__('Destination file is not writable'));
72: }
73: $this->_destination = $destination;
74:
75: $this->_init();
76: }
77:
78: /**
79: * Method called as last step of object instance creation. Can be overridden in child classes.
80: *
81: * @return Mage_ImportExport_Model_Export_Adapter_Abstract
82: */
83: protected function _init()
84: {
85: return $this;
86: }
87:
88: /**
89: * Get contents of export file.
90: *
91: * @return string
92: */
93: public function getContents()
94: {
95: return file_get_contents($this->_destination);
96: }
97:
98: /**
99: * MIME-type for 'Content-Type' header.
100: *
101: * @return string
102: */
103: public function getContentType()
104: {
105: return 'application/octet-stream';
106: }
107:
108: /**
109: * Return file extension for downloading.
110: *
111: * @return string
112: */
113: public function getFileExtension()
114: {
115: return '';
116: }
117:
118: /**
119: * Set column names.
120: *
121: * @param array $headerCols
122: * @throws Exception
123: * @return Mage_ImportExport_Model_Export_Adapter_Abstract
124: */
125: public function setHeaderCols(array $headerCols)
126: {
127: if (null !== $this->_headerCols) {
128: Mage::throwException(Mage::helper('importexport')->__('Header column names already set'));
129: }
130: if ($headerCols) {
131: foreach ($headerCols as $colName) {
132: $this->_headerCols[$colName] = false;
133: }
134: fputcsv($this->_fileHandler, array_keys($this->_headerCols), $this->_delimiter, $this->_enclosure);
135: }
136: return $this;
137: }
138:
139: /**
140: * Write row data to source file.
141: *
142: * @param array $rowData
143: * @return Mage_ImportExport_Model_Export_Adapter_Abstract
144: */
145: abstract public function writeRow(array $rowData);
146: }
147: