Mage_Dataflow_Model_Profile Class Reference

Inheritance diagram for Mage_Dataflow_Model_Profile:

Mage_Core_Model_Abstract Varien_Object

List of all members.

Public Member Functions

 run ()
 _parseGuiData ()

Public Attributes

const DEFAULT_EXPORT_PATH = 'var/export'
const DEFAULT_EXPORT_FILENAME = 'export_'

Protected Member Functions

 _construct ()
 _afterLoad ()
 _beforeSave ()
 _afterSave ()


Detailed Description

Convert profile

Author:
Magento Core Team <core@magentocommerce.com>

Definition at line 32 of file Profile.php.


Member Function Documentation

_afterLoad (  )  [protected]

Processing object after load data

Returns:
Mage_Core_Model_Abstract

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 42 of file Profile.php.

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     }

_afterSave (  )  [protected]

Processing object after save data

Returns:
Mage_Core_Model_Abstract

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 89 of file Profile.php.

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     }

_beforeSave (  )  [protected]

Processing object before save data

Returns:
Mage_Core_Model_Abstract

Reimplemented from Mage_Core_Model_Abstract.

Definition at line 54 of file Profile.php.

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 //          echo '<pre>';
00078 //          print_r($this->getGuiData());
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     }

_construct (  )  [protected]

Enter description here...

Reimplemented from Varien_Object.

Definition at line 37 of file Profile.php.

00038     {
00039         $this->_init('dataflow/profile');
00040     }

_parseGuiData (  ) 

Definition at line 167 of file Profile.php.

00168     {
00169         $nl = "\r\n";
00170         $import = $this->getDirection()==='import';
00171         $p = $this->getGuiData();
00172 
00173         if ($this->getDataTransfer()==='interactive') {
00174 //            $p['file']['type'] = 'file';
00175 //            $p['file']['filename'] = $p['interactive']['filename'];
00176 //            $p['file']['path'] = 'var/export';
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             //$mapXml .= '    <var name="map">'.$nl;
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 //            if ($this->getDataTransfer()==='interactive') {
00271                 $parseFileXmlInter .= '    <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00272 //            } else {
00273 //                $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="parse">'.$nl;
00274 //                $parseDataXml = '    <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00275 //                $parseDataXml .= '</action>'.$nl.$nl;
00276 //            }
00277 //            $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="parse">'.$nl;
00278 //            $parseDataXml .= '    <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
00279 //            $parseDataXml .= '</action>'.$nl.$nl;
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         // Need to rewrite the whole xml action format
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             //$xml = $interactiveXml.$fileXml.$parseFileXml.$mapXml.$parseDataXml.$entityXml;
00344 
00345         } else {
00346             $xml = $entityXml.$parseDataXml.$mapXml.$parseFileXml.$fileXml.$interactiveXml;
00347         }
00348 
00349         $this->setGuiData($p);
00350         $this->setActionsXml($xml);
00351 /*echo "<pre>".print_r($p,1)."</pre>";
00352 echo "<xmp>".$xml."</xmp>";
00353 die;*/
00354         return $this;
00355     }

run (  ) 

Run profile

Returns:
Mage_Dataflow_Model_Profile

Save history

Prepare xml convert profile actions data

Definition at line 122 of file Profile.php.

00123     {
00124         /**
00125          * Save history
00126          */
00127         Mage::getModel('dataflow/profile_history')
00128             ->setProfileId($this->getId())
00129             ->setActionCode('run')
00130             ->save();
00131 
00132         /**
00133          * Prepare xml convert profile actions data
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         /* @var $profile Mage_Dataflow_Model_Convert_Profile */
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 //            print '<pre>';
00149 //            print_r($this->getData());
00150 //            print '</pre>';
00151 
00152             $profile->setDataflowProfile($this->getData());
00153             $profile->run();
00154         }
00155         catch (Exception $e) {
00156             echo $e;
00157         }
00158 
00159 //        if ($batch) {
00160 //            $batch->delete();
00161 //        }
00162 
00163         $this->setExceptions($profile->getExceptions());
00164         return $this;
00165     }


Member Data Documentation

const DEFAULT_EXPORT_FILENAME = 'export_'

Definition at line 35 of file Profile.php.

const DEFAULT_EXPORT_PATH = 'var/export'

Definition at line 34 of file Profile.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:06 2009 for Magento by  doxygen 1.5.8