OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
genimport_main.php
Go to the documentation of this file.
1 <?php
2 
7 {
13  public $sClassDo = "genImport_do";
14 
20  public $sClassMain = "genImport_main";
21 
27  protected $_sCsvFilePath = null;
28 
33  protected $_sStringTerminator = null;
34 
39  protected $_sStringEncloser = null;
40 
45  protected $_sDefaultStringTerminator = ";";
46 
51  protected $_sDefaultStringEncloser = '"';
52 
57  protected $_sThisTemplate = "genimport_main.tpl";
58 
65  public function render()
66  {
67  $oConfig = $this->getConfig();
68 
69  $oErpImport = new oxErpGenImport();
70  $this->_sCsvFilePath = null;
71 
72  $sNavStep = $oConfig->getParameter( 'sNavStep' );
73 
74  if ( !$sNavStep ) {
75  $sNavStep = 1;
76  } else {
77  $sNavStep++;
78  }
79 
80 
81  $sNavStep = $this->_checkErrors( $sNavStep );
82 
83  if ( $sNavStep == 1 ) {
84  $this->_aViewData['sGiCsvFieldTerminator'] = htmlentities( $this->_getCsvFieldsTerminator() );
85  $this->_aViewData['sGiCsvFieldEncloser'] = htmlentities( $this->_getCsvFieldsEncolser() );
86  }
87 
88  if ($sNavStep == 2) {
89  //saving csv field terminator and encloser to config
90  $sTerminator = $oConfig->getRequestParameter('sGiCsvFieldTerminator');
91  if ($sTerminator && !$configValidator->isValid($sTerminator)) {
92  $this->setErrorToView($sTerminator);
93  } else {
94  $this->_sStringTerminator = $sTerminator;
95  $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldTerminator', $sTerminator );
96  }
97 
98  $sEncloser = $oConfig->getRequestParameter('sGiCsvFieldEncloser');
99  if ($sEncloser && !$configValidator->isValid($sEncloser)) {
100  $this->setErrorToView($sEncloser);
101  } else {
102  $this->_sStringEncloser = $sEncloser;
103  $oConfig->saveShopConfVar( 'str', 'sGiCsvFieldEncloser', $sEncloser );
104  }
105 
106  $sType = $oConfig->getParameter( 'sType' );
107  $oType = $oErpImport->getImportObject( $sType );
108  $this->_aViewData['sType'] = $sType;
109  $this->_aViewData['sImportTable'] = $oType->getBaseTableName();
110  $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
111  $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
112  }
113 
114  if ( $sNavStep == 3 ) {
115  $aCsvFields = $oConfig->getParameter( 'aCsvFields' );
116  $sType = $oConfig->getParameter( 'sType' );
117 
118  $oErpImport = new oxErpGenImport();
119  $oErpImport->setImportTypePrefix( $sType );
120  $oErpImport->setCsvFileFieldsOrder( $aCsvFields );
121  $oErpImport->setCsvContainsHeader( oxSession::getVar( 'blCsvContainsHeader' ) );
122 
123  $oErpImport->doImport( $this->_getUploadedCsvFilePath() );
124  $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
125 
126  //checking if errors occured during import
127  $this->_checkImportErrors( $oErpImport );
128 
129  //deleting uploaded csv file from temp dir
130  $this->_deleteCsvFile();
131 
132  //check if repeating import - then forsing first step
133  if ( $oConfig->getParameter( 'iRepeatImport' ) ) {
134  $this->_aViewData['iRepeatImport'] = 1;
135  $sNavStep = 1;
136  }
137  }
138 
139  if ( $sNavStep == 1 ) {
140  $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
141  asort( $this->_aViewData['aImportTables'] );
142  $this->_resetUploadedCsvData();
143  }
144 
145  $this->_aViewData['sNavStep'] = $sNavStep;
146 
147  return parent::render();
148  }
149 
156  protected function _deleteCsvFile()
157  {
158  $sPath = $this->_getUploadedCsvFilePath();
159  if ( is_file( $sPath ) ) {
160  @unlink( $sPath );
161  }
162  }
163 
170  protected function _getCsvFieldsNames()
171  {
172  $blCsvContainsHeader = $this->getConfig()->getParameter( 'blContainsHeader' );
173  oxSession::setVar( 'blCsvContainsHeader', $blCsvContainsHeader );
174  $sCsvPath = $this->_getUploadedCsvFilePath();
175 
176  $aFirstRow = $this->_getCsvFirstRow();
177 
178  if ( !$blCsvContainsHeader ) {
179  $iIndex = 1;
180  foreach ( $aFirstRow as $sValue ) {
181  $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
182  }
183  } else {
184  foreach ( $aFirstRow as $sKey => $sValue ) {
185  $aFirstRow[$sKey] = htmlentities( $sValue );
186  }
187 
188  $aCsvFields = $aFirstRow;
189  }
190 
191  return $aCsvFields;
192  }
193 
199  protected function _getCsvFirstRow()
200  {
201  $sPath = $this->_getUploadedCsvFilePath();
202  $iMaxLineLength = 8192;
203 
204  //getting first row
205  if ( ( $rFile = @fopen( $sPath, "r" ) ) !== false ) {
206  $aRow = fgetcsv( $rFile, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser() );
207  fclose( $rFile );
208  }
209 
210  return $aRow;
211  }
212 
218  protected function _resetUploadedCsvData()
219  {
220  $this->_sCsvFilePath = null;
221  oxSession::setVar( 'sCsvFilePath', null );
222  oxSession::setVar( 'blCsvContainsHeader', null );
223  }
224 
233  protected function _checkErrors( $iNavStep )
234  {
235  if ( $iNavStep == 2 ) {
236  if ( !$this->_getUploadedCsvFilePath() ) {
237  $oEx = oxNew( "oxExceptionToDisplay" );
238  $oEx->setMessage( 'GENIMPORT_ERRORUPLOADINGFILE' );
239  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, 'genimport' );
240  $iNavStep = 1;
241  }
242  }
243 
244  if ( $iNavStep == 3 ) {
245  $blIsEmpty = true;
246  $aCsvFields = $this->getConfig()->getParameter( 'aCsvFields' );
247  foreach ( $aCsvFields as $sValue ) {
248  if ( $sValue ) {
249  $blIsEmpty = false;
250  break;
251  }
252  }
253 
254  if ( $blIsEmpty ) {
255  $oEx = oxNew( "oxExceptionToDisplay" );
256  $oEx->setMessage( 'GENIMPORT_ERRORASSIGNINGFIELDS' );
257  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, 'genimport' );
258  $iNavStep = 2;
259  }
260  }
261 
262  return $iNavStep;
263  }
264 
271  protected function _getUploadedCsvFilePath()
272  {
273  //try to get uploaded csv file path
274  if ( $this->_sCsvFilePath !== null ) {
275  return $this->_sCsvFilePath;
276  } elseif ( $this->_sCsvFilePath = oxSession::getVar( 'sCsvFilePath' ) ) {
277  return $this->_sCsvFilePath;
278  }
279 
280  $oConfig = $this->getConfig();
281  $aFile = $oConfig->getUploadedFile( 'csvfile' );
282  if ( isset( $aFile['name'] ) && $aFile['name'] ) {
283  $this->_sCsvFilePath = $oConfig->getConfigParam( 'sCompileDir' ) . basename( $aFile['tmp_name'] );
284  move_uploaded_file( $aFile['tmp_name'], $this->_sCsvFilePath );
285  oxSession::setVar( 'sCsvFilePath', $this->_sCsvFilePath );
286  return $this->_sCsvFilePath;
287  }
288  }
289 
297  protected function _checkImportErrors( $oErpImport )
298  {
299  foreach ( $oErpImport->getStatistics() as $aValue ) {
300  if ( !$aValue ['r'] ) {
301  $oEx = oxNew( "oxExceptionToDisplay" );
302  $oEx->setMessage( $aValue ['m'] );
303  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, true, 'genimport' );
304  }
305  }
306 
307  }
308 
314  protected function _getCsvFieldsTerminator()
315  {
316  if ( $this->_sStringTerminator === null ) {
317  $this->_sStringTerminator = $this->_sDefaultStringTerminator;
318  if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldTerminator' ) ) {
319  $this->_sStringTerminator = $sChar;
320  }
321  }
323  }
324 
330  protected function _getCsvFieldsEncolser()
331  {
332  if ( $this->_sStringEncloser === null ) {
333  $this->_sStringEncloser = $this->_sDefaultStringEncloser;
334  if ( $sChar = $this->getConfig()->getConfigParam( 'sGiCsvFieldEncloser' ) ) {
335  $this->_sStringEncloser = $sChar;
336  }
337  }
339  }
340 
344  private function setErrorToView($sInvalidData)
345  {
346  $error = oxNew('oxDisplayError');
347  $error->setFormatParameters(htmlspecialchars($sInvalidData));
348  $error->setMessage("SHOP_CONFIG_ERROR_INVALID_VALUE");
349  oxRegistry::get("oxUtilsView")->addErrorToDisplay($error);
350  }
351 }