OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxerpbase.php
Go to the documentation of this file.
1 <?php
2 
6 abstract class oxERPBase
7 {
8  const ERROR_USER_WRONG = "ERROR: Could not login";
9  const ERROR_USER_NO_RIGHTS = "Not sufficient rights to perform operation!";
10  const ERROR_USER_EXISTS = "ERROR: User already exists";
11  const ERROR_NO_INIT = "Init not executed, Access denied!";
12  const ERROR_DELETE_NO_EMPTY_CATEGORY = "Only empty category can be deleated";
13  const ERROR_OBJECT_NOT_EXISTING = "Object does not exist";
14  const ERROR_ERP_VERSION_NOT_SUPPORTED_BY_SHOP = "ERROR: shop does not support requested ERP version.";
15  const ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP = "ERROR: ERP does not support current shop version.";
16 
17  static $MODE_IMPORT = "Import";
18  static $MODE_DELETE = "Delete";
19 
20  protected $_blInit = false;
21  protected $_iLanguage = null;
22  protected $_sUserID = null;
23  //session id
24  protected $_sSID = null;
25 
26  protected static $_sRequestedVersion = '';
27 
40  protected static $_aDbLayer2ShopDbVersions = array(
41  '2.9.0' => '8', // added new fields to oxcategories, oxorderarticle
42  );
43 
48  protected $_aImportedIds = array();
49 
54  protected $_iImportedRowCount = 0;
55 
56  public $_aStatistics = array();
57  public $_iIdx = 0;
58 
63  public abstract function getImportedRowCount();
64 
71  public abstract function setImportedIds( $key );
77  public function getStatistics()
78  {
79  return $this->_aStatistics;
80  }
81 
87  public function getSessionID()
88  {
89  return $this->_sSID;
90  }
91 
99  protected abstract function _beforeExport($sType);
100 
108  protected abstract function _afterExport($sType);
109 
115  protected abstract function _beforeImport();
116 
122  protected abstract function _afterImport();
123 
131  public abstract function getImportData($iIdx = null);
132 
140  protected abstract function _getImportType(&$aData);
141 
149  protected abstract function _getImportMode($aData);
150 
159  protected abstract function _modifyData($aData, $oType);
160 
171  public function __call($sMethod, $aArguments)
172  {
173  throw new Exception( "ERROR: Handler for Object '$sMethod' not implemented!");
174  }
175 
176 
177  // -------------------------------------------------------------------------
178  //
179  // public interface
180  //
181  // -------------------------------------------------------------------------
182 
183 
195  public function init($sUserName, $sPassword, $iShopID = 1, $iLanguage = 0)
196  {
197  ini_set('session.use_cookies', 0);
198  $_COOKIE = array('admin_sid' => false);
200  $myConfig->setConfigParam( 'blForceSessionStart', 1 );
201  $myConfig->setConfigParam( 'blSessionUseCookies', 0);
202  $myConfig->setConfigParam( 'blAdmin', 1 );
203  $myConfig->setAdminMode( true );
204 
205  $mySession = oxRegistry::getSession();
206  @$mySession->start();
207 
208 
209  oxSession::setVar( "lang", $iLanguage);
210  oxSession::setVar( "language", $iLanguage);
211 
212  $oUser = oxNew('oxuser');
213  try {
214  if (!$oUser->login($sUserName, $sPassword)) {
215  $oUser = null;
216  }
217  }catch(oxUserException $e) {
218  $oUser = null;
219  }
220 
222 
223  if ( !$oUser || ( isset($oUser->iError) && $oUser->iError == -1000)) {
224  // authorization error
225  throw new Exception( self::ERROR_USER_WRONG );
226  } elseif ( ($oUser->oxuser__oxrights->value == "malladmin" || $oUser->oxuser__oxrights->value == $myConfig->getShopID()) ) {
227  $this->_sSID = $mySession->getId();
228  $this->_blInit = true;
229  $this->_iLanguage = $iLanguage;
230  $this->_sUserID = $oUser->getId();
231  //$mySession->freeze();
232  } else {
233 
234  //user does not have sufficient rights for shop
235  throw new Exception( self::ERROR_USER_NO_RIGHTS );
236  }
237 
238  $this->_resetIdx();
239 
240  return $this->_blInit;
241  }
242 
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->getVar('auth');
273 
274  if (!isset($sAuth) || !$sAuth) {
275  throw new Exception( "ERROR: Session ID not valid!");
276  }
277 
278  $this->_iLanguage = $mySession->getVar('lang');
279  $this->_sUserID = $sAuth;
280 
281 
282  $this->_blInit = true;
283  }
284 
297  public function exportType($sType, $sWhere = null, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
298  {
299  $this->_beforeExport($sType);
300  $this->_export($sType, $sWhere, $iStart, $iCount, $sSortFieldName, $sSortType);
301  $this->_afterExport($sType);
302  }
303 
309  public function import()
310  {
311  $this->_beforeImport();
312  while ($this->_importOne()) {
313  }
314  $this->_afterImport();
315  }
316 
324  protected function _getInstanceOfType($sType)
325  {
326  $sClassName = 'oxerptype_'.$sType;
327  $sFullPath = dirname(__FILE__).'/objects/'.$sClassName.'.php';
328 
329  if ( !file_exists($sFullPath)) {
330  throw new Exception( "Type $sType not supported in ERP interface!");
331  }
332 
333  include_once $sFullPath;
334 
335  //return new $sClassName;
336  return oxNew ($sClassName);
337  }
338 
352  protected function _export($sType, $sWhere, $iStart = null, $iCount = null, $sSortFieldName = null, $sSortType = null)
353  {
354  global $ADODB_FETCH_MODE;
355 
357  // prepare
358  $oType = $this->_getInstanceOfType($sType);
359  //$sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $this->_iShopID);
360  $sSQL = $oType->getSQL($sWhere, $this->_iLanguage, $myConfig->getShopId());
361  $sSQL .= $oType->getSortString($sSortFieldName, $sSortType);
362  $sFnc = '_Export'.$oType->getFunctionSuffix();
363 
364  $save = $ADODB_FETCH_MODE;
365 
367  if (isset($iCount) || isset($iStart)) {
368  $rs = $oDb->selectLimit( $sSQL, $iCount, $iStart );
369  } else {
370  $rs = $oDb->select( $sSQL );
371  }
372 
373  if ($rs != false && $rs->recordCount() > 0) {
374  while (!$rs->EOF) {
375  $blExport = false;
376  $sMessage = '';
377 
378  $rs->fields = $oType->addExportData($rs->fields);
379 
380  // check rights
381  $this->_checkAccess($oType, false);
382 
383  // export now
384  try{
385  $blExport = $this->$sFnc($rs->fields );
386  } catch (Exception $e) {
387  $sMessage = $e->getMessage();
388 
389  }
390 
391  $this->_aStatistics[$this->_iIdx] = array('r'=>$blExport,'m'=>$sMessage);
392  //#2428 MAFI
393  $this->_nextIdx();
394 
395  $rs->moveNext();
396  }
397  }
398  $ADODB_FETCH_MODE = $save;
399  }
400 
408  protected function _outputMappingArray($sTable)
409  {
410  $aData = GetTableDescription($sTable);
411 
412  $iIdx = 0;
413  foreach ($aData as $key => $oADODBField) {
414  if ( !(is_numeric( substr($oADODBField->name, strlen($oADODBField->name) - 1, 1)) && substr($oADODBField->name, strlen($oADODBField->name) - 2, 1) == '_')) {
415  echo( "'".$oADODBField->name."'\t\t => '".$oADODBField->name."',\n");
416  $iIdx++;
417  }
418  }
419  }
420 
429  protected function _getKeyID($oType, $aData)
430  {
431  $sOXID = $oType->getOxidFromKeyFields($aData);
432  if (isset($sOXID)) {
433  // note: also pass false here
434  return $sOXID;
435  }
436  return oxUtilsObject::getInstance()->generateUID();
437  }
438 
444  protected function _resetIdx()
445  {
446  $this->_iIdx = 0;
447 
448  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
449  while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
450  $this->_iIdx ++;
451  }
452  }
453  }
454 
460  protected function _nextIdx()
461  {
462  $this->_iIdx ++;
463 
464  if (count($this->_aStatistics) && isset($this->_aStatistics[$this->_iIdx])) {
465  while ( isset($this->_aStatistics[$this->_iIdx]) && $this->_aStatistics[$this->_iIdx]['r'] ) {
466  $this->_iIdx ++;
467  }
468  }
469  }
470 
480  protected function _checkAccess($oType, $blWrite, $sOxid = null)
481  {
483  static $aAccessCache;
484 
485  if (!$this->_blInit) {
486  throw new Exception(self::ERROR_NO_INIT);
487  }
488 
489  }
490 
501  protected function _importOne()
502  {
503  $blRet = false;
504 
505  // import one row/call/object...
506  $aData = $this->getImportData();
507 
508  if ($aData) {
509  $blRet = true;
510  $blImport = false;
511  $sMessage = '';
512 
513  $sType = $this->_getImportType($aData);
514  $sMode = $this->_getImportMode($aData);
515  $oType = $this->_getInstanceOfType($sType);
516  $aData = $this->_modifyData($aData, $oType);
517 
518  // import now
519  $sFnc = '_' . $sMode . $oType->getFunctionSuffix();
520 
521  if ($sMode == oxERPBase::$MODE_IMPORT) {
522  $aData = $oType->addImportData($aData);
523  }
524 
525  try {
526  $iId = $this->$sFnc($oType, $aData);
527  if ( !$iId )
528  $blImport = false;
529  else {
530  $this->setImportedIds( $iId );
531  $blImport = true;
532  }
533  $sMessage = '';
534  } catch (Exception $e) {
535  $sMessage = $e->getMessage();
536  }
537 
538  $this->_aStatistics[$this->_iIdx] = array('r'=>$blImport,'m'=>$sMessage);
539 
540  }
541  //hotfix #2428 MAFI
542  $this->_nextIdx();
543 
544  return $blRet;
545  }
546 
547 
557  protected function _save(oxERPType &$oType, $aData, $blAllowCustomShopId = false)
558  {
560 
561  // check rights
562  $sOxid = null;
563  if (isset($aData['OXID'])) {
564  $sOxid = $aData['OXID'];
565  }
566  $this->_checkAccess($oType, true, $sOxid);
567 
568  return $oType->saveObject($aData, $blAllowCustomShopId);
569  }
570 
578  protected static function _checkShopVersion()
579  {
581  if ( method_exists($myConfig, 'getSerial') ) {
582  if ($myConfig->getSerial() instanceof oxSerial) {
583  return;
584  }
585  }
586  throw new Exception(self::ERROR_SHOP_VERSION_NOT_SUPPORTED_BY_ERP);
587  }
588 
596  protected static function _checkRequestedVersion()
597  {
598  return true;
599  }
600 
608  public static function getRequestedVersion()
609  {
610  if (!self::$_sRequestedVersion) {
612  }
614  }
615 
621  public static function getUsedDbFieldsVersion()
622  {
623  return self::$_aDbLayer2ShopDbVersions[self::getRequestedVersion()];
624  }
625 
635  public static function setVersion($sDbLayerVersion = '')
636  {
637  $sDbLayerVersion = '2.9.0';
638  self::$_sRequestedVersion = $sDbLayerVersion;
640  }
641 
649  public function createPluginObject($sId)
650  {
651  $sClassName = preg_replace('/[^a-z0-9_]/i', '', $sId);
652  if (preg_match('/(.*)Plugin$/i', $sClassName, $m)) {
653  // fix possible case changes
654  $sClassName = $m[1].'Plugin';
655  } else {
656  throw new Exception("Plugin handler class has to end with 'Plugin' word (GOT '$sClassName').");
657  }
658 
659  $sFileName = dirname(__FILE__).'/plugins/'.strtolower($sClassName).'.php';
660  if (!is_readable($sFileName)) {
661  $sFileName = basename($sFileName);
662  throw new Exception("Can not find the requested plugin file ('$sFileName').");
663  }
664  include_once dirname(__FILE__).'/plugins/oxerppluginbase.php';
665  include_once $sFileName;
666  if (!class_exists($sClassName)) {
667  throw new Exception("Can not find the requested plugin class.");
668  }
669  $o = new $sClassName();
670  if ($o instanceof oxErpPluginBase) {
671  return $o;
672  }
673  throw new Exception("Plugin does not extend oxErpPluginBase class.");
674  }
675 }
676