PageRenderTime 26ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/ImportExport/Model/Export.php

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