OXID eShop CE  4.10.4
 All Classes Namespaces 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 
49  protected $_sDefaultStringTerminator = ";";
50 
56  protected $_sDefaultStringEncloser = '"';
57 
69  public function __call($sMethod, $aArgs)
70  {
71  if (defined('OXID_PHP_UNIT')) {
72  if (substr($sMethod, 0, 4) == "UNIT") {
73  $sMethod = str_replace("UNIT", "_", $sMethod);
74  }
75  if (method_exists($this, $sMethod)) {
76  return call_user_func_array(array(& $this, $sMethod), $aArgs);
77  }
78  }
79 
80  throw new oxSystemComponentException("Function '$sMethod' does not exist or is not accessible! (" . get_class($this) . ")" . PHP_EOL);
81  }
82 
88  public function __construct()
89  {
90  $this->_setDbLayerVersion();
91  }
92 
102  public function getInstanceOfType($sType)
103  {
104  return parent::_getInstanceOfType($sType);
105  }
106 
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 
203  return $this->_getInstanceOfType($sImportType);
204  } catch (Exception $e) {
205  }
206  }
207 
213  public function setImportTypePrefix($sType)
214  {
215  $this->_sImportTypePrefix = $sType;
216  }
217 
223  public function getImportObjectsList()
224  {
225  foreach ($this->_aObjects as $sKey => $sImportType) {
226  $oType = $this->_getInstanceOfType($sImportType);
227  $aList[$sKey] = $oType->getBaseTableName();
228  }
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 
273  public function setCsvFileFieldsOrder($aCsvFields)
274  {
275  $this->_aCsvFileFieldsOrder = $aCsvFields;
276  }
277 
283  public function setCsvContainsHeader($blCsvContainsHeader)
284  {
285  $this->_blCsvContainsHeader = $blCsvContainsHeader;
286  }
287 
293  public function getTotalImportedRowsNumber()
294  {
295  return $this->getImportedRowCount();
296  }
297 
310  public function doImport($sPath = null, $sUserName = null, $sUserPassword = null, $sShopId = null, $sShopLanguage = null)
311  {
313  $mySession = oxRegistry::getSession();
314 
315  $this->_sReturn = "";
316  $iMaxLineLength = 8192; //TODO change
317 
318  $this->_sPath = $sPath;
319 
320  //init with given data
321  try {
322  $this->init(null, null);
323  } catch (Exception $ex) {
324  return $this->_sReturn = 'ERPGENIMPORT_ERROR_USER_NO_RIGHTS';
325  }
326 
327  $file = @fopen($this->_sPath, "r");
328 
329  if (isset($file) && $file) {
330  $iRow = 0;
331  $aRow = array();
332 
333  while (($aRow = fgetcsv($file, $iMaxLineLength, $this->_getCsvFieldsTerminator(), $this->_getCsvFieldsEncolser())) !== false) {
334 
335  $this->_aData[] = $aRow;
336  }
337 
338  if ($this->_blCsvContainsHeader) {
339  //skipping first row - it's header
340  array_shift($this->_aData);
341  }
342 
343  try {
344  $this->Import();
345  } catch (Exception $ex) {
346  echo $ex->getMessage();
347  $this->_sReturn = 'ERPGENIMPORT_ERROR_DURING_IMPORT';
348  }
349 
350  } else {
351  $this->_sReturn = 'ERPGENIMPORT_ERROR_WRONG_FILE';
352  }
353 
354  @fclose($file);
355 
356  return $this->_sReturn;
357  }
358 
364  protected function _getCsvFieldsTerminator()
365  {
367 
368  $sChar = $myConfig->getConfigParam('sGiCsvFieldTerminator');
369 
370  if (!$sChar) {
371  $sChar = $myConfig->getConfigParam('sCSVSign');
372  }
373  if (!$sChar) {
375  }
376 
377  return $sChar;
378  }
379 
385  protected function _getCsvFieldsEncolser()
386  {
388 
389  if ($sChar = $myConfig->getConfigParam('sGiCsvFieldEncloser')) {
390  return $sChar;
391  } else {
393  }
394  }
395 }