*/ class Mage_Dataflow_Model_Profile extends Mage_Core_Model_Abstract { const DEFAULT_EXPORT_PATH = 'var/export'; const DEFAULT_EXPORT_FILENAME = 'export_'; protected function _construct() { $this->_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 (strlen($actionsXML) < 0 && @simplexml_load_string('' . $actionsXML . '', null, LIBXML_NOERROR) === false) { Mage::throwException(Mage::helper('dataflow')->__("Actions XML is not valid.")); } if (is_array($this->getGuiData())) { $data = $this->getData(); $guiData = $this->getGuiData(); $charSingleList = array('\\', '/', '.', '!', '@', '#', '$', '%', '&', '*', '~', '^'); if (isset($guiData['file']['type']) && $guiData['file']['type'] == 'file') { if (empty($guiData['file']['path']) || (strlen($guiData['file']['path']) == 1 && in_array($guiData['file']['path'], $charSingleList))) { $guiData['file']['path'] = self::DEFAULT_EXPORT_PATH; } if (empty($guiData['file']['filename'])) { $guiData['file']['filename'] = self::DEFAULT_EXPORT_FILENAME . $data['entity_type'] . '.' . ($guiData['parse']['type']=='csv' ? $guiData['parse']['type'] : 'xml'); } //validate export available path $path = rtrim($guiData['file']['path'], '\\/') . DS . $guiData['file']['filename']; /** @var $validator Mage_Core_Model_File_Validator_AvailablePath */ $validator = Mage::getModel('core/file_validator_availablePath'); /** @var $helperImportExport Mage_ImportExport_Helper_Data */ $helperImportExport = Mage::helper('importexport'); $validator->setPaths($helperImportExport->getLocalValidPaths()); if (!$validator->isValid($path)) { foreach ($validator->getMessages() as $message) { Mage::throwException($message); } } $this->setGuiData($guiData); } $this->_parseGuiData(); $this->setGuiData(serialize($this->getGuiData())); } if ($this->_getResource()->isProfileExists($this->getName(), $this->getId())) { Mage::throwException(Mage::helper('dataflow')->__("Profile with the same name already exists.")); } } protected function _afterSave() { if (is_string($this->getGuiData())) { $this->setGuiData(unserialize($this->getGuiData())); } $profileHistory = Mage::getModel('dataflow/profile_history'); $adminUserId = $this->getAdminUserId(); if($adminUserId) { $profileHistory->setUserId($adminUserId); } $profileHistory ->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 Mage_Core_Model_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); } } //BOM deleting for UTF files if (isset($newFilename) && $newFilename) { $contents = file_get_contents($path . $newFilename); if (ord($contents[0]) == 0xEF && ord($contents[1]) == 0xBB && ord($contents[2]) == 0xBF) { $contents = substr($contents, 3); file_put_contents($path . $newFilename, $contents); } unset($contents); } } } parent::_afterSave(); } /** * Run profile * * @return Mage_Dataflow_Model_Profile */ public function run() { /** * Save history */ Mage::getModel('dataflow/profile_history') ->setProfileId($this->getId()) ->setActionCode('run') ->save(); /** * Prepare xml convert profile actions data */ $xml = '' . $this->getActionsXml() . ''; $profile = Mage::getModel('core/convert') ->importXml($xml) ->getProfile('default'); /* @var $profile Mage_Dataflow_Model_Convert_Profile */ try { $batch = Mage::getSingleton('dataflow/batch') ->setProfileId($this->getId()) ->setStoreId($this->getStoreId()) ->save(); $this->setBatchId($batch->getId()); $profile->setDataflowProfile($this->getData()); $profile->run(); } catch (Exception $e) { echo $e; } // if ($batch) { // $batch->delete(); // } $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']['file_mode'])) && ($p['file']['file_mode'] == FTP_ASCII || $p['file']['file_mode'] == FTP_BINARY) ) { $fileXml .= ' ' . $p['file']['file_mode'] . '' . $nl; } if (!empty($p['file']['user'])) { $fileXml .= ' ' . $nl; } if (!empty($p['file']['password'])) { $fileXml .= ' ' . $nl; } } if ($import) { $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']; $mapXml .= ' ' . $nl; $parseFileXmlInter .= ' ' . $nl; foreach ($from as $i=>$f) { $mapXml .= ' ' . $nl; $parseFileXmlInter .= ' ' . $nl; } $mapXml .= ' ' . $nl; $parseFileXmlInter .= ' ' . $nl; } if ($p['map']['only_specified']) { $mapXml .= ' ' . $p['map']['only_specified'] . '' . $nl; //$mapXml .= ' ' . $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; if (isset($p['export']['add_url_field'])) { $parseDataXml .= ' ' . $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) { $numberOfRecords = isset($p['import']['number_of_records']) ? $p['import']['number_of_records'] : 1; $decimalSeparator = isset($p['import']['decimal_separator']) ? $p['import']['decimal_separator'] : ' . '; $parseFileXmlInter .= ' ' . $numberOfRecords . '' . $nl; $parseFileXmlInter .= ' ' . $nl; if ($this->getDataTransfer()==='interactive') { $xml = $parseFileXmlInter; $xml .= ' ' . $adapters[$this->getEntityType()] . '' . $nl; $xml .= ' parse' . $nl; $xml .= ''; } else { $xml = $fileXml; $xml .= $parseFileXmlInter; $xml .= ' ' . $adapters[$this->getEntityType()] . '' . $nl; $xml .= ' parse' . $nl; $xml .= ''; } //$xml = $interactiveXml.$fileXml.$parseFileXml.$mapXml.$parseDataXml.$entityXml; } else { $xml = $entityXml . $parseDataXml . $mapXml . $parseFileXml . $fileXml . $interactiveXml; } $this->setGuiData($p); $this->setActionsXml($xml); /*echo "
" . print_r($p,1) . "
"; echo "" . $xml . ""; die;*/ return $this; } }