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: * Export model
29: *
30: * @category Mage
31: * @package Mage_ImportExport
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_ImportExport_Model_Export extends Mage_ImportExport_Model_Abstract
35: {
36: const FILTER_ELEMENT_GROUP = 'export_filter';
37: const FILTER_ELEMENT_SKIP = 'skip_attr';
38:
39: /**
40: * Filter fields types.
41: */
42: const FILTER_TYPE_SELECT = 'select';
43: const FILTER_TYPE_INPUT = 'input';
44: const FILTER_TYPE_DATE = 'date';
45: const FILTER_TYPE_NUMBER = 'number';
46:
47: /**
48: * Config keys.
49: */
50: const CONFIG_KEY_ENTITIES = 'global/importexport/export_entities';
51: const CONFIG_KEY_FORMATS = 'global/importexport/export_file_formats';
52:
53: /**
54: * Entity adapter.
55: *
56: * @var Mage_ImportExport_Model_Export_Entity_Abstract
57: */
58: protected $_entityAdapter;
59:
60: /**
61: * Writer object instance.
62: *
63: * @var Mage_ImportExport_Model_Export_Adapter_Abstract
64: */
65: protected $_writer;
66:
67: /**
68: * Create instance of entity adapter and returns it.
69: *
70: * @throws Exception
71: * @return Mage_ImportExport_Model_Export_Entity_Abstract
72: */
73: protected function _getEntityAdapter()
74: {
75: if (!$this->_entityAdapter) {
76: $validTypes = Mage_ImportExport_Model_Config::getModels(self::CONFIG_KEY_ENTITIES);
77:
78: if (isset($validTypes[$this->getEntity()])) {
79: try {
80: $this->_entityAdapter = Mage::getModel($validTypes[$this->getEntity()]['model']);
81: } catch (Exception $e) {
82: Mage::logException($e);
83: Mage::throwException(
84: Mage::helper('importexport')->__('Invalid entity model')
85: );
86: }
87: if (! $this->_entityAdapter instanceof Mage_ImportExport_Model_Export_Entity_Abstract) {
88: Mage::throwException(
89: Mage::helper('importexport')->__('Entity adapter obejct must be an instance of Mage_ImportExport_Model_Export_Entity_Abstract')
90: );
91: }
92: } else {
93: Mage::throwException(Mage::helper('importexport')->__('Invalid entity'));
94: }
95: // check for entity codes integrity
96: if ($this->getEntity() != $this->_entityAdapter->getEntityTypeCode()) {
97: Mage::throwException(
98: Mage::helper('importexport')->__('Input entity code is not equal to entity adapter code')
99: );
100: }
101: $this->_entityAdapter->setParameters($this->getData());
102: }
103: return $this->_entityAdapter;
104: }
105:
106: /**
107: * Get writer object.
108: *
109: * @throws Mage_Core_Exception
110: * @return Mage_ImportExport_Model_Export_Adapter_Abstract
111: */
112: protected function _getWriter()
113: {
114: if (!$this->_writer) {
115: $validWriters = Mage_ImportExport_Model_Config::getModels(self::CONFIG_KEY_FORMATS);
116:
117: if (isset($validWriters[$this->getFileFormat()])) {
118: try {
119: $this->_writer = Mage::getModel($validWriters[$this->getFileFormat()]['model']);
120: } catch (Exception $e) {
121: Mage::logException($e);
122: Mage::throwException(
123: Mage::helper('importexport')->__('Invalid entity model')
124: );
125: }
126: if (! $this->_writer instanceof Mage_ImportExport_Model_Export_Adapter_Abstract) {
127: Mage::throwException(
128: Mage::helper('importexport')->__('Adapter object must be an instance of %s', 'Mage_ImportExport_Model_Export_Adapter_Abstract')
129: );
130: }
131: } else {
132: Mage::throwException(Mage::helper('importexport')->__('Invalid file format'));
133: }
134: }
135: return $this->_writer;
136: }
137:
138: /**
139: * Export data.
140: *
141: * @throws Mage_Core_Exception
142: * @return string
143: */
144: public function export()
145: {
146: if (isset($this->_data[self::FILTER_ELEMENT_GROUP])) {
147: $this->addLogComment(Mage::helper('importexport')->__('Begin export of %s', $this->getEntity()));
148: $result = $this->_getEntityAdapter()
149: ->setWriter($this->_getWriter())
150: ->export();
151: $countRows = substr_count(trim($result), "\n");
152: if (!$countRows) {
153: Mage::throwException(
154: Mage::helper('importexport')->__('There is no data for export')
155: );
156: }
157: if ($result) {
158: $this->addLogComment(array(
159: Mage::helper('importexport')->__('Exported %s rows.', $countRows),
160: Mage::helper('importexport')->__('Export has been done.')
161: ));
162: }
163: return $result;
164: } else {
165: Mage::throwException(
166: Mage::helper('importexport')->__('No filter data provided')
167: );
168: }
169: }
170:
171: /**
172: * Clean up already loaded attribute collection.
173: *
174: * @param Mage_Eav_Model_Resource_Entity_Attribute_Collection $collection
175: * @return Mage_Eav_Model_Resource_Entity_Attribute_Collection
176: */
177: public function filterAttributeCollection(Mage_Eav_Model_Resource_Entity_Attribute_Collection $collection)
178: {
179: return $this->_getEntityAdapter()->filterAttributeCollection($collection);
180: }
181:
182: /**
183: * Determine filter type for specified attribute.
184: *
185: * @static
186: * @param Mage_Eav_Model_Entity_Attribute $attribute
187: * @throws Exception
188: * @return string
189: */
190: public static function getAttributeFilterType(Mage_Eav_Model_Entity_Attribute $attribute)
191: {
192: if ($attribute->usesSource() || $attribute->getFilterOptions()) {
193: return self::FILTER_TYPE_SELECT;
194: } elseif ('datetime' == $attribute->getBackendType()) {
195: return self::FILTER_TYPE_DATE;
196: } elseif ('decimal' == $attribute->getBackendType() || 'int' == $attribute->getBackendType()) {
197: return self::FILTER_TYPE_NUMBER;
198: } elseif ($attribute->isStatic()
199: || 'varchar' == $attribute->getBackendType()
200: || 'text' == $attribute->getBackendType()
201: ) {
202: return self::FILTER_TYPE_INPUT;
203: } else {
204: Mage::throwException(
205: Mage::helper('importexport')->__('Can not determine attribute filter type')
206: );
207: }
208: }
209:
210: /**
211: * MIME-type for 'Content-Type' header.
212: *
213: * @return string
214: */
215: public function getContentType()
216: {
217: return $this->_getWriter()->getContentType();
218: }
219:
220: /**
221: * Override standard entity getter.
222: *
223: * @throw Exception
224: * @return string
225: */
226: public function getEntity()
227: {
228: if (empty($this->_data['entity'])) {
229: Mage::throwException(Mage::helper('importexport')->__('Entity is unknown'));
230: }
231: return $this->_data['entity'];
232: }
233:
234: /**
235: * Entity attributes collection getter.
236: *
237: * @return Mage_Eav_Model_Resource_Entity_Attribute_Collection
238: */
239: public function getEntityAttributeCollection()
240: {
241: return $this->_getEntityAdapter()->getAttributeCollection();
242: }
243:
244: /**
245: * Override standard entity getter.
246: *
247: * @throw Exception
248: * @return string
249: */
250: public function getFileFormat()
251: {
252: if (empty($this->_data['file_format'])) {
253: Mage::throwException(Mage::helper('importexport')->__('File format is unknown'));
254: }
255: return $this->_data['file_format'];
256: }
257:
258: /**
259: * Return file name for downloading.
260: *
261: * @return string
262: */
263: public function getFileName()
264: {
265: return $this->getEntity() . '_' . date('Ymd_His') . '.' . $this->_getWriter()->getFileExtension();
266: }
267: }
268: