oxerpgenimport.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxErpGenImport extends oxErpCsv
00008 {
00009 
00015     protected $_aObjects = array(
00016         'A' => 'article',
00017         'K' => 'category',
00018         'H' => 'vendor',
00019         'C' => 'crossselling',
00020         'Z' => 'accessoire',
00021         'T' => 'article2category',
00022         'I' => 'article2action',
00023         'P' => 'scaleprice',
00024         'U' => 'user',
00025         'O' => 'order',
00026         'R' => 'orderarticle',
00027         'N' => 'country',
00028         'Y' => 'artextends',
00029     );
00035     protected $_aCsvFileFieldsOrder = array();
00036 
00042     protected $_blCsvContainsHeader = null;
00043 
00049     protected $_sDefaultStringTerminator = ";";
00050 
00056     protected $_sDefaultStringEncloser = '"';
00057 
00069     public function __call($sMethod, $aArgs)
00070     {
00071         if (defined('OXID_PHP_UNIT')) {
00072             if (substr($sMethod, 0, 4) == "UNIT") {
00073                 $sMethod = str_replace("UNIT", "_", $sMethod);
00074             }
00075             if (method_exists($this, $sMethod)) {
00076                 return call_user_func_array(array(& $this, $sMethod), $aArgs);
00077             }
00078         }
00079 
00080         throw new oxSystemComponentException("Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")" . PHP_EOL);
00081     }
00082 
00088     public function __construct()
00089     {
00090         $this->_setDbLayerVersion();
00091     }
00092 
00102     public function getInstanceOfType($sType)
00103     {
00104         return parent::_getInstanceOfType($sType);
00105     }
00106 
00110     protected function _setDbLayerVersion()
00111     {
00112         $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
00113         $sVersion = array_pop($aVersions);
00114         oxErpBase::setVersion($sVersion);
00115     }
00116 
00126     protected function _modifyData($aData, $oType)
00127     {
00128         return $this->_mapFields($aData, $oType);
00129     }
00130 
00139     protected function _mapFields($aData, $oType)
00140     {
00141         $aRet = array();
00142         $iIndex = 0;
00143 
00144         foreach ($this->_aCsvFileFieldsOrder as $sValue) {
00145             if (!empty($sValue)) {
00146                 if (strtolower($aData[$iIndex]) == "null") {
00147                     $aRet[$sValue] = null;
00148                 } else {
00149                     $aRet[$sValue] = $aData[$iIndex];
00150                 }
00151             }
00152             $iIndex++;
00153         }
00154 
00155         return $aRet;
00156     }
00157 
00167     protected function _getImportType(& $aData)
00168     {
00169         $sType = $this->_sImportTypePrefix;
00170 
00171         if (strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects)) {
00172             throw new Exception("Error unknown command: " . $sType);
00173         } else {
00174             return $this->_aObjects[$sType];
00175         }
00176     }
00177 
00185     protected function _getImportMode($aData)
00186     {
00187         return oxERPBase::$MODE_IMPORT;
00188     }
00189 
00197     public function getImportObject($sType)
00198     {
00199         $this->_sImportTypePrefix = $sType;
00200         try {
00201             $sImportType = $this->_getImportType($this->_aData);
00202 
00203             return $this->_getInstanceOfType($sImportType);
00204         } catch (Exception $e) {
00205         }
00206     }
00207 
00213     public function setImportTypePrefix($sType)
00214     {
00215         $this->_sImportTypePrefix = $sType;
00216     }
00217 
00223     public function getImportObjectsList()
00224     {
00225         foreach ($this->_aObjects as $sKey => $sImportType) {
00226             $oType = $this->_getInstanceOfType($sImportType);
00227             $aList[$sKey] = $oType->getBaseTableName();
00228         }
00229 
00230         return $aList;
00231     }
00232 
00244     public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
00245     {
00246         $myConfig = oxRegistry::getConfig();
00247         $mySession = oxRegistry::getSession();
00248         $oUser = oxNew('oxUser');
00249         $oUser->loadAdminUser();
00250 
00251         if (($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID())) {
00252             $this->_sSID = $mySession->getId();
00253             $this->_blInit = true;
00254             $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
00255             $this->_sUserID = $oUser->getId();
00256             //$mySession->freeze();
00257         } else {
00258 
00259             //user does not have sufficient rights for shop
00260             throw new Exception(self::ERROR_USER_NO_RIGHTS);
00261         }
00262 
00263         $this->_resetIdx();
00264 
00265         return $this->_blInit;
00266     }
00267 
00273     public function setCsvFileFieldsOrder($aCsvFields)
00274     {
00275         $this->_aCsvFileFieldsOrder = $aCsvFields;
00276     }
00277 
00283     public function setCsvContainsHeader($blCsvContainsHeader)
00284     {
00285         $this->_blCsvContainsHeader = $blCsvContainsHeader;
00286     }
00287 
00293     public function getTotalImportedRowsNumber()
00294     {
00295         return $this->getImportedRowCount();
00296     }
00297 
00310     public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null)
00311     {
00312         $myConfig = oxRegistry::getConfig();
00313         $mySession = oxRegistry::getSession();
00314 
00315         $this->_sReturn = "";
00316         $iMaxLineLength = 8192; //TODO change
00317 
00318         $this->_sPath = $sPath;
00319 
00320         //init with given data
00321         try {
00322             $this->init(null, null);
00323         } catch (Exception $ex) {
00324             return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
00325         }
00326 
00327         $file = @fopen($this->_sPath, "r");
00328 
00329         if (isset($file) && $file) {
00330             $iRow = 0;
00331             $aRow = array();
00332 
00333             while (($aRow = fgetcsv($file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser())) !== false) {
00334 
00335                 $this->_aData[] = $aRow;
00336             }
00337 
00338             if ($this->_blCsvContainsHeader) {
00339                 //skipping first row - it's header
00340                 array_shift($this->_aData);
00341             }
00342 
00343             try {
00344                 $this->Import();
00345             } catch (Exception $ex) {
00346                 echo $ex->getMessage();
00347                 $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
00348             }
00349 
00350         } else {
00351             $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
00352         }
00353 
00354         @fclose($file);
00355 
00356         return $this->_sReturn;
00357     }
00358 
00364     protected function _getCsvFieldsTerminator()
00365     {
00366         $myConfig = oxRegistry::getConfig();
00367 
00368         $sChar = $myConfig->getConfigParam('sGiCsvFieldTerminator');
00369 
00370         if (!$sChar) {
00371             $sChar = $myConfig->getConfigParam('sCSVSign');
00372         }
00373         if (!$sChar) {
00374             $sChar = $this->_sDefaultStringTerminator;
00375         }
00376 
00377         return $sChar;
00378     }
00379 
00385     protected function _getCsvFieldsEncolser()
00386     {
00387         $myConfig = oxRegistry::getConfig();
00388 
00389         if ($sChar = $myConfig->getConfigParam('sGiCsvFieldEncloser')) {
00390             return $sChar;
00391         } else {
00392             return $this->_sDefaultStringEncloser;
00393         }
00394     }
00395 }