PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Dataflow/Model/Convert/Adapter/Io.php

https://github.com/ticean/magento-mirror
PHP | 172 lines | 105 code | 20 blank | 47 comment | 19 complexity | e7700e3f95a860726ce883e2106b1d9d 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_Dataflow
  23. * @copyright Copyright (c) 2010 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. * Convert IO adapter
  28. *
  29. * @category Mage
  30. * @package Mage_Dataflow
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Dataflow_Model_Convert_Adapter_Io extends Mage_Dataflow_Model_Convert_Adapter_Abstract
  34. {
  35. /**
  36. * @return Varien_Io_Abstract
  37. */
  38. public function getResource($forWrite = false)
  39. {
  40. if (!$this->_resource) {
  41. $type = $this->getVar('type', 'file');
  42. $className = 'Varien_Io_'.ucwords($type);
  43. $this->_resource = new $className();
  44. $isError = false;
  45. $ioConfig = $this->getVars();
  46. switch ($this->getVar('type', 'file')) {
  47. case 'file':
  48. //validate export/import path
  49. $path = rtrim($ioConfig['path'], '\\/')
  50. . DS . $ioConfig['filename'];
  51. /** @var $validator Mage_Core_Model_File_Validator_AvailablePath */
  52. $validator = Mage::getModel('core/file_validator_availablePath');
  53. /** @var $helper Mage_ImportExport_Helper_Data */
  54. $helper = Mage::helper('importexport');
  55. $validator->setPaths($helper->getLocalValidPaths());
  56. if (!$validator->isValid($path)) {
  57. foreach ($validator->getMessages() as $message) {
  58. Mage::throwException($message);
  59. return false;
  60. }
  61. }
  62. if (preg_match('#^' . preg_quote(DS, '#').'#', $this->getVar('path')) ||
  63. preg_match('#^[a-z]:' . preg_quote(DS, '#') . '#i', $this->getVar('path'))) {
  64. $path = $this->_resource->getCleanPath($this->getVar('path'));
  65. } else {
  66. $baseDir = Mage::getBaseDir();
  67. $path = $this->_resource->getCleanPath($baseDir . DS . trim($this->getVar('path'), DS));
  68. }
  69. $this->_resource->checkAndCreateFolder($path);
  70. $realPath = realpath($path);
  71. if (!$isError && $realPath === false) {
  72. $message = Mage::helper('dataflow')->__('The destination folder "%s" does not exist or there is no access to create it.', $ioConfig['path']);
  73. Mage::throwException($message);
  74. } elseif (!$isError && !is_dir($realPath)) {
  75. $message = Mage::helper('dataflow')->__('Destination folder "%s" is not a directory.', $realPath);
  76. Mage::throwException($message);
  77. } elseif (!$isError) {
  78. if ($forWrite && !is_writeable($realPath)) {
  79. $message = Mage::helper('dataflow')->__('Destination folder "%s" is not writable.', $realPath);
  80. Mage::throwException($message);
  81. } else {
  82. $ioConfig['path'] = rtrim($realPath, DS);
  83. }
  84. }
  85. break;
  86. default:
  87. $ioConfig['path'] = rtrim($this->getVar('path'), '/');
  88. break;
  89. }
  90. if ($isError) {
  91. return false;
  92. }
  93. try {
  94. $this->_resource->open($ioConfig);
  95. } catch (Exception $e) {
  96. $message = Mage::helper('dataflow')->__('An error occurred while opening file: "%s".', $e->getMessage());
  97. Mage::throwException($message);
  98. }
  99. }
  100. return $this->_resource;
  101. }
  102. /**
  103. * Load data
  104. *
  105. * @return Mage_Dataflow_Model_Convert_Adapter_Io
  106. */
  107. public function load()
  108. {
  109. if (!$this->getResource()) {
  110. return $this;
  111. }
  112. $batchModel = Mage::getSingleton('dataflow/batch');
  113. $destFile = $batchModel->getIoAdapter()->getFile(true);
  114. $result = $this->getResource()->read($this->getVar('filename'), $destFile);
  115. $filename = $this->getResource()->pwd() . '/' . $this->getVar('filename');
  116. if (false === $result) {
  117. $message = Mage::helper('dataflow')->__('Could not load file: "%s".', $filename);
  118. Mage::throwException($message);
  119. } else {
  120. $message = Mage::helper('dataflow')->__('Loaded successfully: "%s".', $filename);
  121. $this->addException($message);
  122. }
  123. $this->setData($result);
  124. return $this;
  125. }
  126. /**
  127. * Save result to destination file from temporary
  128. *
  129. * @return Mage_Dataflow_Model_Convert_Adapter_Io
  130. */
  131. public function save()
  132. {
  133. if (!$this->getResource(true)) {
  134. return $this;
  135. }
  136. $batchModel = Mage::getSingleton('dataflow/batch');
  137. $dataFile = $batchModel->getIoAdapter()->getFile(true);
  138. $filename = $this->getVar('filename');
  139. $result = $this->getResource()->write($filename, $dataFile, 0777);
  140. if (false === $result) {
  141. $message = Mage::helper('dataflow')->__('Could not save file: %s.', $filename);
  142. Mage::throwException($message);
  143. } else {
  144. $message = Mage::helper('dataflow')->__('Saved successfully: "%s" [%d byte(s)].', $filename, $batchModel->getIoAdapter()->getFileSize());
  145. if ($this->getVar('link')) {
  146. $message .= Mage::helper('dataflow')->__('<a href="%s" target="_blank">Link</a>', $this->getVar('link'));
  147. }
  148. $this->addException($message);
  149. }
  150. return $this;
  151. }
  152. }