OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxerpbase.php
Go to the documentation of this file.
1 <?php
2 
6 abstract class oxERPBase
7 {
8 
9  const ERROR_USER_WRONG = "ERROR: Could not login";
10  const ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
11  const ERROR_USER_EXISTS = "ERROR: User already exists";
12  const ERROR_NO_INIT = "Init not executed, Access denied!";
13  const ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
14  const ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
15  const ERROR_ERP_VERSION_NOT_SUPPORTED_BY_SHOP = "ERROR: shop does not support requested ERP version.";
16  const ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP = "ERROR: ERP does not support current shop version.";
17 
18  public static $MODE_IMPORT = "Import";
19  public static $MODE_DELETE = "Delete";
20 
21  protected $_blInit = false;
22  protected $_iLanguage = null;
23  protected $_sUserID = null;
24  //session id
25  protected $_sSID = null;
26 
27  protected static $_sRequestedVersion = '';
28 
41  protected static $_aDbLayer2ShopDbVersions = array(
42  '2.9.0' => '8', // added new fields to oxcategories, oxorderarticle
43  );
44 
50  protected $_aImportedIds = array();
51 
57  protected $_iImportedRowCount = 0;
58 
59  public $_aStatistics = array();
60  public $_iIdx = 0;
61 
66  abstract public function getImportedRowCount();
67 
74  abstract public function setImportedIds($key);
75 
81  public function getStatistics()
82  {
83  return $this->_aStatistics;
84  }
85 
91  public function getSessionID()
92  {
93  return $this->_sSID;
94  }
95 
103  abstract protected function _beforeExport($sType);
104 
112  abstract protected function _afterExport($sType);
113 
119  abstract protected function _beforeImport();
120 
126  abstract protected function _afterImport();
127 
135  abstract public function getImportData($iIdx = null);
136 
144  abstract protected function _getImportType(&$aData);
145 
153  abstract protected function _getImportMode($aData);
154 
163  abstract protected function _modifyData($aData, $oType);
164 
173  public function __call($sMethod, $aArguments)
174  {
175  throw new Exception("ERROR: Handler for Object '$sMethod' not implemented!");
176  }
177 
178 
179  // -------------------------------------------------------------------------
180  //
181  // public interface
182  //
183  // -------------------------------------------------------------------------
184 
185 
197  public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
198  {
199  ini_set('session.use_cookies', 0);
200  $_COOKIE = array('admin_sid' => false);
202  $myConfig->setConfigParam('blForceSessionStart', 1);
203  $myConfig->setConfigParam('blSessionUseCookies', 0);
204  $myConfig->setConfigParam('blAdmin', 1);
205  $myConfig->setAdminMode(true);
206 
207  $mySession = oxRegistry::getSession();
208  @$mySession->start();
209 
210 
211  oxRegistry::getSession()->setVariable("lang", $iLanguage);
212  oxRegistry::getSession()->setVariable("language", $iLanguage);
213 
214  $oUser = oxNew('oxuser');
215  try {
216  if (!$oUser->login($sUserName, $sPassword)) {
217  $oUser = null;
218  }
219  } catch (oxUserException $e) {
220  $oUser = null;
221  }
222 
224 
225  if (!$oUser || (isset($oUser->iError) && $oUser->iError == -1000)) {
226  // authorization error
227  throw new Exception(self::ERROR_USER_WRONG);
228  } elseif (($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID())) {
229  $this->_sSID = $mySession->getId();
230  $this->_blInit = true;
231  $this->_iLanguage = $iLanguage;
232  $this->_sUserID = $oUser->getId();
233  //$mySession->freeze();
234  } else {
235 
236  //user does not have sufficient rights for shop
237  throw new Exception(self::ERROR_USER_NO_RIGHTS);
238  }
239 
240  $this->_resetIdx();
241 
242  return $this->_blInit;
243  }
244 
251  public function loadSessionData($sSessionID)
252  {
253  if (!$sSessionID) {
254  throw new Exception("ERROR: Session ID not valid!");
255  }
256  $_COOKIE = array('admin_sid' => $sSessionID);
257  // start session
259  $myConfig->setConfigParam('blAdmin', 1);
260  $myConfig->setAdminMode(true);
261  $mySession = oxRegistry::getSession();
262 
263  // change session if needed
264  if ($sSessionID != session_id()) {
265  if (session_id()) {
266  session_write_close();
267  }
268  session_id($sSessionID);
269  session_start();
270  }
271 
272  $sAuth = $mySession->getVariable('auth');
273 
274  if (!isset($sAuth) || !$sAuth) {
275  throw new Exception("ERROR: Session ID not valid!");
276  }
277 
278  $this->_iLanguage = $mySession->getVariable('lang');
279  $this->_sUserID = $sAuth;
280 
281 
282  $this->_blInit = true;
283  }
284 
295  public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
296  {
297  $this->_beforeExport($sType);
298  $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
299  $this->_afterExport($sType);
300  }
301 
305  public function import()
306  {
307  $this->_beforeImport();
308  while ($this->_importOne()) {
309  }
310  $this->_afterImport();
311  }
312 
320  protected function _getInstanceOfType($sType)
321  {
322  $sClassName = 'oxerptype_' . $sType;
323  $sFullPath = dirname(__FILE__) . '/objects/' . $sClassName . '.php';
324 
325  if (!file_exists($sFullPath)) {
326  throw new Exception("Type $sType not supported in ERP interface!");
327  }
328 
329  include_once $sFullPath;
330 
331  //return new $sClassName;
332  return oxNew($sClassName);
333  }
334 
346  protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
347  {
348  global $ADODB_FETCH_MODE;
349 
351  // prepare
352  $oType = $this->_getInstanceOfType($sType);
353  //$sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $this->_iShopID);
354  $sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
355  $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
356  $sFnc = '_Export' . $oType->getFunctionSuffix();
357 
358  $save = $ADODB_FETCH_MODE;
359 
361  if (isset($iCount) || isset($iStart)) {
362  $rs = $oDb->selectLimit($sSQL, $iCount, $iStart);
363  } else {
364  $rs = $oDb->select($sSQL);
365  }
366 
367  if ($rs != false && $rs->recordCount() > 0) {
368  while (!$rs->EOF) {
369  $blExport = false;
370  $sMessage = '';
371 
372  $rs->fields = $oType->addExportData($rs->fields);
373 
374  // check rights
375  $this->_checkAccess($oType, false);
376 
377  // export now
378  try {
379  $blExport = $this->$sFnc($rs->fields);
380  } catch (Exception $e) {
381  $sMessage = $e->getMessage();
382 
383  }
384 
385  $this->_aStatistics[$this->_iIdx] = array('r' => $blExport, 'm' => $sMessage);
386  //#2428 MAFI
387  $this->_nextIdx();
388 
389  $rs->moveNext();
390  }
391  }
392  $ADODB_FETCH_MODE = $save;
393  }
394 
400  protected function _outputMappingArray($sTable)
401  {
402  $aData = GetTableDescription($sTable);
403 
404  $iIdx = 0;
405  foreach ($aData as $key => $oADODBField) {
406  if (!(is_numeric(substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
407  echo("'" . $oADODBField->name . "'\t\t => '" . $oADODBField->name . "',\n");
408  $iIdx++;
409  }
410  }
411  }
412 
421  protected function _getKeyID($oType, $aData)
422  {
423  $sOXID = $oType->getOxidFromKeyFields($aData);
424  if (isset($sOXID)) {
425  // note: also pass false here
426  return $sOXID;
427  }
428 
429  return oxUtilsObject::getInstance()->generateUID();
430  }
431 
435  protected function _resetIdx()
436  {
437  $this->_iIdx = 0;
438 
439  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
440  while (isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r']) {
441  $this->_iIdx++;
442  }
443  }
444  }
445 
449  protected function _nextIdx()
450  {
451  $this->_iIdx++;
452 
453  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
454  while (isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r']) {
455  $this->_iIdx++;
456  }
457  }
458  }
459 
467  protected function _checkAccess($oType, $blWrite, $sOxid = null)
468  {
470  static $aAccessCache;
471 
472  if (!$this->_blInit) {
473  throw new Exception(self::ERROR_NO_INIT);
474  }
475 
476  }
477 
488  protected function _importOne()
489  {
490  $blRet = false;
491 
492  // import one row/call/object...
493  $aData = $this->getImportData();
494 
495  if ($aData) {
496  $blRet = true;
497  $blImport = false;
498  $sMessage = '';
499 
500  $sType = $this->_getImportType($aData);
501  $sMode = $this->_getImportMode($aData);
502  $oType = $this->_getInstanceOfType($sType);
503  $aData = $this->_modifyData($aData, $oType);
504 
505  // import now
506  $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
507 
508  if ($sMode == oxERPBase::$MODE_IMPORT) {
509  $aData = $oType->addImportData($aData);
510  }
511 
512  try {
513  $iId = $this->$sFnc($oType, $aData);
514  if (!$iId) {
515  $blImport = false;
516  } else {
517  $this->setImportedIds($iId);
518  $blImport = true;
519  }
520  $sMessage = '';
521  } catch (Exception $e) {
522  $sMessage = $e->getMessage();
523  }
524 
525  $this->_aStatistics[$this->_iIdx] = array('r' => $blImport, 'm' => $sMessage);
526 
527  }
528  //hotfix #2428 MAFI
529  $this->_nextIdx();
530 
531  return $blRet;
532  }
533 
534 
544  protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
545  {
547 
548  // check rights
549  $sOxid = null;
550  if (isset($aData['OXID'])) {
551  $sOxid = $aData['OXID'];
552  }
553  $this->_checkAccess($oType, true, $sOxid);
554 
555  return $oType->saveObject($aData, $blAllowCustomShopId);
556  }
557 
565  protected static function _checkShopVersion()
566  {
568  if (method_exists($myConfig, 'getSerial')) {
569  if ($myConfig->getSerial() instanceof oxSerial) {
570  return;
571  }
572  }
573  throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
574  }
575 
583  protected static function _checkRequestedVersion()
584  {
585  return true;
586  }
587 
595  public static function getRequestedVersion()
596  {
597  if (!self::$_sRequestedVersion) {
599  }
600 
602  }
603 
609  public static function getUsedDbFieldsVersion()
610  {
611  return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
612  }
613 
621  public static function setVersion($sDbLayerVersion = '')
622  {
623  $sDbLayerVersion = '2.9.0';
624  self::$_sRequestedVersion = $sDbLayerVersion;
626  }
627 
635  public function createPluginObject($sId)
636  {
637  $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
638  if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
639  // fix possible case changes
640  $sClassName = $m[1] . 'Plugin';
641  } else {
642  throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
643  }
644 
645  $sFileName = dirname(__FILE__) . '/plugins/' . strtolower($sClassName) . '.php';
646  if (!is_readable($sFileName)) {
647  $sFileName = basename($sFileName);
648  throw new Exception("Can not find the requested plugin file ('$sFileName').");
649  }
650  include_once dirname(__FILE__) . '/plugins/oxerppluginbase.php';
651  include_once $sFileName;
652  if (!class_exists($sClassName)) {
653  throw new Exception("Can not find the requested plugin class.");
654  }
655  $o = new $sClassName();
656  if ($o instanceof oxErpPluginBase) {
657  return $o;
658  }
659  throw new Exception("Plugin does not extend oxErpPluginBase class.");
660  }
661 }