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 class Mage_Dataflow_Model_Profile extends Mage_Core_Model_Abstract
00033 {
00034 const DEFAULT_EXPORT_PATH = 'var/export';
00035 const DEFAULT_EXPORT_FILENAME = 'export_';
00036
00037 protected function _construct()
00038 {
00039 $this->_init('dataflow/profile');
00040 }
00041
00042 protected function _afterLoad()
00043 {
00044 if (is_string($this->getGuiData())) {
00045 $guiData = unserialize($this->getGuiData());
00046 } else {
00047 $guiData = '';
00048 }
00049 $this->setGuiData($guiData);
00050
00051 parent::_afterLoad();
00052 }
00053
00054 protected function _beforeSave()
00055 {
00056 parent::_beforeSave();
00057
00058 $actionsXML = $this->getData('actions_xml');
00059 if (strlen($actionsXML) < 0 && @simplexml_load_string('<data>'.$actionsXML.'</data>', null, LIBXML_NOERROR) === false) {
00060 Mage::throwException(Mage::helper("dataflow")->__("Actions XML is not valid."));
00061 }
00062
00063 if (is_array($this->getGuiData())) {
00064 $data = $this->getData();
00065 $guiData = $this->getGuiData();
00066 if (isset($guiData['file']['type']) && $guiData['file']['type'] == 'file') {
00067 if (strlen($guiData['file']['path']) == 0
00068 || (strlen($guiData['file']['path']) == 1
00069 && in_array($guiData['file']['path'], array('\\','/','.','!','@','#','$','%','&', '*','~', '^')))) {
00070 $guiData['file']['path'] = self::DEFAULT_EXPORT_PATH;
00071 }
00072 if (strlen($guiData['file']['filename']) == 0 ) {
00073 $guiData['file']['filename'] = self::DEFAULT_EXPORT_FILENAME.$data['entity_type'].'.'.($guiData['parse']['type']=='csv'?$guiData['parse']['type']:'xml');
00074 }
00075 $this->setGuiData($guiData);
00076 }
00077
00078
00079 $this->_parseGuiData();
00080
00081 $this->setGuiData(serialize($this->getGuiData()));
00082 }
00083
00084 if ($this->_getResource()->isProfileExists($this->getName(), $this->getId())) {
00085 Mage::throwException(Mage::helper("dataflow")->__("Profile with such name already exists."));
00086 }
00087 }
00088
00089 protected function _afterSave()
00090 {
00091 if (is_string($this->getGuiData())) {
00092 $this->setGuiData(unserialize($this->getGuiData()));
00093 }
00094
00095 Mage::getModel('dataflow/profile_history')
00096 ->setProfileId($this->getId())
00097 ->setActionCode($this->getOrigData('profile_id') ? 'update' : 'create')
00098 ->save();
00099
00100 if (isset($_FILES['file_1']['tmp_name']) || isset($_FILES['file_2']['tmp_name']) || isset($_FILES['file_3']['tmp_name'])) {
00101 for ($index = 0; $index < 3; $index++) {
00102 if ($file = $_FILES['file_'.($index+1)]['tmp_name']) {
00103 $uploader = new Varien_File_Uploader('file_'.($index+1));
00104 $uploader->setAllowedExtensions(array('csv','xml'));
00105 $path = Mage::app()->getConfig()->getTempVarDir().'/import/';
00106 $uploader->save($path);
00107 if ($uploadFile = $uploader->getUploadedFileName()) {
00108 $newFilename = 'import-'.date('YmdHis').'-'.($index+1).'_'.$uploadFile;
00109 rename($path.$uploadFile, $path.$newFilename);
00110 }
00111 }
00112 }
00113 }
00114 parent::_afterSave();
00115 }
00116
00117
00118
00119
00120
00121
00122 public function run()
00123 {
00124
00125
00126
00127 Mage::getModel('dataflow/profile_history')
00128 ->setProfileId($this->getId())
00129 ->setActionCode('run')
00130 ->save();
00131
00132
00133
00134
00135 $xml = '<convert version="1.0"><profile name="default">'.$this->getActionsXml().'</profile></convert>';
00136 $profile = Mage::getModel('core/convert')
00137 ->importXml($xml)
00138 ->getProfile('default');
00139
00140
00141 try {
00142 $batch = Mage::getSingleton('dataflow/batch')
00143 ->setProfileId($this->getId())
00144 ->setStoreId($this->getStoreId())
00145 ->save();
00146 $this->setBatchId($batch->getId());
00147
00148
00149
00150
00151
00152 $profile->setDataflowProfile($this->getData());
00153 $profile->run();
00154 }
00155 catch (Exception $e) {
00156 echo $e;
00157 }
00158
00159
00160
00161
00162
00163 $this->setExceptions($profile->getExceptions());
00164 return $this;
00165 }
00166
00167 public function _parseGuiData()
00168 {
00169 $nl = "\r\n";
00170 $import = $this->getDirection()==='import';
00171 $p = $this->getGuiData();
00172
00173 if ($this->getDataTransfer()==='interactive') {
00174
00175
00176
00177
00178 $interactiveXml = '<action type="dataflow/convert_adapter_http" method="'.($import?'load':'save').'">'.$nl;
00179 #$interactiveXml .= ' <var name="filename"><![CDATA['.$p['interactive']['filename'].']]></var>'.$nl;
00180 $interactiveXml .= '</action>';
00181
00182 $fileXml = '';
00183 } else {
00184 $interactiveXml = '';
00185
00186 $fileXml = '<action type="dataflow/convert_adapter_io" method="'.($import?'load':'save').'">'.$nl;
00187 $fileXml .= ' <var name="type">'.$p['file']['type'].'</var>'.$nl;
00188 $fileXml .= ' <var name="path">'.$p['file']['path'].'</var>'.$nl;
00189 $fileXml .= ' <var name="filename"><![CDATA['.$p['file']['filename'].']]></var>'.$nl;
00190 if ($p['file']['type']==='ftp') {
00191 $hostArr = explode(':', $p['file']['host']);
00192 $fileXml .= ' <var name="host"><![CDATA['.$hostArr[0].']]></var>'.$nl;
00193 if (isset($hostArr[1])) {
00194 $fileXml .= ' <var name="port"><![CDATA['.$hostArr[1].']]></var>'.$nl;
00195 }
00196 if (!empty($p['file']['passive'])) {
00197 $fileXml .= ' <var name="passive">true</var>'.$nl;
00198 }
00199 if (!empty($p['file']['user'])) {
00200 $fileXml .= ' <var name="user"><![CDATA['.$p['file']['user'].']]></var>'.$nl;
00201 }
00202 if (!empty($p['file']['password'])) {
00203 $fileXml .= ' <var name="password"><![CDATA['.$p['file']['password'].']]></var>'.$nl;
00204 }
00205 }
00206 if ($import) {
00207 $fileXml .= ' <var name="format"><![CDATA['.$p['parse']['type'].']]></var>'.$nl;
00208 }
00209 $fileXml .= '</action>'.$nl.$nl;
00210 }
00211
00212 switch ($p['parse']['type']) {
00213 case 'excel_xml':
00214 $parseFileXml = '<action type="dataflow/convert_parser_xml_excel" method="'.($import?'parse':'unparse').'">'.$nl;
00215 $parseFileXml .= ' <var name="single_sheet"><![CDATA['.($p['parse']['single_sheet']!==''?$p['parse']['single_sheet']:'').']]></var>'.$nl;
00216 break;
00217
00218 case 'csv':
00219 $parseFileXml = '<action type="dataflow/convert_parser_csv" method="'.($import?'parse':'unparse').'">'.$nl;
00220 $parseFileXml .= ' <var name="delimiter"><![CDATA['.$p['parse']['delimiter'].']]></var>'.$nl;
00221 $parseFileXml .= ' <var name="enclose"><![CDATA['.$p['parse']['enclose'].']]></var>'.$nl;
00222 break;
00223 }
00224 $parseFileXml .= ' <var name="fieldnames">'.$p['parse']['fieldnames'].'</var>'.$nl;
00225 $parseFileXmlInter = $parseFileXml;
00226 $parseFileXml .= '</action>'.$nl.$nl;
00227
00228 $mapXml = '';
00229
00230 if (isset($p['map']) && is_array($p['map'])) {
00231 foreach ($p['map'] as $side=>$fields) {
00232 if (!is_array($fields)) {
00233 continue;
00234 }
00235 foreach ($fields['db'] as $i=>$k) {
00236 if ($k=='' || $k=='0') {
00237 unset($p['map'][$side]['db'][$i]);
00238 unset($p['map'][$side]['file'][$i]);
00239 }
00240 }
00241 }
00242 }
00243 $mapXml .= '<action type="dataflow/convert_mapper_column" method="map">'.$nl;
00244 $map = $p['map'][$this->getEntityType()];
00245 if (sizeof($map['db'])>0) {
00246 $from = $map[$import?'file':'db'];
00247 $to = $map[$import?'db':'file'];
00248 $mapXml .= ' <var name="map">'.$nl;
00249 $parseFileXmlInter .= ' <var name="map">'.$nl;
00250 foreach ($from as $i=>$f) {
00251 $mapXml .= ' <map name="'.$f.'"><![CDATA['.$to[$i].']]></map>'.$nl;
00252 $parseFileXmlInter .= ' <map name="'.$f.'"><![CDATA['.$to[$i].']]></map>'.$nl;
00253 }
00254 $mapXml .= ' </var>'.$nl;
00255 $parseFileXmlInter .= ' </var>'.$nl;
00256 }
00257 if ($p['map']['only_specified']) {
00258 $mapXml .= ' <var name="_only_specified">'.$p['map']['only_specified'].'</var>'.$nl;
00259
00260 $parseFileXmlInter .= ' <var name="_only_specified">'.$p['map']['only_specified'].'</var>'.$nl;
00261 }
00262 $mapXml .= '</action>'.$nl.$nl;
00263
00264 $parsers = array(
00265 'product'=>'catalog/convert_parser_product',
00266 'customer'=>'customer/convert_parser_customer',
00267 );
00268
00269 if ($import) {
00270
00271 $parseFileXmlInter .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00272
00273
00274
00275
00276
00277
00278
00279
00280 } else {
00281 $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="unparse">'.$nl;
00282 $parseDataXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00283 if (isset($p['export']['add_url_field'])) {
00284 $parseDataXml .= ' <var name="url_field"><![CDATA['.$p['export']['add_url_field'].']]></var>'.$nl;
00285 }
00286 $parseDataXml .= '</action>'.$nl.$nl;
00287 }
00288
00289 $adapters = array(
00290 'product'=>'catalog/convert_adapter_product',
00291 'customer'=>'customer/convert_adapter_customer',
00292 );
00293
00294 if ($import) {
00295 $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="save">'.$nl;
00296 $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00297 $entityXml .= '</action>'.$nl.$nl;
00298 } else {
00299 $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="load">'.$nl;
00300 $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00301 foreach ($p[$this->getEntityType()]['filter'] as $f=>$v) {
00302
00303 if (empty($v)) {
00304 continue;
00305 }
00306 if (is_scalar($v)) {
00307 $entityXml .= ' <var name="filter/'.$f.'"><![CDATA['.$v.']]></var>'.$nl;
00308 $parseFileXmlInter .= ' <var name="filter/'.$f.'"><![CDATA['.$v.']]></var>'.$nl;
00309 } elseif (is_array($v)) {
00310 foreach ($v as $a=>$b) {
00311
00312 if (strlen($b) == 0) {
00313 continue;
00314 }
00315 $entityXml .= ' <var name="filter/'.$f.'/'.$a.'"><![CDATA['.$b.']]></var>'.$nl;
00316 $parseFileXmlInter .= ' <var name="filter/'.$f.'/'.$a.'"><![CDATA['.$b.']]></var>'.$nl;
00317 }
00318 }
00319 }
00320 $entityXml .= '</action>'.$nl.$nl;
00321 }
00322
00323
00324 if ($import) {
00325 $numberOfRecords = isset($p['import']['number_of_records']) ? $p['import']['number_of_records'] : 1;
00326 $decimalSeparator = isset($p['import']['decimal_separator']) ? $p['import']['decimal_separator'] : '.';
00327 $parseFileXmlInter .= ' <var name="number_of_records">'
00328 . $numberOfRecords . '</var>' . $nl;
00329 $parseFileXmlInter .= ' <var name="decimal_separator"><![CDATA['
00330 . $decimalSeparator . ']]></var>' . $nl;
00331 if ($this->getDataTransfer()==='interactive') {
00332 $xml = $parseFileXmlInter;
00333 $xml .= ' <var name="adapter">'.$adapters[$this->getEntityType()].'</var>'.$nl;
00334 $xml .= ' <var name="method">parse</var>'.$nl;
00335 $xml .= '</action>';
00336 } else {
00337 $xml = $fileXml;
00338 $xml .= $parseFileXmlInter;
00339 $xml .= ' <var name="adapter">'.$adapters[$this->getEntityType()].'</var>'.$nl;
00340 $xml .= ' <var name="method">parse</var>'.$nl;
00341 $xml .= '</action>';
00342 }
00343
00344
00345 } else {
00346 $xml = $entityXml.$parseDataXml.$mapXml.$parseFileXml.$fileXml.$interactiveXml;
00347 }
00348
00349 $this->setGuiData($p);
00350 $this->setActionsXml($xml);
00351
00352
00353
00354 return $this;
00355 }
00356 }