OXID eShop CE  4.9.10
 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  $configValidator = oxNew('oxNoJsValidator');
96  //saving csv field terminator and encloser to config
97  $sTerminator = $oConfig->getRequestParameter('sGiCsvFieldTerminator');
98  if ($sTerminator && !$configValidator->isValid($sTerminator)) {
99  $this->setErrorToView($sTerminator);
100  } else {
101  $this->_sStringTerminator = $sTerminator;
102  $oConfig->saveShopConfVar('str', 'sGiCsvFieldTerminator', $sTerminator);
103  }
104 
105  $sEncloser = $oConfig->getRequestParameter('sGiCsvFieldEncloser');
106  if ($sEncloser && !$configValidator->isValid($sEncloser)) {
107  $this->setErrorToView($sEncloser);
108  } else {
109  $this->_sStringEncloser = $sEncloser;
110  $oConfig->saveShopConfVar('str', 'sGiCsvFieldEncloser', $sEncloser);
111  }
112 
113  $sType = $oConfig->getRequestParameter('sType');
114  $oType = $oErpImport->getImportObject($sType);
115  $this->_aViewData['sType'] = $sType;
116  $this->_aViewData['sImportTable'] = $oType->getBaseTableName();
117  $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
118  $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
119  }
120 
121  if ($sNavStep == 3) {
122  $aCsvFields = $oConfig->getRequestParameter('aCsvFields');
123  $sType = $oConfig->getRequestParameter('sType');
124 
125  $oErpImport = new oxErpGenImport();
126  $oErpImport->setImportTypePrefix($sType);
127  $oErpImport->setCsvFileFieldsOrder($aCsvFields);
128  $oErpImport->setCsvContainsHeader(oxRegistry::getSession()->getVariable('blCsvContainsHeader'));
129 
130  $oErpImport->doImport($this->_getUploadedCsvFilePath());
131  $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
132 
133  //checking if errors occured during import
134  $this->_checkImportErrors($oErpImport);
135 
136  //deleting uploaded csv file from temp dir
137  $this->_deleteCsvFile();
138 
139  //check if repeating import - then forsing first step
140  if ($oConfig->getRequestParameter('iRepeatImport')) {
141  $this->_aViewData['iRepeatImport'] = 1;
142  $sNavStep = 1;
143  }
144  }
145 
146  if ($sNavStep == 1) {
147  $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
148  asort($this->_aViewData['aImportTables']);
149  $this->_resetUploadedCsvData();
150  }
151 
152  $this->_aViewData['sNavStep'] = $sNavStep;
153 
154  return parent::render();
155  }
156 
160  protected function _deleteCsvFile()
161  {
162  $sPath = $this->_getUploadedCsvFilePath();
163  if (is_file($sPath)) {
164  @unlink($sPath);
165  }
166  }
167 
174  protected function _getCsvFieldsNames()
175  {
176  $blCsvContainsHeader = $this->getConfig()->getRequestParameter('blContainsHeader');
177  oxRegistry::getSession()->setVariable('blCsvContainsHeader', $blCsvContainsHeader);
178  $sCsvPath = $this->_getUploadedCsvFilePath();
179 
180  $aFirstRow = $this->_getCsvFirstRow();
181 
182  if (!$blCsvContainsHeader) {
183  $iIndex = 1;
184  foreach ($aFirstRow as $sValue) {
185  $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
186  }
187  } else {
188  foreach ($aFirstRow as $sKey => $sValue) {
189  $aFirstRow[$sKey] = oxStr::getStr()->htmlentities($sValue);
190  }
191 
192  $aCsvFields = $aFirstRow;
193  }
194 
195  return $aCsvFields;
196  }
197 
203  protected function _getCsvFirstRow()
204  {
205  $sPath = $this->_getUploadedCsvFilePath();
206  $iMaxLineLength = 8192;
207 
208  //getting first row
209  if (($rFile = @fopen($sPath, "r")) !== false) {
210  $aRow = fgetcsv($rFile, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser());
211  fclose($rFile);
212  }
213 
214  return $aRow;
215  }
216 
220  protected function _resetUploadedCsvData()
221  {
222  $this->_sCsvFilePath = null;
223  oxRegistry::getSession()->setVariable('sCsvFilePath', null);
224  oxRegistry::getSession()->setVariable('blCsvContainsHeader', null);
225  }
226 
235  protected function _checkErrors($iNavStep)
236  {
237  if ($iNavStep == 2) {
238  if (!$this->_getUploadedCsvFilePath()) {
239  $oEx = oxNew("oxExceptionToDisplay");
240  $oEx->setMessage('GENIMPORT_ERRORUPLOADINGFILE');
241  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
242  $iNavStep = 1;
243  }
244  }
245 
246  if ($iNavStep == 3) {
247  $blIsEmpty = true;
248  $aCsvFields = $this->getConfig()->getRequestParameter('aCsvFields');
249  foreach ($aCsvFields as $sValue) {
250  if ($sValue) {
251  $blIsEmpty = false;
252  break;
253  }
254  }
255 
256  if ($blIsEmpty) {
257  $oEx = oxNew("oxExceptionToDisplay");
258  $oEx->setMessage('GENIMPORT_ERRORASSIGNINGFIELDS');
259  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
260  $iNavStep = 2;
261  }
262  }
263 
264  return $iNavStep;
265  }
266 
273  protected function _getUploadedCsvFilePath()
274  {
275  //try to get uploaded csv file path
276  if ($this->_sCsvFilePath !== null) {
277  return $this->_sCsvFilePath;
278  } elseif ($this->_sCsvFilePath = oxRegistry::getSession()->getVariable('sCsvFilePath')) {
279  return $this->_sCsvFilePath;
280  }
281 
282  $oConfig = $this->getConfig();
283  $aFile = $oConfig->getUploadedFile('csvfile');
284  if (isset($aFile['name']) && $aFile['name']) {
285  $this->_sCsvFilePath = $oConfig->getConfigParam('sCompileDir') . basename($aFile['tmp_name']);
286  move_uploaded_file($aFile['tmp_name'], $this->_sCsvFilePath);
287  oxRegistry::getSession()->setVariable('sCsvFilePath', $this->_sCsvFilePath);
288 
289  return $this->_sCsvFilePath;
290  }
291  }
292 
298  protected function _checkImportErrors($oErpImport)
299  {
300  foreach ($oErpImport->getStatistics() as $aValue) {
301  if (!$aValue ['r']) {
302  $oEx = oxNew("oxExceptionToDisplay");
303  $oEx->setMessage($aValue ['m']);
304  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
305  }
306  }
307 
308  }
309 
315  protected function _getCsvFieldsTerminator()
316  {
317  if ($this->_sStringTerminator === null) {
318  $this->_sStringTerminator = $this->_sDefaultStringTerminator;
319  if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldTerminator')) {
320  $this->_sStringTerminator = $sChar;
321  }
322  }
323 
325  }
326 
332  protected function _getCsvFieldsEncolser()
333  {
334  if ($this->_sStringEncloser === null) {
335  $this->_sStringEncloser = $this->_sDefaultStringEncloser;
336  if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldEncloser')) {
337  $this->_sStringEncloser = $sChar;
338  }
339  }
340 
342  }
343 
347  private function setErrorToView($sInvalidData)
348  {
349  $error = oxNew('oxDisplayError');
350  $error->setFormatParameters(htmlspecialchars($sInvalidData));
351  $error->setMessage("SHOP_CONFIG_ERROR_INVALID_VALUE");
352  oxRegistry::get("oxUtilsView")->addErrorToDisplay($error);
353  }
354 }