00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Dataflow_Model_Convert_Adapter_Io extends Mage_Dataflow_Model_Convert_Adapter_Abstract
00036 {
00037
00038
00039
00040 public function getResource($forWrite = false)
00041 {
00042 if (!$this->_resource) {
00043 $type = $this->getVar('type', 'file');
00044 $className = 'Varien_Io_'.ucwords($type);
00045 $this->_resource = new $className();
00046
00047 $isError = false;
00048
00049 $ioConfig = $this->getVars();
00050 switch ($this->getVar('type', 'file')) {
00051 case 'file':
00052 if (preg_match('#^'.preg_quote(DS, '#').'#', $this->getVar('path')) ||
00053 preg_match('#^[a-z]:'.preg_quote(DS, '#') .'#i', $this->getVar('path'))) {
00054
00055 $path = $this->_resource->getCleanPath($this->getVar('path'));
00056 }
00057 else {
00058 $baseDir = Mage::getBaseDir();
00059 $path = $this->_resource->getCleanPath($baseDir . DS . trim($this->getVar('path'), DS));
00060 }
00061
00062 $this->_resource->checkAndCreateFolder($path);
00063
00064 $realPath = realpath($path);
00065
00066 if (!$isError && $realPath === false) {
00067 $message = Mage::helper('dataflow')->__('Destination folder "%s" does not exist or not access to create', $ioConfig['path']);
00068 Mage::throwException($message);
00069 }
00070 elseif (!$isError && !is_dir($realPath)) {
00071 $message = Mage::helper('dataflow')->__('Destination folder "%s" is not a directory', $realPath);
00072 Mage::throwException($message);
00073 }
00074 elseif (!$isError) {
00075 if ($forWrite && !is_writeable($realPath)) {
00076 $message = Mage::helper('dataflow')->__('Destination folder "%s" is not a writeable', $realPath);
00077 Mage::throwException($message);
00078 }
00079 else {
00080 $ioConfig['path'] = rtrim($realPath, DS);
00081 }
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 break;
00114 default:
00115 $ioConfig['path'] = rtrim($this->getVar('path'), '/');
00116 break;
00117 }
00118
00119 if ($isError) {
00120 return false;
00121 }
00122 try {
00123 $this->_resource->open($ioConfig);
00124 } catch (Exception $e) {
00125 $message = Mage::helper('dataflow')->__('Error occured during file opening: "%s"', $e->getMessage());
00126 Mage::throwException($message);
00127 }
00128 }
00129 return $this->_resource;
00130 }
00131
00132
00133
00134
00135
00136
00137 public function load()
00138 {
00139 if (!$this->getResource()) {
00140 return $this;
00141 }
00142
00143 $batchModel = Mage::getSingleton('dataflow/batch');
00144 $destFile = $batchModel->getIoAdapter()->getFile(true);
00145
00146 $result = $this->getResource()->read($this->getVar('filename'), $destFile);
00147 $filename = $this->getResource()->pwd() . '/' . $this->getVar('filename');
00148 if (false === $result) {
00149 $message = Mage::helper('dataflow')->__('Could not load file: "%s"', $filename);
00150 Mage::throwException($message);
00151 } else {
00152 $message = Mage::helper('dataflow')->__('Loaded successfully: "%s"', $filename);
00153 $this->addException($message);
00154 }
00155
00156 $this->setData($result);
00157 return $this;
00158 }
00159
00160
00161
00162
00163
00164
00165 public function save()
00166 {
00167 if (!$this->getResource(true)) {
00168 return $this;
00169 }
00170
00171 $batchModel = Mage::getSingleton('dataflow/batch');
00172
00173 $dataFile = $batchModel->getIoAdapter()->getFile(true);
00174
00175 $filename = $this->getVar('filename');
00176
00177 $result = $this->getResource()->write($filename, $dataFile, 0777);
00178
00179 if (false === $result) {
00180 $message = Mage::helper('dataflow')->__('Could not save file: %s', $filename);
00181 Mage::throwException($message);
00182 } else {
00183 $message = Mage::helper('dataflow')->__('Saved successfully: "%s" [%d byte(s)]', $filename, $batchModel->getIoAdapter()->getFileSize());
00184 if ($this->getVar('link')) {
00185 $message .= Mage::helper('dataflow')->__('<a href="%s" target="_blank">Link</a>', $this->getVar('link'));
00186 }
00187 $this->addException($message);
00188 }
00189 return $this;
00190 }
00191 }