OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
genimport_main.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
14  public $sClassDo = "genImport_do";
15 
21  public $sClassMain = "genImport_main";
22 
28  protected $_sCsvFilePath = null;
29 
35  protected $_sStringTerminator = null;
36 
42  protected $_sStringEncloser = null;
43 
49  protected $_sDefaultStringTerminator = ";";
50 
56  protected $_sDefaultStringEncloser = '"';
57 
63  protected $_sThisTemplate = "genimport_main.tpl";
64 
71  public function render()
72  {
73  $oConfig = $this->getConfig();
74 
75  $oErpImport = new oxErpGenImport();
76  $this->_sCsvFilePath = null;
77 
78  $sNavStep = $oConfig->getRequestParameter('sNavStep');
79 
80  if (!$sNavStep) {
81  $sNavStep = 1;
82  } else {
83  $sNavStep++;
84  }
85 
86 
87  $sNavStep = $this->_checkErrors($sNavStep);
88 
89  if ($sNavStep == 1) {
90  $this->_aViewData['sGiCsvFieldTerminator'] = oxStr::getStr()->htmlentities($this->_getCsvFieldsTerminator());
91  $this->_aViewData['sGiCsvFieldEncloser'] = oxStr::getStr()->htmlentities($this->_getCsvFieldsEncolser());
92  }
93 
94  if ($sNavStep == 2) {
95  //saving csv field terminator and encloser to config
96  if ($sTerminator = $oConfig->getRequestParameter('sGiCsvFieldTerminator')) {
97  $this->_sStringTerminator = $sTerminator;
98  $oConfig->saveShopConfVar('str', 'sGiCsvFieldTerminator', $sTerminator);
99  }
100 
101  if ($sEncloser = $oConfig->getRequestParameter('sGiCsvFieldEncloser')) {
102  $this->_sStringEncloser = $sEncloser;
103  $oConfig->saveShopConfVar('str', 'sGiCsvFieldEncloser', $sEncloser);
104  }
105 
106  $sType = $oConfig->getRequestParameter('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->getRequestParameter('aCsvFields');
116  $sType = $oConfig->getRequestParameter('sType');
117 
118  $oErpImport = new oxErpGenImport();
119  $oErpImport->setImportTypePrefix($sType);
120  $oErpImport->setCsvFileFieldsOrder($aCsvFields);
121  $oErpImport->setCsvContainsHeader(oxRegistry::getSession()->getVariable('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->getRequestParameter('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 
153  protected function _deleteCsvFile()
154  {
155  $sPath = $this->_getUploadedCsvFilePath();
156  if (is_file($sPath)) {
157  @unlink($sPath);
158  }
159  }
160 
167  protected function _getCsvFieldsNames()
168  {
169  $blCsvContainsHeader = $this->getConfig()->getRequestParameter('blContainsHeader');
170  oxRegistry::getSession()->setVariable('blCsvContainsHeader', $blCsvContainsHeader);
171  $sCsvPath = $this->_getUploadedCsvFilePath();
172 
173  $aFirstRow = $this->_getCsvFirstRow();
174 
175  if (!$blCsvContainsHeader) {
176  $iIndex = 1;
177  foreach ($aFirstRow as $sValue) {
178  $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
179  }
180  } else {
181  foreach ($aFirstRow as $sKey => $sValue) {
182  $aFirstRow[$sKey] = oxStr::getStr()->htmlentities($sValue);
183  }
184 
185  $aCsvFields = $aFirstRow;
186  }
187 
188  return $aCsvFields;
189  }
190 
196  protected function _getCsvFirstRow()
197  {
198  $sPath = $this->_getUploadedCsvFilePath();
199  $iMaxLineLength = 8192;
200 
201  //getting first row
202  if (($rFile = @fopen($sPath, "r")) !== false) {
203  $aRow = fgetcsv($rFile, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser());
204  fclose($rFile);
205  }
206 
207  return $aRow;
208  }
209 
213  protected function _resetUploadedCsvData()
214  {
215  $this->_sCsvFilePath = null;
216  oxRegistry::getSession()->setVariable('sCsvFilePath', null);
217  oxRegistry::getSession()->setVariable('blCsvContainsHeader', null);
218  }
219 
228  protected function _checkErrors($iNavStep)
229  {
230  if ($iNavStep == 2) {
231  if (!$this->_getUploadedCsvFilePath()) {
232  $oEx = oxNew("oxExceptionToDisplay");
233  $oEx->setMessage('GENIMPORT_ERRORUPLOADINGFILE');
234  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
235  $iNavStep = 1;
236  }
237  }
238 
239  if ($iNavStep == 3) {
240  $blIsEmpty = true;
241  $aCsvFields = $this->getConfig()->getRequestParameter('aCsvFields');
242  foreach ($aCsvFields as $sValue) {
243  if ($sValue) {
244  $blIsEmpty = false;
245  break;
246  }
247  }
248 
249  if ($blIsEmpty) {
250  $oEx = oxNew("oxExceptionToDisplay");
251  $oEx->setMessage('GENIMPORT_ERRORASSIGNINGFIELDS');
252  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
253  $iNavStep = 2;
254  }
255  }
256 
257  return $iNavStep;
258  }
259 
266  protected function _getUploadedCsvFilePath()
267  {
268  //try to get uploaded csv file path
269  if ($this->_sCsvFilePath !== null) {
270  return $this->_sCsvFilePath;
271  } elseif ($this->_sCsvFilePath = oxRegistry::getSession()->getVariable('sCsvFilePath')) {
272  return $this->_sCsvFilePath;
273  }
274 
275  $oConfig = $this->getConfig();
276  $aFile = $oConfig->getUploadedFile('csvfile');
277  if (isset($aFile['name']) && $aFile['name']) {
278  $this->_sCsvFilePath = $oConfig->getConfigParam('sCompileDir') . basename($aFile['tmp_name']);
279  move_uploaded_file($aFile['tmp_name'], $this->_sCsvFilePath);
280  oxRegistry::getSession()->setVariable('sCsvFilePath', $this->_sCsvFilePath);
281 
282  return $this->_sCsvFilePath;
283  }
284  }
285 
291  protected function _checkImportErrors($oErpImport)
292  {
293  foreach ($oErpImport->getStatistics() as $aValue) {
294  if (!$aValue ['r']) {
295  $oEx = oxNew("oxExceptionToDisplay");
296  $oEx->setMessage($aValue ['m']);
297  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
298  }
299  }
300 
301  }
302 
308  protected function _getCsvFieldsTerminator()
309  {
310  if ($this->_sStringTerminator === null) {
311  $this->_sStringTerminator = $this->_sDefaultStringTerminator;
312  if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldTerminator')) {
313  $this->_sStringTerminator = $sChar;
314  }
315  }
316 
318  }
319 
325  protected function _getCsvFieldsEncolser()
326  {
327  if ($this->_sStringEncloser === null) {
328  $this->_sStringEncloser = $this->_sDefaultStringEncloser;
329  if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldEncloser')) {
330  $this->_sStringEncloser = $sChar;
331  }
332  }
333 
335  }
336 }