_init('dataflow/profile'); } protected function _afterLoad() { if (is_string($this->getGuiData())) { $guiData = unserialize($this->getGuiData()); } else { $guiData = ''; } $this->setGuiData($guiData); parent::_afterLoad(); } protected function _beforeSave() { parent::_beforeSave(); $actionsXML = $this->getData('actions_xml'); if (0 < strlen($actionsXML) && false === simplexml_load_string(''.$actionsXML.'', null, LIBXML_NOERROR)) { Mage::throwException(Mage::helper("dataflow")->__("Actions XML is not valid.")); } if (is_array($this->getGuiData())) { $data = $this->getData(); $guiData = $this->getGuiData(); if (isset($guiData['file']['type']) && $guiData['file']['type'] == 'file') { if (strlen($guiData['file']['path']) == 0 || (strlen($guiData['file']['path']) == 1 && in_array($guiData['file']['path'], array('\\','/','.','!','@','#','$','%','&', '*','~', '^')))) { $guiData['file']['path'] = self::DEFAULT_EXPORT_PATH; } if (strlen($guiData['file']['filename']) == 0 ) { $guiData['file']['filename'] = self::DEFAULT_EXPORT_FILENAME.$data['entity_type'].'.'.($guiData['parse']['type']=='csv'?$guiData['parse']['type']:'xml'); } $this->setGuiData($guiData); } $this->_parseGuiData(); $this->setGuiData(serialize($this->getGuiData())); } } protected function _afterSave() { if (is_string($this->getGuiData())) { $this->setGuiData(unserialize($this->getGuiData())); } Mage::getModel('dataflow/profile_history') ->setProfileId($this->getId()) ->setActionCode($this->getOrigData('profile_id') ? 'update' : 'create') ->save(); if (isset($_FILES['file_1']['tmp_name']) || isset($_FILES['file_2']['tmp_name']) || isset($_FILES['file_3']['tmp_name'])) { for ($index = 0; $index < 3; $index++) { if ($file = $_FILES['file_'.($index+1)]['tmp_name']) { $uploader = new Varien_File_Uploader('file_'.($index+1)); $uploader->setAllowedExtensions(array('csv','xml')); $path = Mage::app()->getConfig()->getTempVarDir().'/import/'; $uploader->save($path); if ($uploadFile = $uploader->getUploadedFileName()) { $newFilename = 'import-'.date('YmdHis').'-'.($index+1).'_'.$uploadFile; rename($path.$uploadFile, $path.$newFilename); } } } } parent::_afterSave(); } public function run() { Mage::getModel('dataflow/profile_history') ->setProfileId($this->getId()) ->setActionCode('run') ->save(); $xml = ''.$this->getActionsXml().''; $profile = Mage::getModel('core/convert')->importXml($xml)->getProfile('default'); try { $profile->setDataflowProfile($this->getData()); $profile->run(); } catch (Exception $e) { } $this->setExceptions($profile->getExceptions()); return $this; } public function _parseGuiData() { $nl = "\r\n"; $import = $this->getDirection()==='import'; $p = $this->getGuiData(); if ($this->getDataTransfer()==='interactive') { // $p['file']['type'] = 'file'; // $p['file']['filename'] = $p['interactive']['filename']; // $p['file']['path'] = 'var/export'; $interactiveXml = ''.$nl; #$interactiveXml .= ' '.$nl; $interactiveXml .= ''; $fileXml = ''; } else { $interactiveXml = ''; $fileXml = ''.$nl; $fileXml .= ' '.$p['file']['type'].''.$nl; $fileXml .= ' '.$p['file']['path'].''.$nl; $fileXml .= ' '.$nl; if ($p['file']['type']==='ftp') { $hostArr = explode(':', $p['file']['host']); $fileXml .= ' '.$nl; if (isset($hostArr[1])) { $fileXml .= ' '.$nl; } if (!empty($p['file']['passive'])) { $fileXml .= ' true'.$nl; } if (!empty($p['file']['user'])) { $fileXml .= ' '.$nl; } if (!empty($p['file']['password'])) { $fileXml .= ' '.$nl; } } $fileXml .= ''.$nl.$nl; } switch ($p['parse']['type']) { case 'excel_xml': $parseFileXml = ''.$nl; $parseFileXml .= ' '.$nl; break; case 'csv': $parseFileXml = ''.$nl; $parseFileXml .= ' '.$nl; $parseFileXml .= ' '.$nl; break; } $parseFileXml .= ' '.$p['parse']['fieldnames'].''.$nl; $parseFileXmlInter = $parseFileXml; $parseFileXml .= ''.$nl.$nl; $mapXml = ''; if (isset($p['map']) && is_array($p['map'])) { foreach ($p['map'] as $side=>$fields) { if (!is_array($fields)) { continue; } foreach ($fields['db'] as $i=>$k) { if ($k=='' || $k=='0') { unset($p['map'][$side]['db'][$i]); unset($p['map'][$side]['file'][$i]); } } } } $mapXml .= ''.$nl; $map = $p['map'][$this->getEntityType()]; if (sizeof($map['db'])>0) { $from = $map[$import?'file':'db']; $to = $map[$import?'db':'file']; foreach ($from as $i=>$f) { $mapXml .= ' '.$nl; $parseFileXmlInter .= ' '.$nl; } } if ($p['map']['only_specified']) { $mapXml .= ' '.$p['map']['only_specified'].''.$nl; $parseFileXmlInter .= ' '.$p['map']['only_specified'].''.$nl; } $mapXml .= ''.$nl.$nl; $parsers = array( 'product'=>'catalog/convert_parser_product', 'customer'=>'customer/convert_parser_customer', ); if ($import) { if ($this->getDataTransfer()==='interactive') { $parseFileXmlInter .= ' getStoreId().']]>'.$nl; } else { $parseDataXml = ''.$nl; $parseDataXml .= ' getStoreId().']]>'.$nl; $parseDataXml .= ''.$nl.$nl; } // $parseDataXml = ''.$nl; // $parseDataXml .= ' getStoreId().']]>'.$nl; // $parseDataXml .= ''.$nl.$nl; } else { $parseDataXml = ''.$nl; $parseDataXml .= ' getStoreId().']]>'.$nl; $parseDataXml .= ''.$nl.$nl; } $adapters = array( 'product'=>'catalog/convert_adapter_product', 'customer'=>'customer/convert_adapter_customer', ); if ($import) { $entityXml = ''.$nl; $entityXml .= ' getStoreId().']]>'.$nl; $entityXml .= ''.$nl.$nl; } else { $entityXml = ''.$nl; $entityXml .= ' getStoreId().']]>'.$nl; foreach ($p[$this->getEntityType()]['filter'] as $f=>$v) { if (empty($v)) { continue; } if (is_scalar($v)) { $entityXml .= ' '.$nl; $parseFileXmlInter .= ' '.$nl; } elseif (is_array($v)) { foreach ($v as $a=>$b) { if (strlen($b) == 0) { continue; } $entityXml .= ' '.$nl; $parseFileXmlInter .= ' '.$nl; } } } $entityXml .= ''.$nl.$nl; } // Need to rewrite the whole xml action format if ($import) { if ($this->getDataTransfer()==='interactive') { $xml = $parseFileXmlInter; $xml .= ' '.$adapters[$this->getEntityType()].''.$nl; $xml .= ' saveRow'.$nl; $xml .= ''; } else { $xml = $fileXml; $xml .= $parseFileXmlInter; $xml .= ' '.$adapters[$this->getEntityType()].''.$nl; $xml .= ' saveRow'.$nl; $xml .= ''; } //$xml = $interactiveXml.$fileXml.$parseFileXml.$mapXml.$parseDataXml.$entityXml; } else { $xml = $entityXml.$parseDataXml.$mapXml.$parseFileXml.$fileXml.$interactiveXml; } $this->setGuiData($p); $this->setActionsXml($xml); return $this; } }