Go to the documentation of this file.00001 <?php
00002
00006 class GenImport_Main extends oxAdminDetails
00007 {
00008
00014 public $sClassDo = "genImport_do";
00015
00021 public $sClassMain = "genImport_main";
00022
00028 protected $_sCsvFilePath = null;
00029
00035 protected $_sStringTerminator = null;
00036
00042 protected $_sStringEncloser = null;
00043
00049 protected $_sDefaultStringTerminator = ";";
00050
00056 protected $_sDefaultStringEncloser = '"';
00057
00063 protected $_sThisTemplate = "genimport_main.tpl";
00064
00071 public function render()
00072 {
00073 $oConfig = $this->getConfig();
00074
00075 $oErpImport = new oxErpGenImport();
00076 $this->_sCsvFilePath = null;
00077
00078 $sNavStep = $oConfig->getRequestParameter('sNavStep');
00079
00080 if (!$sNavStep) {
00081 $sNavStep = 1;
00082 } else {
00083 $sNavStep++;
00084 }
00085
00086
00087 $sNavStep = $this->_checkErrors($sNavStep);
00088
00089 if ($sNavStep == 1) {
00090 $this->_aViewData['sGiCsvFieldTerminator'] = oxStr::getStr()->htmlentities($this->_getCsvFieldsTerminator());
00091 $this->_aViewData['sGiCsvFieldEncloser'] = oxStr::getStr()->htmlentities($this->_getCsvFieldsEncolser());
00092 }
00093
00094 if ($sNavStep == 2) {
00095
00096 if ($sTerminator = $oConfig->getRequestParameter('sGiCsvFieldTerminator')) {
00097 $this->_sStringTerminator = $sTerminator;
00098 $oConfig->saveShopConfVar('str', 'sGiCsvFieldTerminator', $sTerminator);
00099 }
00100
00101 if ($sEncloser = $oConfig->getRequestParameter('sGiCsvFieldEncloser')) {
00102 $this->_sStringEncloser = $sEncloser;
00103 $oConfig->saveShopConfVar('str', 'sGiCsvFieldEncloser', $sEncloser);
00104 }
00105
00106 $sType = $oConfig->getRequestParameter('sType');
00107 $oType = $oErpImport->getImportObject($sType);
00108 $this->_aViewData['sType'] = $sType;
00109 $this->_aViewData['sImportTable'] = $oType->getBaseTableName();
00110 $this->_aViewData['aCsvFieldsList'] = $this->_getCsvFieldsNames();
00111 $this->_aViewData['aDbFieldsList'] = $oType->getFieldList();
00112 }
00113
00114 if ($sNavStep == 3) {
00115 $aCsvFields = $oConfig->getRequestParameter('aCsvFields');
00116 $sType = $oConfig->getRequestParameter('sType');
00117
00118 $oErpImport = new oxErpGenImport();
00119 $oErpImport->setImportTypePrefix($sType);
00120 $oErpImport->setCsvFileFieldsOrder($aCsvFields);
00121 $oErpImport->setCsvContainsHeader(oxRegistry::getSession()->getVariable('blCsvContainsHeader'));
00122
00123 $oErpImport->doImport($this->_getUploadedCsvFilePath());
00124 $this->_aViewData['iTotalRows'] = $oErpImport->getTotalImportedRowsNumber();
00125
00126
00127 $this->_checkImportErrors($oErpImport);
00128
00129
00130 $this->_deleteCsvFile();
00131
00132
00133 if ($oConfig->getRequestParameter('iRepeatImport')) {
00134 $this->_aViewData['iRepeatImport'] = 1;
00135 $sNavStep = 1;
00136 }
00137 }
00138
00139 if ($sNavStep == 1) {
00140 $this->_aViewData['aImportTables'] = $oErpImport->getImportObjectsList();
00141 asort($this->_aViewData['aImportTables']);
00142 $this->_resetUploadedCsvData();
00143 }
00144
00145 $this->_aViewData['sNavStep'] = $sNavStep;
00146
00147 return parent::render();
00148 }
00149
00153 protected function _deleteCsvFile()
00154 {
00155 $sPath = $this->_getUploadedCsvFilePath();
00156 if (is_file($sPath)) {
00157 @unlink($sPath);
00158 }
00159 }
00160
00167 protected function _getCsvFieldsNames()
00168 {
00169 $blCsvContainsHeader = $this->getConfig()->getRequestParameter('blContainsHeader');
00170 oxRegistry::getSession()->setVariable('blCsvContainsHeader', $blCsvContainsHeader);
00171 $sCsvPath = $this->_getUploadedCsvFilePath();
00172
00173 $aFirstRow = $this->_getCsvFirstRow();
00174
00175 if (!$blCsvContainsHeader) {
00176 $iIndex = 1;
00177 foreach ($aFirstRow as $sValue) {
00178 $aCsvFields[$iIndex] = 'Column ' . $iIndex++;
00179 }
00180 } else {
00181 foreach ($aFirstRow as $sKey => $sValue) {
00182 $aFirstRow[$sKey] = oxStr::getStr()->htmlentities($sValue);
00183 }
00184
00185 $aCsvFields = $aFirstRow;
00186 }
00187
00188 return $aCsvFields;
00189 }
00190
00196 protected function _getCsvFirstRow()
00197 {
00198 $sPath = $this->_getUploadedCsvFilePath();
00199 $iMaxLineLength = 8192;
00200
00201
00202 if (($rFile = @fopen($sPath, "r")) !== false) {
00203 $aRow = fgetcsv($rFile, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser());
00204 fclose($rFile);
00205 }
00206
00207 return $aRow;
00208 }
00209
00213 protected function _resetUploadedCsvData()
00214 {
00215 $this->_sCsvFilePath = null;
00216 oxRegistry::getSession()->setVariable('sCsvFilePath', null);
00217 oxRegistry::getSession()->setVariable('blCsvContainsHeader', null);
00218 }
00219
00228 protected function _checkErrors($iNavStep)
00229 {
00230 if ($iNavStep == 2) {
00231 if (!$this->_getUploadedCsvFilePath()) {
00232 $oEx = oxNew("oxExceptionToDisplay");
00233 $oEx->setMessage('GENIMPORT_ERRORUPLOADINGFILE');
00234 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
00235 $iNavStep = 1;
00236 }
00237 }
00238
00239 if ($iNavStep == 3) {
00240 $blIsEmpty = true;
00241 $aCsvFields = $this->getConfig()->getRequestParameter('aCsvFields');
00242 foreach ($aCsvFields as $sValue) {
00243 if ($sValue) {
00244 $blIsEmpty = false;
00245 break;
00246 }
00247 }
00248
00249 if ($blIsEmpty) {
00250 $oEx = oxNew("oxExceptionToDisplay");
00251 $oEx->setMessage('GENIMPORT_ERRORASSIGNINGFIELDS');
00252 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
00253 $iNavStep = 2;
00254 }
00255 }
00256
00257 return $iNavStep;
00258 }
00259
00266 protected function _getUploadedCsvFilePath()
00267 {
00268
00269 if ($this->_sCsvFilePath !== null) {
00270 return $this->_sCsvFilePath;
00271 } elseif ($this->_sCsvFilePath = oxRegistry::getSession()->getVariable('sCsvFilePath')) {
00272 return $this->_sCsvFilePath;
00273 }
00274
00275 $oConfig = $this->getConfig();
00276 $aFile = $oConfig->getUploadedFile('csvfile');
00277 if (isset($aFile['name']) && $aFile['name']) {
00278 $this->_sCsvFilePath = $oConfig->getConfigParam('sCompileDir') . basename($aFile['tmp_name']);
00279 move_uploaded_file($aFile['tmp_name'], $this->_sCsvFilePath);
00280 oxRegistry::getSession()->setVariable('sCsvFilePath', $this->_sCsvFilePath);
00281
00282 return $this->_sCsvFilePath;
00283 }
00284 }
00285
00291 protected function _checkImportErrors($oErpImport)
00292 {
00293 foreach ($oErpImport->getStatistics() as $aValue) {
00294 if (!$aValue ['r']) {
00295 $oEx = oxNew("oxExceptionToDisplay");
00296 $oEx->setMessage($aValue ['m']);
00297 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, true, 'genimport');
00298 }
00299 }
00300
00301 }
00302
00308 protected function _getCsvFieldsTerminator()
00309 {
00310 if ($this->_sStringTerminator === null) {
00311 $this->_sStringTerminator = $this->_sDefaultStringTerminator;
00312 if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldTerminator')) {
00313 $this->_sStringTerminator = $sChar;
00314 }
00315 }
00316
00317 return $this->_sStringTerminator;
00318 }
00319
00325 protected function _getCsvFieldsEncolser()
00326 {
00327 if ($this->_sStringEncloser === null) {
00328 $this->_sStringEncloser = $this->_sDefaultStringEncloser;
00329 if ($sChar = $this->getConfig()->getConfigParam('sGiCsvFieldEncloser')) {
00330 $this->_sStringEncloser = $sChar;
00331 }
00332 }
00333
00334 return $this->_sStringEncloser;
00335 }
00336 }