OXID eShop CE  4.10.8
 All Classes Namespaces Files Functions Variables Pages
oxerpgenimport.php
Go to the documentation of this file.
1 <?php
2 
8 class oxErpGenImport extends oxErpCsv
9 {
10 
16  protected $_aObjects = array(
17  'A' => 'article',
18  'K' => 'category',
19  'H' => 'vendor',
20  'C' => 'crossselling',
21  'Z' => 'accessoire',
22  'T' => 'article2category',
23  'I' => 'article2action',
24  'P' => 'scaleprice',
25  'U' => 'user',
26  'O' => 'order',
27  'R' => 'orderarticle',
28  'N' => 'country',
29  'Y' => 'artextends',
30  );
36  protected $_aCsvFileFieldsOrder = array();
37 
43  protected $_blCsvContainsHeader = null;
44 
50  protected $_sDefaultStringTerminator = ";";
51 
57  protected $_sDefaultStringEncloser = '"';
58 
70  public function __call($sMethod, $aArgs)
71  {
72  if (defined('OXID_PHP_UNIT')) {
73  if (substr($sMethod, 0, 4) == "UNIT") {
74  $sMethod = str_replace("UNIT", "_", $sMethod);
75  }
76  if (method_exists($this, $sMethod)) {
77  return call_user_func_array(array(& $this, $sMethod), $aArgs);
78  }
79  }
80 
81  throw new oxSystemComponentException("Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")" . PHP_EOL);
82  }
83 
89  public function __construct()
90  {
91  $this->_setDbLayerVersion();
92  }
93 
103  public function getInstanceOfType($sType)
104  {
105  return parent::_getInstanceOfType($sType);
106  }
107 
111  protected function _setDbLayerVersion()
112  {
113  $aVersions = array_keys(oxErpBase::$_aDbLayer2ShopDbVersions);
114  $sVersion = array_pop($aVersions);
115  oxErpBase::setVersion($sVersion);
116  }
117 
127  protected function _modifyData($aData, $oType)
128  {
129  return $this->_mapFields($aData, $oType);
130  }
131 
140  protected function _mapFields($aData, $oType)
141  {
142  $aRet = array();
143  $iIndex = 0;
144 
145  foreach ($this->_aCsvFileFieldsOrder as $sValue) {
146  if (!empty($sValue)) {
147  if (strtolower($aData[$iIndex]) == "null") {
148  $aRet[$sValue] = null;
149  } else {
150  $aRet[$sValue] = $aData[$iIndex];
151  }
152  }
153  $iIndex++;
154  }
155 
156  return $aRet;
157  }
158 
168  protected function _getImportType(& $aData)
169  {
170  $sType = $this->_sImportTypePrefix;
171 
172  if (strlen($sType) != 1 || !array_key_exists($sType, $this->_aObjects)) {
173  throw new Exception("Error unknown command: " . $sType);
174  } else {
175  return $this->_aObjects[$sType];
176  }
177  }
178 
186  protected function _getImportMode($aData)
187  {
189  }
190 
198  public function getImportObject($sType)
199  {
200  $this->_sImportTypePrefix = $sType;
201  try {
202  $sImportType = $this->_getImportType($this->_aData);
203 
204  return $this->_getInstanceOfType($sImportType);
205  } catch (Exception $e) {
206  }
207  }
208 
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 
231  return $aList;
232  }
233 
245  public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
246  {
248  $mySession = oxRegistry::getSession();
249  $oUser = oxNew('oxUser');
250  $oUser->loadAdminUser();
251 
252  if (($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID())) {
253  $this->_sSID = $mySession->getId();
254  $this->_blInit = true;
255  $this->_iLanguage = oxRegistry::getLang()->getBaseLanguage();
256  $this->_sUserID = $oUser->getId();
257  //$mySession->freeze();
258  } else {
259 
260  //user does not have sufficient rights for shop
261  throw new Exception(self::ERROR_USER_NO_RIGHTS);
262  }
263 
264  $this->_resetIdx();
265 
266  return $this->_blInit;
267  }
268 
274  public function setCsvFileFieldsOrder($aCsvFields)
275  {
276  $this->_aCsvFileFieldsOrder = $aCsvFields;
277  }
278 
284  public function setCsvContainsHeader($blCsvContainsHeader)
285  {
286  $this->_blCsvContainsHeader = $blCsvContainsHeader;
287  }
288 
294  public function getTotalImportedRowsNumber()
295  {
296  return $this->getImportedRowCount();
297  }
298 
311  public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null)
312  {
314  $mySession = oxRegistry::getSession();
315 
316  $this->_sReturn = "";
317  $iMaxLineLength = 8192; //TODO change
318 
319  $this->_sPath = $sPath;
320 
321  //init with given data
322  try {
323  $this->init(null, null);
324  } catch (Exception $ex) {
325  return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
326  }
327 
328  $file = @fopen($this->_sPath, "r");
329 
330  if (isset($file) && $file) {
331  $iRow = 0;
332  $aRow = array();
333 
334  while (($aRow = fgetcsv($file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser())) !== false) {
335 
336  $this->_aData[] = $aRow;
337  }
338 
339  if ($this->_blCsvContainsHeader) {
340  //skipping first row - it's header
341  array_shift($this->_aData);
342  }
343 
344  try {
345  $this->Import();
346  } catch (Exception $ex) {
347  echo $ex->getMessage();
348  $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
349  }
350 
351  } else {
352  $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
353  }
354 
355  @fclose($file);
356 
357  return $this->_sReturn;
358  }
359 
365  protected function _getCsvFieldsTerminator()
366  {
368 
369  $sChar = $myConfig->getConfigParam('sGiCsvFieldTerminator');
370 
371  if (!$sChar) {
372  $sChar = $myConfig->getConfigParam('sCSVSign');
373  }
374  if (!$sChar) {
376  }
377 
378  return $sChar;
379  }
380 
386  protected function _getCsvFieldsEncolser()
387  {
389 
390  if ($sChar = $myConfig->getConfigParam('sGiCsvFieldEncloser')) {
391  return $sChar;
392  } else {
394  }
395  }
396 }