OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxerpgenimport.php
Go to the documentation of this file.
1 <?php
2 
7 class oxErpGenImport extends oxErpCsv
8 {
9 
15  protected $_aObjects = array (
16  'A' => 'article',
17  'K' => 'category',
18  'H' => 'vendor',
19  'C' => 'crossselling',
20  'Z' => 'accessoire',
21  'T' => 'article2category',
22  'I' => 'article2action',
23  'P' => 'scaleprice',
24  'U' => 'user',
25  'O' => 'order',
26  'R' => 'orderarticle',
27  'N' => 'country',
28  'Y' => 'artextends',
29  );
35  protected $_aCsvFileFieldsOrder = array();
36 
42  protected $_blCsvContainsHeader = null;
43 
48  protected $_sDefaultStringTerminator = ";";
49 
54  protected $_sDefaultStringEncloser = '"';
55 
67  public function __call( $sMethod, $aArgs )
68  {
69  if ( defined( 'OXID_PHP_UNIT' ) ) {
70  if ( substr( $sMethod, 0, 4) == "UNIT" ) {
71  $sMethod = str_replace( "UNIT", "_", $sMethod );
72  }
73  if ( method_exists( $this, $sMethod)) {
74  return call_user_func_array( array( & $this, $sMethod ), $aArgs );
75  }
76  }
77 
78  throw new oxSystemComponentException( "Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")".PHP_EOL);
79  }
80 
86  public function __construct()
87  {
88  $this->_setDbLayerVersion();
89  }
90 
100  public function getInstanceOfType( $sType )
101  {
102  return parent::_getInstanceOfType( $sType );
103  }
104 
110  protected function _setDbLayerVersion()
111  {
112  $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
113  $sVersion = array_pop( $aVersions) ;
114  oxErpBase::setVersion( $sVersion );
115  }
116 
126  protected function _modifyData($aData, $oType)
127  {
128  return $this->_mapFields($aData, $oType);
129  }
130 
139  protected function _mapFields($aData, $oType)
140  {
141  $aRet = array();
142  $iIndex = 0;
143 
144  foreach ( $this->_aCsvFileFieldsOrder as $sValue ) {
145  if ( !empty($sValue) ) {
146  if ( strtolower( $aData[$iIndex] ) == "null" ) {
147  $aRet[$sValue] = null;
148  } else {
149  $aRet[$sValue] = $aData[$iIndex];
150  }
151  }
152  $iIndex++;
153  }
154 
155  return $aRet;
156  }
157 
167  protected function _getImportType( & $aData )
168  {
169  $sType = $this->_sImportTypePrefix;
170 
171  if ( strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects) ) {
172  throw new Exception("Error unknown command: ".$sType);
173  } else {
174  return $this->_aObjects[$sType];
175  }
176  }
177 
185  protected function _getImportMode( $aData )
186  {
188  }
189 
197  public function getImportObject( $sType )
198  {
199  $this->_sImportTypePrefix = $sType;
200  try {
201  $sImportType = $this->_getImportType( $this->_aData );
202  return $this->_getInstanceOfType( $sImportType );
203  } catch( Exception $e) {
204  }
205  }
206 
214  public function setImportTypePrefix( $sType )
215  {
216  $this->_sImportTypePrefix = $sType;
217  }
218 
224  public function getImportObjectsList()
225  {
226  foreach ( $this->_aObjects as $sKey => $sImportType ) {
227  $oType = $this->_getInstanceOfType( $sImportType );
228  $aList[$sKey] = $oType->getBaseTableName();
229  }
230  return $aList;
231  }
232 
244  public function init( $sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
245  {
247  $mySession = oxRegistry::getSession();
248  $oUser = oxNew('oxUser');
249  $oUser->loadAdminUser();
250 
251  if ( ( $oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
252  $this->_sSID = $mySession->getId();
253  $this->_blInit = true;
254  $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
255  $this->_sUserID = $oUser->getId();
256  //$mySession->freeze();
257  } else {
258 
259  //user does not have sufficient rights for shop
260  throw new Exception( self::ERROR_USER_NO_RIGHTS);
261  }
262 
263  $this->_resetIdx();
264 
265  return $this->_blInit;
266  }
267 
275  public function setCsvFileFieldsOrder( $aCsvFields )
276  {
277  $this->_aCsvFileFieldsOrder = $aCsvFields;
278  }
279 
287  public function setCsvContainsHeader( $blCsvContainsHeader )
288  {
289  $this->_blCsvContainsHeader = $blCsvContainsHeader;
290  }
291 
297  public function getTotalImportedRowsNumber()
298  {
299  return $this->getImportedRowCount();
300  }
301 
314  public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null )
315  {
317  $mySession = oxRegistry::getSession();
318 
319  $this->_sReturn = "";
320  $iMaxLineLength = 8192; //TODO change
321 
322  $this->_sPath = $sPath;
323 
324  //init with given data
325  try {
326  $this->init(null, null);
327  }catch(Exception $ex){
328  return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
329  }
330 
331  $file = @fopen($this->_sPath, "r");
332 
333  if ( isset($file) && $file ) {
334  $iRow = 0;
335  $aRow = array();
336 
337  while ( ($aRow = fgetcsv( $file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser()) ) !== false ) {
338 
339  $this->_aData[] = $aRow;
340  }
341 
342  if ( $this->_blCsvContainsHeader ) {
343  //skipping first row - it's header
344  array_shift( $this->_aData );
345  }
346 
347  try {
348  $this->Import();
349  } catch (Exception $ex) {
350  echo $ex->getMessage();
351  $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
352  }
353 
354  } else {
355  $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
356  }
357 
358  @fclose($file);
359 
360  return $this->_sReturn;
361  }
362 
368  protected function _getCsvFieldsTerminator()
369  {
371 
372  $sChar = $myConfig->getConfigParam( 'sGiCsvFieldTerminator' );
373 
374  if ( !$sChar ) {
375  $sChar = $myConfig->getConfigParam( 'sCSVSign' );
376  }
377  if ( !$sChar ) {
379  }
380  return $sChar;
381  }
382 
388  protected function _getCsvFieldsEncolser()
389  {
391 
392  if ( $sChar = $myConfig->getConfigParam( 'sGiCsvFieldEncloser' ) ) {
393  return $sChar;
394  } else {
396  }
397  }
398 }